Salome HOME
spns #42205 [SAT][Windows] support different values for CMAKE_BUILD_TYPE - define...
[tools/sat.git] / src / compilation.py
index d46a2171a149e3a8bc24234453594aaf0a5b91ea..6d06b352875d8985f6df5c14f103c8903074406e 100644 (file)
@@ -20,6 +20,7 @@ import os
 import subprocess
 import sys
 import shutil
+import glob
 
 import src
 
@@ -37,22 +38,57 @@ class Builder:
     def __init__(self,
                  config,
                  logger,
+                 product_name,
                  product_info,
                  options = src.options.OptResult(),
                  check_src=True):
         self.config = config
         self.logger = logger
         self.options = options
+        self.product_name = product_name
         self.product_info = product_info
         self.build_dir = src.Path(self.product_info.build_dir)
         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 = False
+        self.cmake_build_type = 'Release'
+        if "cmake_build_type" in self.config.APPLICATION:
+            self.set_cmake_build_type(self.config.APPLICATION.cmake_build_type)
+        # keep backward compatibility
+        if "debug" in self.config.APPLICATION and self.config.APPLICATION.debug == "yes":
+            self.debug_mode = True
+            self.cmake_build_type = 'Debug'
+
+        # in case a product defines its own configuration, then use it
+        if "cmake_build_type" in self.product_info:
+            self.set_cmake_build_type(self.product_info.cmake_build_type)
+        # keep backward compatibility
         if "debug" in self.product_info and self.product_info.debug == "yes":
             self.debug_mode = True
+            self.cmake_build_type = 'Debug'
+
+        self.verbose_mode = False
+        if "verbose" in self.product_info and self.product_info.verbose == "yes":
+            self.verbose_mode = True
+
+
+    # set cmake build type
+    def set_cmake_build_type(self, build_type):
+        if build_type.lower() in ['debug', 'relwithdebinfo', 'release', 'minsizerel']:
+            if build_type.lower() ==  'debug':
+                self.cmake_build_type = 'Debug'
+                self.debug_mode = True
+            elif build_type.lower() ==  'relwithdebinfo':
+                self.cmake_build_type = 'RelWithDebInfo'
+            elif build_type.lower() ==  'release':
+                self.cmake_build_type = 'Release'
+            elif build_type.lower() ==  'minsizerel':
+                self.cmake_build_type = 'MinSizeRel'
+            else:
+                raise src.SatException("Unknown cmake build mode: {}. Supported values are: Debug, RelWithDebInfo, Release or MinSizeRel".format(build_type))
+        return
 
-    ##
     # Shortcut method to log in log file.
     def log(self, text, level, showInfo=True):
         self.logger.write(text, level, showInfo)
@@ -67,7 +103,7 @@ class Builder:
     ##
     # Prepares the environment.
     # Build two environment: one for building and one for testing (launch).
-    def prepare(self):
+    def prepare(self, add_env_launch=False):
 
         if not self.build_dir.exists():
             # create build dir
@@ -79,6 +115,7 @@ class Builder:
 
         # add products in depend and opt_depend list recursively
         environ_info = src.product.get_product_dependencies(self.config,
+                                                            self.product_name,
                                                             self.product_info)
         #environ_info.append(self.product_info.name)
 
@@ -89,12 +126,13 @@ class Builder:
         self.build_environ.silent = (self.config.USER.output_verbose_level < 5)
         self.build_environ.set_full_environ(self.logger, environ_info)
         
+        if add_env_launch:
         # create runtime environment
