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
48 # Shortcut method to log in log file.
49 def log(self, text, level, showInfo=True):
50 self.logger.write(text, level, showInfo)
51 self.logger.logTxtFile.write(src.printcolors.cleancolor(text))
55 # Shortcut method to log a command.
56 def log_command(self, command):
57 self.log("> %s\n" % command, 5)
60 # Prepares the environment.
61 # Build two environment: one for building and one for testing (launch).
64 if not self.build_dir.exists():
68 self.log(' build_dir = %s\n' % str(self.build_dir), 4)
69 self.log(' install_dir = %s\n' % str(self.install_dir), 4)
72 # add products in depend and opt_depend list recursively
73 environ_info = src.product.get_product_dependencies(self.config, self.product_info)
74 #environ_info.append(self.product_info.name)
76 # create build environment
77 self.build_environ = src.environment.SalomeEnviron(self.config, src.environment.Environ(dict(os.environ)), True)
78 self.build_environ.silent = (self.config.USER.output_verbose_level < 5)
79 self.build_environ.set_full_environ(self.logger, environ_info)
81 # create runtime environment
82 self.launch_environ = src.environment.SalomeEnviron(self.config, src.environment.Environ(dict(os.environ)), False)
83 self.launch_environ.silent = True # no need to show here
84 self.launch_environ.set_full_environ(self.logger, environ_info)
86 for ee in C_COMPILE_ENV_LIST:
87 vv = self.build_environ.get(ee)
89 self.log(" %s = %s\n" % (ee, vv), 4, False)
94 # Runs cmake with the given options.
95 def cmake(self, options=""):
97 cmake_option = options
98 # cmake_option +=' -DCMAKE_VERBOSE_MAKEFILE=ON -DSALOME_CMAKE_DEBUG=ON'
99 if 'cmake_options' in self.product_info:
100 cmake_option += " %s " % " ".join(self.product_info.cmake_options.split())
104 cmake_option += " -DCMAKE_BUILD_TYPE=Debug"
106 cmake_option += " -DCMAKE_BUILD_TYPE=Release"
108 command = ("cmake %s -DCMAKE_INSTALL_PREFIX=%s %s" %
109 (cmake_option, self.install_dir, self.source_dir))
111 self.log_command(command)
112 res = subprocess.call(command,
114 cwd=str(self.build_dir),
115 env=self.build_environ.environ.environ,
116 stdout=self.logger.logTxtFile,
117 stderr=subprocess.STDOUT)
125 # Runs build_configure with the given options.
126 def build_configure(self, options=""):
128 if 'buildconfigure_options' in self.product_info:
129 options += " %s " % self.product_info.buildconfigure_options
131 command = str('%s/build_configure') % (self.source_dir)
132 command = command + " " + options
133 self.log_command(command)
135 res = subprocess.call(command,
137 cwd=str(self.build_dir),
138 env=self.build_environ.environ.environ,
139 stdout=self.logger.logTxtFile,
140 stderr=subprocess.STDOUT)
148 # Runs configure with the given options.
149 def configure(self, options=""):
151 if 'configure_options' in self.product_info:
152 options += " %s " % self.product_info.configure_options
154 command = "%s/configure --prefix=%s" % (self.source_dir, str(self.install_dir))
156 command = command + " " + options
157 self.log_command(command)
159 res = subprocess.call(command,
161 cwd=str(self.build_dir),
162 env=self.build_environ.environ.environ,
163 stdout=self.logger.logTxtFile,
164 stderr=subprocess.STDOUT)
171 def hack_libtool(self):
172 if not os.path.exists(str(self.build_dir + 'libtool')):
175 lf = open(os.path.join(str(self.build_dir), "libtool"), 'r')
176 for line in lf.readlines():
177 if 'hack_libtool' in line:
180 # fix libtool by replacing CC="<compil>" with hack_libtool function
181 hack_command='''sed -i "s%^CC=\\"\(.*\)\\"%hack_libtool() { \\n\\
182 if test \\"\$(echo \$@ | grep -E '\\\\\\-L/usr/lib(/../lib)?(64)? ')\\" == \\\"\\\" \\n\\
184 cmd=\\"\\1 \$@\\"\\n\\
186 cmd=\\"\\1 \\"\`echo \$@ | sed -r -e 's|(.*)-L/usr/lib(/../lib)?(64)? (.*)|\\\\\\1\\\\\\4 -L/usr/lib\\\\\\3|g'\`\\n\\
190 CC=\\"hack_libtool\\"%g" libtool'''
192 self.log_command(hack_command)
193 subprocess.call(hack_command,
195 cwd=str(self.build_dir),
196 env=self.build_environ.environ.environ,
197 stdout=self.logger.logTxtFile,
198 stderr=subprocess.STDOUT)
202 # Runs make to build the module.
203 def make(self, nb_proc, make_opt):
207 command = command + " -j" + str(nb_proc)
208 command = command + " " + make_opt
209 self.log_command(command)
210 res = subprocess.call(command,
212 cwd=str(self.build_dir),
213 env=self.build_environ.environ.environ,
214 stdout=self.logger.logTxtFile,
215 stderr=subprocess.STDOUT)
223 # Runs msbuild to build the module.
224 def wmake(self, opt_nb_proc = None):
225 nbproc = self.get_nb_proc(opt_nb_proc)
227 hh = 'MSBUILD /m:%s' % str(nbproc)
229 hh += " " + src.printcolors.printcWarning("DEBUG")
234 if self.options.makeflags:
235 command = command + " " + self.options.makeflags
236 command = command + " /maxcpucount:" + str(nbproc)
238 command = command + " /p:Configuration=Debug"
240 command = command + " /p:Configuration=Release"
241 command = command + " ALL_BUILD.vcxproj"
243 self.log_command(command)
244 res = subprocess.call(command,
246 cwd=str(self.build_dir),
247 env=self.build_environ.environ.environ,
248 stdout=self.logger.logTxtFile,
249 stderr=subprocess.STDOUT)
257 # Runs 'make install'.
259 if self.config.VARS.dist_name=="Win":
260 command = 'msbuild INSTALL.vcxproj'
262 command = command + " /p:Configuration=Debug"
264 command = command + " /p:Configuration=Release"
266 command = 'make install'
268 self.log_command(command)
270 res = subprocess.call(command,
272 cwd=str(self.build_dir),
273 env=self.build_environ.environ.environ,
274 stdout=self.logger.logTxtFile,
275 stderr=subprocess.STDOUT)
285 if src.architecture.is_windows():
286 command = 'msbuild RUN_TESTS.vcxproj'
288 if self.use_autotools :
289 command = 'make check'
291 command = 'make test'
293 self.log_command(command)
295 res = subprocess.call(command,
297 cwd=str(self.build_dir),
298 env=self.launch_environ.environ.environ,
299 stdout=self.logger.logTxtFile,
300 stderr=subprocess.STDOUT)
308 # Performs a default build for this module.
309 def do_default_build(self, build_conf_options="", configure_options="", show_warning=True):
310 use_autotools = False
311 if 'use_autotools' in self.product_info:
312 uc = self.product_info.use_autotools
313 if uc in ['always', 'yes']:
316 use_autotools = self.options.autotools
319 self.use_autotools = use_autotools
322 if 'use_ctest' in self.product_info:
323 uc = self.product_info.use_ctest
324 if uc in ['always', 'yes']:
327 use_ctest = self.options.ctest
329 self.use_ctest = use_ctest
333 if use_autotools: cmd = "(autotools)"
334 if use_ctest: cmd = "(ctest)"
336 self.log("\n", 4, False)
337 self.log("%(module)s: Run default compilation method %(cmd)s\n" % \
338 { "module": self.module, "cmd": cmd }, 4)
341 if not self.prepare(): return self.get_result()
342 if not self.build_configure(build_conf_options): return self.get_result()
343 if not self.configure(configure_options): return self.get_result()
344 if not self.make(): return self.get_result()
345 if not self.install(): return self.get_result()
346 if not self.clean(): return self.get_result()
349 if self.config.VARS.dist_name=='Win':
350 if not self.wprepare(): return self.get_result()
351 if not self.cmake(): return self.get_result()
352 if not self.wmake(): return self.get_result()
353 if not self.install(): return self.get_result()
354 if not self.clean(): return self.get_result()
356 if not self.prepare(): return self.get_result()
357 if not self.cmake(): return self.get_result()
358 if not self.make(): return self.get_result()
359 if not self.install(): return self.get_result()
360 if not self.clean(): return self.get_result()
362 return self.get_result()
365 # Performs a build with a script.
366 def do_python_script_build(self, script):
368 self.logger.write(_("Compile %(module)s using script %(script)s\n") % \
369 { 'module': self.module, 'script': src.printcolors.printcLabel(script) }, 4)
372 pymodule = imp.load_source(self.product + "_compile_script", script)
373 retcode = pymodule.compil(self.config, self, self.logger)
375 __, exceptionValue, exceptionTraceback = sys.exc_info()
376 print(exceptionValue)
378 traceback.print_tb(exceptionTraceback)
379 traceback.print_exc()
383 def complete_environment(self, make_options):
384 assert self.build_environ is not None
385 # pass additional variables to environment (may be used by the build script)
386 self.build_environ.set("SOURCE_DIR", str(self.source_dir))
387 self.build_environ.set("INSTALL_DIR", str(self.install_dir))
388 self.build_environ.set("PRODUCT_INSTALL", str(self.install_dir))
389 self.build_environ.set("BUILD_DIR", str(self.build_dir))
390 self.build_environ.set("PRODUCT_BUILD", str(self.build_dir))
391 self.build_environ.set("MAKE_OPTIONS", make_options)
392 self.build_environ.set("DIST_NAME", self.config.VARS.dist_name)
393 self.build_environ.set("DIST_VERSION", self.config.VARS.dist_version)
394 self.build_environ.set("DIST", self.config.VARS.dist)
396 def do_batch_script_build(self, script):
397 # define make options (may not be used by the script)
398 nb_proc = src.get_cfg_param(self.product_info,"nb_proc", 0)
400 nb_proc = self.config.VARS.nb_proc
402 if src.architecture.is_windows():
403 make_options = "/maxcpucount:%s" % nb_proc
405 make_options = "-j%s" % nb_proc
407 self.log_command(" " + _("Run build script %s\n") % script)
408 self.complete_environment(make_options)
409 res = subprocess.call(script,
411 stdout=self.logger.logTxtFile,
412 stderr=subprocess.STDOUT,
413 cwd=str(self.build_dir),
414 env=self.build_environ.environ.environ)
421 def do_script_build(self, script):
422 extension = script.split('.')[-1]
423 if extension in ["bat","sh"]:
424 return self.do_batch_script_build(script)
425 if extension == "py":
426 return self.do_python_script_build(script)
428 msg = _("The script %s must have .sh, .bat or .py extension." % script)
429 raise src.SatException(msg)