[pkg-d-commits] [ldc] 55/74: Move static lib generation from driver/linker.cpp to driver/archiver.cpp

Matthias Klumpp mak at moszumanska.debian.org
Thu Jul 13 20:54:18 UTC 2017


This is an automated email from the git hooks/post-receive script.

mak pushed a commit to annotated tag v1.3.0-beta2
in repository ldc.

commit ec1a54aadea74389d5cab10e2deca09c75630b65
Author: Martin <noone at nowhere.com>
Date:   Fri May 26 21:45:53 2017 +0200

    Move static lib generation from driver/linker.cpp to driver/archiver.cpp
---
 ddmd/mars.d         |   3 +-
 driver/archiver.cpp | 151 +++++++++++++++++++++++++++++++++++++++++++++-------
 driver/archiver.h   |  20 +++----
 driver/linker.cpp   | 121 +----------------------------------------
 driver/linker.h     |   6 ---
 driver/tool.cpp     |  14 +++++
 driver/tool.h       |   2 +
 7 files changed, 159 insertions(+), 158 deletions(-)

diff --git a/ddmd/mars.d b/ddmd/mars.d
index 8bd8a9a..c4e2802 100644
--- a/ddmd/mars.d
+++ b/ddmd/mars.d
@@ -64,9 +64,10 @@ version(IN_LLVM)
     // in driver/main.cpp
     void addDefaultVersionIdentifiers();
     void codegenModules(ref Modules modules);
+    // in driver/archiver.cpp
+    int createStaticLibrary();
     // in driver/linker.cpp
     int linkObjToBinary();
-    int createStaticLibrary();
     void deleteExeFile();
     int runProgram();
 }
diff --git a/driver/archiver.cpp b/driver/archiver.cpp
index 551e023..eb75833 100644
--- a/driver/archiver.cpp
+++ b/driver/archiver.cpp
@@ -6,17 +6,16 @@
 // License. See LLVM LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
-//
-// Builds up (relatively) standard unix archive files (.a) containing LLVM
-// bitcode or other files.
-//
-//===----------------------------------------------------------------------===//
 
-#if LDC_LLVM_VER >= 309
+#include "errors.h"
+#include "globals.h"
+#include "driver/cl_options.h"
+#include "driver/tool.h"
+#include "gen/logger.h"
+#include "llvm/ADT/Triple.h"
 
-#include "driver/archiver.h"
+#if LDC_LLVM_VER >= 309
 
-#include "llvm/ADT/Triple.h"
 #include "llvm/Object/Archive.h"
 #include "llvm/Object/ArchiveWriter.h"
 #include "llvm/Object/MachO.h"
@@ -42,7 +41,7 @@ using namespace llvm;
  * support for `llvm-ar rcs <archive name> <member> ...`.
  * It also makes sure the process isn't simply exited whenever a problem arises.
  */
-namespace {
+namespace llvm_ar {
 
 StringRef ArchiveName;
 std::vector<const char *> Members;
@@ -237,13 +236,13 @@ int performWriteOperation() {
   return performWriteOperation(nullptr, nullptr);
 }
 
-} // anonymous namespace
+} // namespace llvm_ar
 
 ////////////////////////////////////////////////////////////////////////////////
 
