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