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 both log files.
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)
88 # create build environment
89 self.build_environ = src.environment.SalomeEnviron(self.config, src.environment.Environ(dict(os.environ)), True)
90 self.build_environ.silent = (self.config.USER.output_verbose_level < 5)
91 self.build_environ.set_full_environ(self.logger, environ_info)
93 # create runtime environment
94 self.launch_environ = src.environment.SalomeEnviron(self.config, src.environment.Environ(dict(os.environ)), False)
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)
137 # Runs build_configure with the given options.
138 def build_configure(self, options=""):
140 if 'buildconfigure_options' in self.product_info:
141 options += " %s " % self.product_info.buildconfigure_options
143 command = str('%s/build_configure') % (self.source_dir)
144 command = command + " " + options
145 self.log_command(command)
147 res = subprocess.call(command,
149 cwd=str(self.build_dir),
150 env=self.build_environ.environ.environ,
151 stdout=self.logger.logTxtFile,
152 stderr=subprocess.STDOUT)
160 # Runs configure with the given options.
161 def configure(self, options=""):
163 if 'configure_options' in self.product_info:
164 options += " %s " % self.product_info.configure_options
166 command = "%s/configure --prefix=%s" % (self.source_dir, str(self.install_dir))
168 command = command + " " + options
169 self.log_command(command)
171 res = subprocess.call(command,
173 cwd=str(self.build_dir),
174 env=self.build_environ.environ.environ,
175 stdout=self.logger.logTxtFile,
176 stderr=subprocess.STDOUT)
183 def hack_libtool(self):
184 if not os.path.exists(str(self.build_dir + 'libtool')):
187 lf = open(os.path.join(str(self.build_dir), "libtool"), 'r')
188 for line in lf.readlines():
189 if 'hack_libtool' in line:
192 # fix libtool by replacing CC="<compil>" with hack_libtool function
193 hack_command='''sed -i "s%^CC=\\"\(.*\)\\"%hack_libtool() { \\n\\
194 if test \\"\$(echo \$@ | grep -E '\\\\\\-L/usr/lib(/../lib)?(64)? ')\\" == \\\"\\\" \\n\\
196 cmd=\\"\\1 \$@\\"\\n\\
198 cmd=\\"\\1 \\"\`echo \$@ | sed -r -e 's|(.*)-L/usr/lib(/../lib)?(64)? (.*)|\\\\\\1\\\\\\4 -L/usr/lib\\\\\\3|g'\`\\n\\
202 CC=\\"hack_libtool\\"%g" libtool'''
204 self.log_command(hack_command)
205 subprocess.call(hack_command,
207 cwd=str(self.build_dir),
208 env=self.build_environ.environ.environ,
209 stdout=self.logger.logTxtFile,
210 stderr=subprocess.STDOUT)
212 def get_nb_proc(self, opt_nb_proc=None):
214 if "nb_proc" in self.product_info:
215 # nb proc is specified in module definition
216 nbproc = self.product_info.nb_proc
217 if opt_nb_proc and opt_nb_proc < self.product_info.nb_proc:
218 # use command line value only if it is lower than module definition
221 # nb proc is not specified in module definition
225 nbproc = self.config.VARS.nb_proc
231 # Runs make to build the module.
232 def make(self, opt_nb_proc = None):
233 nbproc = self.get_nb_proc(opt_nb_proc)
235 hh = 'MAKE -j%s' % str(nbproc)
237 hh += " " + src.printcolors.printcWarning("DEBUG")
242 if self.options.makeflags:
243 command = command + " " + self.options.makeflags
244 command = command + " -j" + str(nbproc)
246 self.log_command(command)
247 res = subprocess.call(command,
249 cwd=str(self.build_dir),
250 env=self.build_environ.environ.environ,
251 stdout=self.logger.logTxtFile,
252 stderr=subprocess.STDOUT)
260 # Runs msbuild to build the module.
261 def wmake(self, opt_nb_proc = None):
262 nbproc = self.get_nb_proc(opt_nb_proc)
264 hh = 'MSBUILD /m:%s' % str(nbproc)
266 hh += " " + src.printcolors.printcWarning("DEBUG")
271 if self.options.makeflags:
272 command = command + " " + self.options.makeflags
273 command = command + " /maxcpucount:" + str(nbproc)
275 command = command + " /p:Configuration=Debug"
277 command = command + " /p:Configuration=Release"
278 command = command + " ALL_BUILD.vcxproj"
280 self.log_command(command)
281 res = subprocess.call(command,
283 cwd=str(self.build_dir),
284 env=self.build_environ.environ.environ,
285 stdout=self.logger.logTxtFile,
286 stderr=subprocess.STDOUT)
294 # Runs 'make install'.
296 if self.config.VARS.dist_name=="Win":
297 command = 'msbuild INSTALL.vcxproj'
299 command = command + " /p:Configuration=Debug"
301 command = command + " /p:Configuration=Release"
303 command = 'make install'
305 self.log_command(command)
307 res = subprocess.call(command,
309 cwd=str(self.build_dir),
310 env=self.build_environ.environ.environ,
311 stdout=self.logger.logTxtFile,
312 stderr=subprocess.STDOUT)
322 if src.architecture.is_windows():
323 command = 'msbuild RUN_TESTS.vcxproj'
325 if self.use_autotools :
326 command = 'make check'
328 command = 'make test'
330 self.log_command(command)
332 res = subprocess.call(command,
334 cwd=str(self.build_dir),
335 env=self.launch_environ.environ.environ,
336 stdout=self.logger.logTxtFile,
337 stderr=subprocess.STDOUT)
345 # Performs a default build for this module.
346 def do_default_build(self, build_conf_options="", configure_options="", show_warning=True):
347 use_autotools = False
348 if 'use_autotools' in self.product_info:
349 uc = self.product_info.use_autotools
350 if uc in ['always', 'yes']:
353 use_autotools = self.options.autotools
356 self.use_autotools = use_autotools
359 if 'use_ctest' in self.product_info:
360 uc = self.product_info.use_ctest
361 if uc in ['always', 'yes']:
364 use_ctest = self.options.ctest
366 self.use_ctest = use_ctest
370 if use_autotools: cmd = "(autotools)"
371 if use_ctest: cmd = "(ctest)"
373 self.log("\n", 4, False)
374 self.log("%(module)s: Run default compilation method %(cmd)s\n" % \
375 { "module": self.module, "cmd": cmd }, 4)
378 if not self.prepare(): return self.get_result()
379 if not self.build_configure(build_conf_options): return self.get_result()
380 if not self.configure(configure_options): return self.get_result()
381 if not self.make(): return self.get_result()
382 if not self.install(): return self.get_result()
383 if not self.clean(): return self.get_result()
386 if self.config.VARS.dist_name=='Win':
387 if not self.wprepare(): return self.get_result()
388 if not self.cmake(): return self.get_result()
389 if not self.wmake(): return self.get_result()
390 if not self.install(): return self.get_result()
391 if not self.clean(): return self.get_result()
393 if not self.prepare(): return self.get_result()
394 if not self.cmake(): return self.get_result()
395 if not self.make(): return self.get_result()
396 if not self.install(): return self.get_result()
397 if not self.clean(): return self.get_result()
399 return self.get_result()
402 # Performs a build with a script.
403 def do_script_build(self, script):
405 self.logger.write(_("Compile %(module)s using script %(script)s\n") % \
406 { 'module': self.module, 'script': src.printcolors.printcLabel(script) }, 4)
409 pymodule = imp.load_source(self.module + "_compile_script", script)
410 retcode = pymodule.compil(self.config, self, self.logger)
412 __, exceptionValue, exceptionTraceback = sys.exc_info()
413 print(exceptionValue)
415 traceback.print_tb(exceptionTraceback)
416 traceback.print_exc()