Salome HOME
Upgrade to new MG license mechanism and keep compatibility with old ones cbr/new_mg_license_215_squashed
authorChristophe Bourcier <christophe.bourcier@cea.fr>
Tue, 7 Mar 2023 14:00:30 +0000 (15:00 +0100)
committerChristophe Bourcier <christophe.bourcier@cea.fr>
Wed, 12 Apr 2023 08:33:31 +0000 (10:33 +0200)
spns #33658 - fix compilation issue on Windows (thanks to Nabil)

src/SMESH/MG_ADAPT.cxx
src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx
src/SMESHUtils/SMESH_MGLicenseKeyGen.hxx
src/Tools/MGCleanerPlug/MGCleanerMonPlugDialog.py
src/Tools/YamsPlug/monYamsPlugDialog.py

index 50f467fc03304a342577f7476898867cb505d79b..82f9221b2f9fa341a9b2a13cdd46008b8e0f7c80 100644 (file)
@@ -1036,14 +1036,19 @@ std::string MgAdapt::getCommandToRun()
     if ( key.empty() )
       return ToComment( "Problem with library SalomeMeshGemsKeyGenerator: " + errorTxt );
 
-    cmd += " --key " + key;
+    if ( key!="0" )
+      cmd += " --key " + key;
   }
 
 #ifdef WIN32
   cmd += " < NUL";
 #endif
-  //   std::cout << "--- cmd :"<< std::endl;
-  //   std::cout << cmd << std::endl;
+
+  if (SALOME::VerbosityActivated())
+    {
+      std::cout << "--- cmd :"<< std::endl;
+      std::cout << cmd << std::endl;
+    }
 
   return cmd;
 }
index 01a926b243a8a21feea56b2f7b2270966116d967..4d2a83cbb511de990c08e348d1fc62fa0e30ffba 100644 (file)
@@ -61,6 +61,7 @@ namespace boofs = boost::filesystem;
 #define SMESH_CAUGHT error =
 
 constexpr char MESHGEMS_OLD_STYLE[] = "MESHGEMS_OLD_STYLE";
+constexpr char SPATIAL_LICENSE[] = "SPATIAL_LICENSE";
 
 
 namespace
@@ -482,8 +483,10 @@ namespace SMESHUtils_MGLicenseKeyGen // API implementation
   {
     const char *meshGemsOldStyleEnvVar( getenv( MESHGEMS_OLD_STYLE ) );
     if ( !meshGemsOldStyleEnvVar || strlen(meshGemsOldStyleEnvVar) == 0 )
+      // sign the mesh (MG 2.13 and 2.14)
       return SignMesh_After(meshgems_mesh, error);
     else
+      // use DLIM8 server (nothing to do here)
       return true;
   }
 
@@ -526,6 +529,164 @@ namespace SMESHUtils_MGLicenseKeyGen // API implementation
     return key;
   }
 
