[PATCH] D129304: [clang-offload-bundler] Library-ize ClangOffloadBundler (3/4)

Jacob Lambert via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 7 09:22:52 PDT 2022


lamb-j created this revision.
lamb-j added reviewers: kzhuravl, scott.linder, yaxunl.
Herald added a reviewer: alexander-shaposhnikov.
Herald added a project: All.
lamb-j requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Lifting the core functionalities of the clang-offload-bundler into a
user-facing library/API.

This patch (3/4) lifts the BundlerExecutable variable, which is
specific to the clang-offload-bundler tool, from the API, and
replaces its use with an ObjcopyPath variable. This variable
must be set in order to internally call llvm-objcopy.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129304

Files:
  clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp
  clang/tools/clang-offload-bundler/OffloadBundler.cpp
  clang/tools/clang-offload-bundler/OffloadBundler.h


Index: clang/tools/clang-offload-bundler/OffloadBundler.h
===================================================================
--- clang/tools/clang-offload-bundler/OffloadBundler.h
+++ clang/tools/clang-offload-bundler/OffloadBundler.h
@@ -29,7 +29,7 @@
   unsigned HostInputIndex = ~0u;
 
   std::string FilesType;
-  std::string BundlerExecutable;
+  std::string ObjcopyPath;
 
   // TODO: Convert these to llvm::SmallVector
   std::vector<std::string> TargetNames;
Index: clang/tools/clang-offload-bundler/OffloadBundler.cpp
===================================================================
--- clang/tools/clang-offload-bundler/OffloadBundler.cpp
+++ clang/tools/clang-offload-bundler/OffloadBundler.cpp
@@ -587,15 +587,8 @@
     // link editor to remove them from linker inputs when linking executable or
     // shared library.
 
-    // Find llvm-objcopy in order to create the bundle binary.
-    ErrorOr<std::string> Objcopy = sys::findProgramByName(
-        "llvm-objcopy",
-	sys::path::parent_path(BundlerConfig->BundlerExecutable));
-    if (!Objcopy)
-      Objcopy = sys::findProgramByName("llvm-objcopy");
-    if (!Objcopy)
-      return createStringError(Objcopy.getError(),
-                               "unable to find 'llvm-objcopy' in path");
+    assert(BundlerConfig->ObjcopyPath != "" &&
+           "llvm-objcopy path not specified");
 
     // We write to the output file directly. So, we close it and use the name
     // to pass down to llvm-objcopy.
@@ -636,7 +629,7 @@
       BundlerConfig->InputFileNames[BundlerConfig->HostInputIndex]);
     ObjcopyArgs.push_back(BundlerConfig->OutputFileNames.front());
 
-    if (Error Err = executeObjcopy(*Objcopy, ObjcopyArgs))
+    if (Error Err = executeObjcopy(BundlerConfig->ObjcopyPath, ObjcopyArgs))
       return Err;
 
     return Error::success();
Index: clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp
===================================================================
--- clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp
+++ clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp
@@ -182,6 +182,16 @@
     BundlerExecutable =
       sys::fs::getMainExecutable(argv[0], &BundlerExecutable);
 
+  // Find llvm-objcopy in order to create the bundle binary.
+  ErrorOr<std::string> Objcopy = sys::findProgramByName(
+    "llvm-objcopy",
+    sys::path::parent_path(BundlerExecutable));
+  if (!Objcopy)
+    Objcopy = sys::findProgramByName("llvm-objcopy");
+  if (!Objcopy)
+    reportError(createStringError(Objcopy.getError(),
+                             "unable to find 'llvm-objcopy' in path"));
+
   if (InputFileNames.getNumOccurrences() != 0 &&
       InputFileNamesDeprecatedOpt.getNumOccurrences() != 0) {
     reportError(createStringError(
@@ -233,7 +243,7 @@
 
     Config BundlerPrint;
     BundlerPrint.PrintExternalCommands = PrintExternalCommands;
-    BundlerPrint.BundlerExecutable = BundlerExecutable;
+    BundlerPrint.ObjcopyPath = *Objcopy;
     doWork([&]() { return ListBundleIDsInFile(InputFileNames.front(),
                                              &BundlerPrint); });
     return 0;
@@ -294,7 +304,7 @@
 
     Config BundlerOpenmp;
     BundlerOpenmp.HipOpenmpCompatible = HipOpenmpCompatible;
-    BundlerOpenmp.BundlerExecutable = BundlerExecutable;
+    BundlerOpenmp.ObjcopyPath = *Objcopy;
     auto OffloadInfo = OffloadTargetInfo(Target, &BundlerOpenmp);
     bool KindIsValid = OffloadInfo.isOffloadKindValid();
     bool TripleIsValid = OffloadInfo.isTripleValid();
@@ -344,7 +354,7 @@
   BundlerConfig.BundleAlignment = BundleAlignment;
   BundlerConfig.HostInputIndex = HostInputIndex;
   BundlerConfig.FilesType = FilesType;
-  BundlerConfig.BundlerExecutable = BundlerExecutable;
+  BundlerConfig.ObjcopyPath = *Objcopy;
 
   BundlerConfig.TargetNames = TargetNames;
   BundlerConfig.InputFileNames = InputFileNames;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129304.442956.patch
Type: text/x-patch
Size: 3871 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220707/42a9702e/attachment-0001.bin>


More information about the cfe-commits mailing list