aboutsummaryrefslogtreecommitdiffstats
path: root/erts/test/utils/gccifier.c
blob: 2acd50a4795a694aa78455c6e101bc695104ef1a (plain) (blame)
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
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 2004-2009. All Rights Reserved.
 * 
 * The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved online at http://www.erlang.org/.
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * %CopyrightEnd%
 *
 */

/*
 * A compiler wrapper that translate (some) gcc command line arguments
 * to the Visual C++ compiler and (of course) the gcc compiler. It also
 * makes some changes in the command line arguments when debug compiling.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>


#if !defined(__WIN32__)
#define USE_EXEC
#include <unistd.h>
#endif


#ifdef __WIN32__
#define EOL "\r\n"
#else
#define EOL "\n"
#endif

#define ARGS_INCR 20

static char *prog;

typedef struct {
    char **vec;
    int no;
    int ix;
    int chars;
} args_t;

static void
enomem(void)
{
    fprintf(stderr, "%s: Out of memory%s", prog, EOL);
    exit(1);
}

static void
save_arg(args_t *args, char *arg1, ...)
{
    char *carg;
    va_list argp;

    va_start(argp, arg1);
    carg = arg1;
    while (carg) {
	if (args->no <= args->ix) {
	    args->vec = (char **) (args->no
				   ? realloc((void *) args->vec,
					     (sizeof(char *)
					      *(args->no + ARGS_INCR + 2)))
				   : malloc((sizeof(char *)
					     *(args->no + ARGS_INCR + 2))));
	    if (!args->vec)
		enomem();
	    args->no += ARGS_INCR;
	}
	if (carg == arg1) {
	  args->vec[args->ix++] = "\"";
	  args->chars++;
	}
	args->vec[args->ix++] = carg;
	args->chars += strlen(carg);
	carg = va_arg(argp, char *);
    }
    args->vec[args->ix++] = "\"";
    args->chars++;
    args->vec[args->ix++] = " ";
    args->chars++;
    va_end(argp);
}

static int
is_prefix(char *prfx, char **str)
{
    int i;
    for (i = 0; prfx[i] && (*str)[i]; i++) {
	if (prfx[i] != (*str)[i])
	    return 0;
    }
    if (!prfx[i]) {
	*str = &(*str)[i];
	return 1;
    }
    return 0;
}

static void
cpy(char **dst, char *src)
{
    int i;
    for (i = 0; src[i]; i++)
	(*dst)[i] = src[i];
    *dst = &(*dst)[i];
}

typedef enum {
    STDLIB_NONE,
    STDLIB_MD,
    STDLIB_ML,
    STDLIB_MT
} stdlib_t;

int
main(int argc, char *argv[])
{
    int res;
    int i;
    size_t cmd_len;
    char *cmd;
    char *cmd_end;
    char *cc = NULL;
    args_t args = {0};
    int is_debug = 0;
    int is_purify = 0;
    int is_quantify = 0;
    int is_purecov = 0;
#ifdef __WIN32__
    int is_shared = 0;
    stdlib_t stdlib = STDLIB_NONE;
    char *shared_flag = "";
    char *stdlib_flag = "";
    int have_link_args = 0;
    args_t link_args = {0};

#define CHECK_FIRST_LINK_ARG						\
	if (!have_link_args) {						\
	    save_arg(&link_args, "-link", NULL);			\
	    have_link_args = 1;						\
	}
#else /* #ifdef __WIN32__ */
#define CHECK_FIRST_LINK_ARG
#endif /* #ifdef __WIN32__ */

   prog = argv[0];


    for (i = 1; i < argc; i++) {
	char *arg = argv[i];
	if (is_prefix("-CC", &arg)) {
	    cc = arg;
	}
	else if (is_prefix("-O", &arg)) {
	    if (!is_debug)
		save_arg(&args, argv[i], NULL);
	}
	else if (strcmp("-DDEBUG", arg) == 0) {
	    save_arg(&args, arg, NULL);
#ifdef __WIN32__
	set_debug:
#endif
	    if (!is_debug) {
		int j;
		is_debug = 1;
#ifdef __WIN32__
		save_arg(&args, "-Z7", NULL);
		CHECK_FIRST_LINK_ARG;
		save_arg(&link_args, "-debug", NULL);
		save_arg(&link_args, "-pdb:none", NULL);
#endif
		for (j = 0; j < args.ix; j++) {
		    char *tmp_arg = args.vec[j];
		    if (is_prefix("-O", &tmp_arg))
			args.vec[j] = "";
		}
	    }
	}
	else if (strcmp("-DPURIFY", arg) == 0) {
	    save_arg(&args, arg, NULL);
	    is_purify = 1;
	}
	else if (strcmp("-DQUANTIFY", arg) == 0) {
	    save_arg(&args, arg, NULL);
	    is_quantify = 1;
	}
	else if (strcmp("-DPURECOV", arg) == 0) {
	    save_arg(&args, arg, NULL);
	    is_purecov = 1;
	}
#ifdef __WIN32__
	else if (strcmp("-g", arg) == 0) {
	    goto set_debug;
	}
	else if (strcmp("-MD", arg) == 0)
	    stdlib = STDLIB_MD;
	else if (strcmp("-MDd", arg) == 0) {
	    stdlib = STDLIB_MD;
	    goto set_debug;
	}
	else if (strcmp("-ML", arg) == 0)
	    stdlib = STDLIB_ML;
	else if (strcmp("-MLd", arg) == 0) {
	    stdlib = STDLIB_ML;
	    goto set_debug;
	}
	else if (strcmp("-MT", arg) == 0)
	    stdlib = STDLIB_MT;
	else if (strcmp("-MTd", arg) == 0) {
	    stdlib = STDLIB_MT;
	    goto set_debug;
	}
	else if (strcmp("-shared", arg) == 0 || strcmp("-LD", arg) == 0)
	    is_shared = 1;
	else if (strcmp("-LDd", arg) == 0) {
	    is_shared = 1;
	    goto set_debug;
	}
	else if (strcmp("-Wall", arg) == 0) {
	    save_arg(&args, "-W3", NULL);
	}
	else if (is_prefix("-L", &arg)) {
	    CHECK_FIRST_LINK_ARG;
	    save_arg(&link_args, "-libpath:", arg, NULL);
	}
#endif /* #ifdef __WIN32__ */
	else if (is_prefix("-l", &arg)) {
	    CHECK_FIRST_LINK_ARG;
	    if (is_debug && strcmp("ethread", arg) == 0)
		arg = "ethread.debug";
	    else if (is_purify && strcmp("ethread", arg) == 0)
		arg = "ethread.purify";
	    else if (is_quantify && strcmp("ethread", arg) == 0)
		arg = "ethread.quantify";
	    else if (is_purecov && strcmp("ethread", arg) == 0)
		arg = "ethread.purecov";
#ifdef __WIN32__
	    else if (strcmp("socket", arg) == 0)
		arg = "ws2_32";
	    save_arg(&link_args, arg, ".lib", NULL);
#else
	    save_arg(&args, "-l", arg, NULL);
#endif
	}
	else
	    save_arg(&args, argv[i], NULL);
    }

    if (!cc || !cc[0]) {
	fprintf(stderr, "%s: Missing compulsory -CC flag%s", prog, EOL);
	exit(1);
    }

    cmd_len = strlen(cc) + 1 + args.chars + 1;