+  //================================================================================
+  /*!
+   * \brief Return a license key to pass as argument to a MG mesher executable (>2.15)
+   *  \param [out] error - return error description
+   *  \return std::string - the key
+   */
+  //================================================================================
+
+  std::string GetKey_After(std::string&       error)
+  {
+    std::string key;
+    LibraryFile libraryFile;
+    if ( !loadLibrary( error, libraryFile ))
+      return key;
+
+    typedef std::string (*GetKeyFun)();
+    GetKeyFun keyFun = (GetKeyFun) GetProc( theLibraryHandle, "GetKey" );
+    if ( !keyFun )
+    {
+      if ( ! getLastError( error ))
+        error = SMESH_Comment( "Can't find symbol 'GetKey' in '") << getenv( theEnvVar ) << "'";
+    }
+    else
+    {
+      key = keyFun( );
+    }
+    if ( key.empty() )
+      error = "GetKey() failed (located in '" + libraryFile._name + "')";
+
+    return key;
+  }
+
+
+  //================================================================================
+  /*!
+   * \brief Get MeshGems version from the keygen library and meshgems built-in functions
+   *  \param [out] error - return error description
+   *  \return int - the version
+   */
+  //================================================================================
+  int GetMGVersionHex(std::string&       error)
+  {
+    // get minor version
+    int v_min = -1;
+    LibraryFile libraryFile;
+    if ( !loadLibrary( error, libraryFile ))
+      return v_min;
+
+    typedef int (*GetKeyFun)();
+    GetKeyFun keyFun = (GetKeyFun) GetProc( theLibraryHandle, "meshgems_core_get_version_minor" );
+    if ( !keyFun )
+    {
+      if ( ! getLastError( error ))
+       error = SMESH_Comment( "Can't find symbol 'meshgems_core_get_version_minor' in '") << getenv( theEnvVar ) << "'";
+    }
+    else
+    {
+       v_min = keyFun( );
+    }
+    if ( v_min==-1 )
+      error = "meshgems_core_get_version_minor() failed (located in '" + libraryFile._name + "')";
+
+    MESSAGE("meshgems_core_get_version_minor: " << v_min);
+
+    // get major version
+    int v_maj = -1;
+
+    typedef int (*GetKeyFun)();
+    keyFun = (GetKeyFun) GetProc( theLibraryHandle, "meshgems_core_get_version_major" );
+    if ( !keyFun )
+    {
+      if ( ! getLastError( error ))
+       error = SMESH_Comment( "Can't find symbol 'meshgems_core_get_version_major' in '") << getenv( theEnvVar ) << "'";
+    }
+    else
+    {
+       v_maj = keyFun( );
+    }
+    if ( v_maj==-1 )
+      error = "meshgems_core_get_version_major() failed (located in '" + libraryFile._name + "')";
+
+    MESSAGE("meshgems_core_get_version_major: " << v_maj);
+
+    // get patch version
+    int v_patch = -1;
+
+    typedef int (*GetKeyFun)();
+    keyFun = (GetKeyFun) GetProc( theLibraryHandle, "meshgems_core_get_version_patch" );
+    if ( !keyFun )
+    {
+      if ( ! getLastError( error ))
+       error = SMESH_Comment( "Can't find symbol 'meshgems_core_get_version_patch' in '") << getenv( theEnvVar ) << "'";
+    }
+    else
+    {
+       v_patch = keyFun( );
+    }
+    if ( v_patch==-1 )
+      error = "meshgems_core_get_version_patch() failed (located in '" + libraryFile._name + "')";
+
+    MESSAGE("meshgems_core_get_version_patch: " << v_patch );
+
+    int v_hex = (v_maj << 16 | v_min << 8 | v_patch);
+
+    MESSAGE("v_hex: " << v_hex);
+
+    return v_hex;
+  }
+
+  //================================================================================
+  /*!
+   * \brief Guess if the Spatial license is needed (if MeshGems is > 2.15.0)
+   *  \param [out] error - return error description
+   *  \return bool - true if MeshGems is > 2.15.0
+   */
+  //================================================================================
+  bool NeedsMGSpatialEnvLicense(std::string& error)
+  {
+    // if MeshGems version is > 2.15.0, need to set SPATIAL_LICENSE
+    int v_hex = GetMGVersionHex(error);
+    bool ok = (v_hex > MESHGEMS_215);
+    if (ok)
+      MESSAGE("MeshGems version is > 2.15.0, need to set SPATIAL_LICENSE");
+    return ok;
+  }
+
+  //================================================================================
+  /*!
+   * \brief Set the SPATIAL_LICENSE environment variable
+   *  \param [out] error - return error description
+   *  \return bool - true in case of success
+   */
+  //================================================================================
+  bool SetMGSpatialEnvLicense(std::string& error)
+  {
+    int ok;
+    std::string key = GetKey(error);
+#ifndef WIN32
+    ok = setenv(SPATIAL_LICENSE, key.c_str(), 0); // 0 means do not overwrite
+#else
+    ok = Kernel_Utils::setenv(SPATIAL_LICENSE, key.c_str(), 0 );
+#endif
+    MESSAGE("Set SPATIAL_LICENSE");
+    return (ok==0);
+  }
+
+  //================================================================================
+  /*!
+   * \brief Get the license key from libMeshGemsKeyGenerator.so or $SPATIAL_LICENSE
+   * Called by plugins calling MG products as executables.
+   * If MESHGEMS_OLD_STYLE is set, return "0", to use old DLIM8 server license
+   * instead of the key.
+   *  \param [in] gmfFile - path to an input mesh file
+   *  \param [in] nb* - nb of entities in the input mesh
+   *  \param [out] error - return error description
+   *  \return std::string - the key
+   */
+  //================================================================================
   std::string GetKey(const std::string& gmfFile,
                     int                nbVertex,
                     int                nbEdge,
@@ -533,11 +694,67 @@ namespace SMESHUtils_MGLicenseKeyGen // API implementation
                     int                nbVol,
                     std::string&       error)
   {
+    // default key if MESHGEMS_OLD_STYLE or SPATIAL_LICENSE is set
+    std::string key("0");
     const char *meshGemsOldStyleEnvVar( getenv( MESHGEMS_OLD_STYLE ) );
     if ( !meshGemsOldStyleEnvVar || strlen(meshGemsOldStyleEnvVar) == 0 )
-      return GetKey_After(gmfFile,nbVertex,nbEdge,nbFace,nbVol,error);
-    else
-      return std::string("0");
+      {
+       const char *spatialLicenseEnvVar( getenv( SPATIAL_LICENSE ) );
+       if ( !spatialLicenseEnvVar || strlen(spatialLicenseEnvVar) == 0 )
+         {
+           if (NeedsMGSpatialEnvLicense(error))
+             {
+               // if MG version > 2.15, set environment license, don't return it as a key
+               // otherwise it will be printed in the command line
+               MESSAGE("SPATIAL_LICENSE not in env => we add it from MGKeygen .so");
+               SetMGSpatialEnvLicense(error);
+             }
+           else
+             {
+               // generate the key from the mesh info (MG 2.13 and 2.14)
+               MESSAGE("MG < 2.15 => get the key from MGKeygen .so and this mesh info");
+               key = GetKey_After(gmfFile,nbVertex,nbEdge,nbFace,nbVol,error);
+             }
+         }
+       else
+         MESSAGE("SPATIAL_LICENSE already in env => we use it");
+      }
+    if (! error.empty())
+      std::cerr << error;
+    return key;
+  }
+
+  //================================================================================
+  /*!
+   * \brief Get the license key from libMeshGemsKeyGenerator.so or $SPATIAL_LICENSE
+   * Called for MG 2.15 by CADSurf and MG plugins calling MG products as library,
+   * i.e. compiled as library with -DSALOME_USE_MG_LIBS=ON
+   *  \param [out] error - return error description
+   *  \return std::string - the key
+   */
+  //================================================================================
+  std::string GetKey(std::string&       error)
+  {
+    // default key if not found in .so or in SPATIAL_LICENSE
+    std::string key("0");
+    const char *meshGemsOldStyleEnvVar( getenv( MESHGEMS_OLD_STYLE ) );
+    if ( !meshGemsOldStyleEnvVar || strlen(meshGemsOldStyleEnvVar) == 0 ){
+      const char *spatialLicenseEnvVar( getenv( SPATIAL_LICENSE ) );
+      if ( !spatialLicenseEnvVar || strlen(spatialLicenseEnvVar) == 0 )
+       {
+         MESSAGE("SPATIAL_LICENSE not in env => we add it from MGKeygen .so");
+         // use new style, i.e. key in a library
+         key = GetKey_After(error);
+       }
+      else
+       {
+         MESSAGE("SPATIAL_LICENSE already in env => we use it");
+         key = std::string(spatialLicenseEnvVar);
+       }
+    }
+    if (! error.empty())
+      std::cerr << error;
+    return key;
   }
 
   //================================================================================
