This repository was archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
641 lines (574 loc) · 19.7 KB
/
Copy pathmain.cpp
File metadata and controls
641 lines (574 loc) · 19.7 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/**
* The MIT License (MIT)
*
* Copyright (C) 2017, djcj <djcj@gmx.de>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#define USAGE "\n" \
"This program is a wrapper for msvc's cl.exe and intended to be used\n" \
"with Windows 10's \"Bash on Ubuntu on Windows\" shell.\n" \
"It is invoked with gcc options (only a limited number) and turns\n" \
"them into msvc options to call cl.exe.\n" \
"The msvc options may not exactly do the same as their gcc counterparts.\n" \
"\n" \
"Supported GCC options (see `man gcc' for more information):\n" \
" -c -C -DDEFINE[=ARG] -fconstexpr-depth=num -ffp-contract=fast|off\n" \
" -finline-functions -fno-inline -frtti -fthreadsafe-statics\n" \
" -fomit-frame-pointer -fopenmp -fpermissive -fsized-deallocation -fstack-check\n" \
" -fstack-protector -funsigned-char -fwhole-program -g -include file -I path\n" \
" -llibname -L path -m32 -mavx -mavx2 -mdll -msse -msse2 -nodefaultlibs -nostdinc\n" \
" -nostdinc++ -nostdlib -O0 -O1 -O2 -O3 -Os -o file -print-search-dirs -shared\n" \
" -std=c<..>|gnu<..> -trigraphs -UDEFINE -w -Wall -Werror -Wextra\n" \
" -Wl,--out-implib,libname -Wl,-output-def,defname -Wl,--whole-archive -x <c|c++>\n" \
"\n" \
"Other options:\n" \
" --help display this information\n" \
" --help-cl display cl.exe's help information\n" \
" --help-link display link.exe's help information\n" \
" --version display version information of cl.exe and link.exe\n" \
" --verbose print commands\n" \
" --print-only print commands and don't to anything\n" \
" --path=path semicolon (;) separated list of win32 paths to run cl.exe\n" \
" -Wcl,arg -Wlink,arg parse msvc options directly to cl.exe/link.exe;\n" \
" see also https://msdn.microsoft.com/en-us/library/19z1t1wy.aspx\n" \
"\n" \
"Environment variables:\n" \
" CL_PATH semicolon (;) separated list of paths to run cl.exe\n" \
" INCLUDE semicolon (;) separated list of include paths\n" \
" LIB semicolon (;) separated list of library search paths\n"
#include <iostream>
#include <string>
#include <ctype.h>
#include <errno.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "config.h"
#define STR(x) std::string(x)
bool begins(const char *p, const char *str);
std::string win_path(char *ch);
void split_env(const char *env_var, std::string msvc_arg, std::string &str);
void print_help(char *self);
extern "C" {
int system_return(const char *command);
}
/* check if the beginning of p equals str and if p is longer than str */
bool begins(const char *p, const char *str)
{
size_t n = strlen(str);
if (strncmp(p, str, n) == 0 && strlen(p) > n)
{
return true;
}
return false;
}
/* C: is mounted as "/mnt/c", D: as "/mnt/d", and so on;
* forward slashes (/) are not converted to backslashes (\)
* because Windows actually supports them */
std::string win_path(char *ch)
{
std::string str;
if (ch[0] == '/')
{
bool prepend = true;
if (begins(ch, "/mnt/"))
{
if (strchr("cdefghijklmnopqrstuvwxyzab", ch[5]) != NULL)
{
char *drive = new char[2];
sprintf(drive, "%c", toupper(ch[5]));
if (strlen(ch) == 6)
{
/* /mnt/d -> D:/ */
prepend = false;
str = STR(drive) + ":/";
}
else if (ch[6] == '/')
{
/* /mnt/d/ -> D:/
* /mnt/d/dir -> D:/dir */
prepend = false;
str = STR(drive) + ":/" + STR(ch+7);
}
}
}
if (prepend)
{
/* /usr/include -> ./usr/include */
str = "." + STR(ch);
}
}
else
{
str = STR(ch);
}
return str;
}
void split_env(const char *env_var, std::string msvc_arg, std::string &str)
{
char *env = getenv(env_var);
if (env != NULL)
{
char *token = strtok(env, ";");
while (token != NULL)
{
str += " /" + msvc_arg + "\"" + win_path(token) + "\"";
token = strtok(NULL, ";");
}
}
}
void print_help(char *self)
{
std::cout << "Usage: " << self << " [options] file...\n" << USAGE << std::endl;
}
int main(int argc, char **argv)
{
std::string str, cmd, lnk, driver_paths, run_exe;
std::string driver_default = DEFAULT_CL_PATH_X64;
std::string includes_default = DEFAULT_INCLUDES;
std::string lib_paths_default = DEFAULT_LIBPATHS_X64;
int bits = 64;
bool verbose = false;
bool print_only = false;
bool have_outname = false;
bool print_search_dirs = false;
bool print_help_cl = false;
bool print_help_link = false;
bool print_version = false;
bool do_link = true;
bool use_default_driver = true;
bool use_default_inc_paths = true;
bool default_lib_paths = true;
bool dll = false;
bool link_static = false;
char *driver_env = getenv("CL_PATH");
if (driver_env != NULL)
{
driver_paths = driver_env;
use_default_driver = false;
}
/* parse arguments */
for (int i = 1; i < argc; ++i)
{
size_t len = strlen(argv[i]);
char *arg = argv[i];
str = STR(argv[i]);
if (arg[0] == '-')
{
if (arg[1] == '-')
{
if (begins(arg, "--path=")) { driver_paths = arg+7;
use_default_driver = false; }
else if (str == "--verbose") { verbose = true; }
else if (str == "--print-only") { verbose = print_only = true; }
else if (str == "--help") { print_help(argv[0]); return 0; }
else if (begins(arg, "--help-"))
{
if (str == "--help-cl") { print_help_cl = true; }
else if (str == "--help-link") { print_help_link = true; }
}
else if (str == "--version") { print_version = true; }
}
else
{
if ((arg[1] == '?' || arg[1] == 'h') && (len == 2 || str == "-help"))
{
print_help(argv[0]);
return 0;
}
/* -c -C -w */
else if (str == "-c" || str == "-C" || str == "-w")
{
cmd += " /" + std::string(arg+1);
if (str == "-c") {
do_link = false;
}
}
/* -g */
else if (str == "-g")
{
cmd += " /Zi";
}
/* -x c -x c++ */
else if (arg[1] == 'x')
{
if (len == 2) {
++i;
if (i < argc) {
if (STR(argv[i]) == "c") { cmd += " /TC"; }
else if (STR(argv[i]) == "c++") { cmd += " /TP"; }
}
}
else if (str == "-xc") { cmd += " /TC"; }
else if (str == "-xc++") { cmd += " /TP"; }
}
/* -o file */
else if (arg[1] == 'o')
{
if (len == 2) {
++i;
if (i < argc) {
lnk += " /out:\"" + STR(argv[i]) + "\"";
}
} else {
lnk += " /out:\"" + STR(arg+2) + "\"";
}
have_outname = true;
}
/* -I path */
else if (arg[1] == 'I')
{
if (len == 2) {
++i;
if (i < argc) {
cmd += " /I\"" + win_path(argv[i]) + "\"";
}
} else {
cmd += " /I\"" + win_path(arg+2) + "\"";
}
}
/* -DDEFINE[=ARG] -UDEFINE */
else if (arg[1] == 'D' || arg[1] == 'U')
{
if (len == 2) {
++i;
if (i < argc) {
cmd += " /" + str.substr(1,1) + "\"" + STR(argv[i]) + "\"";
}
} else {
cmd += " /" + str.substr(1,1) + "\"" + STR(arg+2) + "\"";
}
}
/* -L path */
else if (arg[1] == 'L')
{
if (len == 2) {
++i;
if (i < argc) {
lnk += " /libpath:\"" + win_path(argv[i]) + "\"";
}
} else {
lnk += " /libpath:\"" + win_path(arg+2) + "\"";
}
}
/* -llibname */
else if (arg[1] == 'l' && len > 2)
{
if (str != "-lc" &&
str != "-lm" &&
str != "-lrt" && /* always ignore these libraries */
str != "-lstdc++" &&
str != "-lgcc_s" &&
str != "-lmingw32" && /* TODO: maybe add an option */
str != "-lmingwex" && /* to disable the blacklisting */
str != "-lmingwthrd" &&
str != "-lmoldname" &&
str != "-lpthread") { lnk += " \"" + STR(arg+2) + ".lib\""; }
}
/* -O0 -O1 -O2 -O3 -Os */
else if (arg[1] == 'O' && len == 3)
{
if (arg[2] == '1' ||
arg[2] == '2') { cmd += " /O2 /Ot"; }
else if (arg[2] == '3') { cmd += " /Ox"; }
else if (arg[2] == 's') { cmd += " /O1 /Os"; }
else if (arg[2] == '0') { cmd += " /Od"; }
}
/* -Wl,--whole-archive
* -Wl,--out-implib,libname
* -Wl,-output-def,defname
* -Wall -Wextra -Werror
* -Wcl,arg -Wlink,arg */
else if (arg[1] == 'W' && len > 2)
{
if (arg[2] == 'l')
{
std::string lopt;
if (len == 3) {
++i;
if (i < argc) {
lopt = STR(argv[i]);
}
} else {
lopt = STR(arg+4);
}
if (lopt == "--whole-archive")
{
lnk += " /wholearchive";
}
else if (begins(lopt.c_str(), "--out-implib,"))
{
lnk += " /implib:\"" + STR(arg+17) + "\"";
}
else if (str == "-Wl,--out-implib")
{
++i;
if (i < argc && begins(argv[i], "-Wl,")) {
lnk += " /implib:\"" + STR(argv[i]+4) + "\"";
}
}
else if (begins(lopt.c_str(), "-output-def,"))
{
lnk += " /def:\"" + STR(arg+16) + "\"";
}
else if (str == "-Wl,-output-def")
{
++i;
if (i < argc && begins(argv[i], "-Wl,")) {
lnk += " /def:\"" + STR(argv[i]+4) + "\"";
}
}
else if (str == "-Wlink" || begins(arg, "-Wlink,"))
{
std::string s;
if (str == "-Wlink" && i+1 < argc)
{
++i;
s = STR(argv[i]);
}
else
{
s = STR(arg+7);
}
if (!s.empty())
{
if (s[0] == '-')
{
lnk += " /" + s.substr(1);
}
else if (s[0] != '/')
{
lnk += " /" + s;
}
else
{
lnk += " " + s;
}
}
}
}
else if (str == "-Wcl" || begins(arg, "-Wcl,"))
{
std::string s;
if (str == "-Wcl" && i+1 < argc)
{
++i;
s = STR(argv[i]);
}
else
{
s = STR(arg+5);
}
if (!s.empty())
{
if (s[0] == '-')
{
cmd += " /" + s.substr(1);
}
else if (s[0] != '/')
{
cmd += " /" + s;
}
else
{
cmd += " " + s;
}
}
}
else if (str == "-Wall") { cmd += " /W3"; }
else if (str == "-Wextra") { cmd += " /Wall"; }
else if (str == "-Werror") { cmd += " /WX"; }
}
/* -mdll -msse -msse2 -mavx -mavx2 */
else if (arg[1] == 'm' && len > 2)
{
if (str == "-m32") { bits = 32; }
else if (str == "-m64") { bits = 64; }
else if (str == "-mdll") { cmd += " /LD"; dll = true; }
else if (str == "-msse") { cmd += " /arch:SSE"; }
else if (str == "-msse2") { cmd += " /arch:SSE2"; }
else if (str == "-mavx") { cmd += " /arch:AVX"; }
else if (str == "-mavx2") { cmd += " /arch:AVX2"; }
}
/* -frtti -fthreadsafe-statics -fno-inline -fomit-frame-pointer
* -fpermissive -finline-functions -fopenmp -fstack-protector -fstack-check
* -fstack-protector-strong -fstack-protector-all
* -funsigned-char -fsized-deallocation -fconstexpr-depth=num
* -ffp-contract=fast|off -fwhole-program */
else if (arg[1] == 'f' && len > 2)
{
if (begins(arg, "-fno-"))
{
if (str == "-fno-rtti") { cmd += " /GR-"; }
else if (str == "-fno-threadsafe-statics") { cmd += " /Zc:threadSafeInit-"; }
else if (str == "-fno-inline") { cmd += " /Ob0"; }
else if (str == "-fno-stack-protector" ||
str == "-fno-stack-check") { cmd += " /GS- /guard:cf-"; }
else if (str == "-fno-sized-deallocation") { cmd += " /Zc:sizedDealloc-"; }
else if (str == "-fno-whole-program") { cmd += " /GL-"; }
}
else
{
if (str == "-fomit-frame-pointer") { cmd += " /Oy"; }
else if (str == "-fpermissive") { cmd += " /permissive"; }
else if (str == "-fstack-protector" ||
str == "-fstack-check") { cmd += " /GS"; }
else if (str == "-fstack-protector-strong" ||
str == "-fstack-protector-all") { cmd += " /GS /guard:cf"; }
else if (str == "-finline-functions") { cmd += " /Ob2"; }
else if (str == "-frtti") { cmd += " /GR"; }
else if (str == "-fthreadsafe-statics") { cmd += " /Zc:threadSafeInit"; }
else if (str == "-fopenmp") { cmd += " /openmp"; }
else if (str == "-funsigned-char") { cmd += " /J"; }
else if (str == "-fsized-deallocation") { cmd += " /Zc:sizedDealloc"; }
else if (begins(arg, "-fconstexpr-depth=")) { cmd += " /constexpr:depth" + STR(arg+18); }
else if (begins(arg, "-ffp-contract="))
{
if (STR(arg+14) == "fast") { cmd += " /fp:fast"; }
else if (STR(arg+14) == "off") { cmd += " /fp:strict"; }
}
else if (str == "-fwhole-program") { cmd += " /GL"; }
}
}
/* -nostdinc -nostdinc++ -nostdlib -nodefaultlibs */
else if (arg[1] == 'n' && len > 8)
{
if (str == "-nostdinc" ||
str == "-nostdinc++") { use_default_inc_paths = false; }
else if (str == "-nostdlib") { default_lib_paths = false; }
else if (str == "-nodefaultlibs") { lnk += " /nodefaultlib";
default_lib_paths = false; }
}
/* -shared -static -static-libgcc -static-libstdc++ -std=c<..>|gnu<..> */
else if (arg[1] == 's' && len > 5)
{
if (str == "-shared") { cmd += " /LD"; dll = true; }
else if (str == "-static" ||
str == "-static-libgcc" ||
str == "-static-libstdc++") { link_static = true; }
else if (begins(arg, "-std="))
{
if (begins(arg, "-std=gnu")) { cmd += " /std:c" + STR(arg+8); }
else { cmd += " /std:" + STR(arg+5); }
}
}
/* -include file */
else if (str == "-include")
{
++i;
if (i < argc) {
cmd += " /FI\"" + STR(argv[i]) + "\"";
}
}
/* -trigraphs */
else if (str == "-trigraphs")
{
cmd += " /Zc:trigraphs";
}
/* -print-search-dirs */
else if (str == "-print-search-dirs")
{
print_search_dirs = true;
}
}
}
else
{
cmd += " \"" + win_path(arg) + "\"";
}
}
if (bits == 32)
{
if (!use_default_driver)
{
std::cerr << "warning: ignoring `-m32' when using a custom cl.exe" << std::endl;
}
driver_default = DEFAULT_CL_PATH_X86;
lib_paths_default = DEFAULT_LIBPATHS_X86;
}
if (use_default_driver)
{
driver_paths = driver_default;
}
run_exe = "cmd.exe /C 'set PATH=" + driver_paths + ";%PATH% & ";
/* print information and exit */
if (print_help_cl)
{
/* piping to cat helps to display the
* output correctly and in one go */
cmd = run_exe + "cl.exe /help' 2>&1 | cat";
return system(cmd.c_str());
}
else if (print_help_link)
{
cmd = run_exe + "link.exe' 2>&1 | cat";
return system(cmd.c_str());
}
else if (print_version)
{
cmd = run_exe + "cl.exe' 2>&1 | head -n3 ; " + run_exe + "link.exe' 2>&1 | head -n3";
return system(cmd.c_str());
}
if (print_search_dirs)
{
std::cout << "cl.exe: " << driver_default << std::endl;
std::cout << "includes: " << includes_default << std::endl;
std::cout << "libraries: " << lib_paths_default << std::endl;
return 0;
}
/* turn lists obtained from environment variables INCLUDE and
* and LIB into command line arguments /I\"dir\" and /libpath\"dir\" */
split_env("INCLUDE", "I", cmd);
split_env("LIB", "libpath", cmd);
/* create the final command to execute */
if (use_default_inc_paths) { cmd += " " + includes_default; }
if (!link_static) { cmd += " /MD"; }
if (do_link)
{
if (!have_outname)
{
if (dll) { lnk += " /out:\"a.dll\""; }
else { lnk += " /out:\"a.exe\""; }
}
if (default_lib_paths) { lnk += " " + lib_paths_default; }
cmd += " /link" + lnk;
}
/* replace all occurences of single quotes (') with double quotes (");
* this is needed to wrap everything within singles quotes so we can
* parse it as a single command line argument to cmd.exe */
for (size_t i = 0; (i = cmd.find("'", i)) != std::string::npos; ++i)
{
cmd.replace(i, 1, "\"");
}
cmd = run_exe + "cl.exe" + cmd + "'";
if (verbose)
{
std::cout << cmd << std::endl;
}
if (print_only)
{
return 0;
}
return system_return(cmd.c_str());
}