Skip to content

Commit

Permalink
make OPENMODELICAHOME have precedence over exe/so path
Browse files Browse the repository at this point in the history
- libOpenModelicaCompiler can be used from other programs that
  may not be installed in the bin directory of OpenModelica,
  using the path from that exe is really wrong!
  • Loading branch information
adrpo committed Jul 10, 2019
1 parent 5e27012 commit bf4545d
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions OMCompiler/Compiler/runtime/settingsimpl.c
Expand Up @@ -64,7 +64,14 @@ static char* winPath = NULL;
/* Helper function to strip /bin/... or /lib/... from the executable path of omc */
static void stripbinpath(char *omhome)
{
char *tmp;
char *tmp = NULL;
/* adrpo: if the path does not contain "bin" or "lib" exit gracefully as otherwise the assertion will trigger */
if (strstr(omhome, "bin") == NULL && strstr(omhome, "lib") == NULL)
{
fprintf(stderr, "could not deduce the OpenModelica installation directory from executable path: [%s], please set OPENMODELICAHOME", omhome);
exit(EXIT_FAILURE);
}

do {
assert(tmp = strrchr(omhome,'/'));
*tmp = '\0';
Expand All @@ -86,14 +93,21 @@ const char* SettingsImpl__getInstallationDirectoryPath(void) {
const char* SettingsImpl__getInstallationDirectoryPath(void) {
struct stat sb;
static char omhome[PATH_MAX];
const char *omhome_env = getenv("OPENMODELICAHOME");
static int init = 0;
ssize_t r;
/* This is bad code using hard-coded limits; but we cannot query the size of symlinks on /proc
* because that FS is not POSIX-compliant.
*/
if (omhome_env != NULL) {
strcpy(omhome, omhome_env);
return omhome;
}

if (init) {
return omhome;
}

r = readlink("/proc/self/exe", omhome, sizeof(omhome)-1);
if (r < 0) {
perror("readlink");
Expand All @@ -113,9 +127,9 @@ const char* SettingsImpl__getInstallationDirectoryPath(void) {
const char* SettingsImpl__getInstallationDirectoryPath(void) {
int ret;
pid_t pid;
static char *omhome;
static char *omhome = getenv("OPENMODELICAHOME");
static int init = 0;
if (init) {
if (init || omhome != NULL) {
return omhome;
}

Expand Down Expand Up @@ -143,11 +157,18 @@ const char* SettingsImpl__getInstallationDirectoryPath(void) {
int ret;
pid_t pid;
static char omhome[PROC_PIDPATHINFO_MAXSIZE];
const char* omhome_env = getenv("OPENMODELICAHOME");
static int init = 0;
if (init) {
if (omhome_env != NULL) {
strcpy(omhome, omhome_env);
return omhome;
}

if (init)
{
return omhome;
}

pid = getpid();
ret = proc_pidpath(pid, omhome, sizeof(omhome));
if (ret <= 0) {
Expand Down

0 comments on commit bf4545d

Please sign in to comment.