Index: lld/COFF/CMakeLists.txt =================================================================== --- lld/COFF/CMakeLists.txt +++ lld/COFF/CMakeLists.txt @@ -32,6 +32,7 @@ DebugInfoPDB LibDriver LTO + MachineFlag MC Object Option Index: lld/COFF/Driver.cpp =================================================================== --- lld/COFF/Driver.cpp +++ lld/COFF/Driver.cpp @@ -40,7 +40,7 @@ #include "llvm/Support/TarWriter.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/ToolDrivers/llvm-lib/LibDriver.h" +#include "llvm/ToolDrivers/MachineFlag/MachineFlag.h" #include #include #include Index: lld/COFF/SymbolTable.cpp =================================================================== --- lld/COFF/SymbolTable.cpp +++ lld/COFF/SymbolTable.cpp @@ -18,7 +18,7 @@ #include "llvm/IR/LLVMContext.h" #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/ToolDrivers/llvm-lib/LibDriver.h" +#include "llvm/ToolDrivers/MachineFlag/MachineFlag.h" #include using namespace llvm; Index: llvm/include/llvm/ToolDrivers/MachineFlag/MachineFlag.h =================================================================== --- /dev/null +++ llvm/include/llvm/ToolDrivers/MachineFlag/MachineFlag.h @@ -0,0 +1,33 @@ +//===- MachineFlag.h --------------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Functions for implementing the /machine: flag. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TOOLDRIVERS_MACHINEFLAG_MACHINEFLAG_H +#define LLVM_TOOLDRIVERS_MACHINEFLAG_MACHINEFLAG_H + +namespace llvm { + +class StringRef; +namespace COFF { +enum MachineTypes : unsigned; +} + +// Returns a user-readable string for ARMNT, ARM64, AMD64, I386. +// Other MachineTypes values must not be passed in. +StringRef machineToStr(COFF::MachineTypes MT); + +// Maps /machine: arguments to a MachineTypes value. +// Only returns ARMNT, ARM64, AMD64, I386, or IMAGE_FILE_MACHINE_UNKNOWN. +COFF::MachineTypes getMachineType(StringRef S); + +} + +#endif Index: llvm/include/llvm/ToolDrivers/llvm-lib/LibDriver.h =================================================================== --- llvm/include/llvm/ToolDrivers/llvm-lib/LibDriver.h +++ llvm/include/llvm/ToolDrivers/llvm-lib/LibDriver.h @@ -19,20 +19,6 @@ int libDriverMain(ArrayRef ARgs); - -class StringRef; -namespace COFF { -enum MachineTypes : unsigned; -} - -// Returns a user-readable string for ARMNT, ARM64, AMD64, I386. -// Other MachineTypes values must not be pased in. -StringRef machineToStr(COFF::MachineTypes MT); - -// Maps /machine: arguments to a MachineTypes value. -// Only returns ARMNT, ARM64, AMD64, I386, or IMAGE_FILE_MACHINE_UNKNOWN. -COFF::MachineTypes getMachineType(StringRef S); - } #endif Index: llvm/lib/ToolDrivers/CMakeLists.txt =================================================================== --- llvm/lib/ToolDrivers/CMakeLists.txt +++ llvm/lib/ToolDrivers/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(llvm-dlltool) add_subdirectory(llvm-lib) +add_subdirectory(MachineFlag) Index: llvm/lib/ToolDrivers/MachineFlag/CMakeLists.txt =================================================================== --- /dev/null +++ llvm/lib/ToolDrivers/MachineFlag/CMakeLists.txt @@ -0,0 +1,3 @@ +add_llvm_library(LLVMMachineFlag + MachineFlag.cpp + ) Index: llvm/lib/ToolDrivers/MachineFlag/LLVMBuild.txt =================================================================== --- /dev/null +++ llvm/lib/ToolDrivers/MachineFlag/LLVMBuild.txt @@ -0,0 +1,21 @@ +;===- ./lib/ToolDrivers/MachineFlag/LLVMBuild.txt --------------*- Conf -*--===; +; +; Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +; See https://llvm.org/LICENSE.txt for license information. +; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +; +;===------------------------------------------------------------------------===; +; +; This is an LLVMBuild description file for the components in this subdirectory. +; +; For more information on the LLVMBuild system, please see: +; +; http://llvm.org/docs/LLVMBuild.html +; +;===------------------------------------------------------------------------===; + +[component_0] +type = Library +name = MachineFlag +parent = Libraries +required_libraries = Index: llvm/lib/ToolDrivers/MachineFlag/MachineFlag.cpp =================================================================== --- /dev/null +++ llvm/lib/ToolDrivers/MachineFlag/MachineFlag.cpp @@ -0,0 +1,44 @@ +//===- MachineFlag.cpp ----------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Functions for implementing the /machine: flag. +// +//===----------------------------------------------------------------------===// + +#include "llvm/ToolDrivers/MachineFlag/MachineFlag.h" + +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSwitch.h" +#include "llvm/BinaryFormat/COFF.h" + +using namespace llvm; + +// Returns /machine's value. +COFF::MachineTypes llvm::getMachineType(StringRef S) { + return StringSwitch(S.lower()) + .Cases("x64", "amd64", COFF::IMAGE_FILE_MACHINE_AMD64) + .Cases("x86", "i386", COFF::IMAGE_FILE_MACHINE_I386) + .Case("arm", COFF::IMAGE_FILE_MACHINE_ARMNT) + .Case("arm64", COFF::IMAGE_FILE_MACHINE_ARM64) + .Default(COFF::IMAGE_FILE_MACHINE_UNKNOWN); +} + +StringRef llvm::machineToStr(COFF::MachineTypes MT) { + switch (MT) { + case COFF::IMAGE_FILE_MACHINE_ARMNT: + return "arm"; + case COFF::IMAGE_FILE_MACHINE_ARM64: + return "arm64"; + case COFF::IMAGE_FILE_MACHINE_AMD64: + return "x64"; + case COFF::IMAGE_FILE_MACHINE_I386: + return "x86"; + default: + llvm_unreachable("unknown machine type"); + } +} Index: llvm/lib/ToolDrivers/llvm-lib/CMakeLists.txt =================================================================== --- llvm/lib/ToolDrivers/llvm-lib/CMakeLists.txt +++ llvm/lib/ToolDrivers/llvm-lib/CMakeLists.txt @@ -1,6 +1,7 @@ set(LLVM_LINK_COMPONENTS BinaryFormat BitReader + MachineFlag Object Option Support Index: llvm/lib/ToolDrivers/llvm-lib/LLVMBuild.txt =================================================================== --- llvm/lib/ToolDrivers/llvm-lib/LLVMBuild.txt +++ llvm/lib/ToolDrivers/llvm-lib/LLVMBuild.txt @@ -18,4 +18,4 @@ type = Library name = LibDriver parent = Libraries -required_libraries = BinaryFormat BitReader Object Option Support +required_libraries = BinaryFormat BitReader MachineFlag Object Option Support Index: llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp =================================================================== --- llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp +++ llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp @@ -26,6 +26,7 @@ #include "llvm/Support/Process.h" #include "llvm/Support/StringSaver.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/ToolDrivers/MachineFlag/MachineFlag.h" using namespace llvm; @@ -140,31 +141,6 @@ fatalOpenError(std::move(Err), B->getBufferIdentifier()); } -// Returns /machine's value. -COFF::MachineTypes llvm::getMachineType(StringRef S) { - return StringSwitch(S.lower()) - .Cases("x64", "amd64", COFF::IMAGE_FILE_MACHINE_AMD64) - .Cases("x86", "i386", COFF::IMAGE_FILE_MACHINE_I386) - .Case("arm", COFF::IMAGE_FILE_MACHINE_ARMNT) - .Case("arm64", COFF::IMAGE_FILE_MACHINE_ARM64) - .Default(COFF::IMAGE_FILE_MACHINE_UNKNOWN); -} - -StringRef llvm::machineToStr(COFF::MachineTypes MT) { - switch (MT) { - case COFF::IMAGE_FILE_MACHINE_ARMNT: - return "arm"; - case COFF::IMAGE_FILE_MACHINE_ARM64: - return "arm64"; - case COFF::IMAGE_FILE_MACHINE_AMD64: - return "x64"; - case COFF::IMAGE_FILE_MACHINE_I386: - return "x86"; - default: - llvm_unreachable("unknown machine type"); - } -} - int llvm::libDriverMain(ArrayRef ArgsArr) { BumpPtrAllocator Alloc; StringSaver Saver(Alloc); Index: llvm/tools/llvm-cvtres/CMakeLists.txt =================================================================== --- llvm/tools/llvm-cvtres/CMakeLists.txt +++ llvm/tools/llvm-cvtres/CMakeLists.txt @@ -1,6 +1,7 @@ set(LLVM_LINK_COMPONENTS Object Option + MachineFlag Support ) Index: llvm/tools/llvm-cvtres/LLVMBuild.txt =================================================================== --- llvm/tools/llvm-cvtres/LLVMBuild.txt +++ llvm/tools/llvm-cvtres/LLVMBuild.txt @@ -18,4 +18,4 @@ type = Tool name = llvm-cvtres parent = Tools -required_libraries = Object Option Support +required_libraries = MachineFlag Object Option Support Index: llvm/tools/llvm-cvtres/llvm-cvtres.cpp =================================================================== --- llvm/tools/llvm-cvtres/llvm-cvtres.cpp +++ llvm/tools/llvm-cvtres/llvm-cvtres.cpp @@ -27,6 +27,7 @@ #include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/Signals.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/ToolDrivers/MachineFlag/MachineFlag.h" #include @@ -111,16 +112,12 @@ COFF::MachineTypes MachineType; - if (InputArgs.hasArg(OPT_MACHINE)) { - std::string MachineString = InputArgs.getLastArgValue(OPT_MACHINE).upper(); - MachineType = StringSwitch(MachineString) - .Case("ARM", COFF::IMAGE_FILE_MACHINE_ARMNT) - .Case("ARM64", COFF::IMAGE_FILE_MACHINE_ARM64) - .Case("X64", COFF::IMAGE_FILE_MACHINE_AMD64) - .Case("X86", COFF::IMAGE_FILE_MACHINE_I386) - .Default(COFF::IMAGE_FILE_MACHINE_UNKNOWN); - if (MachineType == COFF::IMAGE_FILE_MACHINE_UNKNOWN) - reportError("Unsupported machine architecture"); + if (opt::Arg *Arg = InputArgs.getLastArg(OPT_MACHINE)) { + MachineType = getMachineType(Arg->getValue()); + if (MachineType == COFF::IMAGE_FILE_MACHINE_UNKNOWN) { + reportError(Twine("Unsupported machine architecture ") + Arg->getValue() + + "\n"); + } } else { if (Verbose) outs() << "Machine architecture not specified; assumed X64.\n"; @@ -135,29 +132,15 @@ SmallString<128> OutputFile; - if (InputArgs.hasArg(OPT_OUT)) { - OutputFile = InputArgs.getLastArgValue(OPT_OUT); + if (opt::Arg *Arg = InputArgs.getLastArg(OPT_OUT)) { + OutputFile = Arg->getValue(); } else { OutputFile = sys::path::filename(StringRef(InputFiles[0])); sys::path::replace_extension(OutputFile, ".obj"); } - if (Verbose) { - outs() << "Machine: "; - switch (MachineType) { - case COFF::IMAGE_FILE_MACHINE_ARM64: - outs() << "ARM64\n"; - break; - case COFF::IMAGE_FILE_MACHINE_ARMNT: - outs() << "ARM\n"; - break; - case COFF::IMAGE_FILE_MACHINE_I386: - outs() << "X86\n"; - break; - default: - outs() << "X64\n"; - } - } + if (Verbose) + outs() << "Machine: " << machineToStr(MachineType) << '\n'; WindowsResourceParser Parser; Index: llvm/utils/gn/secondary/lld/COFF/BUILD.gn =================================================================== --- llvm/utils/gn/secondary/lld/COFF/BUILD.gn +++ llvm/utils/gn/secondary/lld/COFF/BUILD.gn @@ -22,6 +22,7 @@ "//llvm/lib/Option", "//llvm/lib/Support", "//llvm/lib/Target:TargetsToBuild", + "//llvm/lib/ToolDrivers/MachineFlag", "//llvm/lib/ToolDrivers/llvm-lib:LibDriver", "//llvm/lib/WindowsManifest", ] Index: llvm/utils/gn/secondary/llvm/lib/ToolDrivers/MachineFlag/BUILD.gn =================================================================== --- /dev/null +++ llvm/utils/gn/secondary/llvm/lib/ToolDrivers/MachineFlag/BUILD.gn @@ -0,0 +1,6 @@ +static_library("MachineFlag") { + output_name = "LLVMMachineFlag" + sources = [ + "MachineFlag.cpp", + ] +} Index: llvm/utils/gn/secondary/llvm/lib/ToolDrivers/llvm-lib/BUILD.gn =================================================================== --- llvm/utils/gn/secondary/llvm/lib/ToolDrivers/llvm-lib/BUILD.gn +++ llvm/utils/gn/secondary/llvm/lib/ToolDrivers/llvm-lib/BUILD.gn @@ -14,6 +14,7 @@ "//llvm/lib/Object", "//llvm/lib/Option", "//llvm/lib/Support", + "//llvm/lib/ToolDrivers/MachineFlag", ] sources = [ "LibDriver.cpp", Index: llvm/utils/gn/secondary/llvm/tools/llvm-cvtres/BUILD.gn =================================================================== --- llvm/utils/gn/secondary/llvm/tools/llvm-cvtres/BUILD.gn +++ llvm/utils/gn/secondary/llvm/tools/llvm-cvtres/BUILD.gn @@ -11,6 +11,7 @@ "//llvm/lib/Object", "//llvm/lib/Option", "//llvm/lib/Support", + "//llvm/lib/ToolDrivers/MachineFlag", ] sources = [ "llvm-cvtres.cpp",