diff --git a/llvm/test/tools/llvm-objcopy/llvm-options.test b/llvm/test/tools/llvm-objcopy/llvm-options.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/llvm-options.test @@ -0,0 +1,8 @@ +# Verify that internal llvm options (such as `-debug`) can be injected via +# the LLVM_INTERNAL_OPTS environment variable. + +RUN: llvm-objcopy --version 2>&1 | FileCheck %s +RUN: env LLVM_INTERNAL_OPTS=-debug llvm-objcopy --version 2>&1 | FileCheck --check-prefix=DEBUG %s + +CHECK-NOT: running as +DEBUG: running as objcopy diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp --- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp @@ -38,6 +38,7 @@ #include "llvm/Option/Option.h" #include "llvm/Support/Casting.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Debug.h" #include "llvm/Support/Errc.h" #include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" @@ -63,6 +64,8 @@ using namespace llvm::objcopy; using namespace llvm::object; +#define DEBUG_TYPE "objcopy" + // The name this program was invoked as. static StringRef ToolName; @@ -86,14 +89,20 @@ (I + Tool.size() == Stem.size() || !isAlnum(Stem[I + Tool.size()])); }; - if (Is("bitcode-strip") || Is("bitcode_strip")) + if (Is("bitcode-strip") || Is("bitcode_strip")) { + LLVM_DEBUG(llvm::dbgs() << "running as bitcode-strip\n"); return parseBitcodeStripOptions(Args); - else if (Is("strip")) + } + if (Is("strip")) { + LLVM_DEBUG(llvm::dbgs() << "running as strip\n"); return parseStripOptions(Args, reportWarning); - else if (Is("install-name-tool") || Is("install_name_tool")) + } + if (Is("install-name-tool") || Is("install_name_tool")) { + LLVM_DEBUG(llvm::dbgs() << "running as install-name-tool\n"); return parseInstallNameToolOptions(Args); - else - return parseObjcopyOptions(Args, reportWarning); + } + LLVM_DEBUG(llvm::dbgs() << "running as objcopy\n"); + return parseObjcopyOptions(Args, reportWarning); } // For regular archives this function simply calls llvm::writeArchive, @@ -417,6 +426,10 @@ ? cl::TokenizeWindowsCommandLine : cl::TokenizeGNUCommandLine, NewArgv); + // Allow llvm options to be passed via $LLVM_INTERNAL_OPTS. This is useful + // for enabling common flags such as -debug. + cl::ParseCommandLineOptions(1, argv, "llvm-objcopy", nullptr, + "LLVM_INTERNAL_OPTS"); auto Args = makeArrayRef(NewArgv).drop_front(); Expected DriverConfig = getDriverConfig(Args);