|
41 | 41 | #include "llvm/Support/LLVMDriver.h" |
42 | 42 | #include "llvm/Support/MemoryBuffer.h" |
43 | 43 | #include "llvm/Support/Path.h" |
| 44 | +#include "llvm/Support/Program.h" |
44 | 45 | #include "llvm/Support/TargetSelect.h" |
45 | 46 | #include "llvm/Support/ThreadPool.h" |
46 | 47 | #include "llvm/Support/WithColor.h" |
@@ -116,6 +117,7 @@ struct DsymutilOptions { |
116 | 117 | bool NoObjectTimestamp = false; |
117 | 118 | std::string OutputFile; |
118 | 119 | std::string Toolchain; |
| 120 | + std::string CodesignIdentity; |
119 | 121 | std::string ReproducerPath; |
120 | 122 | std::string AllowFile; |
121 | 123 | std::string DisallowFile; |
@@ -221,6 +223,16 @@ static Error verifyOptions(const DsymutilOptions &Options) { |
221 | 223 | "--embed-resource is not supported with --flat", |
222 | 224 | errc::invalid_argument); |
223 | 225 |
|
| 226 | + if (!Options.CodesignIdentity.empty() && Options.Flat) |
| 227 | + return make_error<StringError>( |
| 228 | + "--codesign is not supported with --flat: no bundle to sign", |
| 229 | + errc::invalid_argument); |
| 230 | + |
| 231 | + if (!Options.CodesignIdentity.empty() && Options.LinkOpts.NoOutput) |
| 232 | + return make_error<StringError>( |
| 233 | + "--codesign is not supported with --no-output: nothing to sign", |
| 234 | + errc::invalid_argument); |
| 235 | + |
224 | 236 | return Error::success(); |
225 | 237 | } |
226 | 238 |
|
@@ -388,6 +400,9 @@ static Expected<DsymutilOptions> getOptions(opt::InputArgList &Args) { |
388 | 400 | if (opt::Arg *Toolchain = Args.getLastArg(OPT_toolchain)) |
389 | 401 | Options.Toolchain = Toolchain->getValue(); |
390 | 402 |
|
| 403 | + if (opt::Arg *Codesign = Args.getLastArg(OPT_codesign)) |
| 404 | + Options.CodesignIdentity = Codesign->getValue(); |
| 405 | + |
391 | 406 | if (Args.hasArg(OPT_assembly)) |
392 | 407 | Options.LinkOpts.FileType = DWARFLinkerBase::OutputFileType::Assembly; |
393 | 408 |
|
@@ -657,6 +672,33 @@ getOutputFileName(StringRef InputFile, const DsymutilOptions &Options) { |
657 | 672 | return OutputLocation(std::string(Path), ResourceDir); |
658 | 673 | } |
659 | 674 |
|
| 675 | +static Error codesignBundle(StringRef BundlePath, StringRef Identity, |
| 676 | + StringRef SDKPath) { |
| 677 | + auto Path = sys::findProgramByName("codesign", ArrayRef(SDKPath)); |
| 678 | + if (!Path) |
| 679 | + Path = sys::findProgramByName("codesign"); |
| 680 | + |
| 681 | + if (!Path) |
| 682 | + return make_error<StringError>( |
| 683 | + "codesign not found: " + Path.getError().message(), Path.getError()); |
| 684 | + |
| 685 | + SmallVector<StringRef, 5> Args; |
| 686 | + Args.push_back("codesign"); |
| 687 | + Args.push_back("-f"); |
| 688 | + Args.push_back("-s"); |
| 689 | + Args.push_back(Identity); |
| 690 | + Args.push_back(BundlePath); |
| 691 | + |
| 692 | + std::string ErrMsg; |
| 693 | + int Result = |
| 694 | + sys::ExecuteAndWait(*Path, Args, std::nullopt, {}, 0, 0, &ErrMsg); |
| 695 | + if (Result) |
| 696 | + return make_error<StringError>("codesign failed: " + ErrMsg, |
| 697 | + inconvertibleErrorCode()); |
| 698 | + |
| 699 | + return Error::success(); |
| 700 | +} |
| 701 | + |
660 | 702 | int dsymutil_main(int argc, char **argv, const llvm::ToolContext &) { |
661 | 703 | // Parse arguments. |
662 | 704 | DsymutilOptTable T; |
@@ -985,6 +1027,21 @@ int dsymutil_main(int argc, char **argv, const llvm::ToolContext &) { |
985 | 1027 | SDKPath, Fat64)) |
986 | 1028 | return EXIT_FAILURE; |
987 | 1029 | } |
| 1030 | + |
| 1031 | + if (!Options.CodesignIdentity.empty()) { |
| 1032 | + StringRef DWARFFile = OutputLocationOrErr->DWARFFile; |
| 1033 | + auto Pos = DWARFFile.find(".dSYM/"); |
| 1034 | + if (Pos == StringRef::npos) |
| 1035 | + Pos = DWARFFile.find(".dSYM"); |
| 1036 | + if (Pos != StringRef::npos) { |
| 1037 | + std::string BundlePath = DWARFFile.substr(0, Pos + 5).str(); |
| 1038 | + if (auto E = |
| 1039 | + codesignBundle(BundlePath, Options.CodesignIdentity, SDKPath)) { |
| 1040 | + WithColor::error() << toString(std::move(E)) << '\n'; |
| 1041 | + return EXIT_FAILURE; |
| 1042 | + } |
| 1043 | + } |
| 1044 | + } |
988 | 1045 | } |
989 | 1046 |
|
990 | 1047 | return EXIT_SUCCESS; |
|
0 commit comments