24
24
#include " llvm/Config/config.h"
25
25
#include " llvm/Support/FileSystem.h"
26
26
#include " llvm/Support/Path.h"
27
+ #include " llvm/Support/Program.h"
27
28
#include " llvm/Support/WithColor.h"
28
29
#include " llvm/Support/raw_ostream.h"
29
30
#include " llvm/TargetParser/Triple.h"
@@ -326,7 +327,7 @@ int main(int argc, char **argv) {
326
327
// information.
327
328
std::string ActivePrefix, ActiveBinDir, ActiveIncludeDir, ActiveLibDir,
328
329
ActiveCMakeDir;
329
- std::string ActiveIncludeOption ;
330
+ std::vector<std:: string> ActiveIncludeOptions ;
330
331
if (IsInDevelopmentTree) {
331
332
ActiveIncludeDir = std::string (LLVM_SRC_ROOT) + " /include" ;
332
333
ActivePrefix = CurrentExecPrefix;
@@ -352,8 +353,8 @@ int main(int argc, char **argv) {
352
353
}
353
354
354
355
// We need to include files from both the source and object trees.
355
- ActiveIncludeOption =
356
- ( " -I " + ActiveIncludeDir + " " + " -I " + ActiveObjRoot + " /include" );
356
+ ActiveIncludeOptions. push_back (ActiveIncludeDir);
357
+ ActiveIncludeOptions. push_back ( ActiveObjRoot + " /include" );
357
358
} else {
358
359
ActivePrefix = CurrentExecPrefix;
359
360
{
@@ -372,7 +373,7 @@ int main(int argc, char **argv) {
372
373
sys::fs::make_absolute (ActivePrefix, Path);
373
374
ActiveCMakeDir = std::string (Path);
374
375
}
375
- ActiveIncludeOption = " -I " + ActiveIncludeDir;
376
+ ActiveIncludeOptions. push_back ( ActiveIncludeDir) ;
376
377
}
377
378
378
379
// / We only use `shared library` mode in cases where the static library form
@@ -401,8 +402,8 @@ int main(int argc, char **argv) {
401
402
std::replace (ActiveBinDir.begin (), ActiveBinDir.end (), ' /' , ' \\ ' );
402
403
std::replace (ActiveLibDir.begin (), ActiveLibDir.end (), ' /' , ' \\ ' );
403
404
std::replace (ActiveCMakeDir.begin (), ActiveCMakeDir.end (), ' /' , ' \\ ' );
404
- std::replace (ActiveIncludeOption. begin (), ActiveIncludeOption. end (), ' / ' ,
405
- ' \\ ' );
405
+ for ( auto &Include : ActiveIncludeOptions)
406
+ std::replace (Include. begin (), Include. end (), ' / ' , ' \\ ' );
406
407
}
407
408
SharedDir = ActiveBinDir;
408
409
StaticDir = ActiveLibDir;
@@ -504,6 +505,20 @@ int main(int argc, char **argv) {
504
505
};
505
506
506
507
raw_ostream &OS = outs ();
508
+
509
+ // Render include paths and associated flags
510
+ auto RenderFlags = [&](StringRef Flags) {
511
+ bool First = true ;
512
+ for (auto &Include : ActiveIncludeOptions) {
513
+ if (!First)
514
+ OS << ' ' ;
515
+ OS << " -I" ;
516
+ sys::printArg (OS, Include, /* Quote=*/ true );
517
+ First = false ;
518
+ }
519
+ OS << ' ' << Flags << ' \n ' ;
520
+ };
521
+
507
522
for (int i = 1 ; i != argc; ++i) {
508
523
StringRef Arg = argv[i];
509
524
@@ -512,24 +527,30 @@ int main(int argc, char **argv) {
512
527
if (Arg == " --version" ) {
513
528
OS << PACKAGE_VERSION << ' \n ' ;
514
529
} else if (Arg == " --prefix" ) {
515
- OS << ActivePrefix << ' \n ' ;
530
+ sys::printArg (OS, ActivePrefix, /* Quote=*/ true );
531
+ OS << ' \n ' ;
516
532
} else if (Arg == " --bindir" ) {
517
- OS << ActiveBinDir << ' \n ' ;
533
+ sys::printArg (OS, ActiveBinDir, /* Quote=*/ true );
534
+ OS << ' \n ' ;
518
535
} else if (Arg == " --includedir" ) {
519
- OS << ActiveIncludeDir << ' \n ' ;
536
+ sys::printArg (OS, ActiveIncludeDir, /* Quote=*/ true );
537
+ OS << ' \n ' ;
520
538
} else if (Arg == " --libdir" ) {
521
- OS << ActiveLibDir << ' \n ' ;
539
+ sys::printArg (OS, ActiveLibDir, /* Quote=*/ true );
540
+ OS << ' \n ' ;
522
541
} else if (Arg == " --cmakedir" ) {
523
- OS << ActiveCMakeDir << ' \n ' ;
542
+ sys::printArg (OS, ActiveCMakeDir, /* Quote=*/ true );
543
+ OS << ' \n ' ;
524
544
} else if (Arg == " --cppflags" ) {
525
- OS << ActiveIncludeOption << ' ' << LLVM_CPPFLAGS << ' \n ' ;
545
+ RenderFlags ( LLVM_CPPFLAGS) ;
526
546
} else if (Arg == " --cflags" ) {
527
- OS << ActiveIncludeOption << ' ' << LLVM_CFLAGS << ' \n ' ;
547
+ RenderFlags ( LLVM_CFLAGS) ;
528
548
} else if (Arg == " --cxxflags" ) {
529
- OS << ActiveIncludeOption << ' ' << LLVM_CXXFLAGS << ' \n ' ;
549
+ RenderFlags ( LLVM_CXXFLAGS) ;
530
550
} else if (Arg == " --ldflags" ) {
531
- OS << ((HostTriple.isWindowsMSVCEnvironment ()) ? " -LIBPATH:" : " -L" )
532
- << ActiveLibDir << ' ' << LLVM_LDFLAGS << ' \n ' ;
551
+ OS << ((HostTriple.isWindowsMSVCEnvironment ()) ? " -LIBPATH:" : " -L" );
552
+ sys::printArg (OS, ActiveLibDir, /* Quote=*/ true );
553
+ OS << ' ' << LLVM_LDFLAGS << ' \n ' ;
533
554
} else if (Arg == " --system-libs" ) {
534
555
PrintSystemLibs = true ;
535
556
} else if (Arg == " --libs" ) {
@@ -590,7 +611,8 @@ int main(int argc, char **argv) {
590
611
} else if (Arg == " --shared-mode" ) {
591
612
PrintSharedMode = true ;
592
613
} else if (Arg == " --obj-root" ) {
593
- OS << ActivePrefix << ' \n ' ;
614
+ sys::printArg (OS, ActivePrefix, /* Quote=*/ true );
615
+ OS << ' \n ' ;
594
616
} else if (Arg == " --ignore-libllvm" ) {
595
617
LinkDyLib = false ;
596
618
LinkMode = BuiltSharedLibs ? LinkModeShared : LinkModeAuto;
@@ -695,26 +717,30 @@ int main(int argc, char **argv) {
695
717
696
718
auto PrintForLib = [&](const StringRef &Lib) {
697
719
const bool Shared = LinkMode == LinkModeShared;
720
+ std::string LibFileName;
698
721
if (PrintLibNames) {
699
- OS << GetComponentLibraryFileName (Lib, Shared);
722
+ LibFileName = GetComponentLibraryFileName (Lib, Shared);
700
723
} else if (PrintLibFiles) {
701
- OS << GetComponentLibraryPath (Lib, Shared);
724
+ LibFileName = GetComponentLibraryPath (Lib, Shared);
702
725
} else if (PrintLibs) {
703
726
// On Windows, output full path to library without parameters.
704
727
// Elsewhere, if this is a typical library name, include it using -l.
705
728
if (HostTriple.isWindowsMSVCEnvironment ()) {
706
- OS << GetComponentLibraryPath (Lib, Shared);
729
+ LibFileName = GetComponentLibraryPath (Lib, Shared);
707
730
} else {
731
+ OS << " -l" ;
708
732
StringRef LibName;
709
733
if (GetComponentLibraryNameSlice (Lib, LibName)) {
710
734
// Extract library name (remove prefix and suffix).
711
- OS << " -l " << LibName;
735
+ LibFileName = LibName;
712
736
} else {
713
737
// Lib is already a library name without prefix and suffix.
714
- OS << " -l " << Lib;
738
+ LibFileName = Lib;
715
739
}
716
740
}
717
741
}
742
+ if (!LibFileName.empty ())
743
+ sys::printArg (OS, LibFileName, /* Quote=*/ true );
718
744
};
719
745
720
746
if (LinkMode == LinkModeShared && LinkDyLib) {
0 commit comments