From 0949cd9b79a501949a1e56b338e639b965d38c49 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Tue, 17 Mar 2015 17:31:41 +0000
Subject: [PATCH] better GetExecutableFileName

---
 Common/Context.h        |  3 ++-
 Common/DefaultOptions.h |  2 ++
 Common/GmshMessage.cpp  | 19 ++++++++++---------
 Common/OS.cpp           |  5 +----
 Common/OS.h             |  2 +-
 Common/Options.cpp      |  5 +++++
 Common/Options.h        |  1 +
 Fltk/FlGui.cpp          |  2 ++
 Fltk/onelabGroup.cpp    |  2 +-
 9 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/Common/Context.h b/Common/Context.h
index 15141d93f4..d84b1dc4cc 100644
--- a/Common/Context.h
+++ b/Common/Context.h
@@ -90,7 +90,8 @@ class CTX {
   std::string bgmFileName, outputFileName, defaultFileName, tmpFileName;
   std::string sessionFileName, optionsFileName, errorFileName;
   std::string meshStatReportFileName;
-  std::string argv0;
+  // filename of the executable, with full path
+  std::string exeFileName;
   // the home directory
   std::string homeDir;
   // file history
diff --git a/Common/DefaultOptions.h b/Common/DefaultOptions.h
index 891399934f..ec1bf764e1 100644
--- a/Common/DefaultOptions.h
+++ b/Common/DefaultOptions.h
@@ -53,6 +53,8 @@ StringXString GeneralOptions_String[] = {
     ".gmsh-errors" ,
 #endif
     "File into which the log is saved if a fatal error occurs" },
+  { F|S, "ExecutableFileName", opt_general_executable_filename , "" ,
+    "File name of the Gmsh executable (read-only)"},
 
   { F,   "FileName" , opt_general_filename , "" ,
     "Current project file name (read-only)" },
diff --git a/Common/GmshMessage.cpp b/Common/GmshMessage.cpp
index c852c493f7..3f6e145b1a 100644
--- a/Common/GmshMessage.cpp
+++ b/Common/GmshMessage.cpp
@@ -99,7 +99,7 @@ static int vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
 
 static void addGmshPathToEnvironmentVar(const std::string &name)
 {
-  std::string gmshPath = SplitFileName(GetExecutableName(CTX::instance()->argv0))[0];
+  std::string gmshPath = SplitFileName(CTX::instance()->exeFileName)[0];
   if(gmshPath.size()){
     std::string path;
     char *tmp = getenv(name.c_str());
@@ -153,14 +153,15 @@ void Msg::Init(int argc, char **argv)
     _commandLine += argv[i];
   }
 
-  if(argc && argv){
-    CTX::instance()->argv0 = std::string(argv[0]);
-    // add the directory where the binary is installed to the path where Python
-    // looks for modules, and to the path for executables (this allows us to
-    // find the onelab.py module or subclients automatically)
-    addGmshPathToEnvironmentVar("PYTHONPATH");
-    addGmshPathToEnvironmentVar("PATH");
-  }
+  CTX::instance()->exeFileName = GetExecutableFileName();
+  if(CTX::instance()->exeFileName.empty() && argc && argv)
+    CTX::instance()->exeFileName = argv[0];
+
+  // add the directory where the binary is installed to the path where Python
+  // looks for modules, and to the path for executables (this allows us to find
+  // the onelab.py module or subclients automatically)
+  addGmshPathToEnvironmentVar("PYTHONPATH");
+  addGmshPathToEnvironmentVar("PATH");
 
   InitializeOnelab("Gmsh");
 }
diff --git a/Common/OS.cpp b/Common/OS.cpp
index 5b7785c304..ebb7903eef 100644
--- a/Common/OS.cpp
+++ b/Common/OS.cpp
@@ -375,7 +375,7 @@ int GetProcessId()
 #endif
 }
 
-std::string GetExecutableName(const std::string &argv0)
+std::string GetExecutableFileName()
 {
   std::string name = "";
 #if defined(WIN32) && !defined(__CYGWIN__)
@@ -401,9 +401,6 @@ std::string GetExecutableName(const std::string &argv0)
     name = std::string(path);
   }
 #endif
