1 # Copyright (C) 2009-2013 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.
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
20 Module that generates SALOME c++ Component from a non SALOME c++ component
21 (its header and its shares library)
28 from tempfile import mkstemp
29 from gener import Component, Invalid
30 from hxx_tmpl import cxxService, hxxCompo, cxxCompo, compoMakefile
31 from module_generator import Service
32 from yacstypes import corba_rtn_type,moduleTypes
33 from hxx_awk import parse01,parse1,parse2,parse3
34 from hxx_awk import cpp2idl_mapping
35 # these tables contain the part of code which depends upon c++ types
36 from hxx_awk import cpp_impl_a,cpp_impl_b,cpp_impl_c
37 from hxx_awk import cpp2yacs_mapping
38 from tempfile import mkdtemp
39 from hxx_tmpl_gui import hxxgui_cxx, hxxgui_h, hxxgui_icon_ts
40 from hxx_tmpl_gui import hxxgui_message_en, hxxgui_message_fr
41 from hxx_tmpl_gui import hxxgui_config, hxxgui_xml_fr, hxxgui_xml_en
43 # ------------------------------------------------------------------------------
45 class HXX2SALOMEComponent(Component):
46 def __init__(self, hxxfile , cpplib , cpp_path ):
47 # search a file within a directory tree
48 def search_file(pattern, root):
50 for path, dirs, files in os.walk(os.path.abspath(root)):
51 for filename in fnmatch.filter(files, pattern):
52 matches.append(os.path.join(path, filename))
55 hxxfileful = search_file(hxxfile,cpp_path)
56 cpplibful = search_file(cpplib,cpp_path)
57 format_error = 'Error in HXX2SALOMEComponent : file %s ot found in %s'
58 assert len(hxxfileful) > 0, format_error % (hxxfile, cpp_path)
59 assert len(cpplibful) > 0, format_error % (cpplib, cpp_path)
60 hxxfile = hxxfileful[0]
63 # grab name of c++ component
64 cmd1="""awk '$1 == "class" && $0 !~ /;/ {print $2}' """ + hxxfile +\
65 """|awk -F: '{printf "%s",$1}' """
67 class_name=f.readlines()[0]
69 print "classname=",class_name
72 # create temporary awk files for the parsing
74 f01=os.fdopen(fd01,"w")
93 # awk parsing of hxx files -
94 # result written in file parse_type_result
98 "sed 's/virtual //g'",
99 "sed 's/MEDMEM_EXPORT//g'",
100 "sed 's/throw.*;/;/g'",
103 "awk -v class_name=%s -f %s" % (class_name, p3n) ]
104 cmd2 = ' | '.join(cmd2)
112 # Retrieve the information which was generated in
113 # the file parse_type_result.
114 # The structure of the file is :
116 # Function return_type function_name
117 # [arg1_type arg1_name]
118 # [arg2_type arg2_name]
120 # The service names are stored in list_of_services
121 # The information relative to a service (called service_name) is stored in
122 # the dictionnary service_definition[service_name]
124 service_definition={}
125 result_parsing=open("parse_type_result","r")
126 for line in result_parsing.readlines():
127 line=line[0:-1] # get rid of trailing \n
128 words = string.split(line,';')
130 if len(words) >=3 and words[0] == "Function": # detect a new service
131 function_name=words[2]
132 # store the name of new service
133 list_of_services.append(function_name)
134 # create a dict to store informations relative to this service
135 service_definition[function_name]={}
136 service_definition[function_name]["ret"]=words[1] # return type
137 service_definition[function_name]["inports"]=[]
138 service_definition[function_name]["outports"]=[]
139 service_definition[function_name]["ports"]=[]
140 service_definition[function_name]["impl"]=[]
142 # an argument type and argument name of the current service
144 current_service=list_of_services[-1]
145 current_service_dict=service_definition[current_service]
148 # store in c++ order the arg names
149 current_service_dict["ports"].append( (argname,typename) )
151 # separate in from out parameters
152 inout=cpp2idl_mapping[typename][0:2]
153 assert inout=="in" or inout=="ou",'Error in table cpp2idl_mapping'
155 current_service_dict["inports"].append((argname, typename) )
157 current_service_dict["outports"].append((argname, typename) )
160 # - generate implementation of c++ servant
161 # - store it in service_definition[serv]["impl"]
162 for serv in list_of_services:
164 print "service : ",serv
165 print " inports -> ",service_definition[serv]["inports"]
166 print " outports -> ",service_definition[serv]["outports"]
167 print " return -> ",service_definition[serv]["ret"]
170 # Part 1 : Argument pre-processing
171 s_argument_processing="//\tArguments processing\n"
172 for (argname,argtype) in service_definition[serv]["inports"] + \
173 service_definition[serv]["outports"]:
174 format=cpp_impl_a[argtype]
175 s_argument_processing += format % {"arg" : argname }
177 # if there was no args
178 if s_argument_processing=="//\tArguments processing\n":
179 s_argument_processing=""
182 # Part 2 : Call to the underlying c++ function
183 s_call_cpp_function="//\tCall cpp component\n\t"
184 rtn_type=service_definition[serv]["ret"]
186 # if return type is void, the call syntax is different
187 if rtn_type == "void" :
188 s_call_cpp_function += "cppCompo_->%s(" % serv
190 s_call_cpp_function +=\
191 "%s _rtn_cpp = cppCompo_->%s(" % (rtn_type ,serv )
193 for (argname,argtype) in service_definition[serv]["ports"]:
194 # special treatment for some arguments
198 if string.find(cpp_impl_a[argtype],"auto_ptr" ) != -1 :
199 # for auto_ptr argument, retrieve the raw pointer behind
201 if argtype == "const MEDMEM::MESH&" or \
202 argtype == "const MEDMEM::SUPPORT&" :
203 # we cannot create MESHClient on the stack
204 # (private constructor!),
205 # so we create it on the heap and dereference it
208 post+="," # separator between arguments
209 s_call_cpp_function += " %s_%s%s" % ( pre,argname,post)
210 if s_call_cpp_function[-1]==',':
211 # get rid of trailing comma
212 s_call_cpp_function=s_call_cpp_function[0:-1]
214 s_call_cpp_function=s_call_cpp_function+');\n'
216 # Part 3.a : Out Argument Post-processing
217 s_argument_postprocessing="//\tPost-processing & return\n"
218 for (argname,argtype) in service_definition[serv]["outports"]:
219 format=cpp_impl_c[argtype]
220 # the treatment of %(module) is postponed in makecxx()
221 # because we don't know here the module name
222 s_argument_postprocessing += \
223 format % {"arg" : argname, "module" : "%(module)s" }
225 # Part 3.b : In Argument Post-processing
226 for (argname,argtype) in service_definition[serv]["inports"]:
227 # not all in types require a treatment
228 if cpp_impl_c.has_key(argtype):
229 format=cpp_impl_c[argtype]
230 # id : treatment of %(module) is postponed in makecxx
231 s_argument_postprocessing += \
232 format % {"arg" : argname, "module" : "%(module)s" }
234 # Part 3.c : return processing
235 s_rtn_processing=cpp_impl_b[rtn_type]
237 format_end_serv = "\tendService(\"%(class_name)s_i::%(serv_name)s\");"
238 format_end_serv += "\n\tEND_OF(\"%(class_name)s_i::%(serv_name)s\");\n"
239 s_rtn_processing += format_end_serv %\
240 { "serv_name" : serv, "class_name" : class_name }
242 if rtn_type != "void":
243 s_rtn_processing += "\treturn _rtn_ior;"
245 service_definition[serv]["impl"] = s_argument_processing + \
246 s_call_cpp_function + \
247 s_argument_postprocessing + \
250 print "implementation :\n",service_definition[serv]["impl"]
253 # Create a list of Service objects (called services),
254 # and give it to Component constructor
257 self.use_medmem=False
258 self.use_medcoupling=False
259 for serv in list_of_services:
260 # for inports and outports, Service class expects a list of tuples,
261 # each tuple containing the name and the yacs type of the port
262 # thus we need to convert c++ types to yacs types
263 # (we use for that the cpp2yacs_mapping table)
265 for op in service_definition[serv]["inports"]:
266 inports.append([op[0], cpp2yacs_mapping[op[1]] ] )
269 for op in service_definition[serv]["outports"]:
270 outports.append([op[0], cpp2yacs_mapping[op[1]] ] )
273 if service_definition[serv]["ret"] != "void":
274 Return=cpp2yacs_mapping[service_definition[serv]["ret"]]
276 # find out if component uses medmem types and/or medcoupling types
277 for (argname,argtype) in inports + outports + [("return",Return)]:
278 if moduleTypes[argtype]=="MED":
279 if argtype.count("Coupling")>0:
280 self.use_medcoupling=True
285 code=service_definition[serv]["impl"]
287 print "service : ",serv
288 print " inports -> ",service_definition[serv]["inports"]
289 print " converted inports -> ",inports
290 print " outports -> ",service_definition[serv]["outports"]
291 print " converted outports -> ",outports
292 print " Return -> ",service_definition[serv]["ret"]
293 print " converted Return -> ",Return
295 services.append(Service(serv,
303 Includes="-I${"+name+"CPP_ROOT_DIR}/include"
304 Libs="-L${"+name+"CPP_ROOT_DIR}/lib -l"+name+"CXX"
307 self.inheritedconstructor=""
310 #include CORBA_CLIENT_HEADER(MED)
311 #include CORBA_CLIENT_HEADER(MED_Gen)
312 #include "FIELDClient.hxx"
313 #include "MESHClient.hxx"
314 #include "MEDMEM_Support_i.hxx"
315 #include "MEDMEM_Mesh_i.hxx"
316 #include "MEDMEM_FieldTemplate_i.hxx"
317 #include "Med_Gen_Driver_i.hxx"
319 Inheritedclass="Med_Gen_Driver_i, public SALOMEMultiComm"
320 self.inheritedconstructor="Med_Gen_Driver_i(orb),"
322 if self.use_medcoupling:
324 #include CORBA_CLIENT_HEADER(MEDCouplingCorbaServant)
325 #include CORBA_CLIENT_HEADER(MED_Gen)
326 #include "MEDCouplingFieldDoubleServant.hxx"
327 #include "MEDCouplingUMeshServant.hxx"
328 #include "MEDCouplingFieldDouble.hxx"
329 #include "MEDCouplingUMesh.hxx"
330 #include "MEDCouplingUMeshClient.hxx"
331 #include "MEDCouplingFieldDouble.hxx"
332 #include "MEDCouplingFieldDoubleClient.hxx"
335 Component.__init__(self, name, services, impl="CPP", libs=Libs,
336 rlibs="", includes=Includes, kind="lib",
337 sources=None,inheritedclass=Inheritedclass,
340 # ------------------------------------------------------------------------------
341 def makeCompo(self, gen):
342 """generate files for C++ component
343 return a dict where key is the file name and
344 value is the content of the file
346 cxxfile = "%s_i.cxx" % self.name
347 hxxfile = "%s_i.hxx" % self.name
348 return {"Makefile.am":gen.makeMakefile(self.getMakefileItems(gen)),
349 cxxfile:self.makecxx(gen),
350 hxxfile:self.makehxx(gen)
353 # ------------------------------------------------------------------------------
354 def getMakefileItems(self,gen):
355 makefileItems={"header":"""
356 include $(top_srcdir)/adm_local/make_common_starter.am
359 makefileItems["lib_LTLIBRARIES"]=["lib"+self.name+"Engine.la"]
360 makefileItems["salomeinclude_HEADERS"]=["%s_i.hxx" % self.name]
361 makefileItems["body"]=compoMakefile.substitute(module=gen.module.name,
364 includes=self.includes)
367 # ------------------------------------------------------------------------------
368 def makehxx(self, gen):
369 """return a string that is the content of .hxx file
372 for serv in self.services:
373 service = " %s %s(" % (corba_rtn_type(serv.ret,gen.module.name),
375 service = service+gen.makeArgs(serv)+") throw (SALOME::SALOME_Exception);"
376 services.append(service)
377 servicesdef = "\n".join(services)
379 inheritedclass=self.inheritedclass
380 if self.inheritedclass:
381 inheritedclass= " public virtual " + self.inheritedclass + ","
383 return hxxCompo.substitute(component=self.name,
384 module=gen.module.name,
385 servicesdef=servicesdef,
386 inheritedclass=inheritedclass,
387 compodefs=self.compodefs)
389 # ------------------------------------------------------------------------------
390 def makecxx(self, gen, exe=0):
391 """return a string that is the content of .cxx file
396 for serv in self.services:
397 defs.append(serv.defs)
398 print "CNC bug : ",serv.body
399 service = cxxService.substitute(
402 ret=corba_rtn_type(serv.ret,gen.module.name),
403 parameters=gen.makeArgs(serv),
404 body=serv.body % {"module":gen.module.name+"_ORB"} )
405 services.append(service)
406 return cxxCompo.substitute(component=self.name,
407 inheritedconstructor=self.inheritedconstructor,
408 servicesdef="\n".join(defs),
409 servicesimpl="\n".join(services))
411 # ------------------------------------------------------------------------------
412 def getGUIfilesTemplate(self):
413 """generate in a temporary directory files for a generic GUI,
414 and return a list with file names.
415 it is the responsability of the user to get rid
416 of the temporary directory when finished
418 gui_cxx=hxxgui_cxx.substitute(component_name=self.name)
419 gui_h=hxxgui_h.substitute(component_name=self.name)
420 gui_icon_ts=hxxgui_icon_ts.substitute(component_name=self.name)
421 gui_message_en=hxxgui_message_en.substitute(component_name=self.name)
422 gui_message_fr=hxxgui_message_fr.substitute(component_name=self.name)
423 gui_config=hxxgui_config.substitute(component_name=self.name)
424 gui_xml_fr=hxxgui_xml_fr.substitute(component_name=self.name)
425 gui_xml_en=hxxgui_xml_en.substitute(component_name=self.name)
427 gui_cxx_file_name=os.path.join(temp_dir,self.name+"GUI.cxx")
428 gui_h_file_name=os.path.join(temp_dir,self.name+"GUI.h")
429 gui_icon_ts_file_name=os.path.join(temp_dir,self.name+"_icons.ts")
430 gui_message_en_file_name=os.path.join(temp_dir,self.name+"_msg_en.ts")
431 gui_message_fr_file_name=os.path.join(temp_dir,self.name+"_msg_fr.ts")
432 gui_config_file_name=os.path.join(temp_dir,"config")
433 gui_xml_fr_file_name=os.path.join(temp_dir,self.name+"_en.xml")
434 gui_xml_en_file_name=os.path.join(temp_dir,self.name+"_fr.xml")
438 gui_cxx_file=open(gui_cxx_file_name,"w")
439 gui_cxx_file.write(gui_cxx)
441 list_of_gui_names.append(gui_cxx_file_name)
443 gui_h_file=open(gui_h_file_name,"w")
444 gui_h_file.write(gui_h)
446 list_of_gui_names.append(gui_h_file_name)
448 gui_icon_ts_file=open(gui_icon_ts_file_name,"w")
449 gui_icon_ts_file.write(gui_icon_ts)
450 gui_icon_ts_file.close()
451 list_of_gui_names.append(gui_icon_ts_file_name)
453 gui_message_en_file=open(gui_message_en_file_name,"w")
454 gui_message_en_file.write(gui_message_en)
455 gui_message_en_file.close()
456 list_of_gui_names.append(gui_message_en_file_name)
458 gui_message_fr_file=open(gui_message_fr_file_name,"w")
459 gui_message_fr_file.write(gui_message_fr)
460 gui_message_fr_file.close()
461 list_of_gui_names.append(gui_message_fr_file_name)
463 gui_config_file=open(gui_config_file_name,"w")
464 gui_config_file.write(gui_config)
465 gui_config_file.close()
466 list_of_gui_names.append(gui_config_file_name)
468 gui_xml_fr_file=open(gui_xml_fr_file_name,"w")
469 gui_xml_fr_file.write(gui_xml_fr)
470 gui_xml_fr_file.close()
471 list_of_gui_names.append(gui_xml_fr_file_name)
473 gui_xml_en_file=open(gui_xml_en_file_name,"w")
474 gui_xml_en_file.write(gui_xml_en)
475 gui_xml_en_file.close()
476 list_of_gui_names.append(gui_xml_en_file_name)
479 return list_of_gui_names