Salome HOME
scs #13189: report des modifications pour que SAT fonctionne sous windows
[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 import shutil
23 import glob
24
25 import src
26
27 C_COMPILE_ENV_LIST = ["CC",
28                       "CXX",
29                       "F77",
30                       "CFLAGS",
31                       "CXXFLAGS",
32                       "LIBS",
33                       "LDFLAGS"]
34
35 class Builder:
36     """Class to handle all construction steps, like cmake, configure, make, ...
37     """
38     def __init__(self,
39                  config,
40                  logger,
41                  product_info,
42                  options = src.options.OptResult(),
43                  check_src=True):
44         self.config = config
45         self.logger = logger
46         self.options = options
47         self.product_info = product_info
48         self.build_dir = src.Path(self.product_info.build_dir)
49         self.source_dir = src.Path(self.product_info.source_dir)
50         self.install_dir = src.Path(self.product_info.install_dir)
51         self.header = ""
52         self.debug_mode = False
53         if "debug" in self.product_info and self.product_info.debug == "yes":
54             self.debug_mode = True
55         self.verbose_mode = False
56         if "verbose" in self.product_info and self.product_info.verbose == "yes":
57             self.verbose_mode = True
58
59     ##
60     # Shortcut method to log in log file.
61     def log(self, text, level, showInfo=True):
62         self.logger.write(text, level, showInfo)
63         self.logger.logTxtFile.write(src.printcolors.cleancolor(text))
64         self.logger.flush()
65
66     ##
67     # Shortcut method to log a command.
68     def log_command(self, command):
69         self.log("> %s\n" % command, 5)
70
71     ##
72     # Prepares the environment.
73     # Build two environment: one for building and one for testing (launch).
74     def prepare(self):
75
76         if not self.build_dir.exists():
77             # create build dir
78             self.build_dir.make()
79
80         self.log('  build_dir   = %s\n' % str(self.build_dir), 4)
81         self.log('  install_dir = %s\n' % str(self.install_dir), 4)
82         self.log('\n', 4)
83
84         # add products in depend and opt_depend list recursively
85         environ_info = src.product.get_product_dependencies(self.config,
86                                                             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,
91                                       src.environment.Environ(dict(os.environ)),
92                                       True)
93         self.build_environ.silent = (self.config.USER.output_verbose_level < 5)
94         self.build_environ.set_full_environ(self.logger, environ_info)
95         
96         # create runtime environment
97         self.launch_environ = src.environment.SalomeEnviron(self.config,
98                                       src.environment.Environ(dict(os.environ)),
99                                       False)
100         self.launch_environ.silent = True # no need to show here
101         self.launch_environ.set_full_environ(self.logger, environ_info)
102
103         for ee in C_COMPILE_ENV_LIST:
104             vv = self.build_environ.get(ee)
105             if len(vv) > 0:
106                 self.log("  %s = %s\n" % (ee, vv), 4, False)
107
108         return 0
109
110     ##
111     # Runs cmake with the given options.
112     def cmake(self, options=""):
113
114         cmake_option = options
115         # cmake_option +=' -DCMAKE_VERBOSE_MAKEFILE=ON -DSALOME_CMAKE_DEBUG=ON'
116         if 'cmake_options' in self.product_info:
117             cmake_option += " %s " % " ".join(
118                                         self.product_info.cmake_options.split())
119
120         # add debug option
121         if self.debug_mode:
122             cmake_option += " -DCMAKE_BUILD_TYPE=Debug"
123         else :
124             cmake_option += " -DCMAKE_BUILD_TYPE=Release"
125
126         # add verbose option if specified in application for this product.
127         if self.verbose_mode:
128             cmake_option += " -DCMAKE_VERBOSE_MAKEFILE=ON"
129
130         # In case CMAKE_GENERATOR is defined in environment, 
131         # use it in spite of automatically detect it
132         if 'cmake_generator' in self.config.APPLICATION:
133             cmake_option += " -DCMAKE_GENERATOR=%s" \
134                                        % self.config.APPLICATION.cmake_generator
135         
136         command = ("cmake %s -DCMAKE_INSTALL_PREFIX=%s %s" %
137                             (cmake_option, self.install_dir, self.source_dir))
138
139         self.log_command(command)
140         # for key in sorted(self.build_environ.environ.environ.keys()):
141             # print key, "  ", self.build_environ.environ.environ[key]
142         res = subprocess.call(command,
143                               shell=True,
144                               cwd=str(self.build_dir),
145                               env=self.build_environ.environ.environ,
146                               stdout=self.logger.logTxtFile,
147                               stderr=subprocess.STDOUT)
148
149         self.put_txt_log_in_appli_log_dir("cmake")
150         if res == 0:
151             return res
152         else:
153             return 1
154
155     ##
156     # Runs build_configure with the given options.
157     def build_configure(self, options=""):
158
159         if 'buildconfigure_options' in self.product_info:
160             options += " %s " % self.product_info.buildconfigure_options
161
162         command = str('%s/build_configure') % (self.source_dir)
163         command = command + " " + options
164         self.log_command(command)
165
166         res = subprocess.call(command,
167                               shell=True,
168                               cwd=str(self.build_dir),
169                               env=self.build_environ.environ.environ,
170                               stdout=self.logger.logTxtFile,
171                               stderr=subprocess.STDOUT)
172         self.put_txt_log_in_appli_log_dir("build_configure")
173         if res == 0:
174             return res
175         else:
176             return 1
177
178     ##
179     # Runs configure with the given options.
180     def configure(self, options=""):
181
182         if 'configure_options' in self.product_info:
183             options += " %s " % self.product_info.configure_options
184
185         command = "%s/configure --prefix=%s" % (self.source_dir,
186                                                 str(self.install_dir))
187
188         command = command + " " + options
189         self.log_command(command)
190
191         res = subprocess.call(command,
192                               shell=True,
193                               cwd=str(self.build_dir),
194                               env=self.build_environ.environ.environ,
195                               stdout=self.logger.logTxtFile,
196                               stderr=subprocess.STDOUT)
197         
198         self.put_txt_log_in_appli_log_dir("configure")
199         if res == 0:
200             return res
201         else:
202             return 1
203
204     def hack_libtool(self):
205         if not os.path.exists(str(self.build_dir + 'libtool')):
206             return
207
208         lf = open(os.path.join(str(self.build_dir), "libtool"), 'r')
209         for line in lf.readlines():
210             if 'hack_libtool' in line:
211                 return
212
213         # fix libtool by replacing CC="<compil>" with hack_libtool function
214         hack_command='''sed -i "s%^CC=\\"\(.*\)\\"%hack_libtool() { \\n\\
215 if test \\"\$(echo \$@ | grep -E '\\\\\\-L/usr/lib(/../lib)?(64)? ')\\" == \\\"\\\" \\n\\
216   then\\n\\
217     cmd=\\"\\1 \$@\\"\\n\\
218   else\\n\\
219     cmd=\\"\\1 \\"\`echo \$@ | sed -r -e 's|(.*)-L/usr/lib(/../lib)?(64)? (.*)|\\\\\\1\\\\\\4 -L/usr/lib\\\\\\3|g'\`\\n\\
220   fi\\n\\
221   \$cmd\\n\\
222 }\\n\\
223 CC=\\"hack_libtool\\"%g" libtool'''
224
225         self.log_command(hack_command)
226         subprocess.call(hack_command,
227                         shell=True,
228                         cwd=str(self.build_dir),
229                         env=self.build_environ.environ.environ,
230                         stdout=self.logger.logTxtFile,
231                         stderr=subprocess.STDOUT)
232
233
234     ##
235     # Runs make to build the module.
236     def make(self, nb_proc, make_opt=""):
237
238         # make
239         command = 'make'
240         command = command + " -j" + str(nb_proc)
241         command = command + " " + make_opt
242         self.log_command(command)
243         res = subprocess.call(command,
244                               shell=True,
245                               cwd=str(self.build_dir),
246                               env=self.build_environ.environ.environ,
247                               stdout=self.logger.logTxtFile,
248                               stderr=subprocess.STDOUT)
249         self.put_txt_log_in_appli_log_dir("make")
250         if res == 0:
251             return res
252         else:
253             return 1
254     
255     ##
256     # Runs msbuild to build the module.
257     def wmake(self,nb_proc, opt_nb_proc = None):
258
259         hh = 'MSBUILD /m:%s' % str(nb_proc)
260         if self.debug_mode:
261             hh += " " + src.printcolors.printcWarning("DEBUG")
262         # make
263         command = 'msbuild'
264         command = command + " /maxcpucount:" + str(nb_proc)
265         if self.debug_mode:
266             command = command + " /p:Configuration=Debug"
267         else:
268             command = command + " /p:Configuration=Release"
269         command = command + " ALL_BUILD.vcxproj"
270
271         self.log_command(command)
272         res = subprocess.call(command,
273                               shell=True,
274                               cwd=str(self.build_dir),
275                               env=self.build_environ.environ.environ,
276                               stdout=self.logger.logTxtFile,
277                               stderr=subprocess.STDOUT)
278         
279         self.put_txt_log_in_appli_log_dir("make")
280         if res == 0:
281             return res
282         else:
283             return 1
284
285     ##
286     # Runs 'make install'.
287     def install(self):
288         if self.config.VARS.dist_name=="Win":
289             command = 'msbuild INSTALL.vcxproj'
290             if self.debug_mode:
291                 command = command + " /p:Configuration=Debug"
292             else:
293                 command = command + " /p:Configuration=Release"
294         else :
295             command = 'make install'
296
297         self.log_command(command)
298
299         res = subprocess.call(command,
300                               shell=True,
301                               cwd=str(self.build_dir),
302                               env=self.build_environ.environ.environ,
303                               stdout=self.logger.logTxtFile,
304                               stderr=subprocess.STDOUT)
305         
306         res_check=self.check_install()
307         if res_check > 0 :
308             self.log_command("Error in sat check install - some files are not installed!")
309         self.put_txt_log_in_appli_log_dir("makeinstall")
310
311         res+=res_check
312         if res == 0:
313             return res
314         else:
315             return 1
316
317     # this function checks wether a list of file patterns (specified by check_install keyword) 
318     # exixts after the make install. The objective is to ensure the installation is complete.
319     # patterns are given relatively to the install dir of the product
320     def check_install(self):
321         res=0
322         if "check_install" in self.product_info:
323             self.log_command("Check installation of files")
324             for pattern in self.product_info.check_install:
325                 # pattern is given relatively to the install dir
326                 complete_pattern=os.path.join(self.product_info.install_dir, pattern) 
327                 self.log_command("    -> check %s" % complete_pattern)
328                 # expansion of pattern : takes into account environment variables and unix shell rules
329                 list_of_path=glob.glob(os.path.expandvars(complete_pattern))
330                 if not list_of_path:
331                     # we expect to find at least one entry, if not we consider the test failed
332                     res+=1
333                     self.logger.write("Error, sat check install failed for file pattern %s\n" % complete_pattern, 1)
334                     self.log_command("Error, sat check install failed for file pattern %s" % complete_pattern)
335         return res
336
337     ##
338     # Runs 'make_check'.
339     def check(self, command=""):
340         if src.architecture.is_windows():
341             cmd = 'msbuild RUN_TESTS.vcxproj'
342         else :
343             if self.product_info.build_source=="autotools" :
344                 cmd = 'make check'
345             else:
346                 cmd = 'make test'
347         
348         if command:
349             cmd = command
350         
351         self.log_command(cmd)
352         self.log_command("For more detailed logs, see test logs in %s" % self.build_dir)
353
354         res = subprocess.call(cmd,
355                               shell=True,
356                               cwd=str(self.build_dir),
357                               env=self.launch_environ.environ.environ,
358                               stdout=self.logger.logTxtFile,
359                               stderr=subprocess.STDOUT)
360
361         self.put_txt_log_in_appli_log_dir("makecheck")
362         if res == 0:
363             return res
364         else:
365             return 1
366       
367     ##
368     # Performs a default build for this module.
369     def do_default_build(self,
370                          build_conf_options="",
371                          configure_options="",
372                          show_warning=True):
373         use_autotools = False
374         if 'use_autotools' in self.product_info:
375             uc = self.product_info.use_autotools
376             if uc in ['always', 'yes']: 
377                 use_autotools = True
378             elif uc == 'option': 
379                 use_autotools = self.options.autotools
380
381
382         self.use_autotools = use_autotools
383
384         use_ctest = False
385         if 'use_ctest' in self.product_info:
386             uc = self.product_info.use_ctest
387             if uc in ['always', 'yes']: 
388                 use_ctest = True
389             elif uc == 'option': 
390                 use_ctest = self.options.ctest
391
392         self.use_ctest = use_ctest
393
394         if show_warning:
395             cmd = ""
396             if use_autotools: cmd = "(autotools)"
397             if use_ctest: cmd = "(ctest)"
398             
399             self.log("\n", 4, False)
400             self.log("%(module)s: Run default compilation method %(cmd)s\n" % \
401                 { "module": self.module, "cmd": cmd }, 4)
402
403         if use_autotools:
404             if not self.prepare(): return self.get_result()
405             if not self.build_configure(
406                                    build_conf_options): return self.get_result()
407             if not self.configure(configure_options): return self.get_result()
408             if not self.make(): return self.get_result()
409             if not self.install(): return self.get_result()
410             if not self.clean(): return self.get_result()
411            
412         else: # CMake
413             if self.config.VARS.dist_name=='Win':
414                 if not self.wprepare(): return self.get_result()
415                 if not self.cmake(): return self.get_result()
416                 if not self.wmake(): return self.get_result()
417                 if not self.install(): return self.get_result()
418                 if not self.clean(): return self.get_result()
419             else :
420                 if not self.prepare(): return self.get_result()
421                 if not self.cmake(): return self.get_result()
422                 if not self.make(): return self.get_result()
423                 if not self.install(): return self.get_result()
424                 if not self.clean(): return self.get_result()
425
426         return self.get_result()
427
428     ##
429     # Performs a build with a script.
430     def do_python_script_build(self, script, nb_proc):
431         # script found
432         self.logger.write(_("Compile %(product)s using script %(script)s\n") % \
433             { 'product': self.product_info.name,
434              'script': src.printcolors.printcLabel(script) }, 4)
435         try:
436             import imp
437             product = self.product_info.name
438             pymodule = imp.load_source(product + "_compile_script", script)
439             self.nb_proc = nb_proc
440             retcode = pymodule.compil(self.config, self, self.logger)
441         except:
442             __, exceptionValue, exceptionTraceback = sys.exc_info()
443             self.logger.write(str(exceptionValue), 1)
444             import traceback
445             traceback.print_tb(exceptionTraceback)
446             traceback.print_exc()
447             retcode = 1
448         finally:
449             self.put_txt_log_in_appli_log_dir("script")
450
451         return retcode
452
453     def complete_environment(self, make_options):
454         assert self.build_environ is not None
455         # pass additional variables to environment 
456         # (may be used by the build script)
457         self.build_environ.set("APPLICATION_NAME", self.config.APPLICATION.name)
458         self.build_environ.set("SOURCE_DIR", str(self.source_dir))
459         self.build_environ.set("INSTALL_DIR", str(self.install_dir))
460         self.build_environ.set("PRODUCT_INSTALL", str(self.install_dir))
461         self.build_environ.set("BUILD_DIR", str(self.build_dir))
462         self.build_environ.set("PRODUCT_BUILD", str(self.build_dir))
463         self.build_environ.set("MAKE_OPTIONS", make_options)
464         self.build_environ.set("DIST_NAME", self.config.VARS.dist_name)
465         self.build_environ.set("DIST_VERSION", self.config.VARS.dist_version)
466         self.build_environ.set("DIST", self.config.VARS.dist)
467         self.build_environ.set("VERSION", self.product_info.version)
468         # if product is in hpc mode, set SAT_HPC to 1 
469         # in order for the compilation script to take it into account
470         if src.product.product_is_hpc(self.product_info):
471             self.build_environ.set("SAT_HPC", "1")
472
473     def do_batch_script_build(self, script, nb_proc):
474
475         if src.architecture.is_windows():
476             make_options = "/maxcpucount:%s" % nb_proc
477         else :
478             make_options = "-j%s" % nb_proc
479
480         self.log_command("  " + _("Run build script %s\n") % script)
481         self.complete_environment(make_options)
482         
483         res = subprocess.call(script, 
484                               shell=True,
485                               stdout=self.logger.logTxtFile,
486                               stderr=subprocess.STDOUT,
487                               cwd=str(self.build_dir),
488                               env=self.build_environ.environ.environ)
489
490         res_check=self.check_install()
491         if res_check > 0 :
492             self.log_command("Error in sat check install - some files are not installed!")
493
494         self.put_txt_log_in_appli_log_dir("script")
495         res += res_check
496         if res == 0:
497             return res
498         else:
499             return 1
500     
501     def do_script_build(self, script, number_of_proc=0):
502         # define make options (may not be used by the script)
503         if number_of_proc==0:
504             nb_proc = src.get_cfg_param(self.product_info,"nb_proc", 0)
505             if nb_proc == 0: 
506                 nb_proc = self.config.VARS.nb_proc
507         else:
508             nb_proc = min(number_of_proc, self.config.VARS.nb_proc)
509             
510         extension = script.split('.')[-1]
511         if extension in ["bat","sh"]:
512             return self.do_batch_script_build(script, nb_proc)
513         if extension == "py":
514             return self.do_python_script_build(script, nb_proc)
515         
516         msg = _("The script %s must have .sh, .bat or .py extension." % script)
517         raise src.SatException(msg)
518     
519     def put_txt_log_in_appli_log_dir(self, file_name):
520         '''Put the txt log (that contain the system logs, like make command
521            output) in the directory <APPLICATION DIR>/LOGS/<product_name>/
522     
523         :param file_name Str: the name of the file to write
524         '''
525         if self.logger.logTxtFile == sys.__stdout__:
526             return
527         dir_where_to_put = os.path.join(self.config.APPLICATION.workdir,
528                                         "LOGS",
529                                         self.product_info.name)
530         file_path = os.path.join(dir_where_to_put, file_name)
531         src.ensure_path_exists(dir_where_to_put)
532         # write the logTxtFile copy it to the destination, and then recreate 
533         # it as it was
534         self.logger.logTxtFile.close()
535         shutil.move(self.logger.txtFilePath, file_path)
536         self.logger.logTxtFile = open(str(self.logger.txtFilePath), 'w')
537         self.logger.logTxtFile.write(open(file_path, "r").read())
538