-  if(name.empty()){
-    name = argv0;
-  }
   return name;
 }
 
diff --git a/Common/OS.h b/Common/OS.h
index 6290b76522..449036c677 100644
--- a/Common/OS.h
+++ b/Common/OS.h
@@ -18,7 +18,7 @@ double Cpu();
 double TotalRam();
 long GetMemoryUsage();
 int GetProcessId();
-std::string GetExecutableName(const std::string &argv0);
+std::string GetExecutableFileName();
 std::string GetHostName();
 int UnlinkFile(const std::string &fileName);
 int StatFile(const std::string &fileName);
diff --git a/Common/Options.cpp b/Common/Options.cpp
index 29e42470aa..0e76656ccb 100644
--- a/Common/Options.cpp
+++ b/Common/Options.cpp
@@ -1113,6 +1113,11 @@ std::string opt_general_filename(OPT_ARGS_STR)
   return GModel::current()->getFileName();
 }
 
+std::string opt_general_executable_filename(OPT_ARGS_STR)
+{
+  return CTX::instance()->exeFileName;
+}
+
 std::string opt_general_default_filename(OPT_ARGS_STR)
 {
   if(action & GMSH_SET)
diff --git a/Common/Options.h b/Common/Options.h
index 2c5e8d88c4..c4ed742ad0 100644
--- a/Common/Options.h
+++ b/Common/Options.h
@@ -36,6 +36,7 @@ std::string opt_general_axes_format2(OPT_ARGS_STR);
 std::string opt_general_background_image_filename(OPT_ARGS_STR);
 std::string opt_general_display(OPT_ARGS_STR);
 std::string opt_general_filename(OPT_ARGS_STR);
+std::string opt_general_executable_filename(OPT_ARGS_STR);
 std::string opt_general_default_filename(OPT_ARGS_STR);
 std::string opt_general_tmp_filename(OPT_ARGS_STR);
 std::string opt_general_error_filename(OPT_ARGS_STR);
diff --git a/Fltk/FlGui.cpp b/Fltk/FlGui.cpp
index 7a39240781..01c0b0dcce 100644
--- a/Fltk/FlGui.cpp
+++ b/Fltk/FlGui.cpp
@@ -40,6 +40,7 @@ typedef unsigned long intptr_t;
 #include "GmshDefines.h"
 #include "GmshMessage.h"
 #include "GModel.h"
+#include "OS.h"
 #include "MElement.h"
 #include "PView.h"
 #include "Field.h"
@@ -429,6 +430,7 @@ FlGui *FlGui::instance(int argc, char **argv)
     Msg::Info("Build date     : %s", GetGmshBuildDate());
     Msg::Info("Build host     : %s", GetGmshBuildHost());
     Msg::Info("Packager       : %s", GetGmshPackager());
+    Msg::Info("Executable     : %s", CTX::instance()->exeFileName.c_str());
     Msg::Info("Home directory : %s", CTX::instance()->homeDir.c_str());
     Msg::Info("Launch date    : %s", Msg::GetLaunchDate().c_str());
     Msg::Info("Command line   : %s", Msg::GetCommandLineArgs().c_str());
diff --git a/Fltk/onelabGroup.cpp b/Fltk/onelabGroup.cpp
index 0826d910ed..fabb78e718 100644
--- a/Fltk/onelabGroup.cpp
+++ b/Fltk/onelabGroup.cpp
@@ -253,7 +253,7 @@ static void onelab_choose_executable_cb(Fl_Widget *w, void *data)
 
     // try to find an executable automatically (this is really useful for
     // beginners)
-    std::string gmshPath = SplitFileName(GetExecutableName(CTX::instance()->argv0))[0];
+    std::string gmshPath = SplitFileName(CTX::instance()->exeFileName)[0];
     if(gmshPath.size()){
       std::string name = c->getName();
       for(unsigned int i = 0; i < name.size(); i++)
-- 
GitLab