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, 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)
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)
94 for ee in C_COMPILE_ENV_LIST:
95 vv = self.build_environ.get(ee)
97 self.log(" %s = %s\n" % (ee, vv), 4, False)
102 # Runs cmake with the given options.
103 def cmake(self, options=""):
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())
112 cmake_option += " -DCMAKE_BUILD_TYPE=Debug"
114 cmake_option += " -DCMAKE_BUILD_TYPE=Release"
116 command = ("cmake %s -DCMAKE_INSTALL_PREFIX=%s %s" %
117 (cmake_option, self.install_dir, self.source_dir))
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,
124 cwd=str(self.build_dir),
125 env=self.build_environ.environ.environ,
126 stdout=self.logger.logTxtFile,
127 stderr=subprocess.STDOUT)
135 # Runs build_configure with the given options.
136 def build_configure(self, options=""):
138 if 'buildconfigure_options' in self.product_info:
139 options += " %s " % self.product_info.buildconfigure_options
141 command = str('%s/build_configure') % (self.source_dir)
142 command = command + " " + options
143 self.log_command(command)
145 res = subprocess.call(command,
147 cwd=str(self.build_dir),
148 env=self.build_environ.environ.environ,
149 stdout=self.logger.logTxtFile,
150 stderr=subprocess.STDOUT)
158 # Runs configure with the given options.
159 def configure(self, options=""):
161 if 'configure_options' in self.product_info:
162 options += " %s " % self.product_info.configure_options
164 command = "%s/configure --prefix=%s" % (self.source_dir, str(self.install_dir))
166 command = command + " " + options
167 self.log_command(command)
169 res = subprocess.call(command,
171 cwd=str(self.build_dir),
172 env=self.build_environ.environ.environ,
173 stdout=self.logger.logTxtFile,
174 stderr=subprocess.STDOUT)
181 def hack_libtool(self):
182 if not os.path.exists(str(self.build_dir + 'libtool')):
185 lf = open(os.path.join(str(self.build_dir), "libtool"), 'r')
186 for line in lf.readlines():
187 if 'hack_libtool' in line:
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\\
194 cmd=\\"\\1 \$@\\"\\n\\
196 cmd=\\"\\1 \\"\`echo \$@ | sed -r -e 's|(.*)-L/usr/lib(/../lib)?(64)? (.*)|\\\\\\1\\\\\\4 -L/usr/lib\\\\\\3|g'\`\\n\\
200 CC=\\"hack_libtool\\"%g" libtool'''
202 self.log_command(hack_command)
203 subprocess.call(hack_command,
205 cwd=str(self.build_dir),
206 env=self.build_environ.environ.environ,
207 stdout=self.logger.logTxtFile,
208 stderr=subprocess.STDOUT)
212 # Runs make to build the module.
213 def make(self, nb_proc, make_opt=""):
217 command = command + " -j" + str(nb_proc)
218 command = command + " " + make_opt
219 self.log_command(command)
220 res = subprocess.call(command,
222 cwd=str(self.build_dir),
223 env=self.build_environ.environ.environ,
224 stdout=self.logger.logTxtFile,
225 stderr=subprocess.STDOUT)
233 # Runs msbuild to build the module.
234 def wmake(self,nb_proc, opt_nb_proc = None):
236 hh = 'MSBUILD /m:%s' % str(nb_proc)
238 hh += " " + src.printcolors.printcWarning("DEBUG")
241 command = command + " /maxcpucount:" + str(nb_proc)
243 command = command + " /p:Configuration=Debug"
245 command = command + " /p:Configuration=Release"
246 command = command + " ALL_BUILD.vcxproj"
248 self.log_command(command)
249 res = subprocess.call(command,
251 cwd=str(self.build_dir),
252 env=self.build_environ.environ.environ,
253 stdout=self.logger.logTxtFile,
254 stderr=subprocess.STDOUT)
262 # Runs 'make install'.
264 if self.config.VARS.dist_name=="Win":
265 command = 'msbuild INSTALL.vcxproj'
267 command = command + " /p:Configuration=Debug"
269 command = command + " /p:Configuration=Release"
271 command = 'make install'
273 self.log_command(command)
275 res = subprocess.call(command,
277 cwd=str(self.build_dir),
278 env=self.build_environ.environ.environ,
279 stdout=self.logger.logTxtFile,
280 stderr=subprocess.STDOUT)
290 if src.architecture.is_windows():
291 command = 'msbuild RUN_TESTS.vcxproj'
293 if self.product_info.build_source=="autotools" :
294 command = 'make check'
296 command = 'make test'
298 self.log_command(command)
300 res = subprocess.call(command,
302 cwd=str(self.build_dir),
303 env=self.launch_environ.environ.environ,
304 stdout=self.logger.logTxtFile,
305 stderr=subprocess.STDOUT)
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']:
321 use_autotools = self.options.autotools
324 self.use_autotools = use_autotools
327 if 'use_ctest' in self.product_info:
328 uc = self.product_info.use_ctest
329 if uc in ['always', 'yes']:
332 use_ctest = self.options.ctest
334 self.use_ctest = use_ctest
338 if use_autotools: cmd = "(autotools)"
339 if use_ctest: cmd = "(ctest)"
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)
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()
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()
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()
367 return self.get_result()
370 # Performs a build with a script.
371 def do_python_script_build(self, script, nb_proc):
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)
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)
383 __, exceptionValue, exceptionTraceback = sys.exc_info()
384 self.logger.write(str(exceptionValue), 1)
386 traceback.print_tb(exceptionTraceback)
387 traceback.print_exc()
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)
406 def do_batch_script_build(self, script, 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)
416 res = subprocess.call(script,
418 stdout=self.logger.logTxtFile,
419 stderr=subprocess.STDOUT,
420 cwd=str(self.build_dir),
421 env=self.build_environ.environ.environ)
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)
432 nb_proc = self.config.VARS.nb_proc
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)
440 msg = _("The script %s must have .sh, .bat or .py extension." % script)
441 raise src.SatException(msg)