Salome HOME
PAL issue 2757: Traiter de manière masquée lâ\80\99exécution de la commande DEBUT...
[tools/yacsgen.git] / module_generator / pacocompo.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 *-
3 # Copyright (C) 2009-2013  EDF R&D
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
22 #  Author : Andre RIBES (EDF R&D)
23
24 """
25   Module that defines PACOComponent for SALOME PaCO++ components implemented in C++
26 """
27 import os
28 from gener import Component, Invalid
29 from paco_tmpl import compoMakefile, hxxCompo, cxxService
30 from paco_tmpl import initService, cxxCompo, paco_sources
31 from paco_tmpl import cxxFactoryDummy, cxxFactoryMpi, cxx_des_parallel_stream
32 from paco_tmpl import hxxparallel_instream, hxxparallel_outstream, hxxinit_ok
33 from paco_tmpl import hxxparallel_instream_init, hxxparallel_outstream_init, cxxService_connect
34 from paco_tmpl import cxx_cons_service, cxx_cons_parallel_outstream, cxx_cons_parallel_instream
35
36 class PACOComponent(Component):
37
38   def __init__(self, name, parallel_lib, services=None, libs="", rlibs="", includes="", 
39                      kind="lib", exe_path=None, sources=None):
40     self.exe_path = exe_path
41     self.parallel_lib = parallel_lib
42     Component.__init__(self, name, services, impl="PACO", libs=libs, 
43                        rlibs=rlibs, includes=includes, kind=kind,
44                        sources=sources)
45   def validate(self):
46     """ validate component definition parameters"""
47     Component.validate(self)
48     kinds = ("lib")
49     if self.kind not in kinds:
50       raise Invalid("kind must be one of %s for component %s" % (kinds,self.name))
51     parallel_libs = ("dummy", "mpi")
52     if self.parallel_lib not in parallel_libs:
53       raise Invalid("parallel_lib must be one of %s" % parallel_libs)
54
55   def makeCompo(self, gen):
56     """generate files for PaCO++ component
57        
58        return a dict where key is the file name and value is the content of the file
59     """
60     cxxfile = "%s.cxx" % self.name
61     hxxfile = "%s.hxx" % self.name
62     if self.kind == "lib":
63       sources = " ".join(map(os.path.basename, self.sources))
64       sources += " "
65       sources += paco_sources.substitute(module=gen.module.name,
66                                          component=self.name)
67       return {"Makefile.am":compoMakefile.substitute(module=gen.module.name, 
68                                                      component=self.name,
69                                                      libs=self.libs, 
70                                                      rlibs=self.rlibs,
71                                                      sources=sources,
72                                                      includes=self.includes),
73               cxxfile:self.makecxx(gen), 
74               hxxfile:self.makehxx(gen)}
75
76   def makehxx(self, gen):
77     """return a string that is the content of .hxx file
78     """
79     services = []
80     parallel_instream = ""
81     parallel_outstream = ""
82     services_init_ok = ""
83     parallelstreamports = ""
84     for serv in self.services:
85       service = "    void %s(" % serv.name
86       service = service+gen.makeArgs(serv)+");"
87       services.append(service)
88       services_init_ok += hxxinit_ok.substitute(service_name=serv.name)
89
90       # Ajout des ports parallel DataStream
91       for name, type in serv.parallel_instream:
92         parallel_instream += hxxparallel_instream.substitute(name=name,
93                                                              type=type)
94       for name, type in serv.parallel_outstream:
95         parallel_outstream += hxxparallel_outstream.substitute(name=name,
96                                                                type=type)
97     servicesdef = "\n".join(services)
98     parallelstreamports += parallel_instream
99     parallelstreamports += parallel_outstream
100     return hxxCompo.substitute(component=self.name, 
101                                module=gen.module.name, 
102                                servicesdef=servicesdef,
103                                services_init_ok=services_init_ok,
104                                parallelstreamports=parallelstreamports,
105                                parallel_lib=self.parallel_lib)
106
107   def makecxx(self, gen):
108     """return a string that is the content of .cxx file
109     """
110     services = []
111     inits = []
112     defs = []
113     cons_services = ""
114     des_services = ""
115     for serv in self.services:
116       defs.append(serv.defs)
117     
118       # Constructeur
119       cons_services += cxx_cons_service.substitute(service_name=serv.name)
120       for name, type in serv.parallel_instream:
121         cons_services += cxx_cons_parallel_instream.substitute(name=name)
122       for name, type in serv.parallel_outstream:
123         cons_services += cxx_cons_parallel_outstream.substitute(name=name)
124
125       # Destructeur  
126       # On détruit uniquement les ports uses
127       # Les ports provides sont détruit lors de la destruction du poa
128       for name, type in serv.parallel_outstream:
129         des_services += cxx_des_parallel_stream.substitute(name=name)
130
131       # init_service
132       init_parallel_datastream_ports=""
133       for name, type in serv.parallel_instream:
134         init_parallel_datastream_ports += hxxparallel_instream_init.substitute(name=name,
135                                                                                type=type)
136       for name, type in serv.parallel_outstream:
137         init_parallel_datastream_ports += hxxparallel_outstream_init.substitute(name=name,
138                                                                                 type=type)
139       init = initService.substitute(service_name=serv.name,
140                                     init_parallel_datastream_ports=init_parallel_datastream_ports)
141       inits.append(init)
142
143       # Code du service
144       connect_parallel_streamport = ""
145       for name, type in serv.parallel_outstream:
146         connect_parallel_streamport += cxxService_connect.substitute(name=name)
147
148       service = cxxService.substitute(component=self.name, service=serv.name, 
149                                       parameters=gen.makeArgs(serv),
150                                       body=serv.body,
151                                       connect_parallel_streamport=connect_parallel_streamport)
152       services.append(service)
153     
154     cxxfile = cxxCompo.substitute(component=self.name, module=gen.module.name, 
155                                   servicesdef="\n".join(defs), 
156                                   servicesimpl="\n".join(services), 
157                                   initservice='\n'.join(inits),
158                                   cons_services=cons_services,
159                                   des_services=des_services)
160
161     if self.parallel_lib == "dummy":
162       cxxfile += cxxFactoryDummy.substitute(component=self.name, module=gen.module.name)
163     elif self.parallel_lib == "mpi":
164       cxxfile += cxxFactoryMpi.substitute(component=self.name, module=gen.module.name)
165
166     return cxxfile
167