#ifdef __WIN32__
    if (is_shared)
	shared_flag = is_debug ? "-LDd " : "-LD ";
    switch (stdlib) {
    case STDLIB_MD: stdlib_flag = is_debug ? "-MDd " : "-MD ";	break;
    case STDLIB_ML: stdlib_flag = is_debug ? "-MLd " : "-ML ";	break;
    case STDLIB_MT: stdlib_flag = is_debug ? "-MTd " : "-MT ";	break;
    case STDLIB_NONE:						break;
    }

    cmd_len += strlen(shared_flag) + strlen(stdlib_flag) + link_args.chars;
#endif

    cmd = (char *) malloc(sizeof(char) * cmd_len);

    if (!cmd)
	enomem();
    cmd_end = cmd;
    cpy(&cmd_end, cc);
    cpy(&cmd_end, " ");
#ifdef __WIN32__
    cpy(&cmd_end, stdlib_flag);
    cpy(&cmd_end, shared_flag);
#endif
    for (i = 0; i < args.ix; i++)
	cpy(&cmd_end, args.vec[i]);
#ifdef __WIN32__
    for (i = 0; i < link_args.ix; i++)
	cpy(&cmd_end, link_args.vec[i]);
#endif
    *cmd_end = '\0';

    printf("==> %s%s", cmd, EOL);
    fflush(stdout);

#ifdef USE_EXEC
    (void) execl("/bin/sh", "sh", "-c", cmd, (char *) NULL);
    perror(NULL);
    res = 1;
#else
    res = system(cmd);
#endif

    free((void *) args.vec);
#ifdef __WIN32__
    free((void *) link_args.vec);
#endif
    free((void *) cmd);

    if (res < 0)
	res = 1;
    return res;
}