index f63b4d69e12a35bd8a134da08e6080500c8e5ad2..6f05f5d047aaf16c852ca01dc6f9169c7c1eac03 100644 (file)
 
 #include <string>
 
+#define MESHGEMS_215 (2 << 16 | 15 << 8 | 0)
+
 /*!
  * \brief Manage loading libSalomeMeshGemsKeyGenerator.[so|dll] and sing MeshGems CAD or mesh
  */
 
 namespace SMESHUtils_MGLicenseKeyGen
 {
+  // MeshGems 2.13, 2.14 (for CADSurf)
   SMESHUtils_EXPORT bool        SignCAD( void* meshgems_cad, std::string& error );
 
+  // MeshGems 2.13, 2.14 (for products launched as library)
   SMESHUtils_EXPORT bool        SignMesh( void* meshgems_mesh, std::string& error );
 
+  // MeshGems 2.13, 2.14 and 2.15 (for products launched as executables)
   SMESHUtils_EXPORT std::string GetKey(const std::string& gmfFile,
                                        int                nbVertex,
                                        int                nbEdge,
@@ -44,9 +49,16 @@ namespace SMESHUtils_MGLicenseKeyGen
                                        int                nbVol,
                                        std::string&       error);
 
+  // MeshGems 2.15 (for products launched as library)
+  SMESHUtils_EXPORT std::string GetKey( std::string& error );
+
   SMESHUtils_EXPORT bool        CheckKeyGenLibrary( std::string& error );
 
   SMESHUtils_EXPORT std::string GetLibraryName();
+
+  SMESHUtils_EXPORT int         GetMGVersionHex(std::string& error);
+  SMESHUtils_EXPORT bool        NeedsMGSpatialEnvLicense(std::string& error);
+  SMESHUtils_EXPORT bool        SetMGSpatialEnvLicense(std::string& error);
 }
 
 #endif
index 081c673c67e620c142f1a63356571cabc516fe85..246bd6cb0b65214fc84c5e4d020316b287b04c8b 100644 (file)
@@ -568,9 +568,10 @@ class MGCleanerMonPlugDialog(Ui_MGCleanerPlugDialog,QWidget):
 
     import SMeshHelper
     key = SMeshHelper.GetMGLicenseKey( self.fichierIn )
-    self.commande+=' --key ' + key
+    if key != "0":
+      self.commande+=' --key ' + key
 
-    if verbose: print(("INFO: MGCCleaner command:\n  %s" % self.commande))
+    if verbose: print("INFO: MG-Cleaner command:\n  %s" % self.commande)
     return True
 
   def clean(self):
index dba2a29cc5d4d5df14fb5d36a9b7c096f507c46e..ae61e4134f129d46e4993039905e0bea4b0dbc12 100644 (file)
@@ -548,9 +548,10 @@ class MonYamsPlugDialog(Ui_YamsPlugDialog,QWidget):
 
     import SMeshHelper
     key = SMeshHelper.GetMGLicenseKey( self.fichierIn )
-    self.commande+=' --key ' + key
-    
-    print(self.commande)
+    if key != "0":
+      self.commande+=' --key ' + key
+
+    if verbose: print("INFO: MG-SurfOpt command:\n  %s" % self.commande)
     return True
 
   def clean(self):