Skip to content

Commit bf4545d

Browse files
committed
make OPENMODELICAHOME have precedence over exe/so path
- 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!
1 parent 5e27012 commit bf4545d

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

OMCompiler/Compiler/runtime/settingsimpl.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,14 @@ static char* winPath = NULL;
6464
/* Helper function to strip /bin/... or /lib/... from the executable path of omc */
6565
static void stripbinpath(char *omhome)
6666
{
67-
char *tmp;
67+
char *tmp = NULL;
68+
/* adrpo: if the path does not contain "bin" or "lib" exit gracefully as otherwise the assertion will trigger */
69+
if (strstr(omhome, "bin") == NULL && strstr(omhome, "lib") == NULL)
70+
{
71+
fprintf(stderr, "could not deduce the OpenModelica installation directory from executable path: [%s], please set OPENMODELICAHOME", omhome);
72+
exit(EXIT_FAILURE);
73+
}
74+
6875
do {
6976
assert(tmp = strrchr(omhome,'/'));
7077
*tmp = '\0';
@@ -86,14 +93,21 @@ const char* SettingsImpl__getInstallationDirectoryPath(void) {
8693
const char* SettingsImpl__getInstallationDirectoryPath(void) {
8794
struct stat sb;
8895
static char omhome[PATH_MAX];
96+
const char *omhome_env = getenv("OPENMODELICAHOME");
8997
static int init = 0;
9098
ssize_t r;
9199
/* This is bad code using hard-coded limits; but we cannot query the size of symlinks on /proc
92100
* because that FS is not POSIX-compliant.
93101
*/
102+
if (omhome_env != NULL) {
103+
strcpy(omhome, omhome_env);
104+
return omhome;
105+
}
106+
94107
if (init) {
95108
return omhome;
96109
}
110+
97111
r = readlink("/proc/self/exe", omhome, sizeof(omhome)-1);
98112
if (r < 0) {
99113
perror("readlink");
@@ -113,9 +127,9 @@ const char* SettingsImpl__getInstallationDirectoryPath(void) {
113127
const char* SettingsImpl__getInstallationDirectoryPath(void) {
114128
int ret;
115129
pid_t pid;
116-
static char *omhome;
130+
static char *omhome = getenv("OPENMODELICAHOME");
117131
static int init = 0;
118-
if (init) {
132+
if (init || omhome != NULL) {
119133
return omhome;
120134
}
121135

@@ -143,11 +157,18 @@ const char* SettingsImpl__getInstallationDirectoryPath(void) {
143157
int ret;
144158
pid_t pid;
145159
static char omhome[PROC_PIDPATHINFO_MAXSIZE];
160+
const char* omhome_env = getenv("OPENMODELICAHOME");
146161
static int init = 0;
147-
if (init) {
162+
if (omhome_env != NULL) {
163+
strcpy(omhome, omhome_env);
148164
return omhome;
149165
}
150166

167+
if (init)
168+
{
169+
return omhome;
170+
}
171+
151172
pid = getpid();
152173
ret = proc_pidpath(pid, omhome, sizeof(omhome));
153174
if (ret <= 0) {

0 commit comments

Comments
 (0)