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