andrej@1731: #!/usr/bin/env python
andrej@1731: # -*- coding: utf-8 -*-
andrej@1731: 
andrej@1731: # This file is part of Beremiz, a Integrated Development Environment for
andrej@1731: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
andrej@1731: #
andrej@1731: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
andrej@1731: # Copyright (C) 2017: Andrey Skvortsov
andrej@1731: #
andrej@1731: # See COPYING file for copyrights details.
andrej@1731: #
andrej@1731: # This program is free software; you can redistribute it and/or
andrej@1731: # modify it under the terms of the GNU General Public License
andrej@1731: # as published by the Free Software Foundation; either version 2
andrej@1731: # of the License, or (at your option) any later version.
andrej@1731: #
andrej@1731: # This program is distributed in the hope that it will be useful,
andrej@1731: # but WITHOUT ANY WARRANTY; without even the implied warranty of
andrej@1731: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
andrej@1731: # GNU General Public License for more details.
andrej@1731: #
andrej@1731: # You should have received a copy of the GNU General Public License
andrej@1731: # along with this program; if not, write to the Free Software
andrej@1731: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
andrej@1731: 
andrej@1731: # Package initialisation
andrej@1782: 
andrej@1731: 
andrej@1731: """
andrej@1731: Beremiz Targets
andrej@1731: 
andrej@1731: - Target are python packages, containing at least one "XSD" file
andrej@1731: - Target class may inherit from a toolchain_(toolchainname)
andrej@1731: - The target folder's name must match to name define in the XSD for TargetType
andrej@1731: """
andrej@1731: 
andrej@1731: from os import listdir, path
andrej@1731: import util.paths as paths
andrej@1731: 
andrej@1731: _base_path = paths.AbsDir(__file__)
andrej@1736: 
andrej@1736: 
andrej@1731: def _GetLocalTargetClassFactory(name):
andrej@1740:     return lambda: getattr(__import__(name, globals(), locals()), name+"_target")
andrej@1731: 
andrej@1749: 
andrej@1767: targets = dict([(name, {"xsd":   path.join(_base_path, name, "XSD"),
andrej@1740:                         "class": _GetLocalTargetClassFactory(name),
andrej@1767:                         "code":  {fname: path.join(_base_path, name, fname)
andrej@1767:                                   for fname in listdir(path.join(_base_path, name))
andrej@1767:                                   if (fname.startswith("plc_%s_main" % name) and
andrej@1767:                                       fname.endswith(".c"))}})
andrej@1731:                 for name in listdir(_base_path)
andrej@1785:                 if (path.isdir(path.join(_base_path, name)) and
andrej@1785:                     not name.startswith("__"))])
andrej@1731: 
andrej@1731: toolchains = {"gcc":  path.join(_base_path, "XSD_toolchain_gcc"),
andrej@1731:               "makefile":  path.join(_base_path, "XSD_toolchain_makefile")}
andrej@1731: 
andrej@1736: 
andrej@1731: def GetBuilder(targetname):
andrej@1731:     return targets[targetname]["class"]()
andrej@1731: 
andrej@1736: 
andrej@1731: def GetTargetChoices():
andrej@1731:     DictXSD_toolchain = {}
andrej@1731:     targetchoices = ""
andrej@1731: 
andrej@1731:     # Get all xsd toolchains
andrej@1740:     for toolchainname, xsdfilename in toolchains.iteritems():
andrej@1757:         if path.isfile(xsdfilename):
andrej@1757:             DictXSD_toolchain["toolchain_"+toolchainname] = open(xsdfilename).read()
andrej@1731: 
andrej@1731:     # Get all xsd targets
andrej@1740:     for targetname, nfo in targets.iteritems():
andrej@1731:         xsd_string = open(nfo["xsd"]).read()
andrej@1758:         targetchoices += xsd_string % DictXSD_toolchain
andrej@1731: 
andrej@1731:     return targetchoices
andrej@1731: 
andrej@1736: 
andrej@1731: def GetTargetCode(targetname):
andrej@1731:     codedesc = targets[targetname]["code"]
andrej@1731:     code = "\n".join([open(fpath).read() for fname, fpath in sorted(codedesc.items())])
andrej@1731:     return code
andrej@1731: 
andrej@1736: 
andrej@1731: def GetHeader():
andrej@1740:     filename = paths.AbsNeighbourFile(__file__, "beremiz.h")
andrej@1731:     return open(filename).read()
andrej@1731: 
andrej@1736: 
andrej@1731: def GetCode(name):
andrej@1740:     filename = paths.AbsNeighbourFile(__file__, name)
andrej@1731:     return open(filename).read()