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