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
26 C_COMPILE_ENV_LIST = ["CC",
35 """Class to handle all construction steps, like cmake, configure, make, ...
41 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 = False
52 if "debug" in self.product_info and self.product_info.debug == "yes":
53 self.debug_mode = True
54 self.verbose_mode = False
55 if "verbose" in self.product_info and self.product_info.verbose == "yes":
56 self.verbose_mode = True
59 # Shortcut method to log in log file.
60 def log(self, text, level, showInfo=True):
61 self.logger.write(text, level, showInfo)
62 self.logger.logTxtFile.write(src.printcolors.cleancolor(text))
66 # Shortcut method to log a command.
67 def log_command(self, command):
68 self.log("> %s\n" % command, 5)
71 # Prepares the environment.
72 # Build two environment: one for building and one for testing (launch).
75 if not self.build_dir.exists():
79 self.log(' build_dir = %s\n' % str(self.build_dir), 4)
80 self.log(' install_dir = %s\n' % str(self.install_dir), 4)
83 # add products in depend and opt_depend list recursively
84 environ_info = src.product.get_product_dependencies(self.config,
86 #environ_info.append(self.product_info.name)
88 # create build environment
89 self.build_environ = src.environment.SalomeEnviron(self.config,
90 src.environment.Environ(dict(os.environ)),
92 self.build_environ.silent = (self.config.USER.output_verbose_level < 5)
93 self.build_environ.set_full_environ(self.logger, environ_info)
95 # create runtime environment
96 self.launch_environ = src.environment.SalomeEnviron(self.config,
97 src.environment.Environ(dict(os.environ)),
99 self.launch_environ.silent = True # no need to show here
100 self.launch_environ.set_full_environ(self.logger, environ_info)
102 for ee in C_COMPILE_ENV_LIST:
103 vv = self.build_environ.get(ee)
105 self.log(" %s = %s\n" % (ee, vv), 4, False)
110 # Runs cmake with the given options.
111 def cmake(self, options=""):
113 cmake_option = options
114 # cmake_option +=' -DCMAKE_VERBOSE_MAKEFILE=ON -DSALOME_CMAKE_DEBUG=ON'
115 if 'cmake_options' in self.product_info:
116 cmake_option += " %s " % " ".join(
117 self.product_info.cmake_options.split())
121 cmake_option += " -DCMAKE_BUILD_TYPE=Debug"
123 cmake_option += " -DCMAKE_BUILD_TYPE=Release"
125 # add verbose option if specified in application for this product.
126 if self.verbose_mode:
127 cmake_option += " -DCMAKE_VERBOSE_MAKEFILE=ON"
129 # In case CMAKE_GENERATOR is defined in environment,
130 # use it in spite of automatically detect it
131 if 'cmake_generator' in self.config.APPLICATION:
132 cmake_option += " -DCMAKE_GENERATOR=%s" \
133 % self.config.APPLICATION.cmake_generator
135 command = ("cmake %s -DCMAKE_INSTALL_PREFIX=%s %s" %
136 (cmake_option, self.install_dir, self.source_dir))
138 self.log_command(command)
139 # for key in sorted(self.build_environ.environ.environ.keys()):
140 # print key, " ", self.build_environ.environ.environ[key]
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)
148 self.put_txt_log_in_appli_log_dir("cmake")
155 # Runs build_configure with the given options.
156 def build_configure(self, options=""):
158 if 'buildconfigure_options' in self.product_info:
159 options += " %s " % self.product_info.buildconfigure_options
161 command = str('%s/build_configure') % (self.source_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)
171 self.put_txt_log_in_appli_log_dir("build_configure")
178 # Runs configure with the given options.
179 def configure(self, options=""):
181 if 'configure_options' in self.product_info:
182 options += " %s " % self.product_info.configure_options
184 command = "%s/configure --prefix=%s" % (self.source_dir,
185 str(self.install_dir))
187 command = command + " " + options
188 self.log_command(command)
190 res = subprocess.call(command,
192 cwd=str(self.build_dir),
193 env=self.build_environ.environ.environ,
194 stdout=self.logger.logTxtFile,
195 stderr=subprocess.STDOUT)
197 self.put_txt_log_in_appli_log_dir("configure")
203 def hack_libtool(self):
204 if not os.path.exists(str(self.build_dir + 'libtool')):
207 lf = open(os.path.join(str(self.build_dir), "libtool"), 'r')
208 for line in lf.readlines():
209 if 'hack_libtool' in line:
212 # fix libtool by replacing CC="<compil>" with hack_libtool function
213 hack_command='''sed -i "s%^CC=\\"\(.*\)\\"%hack_libtool() { \\n\\
214 if test \\"\$(echo \$@ | grep -E '\\\\\\-L/usr/lib(/../lib)?(64)? ')\\" == \\\"\\\" \\n\\
216 cmd=\\"\\1 \$@\\"\\n\\
218 cmd=\\"\\1 \\"\`echo \$@ | sed -r -e 's|(.*)-L/usr/lib(/../lib)?(64)? (.*)|\\\\\\1\\\\\\4 -L/usr/lib\\\\\\3|g'\`\\n\\
222 CC=\\"hack_libtool\\"%g" libtool'''
224 self.log_command(hack_command)
225 subprocess.call(hack_command,
227 cwd=str(self.build_dir),
228 env=self.build_environ.environ.environ,
229 stdout=self.logger.logTxtFile,
230 stderr=subprocess.STDOUT)
234 # Runs make to build the module.
235 def make(self, nb_proc, make_opt=""):
239 command = command + " -j" + str(nb_proc)
240 command = command + " " + make_opt
241 self.log_command(command)
242 res = subprocess.call(command,
244 cwd=str(self.build_dir),
245 env=self.build_environ.environ.environ,
246 stdout=self.logger.logTxtFile,
247 stderr=subprocess.STDOUT)
248 self.put_txt_log_in_appli_log_dir("make")
255 # Runs msbuild to build the module.
256 def wmake(self,nb_proc, opt_nb_proc = None):
258 hh = 'MSBUILD /m:%s' % str(nb_proc)
260 hh += " " + src.printcolors.printcWarning("DEBUG")
263 command = command + " /maxcpucount:" + str(nb_proc)
265 command = command + " /p:Configuration=Debug"
267 command = command + " /p:Configuration=Release"
268 command = command + " ALL_BUILD.vcxproj"
270 self.log_command(command)
271 res = subprocess.call(command,
273 cwd=str(self.build_dir),
274 env=self.build_environ.environ.environ,
275 stdout=self.logger.logTxtFile,
276 stderr=subprocess.STDOUT)
278 self.put_txt_log_in_appli_log_dir("make")
285 # Runs 'make install'.
287 if self.config.VARS.dist_name=="Win":
288 command = 'msbuild INSTALL.vcxproj'
290 command = command + " /p:Configuration=Debug"
292 command = command + " /p:Configuration=Release"
294 command = 'make install'
296 self.log_command(command)
298 res = subprocess.call(command,
300 cwd=str(self.build_dir),
301 env=self.build_environ.environ.environ,
302 stdout=self.logger.logTxtFile,
303 stderr=subprocess.STDOUT)
305 self.put_txt_log_in_appli_log_dir("makeinstall")
313 def check(self, command=""):
314 if src.architecture.is_windows():
315 cmd = 'msbuild RUN_TESTS.vcxproj'
317 if self.product_info.build_source=="autotools" :
325 self.log_command(cmd)
327 res = subprocess.call(cmd,
329 cwd=str(self.build_dir),
330 env=self.launch_environ.environ.environ,
331 stdout=self.logger.logTxtFile,
332 stderr=subprocess.STDOUT)
340 # Performs a default build for this module.
341 def do_default_build(self,
342 build_conf_options="",
343 configure_options="",
345 use_autotools = False
346 if 'use_autotools' in self.product_info:
347 uc = self.product_info.use_autotools
348 if uc in ['always', 'yes']:
351 use_autotools = self.options.autotools
354 self.use_autotools = use_autotools
357 if 'use_ctest' in self.product_info:
358 uc = self.product_info.use_ctest
359 if uc in ['always', 'yes']:
362 use_ctest = self.options.ctest
364 self.use_ctest = use_ctest
368 if use_autotools: cmd = "(autotools)"
369 if use_ctest: cmd = "(ctest)"
371 self.log("\n", 4, False)
372 self.log("%(module)s: Run default compilation method %(cmd)s\n" % \
373 { "module": self.module, "cmd": cmd }, 4)
376 if not self.prepare(): return self.get_result()
377 if not self.build_configure(
378 build_conf_options): return self.get_result()
379 if not self.configure(configure_options): return self.get_result()
380 if not self.make(): return self.get_result()
381 if not self.install(): return self.get_result()
382 if not self.clean(): return self.get_result()
385 if self.config.VARS.dist_name=='Win':
386 if not self.wprepare(): return self.get_result()
387 if not self.cmake(): return self.get_result()
388 if not self.wmake(): return self.get_result()
389 if not self.install(): return self.get_result()
390 if not self.clean(): return self.get_result()
392 if not self.prepare(): return self.get_result()
393 if not self.cmake(): return self.get_result()
394 if not self.make(): return self.get_result()
395 if not self.install(): return self.get_result()
396 if not self.clean(): return self.get_result()
398 return self.get_result()
401 # Performs a build with a script.
402 def do_python_script_build(self, script, nb_proc):
404 self.logger.write(_("Compile %(product)s using script %(script)s\n") % \
405 { 'product': self.product_info.name,
406 'script': src.printcolors.printcLabel(script) }, 4)
409 product = self.product_info.name
410 pymodule = imp.load_source(product + "_compile_script", script)
411 self.nb_proc = nb_proc
412 retcode = pymodule.compil(self.config, self, self.logger)
414 __, exceptionValue, exceptionTraceback = sys.exc_info()
415 self.logger.write(str(exceptionValue), 1)
417 traceback.print_tb(exceptionTraceback)
418 traceback.print_exc()
421 self.put_txt_log_in_appli_log_dir("script")
425 def complete_environment(self, make_options):
426 assert self.build_environ is not None
427 # pass additional variables to environment
428 # (may be used by the build script)
429 self.build_environ.set("SOURCE_DIR", str(self.source_dir))
430 self.build_environ.set("INSTALL_DIR", str(self.install_dir))
431 self.build_environ.set("PRODUCT_INSTALL", str(self.install_dir))
432 self.build_environ.set("BUILD_DIR", str(self.build_dir))
433 self.build_environ.set("PRODUCT_BUILD", str(self.build_dir))
434 self.build_environ.set("MAKE_OPTIONS", make_options)
435 self.build_environ.set("DIST_NAME", self.config.VARS.dist_name)
436 self.build_environ.set("DIST_VERSION", self.config.VARS.dist_version)
437 self.build_environ.set("DIST", self.config.VARS.dist)
438 self.build_environ.set("VERSION", self.product_info.version)
440 def do_batch_script_build(self, script, nb_proc):
442 if src.architecture.is_windows():
443 make_options = "/maxcpucount:%s" % nb_proc
445 make_options = "-j%s" % nb_proc
447 self.log_command(" " + _("Run build script %s\n") % script)
448 self.complete_environment(make_options)
450 res = subprocess.call(script,
452 stdout=self.logger.logTxtFile,
453 stderr=subprocess.STDOUT,
454 cwd=str(self.build_dir),
455 env=self.build_environ.environ.environ)
457 self.put_txt_log_in_appli_log_dir("script")
463 def do_script_build(self, script, number_of_proc=0):
464 # define make options (may not be used by the script)
465 if number_of_proc==0:
466 nb_proc = src.get_cfg_param(self.product_info,"nb_proc", 0)
468 nb_proc = self.config.VARS.nb_proc
470 nb_proc = min(number_of_proc, self.config.VARS.nb_proc)
472 extension = script.split('.')[-1]
473 if extension in ["bat","sh"]:
474 return self.do_batch_script_build(script, nb_proc)
475 if extension == "py":
476 return self.do_python_script_build(script, nb_proc)
478 msg = _("The script %s must have .sh, .bat or .py extension." % script)
479 raise src.SatException(msg)
481 def put_txt_log_in_appli_log_dir(self, file_name):
482 '''Put the txt log (that contain the system logs, like make command
483 output) in the directory <APPLICATION DIR>/LOGS/<product_name>/
485 :param file_name Str: the name of the file to write
487 if self.logger.logTxtFile == sys.__stdout__:
489 dir_where_to_put = os.path.join(self.config.APPLICATION.workdir,
491 self.product_info.name)
492 file_path = os.path.join(dir_where_to_put, file_name)
493 src.ensure_path_exists(dir_where_to_put)
494 # write the logTxtFile copy it to the destination, and then recreate
496 self.logger.logTxtFile.close()
497 shutil.move(self.logger.txtFilePath, file_path)
498 self.logger.logTxtFile = open(str(self.logger.txtFilePath), 'w')
499 self.logger.logTxtFile.write(open(file_path, "r").read())