Salome HOME
Add the run command
[tools/sat.git] / src / compilation.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2013  CEA/DEN
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 import os
20 import subprocess
21 import sys
22
23 import src
24
25 C_COMPILE_ENV_LIST = ["CC",
26                       "CXX",
27                       "F77",
28                       "CFLAGS",
29                       "CXXFLAGS",
30                       "LIBS",
31                       "LDFLAGS"]
32
33 class Builder:
34     """Class to handle all construction steps, like cmake, configure, make, ...
35     """
36     def __init__(self, config, logger, product_info, options = src.options.OptResult(), debug_mode=False, check_src=True):
37         self.config = config
38         self.logger = logger
39         self.options = options
40         self.product_info = product_info
41         self.build_dir = src.Path(self.product_info.build_dir)
42         self.source_dir = src.Path(self.product_info.source_dir)
43         self.install_dir = src.Path(self.product_info.install_dir)
44         self.header = ""
45         self.debug_mode = debug_mode
46
47         if not self.source_dir.exists() and check_src:
48             raise src.SatException(_("No sources found for product %(product)s in %(source_dir)s" % \
49                 { "product": self.product_info.name, "source_dir": self.source_dir } ))
50
51         """
52         # check that required modules exist
53         for dep in self.product_info.depend:
54             assert dep in self.config.TOOLS.src.product_info, "UNDEFINED product: %s" % dep
55             dep_info = self.config.TOOLS.src.product_info[dep]
56             if 'install_dir' in dep_info and not os.path.exists(dep_info.install_dir):
57                 raise src.SatException(_("Module %s is required") % dep)
58         """
59
60     ##
61     # Shortcut method to log in log file.
62     def log(self, text, level, showInfo=True):
63         self.logger.write(text, level, showInfo)
64         self.logger.logTxtFile.write(src.printcolors.cleancolor(text))
65         self.logger.flush()
66
67     ##
68     # Shortcut method to log a command.
69     def log_command(self, command):
70         self.log("> %s\n" % command, 5)
71
72     ##
73     # Prepares the environment.
74     # Build two environment: one for building and one for testing (launch).
75     def prepare(self):
76
77         if not self.build_dir.exists():
78             # create build dir
79             self.build_dir.make()
80
81         self.log('  build_dir   = %s\n' % str(self.build_dir), 4)
82         self.log('  install_dir = %s\n' % str(self.install_dir), 4)
83         self.log('\n', 4)
84
85         # add products in depend and opt_depend list recursively
86         environ_info = src.product.get_product_dependencies(self.config, self.product_info)
87         #environ_info.append(self.product_info.name)
88
89         # create build environment
90         self.build_environ = src.environment.SalomeEnviron(self.config, src.environment.Environ(dict(os.environ)), True)
91         self.build_environ.silent = (self.config.USER.output_verbose_level < 5)
92         self.build_environ.set_full_environ(self.logger, environ_info)
93         
94         # create runtime environment
95         self.launch_environ = src.environment.SalomeEnviron(self.config, src.environment.Environ(dict(os.environ)), False)
96         self.launch_environ.silent = True # no need to show here
97         self.launch_environ.set_full_environ(self.logger, environ_info)
98
99         for ee in C_COMPILE_ENV_LIST:
100             vv = self.build_environ.get(ee)
101             if len(vv) > 0:
102                 self.log("  %s = %s\n" % (ee, vv), 4, False)
103
104         return 0
105
106     ##
107     # Runs cmake with the given options.
108     def cmake(self, options=""):
109
110         cmake_option = options
111         # cmake_option +=' -DCMAKE_VERBOSE_MAKEFILE=ON -DSALOME_CMAKE_DEBUG=ON'
112         if 'cmake_options' in self.product_info:
113             cmake_option += " %s " % " ".join(self.product_info.cmake_options.split())
114
115         # add debug option
116         if self.debug_mode:
117             cmake_option += " -DCMAKE_BUILD_TYPE=Debug"
118         else :
119             cmake_option += " -DCMAKE_BUILD_TYPE=Release"
120         
121         command = ("cmake %s -DCMAKE_INSTALL_PREFIX=%s %s" %
122                             (cmake_option, self.install_dir, self.source_dir))
123
124         self.log_command(command)
125         res = subprocess.call(command,
126                               shell=True,
127                               cwd=str(self.build_dir),
128                               env=self.build_environ.environ.environ,
129                               stdout=self.logger.logTxtFile,
130                               stderr=subprocess.STDOUT)
131
132         if res == 0:
133             return res
134         else:
135             return 1
136
137     ##
138     # Runs build_configure with the given options.
139     def build_configure(self, options=""):
140
141         if 'buildconfigure_options' in self.product_info:
142             options += " %s " % self.product_info.buildconfigure_options
143
144         command = str('%s/build_configure') % (self.source_dir)
145         command = command + " " + options
146         self.log_command(command)
147
148         res = subprocess.call(command,
149                               shell=True,
150                               cwd=str(self.build_dir),
151                               env=self.build_environ.environ.environ,
152                               stdout=self.logger.logTxtFile,
153                               stderr=subprocess.STDOUT)
154
155         if res == 0:
156             return res
157         else:
158             return 1
159
160     ##
161     # Runs configure with the given options.
162     def configure(self, options=""):
163
164         if 'configure_options' in self.product_info:
165             options += " %s " % self.product_info.configure_options
166
167         command = "%s/configure --prefix=%s" % (self.source_dir, str(self.install_dir))
168
169         command = command + " " + options
170         self.log_command(command)
171
172         res = subprocess.call(command,
173                               shell=True,
174                               cwd=str(self.build_dir),
175                               env=self.build_environ.environ.environ,
176                               stdout=self.logger.logTxtFile,
177                               stderr=subprocess.STDOUT)
178
179         if res == 0:
180             return res
181         else:
182             return 1
183
184     def hack_libtool(self):
185         if not os.path.exists(str(self.build_dir + 'libtool')):
186             return
187
188         lf = open(os.path.join(str(self.build_dir), "libtool"), 'r')
189         for line in lf.readlines():
190             if 'hack_libtool' in line:
191                 return
192
193         # fix libtool by replacing CC="<compil>" with hack_libtool function
194         hack_command='''sed -i "s%^CC=\\"\(.*\)\\"%hack_libtool() { \\n\\
195 if test \\"\$(echo \$@ | grep -E '\\\\\\-L/usr/lib(/../lib)?(64)? ')\\" == \\\"\\\" \\n\\
196   then\\n\\
197     cmd=\\"\\1 \$@\\"\\n\\
198   else\\n\\
199     cmd=\\"\\1 \\"\`echo \$@ | sed -r -e 's|(.*)-L/usr/lib(/../lib)?(64)? (.*)|\\\\\\1\\\\\\4 -L/usr/lib\\\\\\3|g'\`\\n\\
200   fi\\n\\
201   \$cmd\\n\\
202 }\\n\\
203 CC=\\"hack_libtool\\"%g" libtool'''
204
205         self.log_command(hack_command)
206         subprocess.call(hack_command,
207                         shell=True,
208                         cwd=str(self.build_dir),
209                         env=self.build_environ.environ.environ,
210                         stdout=self.logger.logTxtFile,
211                         stderr=subprocess.STDOUT)
212
213
214     ##
215     # Runs make to build the module.
216     def make(self, nb_proc, make_opt):
217
218         # make
219         command = 'make'
220         command = command + " -j" + str(nb_proc)
221         command = command + " " + make_opt
222         self.log_command(command)
223         res = subprocess.call(command,
224                               shell=True,
225                               cwd=str(self.build_dir),
226                               env=self.build_environ.environ.environ,
227                               stdout=self.logger.logTxtFile,
228                               stderr=subprocess.STDOUT)
229
230         if res == 0:
231             return res
232         else:
233             return 1
234     
235     ##
236     # Runs msbuild to build the module.
237     def wmake(self, opt_nb_proc = None):
238         nbproc = self.get_nb_proc(opt_nb_proc)
239
240         hh = 'MSBUILD /m:%s' % str(nbproc)
241         if self.debug_mode:
242             hh += " " + src.printcolors.printcWarning("DEBUG")
243         self.log_step(hh)
244
245         # make
246         command = 'msbuild'
247         if self.options.makeflags:
248             command = command + " " + self.options.makeflags
249         command = command + " /maxcpucount:" + str(nbproc)
250         if self.debug_mode:
251             command = command + " /p:Configuration=Debug"
252         else:
253             command = command + " /p:Configuration=Release"
254         command = command + " ALL_BUILD.vcxproj"
255
256         self.log_command(command)
257         res = subprocess.call(command,
258                               shell=True,
259                               cwd=str(self.build_dir),
260                               env=self.build_environ.environ.environ,
261                               stdout=self.logger.logTxtFile,
262                               stderr=subprocess.STDOUT)
263
264         if res == 0:
265             return res
266         else:
267             return 1
268
269     ##
270     # Runs 'make install'.
271     def install(self):
272         if self.config.VARS.dist_name=="Win":
273             command = 'msbuild INSTALL.vcxproj'
274             if self.debug_mode:
275                 command = command + " /p:Configuration=Debug"
276             else:
277                 command = command + " /p:Configuration=Release"
278         else :
279             command = 'make install'
280
281         self.log_command(command)
282
283         res = subprocess.call(command,
284                               shell=True,
285                               cwd=str(self.build_dir),
286                               env=self.build_environ.environ.environ,
287                               stdout=self.logger.logTxtFile,
288                               stderr=subprocess.STDOUT)
289
290         if res == 0:
291             return res
292         else:
293             return 1
294
295     ##
296     # Runs 'make_check'.
297     def check(self):
298         if src.architecture.is_windows():
299             command = 'msbuild RUN_TESTS.vcxproj'
300         else :
301             if self.use_autotools :
302                 command = 'make check'
303             else :
304                 command = 'make test'
305             
306         self.log_command(command)
307
308         res = subprocess.call(command,
309                               shell=True,
310                               cwd=str(self.build_dir),
311                               env=self.launch_environ.environ.environ,
312                               stdout=self.logger.logTxtFile,
313                               stderr=subprocess.STDOUT)
314
315         if res == 0:
316             return res
317         else:
318             return 1
319       
320     ##
321     # Performs a default build for this module.
322     def do_default_build(self, build_conf_options="", configure_options="", show_warning=True):
323         use_autotools = False
324         if 'use_autotools' in self.product_info:
325             uc = self.product_info.use_autotools
326             if uc in ['always', 'yes']: 
327                 use_autotools = True
328             elif uc == 'option': 
329                 use_autotools = self.options.autotools
330
331
332         self.use_autotools = use_autotools
333
334         use_ctest = False
335         if 'use_ctest' in self.product_info:
336             uc = self.product_info.use_ctest
337             if uc in ['always', 'yes']: 
338                 use_ctest = True
339             elif uc == 'option': 
340                 use_ctest = self.options.ctest
341
342         self.use_ctest = use_ctest
343
344         if show_warning:
345             cmd = ""
346             if use_autotools: cmd = "(autotools)"
347             if use_ctest: cmd = "(ctest)"
348             
349             self.log("\n", 4, False)
350             self.log("%(module)s: Run default compilation method %(cmd)s\n" % \
351                 { "module": self.module, "cmd": cmd }, 4)
352
353         if use_autotools:
354             if not self.prepare(): return self.get_result()
355             if not self.build_configure(build_conf_options): return self.get_result()
356             if not self.configure(configure_options): return self.get_result()
357             if not self.make(): return self.get_result()
358             if not self.install(): return self.get_result()
359             if not self.clean(): return self.get_result()
360            
361         else: # CMake
362             if self.config.VARS.dist_name=='Win':
363                 if not self.wprepare(): return self.get_result()
364                 if not self.cmake(): return self.get_result()
365                 if not self.wmake(): return self.get_result()
366                 if not self.install(): return self.get_result()
367                 if not self.clean(): return self.get_result()
368             else :
369                 if not self.prepare(): return self.get_result()
370                 if not self.cmake(): return self.get_result()
371                 if not self.make(): return self.get_result()
372                 if not self.install(): return self.get_result()
373                 if not self.clean(): return self.get_result()
374
375         return self.get_result()
376
377     ##
378     # Performs a build with a script.
379     def do_python_script_build(self, script):
380         # script found
381         self.logger.write(_("Compile %(module)s using script %(script)s\n") % \
382             { 'module': self.module, 'script': src.printcolors.printcLabel(script) }, 4)
383         try:
384             import imp
385             pymodule = imp.load_source(self.product + "_compile_script", script)
386             retcode = pymodule.compil(self.config, self, self.logger)
387         except:
388             __, exceptionValue, exceptionTraceback = sys.exc_info()
389             print(exceptionValue)
390             import traceback
391             traceback.print_tb(exceptionTraceback)
392             traceback.print_exc()
393
394         return retcode
395
396     def complete_environment(self, make_options):
397         assert self.build_environ is not None
398         # pass additional variables to environment (may be used by the build script)
399         self.build_environ.set("SOURCE_DIR", str(self.source_dir))
400         self.build_environ.set("INSTALL_DIR", str(self.install_dir))
401         self.build_environ.set("PRODUCT_INSTALL", str(self.install_dir))
402         self.build_environ.set("BUILD_DIR", str(self.build_dir))
403         self.build_environ.set("PRODUCT_BUILD", str(self.build_dir))
404         self.build_environ.set("MAKE_OPTIONS", make_options)
405         self.build_environ.set("DIST_NAME", self.config.VARS.dist_name)
406         self.build_environ.set("DIST_VERSION", self.config.VARS.dist_version)
407         self.build_environ.set("DIST", self.config.VARS.dist)
408
409     def do_batch_script_build(self, script):
410         # define make options (may not be used by the script)
411         nb_proc = src.get_cfg_param(self.product_info,"nb_proc", 0)
412         if nb_proc == 0: 
413             nb_proc = self.config.VARS.nb_proc
414
415         if src.architecture.is_windows():
416             make_options = "/maxcpucount:%s" % nb_proc
417         else :
418             make_options = "-j%s" % nb_proc
419
420         self.log_command("  " + _("Run build script %s\n") % script)
421         self.complete_environment(make_options)
422         res = subprocess.call(script, 
423                               shell=True,
424                               stdout=self.logger.logTxtFile,
425                               stderr=subprocess.STDOUT,
426                               cwd=str(self.build_dir), 
427                               env=self.build_environ.environ.environ)
428
429         if res == 0:
430             return res
431         else:
432             return 1
433     
434     def do_script_build(self, script):
435         extension = script.split('.')[-1]
436         if extension in ["bat","sh"]:
437             return self.do_batch_script_build(script)
438         if extension == "py":
439             return self.do_python_script_build(script)
440         
441         msg = _("The script %s must have .sh, .bat or .py extension." % script)
442         raise src.SatException(msg)