Salome HOME
Update copyrights 2014.
[tools/yacsgen.git] / script / hxx2salome.py
1 #! /usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2009-2014  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, or (at your option) any later version.
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 usage="""
22 hxx2salome.py [options] <CPPCOMPO>_root_dir lib<CPPCOMPO>.hxx <CPPCOMPO>.so installDir
23
24 generate a SALOME component that wrapps given the C++ component
25
26 Mandatory arguments:
27
28   - <CPPCOMPO>_root_dir   : install directory (absolute path) of the c++ component
29   - <CPPCOMPO>.hxx        : header of the c++ component"
30   - lib<CPPCOMPO>.so      : the shared library containing the c++ component
31   - installDir            : directory where the generated files and the build should be installed 
32
33   Note that <CPPCOMPO>.hxx and lib<CPPCOMPO>.so should be found in <CPPCOMPO>_root_dir)
34   
35 """
36
37 import os
38 import sys
39 import string
40 import optparse
41 from module_generator import Generator,Module,Service,HXX2SALOMEComponent
42
43 # ------------------------------------------------------------------------------
44
45 def main():
46 #   Reproduce the main options of original hxx2salome script
47     parser = optparse.OptionParser(usage=usage)
48     parser.add_option(
49         '-e', 
50         dest="environ_file", 
51         default='sh', 
52         help="specify the name of a environment file (bash/sh) that will" +\
53              " be updated")
54     parser.add_option(
55         '-g', 
56         action="store_true", 
57         dest="do_gui", 
58         default=False,
59         help="to create a generic gui in your component building tree")
60     parser.add_option(
61         '-c', 
62         action="store_true", 
63         dest="do_compile", 
64         default=False,
65         help="to compile after generation")
66     parser.add_option(
67         '-s', 
68         dest="shell_syntax", 
69         help="use this option with csh to update the environment " +\
70              "with the CSH syntax")
71
72     options, args = parser.parse_args()
73
74     assert len(args) == 4, \
75         'ERROR, four mandatory arguments are expected!\n\n%s\n' % usage
76     cppdir = args[0]      # install directory of the c++ component
77     hxxfile = args[1]     # header of the c++ component
78     libfile = args[2]     # the shared library containing the c++ component
79     installpath = args[3] # directory where the generated files are installed
80
81 #   Make sure given paths/files are valid
82     if not os.path.exists(cppdir):
83         print "ERROR: cppdir %s does not exist. It is mandatory" % cppdir
84         print usage
85         sys.exit(1)
86
87     if not os.path.exists(installpath):
88         print "ERROR: installpath %s does not exist. It is mandatory" \
89               % installpath
90         print usage
91         sys.exit(1)
92
93     if options.environ_file != None:
94         if not os.path.exists(options.environ_file):
95             print "ERROR: environ_file %s does not exist. It is mandatory" \
96                   % options.environ_file
97             print usage
98             sys.exit(1)
99
100     hxx2salome(cppdir=cppdir,
101                hxxfile=hxxfile,
102                libfile=libfile,
103                installpath=installpath,
104                do_gui=options.do_gui,
105                do_compile=options.do_compile,
106                environ_file=options.environ_file,
107                shell_syntax=options.shell_syntax
108                )
109     pass
110
111 # ------------------------------------------------------------------------------
112
113 def hxx2salome(cppdir,
114         hxxfile,
115         libfile,
116         installpath,
117         do_gui,
118         do_compile,
119         environ_file,
120         shell_syntax):
121
122     # setup from environment a minimal context
123     kernel_root_dir=os.environ["KERNEL_ROOT_DIR"]
124     gui_root_dir=os.environ["GUI_ROOT_DIR"]
125     context={'update':1,
126              "makeflags":"-j2",
127              "kernel":kernel_root_dir,
128              "gui":gui_root_dir,
129             }
130     #
131     salome_compo = HXX2SALOMEComponent(hxxfile,libfile,cppdir)
132     install_root_dir = os.path.join(installpath,salome_compo.name)
133     module_root_dir = os.path.join(install_root_dir,
134             salome_compo.name+"_INSTALL")
135
136     # to be able to compile the generated component
137     os.environ[salome_compo.name+"CPP_ROOT_DIR"]=cppdir  
138
139     # if necessary creates the directory in which the component 
140     # will be geberated and compiled.
141     try: 
142         os.mkdir(install_root_dir)
143     except OSError:
144         print "Warning : directory %s already exixts!" % install_root_dir
145
146     # if a graphical user interface is required,
147     # ask HXX2SALOMEComponent to generate template files
148     if do_gui:
149         gui_files=salome_compo.getGUIfilesTemplate()
150         g=Generator(Module(salome_compo.name,components=[salome_compo],
151             prefix=module_root_dir,
152             gui=gui_files),
153             context)
154     else:
155         g=Generator(Module(salome_compo.name,components=[salome_compo],
156             prefix=module_root_dir),
157             context)
158
159     # go in install_root_dir, generate the component
160     os.chdir(install_root_dir)
161     g.generate()
162
163     # if specified : compile and install the generated component
164     if do_compile:
165         g.bootstrap()
166         g.configure()
167         g.make()
168         g.install()
169         pass
170     #
171     # update environment file if furnished
172     if environ_file != None:
173        envfile=open(environ_file,"a")
174        if shell_syntax == "csh":
175            update_environ="""
176 #------ ${compo_name}
177 setenv ${compo_name}_SRC_DIR ${install_root_dir}/${compo_name}_SRC
178 setenv ${compo_name}_ROOT_DIR ${install_root_dir}/${compo_name}_INSTALL
179 """
180        else:
181            update_environ="""
182 #------ ${compo_name}
183 export ${compo_name}_SRC_DIR=${install_root_dir}/${compo_name}_SRC
184 export ${compo_name}_ROOT_DIR=${install_root_dir}/${compo_name}_INSTALL
185 """
186        update_environ=string.Template(update_environ)
187        update_environ=update_environ.substitute(compo_name=salome_compo.name,
188                install_root_dir=install_root_dir)
189        envfile.write(update_environ)
190        envfile.close()
191     pass
192
193 # ------------------------------------------------------------------------------
194
195 if __name__ == '__main__':
196     main()
197     pass