forked from leleliu008/xcpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper-native-c++.c
118 lines (86 loc) · 2.82 KB
/
wrapper-native-c++.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char * argv[]) {
char * const compiler = getenv("XCPKG_COMPILER_CXX");
if (compiler == NULL) {
fprintf(stderr, "XCPKG_COMPILER_CXX environment variable is not set.\n");
return 1;
}
if (compiler[0] == '\0') {
fprintf(stderr, "XCPKG_COMPILER_CXX environment variable value should be a non-empty string.\n");
return 2;
}
/////////////////////////////////////////////////////////////////
char * const baseArgs = getenv("XCPKG_COMPILER_ARGS_FOR_BUILD");
if (baseArgs == NULL) {
fprintf(stderr, "XCPKG_COMPILER_ARGS_FOR_BUILD environment variable is not set.\n");
return 5;
}
if (baseArgs[0] == '\0') {
fprintf(stderr, "XCPKG_COMPILER_ARGS_FOR_BUILD environment variable value should be a non-empty string.\n");
return 6;
}
/////////////////////////////////////////////////////////////////
size_t baseArgc = 1U;
for (size_t i = 0U; ; i++) {
if (baseArgs[i] == '\0') {
break;
}
if (baseArgs[i] == ' ') {
baseArgc++;
}
}
/////////////////////////////////////////////////////////////////
int createSharedLibrary = 0;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-dynamiclib") == 0 || strcmp(argv[i], "-shared") == 0) {
createSharedLibrary = 1;
break;
}
}
/////////////////////////////////////////////////////////////////
char* argv2[argc + baseArgc + 2];
argv2[0] = compiler;
for (int i = 1; i < argc; i++) {
argv2[i] = argv[i];
}
/////////////////////////////////////////////////////////////////
char * p = baseArgs;
for (size_t i = 0U; ; i++) {
if (baseArgs[i] == '\0') {
if (p[0] != '\0') {
argv2[argc++] = p;
}
break;
}
if (baseArgs[i] == ' ') {
baseArgs[i] = '\0';
if (p[0] != '\0') {
argv2[argc++] = p;
}
p = &baseArgs[i + 1];
}
}
/////////////////////////////////////////////////////////////////
if (createSharedLibrary == 1) {
argv2[argc++] = (char*)"-fPIC";
}
argv2[argc++] = NULL;
/////////////////////////////////////////////////////////////////
const char * verbose = getenv("XCPKG_VERBOSE");
if (verbose != NULL && strcmp(verbose, "1") == 0) {
for (int i = 0; ;i++) {
if (argv2[i] == NULL) {
break;
} else {
fprintf(stderr, "%s\n", argv2[i]);
}
}
}
/////////////////////////////////////////////////////////////////
execv (compiler, argv2);
perror(compiler);
return 255;
}