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