From 2e27459d6c016cd0e49151fb3cd0c5c64b858412 Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Tue, 13 Aug 2013 23:38:57 +0000 Subject: [PATCH] clang-cl: Support /link option and set target to win32 This adds support for the /link option, which forwards subsequent arguments to the linker. The test for this will only work when targetting win32. Since that's the only target where clang-cl makes sense, use that target by default. Differential Revision: http://llvm-reviews.chandlerc.com/D1388 llvm-svn: 188331 --- clang/include/clang/Driver/CLCompatOptions.td | 5 +++++ clang/lib/Driver/Driver.cpp | 6 ++++++ clang/lib/Driver/Tools.cpp | 2 ++ clang/test/Driver/cl-link.c | 12 ++++++++++++ 4 files changed, 25 insertions(+) create mode 100644 clang/test/Driver/cl-link.c diff --git a/clang/include/clang/Driver/CLCompatOptions.td b/clang/include/clang/Driver/CLCompatOptions.td index a4b8b61e2b4d..083686d0e84c 100644 --- a/clang/include/clang/Driver/CLCompatOptions.td +++ b/clang/include/clang/Driver/CLCompatOptions.td @@ -32,6 +32,9 @@ class CLIgnoredJoined : Option<["/", "-"], name, KIND_JOINED>, class CLJoinedOrSeparate : Option<["/", "-"], name, KIND_JOINED_OR_SEPARATE>, Group, Flags<[CLOption, DriverOption]>; +class CLRemainingArgs : Option<["/", "-"], name, + KIND_REMAINING_ARGS>, Group, Flags<[CLOption, DriverOption]>; + // Aliases: def _SLASH_c : CLFlag<"c">, HelpText<"Compile only">, Alias; @@ -96,6 +99,8 @@ def _SLASH_Fe : CLJoined<"Fe">, def _SLASH_Fo : CLJoined<"Fo">, HelpText<"Set output object file, or directory (ends in / or \\)">, MetaVarName<"">; +def _SLASH_link : CLRemainingArgs<"link">, + HelpText<"Forward options to the linker">, MetaVarName<"">; def _SLASH_MD : CLFlag<"MD">, HelpText<"Use DLL run-time">; def _SLASH_MDd : CLFlag<"MDd">, diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 68958047ba73..32a7d1aafd3f 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -351,6 +351,12 @@ Compilation *Driver::BuildCompilation(ArrayRef ArgList) { options::OPT_ccc_pch_is_pth); // FIXME: DefaultTargetTriple is used by the target-prefixed calls to as/ld // and getToolChain is const. + if (IsCLMode()) { + // clang-cl targets Win32. + llvm::Triple T(DefaultTargetTriple); + T.setOSName(llvm::Triple::getOSTypeName(llvm::Triple::Win32)); + DefaultTargetTriple = T.str(); + } if (const Arg *A = Args->getLastArg(options::OPT_target)) DefaultTargetTriple = A->getValue(); if (const Arg *A = Args->getLastArg(options::OPT_ccc_install_dir)) diff --git a/clang/lib/Driver/Tools.cpp b/clang/lib/Driver/Tools.cpp index 2ae23aec8e15..5dd6fa1eacca 100644 --- a/clang/lib/Driver/Tools.cpp +++ b/clang/lib/Driver/Tools.cpp @@ -6590,12 +6590,14 @@ void visualstudio::Link::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-nologo"); Args.AddAllArgValues(CmdArgs, options::OPT_l); + Args.AddAllArgValues(CmdArgs, options::OPT__SLASH_link); // Add filenames immediately. for (InputInfoList::const_iterator it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { if (it->isFilename()) CmdArgs.push_back(it->getFilename()); + // FIXME: Forward -Wl, etc. } const char *Exec = diff --git a/clang/test/Driver/cl-link.c b/clang/test/Driver/cl-link.c new file mode 100644 index 000000000000..6de089ba0c90 --- /dev/null +++ b/clang/test/Driver/cl-link.c @@ -0,0 +1,12 @@ +// Don't attempt slash switches on msys bash. +// REQUIRES: shell-preserves-root + +// Note: %s must be preceded by -- or bound to another option, otherwise it may +// be interpreted as a command-line option, e.g. on Mac where %s is commonly +// under /Users. + +// RUN: %clang_cl /Tc%s -### /link foo bar baz 2>&1 | FileCheck %s +// CHECK: link.exe +// CHECK: "foo" +// CHECK: "bar" +// CHECK: "baz"