-        self.launch_environ = src.environment.SalomeEnviron(self.config,
+            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
-        self.launch_environ.set_full_environ(self.logger, environ_info)
+            self.launch_environ.silent = True # no need to show here
+            self.launch_environ.set_full_environ(self.logger, environ_info)
 
         for ee in C_COMPILE_ENV_LIST:
             vv = self.build_environ.get(ee)
@@ -108,23 +146,28 @@ class Builder:
     def cmake(self, options=""):
 
         cmake_option = options
-        # cmake_option +=' -DCMAKE_VERBOSE_MAKEFILE=ON -DSALOME_CMAKE_DEBUG=ON'
+        if "cmake_build_type" in self.config.APPLICATION:
+            self.set_cmake_build_type(self.config.APPLICATION.cmake_build_type)
+        # keep backward compatibility
+        if "debug" in self.config.APPLICATION and self.config.APPLICATION.debug == "yes":
+            self.debug_mode = True
+            self.cmake_build_type = 'Debug'
+
         if 'cmake_options' in self.product_info:
             cmake_option += " %s " % " ".join(
                                         self.product_info.cmake_options.split())
 
-        # add debug option
-        if self.debug_mode:
-            cmake_option += " -DCMAKE_BUILD_TYPE=Debug"
-        else :
-            cmake_option += " -DCMAKE_BUILD_TYPE=Release"
-        
+        # add cmake build_type options
+        cmake_option += " -DCMAKE_BUILD_TYPE=" + self.cmake_build_type
+
+        # add verbose option if specified in application for this product.
+        if self.verbose_mode:
+            cmake_option += " -DCMAKE_VERBOSE_MAKEFILE=ON"
+
         # In case CMAKE_GENERATOR is defined in environment, 
         # use it in spite of automatically detect it
         if 'cmake_generator' in self.config.APPLICATION:
-            cmake_option += " -DCMAKE_GENERATOR=%s" \
-                                       % self.config.APPLICATION.cmake_generator
-        
+            cmake_option += " -G \"%s\" -A x64 " % self.config.APPLICATION.cmake_generator
         command = ("cmake %s -DCMAKE_INSTALL_PREFIX=%s %s" %
                             (cmake_option, self.install_dir, self.source_dir))
 
@@ -253,11 +296,8 @@ CC=\\"hack_libtool\\"%g" libtool'''
             hh += " " + src.printcolors.printcWarning("DEBUG")
         # make
         command = 'msbuild'
-        command = command + " /maxcpucount:" + str(nb_proc)
-        if self.debug_mode:
-            command = command + " /p:Configuration=Debug"
-        else:
-            command = command + " /p:Configuration=Release"
+        command+= " /maxcpucount:" + str(nb_proc)
+        command+= " /p:Configuration={}  /p:Platform=x64 ".format(self.cmake_build_type)
         command = command + " ALL_BUILD.vcxproj"
 
         self.log_command(command)
@@ -277,15 +317,11 @@ CC=\\"hack_libtool\\"%g" libtool'''
     ##
     # Runs 'make install'.
     def install(self):
-        if self.config.VARS.dist_name=="Win":
+        if src.architecture.is_windows():
             command = 'msbuild INSTALL.vcxproj'
-            if self.debug_mode:
-                command = command + " /p:Configuration=Debug"
-            else:
-                command = command + " /p:Configuration=Release"
+            command+= ' /p:Configuration={}  /p:Platform=x64 '.format(self.cmake_build_type)
         else :
             command = 'make install'
-
         self.log_command(command)
 
         res = subprocess.call(command,
@@ -295,17 +331,42 @@ CC=\\"hack_libtool\\"%g" libtool'''
                               stdout=self.logger.logTxtFile,
                               stderr=subprocess.STDOUT)
         
+        res_check=self.check_install()
+        if res_check > 0 :
+            self.log_command("Error in sat check install - some files are not installed!")
         self.put_txt_log_in_appli_log_dir("makeinstall")
+
+        res+=res_check
         if res == 0:
             return res
         else:
             return 1
 
+    # this function checks wether a list of file patterns (specified by check_install keyword) 
+    # exixts after the make install. The objective is to ensure the installation is complete.
+    # patterns are given relatively to the install dir of the product
+    def check_install(self):
+        res=0
+        if "check_install" in self.product_info:
+            self.log_command("Check installation of files")
+            for pattern in self.product_info.check_install:
+                # pattern is given relatively to the install dir
+                complete_pattern=os.path.join(self.product_info.install_dir, pattern) 
+                self.log_command("    -> check %s" % complete_pattern)
+                # expansion of pattern : takes into account environment variables and unix shell rules
+                list_of_path=glob.glob(os.path.expandvars(complete_pattern))
+                if not list_of_path:
+                    # we expect to find at least one entry, if not we consider the test failed
+                    res+=1
+                    self.logger.write("Error, sat check install failed for file pattern %s\n" % complete_pattern, 1)
+                    self.log_command("Error, sat check install failed for file pattern %s" % complete_pattern)
+        return res
+
     ##
     # Runs 'make_check'.
     def check(self, command=""):
         if src.architecture.is_windows():
-            cmd = 'msbuild RUN_TESTS.vcxproj'
+            cmd = 'msbuild RUN_TESTS.vcxproj /p:Configuration={}  /p:Platform=x64 '.format(self.cmake_build_type)
         else :
             if self.product_info.build_source=="autotools" :
                 cmd = 'make check'
@@ -316,6 +377,7 @@ CC=\\"hack_libtool\\"%g" libtool'''
             cmd = command
         
         self.log_command(cmd)
+        self.log_command("For more detailed logs, see test logs in %s" % self.build_dir)
 
         res = subprocess.call(cmd,
                               shell=True,
@@ -324,6 +386,7 @@ CC=\\"hack_libtool\\"%g" libtool'''
                               stdout=self.logger.logTxtFile,
                               stderr=subprocess.STDOUT)
 
+        self.put_txt_log_in_appli_log_dir("makecheck")
         if res == 0:
             return res
         else:
@@ -403,7 +466,7 @@ CC=\\"hack_libtool\\"%g" libtool'''
             pymodule = imp.load_source(product + "_compile_script", script)
             self.nb_proc = nb_proc
             retcode = pymodule.compil(self.config, self, self.logger)
-        except:
+        except Exception:
             __, exceptionValue, exceptionTraceback = sys.exc_info()
             self.logger.write(str(exceptionValue), 1)
             import traceback
@@ -419,6 +482,7 @@ CC=\\"hack_libtool\\"%g" libtool'''
         assert self.build_environ is not None
         # pass additional variables to environment 
         # (may be used by the build script)
+        self.build_environ.set("APPLICATION_NAME", self.config.APPLICATION.name)
         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))
@@ -429,6 +493,18 @@ CC=\\"hack_libtool\\"%g" libtool'''
         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)
+        # if product is in hpc mode, set SAT_HPC to 1 
+        # in order for the compilation script to take it into account
+        if src.product.product_is_hpc(self.product_info):
+            self.build_environ.set("SAT_HPC", "1")
+        if self.debug_mode:
+            self.build_environ.set("SAT_DEBUG", "1")
+        if "cmake_build_type" in self.config.APPLICATION:
+            self.set_cmake_build_type(self.config.APPLICATION.cmake_build_type)
+            self.build_environ.set("SAT_CMAKE_BUILD_TYPE", self.cmake_build_type)
+        if self.verbose_mode:
+            self.build_environ.set("SAT_VERBOSE", "1")
+
 
     def do_batch_script_build(self, script, nb_proc):
 
@@ -447,7 +523,12 @@ CC=\\"hack_libtool\\"%g" libtool'''
                               cwd=str(self.build_dir),
                               env=self.build_environ.environ.environ)
 
+        res_check=self.check_install()
+        if res_check > 0 :
+            self.log_command("Error in sat check install - some files are not installed!")
+
         self.put_txt_log_in_appli_log_dir("script")
+        res += res_check
         if res == 0:
             return res
         else: