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 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
|
//===-- ldmd.cpp - Drop-in DMD replacement wrapper for LDC ----------------===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license, except for the
// command line handling code, which originated from DMD. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
//
// Wrapper allowing use of LDC as drop-in replacement for DMD.
//
// Most command-line options are passed through to LDC; some with different
// names or semantics need to be translated.
//
// DMD also reads switches from the DFLAGS enviroment variable, if present. This
// is contrary to what C compilers do, where CFLAGS is usually handled by the
// build system.
//
//===----------------------------------------------------------------------===//
#ifndef LDC_EXE_NAME
#error "Please define LDC_EXE_NAME to the name of the LDC executable to use."
#endif
#include "driver/args.h"
#include "driver/exe_path.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/SystemUtils.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <cerrno>
#include <climits>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <vector>
#if _WIN32
#include <windows.h>
#else
#include <sys/stat.h>
#endif
#ifdef HAVE_SC_ARG_MAX
#include <unistd.h>
#endif
namespace ls = llvm::sys;
// We reuse DMD's response file parsing routine for maximum compatibility - it
// handles quotes in a very peculiar way.
int response_expand(size_t *pargc, char ***pargv);
// in dmd/root/man.d
void browse(const char *url);
/**
* Prints a formatted error message to stderr and exits the program.
*/
void error(const char *fmt, ...) {
va_list argp;
va_start(argp, fmt);
fprintf(stderr, "Error: ");
vfprintf(stderr, fmt, argp);
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
va_end(argp);
}
/**
* Prints a formatted warning message to stderr.
*/
void warning(const char *fmt, ...) {
va_list argp;
va_start(argp, fmt);
fprintf(stderr, "Warning: ");
vfprintf(stderr, fmt, argp);
fprintf(stderr, "\n");
va_end(argp);
}
char *concat(const char *a, const char *b) {
size_t na = strlen(a);
size_t nb = strlen(b);
char *result = static_cast<char *>(malloc(na + nb + 1));
assert(result);
memcpy(result, a, na);
memcpy(result + na, b, nb + 1);
return result;
}
char *concat(const char *a, int b) {
char bStr[14];
snprintf(bStr, sizeof(bStr), "%d", b);
return concat(a, bStr);
}
template <int N>
bool startsWith(const char *str, const char (&prefix)[N]) {
// N includes terminating null
return strncmp(str, prefix, N - 1) == 0;
}
/**
* Runs the given executable, returning its error code.
*/
int execute(std::vector<const char *> fullArgs) {
std::string errorMsg;
const char *executable = fullArgs[0];
const int rc =
args::executeAndWait(std::move(fullArgs), llvm::sys::WEM_UTF8, &errorMsg);
if (rc && !errorMsg.empty()) {
error("Error executing %s: %s", executable, errorMsg.c_str());
}
return rc;
}
/**
* Prints usage information to stdout.
*/
void printUsage(const char *argv0, const std::string &ldcPath) {
// Print version information by actually invoking ldc -version.
execute({ldcPath.c_str(), "-version"});
printf(
"\n\
Usage:\n\
%s [<option>...] <file>...\n\
%s [<option>...] -run <file> [<arg>...]\n\
\n\
Where:\n\
<file> D source file\n\
<arg> Argument to pass when running the resulting program\n\
\n\
<option>:\n\
@<cmdfile> read arguments from cmdfile\n\
-allinst generate code for all template instantiations\n\
-betterC omit generating some runtime information and helper functions\n\
-boundscheck=[on|safeonly|off]\n\
bounds checks on, in @safe only, or off\n\
-c compile only, do not link\n\
-check=[assert|bounds|in|invariant|out|switch][=[on|off]]\n\
enable or disable specific checks\n"
#if 0
" -check=[h|help|?] list information on all available checks\n"
#endif
" -checkaction=[D|C|halt|context]\n\
behavior on assert/boundscheck/finalswitch failure\n"
#if 0
" -checkaction=[h|help|?]\n\
list information on all available check actions\n"
#endif
" -color turn colored console output on\n\
-color=[on|off|auto]\n\
force colored console output on or off, or only when not redirected (default)\n\
-conf=<filename> use config file at filename\n\
-cov do code coverage analysis\n\
-cov=ctfe include code executed during CTFE in coverage report\n\
-cov=<nnn> require at least nnn%% code coverage\n\
-D generate documentation\n\
-Dd<directory> write documentation file to directory\n\
-Df<filename> write documentation file to filename\n\
-d silently allow deprecated features and symbols\n\
-de issue an error when deprecated features or symbols are used (halt compilation)\n\
-dw issue a message when deprecated features or symbols are used (default)\n\
-debug compile in debug code\n\
-debug=<level> compile in debug code <= level\n\
-debug=<ident> compile in debug code identified by ident\n\
-debuglib=<name> set symbolic debug library to name\n\
-defaultlib=<name>\n\
set default library to name\n\
-deps print module dependencies (imports/file/version/debug/lib)\n\
-deps=<filename> write module dependencies to filename (only imports)\n\
-extern-std=<standard>\n\
set C++ name mangling compatibility with <standard>\n"
#if 0
" -extern-std=[h|help|?]\n\
list all supported standards\n"
#endif
" -fPIC generate position independent code\n\
-g add symbolic debug info\n\
-gf emit debug info for all referenced types\n\
-gs always emit stack frame\n"
#if 0
" -gx add stack stomp code\n"
#endif
" -H generate 'header' file\n\
-Hd=<directory> write 'header' file to directory\n\
-Hf=<filename> write 'header' file to filename\n\
-HC[=[silent|verbose]]\n\
generate C++ 'header' file\n\
-HCd=<directory> write C++ 'header' file to directory\n\
-HCf=<filename> write C++ 'header' file to filename\n\
--help print help and exit\n\
-I=<directory> look for imports also in directory\n\
-i[=<pattern>] include imported modules in the compilation\n\
-ignore ignore unsupported pragmas\n\
-inline do function inlining\n\
-J=<directory> look for string imports also in directory\n\
-L=<linkerflag> pass linkerflag to link\n\
-lib generate library rather than object files\n\
-lowmem enable garbage collection for the compiler\n\
-m32 generate 32 bit code\n"
#if 0
" -m32mscoff generate 32 bit code and write MS-COFF object files\n"
#endif
" -m64 generate 64 bit code\n\
-main add default main() (e.g. for unittesting)\n\
-man open web browser on manual page\n"
#if 0
" -map generate linker .map file\n"
#endif
" -mcpu=<id> generate instructions for architecture identified by 'id'\n\
-mcpu=[h|help|?] list all architecture options\n\
-mixin=<filename> expand and save mixins to file specified by <filename>\n\
-mscrtlib=<libname>\n\
MS C runtime library to reference from main/WinMain/DllMain\n\
-mv=<package.module>=<filespec>\n\
use <filespec> as source file for <package.module>\n\
-noboundscheck no array bounds checking (deprecated, use -boundscheck=off)\n\
-O optimize\n\
-o- do not write object file\n\
-od=<directory> write object & library files to directory\n\
-of=<filename> name output file to filename\n\
-op preserve source path for output files\n\
-preview=<name> enable an upcoming language change identified by 'name'\n\
-preview=[h|help|?]\n\
list all upcoming language changes\n\
-profile profile runtime performance of generated code\n"
#if 0
" -profile=gc profile runtime allocations\n"
#endif
" -release compile release version\n\
-revert=<name> revert language change identified by 'name'\n\
-revert=[h|help|?]\n\
list all revertable language changes\n\
-run <srcfile> compile, link, and run the program srcfile\n\
-shared generate shared library (DLL)\n\
-transition=<name>\n\
help with language change identified by 'name'\n\
-transition=[h|help|?]\n\
list all language changes\n\
-unittest compile in unit tests\n\
-v verbose\n\
-vcolumns print character (column) numbers in diagnostics\n\
-vdmd print the underlying LDC command line\n\
-verror-style=[digitalmars|gnu]\n\
set the style for file/line number annotations on compiler messages\n\
-verrors=<num> limit the number of error messages (0 means unlimited)\n\
-verrors=context show error messages with the context of the erroring source line\n\
-verrors=spec show errors from speculative compiles such as __traits(compiles,...)\n\
--version print compiler version and exit\n\
-version=<level> compile in version code >= level\n\
-version=<ident> compile in version code identified by ident\n\
-vgc list all gc allocations including hidden ones\n\
-vtemplates=[list-instances]\n\
list statistics on template instantiations\n\
-vtls list all variables going into thread local storage\n\
-w warnings as errors (compilation will halt)\n\
-wi warnings as messages (compilation will continue)\n\
-X generate JSON file\n\
-Xf=<filename> write JSON file to filename\n\
-Xcc=<driverflag> pass driverflag to linker driver (cc)\n",
argv0, argv0);
}
/**
* Parses an enviroment variable for flags and appends them to given list of
* arguments.
*
* This corresponds to getenv_setargv() in DMD, but we need to duplicate it
* here since it is defined in mars.c.
*/
void appendEnvVar(const char *envVarName, std::vector<char *> &args) {
std::string envVar = env::get(envVarName);
if (envVar.empty()) {
return;
}
char *env = strdup(envVar.c_str()); // create forever-living copy
size_t j = 1; // leave argv[0] alone
while (1) {
switch (*env) {
case ' ':
case '\t':
env++;
break;
case 0:
return;
default:
args.push_back(env); // append
j++;
char *p = env;
int slash = 0;
int instring = 0;
char c = 0;
while (1) {
c = *env++;
switch (c) {
case '"':
p -= (slash >> 1);
if (slash & 1) {
p--;
goto Laddc;
}
instring ^= 1;
slash = 0;
continue;
case ' ':
case '\t':
if (instring) {
goto Laddc;
}
*p = 0;
break;
case '\\':
slash++;
*p++ = c;
continue;
case 0:
*p = 0;
return;
default:
Laddc:
slash = 0;
*p++ = c;
continue;
}
break;
}
}
}
}
/**
* Translates the LDMD command-line args (incl. DFLAGS environment variable)
* to LDC args.
* `ldcArgs` needs to be initialized with the path to the LDC executable.
*/
void translateArgs(const llvm::SmallVectorImpl<const char *> &ldmdArgs,
std::vector<const char *> &ldcArgs) {
// Expand any response files present into the list of arguments.
size_t argc = ldmdArgs.size();
char **argv = const_cast<char **>(ldmdArgs.data());
if (response_expand(&argc, &argv)) {
error("Could not read response file.");
}
std::vector<const char *> args(argv, argv + argc);
std::vector<char *> dflags;
appendEnvVar("DFLAGS", dflags);
if (!dflags.empty()) {
// append, but before a first potential '-run'
size_t runIndex = 0;
for (size_t i = 1; i < args.size(); ++i) {
if (strcmp(args[i], "-run") == 0) {
runIndex = i;
break;
}
}
args.insert(runIndex == 0 ? args.end() : args.begin() + runIndex,
dflags.begin(), dflags.end());
}
assert(ldcArgs.size() == 1);
const std::string ldcPath = ldcArgs[0];
ldcArgs.push_back("-ldmd");
bool vdmd = false;
bool pic = false; // -fPIC already encountered?
for (size_t i = 1; i < args.size(); i++) {
const char *p = args[i];
if (*p == '-') {
if (strcmp(p + 1, "vdmd") == 0) {
vdmd = true;
}
/* Most args are handled directly by LDC.
* Order corresponds to parsing order in dmd's mars.d.
*
* -allinst
* -de
* -d
* -dw
* -c
*/
else if (startsWith(p + 1, "check=")) {
// Parse:
// -check=[assert|bounds|in|invariant|out|switch][=[on|off]]
const char *arg = p + 7;
const auto argLength = strlen(arg);
bool enabled = false;
size_t kindLength = 0;
if (argLength > 3 && memcmp(arg + argLength - 3, "=on", 3) == 0) {
enabled = true;
kindLength = argLength - 3;
} else if (argLength > 4 &&
memcmp(arg + argLength - 4, "=off", 4) == 0) {
enabled = false;
kindLength = argLength - 4;
} else {
enabled = true;
kindLength = argLength;
}
const auto check = [&](size_t dmdLength, const char *dmd,
const char *ldc) {
if (kindLength == dmdLength && memcmp(arg, dmd, dmdLength) == 0) {
ldcArgs.push_back(concat(enabled ? "-enable-" : "-disable-", ldc));
return true;
}
return false;
};
if (kindLength == 6 && memcmp(arg, "bounds", 6) == 0) {
ldcArgs.push_back(enabled ? "-boundscheck=on" : "-boundscheck=off");
} else if (!(check(6, "assert", "asserts") ||
check(2, "in", "preconditions") ||
check(9, "invariant", "invariants") ||
check(3, "out", "postconditions") ||
check(6, "switch", "switch-errors"))) {
goto Lerror;
}
}
/* -checkaction
*/
else if (startsWith(p + 1, "color")) {
// Parse:
// -color
// -color=auto|on|off
if (p[6] == '=') {
if (strcmp(p + 7, "on") == 0) {
ldcArgs.push_back("-enable-color");
} else if (strcmp(p + 7, "off") == 0) {
ldcArgs.push_back("-disable-color");
} else if (strcmp(p + 7, "auto") != 0) {
goto Lerror;
}
} else if (p[6]) {
goto Lerror;
} else {
ldcArgs.push_back("-enable-color");
}
}
/* -conf
* -cov
* -shared
*/
else if (strcmp(p + 1, "dylib") == 0) {
ldcArgs.push_back("-shared");
} else if (strcmp(p + 1, "fPIC") == 0) {
if (!pic) {
ldcArgs.push_back("-relocation-model=pic");
pic = true;
}
} else if (strcmp(p + 1, "fPIE") == 0) {
goto Lnot_in_ldc;
} else if (strcmp(p + 1, "map") == 0) {
goto Lnot_in_ldc;
} else if (strcmp(p + 1, "multiobj") == 0) {
goto Lnot_in_ldc;
}
/* -g
* -gc
*/
else if (strcmp(p + 1, "gf") == 0) {
ldcArgs.push_back("-g");
} else if (strcmp(p + 1, "gs") == 0) {
#if LDC_LLVM_VER >= 1100
ldcArgs.push_back("-frame-pointer=all");
#else
ldcArgs.push_back("-disable-fp-elim");
#endif
} else if (strcmp(p + 1, "gx") == 0) {
goto Lnot_in_ldc;
} else if (strcmp(p + 1, "gt") == 0) {
error("use -profile instead of -gt\n");
}
/* -m32
* -m64
*/
else if (strcmp(p + 1, "m32mscoff") == 0) {
ldcArgs.push_back("-m32");
}
/* -mixin
* -mscrtlib
*/
else if (startsWith(p + 1, "profile")) {
if (p[8] == 0) {
ldcArgs.push_back("-fdmd-trace-functions");
} else if (strcmp(p + 8, "=gc") == 0) {
goto Lnot_in_ldc; // ldcArgs.push_back("-fdmd-trace-gc");
} else {
goto Lerror;
}
}
/* -v
*/
else if (strcmp(p + 1, "vtls") == 0) {
ldcArgs.push_back("-transition=tls");
}
/* -vtemplates
* -vcolumns
* -vgc
*/
else if (startsWith(p + 1, "verrors")) {
if (p[8] == '=' && isdigit(static_cast<unsigned char>(p[9]))) {
ldcArgs.push_back(p);
} else if (startsWith(p + 9, "spec")) {
ldcArgs.push_back("-verrors-spec");
} else if (startsWith(p + 9, "context")) {
ldcArgs.push_back("-verrors-context");
} else {
goto Lerror;
}
}
/* -verror-style
*/
else if (startsWith(p + 1, "mcpu=")) {
const char *c = p + 6;
if (strcmp(c, "?") == 0 || strcmp(c, "h") == 0 ||
strcmp(c, "help") == 0) {
execute({ldcPath.c_str(), "-mcpu=help"});
exit(EXIT_SUCCESS);
} else if (strcmp(c, "baseline") == 0) {
// ignore
} else if (strcmp(c, "avx") == 0) {
ldcArgs.push_back("-mattr=+avx");
} else if (strcmp(c, "avx2") == 0) {
ldcArgs.push_back("-mattr=+avx2");
} else if (strcmp(c, "native") == 0) {
ldcArgs.push_back(p);
} else {
goto Lerror;
}
}
/* -extern-std
* -transition
* -preview
* -revert
* -w
* -wi
* -O
* -o-
* -od
* -of
* -op
*/
else if (strcmp(p + 1, "o") == 0) {
error("-o no longer supported, use -of or -od");
}
/* -D
* -Dd
* -Df
* -HC
* -HCd
* -HCf
* -H
* -Hd
* -Hf
* -X
* -Xf
* -ignore
* -property
*/
else if (strcmp(p + 1, "inline") == 0) {
ldcArgs.push_back("-enable-inlining");
ldcArgs.push_back("-Hkeep-all-bodies");
}
/* -dip25
* -dip1000
* -dip1008
*/
else if (strcmp(p + 1, "lib") == 0) {
ldcArgs.push_back(p);
// DMD seems to emit objects directly into the static lib being
// generated. No object files are created and therefore they never
// collide due to duplicate .d filenames (in different dirs).
// Approximate that behavior by naming the object files uniquely via -oq
// and instructing LDC to remove the object files on success.
ldcArgs.push_back("-oq");
ldcArgs.push_back("-cleanup-obj");
} else if (strcmp(p + 1, "nofloat") == 0) {
goto Lnot_in_ldc;
} else if (strcmp(p + 1, "quiet") == 0) {
// ignore
}
/* -release
* -betterC
*/
else if (strcmp(p + 1, "noboundscheck") == 0) {
ldcArgs.push_back("-boundscheck=off");
}
/* -boundscheck
* -unittest
* -I
* -J
*/
else if (startsWith(p + 1, "debug") && p[6] != 'l') {
// Parse:
// -debug
// -debug=number
// -debug=identifier
if (p[6] == '=') {
if (isdigit(static_cast<unsigned char>(p[7]))) {
long level;
errno = 0;
char *end;
level = strtol(p + 7, &end, 10);
if (*end || errno || level > INT_MAX) {
goto Lerror;
}
ldcArgs.push_back(concat("-d-debug=", static_cast<int>(level)));
} else {
ldcArgs.push_back(concat("-d-debug=", p + 7));
}
} else if (p[6]) {
goto Lerror;
} else {
ldcArgs.push_back("-d-debug");
}
} else if (startsWith(p + 1, "version")) {
// Parse:
// -version=number
// -version=identifier
if (p[8] == '=') {
if (isdigit(static_cast<unsigned char>(p[9]))) {
long level;
errno = 0;
char *end;
level = strtol(p + 9, &end, 10);
if (*end || errno || level > INT_MAX) {
goto Lerror;
}
ldcArgs.push_back(concat("-d-version=", static_cast<int>(level)));
} else {
ldcArgs.push_back(concat("-d-version=", p + 9));
}
} else {
goto Lerror;
}
} else if (strcmp(p + 1, "-b") == 0 || strcmp(p + 1, "-c") == 0 ||
strcmp(p + 1, "-f") == 0 || strcmp(p + 1, "-r") == 0 ||
strcmp(p + 1, "-x") == 0 || strcmp(p + 1, "-y") == 0) {
ldcArgs.push_back(concat("-hidden-debug-", p + 2));
} else if (strcmp(p + 1, "-help") == 0 || strcmp(p + 1, "h") == 0) {
printUsage(ldmdArgs[0], ldcPath);
exit(EXIT_SUCCESS);
} else if (strcmp(p + 1, "-version") == 0) {
// Print version information by actually invoking ldc -version.
execute({ldcPath.c_str(), "-version"});
exit(EXIT_SUCCESS);
}
/* -L
* -defaultlib
* -debuglib
* -deps
* -main
*/
else if (startsWith(p + 1, "man")) {
browse("http://wiki.dlang.org/LDC");
exit(EXIT_SUCCESS);
} else if (strcmp(p + 1, "run") == 0) {
ldcArgs.insert(ldcArgs.end(), args.begin() + i, args.end());
break;
} else if (p[1] == '\0') {
ldcArgs.push_back("-");
} else if (p[1] == 'C') {
ldcArgs.push_back(concat("-", p + 2));
} else {
Lerror:
ldcArgs.push_back(p);
continue;
Lnot_in_ldc:
warning("command-line option '%s' not yet supported by LDC.", p);
continue;
}
} else {
const auto ext = ls::path::extension(p);
if (ext.equals_lower(".exe")) {
// should be for Windows targets only
ldcArgs.push_back(concat("-of=", p));
continue;
}
#ifdef _WIN32
else if (strcmp(p, "/?") == 0) {
printUsage(ldmdArgs[0], ldcPath);
exit(EXIT_SUCCESS);
}
#endif
ldcArgs.push_back(p);
}
}
if (vdmd) {
printf(" -- Invoking:");
for (const auto &arg : ldcArgs) {
printf(" %s", arg);
}
puts("");
}
}
/**
* Tries to locate an executable with the given name, or an invalid path if
* nothing was found. Search paths: 1. Directory where this binary resides.
* 2. System PATH.
*/
std::string locateBinary(std::string exeName) {
std::string path = exe_path::prependBinDir(exeName.c_str());
if (ls::fs::can_execute(path)) {
return path;
}
llvm::ErrorOr<std::string> res = ls::findProgramByName(exeName);
path = res ? res.get() : std::string();
if (ls::fs::can_execute(path)) {
return path;
}
return "";
}
static llvm::SmallVector<const char *, 32> ldmdArguments;
/// LDMD's entry point, C main.
#if LDC_WINDOWS_WMAIN
int wmain(int argc, const wchar_t **originalArgv)
#else
int main(int argc, const char **originalArgv)
#endif
{
// Initialize `ldmdArguments` with the UTF-8 command-line args.
args::getCommandLineArguments(argc, originalArgv, ldmdArguments);
// Move on to _d_run_main, _Dmain, and finally cppmain below.
// Only pass the first arg to skip useless work, e.g., not applying --DRT-* to
// LDMD itself.
return args::forwardToDruntime(1, originalArgv);
}
int cppmain() {
exe_path::initialize(ldmdArguments[0]);
std::string ldcExeName = LDC_EXE_NAME;
#ifdef _WIN32
ldcExeName += ".exe";
#endif
const std::string ldcPath = locateBinary(ldcExeName);
if (ldcPath.empty()) {
error("Could not locate " LDC_EXE_NAME " executable.");
}
if (ldmdArguments.size() == 1) {
printUsage(ldmdArguments[0], ldcPath);
exit(EXIT_FAILURE);
}
std::vector<const char *> fullArgs;
fullArgs.push_back(ldcPath.c_str());
translateArgs(ldmdArguments, fullArgs);
return execute(std::move(fullArgs));
}
|