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 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
|
//===-- Options.cpp -------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "Options.h"
#include "clang/Basic/DiagnosticIDs.h"
#include "clang/Driver/Driver.h"
#include "clang/InstallAPI/DirectoryScanner.h"
#include "clang/InstallAPI/FileList.h"
#include "clang/InstallAPI/HeaderFile.h"
#include "clang/InstallAPI/InstallAPIDiagnostic.h"
#include "llvm/BinaryFormat/Magic.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/Program.h"
#include "llvm/TargetParser/Host.h"
#include "llvm/TextAPI/DylibReader.h"
#include "llvm/TextAPI/TextAPIError.h"
#include "llvm/TextAPI/TextAPIReader.h"
#include "llvm/TextAPI/TextAPIWriter.h"
using namespace llvm;
using namespace llvm::opt;
using namespace llvm::MachO;
namespace drv = clang::driver::options;
namespace clang {
namespace installapi {
/// Create prefix string literals used in InstallAPIOpts.td.
#define PREFIX(NAME, VALUE) \
static constexpr llvm::StringLiteral NAME##_init[] = VALUE; \
static constexpr llvm::ArrayRef<llvm::StringLiteral> NAME( \
NAME##_init, std::size(NAME##_init) - 1);
#include "InstallAPIOpts.inc"
#undef PREFIX
static constexpr const llvm::StringLiteral PrefixTable_init[] =
#define PREFIX_UNION(VALUES) VALUES
#include "InstallAPIOpts.inc"
#undef PREFIX_UNION
;
static constexpr const ArrayRef<StringLiteral>
PrefixTable(PrefixTable_init, std::size(PrefixTable_init) - 1);
/// Create table mapping all options defined in InstallAPIOpts.td.
static constexpr OptTable::Info InfoTable[] = {
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, \
VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, METAVAR, \
VALUES) \
{PREFIX, \
NAME, \
HELPTEXT, \
HELPTEXTSFORVARIANTS, \
METAVAR, \
OPT_##ID, \
Option::KIND##Class, \
PARAM, \
FLAGS, \
VISIBILITY, \
OPT_##GROUP, \
OPT_##ALIAS, \
ALIASARGS, \
VALUES},
#include "InstallAPIOpts.inc"
#undef OPTION
};
namespace {
/// \brief Create OptTable class for parsing actual command line arguments.
class DriverOptTable : public opt::PrecomputedOptTable {
public:
DriverOptTable() : PrecomputedOptTable(InfoTable, PrefixTable) {}
};
} // end anonymous namespace.
static llvm::opt::OptTable *createDriverOptTable() {
return new DriverOptTable();
}
/// Parse JSON input into argument list.
///
/* Expected input format.
* { "label" : ["-ClangArg1", "-ClangArg2"] }
*/
///
/// Input is interpreted as "-Xlabel ClangArg1 -XLabel ClangArg2".
static Expected<llvm::opt::InputArgList>
getArgListFromJSON(const StringRef Input, llvm::opt::OptTable *Table,
std::vector<std::string> &Storage) {
using namespace json;
Expected<Value> ValOrErr = json::parse(Input);
if (!ValOrErr)
return ValOrErr.takeError();
const Object *Root = ValOrErr->getAsObject();
if (!Root)
return llvm::opt::InputArgList();
for (const auto &KV : *Root) {
const Array *ArgList = KV.getSecond().getAsArray();
std::string Label = "-X" + KV.getFirst().str();
if (!ArgList)
return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat);
for (auto Arg : *ArgList) {
std::optional<StringRef> ArgStr = Arg.getAsString();
if (!ArgStr)
return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat);
Storage.emplace_back(Label);
Storage.emplace_back(*ArgStr);
}
}
std::vector<const char *> CArgs(Storage.size());
llvm::for_each(Storage,
[&CArgs](StringRef Str) { CArgs.emplace_back(Str.data()); });
unsigned MissingArgIndex, MissingArgCount;
return Table->ParseArgs(CArgs, MissingArgIndex, MissingArgCount);
}
bool Options::processDriverOptions(InputArgList &Args) {
// Handle inputs.
for (const StringRef Path : Args.getAllArgValues(drv::OPT_INPUT)) {
// Assume any input that is not a directory is a filelist.
// InstallAPI does not accept multiple directories, so retain the last one.
if (FM->getOptionalDirectoryRef(Path))
DriverOpts.InputDirectory = Path.str();
else
DriverOpts.FileLists.emplace_back(Path.str());
}
// Handle output.
SmallString<PATH_MAX> OutputPath;
if (auto *Arg = Args.getLastArg(drv::OPT_o)) {
OutputPath = Arg->getValue();
if (OutputPath != "-")
FM->makeAbsolutePath(OutputPath);
DriverOpts.OutputPath = std::string(OutputPath);
}
if (DriverOpts.OutputPath.empty()) {
Diags->Report(diag::err_no_output_file);
return false;
}
// Do basic error checking first for mixing -target and -arch options.
auto *ArgArch = Args.getLastArgNoClaim(drv::OPT_arch);
auto *ArgTarget = Args.getLastArgNoClaim(drv::OPT_target);
auto *ArgTargetVariant =
Args.getLastArgNoClaim(drv::OPT_darwin_target_variant);
if (ArgArch && (ArgTarget || ArgTargetVariant)) {
Diags->Report(clang::diag::err_drv_argument_not_allowed_with)
<< ArgArch->getAsString(Args)
<< (ArgTarget ? ArgTarget : ArgTargetVariant)->getAsString(Args);
return false;
}
auto *ArgMinTargetOS = Args.getLastArgNoClaim(drv::OPT_mtargetos_EQ);
if ((ArgTarget || ArgTargetVariant) && ArgMinTargetOS) {
Diags->Report(clang::diag::err_drv_cannot_mix_options)
<< ArgTarget->getAsString(Args) << ArgMinTargetOS->getAsString(Args);
return false;
}
// Capture target triples first.
if (ArgTarget) {
for (const Arg *A : Args.filtered(drv::OPT_target)) {
A->claim();
llvm::Triple TargetTriple(A->getValue());
Target TAPITarget = Target(TargetTriple);
if ((TAPITarget.Arch == AK_unknown) ||
(TAPITarget.Platform == PLATFORM_UNKNOWN)) {
Diags->Report(clang::diag::err_drv_unsupported_opt_for_target)
<< "installapi" << TargetTriple.str();
return false;
}
DriverOpts.Targets[TAPITarget] = TargetTriple;
}
}
// Capture target variants.
DriverOpts.Zippered = ArgTargetVariant != nullptr;
for (Arg *A : Args.filtered(drv::OPT_darwin_target_variant)) {
A->claim();
Triple Variant(A->getValue());
if (Variant.getVendor() != Triple::Apple) {
Diags->Report(diag::err_unsupported_vendor)
<< Variant.getVendorName() << A->getAsString(Args);
return false;
}
switch (Variant.getOS()) {
default:
Diags->Report(diag::err_unsupported_os)
<< Variant.getOSName() << A->getAsString(Args);
return false;
case Triple::MacOSX:
case Triple::IOS:
break;
}
switch (Variant.getEnvironment()) {
default:
Diags->Report(diag::err_unsupported_environment)
<< Variant.getEnvironmentName() << A->getAsString(Args);
return false;
case Triple::UnknownEnvironment:
case Triple::MacABI:
break;
}
Target TAPIVariant(Variant);
// See if there is a matching --target option for this --target-variant
// option.
auto It = find_if(DriverOpts.Targets, [&](const auto &T) {
return (T.first.Arch == TAPIVariant.Arch) &&
(T.first.Platform != PlatformType::PLATFORM_UNKNOWN);
});
if (It == DriverOpts.Targets.end()) {
Diags->Report(diag::err_no_matching_target) << Variant.str();
return false;
}
DriverOpts.Targets[TAPIVariant] = Variant;
}
DriverOpts.Verbose = Args.hasArgNoClaim(drv::OPT_v);
return true;
}
bool Options::processInstallAPIXOptions(InputArgList &Args) {
for (arg_iterator It = Args.begin(), End = Args.end(); It != End; ++It) {
Arg *A = *It;
if (A->getOption().matches(OPT_Xarch__)) {
if (!processXarchOption(Args, It))
return false;
continue;
} else if (A->getOption().matches(OPT_Xplatform__)) {
if (!processXplatformOption(Args, It))
return false;
continue;
} else if (A->getOption().matches(OPT_Xproject)) {
if (!processXprojectOption(Args, It))
return false;
continue;
} else if (!A->getOption().matches(OPT_X__))
continue;
// Handle any user defined labels.
const StringRef Label = A->getValue(0);
// Ban "public" and "private" labels.
if ((Label.lower() == "public") || (Label.lower() == "private")) {
Diags->Report(diag::err_invalid_label) << Label;
return false;
}
auto NextIt = std::next(It);
if (NextIt == End) {
Diags->Report(clang::diag::err_drv_missing_argument)
<< A->getAsString(Args) << 1;
return false;
}
Arg *NextA = *NextIt;
switch ((ID)NextA->getOption().getID()) {
case OPT_D:
case OPT_U:
break;
default:
Diags->Report(clang::diag::err_drv_argument_not_allowed_with)
<< A->getAsString(Args) << NextA->getAsString(Args);
return false;
}
const StringRef ASpelling = NextA->getSpelling();
const auto &AValues = NextA->getValues();
if (AValues.empty())
FEOpts.UniqueArgs[Label].emplace_back(ASpelling.str());
else
for (const StringRef Val : AValues)
FEOpts.UniqueArgs[Label].emplace_back((ASpelling + Val).str());
A->claim();
NextA->claim();
}
return true;
}
bool Options::processXplatformOption(InputArgList &Args, arg_iterator Curr) {
Arg *A = *Curr;
PlatformType Platform = getPlatformFromName(A->getValue(0));
if (Platform == PLATFORM_UNKNOWN) {
Diags->Report(diag::err_unsupported_os)
<< getPlatformName(Platform) << A->getAsString(Args);
return false;
}
auto NextIt = std::next(Curr);
if (NextIt == Args.end()) {
Diags->Report(diag::err_drv_missing_argument) << A->getAsString(Args) << 1;
return false;
}
Arg *NextA = *NextIt;
switch ((ID)NextA->getOption().getID()) {
case OPT_iframework:
FEOpts.SystemFwkPaths.emplace_back(NextA->getValue(), Platform);
break;
default:
Diags->Report(diag::err_drv_invalid_argument_to_option)
<< A->getAsString(Args) << NextA->getAsString(Args);
return false;
}
A->claim();
NextA->claim();
return true;
}
bool Options::processXprojectOption(InputArgList &Args, arg_iterator Curr) {
Arg *A = *Curr;
auto NextIt = std::next(Curr);
if (NextIt == Args.end()) {
Diags->Report(diag::err_drv_missing_argument) << A->getAsString(Args) << 1;
return false;
}
Arg *NextA = *NextIt;
switch ((ID)NextA->getOption().getID()) {
case OPT_fobjc_arc:
case OPT_fmodules:
case OPT_fmodules_cache_path:
case OPT_include_:
case OPT_fvisibility_EQ:
break;
default:
Diags->Report(diag::err_drv_argument_not_allowed_with)
<< A->getAsString(Args) << NextA->getAsString(Args);
return false;
}
std::string ArgString = NextA->getSpelling().str();
for (const StringRef Val : NextA->getValues())
ArgString += Val.str();
ProjectLevelArgs.push_back(ArgString);
A->claim();
NextA->claim();
return true;
}
bool Options::processXarchOption(InputArgList &Args, arg_iterator Curr) {
Arg *CurrArg = *Curr;
Architecture Arch = getArchitectureFromName(CurrArg->getValue(0));
if (Arch == AK_unknown) {
Diags->Report(diag::err_drv_invalid_arch_name)
<< CurrArg->getAsString(Args);
return false;
}
auto NextIt = std::next(Curr);
if (NextIt == Args.end()) {
Diags->Report(diag::err_drv_missing_argument)
<< CurrArg->getAsString(Args) << 1;
return false;
}
// InstallAPI has a limited understanding of supported Xarch options.
// Currently this is restricted to linker inputs.
const Arg *NextArg = *NextIt;
switch (NextArg->getOption().getID()) {
case OPT_allowable_client:
case OPT_reexport_l:
case OPT_reexport_framework:
case OPT_reexport_library:
case OPT_rpath:
break;
default:
Diags->Report(diag::err_drv_invalid_argument_to_option)
<< NextArg->getAsString(Args) << CurrArg->getAsString(Args);
return false;
}
ArgToArchMap[NextArg] = Arch;
CurrArg->claim();
return true;
}
bool Options::processOptionList(InputArgList &Args,
llvm::opt::OptTable *Table) {
Arg *A = Args.getLastArg(OPT_option_list);
if (!A)
return true;
const StringRef Path = A->getValue(0);
auto InputOrErr = FM->getBufferForFile(Path);
if (auto Err = InputOrErr.getError()) {
Diags->Report(diag::err_cannot_open_file) << Path << Err.message();
return false;
}
// Backing storage referenced for argument processing.
std::vector<std::string> Storage;
auto ArgsOrErr =
getArgListFromJSON((*InputOrErr)->getBuffer(), Table, Storage);
if (auto Err = ArgsOrErr.takeError()) {
Diags->Report(diag::err_cannot_read_input_list)
<< "option" << Path << toString(std::move(Err));
return false;
}
return processInstallAPIXOptions(*ArgsOrErr);
}
bool Options::processLinkerOptions(InputArgList &Args) {
// Handle required arguments.
if (const Arg *A = Args.getLastArg(drv::OPT_install__name))
LinkerOpts.InstallName = A->getValue();
if (LinkerOpts.InstallName.empty()) {
Diags->Report(diag::err_no_install_name);
return false;
}
// Defaulted or optional arguments.
if (auto *Arg = Args.getLastArg(drv::OPT_current__version))
LinkerOpts.CurrentVersion.parse64(Arg->getValue());
if (auto *Arg = Args.getLastArg(drv::OPT_compatibility__version))
LinkerOpts.CompatVersion.parse64(Arg->getValue());
if (auto *Arg = Args.getLastArg(drv::OPT_compatibility__version))
LinkerOpts.CompatVersion.parse64(Arg->getValue());
if (auto *Arg = Args.getLastArg(drv::OPT_umbrella))
LinkerOpts.ParentUmbrella = Arg->getValue();
LinkerOpts.IsDylib = Args.hasArg(drv::OPT_dynamiclib);
for (auto *Arg : Args.filtered(drv::OPT_alias_list)) {
LinkerOpts.AliasLists.emplace_back(Arg->getValue());
Arg->claim();
}
LinkerOpts.AppExtensionSafe = Args.hasFlag(
drv::OPT_fapplication_extension, drv::OPT_fno_application_extension,
/*Default=*/LinkerOpts.AppExtensionSafe);
if (::getenv("LD_NO_ENCRYPT") != nullptr)
LinkerOpts.AppExtensionSafe = true;
if (::getenv("LD_APPLICATION_EXTENSION_SAFE") != nullptr)
LinkerOpts.AppExtensionSafe = true;
// Capture library paths.
PathSeq LibraryPaths;
for (const Arg *A : Args.filtered(drv::OPT_L)) {
LibraryPaths.emplace_back(A->getValue());
A->claim();
}
if (!LibraryPaths.empty())
LinkerOpts.LibPaths = std::move(LibraryPaths);
return true;
}
// NOTE: Do not claim any arguments, as they will be passed along for CC1
// invocations.
bool Options::processFrontendOptions(InputArgList &Args) {
// Capture language mode.
if (auto *A = Args.getLastArgNoClaim(drv::OPT_x)) {
FEOpts.LangMode = llvm::StringSwitch<clang::Language>(A->getValue())
.Case("c", clang::Language::C)
.Case("c++", clang::Language::CXX)
.Case("objective-c", clang::Language::ObjC)
.Case("objective-c++", clang::Language::ObjCXX)
.Default(clang::Language::Unknown);
if (FEOpts.LangMode == clang::Language::Unknown) {
Diags->Report(clang::diag::err_drv_invalid_value)
<< A->getAsString(Args) << A->getValue();
return false;
}
}
for (auto *A : Args.filtered(drv::OPT_ObjC, drv::OPT_ObjCXX)) {
if (A->getOption().matches(drv::OPT_ObjC))
FEOpts.LangMode = clang::Language::ObjC;
else
FEOpts.LangMode = clang::Language::ObjCXX;
}
// Capture Sysroot.
if (const Arg *A = Args.getLastArgNoClaim(drv::OPT_isysroot)) {
SmallString<PATH_MAX> Path(A->getValue());
FM->makeAbsolutePath(Path);
if (!FM->getOptionalDirectoryRef(Path)) {
Diags->Report(diag::err_missing_sysroot) << Path;
return false;
}
FEOpts.ISysroot = std::string(Path);
} else if (FEOpts.ISysroot.empty()) {
// Mirror CLANG and obtain the isysroot from the SDKROOT environment
// variable, if it wasn't defined by the command line.
if (auto *Env = ::getenv("SDKROOT")) {
if (StringRef(Env) != "/" && llvm::sys::path::is_absolute(Env) &&
FM->getOptionalFileRef(Env))
FEOpts.ISysroot = Env;
}
}
// Capture system frameworks for all platforms.
for (const Arg *A : Args.filtered(drv::OPT_iframework))
FEOpts.SystemFwkPaths.emplace_back(A->getValue(),
std::optional<PlatformType>{});
// Capture framework paths.
PathSeq FrameworkPaths;
for (const Arg *A : Args.filtered(drv::OPT_F))
FrameworkPaths.emplace_back(A->getValue());
if (!FrameworkPaths.empty())
FEOpts.FwkPaths = std::move(FrameworkPaths);
// Add default framework/library paths.
PathSeq DefaultLibraryPaths = {"/usr/lib", "/usr/local/lib"};
PathSeq DefaultFrameworkPaths = {"/Library/Frameworks",
"/System/Library/Frameworks"};
for (const StringRef LibPath : DefaultLibraryPaths) {
SmallString<PATH_MAX> Path(FEOpts.ISysroot);
sys::path::append(Path, LibPath);
LinkerOpts.LibPaths.emplace_back(Path.str());
}
for (const StringRef FwkPath : DefaultFrameworkPaths) {
SmallString<PATH_MAX> Path(FEOpts.ISysroot);
sys::path::append(Path, FwkPath);
FEOpts.SystemFwkPaths.emplace_back(Path.str(),
std::optional<PlatformType>{});
}
return true;
}
bool Options::addFilePaths(InputArgList &Args, PathSeq &Headers,
OptSpecifier ID) {
for (const StringRef Path : Args.getAllArgValues(ID)) {
if ((bool)FM->getDirectory(Path, /*CacheFailure=*/false)) {
auto InputHeadersOrErr = enumerateFiles(*FM, Path);
if (!InputHeadersOrErr) {
Diags->Report(diag::err_cannot_open_file)
<< Path << toString(InputHeadersOrErr.takeError());
return false;
}
// Sort headers to ensure deterministic behavior.
sort(*InputHeadersOrErr);
for (StringRef H : *InputHeadersOrErr)
Headers.emplace_back(std::move(H));
} else
Headers.emplace_back(Path);
}
return true;
}
std::vector<const char *>
Options::processAndFilterOutInstallAPIOptions(ArrayRef<const char *> Args) {
std::unique_ptr<llvm::opt::OptTable> Table;
Table.reset(createDriverOptTable());
unsigned MissingArgIndex, MissingArgCount;
auto ParsedArgs = Table->ParseArgs(Args.slice(1), MissingArgIndex,
MissingArgCount, Visibility());
// Capture InstallAPI only driver options.
if (!processInstallAPIXOptions(ParsedArgs))
return {};
if (!processOptionList(ParsedArgs, Table.get()))
return {};
DriverOpts.Demangle = ParsedArgs.hasArg(OPT_demangle);
if (auto *A = ParsedArgs.getLastArg(OPT_filetype)) {
DriverOpts.OutFT = TextAPIWriter::parseFileType(A->getValue());
if (DriverOpts.OutFT == FileType::Invalid) {
Diags->Report(clang::diag::err_drv_invalid_value)
<< A->getAsString(ParsedArgs) << A->getValue();
return {};
}
}
if (const Arg *A = ParsedArgs.getLastArg(OPT_verify_mode_EQ)) {
DriverOpts.VerifyMode =
StringSwitch<VerificationMode>(A->getValue())
.Case("ErrorsOnly", VerificationMode::ErrorsOnly)
.Case("ErrorsAndWarnings", VerificationMode::ErrorsAndWarnings)
.Case("Pedantic", VerificationMode::Pedantic)
.Default(VerificationMode::Invalid);
if (DriverOpts.VerifyMode == VerificationMode::Invalid) {
Diags->Report(clang::diag::err_drv_invalid_value)
<< A->getAsString(ParsedArgs) << A->getValue();
return {};
}
}
if (const Arg *A = ParsedArgs.getLastArg(OPT_verify_against))
DriverOpts.DylibToVerify = A->getValue();
if (const Arg *A = ParsedArgs.getLastArg(OPT_dsym))
DriverOpts.DSYMPath = A->getValue();
DriverOpts.TraceLibraryLocation = ParsedArgs.hasArg(OPT_t);
// Linker options not handled by clang driver.
LinkerOpts.OSLibNotForSharedCache =
ParsedArgs.hasArg(OPT_not_for_dyld_shared_cache);
for (const Arg *A : ParsedArgs.filtered(OPT_allowable_client)) {
LinkerOpts.AllowableClients[A->getValue()] =
ArgToArchMap.count(A) ? ArgToArchMap[A] : ArchitectureSet();
A->claim();
}
for (const Arg *A : ParsedArgs.filtered(OPT_reexport_l)) {
LinkerOpts.ReexportedLibraries[A->getValue()] =
ArgToArchMap.count(A) ? ArgToArchMap[A] : ArchitectureSet();
A->claim();
}
for (const Arg *A : ParsedArgs.filtered(OPT_reexport_library)) {
LinkerOpts.ReexportedLibraryPaths[A->getValue()] =
ArgToArchMap.count(A) ? ArgToArchMap[A] : ArchitectureSet();
A->claim();
}
for (const Arg *A : ParsedArgs.filtered(OPT_reexport_framework)) {
LinkerOpts.ReexportedFrameworks[A->getValue()] =
ArgToArchMap.count(A) ? ArgToArchMap[A] : ArchitectureSet();
A->claim();
}
for (const Arg *A : ParsedArgs.filtered(OPT_rpath)) {
LinkerOpts.RPaths[A->getValue()] =
ArgToArchMap.count(A) ? ArgToArchMap[A] : ArchitectureSet();
A->claim();
}
// Handle exclude & extra header directories or files.
auto handleAdditionalInputArgs = [&](PathSeq &Headers,
clang::installapi::ID OptID) {
if (ParsedArgs.hasArgNoClaim(OptID))
Headers.clear();
return addFilePaths(ParsedArgs, Headers, OptID);
};
if (!handleAdditionalInputArgs(DriverOpts.ExtraPublicHeaders,
OPT_extra_public_header))
return {};
if (!handleAdditionalInputArgs(DriverOpts.ExtraPrivateHeaders,
OPT_extra_private_header))
return {};
if (!handleAdditionalInputArgs(DriverOpts.ExtraProjectHeaders,
OPT_extra_project_header))
return {};
if (!handleAdditionalInputArgs(DriverOpts.ExcludePublicHeaders,
OPT_exclude_public_header))
return {};
if (!handleAdditionalInputArgs(DriverOpts.ExcludePrivateHeaders,
OPT_exclude_private_header))
return {};
if (!handleAdditionalInputArgs(DriverOpts.ExcludeProjectHeaders,
OPT_exclude_project_header))
return {};
// Handle umbrella headers.
if (const Arg *A = ParsedArgs.getLastArg(OPT_public_umbrella_header))
DriverOpts.PublicUmbrellaHeader = A->getValue();
if (const Arg *A = ParsedArgs.getLastArg(OPT_private_umbrella_header))
DriverOpts.PrivateUmbrellaHeader = A->getValue();
if (const Arg *A = ParsedArgs.getLastArg(OPT_project_umbrella_header))
DriverOpts.ProjectUmbrellaHeader = A->getValue();
/// Any unclaimed arguments should be forwarded to the clang driver.
std::vector<const char *> ClangDriverArgs(ParsedArgs.size());
for (const Arg *A : ParsedArgs) {
if (A->isClaimed())
continue;
// Forward along unclaimed but overlapping arguments to the clang driver.
if (A->getOption().getID() > (unsigned)OPT_UNKNOWN) {
ClangDriverArgs.push_back(A->getSpelling().data());
} else
llvm::copy(A->getValues(), std::back_inserter(ClangDriverArgs));
}
return ClangDriverArgs;
}
Options::Options(DiagnosticsEngine &Diag, FileManager *FM,
ArrayRef<const char *> Args, const StringRef ProgName)
: Diags(&Diag), FM(FM) {
// First process InstallAPI specific options.
auto DriverArgs = processAndFilterOutInstallAPIOptions(Args);
if (Diags->hasErrorOccurred())
return;
// Set up driver to parse remaining input arguments.
clang::driver::Driver Driver(ProgName, llvm::sys::getDefaultTargetTriple(),
*Diags, "clang installapi tool");
auto TargetAndMode =
clang::driver::ToolChain::getTargetAndModeFromProgramName(ProgName);
Driver.setTargetAndMode(TargetAndMode);
bool HasError = false;
llvm::opt::InputArgList ArgList =
Driver.ParseArgStrings(DriverArgs, /*UseDriverMode=*/true, HasError);
if (HasError)
return;
Driver.setCheckInputsExist(false);
if (!processDriverOptions(ArgList))
return;
if (!processLinkerOptions(ArgList))
return;
if (!processFrontendOptions(ArgList))
return;
// After all InstallAPI necessary arguments have been collected. Go back and
// assign values that were unknown before the clang driver opt table was used.
ArchitectureSet AllArchs;
llvm::for_each(DriverOpts.Targets,
[&AllArchs](const auto &T) { AllArchs.set(T.first.Arch); });
auto assignDefaultLibAttrs = [&AllArchs](LibAttrs &Attrs) {
for (StringMapEntry<ArchitectureSet> &Entry : Attrs)
if (Entry.getValue().empty())
Entry.setValue(AllArchs);
};
assignDefaultLibAttrs(LinkerOpts.AllowableClients);
assignDefaultLibAttrs(LinkerOpts.ReexportedFrameworks);
assignDefaultLibAttrs(LinkerOpts.ReexportedLibraries);
assignDefaultLibAttrs(LinkerOpts.ReexportedLibraryPaths);
assignDefaultLibAttrs(LinkerOpts.RPaths);
/// Force cc1 options that should always be on.
FrontendArgs = {"-fsyntax-only", "-Wprivate-extern"};
/// Any unclaimed arguments should be handled by invoking the clang frontend.
for (const Arg *A : ArgList) {
if (A->isClaimed())
continue;
FrontendArgs.emplace_back(A->getSpelling());
llvm::copy(A->getValues(), std::back_inserter(FrontendArgs));
}
}
static Expected<std::unique_ptr<InterfaceFile>>
getInterfaceFile(const StringRef Filename) {
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
MemoryBuffer::getFile(Filename);
if (auto Err = BufferOrErr.getError())
return errorCodeToError(std::move(Err));
auto Buffer = std::move(*BufferOrErr);
std::unique_ptr<InterfaceFile> IF;
switch (identify_magic(Buffer->getBuffer())) {
case file_magic::macho_dynamically_linked_shared_lib:
case file_magic::macho_dynamically_linked_shared_lib_stub:
case file_magic::macho_universal_binary:
return DylibReader::get(Buffer->getMemBufferRef());
break;
case file_magic::tapi_file:
return TextAPIReader::get(Buffer->getMemBufferRef());
default:
return make_error<TextAPIError>(TextAPIErrorCode::InvalidInputFormat,
"unsupported library file format");
}
llvm_unreachable("unexpected failure in getInterface");
}
std::pair<LibAttrs, ReexportedInterfaces> Options::getReexportedLibraries() {
LibAttrs Reexports;
ReexportedInterfaces ReexportIFs;
auto AccumulateReexports = [&](StringRef Path, const ArchitectureSet &Archs) {
auto ReexportIFOrErr = getInterfaceFile(Path);
if (!ReexportIFOrErr)
return false;
std::unique_ptr<InterfaceFile> Reexport = std::move(*ReexportIFOrErr);
StringRef InstallName = Reexport->getInstallName();
assert(!InstallName.empty() && "Parse error for install name");
Reexports.insert({InstallName, Archs});
ReexportIFs.emplace_back(std::move(*Reexport));
return true;
};
PlatformSet Platforms;
llvm::for_each(DriverOpts.Targets,
[&](const auto &T) { Platforms.insert(T.first.Platform); });
// Populate search paths by looking at user paths before system ones.
PathSeq FwkSearchPaths(FEOpts.FwkPaths.begin(), FEOpts.FwkPaths.end());
for (const PlatformType P : Platforms) {
PathSeq PlatformSearchPaths = getPathsForPlatform(FEOpts.SystemFwkPaths, P);
FwkSearchPaths.insert(FwkSearchPaths.end(), PlatformSearchPaths.begin(),
PlatformSearchPaths.end());
for (const StringMapEntry<ArchitectureSet> &Lib :
LinkerOpts.ReexportedFrameworks) {
std::string Name = (Lib.getKey() + ".framework/" + Lib.getKey()).str();
std::string Path = findLibrary(Name, *FM, FwkSearchPaths, {}, {});
if (Path.empty()) {
Diags->Report(diag::err_cannot_find_reexport) << false << Lib.getKey();
return {};
}
if (DriverOpts.TraceLibraryLocation)
errs() << Path << "\n";
AccumulateReexports(Path, Lib.getValue());
}
FwkSearchPaths.resize(FwkSearchPaths.size() - PlatformSearchPaths.size());
}
for (const StringMapEntry<ArchitectureSet> &Lib :
LinkerOpts.ReexportedLibraries) {
std::string Name = "lib" + Lib.getKey().str() + ".dylib";
std::string Path = findLibrary(Name, *FM, {}, LinkerOpts.LibPaths, {});
if (Path.empty()) {
Diags->Report(diag::err_cannot_find_reexport) << true << Lib.getKey();
return {};
}
if (DriverOpts.TraceLibraryLocation)
errs() << Path << "\n";
AccumulateReexports(Path, Lib.getValue());
}
for (const StringMapEntry<ArchitectureSet> &Lib :
LinkerOpts.ReexportedLibraryPaths)
AccumulateReexports(Lib.getKey(), Lib.getValue());
return {std::move(Reexports), std::move(ReexportIFs)};
}
InstallAPIContext Options::createContext() {
InstallAPIContext Ctx;
Ctx.FM = FM;
Ctx.Diags = Diags;
// InstallAPI requires two level namespacing.
Ctx.BA.TwoLevelNamespace = true;
Ctx.BA.InstallName = LinkerOpts.InstallName;
Ctx.BA.CurrentVersion = LinkerOpts.CurrentVersion;
Ctx.BA.CompatVersion = LinkerOpts.CompatVersion;
Ctx.BA.AppExtensionSafe = LinkerOpts.AppExtensionSafe;
Ctx.BA.ParentUmbrella = LinkerOpts.ParentUmbrella;
Ctx.BA.OSLibNotForSharedCache = LinkerOpts.OSLibNotForSharedCache;
Ctx.FT = DriverOpts.OutFT;
Ctx.OutputLoc = DriverOpts.OutputPath;
Ctx.LangMode = FEOpts.LangMode;
auto [Reexports, ReexportedIFs] = getReexportedLibraries();
if (Diags->hasErrorOccurred())
return Ctx;
Ctx.Reexports = Reexports;
// Collect symbols from alias lists.
AliasMap Aliases;
for (const StringRef ListPath : LinkerOpts.AliasLists) {
auto Buffer = FM->getBufferForFile(ListPath);
if (auto Err = Buffer.getError()) {
Diags->Report(diag::err_cannot_open_file) << ListPath << Err.message();
return Ctx;
}
Expected<AliasMap> Result = parseAliasList(Buffer.get());
if (!Result) {
Diags->Report(diag::err_cannot_read_input_list)
<< "symbol alias" << ListPath << toString(Result.takeError());
return Ctx;
}
Aliases.insert(Result.get().begin(), Result.get().end());
}
// Attempt to find umbrella headers by capturing framework name.
StringRef FrameworkName;
if (!LinkerOpts.IsDylib)
FrameworkName =
Library::getFrameworkNameFromInstallName(LinkerOpts.InstallName);
/// Process inputs headers.
// 1. For headers discovered by directory scanning, sort them.
// 2. For headers discovered by filelist, respect ordering.
// 3. Append extra headers and mark any excluded headers.
// 4. Finally, surface up umbrella headers to top of the list.
if (!DriverOpts.InputDirectory.empty()) {
DirectoryScanner Scanner(*FM, LinkerOpts.IsDylib
? ScanMode::ScanDylibs
: ScanMode::ScanFrameworks);
SmallString<PATH_MAX> NormalizedPath(DriverOpts.InputDirectory);
FM->getVirtualFileSystem().makeAbsolute(NormalizedPath);
sys::path::remove_dots(NormalizedPath, /*remove_dot_dot=*/true);
if (llvm::Error Err = Scanner.scan(NormalizedPath)) {
Diags->Report(diag::err_directory_scanning)
<< DriverOpts.InputDirectory << std::move(Err);
return Ctx;
}
std::vector<Library> InputLibraries = Scanner.takeLibraries();
if (InputLibraries.size() > 1) {
Diags->Report(diag::err_more_than_one_library);
return Ctx;
}
llvm::append_range(Ctx.InputHeaders,
DirectoryScanner::getHeaders(InputLibraries));
llvm::stable_sort(Ctx.InputHeaders);
}
for (const StringRef ListPath : DriverOpts.FileLists) {
auto Buffer = FM->getBufferForFile(ListPath);
if (auto Err = Buffer.getError()) {
Diags->Report(diag::err_cannot_open_file) << ListPath << Err.message();
return Ctx;
}
if (auto Err = FileListReader::loadHeaders(std::move(Buffer.get()),
Ctx.InputHeaders, FM)) {
Diags->Report(diag::err_cannot_read_input_list)
<< "header file" << ListPath << std::move(Err);
return Ctx;
}
}
// After initial input has been processed, add any extra headers.
auto HandleExtraHeaders = [&](PathSeq &Headers, HeaderType Type) -> bool {
assert(Type != HeaderType::Unknown && "Missing header type.");
for (const StringRef Path : Headers) {
if (!FM->getOptionalFileRef(Path)) {
Diags->Report(diag::err_no_such_header_file) << Path << (unsigned)Type;
return false;
}
SmallString<PATH_MAX> FullPath(Path);
FM->makeAbsolutePath(FullPath);
auto IncludeName = createIncludeHeaderName(FullPath);
Ctx.InputHeaders.emplace_back(
FullPath, Type, IncludeName.has_value() ? *IncludeName : "");
Ctx.InputHeaders.back().setExtra();
}
return true;
};
if (!HandleExtraHeaders(DriverOpts.ExtraPublicHeaders, HeaderType::Public) ||
!HandleExtraHeaders(DriverOpts.ExtraPrivateHeaders,
HeaderType::Private) ||
!HandleExtraHeaders(DriverOpts.ExtraProjectHeaders, HeaderType::Project))
return Ctx;
// After all headers have been added, consider excluded headers.
std::vector<std::unique_ptr<HeaderGlob>> ExcludedHeaderGlobs;
std::set<FileEntryRef> ExcludedHeaderFiles;
auto ParseGlobs = [&](const PathSeq &Paths, HeaderType Type) {
assert(Type != HeaderType::Unknown && "Missing header type.");
for (const StringRef Path : Paths) {
auto Glob = HeaderGlob::create(Path, Type);
if (Glob)
ExcludedHeaderGlobs.emplace_back(std::move(Glob.get()));
else {
consumeError(Glob.takeError());
if (auto File = FM->getFileRef(Path))
ExcludedHeaderFiles.emplace(*File);
else {
Diags->Report(diag::err_no_such_header_file)
<< Path << (unsigned)Type;
return false;
}
}
}
return true;
};
if (!ParseGlobs(DriverOpts.ExcludePublicHeaders, HeaderType::Public) ||
!ParseGlobs(DriverOpts.ExcludePrivateHeaders, HeaderType::Private) ||
!ParseGlobs(DriverOpts.ExcludeProjectHeaders, HeaderType::Project))
return Ctx;
for (HeaderFile &Header : Ctx.InputHeaders) {
for (auto &Glob : ExcludedHeaderGlobs)
if (Glob->match(Header))
Header.setExcluded();
}
if (!ExcludedHeaderFiles.empty()) {
for (HeaderFile &Header : Ctx.InputHeaders) {
auto FileRef = FM->getFileRef(Header.getPath());
if (!FileRef)
continue;
if (ExcludedHeaderFiles.count(*FileRef))
Header.setExcluded();
}
}
// Report if glob was ignored.
for (const auto &Glob : ExcludedHeaderGlobs)
if (!Glob->didMatch())
Diags->Report(diag::warn_glob_did_not_match) << Glob->str();
// Mark any explicit or inferred umbrella headers. If one exists, move
// that to the beginning of the input headers.
auto MarkandMoveUmbrellaInHeaders = [&](llvm::Regex &Regex,
HeaderType Type) -> bool {
auto It = find_if(Ctx.InputHeaders, [&Regex, Type](const HeaderFile &H) {
return (H.getType() == Type) && Regex.match(H.getPath());
});
if (It == Ctx.InputHeaders.end())
return false;
It->setUmbrellaHeader();
// Because there can be an umbrella header per header type,
// find the first non umbrella header to swap position with.
auto BeginPos = find_if(Ctx.InputHeaders, [](const HeaderFile &H) {
return !H.isUmbrellaHeader();
});
if (BeginPos != Ctx.InputHeaders.end() && BeginPos < It)
std::swap(*BeginPos, *It);
return true;
};
auto FindUmbrellaHeader = [&](StringRef HeaderPath, HeaderType Type) -> bool {
assert(Type != HeaderType::Unknown && "Missing header type.");
if (!HeaderPath.empty()) {
auto EscapedString = Regex::escape(HeaderPath);
Regex UmbrellaRegex(EscapedString);
if (!MarkandMoveUmbrellaInHeaders(UmbrellaRegex, Type)) {
Diags->Report(diag::err_no_such_umbrella_header_file)
<< HeaderPath << (unsigned)Type;
return false;
}
} else if (!FrameworkName.empty() && (Type != HeaderType::Project)) {
auto UmbrellaName = "/" + Regex::escape(FrameworkName);
if (Type == HeaderType::Public)
UmbrellaName += "\\.h";
else
UmbrellaName += "[_]?Private\\.h";
Regex UmbrellaRegex(UmbrellaName);
MarkandMoveUmbrellaInHeaders(UmbrellaRegex, Type);
}
return true;
};
if (!FindUmbrellaHeader(DriverOpts.PublicUmbrellaHeader,
HeaderType::Public) ||
!FindUmbrellaHeader(DriverOpts.PrivateUmbrellaHeader,
HeaderType::Private) ||
!FindUmbrellaHeader(DriverOpts.ProjectUmbrellaHeader,
HeaderType::Project))
return Ctx;
// Parse binary dylib and initialize verifier.
if (DriverOpts.DylibToVerify.empty()) {
Ctx.Verifier = std::make_unique<DylibVerifier>();
return Ctx;
}
auto Buffer = FM->getBufferForFile(DriverOpts.DylibToVerify);
if (auto Err = Buffer.getError()) {
Diags->Report(diag::err_cannot_open_file)
<< DriverOpts.DylibToVerify << Err.message();
return Ctx;
}
DylibReader::ParseOption PO;
PO.Undefineds = false;
Expected<Records> Slices =
DylibReader::readFile((*Buffer)->getMemBufferRef(), PO);
if (auto Err = Slices.takeError()) {
Diags->Report(diag::err_cannot_open_file)
<< DriverOpts.DylibToVerify << std::move(Err);
return Ctx;
}
Ctx.Verifier = std::make_unique<DylibVerifier>(
std::move(*Slices), std::move(ReexportedIFs), std::move(Aliases), Diags,
DriverOpts.VerifyMode, DriverOpts.Zippered, DriverOpts.Demangle,
DriverOpts.DSYMPath);
return Ctx;
}
void Options::addConditionalCC1Args(std::vector<std::string> &ArgStrings,
const llvm::Triple &Targ,
const HeaderType Type) {
// Unique to architecture (Xarch) options hold no arguments to pass along for
// frontend.
// Add specific to platform arguments.
PathSeq PlatformSearchPaths =
getPathsForPlatform(FEOpts.SystemFwkPaths, mapToPlatformType(Targ));
llvm::for_each(PlatformSearchPaths, [&ArgStrings](const StringRef Path) {
ArgStrings.push_back("-iframework");
ArgStrings.push_back(Path.str());
});
// Add specific to header type arguments.
if (Type == HeaderType::Project)
for (const StringRef A : ProjectLevelArgs)
ArgStrings.emplace_back(A);
}
} // namespace installapi
} // namespace clang
|