Salome HOME
120 s betwenn two refreshs in the boards
[tools/sat.git] / src / compilation.py
index f08d171e98ee4badefa6c950343021515ccc8205..0cf9b16713353c02a0d7ef3263248767f1f6cb57 100644 (file)
@@ -33,7 +33,12 @@ C_COMPILE_ENV_LIST = ["CC",
 class Builder:
     """Class to handle all construction steps, like cmake, configure, make, ...
     """
-    def __init__(self, config, logger, product_info, options = src.options.OptResult(), debug_mode=False, check_src=True):
+    def __init__(self,
+                 config,
+                 logger,
+                 product_info,
+                 options = src.options.OptResult(),
+                 check_src=True):
         self.config = config
         self.logger = logger
         self.options = options
@@ -42,23 +47,12 @@ class Builder:
         self.source_dir = src.Path(self.product_info.source_dir)
         self.install_dir = src.Path(self.product_info.install_dir)
         self.header = ""
-        self.debug_mode = debug_mode
-
-        if not self.source_dir.exists() and check_src:
-            raise src.SatException(_("No sources found for product %(product)s in %(source_dir)s" % \
-                { "product": self.product_info.name, "source_dir": self.source_dir } ))
-
-        """
-        # check that required modules exist
-        for dep in self.product_info.depend:
-            assert dep in self.config.TOOLS.src.product_info, "UNDEFINED product: %s" % dep
-            dep_info = self.config.TOOLS.src.product_info[dep]
-            if 'install_dir' in dep_info and not os.path.exists(dep_info.install_dir):
-                raise src.SatException(_("Module %s is required") % dep)
-        """
+        self.debug_mode = False
+        if "debug" in self.product_info and self.product_info.debug == "yes":
+            self.debug_mode = True
 
     ##
-    # Shortcut method to log in both log files.
+    # Shortcut method to log in log file.
     def log(self, text, level, showInfo=True):
         self.logger.write(text, level, showInfo)
         self.logger.logTxtFile.write(src.printcolors.cleancolor(text))
@@ -83,13 +77,15 @@ class Builder:
         self.log('\n', 4)
 
         # add products in depend and opt_depend list recursively
-        environ_info = src.product.get_product_dependencies(self.config, self.product_info)
+        environ_info = src.product.get_product_dependencies(self.config,
+                                                            self.product_info)
+        #environ_info.append(self.product_info.name)
 
         # create build environment
         self.build_environ = src.environment.SalomeEnviron(self.config, src.environment.Environ(dict(os.environ)), True)
         self.build_environ.silent = (self.config.USER.output_verbose_level < 5)
         self.build_environ.set_full_environ(self.logger, environ_info)
-
+        
         # create runtime environment
         self.launch_environ = src.environment.SalomeEnviron(self.config, src.environment.Environ(dict(os.environ)), False)
         self.launch_environ.silent = True # no need to show here
@@ -209,27 +205,10 @@ CC=\\"hack_libtool\\"%g" libtool'''
                         stdout=self.logger.logTxtFile,
                         stderr=subprocess.STDOUT)
 
-    def get_nb_proc(self, opt_nb_proc=None):
-        nbproc = -1
-        if "nb_proc" in self.product_info:
-            # nb proc is specified in module definition
-            nbproc = self.product_info.nb_proc
-            if opt_nb_proc and opt_nb_proc < self.product_info.nb_proc:
-                # use command line value only if it is lower than module definition
-                nbproc = opt_nb_proc
-        else:
-            # nb proc is not specified in module definition
-            if opt_nb_proc:
-                nbproc = opt_nb_proc
-            else:
-                nbproc = self.config.VARS.nb_proc
-        
-        assert nbproc > 0
-        return nbproc
 
     ##
     # Runs make to build the module.
-    def make(self, nb_proc, make_opt):
+    def make(self, nb_proc, make_opt=""):
 
         # make
         command = 'make'
@@ -314,9 +293,9 @@ CC=\\"hack_libtool\\"%g" libtool'''
         if src.architecture.is_windows():
             command = 'msbuild RUN_TESTS.vcxproj'
         else :
-            if self.use_autotools :
+            if self.product_info.build_source=="autotools" :
                 command = 'make check'
-            else :
+            else:
                 command = 'make test'
             
         self.log_command(command)
@@ -392,20 +371,73 @@ CC=\\"hack_libtool\\"%g" libtool'''
 
     ##
     # Performs a build with a script.
-    def do_script_build(self, script):
+    def do_python_script_build(self, script, nb_proc):
         # script found
-        self.logger.write(_("Compile %(module)s using script %(script)s\n") % \
-            { 'module': self.module, 'script': src.printcolors.printcLabel(script) }, 4)
+        self.logger.write(_("Compile %(product)s using script %(script)s\n") % \
+            { 'product': self.product_info.name,
+             'script': src.printcolors.printcLabel(script) }, 4)
         try:
             import imp
-            pymodule = imp.load_source(self.module + "_compile_script", script)
+            product = self.product_info.name
+            pymodule = imp.load_source(product + "_compile_script", script)
+            self.nb_proc = nb_proc
             retcode = pymodule.compil(self.config, self, self.logger)
         except:
             __, exceptionValue, exceptionTraceback = sys.exc_info()
-            print(exceptionValue)
+            self.logger.write(str(exceptionValue), 1)
             import traceback
             traceback.print_tb(exceptionTraceback)
             traceback.print_exc()
+            retcode = 1
 
         return retcode
 
+    def complete_environment(self, make_options):
+        assert self.build_environ is not None
+        # pass additional variables to environment (may be used by the build script)
+        self.build_environ.set("SOURCE_DIR", str(self.source_dir))
+        self.build_environ.set("INSTALL_DIR", str(self.install_dir))
+        self.build_environ.set("PRODUCT_INSTALL", str(self.install_dir))
+        self.build_environ.set("BUILD_DIR", str(self.build_dir))
+        self.build_environ.set("PRODUCT_BUILD", str(self.build_dir))
+        self.build_environ.set("MAKE_OPTIONS", make_options)
+        self.build_environ.set("DIST_NAME", self.config.VARS.dist_name)
+        self.build_environ.set("DIST_VERSION", self.config.VARS.dist_version)
+        self.build_environ.set("DIST", self.config.VARS.dist)
+        self.build_environ.set("VERSION", self.product_info.version)
+
+    def do_batch_script_build(self, script, nb_proc):
+
+        if src.architecture.is_windows():
+            make_options = "/maxcpucount:%s" % nb_proc
+        else :
+            make_options = "-j%s" % nb_proc
+
+        self.log_command("  " + _("Run build script %s\n") % script)
+        self.complete_environment(make_options)
+        res = subprocess.call(script, 
+                              shell=True,
+                              stdout=self.logger.logTxtFile,
+                              stderr=subprocess.STDOUT,
+                              cwd=str(self.build_dir), 
+                              env=self.build_environ.environ.environ)
+
+        if res == 0:
+            return res
+        else:
+            return 1
+    
+    def do_script_build(self, script):
+        # define make options (may not be used by the script)
+        nb_proc = src.get_cfg_param(self.product_info,"nb_proc", 0)
+        if nb_proc == 0: 
+            nb_proc = self.config.VARS.nb_proc
+            
+        extension = script.split('.')[-1]
+        if extension in ["bat","sh"]:
+            return self.do_batch_script_build(script, nb_proc)
+        if extension == "py":
+            return self.do_python_script_build(script, nb_proc)
+        
+        msg = _("The script %s must have .sh, .bat or .py extension." % script)
+        raise src.SatException(msg)
\ No newline at end of file