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, ...
36 def __init__(self, config, logger, product_info, options = src.options.OptResult(), debug_mode=False, check_src=True):
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)
45 self.debug_mode = debug_mode
47 if not self.source_dir.exists() and check_src:
48 raise src.SatException(_("No sources found for product %(product)s in %(source_dir)s" % \
49 { "product": self.product_info.name, "source_dir": self.source_dir } ))
52 # check that required modules exist
53 for dep in self.product_info.depend:
54 assert dep in self.config.TOOLS.src.product_info, "UNDEFINED product: %s" % dep
55 dep_info = self.config.TOOLS.src.product_info[dep]
56 if 'install_dir' in dep_info and not os.path.exists(dep_info.install_dir):
57 raise src.SatException(_("Module %s is required") % dep)
61 # Shortcut method to log in log file.
62 def log(self, text, level, showInfo=True):
63 self.logger.write(text, level, showInfo)
64 self.logger.logTxtFile.write(src.printcolors.cleancolor(text))
68 # Shortcut method to log a command.
69 def log_command(self, command):
70 self.log("> %s\n" % command, 5)
73 # Prepares the environment.
74 # Build two environment: one for building and one for testing (launch).
77 if not self.build_dir.exists():
81 self.log(' build_dir = %s\n' % str(self.build_dir), 4)
82 self.log(' install_dir = %s\n' % str(self.install_dir), 4)
85 # add products in depend and opt_depend list recursively
86 environ_info = src.product.get_product_dependencies(self.config, self.product_info)
87 #environ_info.append(self.product_info.name)
89 # create build environment
90 self.build_environ = src.environment.SalomeEnviron(self.config, src.environment.Environ(dict(os.environ)), True)
91 self.build_environ.silent = (self.config.USER.output_verbose_level < 5)
92 self.build_environ.set_full_environ(self.logger, environ_info)
94 # create runtime environment
95 self.launch_environ = src.environment.SalomeEnviron(self.config, src.environment.Environ(dict(os.environ)), False)
96 self.launch_environ.silent = True # no need to show here
97 self.launch_environ.set_full_environ(self.logger, environ_info)
99 for ee in C_COMPILE_ENV_LIST:
100 vv = self.build_environ.get(ee)
102 self.log(" %s = %s\n" % (ee, vv), 4, False)
107 # Runs cmake with the given options.
108 def cmake(self, options=""):
110 cmake_option = options
111 # cmake_option +=' -DCMAKE_VERBOSE_MAKEFILE=ON -DSALOME_CMAKE_DEBUG=ON'
112 if 'cmake_options' in self.product_info:
113 cmake_option += " %s " % " ".join(self.product_info.cmake_options.split())
117 cmake_option += " -DCMAKE_BUILD_TYPE=Debug"
119 cmake_option += " -DCMAKE_BUILD_TYPE=Release"
121 command = ("cmake %s -DCMAKE_INSTALL_PREFIX=%s %s" %
122 (cmake_option, self.install_dir, self.source_dir))
124 self.log_command(command)
125 res = subprocess.call(command,
127 cwd=str(self.build_dir),
128 env=self.build_environ.environ.environ,
129 stdout=self.logger.logTxtFile,
130 stderr=subprocess.STDOUT)
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)
161 # Runs configure with the given options.
162 def configure(self, options=""):
164 if 'configure_options' in self.product_info:
165 options += " %s " % self.product_info.configure_options
167 command = "%s/configure --prefix=%s" % (self.source_dir, str(self.install_dir))
169 command = command + " " + options
170 self.log_command(command)
172 res = subprocess.call(command,
174 cwd=str(self.build_dir),
175 env=self.build_environ.environ.environ,
176 stdout=self.logger.logTxtFile,
177 stderr=subprocess.STDOUT)
184 def hack_libtool(self):
185 if not os.path.exists(str(self.build_dir + 'libtool')):
188 lf = open(os.path.join(str(self.build_dir), "libtool"), 'r')
189 for line in lf.readlines():
190 if 'hack_libtool' in line:
193 # fix libtool by replacing CC="<compil>" with hack_libtool function
194 hack_command='''sed -i "s%^CC=\\"\(.*\)\\"%hack_libtool() { \\n\\
195 if test \\"\$(echo \$@ | grep -E '\\\\\\-L/usr/lib(/../lib)?(64)? ')\\" == \\\"\\\" \\n\\
197 cmd=\\"\\1 \$@\\"\\n\\
199 cmd=\\"\\1 \\"\`echo \$@ | sed -r -e 's|(.*)-L/usr/lib(/../lib)?(64)? (.*)|\\\\\\1\\\\\\4 -L/usr/lib\\\\\\3|g'\`\\n\\
203 CC=\\"hack_libtool\\"%g" libtool'''
205 self.log_command(hack_command)
206 subprocess.call(hack_command,
208 cwd=str(self.build_dir),
209 env=self.build_environ.environ.environ,
210 stdout=self.logger.logTxtFile,
211 stderr=subprocess.STDOUT)
215 # Runs make to build the module.
216 def make(self, nb_proc, make_opt):
220 command = command + " -j" + str(nb_proc)
221 command = command + " " + make_opt
222 self.log_command(command)
223 res = subprocess.call(command,
225 cwd=str(self.build_dir),
226 env=self.build_environ.environ.environ,
227 stdout=self.logger.logTxtFile,
228 stderr=subprocess.STDOUT)
236 # Runs msbuild to build the module.
237 def wmake(self, opt_nb_proc = None):
238 nbproc = self.get_nb_proc(opt_nb_proc)
240 hh = 'MSBUILD /m:%s' % str(nbproc)
242 hh += " " + src.printcolors.printcWarning("DEBUG")
247 if self.options.makeflags:
248 command = command + " " + self.options.makeflags
249 command = command + " /maxcpucount:" + str(nbproc)
251 command = command + " /p:Configuration=Debug"
253 command = command + " /p:Configuration=Release"
254 command = command + " ALL_BUILD.vcxproj"
256 self.log_command(command)
257 res = subprocess.call(command,
259 cwd=str(self.build_dir),
260 env=self.build_environ.environ.environ,
261 stdout=self.logger.logTxtFile,
262 stderr=subprocess.STDOUT)
270 # Runs 'make install'.
272 if self.config.VARS.dist_name=="Win":
273 command = 'msbuild INSTALL.vcxproj'
275 command = command + " /p:Configuration=Debug"
277 command = command + " /p:Configuration=Release"
279 command = 'make install'
281 self.log_command(command)
283 res = subprocess.call(command,
285 cwd=str(self.build_dir),
286 env=self.build_environ.environ.environ,
287 stdout=self.logger.logTxtFile,
288 stderr=subprocess.STDOUT)
298 if src.architecture.is_windows():
299 command = 'msbuild RUN_TESTS.vcxproj'
301 if self.use_autotools :
302 command = 'make check'
304 command = 'make test'
306 self.log_command(command)
308 res = subprocess.call(command,
310 cwd=str(self.build_dir),
311 env=self.launch_environ.environ.environ,
312 stdout=self.logger.logTxtFile,
313 stderr=subprocess.STDOUT)
321 # Performs a default build for this module.
322 def do_default_build(self, build_conf_options="", configure_options="", show_warning=True):
323 use_autotools = False
324 if 'use_autotools' in self.product_info:
325 uc = self.product_info.use_autotools
326 if uc in ['always', 'yes']:
329 use_autotools = self.options.autotools
332 self.use_autotools = use_autotools
335 if 'use_ctest' in self.product_info:
336 uc = self.product_info.use_ctest
337 if uc in ['always', 'yes']:
340 use_ctest = self.options.ctest
342 self.use_ctest = use_ctest
346 if use_autotools: cmd = "(autotools)"
347 if use_ctest: cmd = "(ctest)"
349 self.log("\n", 4, False)
350 self.log("%(module)s: Run default compilation method %(cmd)s\n" % \
351 { "module": self.module, "cmd": cmd }, 4)
354 if not self.prepare(): return self.get_result()
355 if not self.build_configure(build_conf_options): return self.get_result()
356 if not self.configure(configure_options): return self.get_result()
357 if not self.make(): return self.get_result()
358 if not self.install(): return self.get_result()
359 if not self.clean(): return self.get_result()
362 if self.config.VARS.dist_name=='Win':
363 if not self.wprepare(): return self.get_result()
364 if not self.cmake(): return self.get_result()
365 if not self.wmake(): return self.get_result()
366 if not self.install(): return self.get_result()
367 if not self.clean(): return self.get_result()
369 if not self.prepare(): return self.get_result()
370 if not self.cmake(): return self.get_result()
371 if not self.make(): return self.get_result()
372 if not self.install(): return self.get_result()
373 if not self.clean(): return self.get_result()
375 return self.get_result()
378 # Performs a build with a script.
379 def do_python_script_build(self, script):
381 self.logger.write(_("Compile %(module)s using script %(script)s\n") % \
382 { 'module': self.module, 'script': src.printcolors.printcLabel(script) }, 4)
385 pymodule = imp.load_source(self.product + "_compile_script", script)
386 retcode = pymodule.compil(self.config, self, self.logger)
388 __, exceptionValue, exceptionTraceback = sys.exc_info()
389 print(exceptionValue)
391 traceback.print_tb(exceptionTraceback)
392 traceback.print_exc()
396 def complete_environment(self, make_options):
397 assert self.build_environ is not None
398 # pass additional variables to environment (may be used by the build script)
399 self.build_environ.set("SOURCE_DIR", str(self.source_dir))
400 self.build_environ.set("INSTALL_DIR", str(self.install_dir))
401 self.build_environ.set("PRODUCT_INSTALL", str(self.install_dir))
402 self.build_environ.set("BUILD_DIR", str(self.build_dir))
403 self.build_environ.set("PRODUCT_BUILD", str(self.build_dir))
404 self.build_environ.set("MAKE_OPTIONS", make_options)
405 self.build_environ.set("DIST_NAME", self.config.VARS.dist_name)
406 self.build_environ.set("DIST_VERSION", self.config.VARS.dist_version)
407 self.build_environ.set("DIST", self.config.VARS.dist)
409 def do_batch_script_build(self, script):
410 # define make options (may not be used by the script)
411 nb_proc = src.get_cfg_param(self.product_info,"nb_proc", 0)
413 nb_proc = self.config.VARS.nb_proc
415 if src.architecture.is_windows():
416 make_options = "/maxcpucount:%s" % nb_proc
418 make_options = "-j%s" % nb_proc
420 self.log_command(" " + _("Run build script %s\n") % script)
421 self.complete_environment(make_options)
422 res = subprocess.call(script,
424 stdout=self.logger.logTxtFile,
425 stderr=subprocess.STDOUT,
426 cwd=str(self.build_dir),
427 env=self.build_environ.environ.environ)
434 def do_script_build(self, script):
435 extension = script.split('.')[-1]
436 if extension in ["bat","sh"]:
437 return self.do_batch_script_build(script)
438 if extension == "py":
439 return self.do_python_script_build(script)
441 msg = _("The script %s must have .sh, .bat or .py extension." % script)
442 raise src.SatException(msg)