-
Notifications
You must be signed in to change notification settings - Fork 298
/
settingsimpl.c
366 lines (324 loc) · 10.7 KB
/
settingsimpl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2011, Linköpings University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
* LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
* THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
* PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköpings University, either from the above address,
* from the URL: http://www.ida.liu.se/projects/OpenModelica
* and in the OpenModelica distribution.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "omc_config.h"
#if defined(OMC_BOOTSTRAPPING)
#include "../boot/tarball-include/OpenModelicaBootstrappingHeader.h"
#else
#include "../OpenModelicaBootstrappingHeader.h"
#endif
#if defined(_MSC_VER) || defined(__MINGW32__)
#define OMC_GROUP_DELIMITER ";"
#else
#define OMC_GROUP_DELIMITER ":"
#include <unistd.h>
#include <pwd.h>
#endif
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
static char* compileCommand = 0;
static char* compilePath = 0;
static char* tempDirectoryPath = 0;
static int echo = 1; //true
static char* omc_installationPath = NULL;
static char* omc_modelicaPath = NULL;
static char* omc_userHome = NULL;
extern char* _replace(char* source_str,char* search_str,char* replace_str); //Defined in systemimpl.c
static void commonSetEnvVar(const char *var, const char *value);
/* convert to fowaard slahes in place */
char* covertToForwardSlashesInPlace(char* path) {
#if defined(__MINGW32__) || defined(__MINGW64__) || defined(_MSC_VER) /* Not linux or Apple */
int i = 0;
while(path[i] != '\0') {
if (path[i] == '\\') path[i] = '/';
i++;
}
#endif
return path;
}
#if !defined(OPENMODELICA_BOOTSTRAPPING_STAGE_1) && (defined(__linux__) || defined(__APPLE_CC__))
/* Helper function to strip /bin/... or /lib/... from the executable path of omc */
static void stripbinpath(char *omhome)
{
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 {
tmp = strrchr(omhome,'/');
assert(tmp);
*tmp = '\0';
} while (strcmp(tmp+1,"bin") && strcmp(tmp+1,"lib"));
return;
}
#endif
/* Do not free or modify the returned variable of getInstallationDirectoryPath. It's part of the environment! */
#if defined(OPENMODELICA_BOOTSTRAPPING_STAGE_1) && !(defined(_MSC_VER) || defined(__MINGW32__))
const char* SettingsImpl__getInstallationDirectoryPath(void) {
const char *path = getenv("OPENMODELICAHOME");
return path ? path : "OPENMODELICA_BOOTSTRAPPING_STAGE_1_NO_OPENMODELICAHOME";
}
#elif defined(__linux__) || defined(__APPLE_CC__)
#include <dlfcn.h>
const char* SettingsImpl__getInstallationDirectoryPath(void) {
int ret;
if (omc_installationPath) {
return omc_installationPath;
}
Dl_info info;
if (!dladdr((void*) SettingsImpl__getInstallationDirectoryPath, &info)) {
fprintf(stderr, "dladdr() failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
} else {
omc_installationPath = omc_alloc_interface.malloc_strdup(info.dli_fname);
stripbinpath(omc_installationPath);
}
if (!(omc_installationPath && omc_installationPath[0])) {
fprintf(stderr, "Failed to get binary path from dladdr path: %s\n", info.dli_fname);
exit(EXIT_FAILURE);
}
commonSetEnvVar("OPENMODELICAHOME", omc_installationPath);
return omc_installationPath;
}
#elif defined(__MINGW32__) || defined(__MINGW64__) || defined(_MSC_VER) /* Not linux or Apple */
const char* SettingsImpl__getInstallationDirectoryPath(void) {
int i = 0;
if (omc_installationPath) {
return omc_installationPath;
}
if (omc_installationPath == NULL) {
char filename[MAX_PATH];
if (0 != GetModuleFileName(GetModuleHandle("libOpenModelicaCompiler.dll"), filename, MAX_PATH)) {
omc_installationPath = strdup(filename); /* duplicate the path */
*strrchr(omc_installationPath, '\\') = '\0';
*strrchr(omc_installationPath, '\\') = '\0';
}
else
{
return CONFIG_DEFAULT_OPENMODELICAHOME; // On Windows, this is NULL; on Unix it is the configured --prefix
}
}
omc_installationPath = covertToForwardSlashesInPlace(omc_installationPath);
commonSetEnvVar("OPENMODELICAHOME", omc_installationPath);
return (const char*)omc_installationPath;
}
#endif
char* Settings_getHomeDir(int runningTestsuite)
{
if (runningTestsuite) {
return omc_alloc_interface.malloc_strdup("");
}
if (omc_userHome)
{
return omc_userHome;
}
#if !(defined(_MSC_VER) || defined(__MINGW32__))
omc_userHome = getenv("HOME");
if (omc_userHome == NULL) {
omc_userHome = getpwuid(getuid())->pw_dir;
}
#else /* windows & mingw */
omc_userHome = getenv("APPDATA");
if (omc_userHome == NULL) {
omc_userHome = getenv("HOME");
}
#endif
if (omc_userHome == NULL || runningTestsuite) {
return omc_alloc_interface.malloc_strdup("");
}
omc_userHome = omc_alloc_interface.malloc_strdup(omc_userHome);
return covertToForwardSlashesInPlace(omc_userHome);
}
/*
* - if already set, use it
* - if not set, use OPENMODELICALIBRARY
* - if not set, get the installation path and use that
*/
char* SettingsImpl__getModelicaPath(int runningTestsuite) {
if (omc_modelicaPath) {
return omc_modelicaPath;
}
{
/* if we are running the testsuite, use the default */
const char *path = runningTestsuite ? NULL : getenv("OPENMODELICALIBRARY");
if (path != NULL)
{
omc_modelicaPath = strdup(path);
}
else
{
/* get the install directory */
const char *omhome = SettingsImpl__getInstallationDirectoryPath();
if (omhome == NULL)
return NULL;
int lenOmhome = strlen(omhome);
const char *homePath = Settings_getHomeDir(0);
assert(homePath != NULL || !runningTestsuite);
if (runningTestsuite) {
int lenHome = strlen(homePath);
omc_modelicaPath = (char*)malloc(lenHome+26);
snprintf(omc_modelicaPath, lenHome+26,"%s/.openmodelica/libraries/", homePath);
} else if (homePath == NULL) {
omc_modelicaPath = (char*)malloc(lenOmhome+15);
snprintf(omc_modelicaPath, lenOmhome+15,"%s/lib/omlibrary", omhome);
} else {
int lenHome = strlen(homePath);
omc_modelicaPath = (char*)omc_alloc_interface.malloc_atomic(lenOmhome+lenHome+41);
snprintf(omc_modelicaPath, lenOmhome+lenHome+41,"%s/lib/omlibrary%s%s/.openmodelica/libraries/", omhome, OMC_GROUP_DELIMITER, homePath);
}
}
omc_modelicaPath = covertToForwardSlashesInPlace(omc_modelicaPath);
if (!runningTestsuite) {
commonSetEnvVar("OPENMODELICALIBRARY", omc_modelicaPath);
}
}
return omc_modelicaPath;
}
static const char* SettingsImpl__getCompileCommand(void)
{
if (compileCommand == NULL) {
// Get a default command
const char *res = getenv("MC_DEFAULT_COMPILE_CMD");
if (res == NULL)
return DEFAULT_CXX;
return res;
}
return compileCommand;
}
extern void SettingsImpl__setCompileCommand(const char *command)
{
if(compileCommand)
free(compileCommand);
compileCommand = strdup(command);
}
static const char* SettingsImpl__getCompilePath(void)
{
if (compilePath == NULL) {
// Get a default command
const char *res = getenv("MC_DEFAULT_COMPILE_PATH");
if (res == NULL)
return "";
return res;
}
return compilePath;
}
extern void SettingsImpl__setCompilePath(const char *path) {
if(compilePath)
free(compilePath);
compilePath = strdup(path);
}
static void commonSetEnvVar(const char *var, const char *value)
{
int lenVar = strlen(var);
int lenVal = strlen(value);
char* command = (char*) malloc(lenVar+lenVal+2);
assert(command != NULL);
/* create a str of the form: <VAR>=<VALUE>*/
snprintf(command, lenVar+lenVal+2, "%s=%s", var, value);
command[lenVar+lenVal+1]='\0';
/* set the env-var to created string this is useful when scripts and clients started by omc wants to use OPENMODELICAHOME*/
assert(putenv(command) == 0); // adrpo: in Linux there is not _putenv if( _putenv(omhome) != 0)
#if defined(WIN32)
/* Only free on windows, in Linux the environment is taking over the ownership of the ptr */
free(command);
#endif
}
extern void SettingsImpl__setInstallationDirectoryPath(const char *value)
{
if (value[0] == '\0') /* clear it if is empty */
{
omc_installationPath = NULL;
return;
}
omc_installationPath = strdup(value);
omc_installationPath = covertToForwardSlashesInPlace(omc_installationPath);
commonSetEnvVar("OPENMODELICAHOME", omc_installationPath);
}
extern void SettingsImpl__setModelicaPath(const char *value)
{
if (value[0] == '\0') /* clear it if is empty */
{
omc_modelicaPath = NULL;
return;
}
omc_modelicaPath = strdup(value);
omc_modelicaPath = covertToForwardSlashesInPlace(omc_modelicaPath);
commonSetEnvVar("OPENMODELICALIBRARY", omc_modelicaPath);
}
extern void SettingsImpl__setTempDirectoryPath(const char *path)
{
if (tempDirectoryPath)
free(tempDirectoryPath);
tempDirectoryPath = strdup(path);
}
extern const char* SettingsImpl__getTempDirectoryPath(void)
{
if (tempDirectoryPath == NULL) {
// On windows, set Temp directory path to Temp directory as returned by GetTempPath,
// which is usually TMP or TEMP or windows catalogue.
#ifdef WIN32
int numChars;
char tempDirectory[1024];
//extract the temp path
numChars= GetTempPath(1024, tempDirectory);
if (numChars == 1024 || numChars == 0) {
fprintf(stderr, "Error setting temppath in Kernel\n");
exit(1);
} else {
// Must do replacement in two steps, since the _replace function can not have similar source as target.
char *str = _replace(tempDirectory, (char*)"\\", (char*)"/");
tempDirectoryPath = _replace(str, (char*)"/", (char*)"\\\\");
GC_free(str);
}
#else
const char* str = getenv("TMPDIR");
if (str == NULL) {
tempDirectoryPath = strdup("/tmp");
} else {
tempDirectoryPath = strdup(str);
}
#endif
}
return tempDirectoryPath;
}
#ifdef __cplusplus
}
#endif