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(),
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)
50 self.debug_mode = False
51 if "debug" in self.product_info and self.product_info.debug == "yes":
52 self.debug_mode = True
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))
62 # Shortcut method to log a command.
63 def log_command(self, command):
64 self.log("> %s\n" % command, 5)
67 # Prepares the environment.
68 # Build two environment: one for building and one for testing (launch).
71 if not self.build_dir.exists():
75 self.log(' build_dir = %s\n' % str(self.build_dir), 4)
76 self.log(' install_dir = %s\n' % str(self.install_dir), 4)
79 # add products in depend and opt_depend list recursively
80 environ_info = src.product.get_product_dependencies(self.config,
82 #environ_info.append(self.product_info.name)
84 # create build environment
85 self.build_environ = src.environment.SalomeEnviron(self.config,
86 src.environment.Environ(dict(os.environ)),
88 self.build_environ.silent = (self.config.USER.output_verbose_level < 5)
89 self.build_environ.set_full_environ(self.logger, environ_info)
91 # create runtime environment
92 self.launch_environ = src.environment.SalomeEnviron(self.config,
93 src.environment.Environ(dict(os.environ)),
95 self.launch_environ.silent = True # no need to show here
96 self.launch_environ.set_full_environ(self.logger, environ_info)
98 for ee in C_COMPILE_ENV_LIST:
99 vv = self.build_environ.get(ee)
101 self.log(" %s = %s\n" % (ee, vv), 4, False)
106 # Runs cmake with the given options.
107 def cmake(self, options=""):
109 cmake_option = options
110 # cmake_option +=' -DCMAKE_VERBOSE_MAKEFILE=ON -DSALOME_CMAKE_DEBUG=ON'
111 if 'cmake_options' in self.product_info:
112 cmake_option += " %s " % " ".join(self.product_info.cmake_options.split())
116 cmake_option += " -DCMAKE_BUILD_TYPE=Debug"
118 cmake_option += " -DCMAKE_BUILD_TYPE=Release"
120 command = ("cmake %s -DCMAKE_INSTALL_PREFIX=%s %s" %
121 (cmake_option, self.install_dir, self.source_dir))
123 self.log_command(command)
124 res = subprocess.call(command,
126 cwd=str(self.build_dir),
127 env=self.build_environ.environ.environ,
128 stdout=self.logger.logTxtFile,
129 stderr=subprocess.STDOUT)
132 self.put_txt_log_in_appli_log_dir("cmake")
138 # Runs build_configure with the given options.
139 def build_configure(self, options=""):
141 if 'buildconfigure_options' in self.product_info:
142 options += " %s " % self.product_info.buildconfigure_options
144 command = str('%s/build_configure') % (self.source_dir)
145 command = command + " " + options
146 self.log_command(command)
148 res = subprocess.call(command,
150 cwd=str(self.build_dir),
151 env=self.build_environ.environ.environ,
152 stdout=self.logger.logTxtFile,
153 stderr=subprocess.STDOUT)
156 self.put_txt_log_in_appli_log_dir("build_configure")
162 # Runs configure with the given options.
163 def configure(self, options=""):
165 if 'configure_options' in self.product_info:
166 options += " %s " % self.product_info.configure_options
168 command = "%s/configure --prefix=%s" % (self.source_dir, str(self.install_dir))
170 command = command + " " + options
171 self.log_command(command)
173 res = subprocess.call(command,
175 cwd=str(self.build_dir),
176 env=self.build_environ.environ.environ,
177 stdout=self.logger.logTxtFile,
178 stderr=subprocess.STDOUT)
181 self.put_txt_log_in_appli_log_dir("configure")
186 def hack_libtool(self):
187 if not os.path.exists(str(self.build_dir + 'libtool')):
190 lf = open(os.path.join(str(self.build_dir), "libtool"), 'r')
191 for line in lf.readlines():
192 if 'hack_libtool' in line:
195 # fix libtool by replacing CC="<compil>" with hack_libtool function
196 hack_command='''sed -i "s%^CC=\\"\(.*\)\\"%hack_libtool() { \\n\\
197 if test \\"\$(echo \$@ | grep -E '\\\\\\-L/usr/lib(/../lib)?(64)? ')\\" == \\\"\\\" \\n\\
199 cmd=\\"\\1 \$@\\"\\n\\
201 cmd=\\"\\1 \\"\`echo \$@ | sed -r -e 's|(.*)-L/usr/lib(/../lib)?(64)? (.*)|\\\\\\1\\\\\\4 -L/usr/lib\\\\\\3|g'\`\\n\\
205 CC=\\"hack_libtool\\"%g" libtool'''
207 self.log_command(hack_command)
208 subprocess.call(hack_command,
210 cwd=str(self.build_dir),
211 env=self.build_environ.environ.environ,
212 stdout=self.logger.logTxtFile,
213 stderr=subprocess.STDOUT)
217 # Runs make to build the module.
218 def make(self, nb_proc, make_opt=""):
222 command = command + " -j" + str(nb_proc)
223 command = command + " " + make_opt
224 self.log_command(command)
225 res = subprocess.call(command,
227 cwd=str(self.build_dir),
228 env=self.build_environ.environ.environ,
229 stdout=self.logger.logTxtFile,
230 stderr=subprocess.STDOUT)
233 self.put_txt_log_in_appli_log_dir("make")
239 # Runs msbuild to build the module.
240 def wmake(self, opt_nb_proc = None):
241 nbproc = self.get_nb_proc(opt_nb_proc)
243 hh = 'MSBUILD /m:%s' % str(nbproc)
245 hh += " " + src.printcolors.printcWarning("DEBUG")
250 if self.options.makeflags:
251 command = command + " " + self.options.makeflags
252 command = command + " /maxcpucount:" + str(nbproc)
254 command = command + " /p:Configuration=Debug"
256 command = command + " /p:Configuration=Release"
257 command = command + " ALL_BUILD.vcxproj"
259 self.log_command(command)
260 res = subprocess.call(command,
262 cwd=str(self.build_dir),
263 env=self.build_environ.environ.environ,
264 stdout=self.logger.logTxtFile,
265 stderr=subprocess.STDOUT)
268 self.put_txt_log_in_appli_log_dir("make")
274 # Runs 'make install'.
276 if self.config.VARS.dist_name=="Win":
277 command = 'msbuild INSTALL.vcxproj'
279 command = command + " /p:Configuration=Debug"
281 command = command + " /p:Configuration=Release"
283 command = 'make install'
285 self.log_command(command)
287 res = subprocess.call(command,
289 cwd=str(self.build_dir),
290 env=self.build_environ.environ.environ,
291 stdout=self.logger.logTxtFile,
292 stderr=subprocess.STDOUT)
295 self.put_txt_log_in_appli_log_dir("makeinstall")
303 if src.architecture.is_windows():
304 command = 'msbuild RUN_TESTS.vcxproj'
306 if self.product_info.build_source=="autotools" :
307 command = 'make check'
309 command = 'make test'
311 self.log_command(command)
313 res = subprocess.call(command,
315 cwd=str(self.build_dir),
316 env=self.launch_environ.environ.environ,
317 stdout=self.logger.logTxtFile,
318 stderr=subprocess.STDOUT)
326 # Performs a default build for this module.
327 def do_default_build(self, build_conf_options="", configure_options="", show_warning=True):
328 use_autotools = False
329 if 'use_autotools' in self.product_info:
330 uc = self.product_info.use_autotools
331 if uc in ['always', 'yes']:
334 use_autotools = self.options.autotools
337 self.use_autotools = use_autotools
340 if 'use_ctest' in self.product_info:
341 uc = self.product_info.use_ctest
342 if uc in ['always', 'yes']:
345 use_ctest = self.options.ctest
347 self.use_ctest = use_ctest
351 if use_autotools: cmd = "(autotools)"
352 if use_ctest: cmd = "(ctest)"
354 self.log("\n", 4, False)
355 self.log("%(module)s: Run default compilation method %(cmd)s\n" % \
356 { "module": self.module, "cmd": cmd }, 4)
359 if not self.prepare(): return self.get_result()
360 if not self.build_configure(build_conf_options): return self.get_result()
361 if not self.configure(configure_options): return self.get_result()
362 if not self.make(): return self.get_result()
363 if not self.install(): return self.get_result()
364 if not self.clean(): return self.get_result()
367 if self.config.VARS.dist_name=='Win':
368 if not self.wprepare(): return self.get_result()
369 if not self.cmake(): return self.get_result()
370 if not self.wmake(): return self.get_result()
371 if not self.install(): return self.get_result()
372 if not self.clean(): return self.get_result()
374 if not self.prepare(): return self.get_result()
375 if not self.cmake(): 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()
380 return self.get_result()
383 # Performs a build with a script.
384 def do_python_script_build(self, script, nb_proc):
386 self.logger.write(_("Compile %(product)s using script %(script)s\n") % \
387 { 'product': self.product_info.name,
388 'script': src.printcolors.printcLabel(script) }, 4)
391 product = self.product_info.name
392 pymodule = imp.load_source(product + "_compile_script", script)
393 self.nb_proc = nb_proc
394 retcode = pymodule.compil(self.config, self, self.logger)
395 self.put_txt_log_in_appli_log_dir("script")
397 __, exceptionValue, exceptionTraceback = sys.exc_info()
398 self.logger.write(str(exceptionValue), 1)
400 traceback.print_tb(exceptionTraceback)
401 traceback.print_exc()
406 def complete_environment(self, make_options):
407 assert self.build_environ is not None
408 # pass additional variables to environment (may be used by the build script)
409 self.build_environ.set("SOURCE_DIR", str(self.source_dir))
410 self.build_environ.set("INSTALL_DIR", str(self.install_dir))
411 self.build_environ.set("PRODUCT_INSTALL", str(self.install_dir))
412 self.build_environ.set("BUILD_DIR", str(self.build_dir))
413 self.build_environ.set("PRODUCT_BUILD", str(self.build_dir))
414 self.build_environ.set("MAKE_OPTIONS", make_options)
415 self.build_environ.set("DIST_NAME", self.config.VARS.dist_name)
416 self.build_environ.set("DIST_VERSION", self.config.VARS.dist_version)
417 self.build_environ.set("DIST", self.config.VARS.dist)
418 self.build_environ.set("VERSION", self.product_info.version)
420 def do_batch_script_build(self, script, nb_proc):
422 if src.architecture.is_windows():
423 make_options = "/maxcpucount:%s" % nb_proc
425 make_options = "-j%s" % nb_proc
427 self.log_command(" " + _("Run build script %s\n") % script)
428 self.complete_environment(make_options)
429 res = subprocess.call(script,
431 stdout=self.logger.logTxtFile,
432 stderr=subprocess.STDOUT,
433 cwd=str(self.build_dir),
434 env=self.build_environ.environ.environ)
437 self.put_txt_log_in_appli_log_dir("script")
442 def do_script_build(self, script, number_of_proc=0):
443 # define make options (may not be used by the script)
444 if number_of_proc==0:
445 nb_proc = src.get_cfg_param(self.product_info,"nb_proc", 0)
447 nb_proc = self.config.VARS.nb_proc
449 nb_proc = min(number_of_proc, self.config.VARS.nb_proc)
451 extension = script.split('.')[-1]
452 if extension in ["bat","sh"]:
453 return self.do_batch_script_build(script, nb_proc)
454 if extension == "py":
455 return self.do_python_script_build(script, nb_proc)
457 msg = _("The script %s must have .sh, .bat or .py extension." % script)
458 raise src.SatException(msg)
460 def put_txt_log_in_appli_log_dir(self, file_name):
461 '''Put the txt log (that contain the system logs, like make command
462 output) in the directory <APPLICATION DIR>/LOGS/<product_name>/
464 :param file_name Str: the name of the file to write
466 if self.logger.logTxtFile == sys.__stdout__:
468 dir_where_to_put = os.path.join(self.config.APPLICATION.workdir,
470 self.product_info.name)
471 file_path = os.path.join(dir_where_to_put, file_name)
472 src.ensure_path_exists(dir_where_to_put)
473 # write the logTxtFile copy it to the destination, and then recreate
475 self.logger.logTxtFile.close()
476 os.rename(self.logger.txtFilePath, file_path)
477 self.logger.logTxtFile = open(str(self.logger.txtFilePath), 'w')
478 self.logger.logTxtFile.write(open(file_path, "r").read())