-
Notifications
You must be signed in to change notification settings - Fork 3
/
wrapper-target-objc.c
169 lines (128 loc) · 4.43 KB
/
wrapper-target-objc.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
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
int main(int argc, char * argv[]) {
char * const compiler = getenv("XCPKG_COMPILER_OBJC");
if (compiler == NULL) {
fprintf(stderr, "XCPKG_COMPILER_OBJC environment variable is not set.\n");
return 1;
}
if (compiler[0] == '\0') {
fprintf(stderr, "XCPKG_COMPILER_OBJC environment variable value should be a non-empty string.\n");
return 2;
}
/////////////////////////////////////////////////////////////////
char * const baseArgs = getenv("XCPKG_COMPILER_ARGS");
if (baseArgs == NULL) {
fprintf(stderr, "XCPKG_COMPILER_ARGS environment variable is not set.\n");
return 5;
}
if (baseArgs[0] == '\0') {
fprintf(stderr, "XCPKG_COMPILER_ARGS 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];
if (createSharedLibrary == 0) {
const char * msle = getenv("XCPKG_CREATE_MOSTLY_STATICALLY_LINKED_EXECUTABLE");
if (msle != NULL && strcmp(msle, "1") == 0) {
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '/') {
int len = 0;
int dotIndex = -1;
for (int j = 0; ; j++) {
if (argv[i][j] == '\0') {
len = j;
break;
}
if (argv[i][j] == '.') {
dotIndex = j;
}
}
if (dotIndex > 0) {
if (len - dotIndex == 6) {
if (strcmp(&argv[i][dotIndex], ".dylib") == 0) {
argv[i][dotIndex + 1] = 'a' ;
argv[i][dotIndex + 2] = '\0';
struct stat st;
if (stat(argv[i], &st) != 0 || !S_ISREG(st.st_mode)) {
argv[i][dotIndex + 1] = 'd';
argv[i][dotIndex + 2] = 'y';
}
}
}
}
}
argv2[i] = argv[i];
}
} else {
for (int i = 1; i < argc; i++) {
argv2[i] = argv[i];
}
}
} else {
for (int i = 1; i < argc; i++) {
argv2[i] = argv[i];
}
}
/////////////////////////////////////////////////////////////////
argv2[0] = compiler;
/////////////////////////////////////////////////////////////////
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;
}