]> SALOME platform Git repositories - tools/yacsgen.git/blob - module_generator/fcompo.py
Salome HOME
Merge branch 'master' of https://git.salome-platform.org/git/tools/yacsgen
[tools/yacsgen.git] / module_generator / fcompo.py
1 # Copyright (C) 2009-2014  EDF R&D
2 #
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.
7 #
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.
12 #
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
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 from gener import Component
21 from cppcompo import CPPComponent
22
23 import platform
24 archi = platform.architecture()[0]
25
26 if archi == "64bit":
27   f77Types = {"double":"double *", "long":"int *", "string":"const char *"}
28 else:
29   f77Types = {"double":"double *", "long":"long *", "string":"const char *"}
30
31 class F77Component(CPPComponent):
32   """
33    A :class:`F77Component` instance represents a Fortran SALOME component with services given as a list of :class:`Service`
34    instances with the parameter *services*.
35
36    :param name: gives the name of the component.
37    :type name: str
38    :param services: the list of services (:class:`Service`) of the component.
39    :param kind: If it is given and has the value "exe", the component will be built as a standalone
40       component (executable or shell script). The default is to build the component as a dynamic library.
41    :param libs: gives all the libraries options to add when linking the generated component (-L...).
42    :param rlibs: gives all the runtime libraries options to add when linking the generated component (-R...).
43    :param sources: gives all the external source files to add in the compilation step (list of paths).
44    :param exe_path: is only used when kind is "exe" and gives the path to the standalone component.
45
46    For example, the following call defines a Fortran component named "mycompo" with one service s1 (it must have been defined before).
47    This component is implemented as a dynamic library linked with a user's library "mylib"::
48
49       >>> c1 = module_generator.F77Component('mycompo', services=[s1,],
50                                                        libs=[[Library(name="mylib", path=mydir)])
51
52   """
53   def __init__(self, name, services=None, libs=[], rlibs="", 
54                      kind="lib", exe_path=None, sources=None):
55     CPPComponent.__init__(self, name, services, libs=libs, rlibs=rlibs, 
56                                 kind=kind, exe_path=exe_path, sources=sources)
57     self.impl = "F77"
58
59   def makebody(self):
60     """generate definitions (defs attribute of services) et bodys (body attribute of services)"""
61     for serv in self.services:
62       #defs generation
63       params=[]
64       if serv.instream or serv.outstream:
65         params = ["void *compo"]
66       strparams = []
67       for name, typ in serv.inport:
68         if typ == "file":continue #files are not passed through service interface
69         if typ == "string":
70           params.append("const STR_PSTR(%s)"%name)
71           strparams.append("STR_PLEN(%s)"%name)
72         else:
73           params.append("%s %s" % (f77Types[typ], name))
74       for name, typ in serv.outport:
75         if typ == "file":continue #files are not passed through service interface
76         if typ == "string":
77           params.append("const STR_PSTR(%s)"%name)
78           strparams.append("STR_PLEN(%s)"%name)
79         else:
80           params.append("%s %s" % (f77Types[typ], name))
81       args = ','.join(params)+" " + " ".join(strparams)
82       serv.defs = serv.defs+'\nextern "C" void F_FUNC(%s,%s)(%s);' % (serv.name.lower(), serv.name.upper(), args)
83
84       #body generation
85       params=[]
86       if serv.instream or serv.outstream:
87         params = ["&component"]
88       strparams = []
89       strallocs = []
90       #length allocated for out string
91       lstr = 20
92       for name, typ in serv.inport:
93         if typ == "file":continue #files are not passed through service interface
94         if typ == "string":
95           params.append("STR_CPTR(%s)" % name)
96           strparams.append("STR_CLEN(%s)"%name)
97         else:
98           params.append("&%s" % name)
99       for name, typ in serv.outport:
100         if typ == "file":continue #files are not passed through service interface
101         if typ == "string":
102           params.append("STR_CPTR(%s.ptr())" % name)
103           strparams.append("STR_CLEN(%s.ptr())"%name)
104           strallocs.append('%s=CORBA::string_dup("%s");' %(name, " "*lstr))
105         else:
106           params.append("&%s" % name)
107       serv.body = '\n'.join(strallocs)+'\n'+serv.body
108       args = ','.join(params)+" " + " ".join(strparams)
109       serv.body = serv.body+"\n   F_CALL(%s,%s)(%s);" % (serv.name.lower(), serv.name.upper(), args)
110
111   def makeCompo(self, gen):
112     """build a dictionary that defines component files
113        dictionary key = file name
114        dictionary value = file content or dictionary defining subdirectory content
115     """
116     self.makebody()
117     return CPPComponent.makeCompo(self, gen)