3 # Copyright (C) 2010-2013 CEA/DEN
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.
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.
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
25 C_COMPILE_ENV_LIST = ["CC",
34 """Class to handle all construction steps, like cmake, configure, make, ...
40 options = src.options.OptResult(),
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)
51 self.debug_mode = debug_mode
54 # Shortcut method to log in log file.
55 def log(self, text, level, showInfo=True):
56 self.logger.write(text, level, showInfo)
57 self.logger.logTxtFile.write(src.printcolors.cleancolor(text))
61 # Shortcut method to log a command.
62 def log_command(self, command):
63 self.log("> %s\n" % command, 5)
66 # Prepares the environment.
67 # Build two environment: one for building and one for testing (launch).
70 if not self.build_dir.exists():
74 self.log(' build_dir = %s\n' % str(self.build_dir), 4)
75 self.log(' install_dir = %s\n' % str(self.install_dir), 4)
78 # add products in depend and opt_depend list recursively
79 environ_info = src.product.get_product_dependencies(self.config, self.product_info)
80 #environ_info.append(self.product_info.name)
82 # create build environment
83 self.build_environ = src.environment.SalomeEnviron(self.config, src.environment.Environ(dict(os.environ)), True)
84 self.build_environ.silent = (self.config.USER.output_verbose_level < 5)
85 self.build_environ.set_full_environ(self.logger, environ_info)
87 # create runtime environment
88 self.launch_environ = src.environment.SalomeEnviron(self.config, src.environment.Environ(dict(os.environ)), False)
89 self.launch_environ.silent = True # no need to show here
90 self.launch_environ.set_full_environ(self.logger, environ_info)
92 for ee in C_COMPILE_ENV_LIST:
93 vv = self.build_environ.get(ee)
95 self.log(" %s = %s\n" % (ee, vv), 4, False)
100 # Runs cmake with the given options.
101 def cmake(self, options=""):
103 cmake_option = options
104 # cmake_option +=' -DCMAKE_VERBOSE_MAKEFILE=ON -DSALOME_CMAKE_DEBUG=ON'
105 if 'cmake_options' in self.product_info:
106 cmake_option += " %s " % " ".join(self.product_info.cmake_options.split())
110 cmake_option += " -DCMAKE_BUILD_TYPE=Debug"
112 cmake_option += " -DCMAKE_BUILD_TYPE=Release"
114 command = ("cmake %s -DCMAKE_INSTALL_PREFIX=%s %s" %
115 (cmake_option, self.install_dir, self.source_dir))
117 self.log_command(command)
118 res = subprocess.call(command,
120 cwd=str(self.build_dir),
121 env=self.build_environ.environ.environ,
122 stdout=self.logger.logTxtFile,
123 stderr=subprocess.STDOUT)
131 # Runs build_configure with the given options.
132 def build_configure(self, options=""):
134 if 'buildconfigure_options' in self.product_info:
135 options += " %s " % self.product_info.buildconfigure_options
137 command = str('%s/build_configure') % (self.source_dir)
138 command = command + " " + options
139 self.log_command(command)
141 res = subprocess.call(command,
143 cwd=str(self.build_dir),
144 env=self.build_environ.environ.environ,
145 stdout=self.logger.logTxtFile,
146 stderr=subprocess.STDOUT)
154 # Runs configure with the given options.
155 def configure(self, options=""):
157 if 'configure_options' in self.product_info:
158 options += " %s " % self.product_info.configure_options
160 command = "%s/configure --prefix=%s" % (self.source_dir, str(self.install_dir))
162 command = command + " " + options
163 self.log_command(command)
165 res = subprocess.call(command,
167 cwd=str(self.build_dir),
168 env=self.build_environ.environ.environ,
169 stdout=self.logger.logTxtFile,
170 stderr=subprocess.STDOUT)
177 def hack_libtool(self):
178 if not os.path.exists(str(self.build_dir + 'libtool')):
181 lf = open(os.path.join(str(self.build_dir), "libtool"), 'r')
182 for line in lf.readlines():
183 if 'hack_libtool' in line:
186 # fix libtool by replacing CC="<compil>" with hack_libtool function
187 hack_command='''sed -i "s%^CC=\\"\(.*\)\\"%hack_libtool() { \\n\\
188 if test \\"\$(echo \$@ | grep -E '\\\\\\-L/usr/lib(/../lib)?(64)? ')\\" == \\\"\\\" \\n\\
190 cmd=\\"\\1 \$@\\"\\n\\
192 cmd=\\"\\1 \\"\`echo \$@ | sed -r -e 's|(.*)-L/usr/lib(/../lib)?(64)? (.*)|\\\\\\1\\\\\\4 -L/usr/lib\\\\\\3|g'\`\\n\\
196 CC=\\"hack_libtool\\"%g" libtool'''
198 self.log_command(hack_command)
199 subprocess.call(hack_command,
201 cwd=str(self.build_dir),
202 env=self.build_environ.environ.environ,
203 stdout=self.logger.logTxtFile,
204 stderr=subprocess.STDOUT)
208 # Runs make to build the module.
209 def make(self, nb_proc, make_opt):
213 command = command + " -j" + str(nb_proc)
214 command = command + " " + make_opt
215 self.log_command(command)
216 res = subprocess.call(command,
218 cwd=str(self.build_dir),
219 env=self.build_environ.environ.environ,
220 stdout=self.logger.logTxtFile,
221 stderr=subprocess.STDOUT)
229 # Runs msbuild to build the module.
230 def wmake(self, opt_nb_proc = None):
231 nbproc = self.get_nb_proc(opt_nb_proc)
233 hh = 'MSBUILD /m:%s' % str(nbproc)
235 hh += " " + src.printcolors.printcWarning("DEBUG")
240 if self.options.makeflags:
241 command = command + " " + self.options.makeflags
242 command = command + " /maxcpucount:" + str(nbproc)
244 command = command + " /p:Configuration=Debug"
246 command = command + " /p:Configuration=Release"
247 command = command + " ALL_BUILD.vcxproj"
249 self.log_command(command)
250 res = subprocess.call(command,
252 cwd=str(self.build_dir),
253 env=self.build_environ.environ.environ,
254 stdout=self.logger.logTxtFile,
255 stderr=subprocess.STDOUT)
263 # Runs 'make install'.
265 if self.config.VARS.dist_name=="Win":
266 command = 'msbuild INSTALL.vcxproj'
268 command = command + " /p:Configuration=Debug"
270 command = command + " /p:Configuration=Release"
272 command = 'make install'
274 self.log_command(command)
276 res = subprocess.call(command,
278 cwd=str(self.build_dir),
279 env=self.build_environ.environ.environ,
280 stdout=self.logger.logTxtFile,
281 stderr=subprocess.STDOUT)
291 if src.architecture.is_windows():
292 command = 'msbuild RUN_TESTS.vcxproj'
294 if self.product_info.build_source=="autotools" :
295 command = 'make check'
297 command = 'make test'
299 self.log_command(command)
301 res = subprocess.call(command,
303 cwd=str(self.build_dir),
304 env=self.launch_environ.environ.environ,
305 stdout=self.logger.logTxtFile,
306 stderr=subprocess.STDOUT)
314 # Performs a default build for this module.
315 def do_default_build(self, build_conf_options="", configure_options="", show_warning=True):
316 use_autotools = False
317 if 'use_autotools' in self.product_info:
318 uc = self.product_info.use_autotools
319 if uc in ['always', 'yes']:
322 use_autotools = self.options.autotools
325 self.use_autotools = use_autotools
328 if 'use_ctest' in self.product_info:
329 uc = self.product_info.use_ctest
330 if uc in ['always', 'yes']:
333 use_ctest = self.options.ctest
335 self.use_ctest = use_ctest
339 if use_autotools: cmd = "(autotools)"
340 if use_ctest: cmd = "(ctest)"
342 self.log("\n", 4, False)
343 self.log("%(module)s: Run default compilation method %(cmd)s\n" % \
344 { "module": self.module, "cmd": cmd }, 4)
347 if not self.prepare(): return self.get_result()
348 if not self.build_configure(build_conf_options): return self.get_result()
349 if not self.configure(configure_options): return self.get_result()
350 if not self.make(): return self.get_result()
351 if not self.install(): return self.get_result()
352 if not self.clean(): return self.get_result()
355 if self.config.VARS.dist_name=='Win':
356 if not self.wprepare(): return self.get_result()
357 if not self.cmake(): return self.get_result()
358 if not self.wmake(): return self.get_result()
359 if not self.install(): return self.get_result()
360 if not self.clean(): return self.get_result()
362 if not self.prepare(): return self.get_result()
363 if not self.cmake(): return self.get_result()
364 if not self.make(): return self.get_result()
365 if not self.install(): return self.get_result()
366 if not self.clean(): return self.get_result()
368 return self.get_result()
371 # Performs a build with a script.
372 def do_python_script_build(self, script):
374 self.logger.write(_("Compile %(module)s using script %(script)s\n") % \
375 { 'module': self.module, 'script': src.printcolors.printcLabel(script) }, 4)
378 pymodule = imp.load_source(self.product + "_compile_script", script)
379 retcode = pymodule.compil(self.config, self, self.logger)
381 __, exceptionValue, exceptionTraceback = sys.exc_info()
382 print(exceptionValue)
384 traceback.print_tb(exceptionTraceback)
385 traceback.print_exc()
389 def complete_environment(self, make_options):
390 assert self.build_environ is not None
391 # pass additional variables to environment (may be used by the build script)
392 self.build_environ.set("SOURCE_DIR", str(self.source_dir))
393 self.build_environ.set("INSTALL_DIR", str(self.install_dir))
394 self.build_environ.set("PRODUCT_INSTALL", str(self.install_dir))
395 self.build_environ.set("BUILD_DIR", str(self.build_dir))
396 self.build_environ.set("PRODUCT_BUILD", str(self.build_dir))
397 self.build_environ.set("MAKE_OPTIONS", make_options)
398 self.build_environ.set("DIST_NAME", self.config.VARS.dist_name)
399 self.build_environ.set("DIST_VERSION", self.config.VARS.dist_version)
400 self.build_environ.set("DIST", self.config.VARS.dist)
402 def do_batch_script_build(self, script):
403 # define make options (may not be used by the script)
404 nb_proc = src.get_cfg_param(self.product_info,"nb_proc", 0)
406 nb_proc = self.config.VARS.nb_proc
408 if src.architecture.is_windows():
409 make_options = "/maxcpucount:%s" % nb_proc
411 make_options = "-j%s" % nb_proc
413 self.log_command(" " + _("Run build script %s\n") % script)
414 self.complete_environment(make_options)
415 res = subprocess.call(script,
417 stdout=self.logger.logTxtFile,
418 stderr=subprocess.STDOUT,
419 cwd=str(self.build_dir),
420 env=self.build_environ.environ.environ)
427 def do_script_build(self, script):
428 extension = script.split('.')[-1]
429 if extension in ["bat","sh"]:
430 return self.do_batch_script_build(script)
431 if extension == "py":
432 return self.do_python_script_build(script)
434 msg = _("The script %s must have .sh, .bat or .py extension." % script)
435 raise src.SatException(msg)