Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 1 | //===--- Tools.cpp - Tools Implementations ------------------------------*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "Tools.h" |
| 11 | |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 12 | #include "clang/Driver/Action.h" |
Daniel Dunbar | 871adcf | 2009-03-18 07:06:02 +0000 | [diff] [blame] | 13 | #include "clang/Driver/Arg.h" |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 14 | #include "clang/Driver/ArgList.h" |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 15 | #include "clang/Driver/Driver.h" // FIXME: Remove? |
| 16 | #include "clang/Driver/DriverDiagnostic.h" // FIXME: Remove? |
Daniel Dunbar | 871adcf | 2009-03-18 07:06:02 +0000 | [diff] [blame] | 17 | #include "clang/Driver/Compilation.h" |
| 18 | #include "clang/Driver/Job.h" |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 19 | #include "clang/Driver/HostInfo.h" |
| 20 | #include "clang/Driver/Option.h" |
| 21 | #include "clang/Driver/ToolChain.h" |
Daniel Dunbar | 871adcf | 2009-03-18 07:06:02 +0000 | [diff] [blame] | 22 | #include "clang/Driver/Util.h" |
| 23 | |
| 24 | #include "llvm/ADT/SmallVector.h" |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Format.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 871adcf | 2009-03-18 07:06:02 +0000 | [diff] [blame] | 27 | |
| 28 | #include "InputInfo.h" |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 29 | #include "ToolChains.h" |
Daniel Dunbar | 871adcf | 2009-03-18 07:06:02 +0000 | [diff] [blame] | 30 | |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 31 | using namespace clang::driver; |
| 32 | using namespace clang::driver::tools; |
| 33 | |
| 34 | void Clang::ConstructJob(Compilation &C, const JobAction &JA, |
Daniel Dunbar | 871adcf | 2009-03-18 07:06:02 +0000 | [diff] [blame] | 35 | Job &Dest, |
| 36 | const InputInfo &Output, |
Daniel Dunbar | 62cf601 | 2009-03-18 06:07:59 +0000 | [diff] [blame] | 37 | const InputInfoList &Inputs, |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 38 | const ArgList &Args, |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 39 | const char *LinkingOutput) const { |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 40 | ArgStringList CmdArgs; |
| 41 | |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 42 | if (isa<AnalyzeJobAction>(JA)) { |
| 43 | assert(JA.getType() == types::TY_Plist && "Invalid output type."); |
| 44 | CmdArgs.push_back("-analyze"); |
| 45 | } else if (isa<PreprocessJobAction>(JA)) { |
| 46 | CmdArgs.push_back("-E"); |
| 47 | } else if (isa<PrecompileJobAction>(JA)) { |
| 48 | // No special option needed, driven by -x. |
| 49 | // |
| 50 | // FIXME: Don't drive this by -x, that is gross. |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 51 | } else { |
| 52 | assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool."); |
| 53 | |
| 54 | if (JA.getType() == types::TY_Nothing) { |
| 55 | CmdArgs.push_back("-fsyntax-only"); |
| 56 | } else if (JA.getType() == types::TY_LLVMAsm) { |
| 57 | CmdArgs.push_back("-emit-llvm"); |
| 58 | } else if (JA.getType() == types::TY_LLVMBC) { |
| 59 | CmdArgs.push_back("-emit-llvm-bc"); |
| 60 | } else if (JA.getType() == types::TY_PP_Asm) { |
| 61 | CmdArgs.push_back("-S"); |
| 62 | } |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 65 | // The make clang go fast button. |
| 66 | CmdArgs.push_back("-disable-free"); |
| 67 | |
| 68 | if (isa<AnalyzeJobAction>(JA)) { |
| 69 | // Add default argument set. |
| 70 | // |
| 71 | // FIXME: Move into clang? |
| 72 | CmdArgs.push_back("-warn-dead-stores"); |
| 73 | CmdArgs.push_back("-checker-cfref"); |
Ted Kremenek | 9b646da | 2009-03-25 00:38:14 +0000 | [diff] [blame] | 74 | CmdArgs.push_back("-analyzer-eagerly-assume"); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 75 | CmdArgs.push_back("-warn-objc-methodsigs"); |
| 76 | // Do not enable the missing -dealloc check. |
| 77 | // '-warn-objc-missing-dealloc', |
| 78 | CmdArgs.push_back("-warn-objc-unused-ivars"); |
| 79 | |
| 80 | CmdArgs.push_back("-analyzer-output=plist"); |
| 81 | |
| 82 | // Add -Xanalyzer arguments when running as analyzer. |
| 83 | Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer); |
| 84 | } else { |
| 85 | // Perform argument translation for LLVM backend. This |
| 86 | // takes some care in reconciling with llvm-gcc. The |
| 87 | // issue is that llvm-gcc translates these options based on |
| 88 | // the values in cc1, whereas we are processing based on |
| 89 | // the driver arguments. |
| 90 | // |
| 91 | // FIXME: This is currently broken for -f flags when -fno |
| 92 | // variants are present. |
| 93 | |
| 94 | // This comes from the default translation the driver + cc1 |
| 95 | // would do to enable flag_pic. |
| 96 | // |
| 97 | // FIXME: Centralize this code. |
| 98 | bool PICEnabled = (Args.hasArg(options::OPT_fPIC) || |
| 99 | Args.hasArg(options::OPT_fpic) || |
| 100 | Args.hasArg(options::OPT_fPIE) || |
| 101 | Args.hasArg(options::OPT_fpie)); |
| 102 | bool PICDisabled = (Args.hasArg(options::OPT_mkernel) || |
| 103 | Args.hasArg(options::OPT_static)); |
| 104 | const char *Model = getToolChain().GetForcedPicModel(); |
| 105 | if (!Model) { |
| 106 | if (Args.hasArg(options::OPT_mdynamic_no_pic)) |
| 107 | Model = "dynamic-no-pic"; |
| 108 | else if (PICDisabled) |
| 109 | Model = "static"; |
| 110 | else if (PICEnabled) |
| 111 | Model = "pic"; |
| 112 | else |
| 113 | Model = getToolChain().GetDefaultRelocationModel(); |
| 114 | } |
| 115 | CmdArgs.push_back("--relocation-model"); |
| 116 | CmdArgs.push_back(Model); |
| 117 | |
| 118 | if (Args.hasArg(options::OPT_ftime_report)) |
| 119 | CmdArgs.push_back("--time-passes"); |
| 120 | // FIXME: Set --enable-unsafe-fp-math. |
| 121 | if (!Args.hasArg(options::OPT_fomit_frame_pointer)) |
| 122 | CmdArgs.push_back("--disable-fp-elim"); |
| 123 | if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss, |
| 124 | options::OPT_fno_zero_initialized_in_bss, |
| 125 | true)) |
| 126 | CmdArgs.push_back("--nozero-initialized-in-bss"); |
Daniel Dunbar | b3fd500 | 2009-03-24 17:59:06 +0000 | [diff] [blame] | 127 | if (Args.hasArg(options::OPT_dA) || Args.hasArg(options::OPT_fverbose_asm)) |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 128 | CmdArgs.push_back("--asm-verbose"); |
| 129 | if (Args.hasArg(options::OPT_fdebug_pass_structure)) |
| 130 | CmdArgs.push_back("--debug-pass=Structure"); |
| 131 | if (Args.hasArg(options::OPT_fdebug_pass_arguments)) |
| 132 | CmdArgs.push_back("--debug-pass=Arguments"); |
| 133 | // FIXME: set --inline-threshhold=50 if (optimize_size || optimize |
| 134 | // < 3) |
| 135 | if (Args.hasFlag(options::OPT_funwind_tables, |
| 136 | options::OPT_fno_unwind_tables, |
| 137 | getToolChain().IsUnwindTablesDefault())) |
| 138 | CmdArgs.push_back("--unwind-tables=1"); |
| 139 | else |
| 140 | CmdArgs.push_back("--unwind-tables=0"); |
| 141 | if (!Args.hasFlag(options::OPT_mred_zone, |
| 142 | options::OPT_mno_red_zone, |
| 143 | true)) |
| 144 | CmdArgs.push_back("--disable-red-zone"); |
| 145 | if (Args.hasFlag(options::OPT_msoft_float, |
| 146 | options::OPT_mno_soft_float, |
| 147 | false)) |
| 148 | CmdArgs.push_back("--soft-float"); |
| 149 | |
| 150 | // FIXME: Need target hooks. |
| 151 | if (memcmp(getToolChain().getPlatform().c_str(), "darwin", 6) == 0) { |
| 152 | if (getToolChain().getArchName() == "x86_64") |
| 153 | CmdArgs.push_back("--mcpu=core2"); |
| 154 | else if (getToolChain().getArchName() == "i386") |
| 155 | CmdArgs.push_back("--mcpu=yonah"); |
| 156 | } |
| 157 | |
| 158 | // FIXME: Ignores ordering. Also, we need to find a realistic |
| 159 | // solution for this. |
| 160 | static const struct { |
| 161 | options::ID Pos, Neg; |
| 162 | const char *Name; |
| 163 | } FeatureOptions[] = { |
| 164 | { options::OPT_mmmx, options::OPT_mno_mmx, "mmx" }, |
| 165 | { options::OPT_msse, options::OPT_mno_sse, "sse" }, |
| 166 | { options::OPT_msse2, options::OPT_mno_sse2, "sse2" }, |
| 167 | { options::OPT_msse3, options::OPT_mno_sse3, "sse3" }, |
| 168 | { options::OPT_mssse3, options::OPT_mno_ssse3, "ssse3" }, |
| 169 | { options::OPT_msse41, options::OPT_mno_sse41, "sse41" }, |
| 170 | { options::OPT_msse42, options::OPT_mno_sse42, "sse42" }, |
| 171 | { options::OPT_msse4a, options::OPT_mno_sse4a, "sse4a" }, |
| 172 | { options::OPT_m3dnow, options::OPT_mno_3dnow, "3dnow" }, |
| 173 | { options::OPT_m3dnowa, options::OPT_mno_3dnowa, "3dnowa" } |
| 174 | }; |
| 175 | const unsigned NumFeatureOptions = |
| 176 | sizeof(FeatureOptions)/sizeof(FeatureOptions[0]); |
| 177 | |
| 178 | // FIXME: Avoid std::string |
| 179 | std::string Attrs; |
| 180 | for (unsigned i=0; i < NumFeatureOptions; ++i) { |
| 181 | if (Args.hasArg(FeatureOptions[i].Pos)) { |
Daniel Dunbar | 55b3b5f | 2009-03-19 17:36:04 +0000 | [diff] [blame] | 182 | if (!Attrs.empty()) |
| 183 | Attrs += ','; |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 184 | Attrs += '+'; |
| 185 | Attrs += FeatureOptions[i].Name; |
| 186 | } else if (Args.hasArg(FeatureOptions[i].Neg)) { |
Daniel Dunbar | 55b3b5f | 2009-03-19 17:36:04 +0000 | [diff] [blame] | 187 | if (!Attrs.empty()) |
| 188 | Attrs += ','; |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 189 | Attrs += '-'; |
| 190 | Attrs += FeatureOptions[i].Name; |
| 191 | } |
| 192 | } |
| 193 | if (!Attrs.empty()) { |
| 194 | CmdArgs.push_back("--mattr"); |
| 195 | CmdArgs.push_back(Args.MakeArgString(Attrs.c_str())); |
| 196 | } |
| 197 | |
| 198 | if (Args.hasFlag(options::OPT_fmath_errno, |
| 199 | options::OPT_fno_math_errno, |
| 200 | getToolChain().IsMathErrnoDefault())) |
| 201 | CmdArgs.push_back("--fmath-errno=1"); |
| 202 | else |
| 203 | CmdArgs.push_back("--fmath-errno=0"); |
| 204 | |
| 205 | if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) { |
| 206 | CmdArgs.push_back("--limit-float-precision"); |
| 207 | CmdArgs.push_back(A->getValue(Args)); |
| 208 | } |
| 209 | |
| 210 | // FIXME: Add --stack-protector-buffer-size=<xxx> on |
| 211 | // -fstack-protect. |
| 212 | |
| 213 | Args.AddLastArg(CmdArgs, options::OPT_MD); |
Daniel Dunbar | 546654a | 2009-03-24 07:20:59 +0000 | [diff] [blame] | 214 | Args.AddLastArg(CmdArgs, options::OPT_MMD); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 215 | Args.AddAllArgs(CmdArgs, options::OPT_MF); |
| 216 | Args.AddLastArg(CmdArgs, options::OPT_MP); |
| 217 | Args.AddAllArgs(CmdArgs, options::OPT_MT); |
| 218 | |
| 219 | Arg *Unsupported = Args.getLastArg(options::OPT_M); |
| 220 | if (!Unsupported) |
| 221 | Unsupported = Args.getLastArg(options::OPT_MM); |
| 222 | if (!Unsupported) |
| 223 | Unsupported = Args.getLastArg(options::OPT_MG); |
| 224 | if (!Unsupported) |
| 225 | Unsupported = Args.getLastArg(options::OPT_MQ); |
| 226 | if (Unsupported) { |
| 227 | const Driver &D = getToolChain().getHost().getDriver(); |
| 228 | D.Diag(clang::diag::err_drv_unsupported_opt) |
| 229 | << Unsupported->getOption().getName(); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | Args.AddAllArgs(CmdArgs, options::OPT_v); |
| 234 | Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U); |
| 235 | Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F); |
| 236 | Args.AddLastArg(CmdArgs, options::OPT_P); |
| 237 | Args.AddAllArgs(CmdArgs, options::OPT_mmacosx_version_min_EQ); |
| 238 | |
| 239 | // Special case debug options to only pass -g to clang. This is |
| 240 | // wrong. |
| 241 | if (Args.hasArg(options::OPT_g_Group)) |
| 242 | CmdArgs.push_back("-g"); |
| 243 | |
| 244 | Args.AddLastArg(CmdArgs, options::OPT_nostdinc); |
| 245 | |
| 246 | // FIXME: Clang isn't going to accept just anything here. |
Daniel Dunbar | 049853d | 2009-03-20 19:38:56 +0000 | [diff] [blame] | 247 | // FIXME: Use iterator. |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 248 | |
Daniel Dunbar | 049853d | 2009-03-20 19:38:56 +0000 | [diff] [blame] | 249 | // Add -i* options, and automatically translate to -include-pth for |
| 250 | // transparent PCH support. It's wonky, but we include looking for |
| 251 | // .gch so we can support seamless replacement into a build system |
| 252 | // already set up to be generating .gch files. |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 253 | for (ArgList::const_iterator |
| 254 | it = Args.begin(), ie = Args.end(); it != ie; ++it) { |
| 255 | const Arg *A = *it; |
Daniel Dunbar | 049853d | 2009-03-20 19:38:56 +0000 | [diff] [blame] | 256 | if (!A->getOption().matches(options::OPT_i_Group)) |
| 257 | continue; |
| 258 | |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 259 | if (A->getOption().matches(options::OPT_include)) { |
Daniel Dunbar | 049853d | 2009-03-20 19:38:56 +0000 | [diff] [blame] | 260 | bool FoundPTH = false; |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 261 | llvm::sys::Path P(A->getValue(Args)); |
| 262 | P.appendSuffix("pth"); |
| 263 | if (P.exists()) { |
Daniel Dunbar | 049853d | 2009-03-20 19:38:56 +0000 | [diff] [blame] | 264 | FoundPTH = true; |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 265 | } else { |
| 266 | P.eraseSuffix(); |
| 267 | P.appendSuffix("gch"); |
Daniel Dunbar | 049853d | 2009-03-20 19:38:56 +0000 | [diff] [blame] | 268 | if (P.exists()) |
| 269 | FoundPTH = true; |
| 270 | } |
| 271 | |
| 272 | if (FoundPTH) { |
| 273 | A->claim(); |
| 274 | CmdArgs.push_back("-include-pth"); |
| 275 | CmdArgs.push_back(Args.MakeArgString(P.c_str())); |
| 276 | continue; |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
Daniel Dunbar | 049853d | 2009-03-20 19:38:56 +0000 | [diff] [blame] | 279 | |
| 280 | // Not translated, render as usual. |
| 281 | A->claim(); |
| 282 | A->render(Args, CmdArgs); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Daniel Dunbar | 337a627 | 2009-03-24 20:17:30 +0000 | [diff] [blame] | 285 | // Manually translate -O to -O1 and -O4 to -O3; let clang reject |
| 286 | // others. |
| 287 | if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { |
| 288 | if (A->getOption().getId() == options::OPT_O4) |
| 289 | CmdArgs.push_back("-O3"); |
| 290 | else if (A->getValue(Args)[0] == '\0') |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 291 | CmdArgs.push_back("-O1"); |
| 292 | else |
Daniel Dunbar | 5697aa0 | 2009-03-18 23:39:35 +0000 | [diff] [blame] | 293 | A->render(Args, CmdArgs); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Daniel Dunbar | ff7488d | 2009-03-20 00:52:38 +0000 | [diff] [blame] | 296 | Args.AddAllArgs(CmdArgs, options::OPT_clang_W_Group, |
| 297 | options::OPT_pedantic_Group); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 298 | Args.AddLastArg(CmdArgs, options::OPT_w); |
| 299 | Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi, |
| 300 | options::OPT_trigraphs); |
| 301 | |
| 302 | if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) { |
| 303 | CmdArgs.push_back("-ftemplate-depth"); |
| 304 | CmdArgs.push_back(A->getValue(Args)); |
| 305 | } |
| 306 | |
| 307 | Args.AddAllArgs(CmdArgs, options::OPT_clang_f_Group); |
| 308 | |
Daniel Dunbar | b9f3a77 | 2009-03-27 15:22:28 +0000 | [diff] [blame^] | 309 | // If tool chain translates fpascal-strings, we want to back |
| 310 | // translate here. |
| 311 | // FIXME: This is gross; that translation should be pulled from the |
| 312 | // tool chain. |
| 313 | if (Arg *A = Args.getLastArg(options::OPT_mpascal_strings, |
| 314 | options::OPT_mno_pascal_strings)) { |
| 315 | if (A->getOption().matches(options::OPT_mpascal_strings)) |
| 316 | CmdArgs.push_back("-fpascal-strings"); |
| 317 | else |
| 318 | CmdArgs.push_back("-fno-pascal-strings"); |
| 319 | } |
| 320 | |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 321 | Args.AddLastArg(CmdArgs, options::OPT_dM); |
| 322 | |
| 323 | Args.AddAllArgValues(CmdArgs, options::OPT_Xclang); |
| 324 | |
| 325 | // FIXME: Always pass the full triple once we aren't concerned with |
| 326 | // ccc compat. |
| 327 | CmdArgs.push_back("-arch"); |
| 328 | CmdArgs.push_back(getToolChain().getArchName().c_str()); |
| 329 | |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 330 | if (Output.isPipe()) { |
| 331 | CmdArgs.push_back("-o"); |
| 332 | CmdArgs.push_back("-"); |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 333 | } else if (Output.isFilename()) { |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 334 | CmdArgs.push_back("-o"); |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 335 | CmdArgs.push_back(Output.getFilename()); |
| 336 | } else { |
| 337 | assert(Output.isNothing() && "Invalid output."); |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 340 | for (InputInfoList::const_iterator |
| 341 | it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { |
| 342 | const InputInfo &II = *it; |
| 343 | CmdArgs.push_back("-x"); |
| 344 | CmdArgs.push_back(types::getTypeName(II.getType())); |
| 345 | if (II.isPipe()) |
| 346 | CmdArgs.push_back("-"); |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 347 | else if (II.isFilename()) |
| 348 | CmdArgs.push_back(II.getFilename()); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 349 | else |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 350 | II.getInputArg().renderAsInput(Args, CmdArgs); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | const char *Exec = |
Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 354 | Args.MakeArgString(getToolChain().GetProgramPath(C, "clang-cc").c_str()); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 355 | Dest.addCommand(new Command(Exec, CmdArgs)); |
Daniel Dunbar | a880db0 | 2009-03-23 19:03:36 +0000 | [diff] [blame] | 356 | |
| 357 | // Claim some arguments which clang doesn't support, but we don't |
| 358 | // care to warn the user about. |
| 359 | |
| 360 | // FIXME: Use iterator. |
| 361 | for (ArgList::const_iterator |
| 362 | it = Args.begin(), ie = Args.end(); it != ie; ++it) { |
| 363 | const Arg *A = *it; |
| 364 | if (A->getOption().matches(options::OPT_clang_ignored_W_Group) || |
| 365 | A->getOption().matches(options::OPT_clang_ignored_f_Group)) |
| 366 | A->claim(); |
| 367 | } |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 370 | void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA, |
| 371 | Job &Dest, |
| 372 | const InputInfo &Output, |
| 373 | const InputInfoList &Inputs, |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 374 | const ArgList &Args, |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 375 | const char *LinkingOutput) const { |
| 376 | ArgStringList CmdArgs; |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 377 | |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 378 | for (ArgList::const_iterator |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 379 | it = Args.begin(), ie = Args.end(); it != ie; ++it) { |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 380 | Arg *A = *it; |
Daniel Dunbar | 7587719 | 2009-03-19 07:55:12 +0000 | [diff] [blame] | 381 | if (A->getOption().hasForwardToGCC()) { |
| 382 | // It is unfortunate that we have to claim here, as this means |
| 383 | // we will basically never report anything interesting for |
| 384 | // platforms using a generic gcc. |
| 385 | A->claim(); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 386 | A->render(Args, CmdArgs); |
Daniel Dunbar | 7587719 | 2009-03-19 07:55:12 +0000 | [diff] [blame] | 387 | } |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | RenderExtraToolArgs(CmdArgs); |
| 391 | |
| 392 | // If using a driver driver, force the arch. |
| 393 | if (getToolChain().getHost().useDriverDriver()) { |
| 394 | CmdArgs.push_back("-arch"); |
| 395 | CmdArgs.push_back(getToolChain().getArchName().c_str()); |
| 396 | } |
| 397 | |
| 398 | if (Output.isPipe()) { |
| 399 | CmdArgs.push_back("-o"); |
| 400 | CmdArgs.push_back("-"); |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 401 | } else if (Output.isFilename()) { |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 402 | CmdArgs.push_back("-o"); |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 403 | CmdArgs.push_back(Output.getFilename()); |
| 404 | } else { |
| 405 | assert(Output.isNothing() && "Unexpected output"); |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 406 | CmdArgs.push_back("-fsyntax-only"); |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 407 | } |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 408 | |
| 409 | |
| 410 | // Only pass -x if gcc will understand it; otherwise hope gcc |
| 411 | // understands the suffix correctly. The main use case this would go |
| 412 | // wrong in is for linker inputs if they happened to have an odd |
| 413 | // suffix; really the only way to get this to happen is a command |
| 414 | // like '-x foobar a.c' which will treat a.c like a linker input. |
| 415 | // |
| 416 | // FIXME: For the linker case specifically, can we safely convert |
| 417 | // inputs into '-Wl,' options? |
| 418 | for (InputInfoList::const_iterator |
| 419 | it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { |
| 420 | const InputInfo &II = *it; |
| 421 | if (types::canTypeBeUserSpecified(II.getType())) { |
| 422 | CmdArgs.push_back("-x"); |
| 423 | CmdArgs.push_back(types::getTypeName(II.getType())); |
| 424 | } |
| 425 | |
| 426 | if (II.isPipe()) |
| 427 | CmdArgs.push_back("-"); |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 428 | else if (II.isFilename()) |
| 429 | CmdArgs.push_back(II.getFilename()); |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 430 | else |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 431 | // Don't render as input, we need gcc to do the translations. |
| 432 | II.getInputArg().render(Args, CmdArgs); |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Daniel Dunbar | 632f50e | 2009-03-18 21:34:08 +0000 | [diff] [blame] | 435 | const char *Exec = |
| 436 | Args.MakeArgString(getToolChain().GetProgramPath(C, "gcc").c_str()); |
| 437 | Dest.addCommand(new Command(Exec, CmdArgs)); |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 440 | void gcc::Preprocess::RenderExtraToolArgs(ArgStringList &CmdArgs) const { |
| 441 | CmdArgs.push_back("-E"); |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 444 | void gcc::Precompile::RenderExtraToolArgs(ArgStringList &CmdArgs) const { |
| 445 | // The type is good enough. |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 448 | void gcc::Compile::RenderExtraToolArgs(ArgStringList &CmdArgs) const { |
| 449 | CmdArgs.push_back("-S"); |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 452 | void gcc::Assemble::RenderExtraToolArgs(ArgStringList &CmdArgs) const { |
| 453 | CmdArgs.push_back("-c"); |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 454 | } |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 455 | |
| 456 | void gcc::Link::RenderExtraToolArgs(ArgStringList &CmdArgs) const { |
| 457 | // The types are (hopefully) good enough. |
| 458 | } |
| 459 | |
Daniel Dunbar | 8cac5f7 | 2009-03-20 16:06:39 +0000 | [diff] [blame] | 460 | void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA, |
| 461 | Job &Dest, const InputInfo &Output, |
| 462 | const InputInfoList &Inputs, |
| 463 | const ArgList &Args, |
| 464 | const char *LinkingOutput) const { |
| 465 | ArgStringList CmdArgs; |
| 466 | |
| 467 | assert(Inputs.size() == 1 && "Unexpected number of inputs."); |
| 468 | const InputInfo &Input = Inputs[0]; |
| 469 | |
| 470 | // Bit of a hack, this is only used for original inputs. |
| 471 | if (Input.isFilename() && |
| 472 | strcmp(Input.getFilename(), Input.getBaseInput()) == 0 && |
| 473 | Args.hasArg(options::OPT_g_Group)) |
| 474 | CmdArgs.push_back("--gstabs"); |
| 475 | |
| 476 | // Derived from asm spec. |
| 477 | CmdArgs.push_back("-arch"); |
| 478 | CmdArgs.push_back(getToolChain().getArchName().c_str()); |
| 479 | |
| 480 | CmdArgs.push_back("-force_cpusubtype_ALL"); |
| 481 | if ((Args.hasArg(options::OPT_mkernel) || |
| 482 | Args.hasArg(options::OPT_static) || |
| 483 | Args.hasArg(options::OPT_fapple_kext)) && |
| 484 | !Args.hasArg(options::OPT_dynamic)) |
| 485 | CmdArgs.push_back("-static"); |
| 486 | |
| 487 | Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, |
| 488 | options::OPT_Xassembler); |
| 489 | |
| 490 | assert(Output.isFilename() && "Unexpected lipo output."); |
| 491 | CmdArgs.push_back("-o"); |
| 492 | CmdArgs.push_back(Output.getFilename()); |
| 493 | |
| 494 | if (Input.isPipe()) { |
| 495 | CmdArgs.push_back("-"); |
| 496 | } else { |
| 497 | assert(Input.isFilename() && "Invalid input."); |
| 498 | CmdArgs.push_back(Input.getFilename()); |
| 499 | } |
| 500 | |
| 501 | // asm_final spec is empty. |
| 502 | |
| 503 | const char *Exec = |
| 504 | Args.MakeArgString(getToolChain().GetProgramPath(C, "as").c_str()); |
| 505 | Dest.addCommand(new Command(Exec, CmdArgs)); |
| 506 | } |
Daniel Dunbar | ff7488d | 2009-03-20 00:52:38 +0000 | [diff] [blame] | 507 | |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 508 | static const char *MakeFormattedString(const ArgList &Args, |
| 509 | const llvm::format_object_base &Fmt) { |
| 510 | std::string Str; |
| 511 | llvm::raw_string_ostream(Str) << Fmt; |
| 512 | return Args.MakeArgString(Str.c_str()); |
| 513 | } |
| 514 | |
| 515 | /// Helper routine for seeing if we should use dsymutil; this is a |
| 516 | /// gcc compatible hack, we should remove it and use the input |
| 517 | /// type information. |
| 518 | static bool isSourceSuffix(const char *Str) { |
| 519 | // match: 'C', 'CPP', 'c', 'cc', 'cp', 'c++', 'cpp', 'cxx', 'm', |
| 520 | // 'mm'. |
| 521 | switch (strlen(Str)) { |
| 522 | default: |
| 523 | return false; |
| 524 | case 1: |
| 525 | return (memcmp(Str, "C", 1) == 0 || |
| 526 | memcmp(Str, "c", 1) == 0 || |
| 527 | memcmp(Str, "m", 1) == 0); |
| 528 | case 2: |
| 529 | return (memcmp(Str, "cc", 2) == 0 || |
| 530 | memcmp(Str, "cp", 2) == 0 || |
| 531 | memcmp(Str, "mm", 2) == 0); |
| 532 | case 3: |
| 533 | return (memcmp(Str, "CPP", 3) == 0 || |
| 534 | memcmp(Str, "c++", 3) == 0 || |
| 535 | memcmp(Str, "cpp", 3) == 0 || |
| 536 | memcmp(Str, "cxx", 3) == 0); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | static bool isMacosxVersionLT(unsigned (&A)[3], unsigned (&B)[3]) { |
| 541 | for (unsigned i=0; i < 3; ++i) { |
| 542 | if (A[i] > B[i]) return false; |
| 543 | if (A[i] < B[i]) return true; |
| 544 | } |
| 545 | return false; |
| 546 | } |
| 547 | |
| 548 | static bool isMacosxVersionLT(unsigned (&A)[3], |
| 549 | unsigned V0, unsigned V1=0, unsigned V2=0) { |
| 550 | unsigned B[3] = { V0, V1, V2 }; |
| 551 | return isMacosxVersionLT(A, B); |
| 552 | } |
| 553 | |
| 554 | static bool isMacosxVersionGTE(unsigned(&A)[3], |
| 555 | unsigned V0, unsigned V1=0, unsigned V2=0) { |
| 556 | return !isMacosxVersionLT(A, V0, V1, V2); |
| 557 | } |
| 558 | |
| 559 | const toolchains::Darwin_X86 &darwin::Link::getDarwinToolChain() const { |
| 560 | return reinterpret_cast<const toolchains::Darwin_X86&>(getToolChain()); |
| 561 | } |
| 562 | |
| 563 | void darwin::Link::AddDarwinArch(const ArgList &Args, |
| 564 | ArgStringList &CmdArgs) const { |
| 565 | // Derived from darwin_arch spec. |
| 566 | CmdArgs.push_back("-arch"); |
| 567 | CmdArgs.push_back(getToolChain().getArchName().c_str()); |
| 568 | } |
| 569 | |
| 570 | void darwin::Link::AddDarwinSubArch(const ArgList &Args, |
| 571 | ArgStringList &CmdArgs) const { |
| 572 | // Derived from darwin_subarch spec, not sure what the distinction |
| 573 | // exists for but at least for this chain it is the same. |
| 574 | AddDarwinArch(Args, CmdArgs); |
| 575 | } |
| 576 | |
| 577 | void darwin::Link::AddLinkArgs(const ArgList &Args, |
| 578 | ArgStringList &CmdArgs) const { |
| 579 | const Driver &D = getToolChain().getHost().getDriver(); |
| 580 | |
| 581 | // Derived from the "link" spec. |
| 582 | Args.AddAllArgs(CmdArgs, options::OPT_static); |
| 583 | if (!Args.hasArg(options::OPT_static)) |
| 584 | CmdArgs.push_back("-dynamic"); |
| 585 | if (Args.hasArg(options::OPT_fgnu_runtime)) { |
| 586 | // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu |
| 587 | // here. How do we wish to handle such things? |
| 588 | } |
| 589 | |
| 590 | if (!Args.hasArg(options::OPT_dynamiclib)) { |
| 591 | if (Args.hasArg(options::OPT_force__cpusubtype__ALL)) { |
| 592 | AddDarwinArch(Args, CmdArgs); |
| 593 | CmdArgs.push_back("-force_cpusubtype_ALL"); |
| 594 | } else |
| 595 | AddDarwinSubArch(Args, CmdArgs); |
| 596 | |
| 597 | Args.AddLastArg(CmdArgs, options::OPT_bundle); |
| 598 | Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader); |
| 599 | Args.AddAllArgs(CmdArgs, options::OPT_client__name); |
| 600 | |
| 601 | Arg *A; |
| 602 | if ((A = Args.getLastArg(options::OPT_compatibility__version)) || |
| 603 | (A = Args.getLastArg(options::OPT_current__version)) || |
| 604 | (A = Args.getLastArg(options::OPT_install__name))) |
| 605 | D.Diag(clang::diag::err_drv_argument_only_allowed_with) |
| 606 | << A->getAsString(Args) << "-dynamiclib"; |
| 607 | |
| 608 | Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace); |
| 609 | Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs); |
| 610 | Args.AddLastArg(CmdArgs, options::OPT_private__bundle); |
| 611 | } else { |
| 612 | CmdArgs.push_back("-dylib"); |
| 613 | |
| 614 | Arg *A; |
| 615 | if ((A = Args.getLastArg(options::OPT_bundle)) || |
| 616 | (A = Args.getLastArg(options::OPT_bundle__loader)) || |
| 617 | (A = Args.getLastArg(options::OPT_client__name)) || |
| 618 | (A = Args.getLastArg(options::OPT_force__flat__namespace)) || |
| 619 | (A = Args.getLastArg(options::OPT_keep__private__externs)) || |
| 620 | (A = Args.getLastArg(options::OPT_private__bundle))) |
| 621 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 622 | << A->getAsString(Args) << "-dynamiclib"; |
| 623 | |
| 624 | Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version, |
| 625 | "-dylib_compatibility_version"); |
| 626 | Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version, |
| 627 | "-dylib_current_version"); |
| 628 | |
| 629 | if (Args.hasArg(options::OPT_force__cpusubtype__ALL)) { |
| 630 | AddDarwinArch(Args, CmdArgs); |
| 631 | // NOTE: We don't add -force_cpusubtype_ALL on this path. Ok. |
| 632 | } else |
| 633 | AddDarwinSubArch(Args, CmdArgs); |
| 634 | |
| 635 | Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name, |
| 636 | "-dylib_install_name"); |
| 637 | } |
| 638 | |
| 639 | Args.AddLastArg(CmdArgs, options::OPT_all__load); |
| 640 | Args.AddAllArgs(CmdArgs, options::OPT_allowable__client); |
| 641 | Args.AddLastArg(CmdArgs, options::OPT_bind__at__load); |
| 642 | Args.AddLastArg(CmdArgs, options::OPT_dead__strip); |
| 643 | Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms); |
| 644 | Args.AddAllArgs(CmdArgs, options::OPT_dylib__file); |
| 645 | Args.AddLastArg(CmdArgs, options::OPT_dynamic); |
| 646 | Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list); |
| 647 | Args.AddLastArg(CmdArgs, options::OPT_flat__namespace); |
| 648 | Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names); |
| 649 | Args.AddAllArgs(CmdArgs, options::OPT_image__base); |
| 650 | Args.AddAllArgs(CmdArgs, options::OPT_init); |
| 651 | |
| 652 | if (!Args.hasArg(options::OPT_mmacosx_version_min_EQ)) { |
| 653 | if (!Args.hasArg(options::OPT_miphoneos_version_min_EQ)) { |
| 654 | // FIXME: I don't understand what is going on here. This is |
| 655 | // supposed to come from darwin_ld_minversion, but gcc doesn't |
| 656 | // seem to be following that; it must be getting overridden |
| 657 | // somewhere. |
| 658 | CmdArgs.push_back("-macosx_version_min"); |
| 659 | CmdArgs.push_back(getDarwinToolChain().getMacosxVersionStr()); |
| 660 | } |
| 661 | } else { |
| 662 | // Adding all arguments doesn't make sense here but this is what |
| 663 | // gcc does. |
| 664 | Args.AddAllArgsTranslated(CmdArgs, options::OPT_mmacosx_version_min_EQ, |
| 665 | "-macosx_version_min"); |
| 666 | } |
| 667 | |
| 668 | Args.AddAllArgsTranslated(CmdArgs, options::OPT_miphoneos_version_min_EQ, |
| 669 | "-iphoneos_version_min"); |
| 670 | Args.AddLastArg(CmdArgs, options::OPT_nomultidefs); |
| 671 | Args.AddLastArg(CmdArgs, options::OPT_multi__module); |
| 672 | Args.AddLastArg(CmdArgs, options::OPT_single__module); |
| 673 | Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined); |
| 674 | Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused); |
| 675 | |
| 676 | if (Args.hasArg(options::OPT_fpie)) |
| 677 | CmdArgs.push_back("-pie"); |
| 678 | |
| 679 | Args.AddLastArg(CmdArgs, options::OPT_prebind); |
| 680 | Args.AddLastArg(CmdArgs, options::OPT_noprebind); |
| 681 | Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding); |
| 682 | Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules); |
| 683 | Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs); |
| 684 | Args.AddAllArgs(CmdArgs, options::OPT_sectcreate); |
| 685 | Args.AddAllArgs(CmdArgs, options::OPT_sectorder); |
| 686 | Args.AddAllArgs(CmdArgs, options::OPT_seg1addr); |
| 687 | Args.AddAllArgs(CmdArgs, options::OPT_segprot); |
| 688 | Args.AddAllArgs(CmdArgs, options::OPT_segaddr); |
| 689 | Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr); |
| 690 | Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr); |
| 691 | Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table); |
| 692 | Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename); |
| 693 | Args.AddAllArgs(CmdArgs, options::OPT_sub__library); |
| 694 | Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella); |
| 695 | Args.AddAllArgsTranslated(CmdArgs, options::OPT_isysroot, "-syslibroot"); |
| 696 | Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace); |
| 697 | Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints); |
| 698 | Args.AddAllArgs(CmdArgs, options::OPT_umbrella); |
| 699 | Args.AddAllArgs(CmdArgs, options::OPT_undefined); |
| 700 | Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list); |
| 701 | Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches); |
| 702 | |
| 703 | if (!Args.hasArg(options::OPT_weak__reference__mismatches)) { |
| 704 | CmdArgs.push_back("-weak_reference_mismatches"); |
| 705 | CmdArgs.push_back("non-weak"); |
| 706 | } |
| 707 | |
| 708 | Args.AddLastArg(CmdArgs, options::OPT_X_Flag); |
| 709 | Args.AddAllArgs(CmdArgs, options::OPT_y); |
| 710 | Args.AddLastArg(CmdArgs, options::OPT_w); |
| 711 | Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size); |
| 712 | Args.AddAllArgs(CmdArgs, options::OPT_segs__read__); |
| 713 | Args.AddLastArg(CmdArgs, options::OPT_seglinkedit); |
| 714 | Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit); |
| 715 | Args.AddAllArgs(CmdArgs, options::OPT_sectalign); |
| 716 | Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols); |
| 717 | Args.AddAllArgs(CmdArgs, options::OPT_segcreate); |
| 718 | Args.AddLastArg(CmdArgs, options::OPT_whyload); |
| 719 | Args.AddLastArg(CmdArgs, options::OPT_whatsloaded); |
| 720 | Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name); |
| 721 | Args.AddLastArg(CmdArgs, options::OPT_dylinker); |
| 722 | Args.AddLastArg(CmdArgs, options::OPT_Mach); |
| 723 | } |
| 724 | |
| 725 | void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA, |
| 726 | Job &Dest, const InputInfo &Output, |
| 727 | const InputInfoList &Inputs, |
| 728 | const ArgList &Args, |
| 729 | const char *LinkingOutput) const { |
| 730 | assert(Output.getType() == types::TY_Image && "Invalid linker output type."); |
| 731 | // The logic here is derived from gcc's behavior; most of which |
| 732 | // comes from specs (starting with link_command). Consult gcc for |
| 733 | // more information. |
| 734 | |
| 735 | // FIXME: The spec references -fdump= which seems to have |
| 736 | // disappeared? |
| 737 | |
| 738 | ArgStringList CmdArgs; |
| 739 | |
| 740 | // I'm not sure why this particular decomposition exists in gcc, but |
| 741 | // we follow suite for ease of comparison. |
| 742 | AddLinkArgs(Args, CmdArgs); |
| 743 | |
| 744 | // FIXME: gcc has %{x} in here. How could this ever happen? Cruft? |
| 745 | Args.AddAllArgs(CmdArgs, options::OPT_d_Flag); |
| 746 | Args.AddAllArgs(CmdArgs, options::OPT_s); |
| 747 | Args.AddAllArgs(CmdArgs, options::OPT_t); |
| 748 | Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag); |
| 749 | Args.AddAllArgs(CmdArgs, options::OPT_u_Group); |
| 750 | Args.AddAllArgs(CmdArgs, options::OPT_A); |
| 751 | Args.AddLastArg(CmdArgs, options::OPT_e); |
| 752 | Args.AddAllArgs(CmdArgs, options::OPT_m_Separate); |
| 753 | Args.AddAllArgs(CmdArgs, options::OPT_r); |
| 754 | |
| 755 | // FIXME: This is just being pedantically bug compatible, gcc |
| 756 | // doesn't *mean* to forward this, it just does (yay for pattern |
| 757 | // matching). It doesn't work, of course. |
| 758 | Args.AddAllArgs(CmdArgs, options::OPT_object); |
| 759 | |
| 760 | CmdArgs.push_back("-o"); |
| 761 | CmdArgs.push_back(Output.getFilename()); |
| 762 | |
| 763 | unsigned MacosxVersion[3]; |
| 764 | if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ)) { |
| 765 | bool HadExtra; |
| 766 | if (!Driver::GetReleaseVersion(A->getValue(Args), MacosxVersion[0], |
| 767 | MacosxVersion[1], MacosxVersion[2], |
| 768 | HadExtra) || |
| 769 | HadExtra) { |
| 770 | const Driver &D = getToolChain().getHost().getDriver(); |
| 771 | D.Diag(clang::diag::err_drv_invalid_version_number) |
| 772 | << A->getAsString(Args); |
| 773 | } |
| 774 | } else { |
| 775 | getDarwinToolChain().getMacosxVersion(MacosxVersion); |
| 776 | } |
| 777 | |
| 778 | if (!Args.hasArg(options::OPT_A) && |
| 779 | !Args.hasArg(options::OPT_nostdlib) && |
| 780 | !Args.hasArg(options::OPT_nostartfiles)) { |
| 781 | // Derived from startfile spec. |
| 782 | if (Args.hasArg(options::OPT_dynamiclib)) { |
| 783 | // Derived from darwin_dylib1 spec. |
| 784 | if (Args.hasArg(options::OPT_miphoneos_version_min_EQ) || |
| 785 | isMacosxVersionLT(MacosxVersion, 10, 5)) |
| 786 | CmdArgs.push_back("-ldylib1.o"); |
| 787 | else |
| 788 | CmdArgs.push_back("-ldylib1.10.5.o"); |
| 789 | } else { |
| 790 | if (Args.hasArg(options::OPT_bundle)) { |
| 791 | if (!Args.hasArg(options::OPT_static)) |
| 792 | CmdArgs.push_back("-lbundle1.o"); |
| 793 | } else { |
| 794 | if (Args.hasArg(options::OPT_pg)) { |
| 795 | if (Args.hasArg(options::OPT_static) || |
| 796 | Args.hasArg(options::OPT_object) || |
| 797 | Args.hasArg(options::OPT_preload)) { |
| 798 | CmdArgs.push_back("-lgcrt0.o"); |
| 799 | } else { |
| 800 | CmdArgs.push_back("-lgcrt1.o"); |
| 801 | |
| 802 | // darwin_crt2 spec is empty. |
| 803 | } |
| 804 | } else { |
| 805 | if (Args.hasArg(options::OPT_static) || |
| 806 | Args.hasArg(options::OPT_object) || |
| 807 | Args.hasArg(options::OPT_preload)) { |
| 808 | CmdArgs.push_back("-lcrt0.o"); |
| 809 | } else { |
| 810 | // Derived from darwin_crt1 spec. |
| 811 | if (Args.hasArg(options::OPT_miphoneos_version_min_EQ) || |
| 812 | isMacosxVersionLT(MacosxVersion, 10, 5)) { |
| 813 | CmdArgs.push_back("-lcrt1.o"); |
| 814 | } else { |
| 815 | CmdArgs.push_back("-lcrt1.10.5.o"); |
| 816 | |
| 817 | // darwin_crt2 spec is empty. |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | if (Args.hasArg(options::OPT_shared_libgcc) && |
| 825 | !Args.hasArg(options::OPT_miphoneos_version_min_EQ) && |
| 826 | isMacosxVersionLT(MacosxVersion, 10, 5)) { |
| 827 | const char *Str = getToolChain().GetFilePath(C, "crt3.o").c_str(); |
| 828 | CmdArgs.push_back(Args.MakeArgString(Str)); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | Args.AddAllArgs(CmdArgs, options::OPT_L); |
| 833 | |
| 834 | if (Args.hasArg(options::OPT_fopenmp)) |
| 835 | // This is more complicated in gcc... |
| 836 | CmdArgs.push_back("-lgomp"); |
| 837 | |
| 838 | // FIXME: Derive these correctly. |
| 839 | const char *TCDir = getDarwinToolChain().getToolChainDir().c_str(); |
| 840 | if (getToolChain().getArchName() == "x86_64") { |
| 841 | CmdArgs.push_back(MakeFormattedString(Args, |
| 842 | llvm::format("-L/usr/lib/gcc/%s/x86_64", TCDir))); |
| 843 | // Intentionally duplicated for (temporary) gcc bug compatibility. |
| 844 | CmdArgs.push_back(MakeFormattedString(Args, |
| 845 | llvm::format("-L/usr/lib/gcc/%s/x86_64", TCDir))); |
| 846 | } |
| 847 | CmdArgs.push_back(MakeFormattedString(Args, |
| 848 | llvm::format("-L/usr/lib/%s", TCDir))); |
| 849 | CmdArgs.push_back(MakeFormattedString(Args, |
| 850 | llvm::format("-L/usr/lib/gcc/%s", TCDir))); |
| 851 | // Intentionally duplicated for (temporary) gcc bug compatibility. |
| 852 | CmdArgs.push_back(MakeFormattedString(Args, |
| 853 | llvm::format("-L/usr/lib/gcc/%s", TCDir))); |
| 854 | CmdArgs.push_back(MakeFormattedString(Args, |
| 855 | llvm::format("-L/usr/lib/gcc/%s/../../../%s", TCDir, TCDir))); |
| 856 | CmdArgs.push_back(MakeFormattedString(Args, |
| 857 | llvm::format("-L/usr/lib/gcc/%s/../../..", TCDir))); |
| 858 | |
| 859 | for (InputInfoList::const_iterator |
| 860 | it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { |
| 861 | const InputInfo &II = *it; |
| 862 | if (II.isFilename()) |
| 863 | CmdArgs.push_back(II.getFilename()); |
| 864 | else |
| 865 | II.getInputArg().renderAsInput(Args, CmdArgs); |
| 866 | } |
| 867 | |
| 868 | if (LinkingOutput) { |
| 869 | CmdArgs.push_back("-arch_multiple"); |
| 870 | CmdArgs.push_back("-final_output"); |
| 871 | CmdArgs.push_back(LinkingOutput); |
| 872 | } |
| 873 | |
| 874 | if (Args.hasArg(options::OPT_fprofile_arcs) || |
| 875 | Args.hasArg(options::OPT_fprofile_generate) || |
| 876 | Args.hasArg(options::OPT_fcreate_profile) || |
| 877 | Args.hasArg(options::OPT_coverage)) |
| 878 | CmdArgs.push_back("-lgcov"); |
| 879 | |
| 880 | if (Args.hasArg(options::OPT_fnested_functions)) |
| 881 | CmdArgs.push_back("-allow_stack_execute"); |
| 882 | |
| 883 | if (!Args.hasArg(options::OPT_nostdlib) && |
| 884 | !Args.hasArg(options::OPT_nodefaultlibs)) { |
| 885 | // link_ssp spec is empty. |
| 886 | |
| 887 | // Derived from libgcc spec. |
| 888 | if (Args.hasArg(options::OPT_static)) { |
| 889 | CmdArgs.push_back("-lgcc_static"); |
| 890 | } else if (Args.hasArg(options::OPT_static_libgcc)) { |
| 891 | CmdArgs.push_back("-lgcc_eh"); |
| 892 | CmdArgs.push_back("-lgcc"); |
| 893 | } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) { |
| 894 | // Derived from darwin_iphoneos_libgcc spec. |
| 895 | CmdArgs.push_back("-lgcc_s.10.5"); |
| 896 | CmdArgs.push_back("-lgcc"); |
| 897 | } else if (Args.hasArg(options::OPT_shared_libgcc) || |
| 898 | Args.hasArg(options::OPT_fexceptions) || |
| 899 | Args.hasArg(options::OPT_fgnu_runtime)) { |
| 900 | if (isMacosxVersionLT(MacosxVersion, 10, 5)) |
| 901 | CmdArgs.push_back("-lgcc_s.10.4"); |
| 902 | else |
| 903 | CmdArgs.push_back("-lgcc_s.10.5"); |
| 904 | CmdArgs.push_back("-lgcc"); |
| 905 | } else { |
| 906 | if (isMacosxVersionLT(MacosxVersion, 10, 5) && |
| 907 | isMacosxVersionGTE(MacosxVersion, 10, 3, 9)) |
| 908 | CmdArgs.push_back("-lgcc_s.10.4"); |
| 909 | if (isMacosxVersionGTE(MacosxVersion, 10, 5)) |
| 910 | CmdArgs.push_back("-lgcc_s.10.5"); |
| 911 | CmdArgs.push_back("-lgcc"); |
| 912 | } |
| 913 | |
| 914 | // Derived from lib spec. |
| 915 | if (!Args.hasArg(options::OPT_static)) |
| 916 | CmdArgs.push_back("-lSystem"); |
| 917 | } |
| 918 | |
| 919 | if (!Args.hasArg(options::OPT_A) && |
| 920 | !Args.hasArg(options::OPT_nostdlib) && |
| 921 | !Args.hasArg(options::OPT_nostartfiles)) { |
| 922 | // endfile_spec is empty. |
| 923 | } |
| 924 | |
| 925 | Args.AddAllArgs(CmdArgs, options::OPT_T_Group); |
| 926 | Args.AddAllArgs(CmdArgs, options::OPT_F); |
| 927 | |
| 928 | const char *Exec = |
| 929 | Args.MakeArgString(getToolChain().GetProgramPath(C, "collect2").c_str()); |
| 930 | Dest.addCommand(new Command(Exec, CmdArgs)); |
| 931 | |
| 932 | if (Args.getLastArg(options::OPT_g_Group) && |
| 933 | !Args.getLastArg(options::OPT_gstabs) && |
| 934 | !Args.getLastArg(options::OPT_g0)) { |
| 935 | // FIXME: This is gross, but matches gcc. The test only considers |
| 936 | // the suffix (not the -x type), and then only of the first |
| 937 | // input. Awesome. |
| 938 | const char *Suffix = strchr(Inputs[0].getBaseInput(), '.'); |
| 939 | if (Suffix && isSourceSuffix(Suffix + 1)) { |
| 940 | const char *Exec = |
| 941 | Args.MakeArgString(getToolChain().GetProgramPath(C, "dsymutil").c_str()); |
| 942 | ArgStringList CmdArgs; |
| 943 | CmdArgs.push_back(Output.getFilename()); |
| 944 | C.getJobs().addCommand(new Command(Exec, CmdArgs)); |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | |
Daniel Dunbar | ff7488d | 2009-03-20 00:52:38 +0000 | [diff] [blame] | 949 | void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA, |
Daniel Dunbar | 8cac5f7 | 2009-03-20 16:06:39 +0000 | [diff] [blame] | 950 | Job &Dest, const InputInfo &Output, |
Daniel Dunbar | ff7488d | 2009-03-20 00:52:38 +0000 | [diff] [blame] | 951 | const InputInfoList &Inputs, |
| 952 | const ArgList &Args, |
| 953 | const char *LinkingOutput) const { |
| 954 | ArgStringList CmdArgs; |
| 955 | |
| 956 | CmdArgs.push_back("-create"); |
| 957 | assert(Output.isFilename() && "Unexpected lipo output."); |
Daniel Dunbar | a428df8 | 2009-03-24 00:24:37 +0000 | [diff] [blame] | 958 | |
| 959 | CmdArgs.push_back("-output"); |
Daniel Dunbar | ff7488d | 2009-03-20 00:52:38 +0000 | [diff] [blame] | 960 | CmdArgs.push_back(Output.getFilename()); |
Daniel Dunbar | a428df8 | 2009-03-24 00:24:37 +0000 | [diff] [blame] | 961 | |
Daniel Dunbar | ff7488d | 2009-03-20 00:52:38 +0000 | [diff] [blame] | 962 | for (InputInfoList::const_iterator |
| 963 | it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { |
| 964 | const InputInfo &II = *it; |
| 965 | assert(II.isFilename() && "Unexpected lipo input."); |
| 966 | CmdArgs.push_back(II.getFilename()); |
| 967 | } |
| 968 | const char *Exec = |
| 969 | Args.MakeArgString(getToolChain().GetProgramPath(C, "lipo").c_str()); |
| 970 | Dest.addCommand(new Command(Exec, CmdArgs)); |
| 971 | } |