Skip to content

Commit 8a9ce9d

Browse files
committed
refs modelica#4576: Update gconstructor handling
* Introduce and apply name mangling to avoid MSVC duplicated symbols issue * Bump version by renaming file and macros
1 parent 99dff88 commit 8a9ce9d

File tree

5 files changed

+88
-76
lines changed

5 files changed

+88
-76
lines changed

Modelica/Resources/BuildProjects/CMake/src.cmake

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ set(EXTC_SOURCES
7070
"${MODELICA_SOURCE_DIR}/ModelicaRandom.h"
7171
"${MODELICA_SOURCE_DIR}/ModelicaStrings.c"
7272
"${MODELICA_SOURCE_DIR}/ModelicaStrings.h"
73-
"${MODELICA_SOURCE_DIR}/gconstructor.h"
73+
"${MODELICA_SOURCE_DIR}/g2constructor.h"
7474
"${MODELICA_SOURCE_DIR}/stdint_msvc.h"
7575
"${MODELICA_SOURCE_DIR}/stdint_wrap.h"
7676
"${MODELICA_SOURCE_DIR}/uthash.h"
@@ -84,7 +84,7 @@ set(TABLES_SOURCES
8484
"${MODELICA_SOURCE_DIR}/ModelicaStandardTables.h"
8585
"${MODELICA_SOURCE_DIR}/ModelicaStandardTablesUsertab.c"
8686
"${MODELICA_SOURCE_DIR}/ModelicaMatIO.h"
87-
"${MODELICA_SOURCE_DIR}/gconstructor.h"
87+
"${MODELICA_SOURCE_DIR}/g2constructor.h"
8888
"${MODELICA_SOURCE_DIR}/stdint_msvc.h"
8989
"${MODELICA_SOURCE_DIR}/stdint_wrap.h"
9090
"${MODELICA_SOURCE_DIR}/uthash.h"

Modelica/Resources/C-Sources/ModelicaInternal.c

+15-15
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ void ModelicaInternal_setenv(_In_z_ const char* name,
279279
#define HASH_NO_STDINT 1
280280
#define HASH_NONFATAL_OOM 1
281281
#include "uthash.h"
282-
#include "gconstructor.h"
282+
#include "g2constructor.h"
283283

284284
#include <string.h>
285285
#include <stdio.h>
@@ -774,16 +774,16 @@ typedef struct FileCache {
774774
static FileCache* fileCache = NULL;
775775
#if defined(_POSIX_) && !defined(NO_MUTEX)
776776
#include <pthread.h>
777-
#if defined(G_HAS_CONSTRUCTORS)
777+
#if defined(G2_HAS_CONSTRUCTORS)
778778
static pthread_mutex_t m;
779-
G_DEFINE_CONSTRUCTOR(initializeMutex)
780-
static void initializeMutex(void) {
779+
G2_DEFINE_CONSTRUCTOR(G2_FUNCNAME(initializeMutex))
780+
static void G2_FUNCNAME(initializeMutex)(void) {
781781
if (pthread_mutex_init(&m, NULL) != 0) {
782782
ModelicaError("Initialization of mutex failed\n");
783783
}
784784
}
785-
G_DEFINE_DESTRUCTOR(destroyMutex)
786-
static void destroyMutex(void) {
785+
G2_DEFINE_DESTRUCTOR(G2_FUNCNAME(destroyMutex))
786+
static void G2_FUNCNAME(destroyMutex)(void) {
787787
if (pthread_mutex_destroy(&m) != 0) {
788788
ModelicaError("Destruction of mutex failed\n");
789789
}
@@ -793,24 +793,24 @@ static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
793793
#endif
794794
#define MUTEX_LOCK() pthread_mutex_lock(&m)
795795
#define MUTEX_UNLOCK() pthread_mutex_unlock(&m)
796-
#elif defined(_WIN32) && defined(G_HAS_CONSTRUCTORS)
796+
#elif defined(_WIN32) && defined(G2_HAS_CONSTRUCTORS)
797797
#if !defined(WIN32_LEAN_AND_MEAN)
798798
#define WIN32_LEAN_AND_MEAN
799799
#endif
800800
#include <windows.h>
801801
static CRITICAL_SECTION cs;
802-
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
803-
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(ModelicaInternal_initializeCS)
802+
#ifdef G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
803+
#pragma G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(ModelicaInternal_initializeCS))
804804
#endif
805-
G_DEFINE_CONSTRUCTOR(ModelicaInternal_initializeCS)
806-
static void ModelicaInternal_initializeCS(void) {
805+
G2_DEFINE_CONSTRUCTOR(G2_FUNCNAME(ModelicaInternal_initializeCS))
806+
static void G2_FUNCNAME(ModelicaInternal_initializeCS)(void) {
807807
InitializeCriticalSection(&cs);
808808
}
809-
#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
810-
#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(ModelicaInternal_deleteCS)
809+
#ifdef G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
810+
#pragma G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(ModelicaInternal_deleteCS))
811811
#endif
812-
G_DEFINE_DESTRUCTOR(ModelicaInternal_deleteCS)
813-
static void ModelicaInternal_deleteCS(void) {
812+
G2_DEFINE_DESTRUCTOR(G2_FUNCNAME(ModelicaInternal_deleteCS))
813+
static void G2_FUNCNAME(ModelicaInternal_deleteCS)(void) {
814814
DeleteCriticalSection(&cs);
815815
}
816816
#define MUTEX_LOCK() EnterCriticalSection(&cs)

Modelica/Resources/C-Sources/ModelicaRandom.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#include <string.h>
4949
#include "ModelicaInternal.h"
5050
#include "ModelicaUtilities.h"
51-
#include "gconstructor.h"
51+
#include "g2constructor.h"
5252

5353
/* The standard way to detect POSIX is to check _POSIX_VERSION,
5454
* which is defined in <unistd.h>
@@ -68,24 +68,24 @@ static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
6868
#define MUTEX_UNLOCK() pthread_mutex_unlock(&m)
6969

7070
/* On Windows systems define a critical section using the single static variable "cs" */
71-
#elif defined(_WIN32) && defined(G_HAS_CONSTRUCTORS)
71+
#elif defined(_WIN32) && defined(G2_HAS_CONSTRUCTORS)
7272
#if !defined(WIN32_LEAN_AND_MEAN)
7373
#define WIN32_LEAN_AND_MEAN
7474
#endif
7575
#include <windows.h>
7676
static CRITICAL_SECTION cs;
77-
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
78-
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(ModelicaRandom_initializeCS)
77+
#ifdef G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
78+
#pragma G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(ModelicaRandom_initializeCS))
7979
#endif
80-
G_DEFINE_CONSTRUCTOR(ModelicaRandom_initializeCS)
81-
static void ModelicaRandom_initializeCS(void) {
80+
G2_DEFINE_CONSTRUCTOR(G2_FUNCNAME(ModelicaRandom_initializeCS))
81+
static void G2_FUNCNAME(ModelicaRandom_initializeCS)(void) {
8282
InitializeCriticalSection(&cs);
8383
}
84-
#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
85-
#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(ModelicaRandom_deleteCS)
84+
#ifdef G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
85+
#pragma G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(ModelicaRandom_deleteCS))
8686
#endif
87-
G_DEFINE_DESTRUCTOR(ModelicaRandom_deleteCS)
88-
static void ModelicaRandom_deleteCS(void) {
87+
G2_DEFINE_DESTRUCTOR(G2_FUNCNAME(ModelicaRandom_deleteCS))
88+
static void G2_FUNCNAME(ModelicaRandom_deleteCS)(void) {
8989
DeleteCriticalSection(&cs);
9090
}
9191
#define MUTEX_LOCK() EnterCriticalSection(&cs)

Modelica/Resources/C-Sources/ModelicaStandardTables.c

+15-15
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@
194194
#define uthash_strlen(s) key_strlen(s)
195195
#define HASH_NONFATAL_OOM 1
196196
#include "uthash.h"
197-
#include "gconstructor.h"
197+
#include "g2constructor.h"
198198
#endif
199199
#include <assert.h>
200200
#include <float.h>
@@ -433,16 +433,16 @@ typedef struct TableShare {
433433
static TableShare* tableShare = NULL;
434434
#if defined(_POSIX_) && !defined(NO_MUTEX)
435435
#include <pthread.h>
436-
#if defined(G_HAS_CONSTRUCTORS)
436+
#if defined(G2_HAS_CONSTRUCTORS)
437437
static pthread_mutex_t m;
438-
G_DEFINE_CONSTRUCTOR(initializeMutex)
439-
static void initializeMutex(void) {
438+
G2_DEFINE_CONSTRUCTOR(G2_FUNCNAME(initializeMutex))
439+
static void G2_FUNCNAME(initializeMutex)(void) {
440440
if (pthread_mutex_init(&m, NULL) != 0) {
441441
ModelicaError("Initialization of mutex failed\n");
442442
}
443443
}
444-
G_DEFINE_DESTRUCTOR(destroyMutex)
445-
static void destroyMutex(void) {
444+
G2_DEFINE_DESTRUCTOR(G2_FUNCNAME(destroyMutex))
445+
static void G2_FUNCNAME(destroyMutex)(void) {
446446
if (pthread_mutex_destroy(&m) != 0) {
447447
ModelicaError("Destruction of mutex failed\n");
448448
}
@@ -452,24 +452,24 @@ static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
452452
#endif
453453
#define MUTEX_LOCK() pthread_mutex_lock(&m)
454454
#define MUTEX_UNLOCK() pthread_mutex_unlock(&m)
455-
#elif defined(_WIN32) && defined(G_HAS_CONSTRUCTORS)
455+
#elif defined(_WIN32) && defined(G2_HAS_CONSTRUCTORS)
456456
#if !defined(WIN32_LEAN_AND_MEAN)
457457
#define WIN32_LEAN_AND_MEAN
458458
#endif
459459
#include <windows.h>
460460
static CRITICAL_SECTION cs;
461-
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
462-
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(ModelicaStandardTables_initializeCS)
461+
#ifdef G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
462+
#pragma G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(ModelicaStandardTables_initializeCS))
463463
#endif
464-
G_DEFINE_CONSTRUCTOR(ModelicaStandardTables_initializeCS)
465-
static void ModelicaStandardTables_initializeCS(void) {
464+
G2_DEFINE_CONSTRUCTOR(G2_FUNCNAME(ModelicaStandardTables_initializeCS))
465+
static void G2_FUNCNAME(ModelicaStandardTables_initializeCS)(void) {
466466
InitializeCriticalSection(&cs);
467467
}
468-
#ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
469-
#pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(ModelicaStandardTables_deleteCS)
468+
#ifdef G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
469+
#pragma G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(G2_FUNCNAME(ModelicaStandardTables_deleteCS))
470470
#endif
471-
G_DEFINE_DESTRUCTOR(ModelicaStandardTables_deleteCS)
472-
static void ModelicaStandardTables_deleteCS(void) {
471+
G2_DEFINE_DESTRUCTOR(G2_FUNCNAME(ModelicaStandardTables_deleteCS))
472+
static void G2_FUNCNAME(ModelicaStandardTables_deleteCS)(void) {
473473
DeleteCriticalSection(&cs);
474474
}
475475
#define MUTEX_LOCK() EnterCriticalSection(&cs)

Modelica/Resources/C-Sources/gconstructor.h renamed to Modelica/Resources/C-Sources/g2constructor.h

+46-34
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,82 @@
1-
/* gconstructor.h - Module constructor and destructor helper header
1+
/* g2constructor.h - Module constructor and destructor helper header
22
3-
If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and
3+
If G2_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and
44
destructors, in a sane way, including e.g. on library unload. If not you're on
55
your own.
66
77
Some compilers need #pragma to handle this, which does not work with macros,
88
so the way you need to use this is (for constructors):
99
10-
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
11-
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor)
10+
#ifdef G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
11+
#pragma G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor)
1212
#endif
13-
G_DEFINE_CONSTRUCTOR(my_constructor)
13+
G2_DEFINE_CONSTRUCTOR(my_constructor)
1414
static void my_constructor(void) {
1515
...
1616
}
1717
1818
*/
1919

20-
#ifndef G_CONSTRUCTOR_H_
21-
#define G_CONSTRUCTOR_H_
20+
#ifndef G2_CONSTRUCTOR_H_
21+
#define G2_CONSTRUCTOR_H_
22+
23+
#ifndef G2_MODEL_PREFIX
24+
#ifdef MODEL_IDENTIFIER
25+
#define G2_MODEL_PREFIX MODEL_IDENTIFIER
26+
#else
27+
#define G2_MODEL_PREFIX
28+
#endif
29+
#endif
30+
31+
#define G2_CONCAT(a, b) a ## b
32+
#define G2_CONCAT_(a, b) G2_CONCAT(a, b)
33+
#define G2_FUNCNAME(name) G2_CONCAT_(G2_MODEL_PREFIX, name)
2234

2335
#if defined(__cplusplus)
2436

25-
#define G_HAS_CONSTRUCTORS 1
37+
#define G2_HAS_CONSTRUCTORS 1
2638

27-
#define G_DEFINE_CONSTRUCTOR(_func) \
39+
#define G2_DEFINE_CONSTRUCTOR(_func) \
2840
static void _func(void); \
2941
struct _func ## _wrapper_struct { _func ## _wrapper_struct() { _func(); } }; \
3042
static _func ## _wrapper_struct _func ## _wrapper;
3143

32-
#define G_DEFINE_DESTRUCTOR(_func) \
44+
#define G2_DEFINE_DESTRUCTOR(_func) \
3345
static void _func(void); \
3446
struct _func ## _wrapper_struct2 { ~_func ## _wrapper_struct2() { _func(); } }; \
3547
static _func ## _wrapper_struct2 _func ## _wrapper2;
3648

3749
#elif (defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))) || \
3850
defined(__clang__)
3951

40-
#define G_HAS_CONSTRUCTORS 1
52+
#define G2_HAS_CONSTRUCTORS 1
4153

42-
#define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
43-
#define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void);
54+
#define G2_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
55+
#define G2_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void);
4456

4557
#elif defined(_MSC_VER) && (_MSC_VER >= 1500)
4658
/* Visual Studio 2008 and later has _pragma */
4759

48-
#define G_HAS_CONSTRUCTORS 1
60+
#define G2_HAS_CONSTRUCTORS 1
4961

5062
#ifdef _WIN64
51-
#define G_MSVC_SYMBOL_PREFIX ""
63+
#define G2_MSVC_SYMBOL_PREFIX ""
5264
#else
53-
#define G_MSVC_SYMBOL_PREFIX "_"
65+
#define G2_MSVC_SYMBOL_PREFIX "_"
5466
#endif
5567

56-
#define G_DEFINE_CONSTRUCTOR(_func) G_MSVC_CTOR (_func, G_MSVC_SYMBOL_PREFIX)
57-
#define G_DEFINE_DESTRUCTOR(_func) G_MSVC_DTOR (_func, G_MSVC_SYMBOL_PREFIX)
68+
#define G2_DEFINE_CONSTRUCTOR(_func) G2_MSVC_CTOR(_func, G2_MSVC_SYMBOL_PREFIX)
69+
#define G2_DEFINE_DESTRUCTOR(_func) G2_MSVC_DTOR(_func, G2_MSVC_SYMBOL_PREFIX)
5870

59-
#define G_MSVC_CTOR(_func,_sym_prefix) \
71+
#define G2_MSVC_CTOR(_func, _sym_prefix) \
6072
static void _func(void); \
6173
extern int (* _array ## _func)(void); \
6274
int _func ## _wrapper(void) { _func(); return _array ## _func == NULL; } \
6375
__pragma(comment(linker,"/include:" _sym_prefix # _func "_wrapper")) \
6476
__pragma(section(".CRT$XCU",read)) \
6577
__declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _wrapper;
6678

67-
#define G_MSVC_DTOR(_func,_sym_prefix) \
79+
#define G2_MSVC_DTOR(_func, _sym_prefix) \
6880
static void _func(void); \
6981
extern int (* _array ## _func)(void); \
7082
int _func ## _constructor(void) { atexit (_func); return _array ## _func == NULL; } \
@@ -74,22 +86,22 @@
7486

7587
#elif defined(_MSC_VER) && (_MSC_VER >= 1400)
7688

77-
#define G_HAS_CONSTRUCTORS 1
89+
#define G2_HAS_CONSTRUCTORS 1
7890

7991
/* Pre Visual Studio 2008 must use #pragma section */
80-
#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
81-
#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
92+
#define G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
93+
#define G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
8294

83-
#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
95+
#define G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
8496
section(".CRT$XCU",read)
85-
#define G_DEFINE_CONSTRUCTOR(_func) \
97+
#define G2_DEFINE_CONSTRUCTOR(_func) \
8698
static void _func(void); \
8799
static int _func ## _wrapper(void) { _func(); return 0; } \
88100
__declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper;
89101

90-
#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
102+
#define G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
91103
section(".CRT$XCU",read)
92-
#define G_DEFINE_DESTRUCTOR(_func) \
104+
#define G2_DEFINE_DESTRUCTOR(_func) \
93105
static void _func(void); \
94106
static int _func ## _constructor(void) { atexit (_func); return 0; } \
95107
__declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
@@ -100,19 +112,19 @@
100112
* http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
101113
*/
102114

103-
#define G_HAS_CONSTRUCTORS 1
115+
#define G2_HAS_CONSTRUCTORS 1
104116

105-
#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
106-
#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
117+
#define G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
118+
#define G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
107119

108-
#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
120+
#define G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
109121
init(_func)
110-
#define G_DEFINE_CONSTRUCTOR(_func) \
122+
#define G2_DEFINE_CONSTRUCTOR(_func) \
111123
static void _func(void);
112124

113-
#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
125+
#define G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
114126
fini(_func)
115-
#define G_DEFINE_DESTRUCTOR(_func) \
127+
#define G2_DEFINE_DESTRUCTOR(_func) \
116128
static void _func(void);
117129

118130
#else

0 commit comments

Comments
 (0)