1 # Copyright (C) 2009-2015 EDF R&D
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 Module that defines CPPComponent for SALOME components implemented in C++
25 from gener import Component, Invalid
26 from cpp_tmpl import initService, cxxService, hxxCompo, cxxCompo
27 from cpp_tmpl import exeCPP, cmake_src_compo_cpp
28 from yacstypes import corba_rtn_type
31 from string import Template
33 from compat import Template,set
35 class CPPComponent(Component):
37 A :class:`CPPComponent` instance represents a C++ SALOME component with services given as a list of :class:`Service`
38 instances with the parameter *services*.
40 :param name: gives the name of the component.
42 :param services: the list of services (:class:`Service`) of the component.
43 :param kind: If it is given and has the value "exe", the component will be built as a standalone
44 component (executable or shell script). The default is to build the component as a dynamic library.
45 :param libs: list of the additional libraries. see *Library* class.
46 If you want to add "libmylib.so", installed in "/path/to/lib" you should use:
47 libs=[Library(name="mylib", path="/path/to/lib")]
48 For more advanced features, see the documentation of cmake / FIND_LIBRARY
49 :param rlibs: space-separated list specifying the rpath to use in installed targets
50 :param includes: additional include directories, separated by spaces.
51 :param sources: gives all the external source files to add in the compilation step (list of paths).
52 :param exe_path: is only used when kind is "exe" and gives the path to the standalone component.
53 :param compodefs: can be used to add extra definition code in the component for example when using a base class
54 to define the component class by deriving it (see *inheritedclass* parameter)
55 :param inheritedclass: can be used to define a base class for the component. The base class can be defined in external
56 source or with the *compodefs* parameter. The value of the *inheritedclass* parameter is the name of the base class.
57 :param idls: can be used to add extra idl CORBA interfaces to the component. This parameter must gives a list of idl file
58 names that are added into the generated module (idl directory) and compiled with the generated idl of the module.
59 :param interfacedefs: can be used to add idl definitions (or includes of idl files) into the generated idl of the module.
60 :param inheritedinterface: can be used to make the component inherit an extra idl interface that has been included through
61 the *idls* and *interfacedefs* parameters. See the cppgui1 example for how to use these last parameters.
62 :param addedmethods: is a C++ specific parameter that can be used to redefine a component method (DumpPython for example). This
63 parameter is a string that must contain the definition and implementation code of the method. See the cppgui1 example
65 :param calciumextendedinterface: if you want to use the Calcium extended interface for C++ as defined by the header CalciumInterface.hxx
66 set this parameter to 1. By default its value is 0 so to not use extended interface. The extended interface requires boost as a dependency.
68 For example, the following call defines a standalone component named "mycompo" with one service s1 (it must have been defined before)::
70 >>> c1 = module_generator.CPPComponent('mycompo', services=[s1,], kind="exe",
71 exe_path="./launch.sh")
73 def __init__(self, name, services=None, libs=[], rlibs="", includes="", kind="lib",
74 exe_path=None, sources=None, inheritedclass="", compodefs="",
75 idls=None,interfacedefs="",inheritedinterface="",addedmethods="",
76 calciumextendedinterface=0):
77 self.exe_path = exe_path
78 self.calciumextendedinterface=calciumextendedinterface
79 Component.__init__(self, name, services, impl="CPP", libs=libs, rlibs=rlibs,
80 includes=includes, kind=kind, sources=sources,
81 inheritedclass=inheritedclass, compodefs=compodefs, idls=idls,
82 interfacedefs=interfacedefs, inheritedinterface=inheritedinterface,
83 addedmethods=addedmethods)
86 """ validate component definition parameters"""
87 Component.validate(self)
88 kinds = ("lib", "exe")
89 if self.kind not in kinds:
90 raise Invalid("kind must be one of %s for component %s" % (kinds,self.name))
92 if self.kind == "exe" :
94 raise Invalid("exe_path must be defined for component %s" % self.name)
96 def targetProperties(self):
97 """ define the rpath property of the target using self.rlibs
99 string containing the commands to add to cmake
102 if self.rlibs.strip() :
103 text="SET_TARGET_PROPERTIES( %sEngine PROPERTIES INSTALL_RPATH %s)\n" % (self.name, self.rlibs)
106 def libraryName(self):
107 """ Name of the target library
110 if self.kind == "lib":
111 ret = self.name + "Engine"
112 elif self.kind == "exe":
113 ret = self.name + "Exelib"
115 raise Invalid("Invalid kind of component: %s. Supported kinds are 'lib' and 'exe'" % self.name)
118 def makeCompo(self, gen):
119 """generate files for C++ component
121 return a dict where key is the file name and value is the file content
123 (cmake_text, cmake_vars) = self.additionalLibraries()
124 cxxfile = "%s.cxx" % self.name
125 hxxfile = "%s.hxx" % self.name
126 if self.kind == "exe":
130 ret = { cxxfile:self.makecxx(gen, exe_opt),
131 hxxfile:self.makehxx(gen)
133 sources = " ".join(map(os.path.basename,self.sources))
134 cmakelist_content = cmake_src_compo_cpp.substitute(
135 module = gen.module.name,
136 component = self.name,
137 componentlib = self.libraryName(),
138 includes = self.includes,
141 find_libs = cmake_text,
142 target_properties = self.targetProperties())
143 if self.kind == "exe":
144 exe_file = self.name+".exe"
145 install_commande = "\nINSTALL(PROGRAMS %s DESTINATION ${SALOME_INSTALL_BINS})\n" % exe_file
146 cmakelist_content = cmakelist_content + install_commande
147 ret[exe_file] = exeCPP.substitute(compoexe=self.exe_path)
150 ret["CMakeLists.txt"] = cmakelist_content
154 def makehxx(self, gen):
155 """return a string that is the content of .hxx file
158 for serv in self.services:
159 service = " %s %s(" % (corba_rtn_type(serv.ret,gen.module.name),serv.name)
160 service = service+gen.makeArgs(serv)+");"
161 services.append(service)
163 if self.addedmethods:
164 services.append(self.addedmethods)
165 servicesdef = "\n".join(services)
167 inheritedclass=self.inheritedclass
168 if self.inheritedclass:
169 inheritedclass= " public virtual " + self.inheritedclass + ","
171 return hxxCompo.substitute(component=self.name, module=gen.module.name,
172 servicesdef=servicesdef, inheritedclass=inheritedclass,
173 compodefs=self.compodefs)
175 def makecxx(self, gen, exe=0):
176 """return a string that is the content of .cxx file
181 for serv in self.services:
182 defs.append(serv.defs)
183 service = cxxService.substitute(component=self.name, service=serv.name,
184 parameters=gen.makeArgs(serv),
185 body=serv.body, exe=exe)
187 for name, typ, dep in serv.instream:
188 streams.append(' create_calcium_port(this,(char *)"%s",(char *)"%s",(char *)"IN",(char *)"%s");'% (name, typ, dep))
189 instream = "\n".join(streams)
191 for name, typ, dep in serv.outstream:
192 streams.append(' create_calcium_port(this,(char *)"%s",(char *)"%s",(char *)"OUT",(char *)"%s");'% (name, typ, dep))
193 outstream = "\n".join(streams)
195 init = initService.substitute(component=self.name, service=serv.name,
196 instream=instream, outstream=outstream)
197 services.append(service)
201 if self.calciumextendedinterface:
202 CalciumInterface="#include <CalciumInterface.hxx>"
204 return cxxCompo.substitute(component=self.name, module=gen.module.name,
205 exe=exe, exe_path=self.exe_path,
206 servicesdef="\n".join(defs),
207 servicesimpl="\n".join(services),
208 initservice='\n'.join(inits),
209 CalciumInterface=CalciumInterface)