1 //===-- MachOUtils.h - Mach-o specific helpers for dsymutil --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MachOUtils.h"
12 #include "llvm/Support/FileUtilities.h"
13 #include "llvm/Support/Program.h"
14 #include "llvm/Support/raw_ostream.h"
18 namespace MachOUtils {
20 static bool runLipo(SmallVectorImpl<const char *> &Args) {
21 auto Path = sys::findProgramByName("lipo");
24 errs() << "error: lipo: " << Path.getError().message() << "\n";
30 sys::ExecuteAndWait(*Path, Args.data(), nullptr, nullptr, 0, 0, &ErrMsg);
32 errs() << "error: lipo: " << ErrMsg << "\n";
39 bool generateUniversalBinary(SmallVectorImpl<ArchAndFilename> &ArchFiles,
40 StringRef OutputFileName,
41 const LinkOptions &Options) {
42 // No need to merge one file into a universal fat binary. First, try
43 // to move it (rename) to the final location. If that fails because
44 // of cross-device link issues then copy and delete.
45 if (ArchFiles.size() == 1) {
46 StringRef From(ArchFiles.front().Path);
47 if (sys::fs::rename(From, OutputFileName)) {
48 if (std::error_code EC = sys::fs::copy_file(From, OutputFileName)) {
49 errs() << "error: while copying " << From << " to " << OutputFileName
50 << ": " << EC.message() << "\n";
53 sys::fs::remove(From);
58 SmallVector<const char *, 8> Args;
59 Args.push_back("lipo");
60 Args.push_back("-create");
62 for (auto &Thin : ArchFiles)
63 Args.push_back(Thin.Path.c_str());
65 // Align segments to match dsymutil-classic alignment
66 for (auto &Thin : ArchFiles) {
67 Args.push_back("-segalign");
68 Args.push_back(Thin.Arch.c_str());
72 Args.push_back("-output");
73 Args.push_back(OutputFileName.data());
74 Args.push_back(nullptr);
76 if (Options.Verbose) {
77 outs() << "Running lipo\n";
79 outs() << ' ' << ((Arg == nullptr) ? "\n" : Arg);
82 return Options.NoOutput ? true : runLipo(Args);