-namespace ldc {
+namespace {
 
-int ar(ArrayRef<const char *> args) {
+int internalAr(ArrayRef<const char *> args) {
   if (args.size() < 4 || strcmp(args[0], "llvm-ar") != 0 ||
       strcmp(args[1], "rcs") != 0) {
     llvm_unreachable(
@@ -252,16 +251,17 @@ int ar(ArrayRef<const char *> args) {
     return -1;
   }
 
-  ArchiveName = args[2];
+  llvm_ar::ArchiveName = args[2];
 
   auto membersSlice = args.slice(3);
-  Members.clear();
-  Members.insert(Members.end(), membersSlice.begin(), membersSlice.end());
+  llvm_ar::Members.clear();
+  llvm_ar::Members.insert(llvm_ar::Members.end(), membersSlice.begin(),
+                          membersSlice.end());
 
-  return performWriteOperation();
+  return llvm_ar::performWriteOperation();
 }
 
-int lib(ArrayRef<const char *> args) {
+int internalLib(ArrayRef<const char *> args) {
   if (args.size() < 1 || strcmp(args[0], "llvm-lib.exe") != 0) {
     llvm_unreachable("Expected archiver command line: llvm-lib.exe ...");
     return -1;
@@ -270,6 +270,121 @@ int lib(ArrayRef<const char *> args) {
   return libDriverMain(args);
 }
 
-} // namespace ldc
+} // anonymous namespace
 
 #endif // LDC_LLVM_VER >= 309
+
+////////////////////////////////////////////////////////////////////////////////
+
+static llvm::cl::opt<std::string> ar("ar", llvm::cl::desc("Archiver"),
+                                     llvm::cl::Hidden, llvm::cl::ZeroOrMore);
+
+int createStaticLibrary() {
+  Logger::println("*** Creating static library ***");
+
+  const bool isTargetMSVC =
+      global.params.targetTriple->isWindowsMSVCEnvironment();
+
+#if LDC_LLVM_VER >= 309
+  const bool useInternalArchiver = ar.empty();
+#else
+  const bool useInternalArchiver = false;
+#endif
+
+  // find archiver
+  std::string tool;
+  if (useInternalArchiver) {
+    tool = isTargetMSVC ? "llvm-lib.exe" : "llvm-ar";
+  } else {
+#ifdef _WIN32
+    if (isTargetMSVC)
+      windows::setupMsvcEnvironment();
+#endif
+
+    tool = getProgram(isTargetMSVC ? "lib.exe" : "ar", &ar);
+  }
+
+  // build arguments
+  std::vector<std::string> args;
+
+  // ask ar to create a new library
+  if (!isTargetMSVC) {
+    args.push_back("rcs");
+  }
+
+  // ask lib.exe to be quiet
+  if (isTargetMSVC) {
+    args.push_back("/NOLOGO");
+  }
+
+  // output filename
+  std::string libName;
+  if (global.params.libname) { // explicit
+    // DMD adds the default extension if there is none
+    libName = opts::invokedByLDMD
+                  ? FileName::defaultExt(global.params.libname, global.lib_ext)
+                  : global.params.libname;
+  } else { // infer from first object file
+    libName = global.params.objfiles->dim
+                  ? FileName::removeExt((*global.params.objfiles)[0])
+                  : "a.out";
+    libName += '.';
+    libName += global.lib_ext;
+  }
+
+  // DMD creates static libraries in the objects directory (unless using an
+  // absolute output path via `-of`).
+  if (opts::invokedByLDMD && global.params.objdir &&
+      !FileName::absolute(libName.c_str())) {
+    libName = FileName::combine(global.params.objdir, libName.c_str());
+  }
+
+  if (isTargetMSVC) {
+    args.push_back("/OUT:" + libName);
+  } else {
+    args.push_back(libName);
+  }
+
+  // object files
+  for (unsigned i = 0; i < global.params.objfiles->dim; i++)
+    args.push_back((*global.params.objfiles)[i]);
+
+  // .res/.def files for lib.exe
+  if (isTargetMSVC) {
+    if (global.params.resfile)
+      args.push_back(global.params.resfile);
+    if (global.params.deffile)
+      args.push_back(std::string("/DEF:") + global.params.deffile);
+  }
+
+  // create path to the library
+  createDirectoryForFileOrFail(libName);
+
+#if LDC_LLVM_VER >= 309
+  if (useInternalArchiver) {
+    std::vector<const char *> fullArgs;
+    fullArgs.reserve(1 + args.size());
+    fullArgs.push_back(tool.c_str());
+    for (const auto &arg : args)
+      fullArgs.push_back(arg.c_str());
+
+    if (global.params.verbose) {
+      for (auto arg : fullArgs) {
+        fprintf(global.stdmsg, "%s ", arg);
+      }
+      fprintf(global.stdmsg, "\n");
+      fflush(global.stdmsg);
+    }
+
+    const int exitCode =
+        isTargetMSVC ? internalLib(fullArgs) : internalAr(fullArgs);
+    if (exitCode)
+      error(Loc(), "%s failed with status: %d", tool.c_str(), exitCode);
+
+    return exitCode;
+  }
+#endif
+
+  // invoke external archiver
+  return executeToolAndWait(tool, args, global.params.verbose);
+}
diff --git a/driver/archiver.h b/driver/archiver.h
index 32e1477..64a1b19 100644
--- a/driver/archiver.h
+++ b/driver/archiver.h
@@ -1,4 +1,4 @@
-//===-- driver/archiver.h - Creating static libs via LLVM--------*- C++ -*-===//
+//===-- driver/archiver.h - Creating static libraries -----------*- C++ -*-===//
 //
 //                         LDC � the LLVM D compiler
 //
@@ -6,22 +6,14 @@
 // file for details.
 //
 //===----------------------------------------------------------------------===//
-//
-// Provides an interface to LLVM built-in static lib generation via llvm-lib
-// (MSVC targets) or llvm-ar (all other targets).
-//
-//===----------------------------------------------------------------------===//
 
 #ifndef LDC_DRIVER_ARCHIVER_H
 #define LDC_DRIVER_ARCHIVER_H
 
-#if LDC_LLVM_VER >= 309
-#include "llvm/ADT/ArrayRef.h"
-
-namespace ldc {
-int ar(llvm::ArrayRef<const char *> args);
-int lib(llvm::ArrayRef<const char *> args);
-}
-#endif // LDC_LLVM_VER >= 309
+/**
+ * Create a static library from object files.
+ * @return 0 on success.
+ */
+int createStaticLibrary();
 
 #endif // !LDC_DRIVER_ARCHIVER_H
diff --git a/driver/linker.cpp b/driver/linker.cpp
index 57215db..d9fbafb 100644
--- a/driver/linker.cpp
+++ b/driver/linker.cpp
@@ -49,22 +49,6 @@ static llvm::cl::opt<std::string>
                               "LLVMgold.so (Unixes) or libLTO.dylib (Darwin))"),
                llvm::cl::value_desc("file"));
 
-static llvm::cl::opt<std::string> ar("ar", llvm::cl::desc("Archiver"),
-                                     llvm::cl::Hidden, llvm::cl::ZeroOrMore);
-
-//////////////////////////////////////////////////////////////////////////////
-
-static void CreateDirectoryOnDisk(llvm::StringRef fileName) {
-  auto dir = llvm::sys::path::parent_path(fileName);
-  if (!dir.empty() && !llvm::sys::fs::exists(dir)) {
-    if (auto ec = llvm::sys::fs::create_directories(dir)) {
-      error(Loc(), "failed to create path to file: %s\n%s", dir.data(),
-            ec.message().c_str());
-      fatal();
-    }
-  }
-}
-
 //////////////////////////////////////////////////////////////////////////////
 
 static std::string getOutputName(bool const sharedLib) {
@@ -335,7 +319,7 @@ static int linkObjToBinaryGcc(bool sharedLib) {
   // assert(gExePath.isValid());
 
   // create path to exe
-  CreateDirectoryOnDisk(gExePath);
+  createDirectoryForFileOrFail(gExePath);
 
   // Pass sanitizer arguments to linker. Requires clang.
   if (opts::sanitize == opts::AddressSanitizer) {
@@ -605,7 +589,7 @@ static int linkObjToBinaryMSVC(bool sharedLib) {
   // assert(gExePath.isValid());
 
   // create path to exe
-  CreateDirectoryOnDisk(gExePath);
+  createDirectoryForFileOrFail(gExePath);
 
   // additional linker switches
   auto addSwitch = [&](std::string str) {
@@ -667,107 +651,6 @@ int linkObjToBinary() {
 
 //////////////////////////////////////////////////////////////////////////////
 
-int createStaticLibrary() {
-  Logger::println("*** Creating static library ***");
-
-  const bool isTargetMSVC =
-      global.params.targetTriple->isWindowsMSVCEnvironment();
-
-#if LDC_LLVM_VER >= 309
-  const bool useInternalArchiver = ar.empty();
-#else
-  const bool useInternalArchiver = false;
-#endif
-
-  // find archiver
-  std::string tool;
-  if (useInternalArchiver) {
-    tool = isTargetMSVC ? "llvm-lib.exe" : "llvm-ar";
-  } else {
-#ifdef _WIN32
-    if (isTargetMSVC)
-      windows::setupMsvcEnvironment();
-#endif
-
-    tool = getProgram(isTargetMSVC ? "lib.exe" : "ar", &ar);
-  }
-
-  // build arguments
-  std::vector<std::string> args;
-
-  // ask ar to create a new library
-  if (!isTargetMSVC) {
-    args.push_back("rcs");
-  }
-
-  // ask lib to be quiet
-  if (isTargetMSVC) {
-    args.push_back("/NOLOGO");
-  }
-
-  // output filename
-  std::string libName;
-  if (global.params.libname) { // explicit
-    // DMD adds the default extension if there is none
-    libName = opts::invokedByLDMD
-                  ? FileName::defaultExt(global.params.libname, global.lib_ext)
-                  : global.params.libname;
-  } else { // infer from first object file
-    libName = global.params.objfiles->dim
-                  ? FileName::removeExt((*global.params.objfiles)[0])
-                  : "a.out";
-    libName += '.';
-    libName += global.lib_ext;
-  }
-
-  // DMD creates static libraries in the objects directory (unless using an
-  // absolute output path via `-of`).
-  if (opts::invokedByLDMD && global.params.objdir &&
-      !FileName::absolute(libName.c_str())) {
-    libName = FileName::combine(global.params.objdir, libName.c_str());
-  }
-
-  if (isTargetMSVC) {
-    args.push_back("/OUT:" + libName);
-  } else {
-    args.push_back(libName);
-  }
-
-  appendObjectFiles(args);
-
-  // create path to the library
-  CreateDirectoryOnDisk(libName);
-
-#if LDC_LLVM_VER >= 309
-  if (useInternalArchiver) {
-    std::vector<const char *> fullArgs;
-    fullArgs.reserve(1 + args.size());
-    fullArgs.push_back(tool.c_str());
-    for (const auto &arg : args)
-      fullArgs.push_back(arg.c_str());
-
-    if (global.params.verbose) {
-      for (auto arg : fullArgs) {
-        fprintf(global.stdmsg, "%s ", arg);
-      }
-      fprintf(global.stdmsg, "\n");
-      fflush(global.stdmsg);
-    }
-
-    const int exitCode = isTargetMSVC ? ldc::lib(fullArgs) : ldc::ar(fullArgs);
-    if (exitCode)
-      error(Loc(), "%s failed with status: %d", tool.c_str(), exitCode);
-
-    return exitCode;
-  }
-#endif
-
-  // try to call archiver
-  return executeToolAndWait(tool, args, global.params.verbose);
-}
-
-//////////////////////////////////////////////////////////////////////////////
-
 void deleteExeFile() {
   if (!gExePath.empty() && !llvm::sys::fs::is_directory(gExePath)) {
     llvm::sys::fs::remove(gExePath);
diff --git a/driver/linker.h b/driver/linker.h
index 9c6d9c0..e731893 100644
--- a/driver/linker.h
+++ b/driver/linker.h
@@ -35,12 +35,6 @@ void insertBitcodeFiles(llvm::Module &M, llvm::LLVMContext &Ctx,
 int linkObjToBinary();
 
 /**
- * Create a static library from object files.
- * @return 0 on success.
- */
-int createStaticLibrary();
-
-/**
  * Delete the executable that was previously linked with linkObjToBinary.
  */
 void deleteExeFile();
diff --git a/driver/tool.cpp b/driver/tool.cpp
index 8d54b21..73c1f6c 100644
--- a/driver/tool.cpp
+++ b/driver/tool.cpp
@@ -13,6 +13,7 @@
 #include "llvm/Support/ConvertUTF.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Path.h"
 #include "llvm/Support/Program.h"
 #ifdef _WIN32
 #include <Windows.h>
@@ -73,6 +74,19 @@ std::string getGcc() {
 #endif
 }
 
+//////////////////////////////////////////////////////////////////////////////
+
+void createDirectoryForFileOrFail(llvm::StringRef fileName) {
+  auto dir = llvm::sys::path::parent_path(fileName);
+  if (!dir.empty() && !llvm::sys::fs::exists(dir)) {
+    if (auto ec = llvm::sys::fs::create_directories(dir)) {
+      error(Loc(), "failed to create path to file: %s\n%s", dir.data(),
+            ec.message().c_str());
+      fatal();
+    }
+  }
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 
 int executeToolAndWait(const std::string &tool_,
diff --git a/driver/tool.h b/driver/tool.h
index 4482621..bdda989 100644
--- a/driver/tool.h
+++ b/driver/tool.h
@@ -26,6 +26,8 @@ std::string getProgram(const char *name,
                        const llvm::cl::opt<std::string> *opt = nullptr,
                        const char *envVar = nullptr);
 
+void createDirectoryForFileOrFail(llvm::StringRef fileName);
+
 int executeToolAndWait(const std::string &tool,
                        std::vector<std::string> const &args,
                        bool verbose = false);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-d/ldc.git



More information about the pkg-d-commits mailing list