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 | 40f1265 | 2009-03-29 17:08:39 +0000 | [diff] [blame] | 460 | const char *darwin::CC1::getCC1Name(types::ID Type) const { |
| 461 | switch (Type) { |
| 462 | default: |
| 463 | assert(0 && "Unexpected type for Darwin CC1 tool."); |
| 464 | case types::TY_Asm: |
| 465 | case types::TY_C: case types::TY_CHeader: |
| 466 | case types::TY_PP_C: case types::TY_PP_CHeader: |
| 467 | return "cc1"; |
| 468 | case types::TY_ObjC: case types::TY_ObjCHeader: |
| 469 | case types::TY_PP_ObjC: case types::TY_PP_ObjCHeader: |
| 470 | return "cc1obj"; |
| 471 | case types::TY_CXX: case types::TY_CXXHeader: |
| 472 | case types::TY_PP_CXX: case types::TY_PP_CXXHeader: |
| 473 | return "cc1plus"; |
| 474 | case types::TY_ObjCXX: case types::TY_ObjCXXHeader: |
| 475 | case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXXHeader: |
| 476 | return "cc1objplus"; |
| 477 | } |
| 478 | } |
| 479 | |
Daniel Dunbar | a3ec60e | 2009-03-29 18:40:18 +0000 | [diff] [blame] | 480 | const char *darwin::CC1::getBaseInputName(const ArgList &Args, |
| 481 | const InputInfoList &Inputs) const { |
| 482 | llvm::sys::Path P(Inputs[0].getBaseInput()); |
| 483 | return Args.MakeArgString(P.getLast().c_str()); |
| 484 | } |
| 485 | |
| 486 | const char *darwin::CC1::getBaseInputStem(const ArgList &Args, |
| 487 | const InputInfoList &Inputs) const { |
| 488 | const char *Str = getBaseInputName(Args, Inputs); |
| 489 | |
| 490 | if (const char *End = strchr(Str, '.')) |
| 491 | return Args.MakeArgString(std::string(Str, End).c_str()); |
| 492 | |
| 493 | return Str; |
| 494 | } |
| 495 | |
| 496 | const char * |
| 497 | darwin::CC1::getDependencyFileName(const ArgList &Args, |
| 498 | const InputInfoList &Inputs) const { |
| 499 | // FIXME: Think about this more. |
| 500 | std::string Res; |
| 501 | |
| 502 | if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) { |
| 503 | std::string Str(OutputOpt->getValue(Args)); |
| 504 | |
| 505 | Res = Str.substr(0, Str.rfind('.')); |
| 506 | } else |
| 507 | Res = getBaseInputStem(Args, Inputs); |
| 508 | |
| 509 | return Args.MakeArgString((Res + ".d").c_str()); |
| 510 | } |
| 511 | |
| 512 | void darwin::CC1::AddCC1Args(const ArgList &Args, |
| 513 | ArgStringList &CmdArgs) const { |
| 514 | // Derived from cc1 spec. |
| 515 | |
| 516 | // FIXME: -fapple-kext seems to disable this too. Investigate. |
| 517 | if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) && |
| 518 | !Args.hasArg(options::OPT_mdynamic_no_pic)) |
| 519 | CmdArgs.push_back("-fPIC"); |
| 520 | |
| 521 | // gcc has some code here to deal with when no -mmacosx-version-min |
| 522 | // and no -miphoneos-version-min is present, but this never happens |
| 523 | // due to tool chain specific argument translation. |
| 524 | |
| 525 | // FIXME: Remove mthumb |
| 526 | // FIXME: Remove mno-thumb |
| 527 | // FIXME: Remove faltivec |
| 528 | // FIXME: Remove mno-fused-madd |
| 529 | // FIXME: Remove mlong-branch |
| 530 | // FIXME: Remove mlongcall |
| 531 | // FIXME: Remove mcpu=G4 |
| 532 | // FIXME: Remove mcpu=G5 |
| 533 | |
| 534 | if (Args.hasArg(options::OPT_g_Flag) && |
| 535 | !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols)) |
| 536 | CmdArgs.push_back("-feliminate-unused-debug-symbols"); |
| 537 | } |
| 538 | |
| 539 | void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs, |
| 540 | const InputInfoList &Inputs, |
| 541 | const ArgStringList &OutputArgs) const { |
| 542 | const Driver &D = getToolChain().getHost().getDriver(); |
| 543 | |
| 544 | // Derived from cc1_options spec. |
| 545 | if (Args.hasArg(options::OPT_fast) || |
| 546 | Args.hasArg(options::OPT_fastf) || |
| 547 | Args.hasArg(options::OPT_fastcp)) |
| 548 | CmdArgs.push_back("-O3"); |
| 549 | |
| 550 | if (Arg *A = Args.getLastArg(options::OPT_pg)) |
| 551 | if (Args.hasArg(options::OPT_fomit_frame_pointer)) |
| 552 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 553 | << A->getAsString(Args) << "-fomit-frame-pointer"; |
| 554 | |
| 555 | AddCC1Args(Args, CmdArgs); |
| 556 | |
| 557 | if (!Args.hasArg(options::OPT_Q)) |
| 558 | CmdArgs.push_back("-quiet"); |
| 559 | |
| 560 | CmdArgs.push_back("-dumpbase"); |
| 561 | CmdArgs.push_back(getBaseInputName(Args, Inputs)); |
| 562 | |
| 563 | Args.AddAllArgs(CmdArgs, options::OPT_d_Group); |
| 564 | |
| 565 | Args.AddAllArgs(CmdArgs, options::OPT_m_Group); |
| 566 | Args.AddAllArgs(CmdArgs, options::OPT_a_Group); |
| 567 | |
| 568 | // FIXME: The goal is to use the user provided -o if that is our |
| 569 | // final output, otherwise to drive from the original input |
| 570 | // name. Find a clean way to go about this. |
| 571 | if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) && |
| 572 | Args.hasArg(options::OPT_o)) { |
| 573 | Arg *OutputOpt = Args.getLastArg(options::OPT_o); |
| 574 | CmdArgs.push_back("-auxbase-strip"); |
| 575 | CmdArgs.push_back(OutputOpt->getValue(Args)); |
| 576 | } else { |
| 577 | CmdArgs.push_back("-auxbase"); |
| 578 | CmdArgs.push_back(getBaseInputStem(Args, Inputs)); |
| 579 | } |
| 580 | |
| 581 | Args.AddAllArgs(CmdArgs, options::OPT_g_Group); |
| 582 | |
| 583 | Args.AddAllArgs(CmdArgs, options::OPT_O); |
| 584 | // FIXME: -Wall is getting some special treatment. Investigate. |
| 585 | Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group); |
| 586 | Args.AddLastArg(CmdArgs, options::OPT_w); |
| 587 | Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi, |
| 588 | options::OPT_trigraphs); |
| 589 | if (Args.hasArg(options::OPT_v)) |
| 590 | CmdArgs.push_back("-version"); |
| 591 | if (Args.hasArg(options::OPT_pg)) |
| 592 | CmdArgs.push_back("-p"); |
| 593 | Args.AddLastArg(CmdArgs, options::OPT_p); |
| 594 | |
| 595 | // The driver treats -fsyntax-only specially. |
| 596 | Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only); |
| 597 | |
| 598 | Args.AddAllArgs(CmdArgs, options::OPT_undef); |
| 599 | if (Args.hasArg(options::OPT_Qn)) |
| 600 | CmdArgs.push_back("-fno-ident"); |
| 601 | |
| 602 | // FIXME: This isn't correct. |
| 603 | //Args.AddLastArg(CmdArgs, options::OPT__help) |
| 604 | //Args.AddLastArg(CmdArgs, options::OPT__targetHelp) |
| 605 | |
| 606 | CmdArgs.append(OutputArgs.begin(), OutputArgs.end()); |
| 607 | |
| 608 | // FIXME: Still don't get what is happening here. Investigate. |
| 609 | Args.AddAllArgs(CmdArgs, options::OPT__param); |
| 610 | |
| 611 | if (Args.hasArg(options::OPT_fmudflap) || |
| 612 | Args.hasArg(options::OPT_fmudflapth)) { |
| 613 | CmdArgs.push_back("-fno-builtin"); |
| 614 | CmdArgs.push_back("-fno-merge-constants"); |
| 615 | } |
| 616 | |
| 617 | if (Args.hasArg(options::OPT_coverage)) { |
| 618 | CmdArgs.push_back("-fprofile-arcs"); |
| 619 | CmdArgs.push_back("-ftest-coverage"); |
| 620 | } |
| 621 | |
| 622 | if (types::isCXX(Inputs[0].getType())) |
| 623 | CmdArgs.push_back("-D__private_extern__=extern"); |
| 624 | } |
| 625 | |
| 626 | void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs, |
| 627 | const InputInfoList &Inputs, |
| 628 | const ArgStringList &OutputArgs) const { |
| 629 | // Derived from cpp_options |
| 630 | AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs); |
| 631 | |
| 632 | CmdArgs.append(OutputArgs.begin(), OutputArgs.end()); |
| 633 | |
| 634 | AddCC1Args(Args, CmdArgs); |
| 635 | |
| 636 | // NOTE: The code below has some commonality with cpp_options, but |
| 637 | // in classic gcc style ends up sending things in different |
| 638 | // orders. This may be a good merge candidate once we drop pedantic |
| 639 | // compatibility. |
| 640 | |
| 641 | Args.AddAllArgs(CmdArgs, options::OPT_m_Group); |
| 642 | Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi, |
| 643 | options::OPT_trigraphs); |
| 644 | Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group); |
| 645 | Args.AddLastArg(CmdArgs, options::OPT_w); |
| 646 | |
| 647 | // The driver treats -fsyntax-only specially. |
| 648 | Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only); |
| 649 | |
| 650 | if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) && |
| 651 | !Args.hasArg(options::OPT_fno_working_directory)) |
| 652 | CmdArgs.push_back("-fworking-directory"); |
| 653 | |
| 654 | Args.AddAllArgs(CmdArgs, options::OPT_O); |
| 655 | Args.AddAllArgs(CmdArgs, options::OPT_undef); |
| 656 | if (Args.hasArg(options::OPT_save_temps)) |
| 657 | CmdArgs.push_back("-fpch-preprocess"); |
| 658 | } |
| 659 | |
| 660 | void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args, |
| 661 | ArgStringList &CmdArgs, |
| 662 | const InputInfoList &Inputs) const |
| 663 | { |
| 664 | const Driver &D = getToolChain().getHost().getDriver(); |
| 665 | |
| 666 | // Derived from cpp_unique_options. |
| 667 | Arg *A; |
| 668 | if ((A = Args.getLastArg(options::OPT_C)) || |
| 669 | (A = Args.getLastArg(options::OPT_CC))) { |
| 670 | if (!Args.hasArg(options::OPT_E)) |
| 671 | D.Diag(clang::diag::err_drv_argument_only_allowed_with) |
| 672 | << A->getAsString(Args) << "-E"; |
| 673 | } |
| 674 | if (!Args.hasArg(options::OPT_Q)) |
| 675 | CmdArgs.push_back("-quiet"); |
| 676 | Args.AddAllArgs(CmdArgs, options::OPT_nostdinc); |
| 677 | Args.AddLastArg(CmdArgs, options::OPT_v); |
| 678 | Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F); |
| 679 | Args.AddLastArg(CmdArgs, options::OPT_P); |
| 680 | |
| 681 | // FIXME: Handle %I properly. |
| 682 | if (getToolChain().getArchName() == "x86_64") { |
| 683 | CmdArgs.push_back("-imultilib"); |
| 684 | CmdArgs.push_back("x86_64"); |
| 685 | } |
| 686 | |
| 687 | if (Args.hasArg(options::OPT_MD)) { |
| 688 | CmdArgs.push_back("-MD"); |
| 689 | CmdArgs.push_back(getDependencyFileName(Args, Inputs)); |
| 690 | } |
| 691 | |
| 692 | if (Args.hasArg(options::OPT_MMD)) { |
| 693 | CmdArgs.push_back("-MMD"); |
| 694 | CmdArgs.push_back(getDependencyFileName(Args, Inputs)); |
| 695 | } |
| 696 | |
| 697 | Args.AddLastArg(CmdArgs, options::OPT_M); |
| 698 | Args.AddLastArg(CmdArgs, options::OPT_MM); |
| 699 | Args.AddAllArgs(CmdArgs, options::OPT_MF); |
| 700 | Args.AddLastArg(CmdArgs, options::OPT_MG); |
| 701 | Args.AddLastArg(CmdArgs, options::OPT_MP); |
| 702 | Args.AddAllArgs(CmdArgs, options::OPT_MQ); |
| 703 | Args.AddAllArgs(CmdArgs, options::OPT_MT); |
| 704 | if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) && |
| 705 | (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) { |
| 706 | if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) { |
| 707 | CmdArgs.push_back("-MQ"); |
| 708 | CmdArgs.push_back(OutputOpt->getValue(Args)); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | Args.AddLastArg(CmdArgs, options::OPT_remap); |
| 713 | if (Args.hasArg(options::OPT_g3)) |
| 714 | CmdArgs.push_back("-dD"); |
| 715 | Args.AddLastArg(CmdArgs, options::OPT_H); |
| 716 | |
| 717 | AddCPPArgs(Args, CmdArgs); |
| 718 | |
| 719 | Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A); |
| 720 | Args.AddAllArgs(CmdArgs, options::OPT_i_Group); |
| 721 | |
| 722 | for (InputInfoList::const_iterator |
| 723 | it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { |
| 724 | const InputInfo &II = *it; |
| 725 | |
| 726 | if (II.isPipe()) |
| 727 | CmdArgs.push_back("-"); |
| 728 | else |
| 729 | CmdArgs.push_back(II.getFilename()); |
| 730 | } |
| 731 | |
| 732 | Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA, |
| 733 | options::OPT_Xpreprocessor); |
| 734 | |
| 735 | if (Args.hasArg(options::OPT_fmudflap)) { |
| 736 | CmdArgs.push_back("-D_MUDFLAP"); |
| 737 | CmdArgs.push_back("-include"); |
| 738 | CmdArgs.push_back("mf-runtime.h"); |
| 739 | } |
| 740 | |
| 741 | if (Args.hasArg(options::OPT_fmudflapth)) { |
| 742 | CmdArgs.push_back("-D_MUDFLAP"); |
| 743 | CmdArgs.push_back("-D_MUDFLAPTH"); |
| 744 | CmdArgs.push_back("-include"); |
| 745 | CmdArgs.push_back("mf-runtime.h"); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | void darwin::CC1::AddCPPArgs(const ArgList &Args, |
| 750 | ArgStringList &CmdArgs) const { |
| 751 | // Derived from cpp spec. |
| 752 | |
| 753 | if (Args.hasArg(options::OPT_static)) { |
| 754 | // The gcc spec is broken here, it refers to dynamic but |
| 755 | // that has been translated. Start by being bug compatible. |
| 756 | |
| 757 | // if (!Args.hasArg(arglist.parser.dynamicOption)) |
| 758 | CmdArgs.push_back("-D__STATIC__"); |
| 759 | } else |
| 760 | CmdArgs.push_back("-D__DYNAMIC__"); |
| 761 | |
| 762 | if (Args.hasArg(options::OPT_pthread)) |
| 763 | CmdArgs.push_back("-D_REENTRANT"); |
| 764 | } |
| 765 | |
Daniel Dunbar | 40f1265 | 2009-03-29 17:08:39 +0000 | [diff] [blame] | 766 | void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA, |
| 767 | Job &Dest, const InputInfo &Output, |
| 768 | const InputInfoList &Inputs, |
| 769 | const ArgList &Args, |
| 770 | const char *LinkingOutput) const { |
| 771 | ArgStringList CmdArgs; |
| 772 | |
| 773 | assert(Inputs.size() == 1 && "Unexpected number of inputs!"); |
| 774 | |
| 775 | CmdArgs.push_back("-E"); |
| 776 | |
| 777 | if (Args.hasArg(options::OPT_traditional) || |
| 778 | Args.hasArg(options::OPT_ftraditional) || |
| 779 | Args.hasArg(options::OPT_traditional_cpp)) |
| 780 | CmdArgs.push_back("-traditional-cpp"); |
| 781 | |
| 782 | ArgStringList OutputArgs; |
| 783 | if (Output.isFilename()) { |
| 784 | OutputArgs.push_back("-o"); |
| 785 | OutputArgs.push_back(Output.getFilename()); |
| 786 | } else { |
| 787 | assert(Output.isPipe() && "Unexpected CC1 output."); |
| 788 | } |
| 789 | |
| 790 | AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs); |
| 791 | |
| 792 | Args.AddAllArgs(CmdArgs, options::OPT_d_Group); |
| 793 | |
| 794 | const char *CC1Name = getCC1Name(Inputs[0].getType()); |
| 795 | const char *Exec = |
| 796 | Args.MakeArgString(getToolChain().GetProgramPath(C, CC1Name).c_str()); |
| 797 | Dest.addCommand(new Command(Exec, CmdArgs)); |
| 798 | } |
| 799 | |
| 800 | void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA, |
| 801 | Job &Dest, const InputInfo &Output, |
| 802 | const InputInfoList &Inputs, |
| 803 | const ArgList &Args, |
| 804 | const char *LinkingOutput) const { |
| 805 | const Driver &D = getToolChain().getHost().getDriver(); |
| 806 | ArgStringList CmdArgs; |
| 807 | |
| 808 | assert(Inputs.size() == 1 && "Unexpected number of inputs!"); |
| 809 | |
| 810 | types::ID InputType = Inputs[0].getType(); |
| 811 | const Arg *A; |
| 812 | if ((A = Args.getLastArg(options::OPT_traditional)) || |
| 813 | (A = Args.getLastArg(options::OPT_ftraditional))) |
| 814 | D.Diag(clang::diag::err_drv_argument_only_allowed_with) |
| 815 | << A->getAsString(Args) << "-E"; |
| 816 | |
| 817 | if (Output.getType() == types::TY_LLVMAsm) |
| 818 | CmdArgs.push_back("-emit-llvm"); |
| 819 | else if (Output.getType() == types::TY_LLVMBC) |
| 820 | CmdArgs.push_back("-emit-llvm-bc"); |
| 821 | |
| 822 | ArgStringList OutputArgs; |
| 823 | if (Output.getType() != types::TY_PCH) { |
| 824 | OutputArgs.push_back("-o"); |
| 825 | if (Output.isPipe()) |
| 826 | OutputArgs.push_back("-"); |
| 827 | else if (Output.isNothing()) |
| 828 | OutputArgs.push_back("/dev/null"); |
| 829 | else |
| 830 | OutputArgs.push_back(Output.getFilename()); |
| 831 | } |
| 832 | |
| 833 | // There is no need for this level of compatibility, but it makes |
| 834 | // diffing easier. |
| 835 | bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) || |
| 836 | Args.hasArg(options::OPT_S)); |
| 837 | |
| 838 | if (types::getPreprocessedType(InputType) != types::TY_INVALID) { |
Daniel Dunbar | a3ec60e | 2009-03-29 18:40:18 +0000 | [diff] [blame] | 839 | AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs); |
Daniel Dunbar | 40f1265 | 2009-03-29 17:08:39 +0000 | [diff] [blame] | 840 | if (OutputArgsEarly) { |
| 841 | AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs); |
| 842 | } else { |
| 843 | AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList()); |
| 844 | CmdArgs.append(OutputArgs.begin(), OutputArgs.end()); |
| 845 | } |
| 846 | } else { |
| 847 | CmdArgs.push_back("-fpreprocessed"); |
| 848 | |
| 849 | // FIXME: There is a spec command to remove |
| 850 | // -fpredictive-compilation args here. Investigate. |
| 851 | |
| 852 | for (InputInfoList::const_iterator |
| 853 | it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { |
| 854 | const InputInfo &II = *it; |
| 855 | |
| 856 | if (II.isPipe()) |
| 857 | CmdArgs.push_back("-"); |
| 858 | else |
| 859 | CmdArgs.push_back(II.getFilename()); |
| 860 | } |
| 861 | |
| 862 | if (OutputArgsEarly) { |
| 863 | AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs); |
| 864 | } else { |
| 865 | AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList()); |
| 866 | CmdArgs.append(OutputArgs.begin(), OutputArgs.end()); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | if (Output.getType() == types::TY_PCH) { |
| 871 | assert(Output.isFilename() && "Invalid PCH output."); |
| 872 | |
| 873 | CmdArgs.push_back("-o"); |
| 874 | // NOTE: gcc uses a temp .s file for this, but there doesn't seem |
| 875 | // to be a good reason. |
| 876 | CmdArgs.push_back("/dev/null"); |
| 877 | |
| 878 | CmdArgs.push_back("--output-pch="); |
| 879 | CmdArgs.push_back(Output.getFilename()); |
| 880 | } |
| 881 | |
| 882 | const char *CC1Name = getCC1Name(Inputs[0].getType()); |
| 883 | const char *Exec = |
| 884 | Args.MakeArgString(getToolChain().GetProgramPath(C, CC1Name).c_str()); |
| 885 | Dest.addCommand(new Command(Exec, CmdArgs)); |
| 886 | } |
| 887 | |
Daniel Dunbar | 8cac5f7 | 2009-03-20 16:06:39 +0000 | [diff] [blame] | 888 | void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA, |
| 889 | Job &Dest, const InputInfo &Output, |
| 890 | const InputInfoList &Inputs, |
| 891 | const ArgList &Args, |
| 892 | const char *LinkingOutput) const { |
| 893 | ArgStringList CmdArgs; |
| 894 | |
| 895 | assert(Inputs.size() == 1 && "Unexpected number of inputs."); |
| 896 | const InputInfo &Input = Inputs[0]; |
| 897 | |
| 898 | // Bit of a hack, this is only used for original inputs. |
| 899 | if (Input.isFilename() && |
| 900 | strcmp(Input.getFilename(), Input.getBaseInput()) == 0 && |
| 901 | Args.hasArg(options::OPT_g_Group)) |
| 902 | CmdArgs.push_back("--gstabs"); |
| 903 | |
| 904 | // Derived from asm spec. |
| 905 | CmdArgs.push_back("-arch"); |
| 906 | CmdArgs.push_back(getToolChain().getArchName().c_str()); |
| 907 | |
| 908 | CmdArgs.push_back("-force_cpusubtype_ALL"); |
| 909 | if ((Args.hasArg(options::OPT_mkernel) || |
| 910 | Args.hasArg(options::OPT_static) || |
| 911 | Args.hasArg(options::OPT_fapple_kext)) && |
| 912 | !Args.hasArg(options::OPT_dynamic)) |
| 913 | CmdArgs.push_back("-static"); |
| 914 | |
| 915 | Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, |
| 916 | options::OPT_Xassembler); |
| 917 | |
| 918 | assert(Output.isFilename() && "Unexpected lipo output."); |
| 919 | CmdArgs.push_back("-o"); |
| 920 | CmdArgs.push_back(Output.getFilename()); |
| 921 | |
| 922 | if (Input.isPipe()) { |
| 923 | CmdArgs.push_back("-"); |
| 924 | } else { |
| 925 | assert(Input.isFilename() && "Invalid input."); |
| 926 | CmdArgs.push_back(Input.getFilename()); |
| 927 | } |
| 928 | |
| 929 | // asm_final spec is empty. |
| 930 | |
| 931 | const char *Exec = |
| 932 | Args.MakeArgString(getToolChain().GetProgramPath(C, "as").c_str()); |
| 933 | Dest.addCommand(new Command(Exec, CmdArgs)); |
| 934 | } |
Daniel Dunbar | ff7488d | 2009-03-20 00:52:38 +0000 | [diff] [blame] | 935 | |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 936 | static const char *MakeFormattedString(const ArgList &Args, |
| 937 | const llvm::format_object_base &Fmt) { |
| 938 | std::string Str; |
| 939 | llvm::raw_string_ostream(Str) << Fmt; |
| 940 | return Args.MakeArgString(Str.c_str()); |
| 941 | } |
| 942 | |
| 943 | /// Helper routine for seeing if we should use dsymutil; this is a |
| 944 | /// gcc compatible hack, we should remove it and use the input |
| 945 | /// type information. |
| 946 | static bool isSourceSuffix(const char *Str) { |
| 947 | // match: 'C', 'CPP', 'c', 'cc', 'cp', 'c++', 'cpp', 'cxx', 'm', |
| 948 | // 'mm'. |
| 949 | switch (strlen(Str)) { |
| 950 | default: |
| 951 | return false; |
| 952 | case 1: |
| 953 | return (memcmp(Str, "C", 1) == 0 || |
| 954 | memcmp(Str, "c", 1) == 0 || |
| 955 | memcmp(Str, "m", 1) == 0); |
| 956 | case 2: |
| 957 | return (memcmp(Str, "cc", 2) == 0 || |
| 958 | memcmp(Str, "cp", 2) == 0 || |
| 959 | memcmp(Str, "mm", 2) == 0); |
| 960 | case 3: |
| 961 | return (memcmp(Str, "CPP", 3) == 0 || |
| 962 | memcmp(Str, "c++", 3) == 0 || |
| 963 | memcmp(Str, "cpp", 3) == 0 || |
| 964 | memcmp(Str, "cxx", 3) == 0); |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | static bool isMacosxVersionLT(unsigned (&A)[3], unsigned (&B)[3]) { |
| 969 | for (unsigned i=0; i < 3; ++i) { |
| 970 | if (A[i] > B[i]) return false; |
| 971 | if (A[i] < B[i]) return true; |
| 972 | } |
| 973 | return false; |
| 974 | } |
| 975 | |
| 976 | static bool isMacosxVersionLT(unsigned (&A)[3], |
| 977 | unsigned V0, unsigned V1=0, unsigned V2=0) { |
| 978 | unsigned B[3] = { V0, V1, V2 }; |
| 979 | return isMacosxVersionLT(A, B); |
| 980 | } |
| 981 | |
| 982 | static bool isMacosxVersionGTE(unsigned(&A)[3], |
| 983 | unsigned V0, unsigned V1=0, unsigned V2=0) { |
| 984 | return !isMacosxVersionLT(A, V0, V1, V2); |
| 985 | } |
| 986 | |
| 987 | const toolchains::Darwin_X86 &darwin::Link::getDarwinToolChain() const { |
| 988 | return reinterpret_cast<const toolchains::Darwin_X86&>(getToolChain()); |
| 989 | } |
| 990 | |
| 991 | void darwin::Link::AddDarwinArch(const ArgList &Args, |
| 992 | ArgStringList &CmdArgs) const { |
| 993 | // Derived from darwin_arch spec. |
| 994 | CmdArgs.push_back("-arch"); |
| 995 | CmdArgs.push_back(getToolChain().getArchName().c_str()); |
| 996 | } |
| 997 | |
| 998 | void darwin::Link::AddDarwinSubArch(const ArgList &Args, |
| 999 | ArgStringList &CmdArgs) const { |
| 1000 | // Derived from darwin_subarch spec, not sure what the distinction |
| 1001 | // exists for but at least for this chain it is the same. |
| 1002 | AddDarwinArch(Args, CmdArgs); |
| 1003 | } |
| 1004 | |
| 1005 | void darwin::Link::AddLinkArgs(const ArgList &Args, |
| 1006 | ArgStringList &CmdArgs) const { |
| 1007 | const Driver &D = getToolChain().getHost().getDriver(); |
| 1008 | |
| 1009 | // Derived from the "link" spec. |
| 1010 | Args.AddAllArgs(CmdArgs, options::OPT_static); |
| 1011 | if (!Args.hasArg(options::OPT_static)) |
| 1012 | CmdArgs.push_back("-dynamic"); |
| 1013 | if (Args.hasArg(options::OPT_fgnu_runtime)) { |
| 1014 | // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu |
| 1015 | // here. How do we wish to handle such things? |
| 1016 | } |
| 1017 | |
| 1018 | if (!Args.hasArg(options::OPT_dynamiclib)) { |
| 1019 | if (Args.hasArg(options::OPT_force__cpusubtype__ALL)) { |
| 1020 | AddDarwinArch(Args, CmdArgs); |
| 1021 | CmdArgs.push_back("-force_cpusubtype_ALL"); |
| 1022 | } else |
| 1023 | AddDarwinSubArch(Args, CmdArgs); |
| 1024 | |
| 1025 | Args.AddLastArg(CmdArgs, options::OPT_bundle); |
| 1026 | Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader); |
| 1027 | Args.AddAllArgs(CmdArgs, options::OPT_client__name); |
| 1028 | |
| 1029 | Arg *A; |
| 1030 | if ((A = Args.getLastArg(options::OPT_compatibility__version)) || |
| 1031 | (A = Args.getLastArg(options::OPT_current__version)) || |
| 1032 | (A = Args.getLastArg(options::OPT_install__name))) |
| 1033 | D.Diag(clang::diag::err_drv_argument_only_allowed_with) |
| 1034 | << A->getAsString(Args) << "-dynamiclib"; |
| 1035 | |
| 1036 | Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace); |
| 1037 | Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs); |
| 1038 | Args.AddLastArg(CmdArgs, options::OPT_private__bundle); |
| 1039 | } else { |
| 1040 | CmdArgs.push_back("-dylib"); |
| 1041 | |
| 1042 | Arg *A; |
| 1043 | if ((A = Args.getLastArg(options::OPT_bundle)) || |
| 1044 | (A = Args.getLastArg(options::OPT_bundle__loader)) || |
| 1045 | (A = Args.getLastArg(options::OPT_client__name)) || |
| 1046 | (A = Args.getLastArg(options::OPT_force__flat__namespace)) || |
| 1047 | (A = Args.getLastArg(options::OPT_keep__private__externs)) || |
| 1048 | (A = Args.getLastArg(options::OPT_private__bundle))) |
| 1049 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 1050 | << A->getAsString(Args) << "-dynamiclib"; |
| 1051 | |
| 1052 | Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version, |
| 1053 | "-dylib_compatibility_version"); |
| 1054 | Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version, |
| 1055 | "-dylib_current_version"); |
| 1056 | |
| 1057 | if (Args.hasArg(options::OPT_force__cpusubtype__ALL)) { |
| 1058 | AddDarwinArch(Args, CmdArgs); |
| 1059 | // NOTE: We don't add -force_cpusubtype_ALL on this path. Ok. |
| 1060 | } else |
| 1061 | AddDarwinSubArch(Args, CmdArgs); |
| 1062 | |
| 1063 | Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name, |
| 1064 | "-dylib_install_name"); |
| 1065 | } |
| 1066 | |
| 1067 | Args.AddLastArg(CmdArgs, options::OPT_all__load); |
| 1068 | Args.AddAllArgs(CmdArgs, options::OPT_allowable__client); |
| 1069 | Args.AddLastArg(CmdArgs, options::OPT_bind__at__load); |
| 1070 | Args.AddLastArg(CmdArgs, options::OPT_dead__strip); |
| 1071 | Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms); |
| 1072 | Args.AddAllArgs(CmdArgs, options::OPT_dylib__file); |
| 1073 | Args.AddLastArg(CmdArgs, options::OPT_dynamic); |
| 1074 | Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list); |
| 1075 | Args.AddLastArg(CmdArgs, options::OPT_flat__namespace); |
| 1076 | Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names); |
| 1077 | Args.AddAllArgs(CmdArgs, options::OPT_image__base); |
| 1078 | Args.AddAllArgs(CmdArgs, options::OPT_init); |
| 1079 | |
| 1080 | if (!Args.hasArg(options::OPT_mmacosx_version_min_EQ)) { |
| 1081 | if (!Args.hasArg(options::OPT_miphoneos_version_min_EQ)) { |
| 1082 | // FIXME: I don't understand what is going on here. This is |
| 1083 | // supposed to come from darwin_ld_minversion, but gcc doesn't |
| 1084 | // seem to be following that; it must be getting overridden |
| 1085 | // somewhere. |
| 1086 | CmdArgs.push_back("-macosx_version_min"); |
| 1087 | CmdArgs.push_back(getDarwinToolChain().getMacosxVersionStr()); |
| 1088 | } |
| 1089 | } else { |
| 1090 | // Adding all arguments doesn't make sense here but this is what |
| 1091 | // gcc does. |
| 1092 | Args.AddAllArgsTranslated(CmdArgs, options::OPT_mmacosx_version_min_EQ, |
| 1093 | "-macosx_version_min"); |
| 1094 | } |
| 1095 | |
| 1096 | Args.AddAllArgsTranslated(CmdArgs, options::OPT_miphoneos_version_min_EQ, |
| 1097 | "-iphoneos_version_min"); |
| 1098 | Args.AddLastArg(CmdArgs, options::OPT_nomultidefs); |
| 1099 | Args.AddLastArg(CmdArgs, options::OPT_multi__module); |
| 1100 | Args.AddLastArg(CmdArgs, options::OPT_single__module); |
| 1101 | Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined); |
| 1102 | Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused); |
| 1103 | |
| 1104 | if (Args.hasArg(options::OPT_fpie)) |
| 1105 | CmdArgs.push_back("-pie"); |
| 1106 | |
| 1107 | Args.AddLastArg(CmdArgs, options::OPT_prebind); |
| 1108 | Args.AddLastArg(CmdArgs, options::OPT_noprebind); |
| 1109 | Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding); |
| 1110 | Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules); |
| 1111 | Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs); |
| 1112 | Args.AddAllArgs(CmdArgs, options::OPT_sectcreate); |
| 1113 | Args.AddAllArgs(CmdArgs, options::OPT_sectorder); |
| 1114 | Args.AddAllArgs(CmdArgs, options::OPT_seg1addr); |
| 1115 | Args.AddAllArgs(CmdArgs, options::OPT_segprot); |
| 1116 | Args.AddAllArgs(CmdArgs, options::OPT_segaddr); |
| 1117 | Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr); |
| 1118 | Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr); |
| 1119 | Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table); |
| 1120 | Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename); |
| 1121 | Args.AddAllArgs(CmdArgs, options::OPT_sub__library); |
| 1122 | Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella); |
| 1123 | Args.AddAllArgsTranslated(CmdArgs, options::OPT_isysroot, "-syslibroot"); |
| 1124 | Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace); |
| 1125 | Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints); |
| 1126 | Args.AddAllArgs(CmdArgs, options::OPT_umbrella); |
| 1127 | Args.AddAllArgs(CmdArgs, options::OPT_undefined); |
| 1128 | Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list); |
| 1129 | Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches); |
| 1130 | |
| 1131 | if (!Args.hasArg(options::OPT_weak__reference__mismatches)) { |
| 1132 | CmdArgs.push_back("-weak_reference_mismatches"); |
| 1133 | CmdArgs.push_back("non-weak"); |
| 1134 | } |
| 1135 | |
| 1136 | Args.AddLastArg(CmdArgs, options::OPT_X_Flag); |
| 1137 | Args.AddAllArgs(CmdArgs, options::OPT_y); |
| 1138 | Args.AddLastArg(CmdArgs, options::OPT_w); |
| 1139 | Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size); |
| 1140 | Args.AddAllArgs(CmdArgs, options::OPT_segs__read__); |
| 1141 | Args.AddLastArg(CmdArgs, options::OPT_seglinkedit); |
| 1142 | Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit); |
| 1143 | Args.AddAllArgs(CmdArgs, options::OPT_sectalign); |
| 1144 | Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols); |
| 1145 | Args.AddAllArgs(CmdArgs, options::OPT_segcreate); |
| 1146 | Args.AddLastArg(CmdArgs, options::OPT_whyload); |
| 1147 | Args.AddLastArg(CmdArgs, options::OPT_whatsloaded); |
| 1148 | Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name); |
| 1149 | Args.AddLastArg(CmdArgs, options::OPT_dylinker); |
| 1150 | Args.AddLastArg(CmdArgs, options::OPT_Mach); |
| 1151 | } |
| 1152 | |
| 1153 | void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA, |
| 1154 | Job &Dest, const InputInfo &Output, |
| 1155 | const InputInfoList &Inputs, |
| 1156 | const ArgList &Args, |
| 1157 | const char *LinkingOutput) const { |
| 1158 | assert(Output.getType() == types::TY_Image && "Invalid linker output type."); |
| 1159 | // The logic here is derived from gcc's behavior; most of which |
| 1160 | // comes from specs (starting with link_command). Consult gcc for |
| 1161 | // more information. |
| 1162 | |
| 1163 | // FIXME: The spec references -fdump= which seems to have |
| 1164 | // disappeared? |
| 1165 | |
| 1166 | ArgStringList CmdArgs; |
| 1167 | |
| 1168 | // I'm not sure why this particular decomposition exists in gcc, but |
| 1169 | // we follow suite for ease of comparison. |
| 1170 | AddLinkArgs(Args, CmdArgs); |
| 1171 | |
| 1172 | // FIXME: gcc has %{x} in here. How could this ever happen? Cruft? |
| 1173 | Args.AddAllArgs(CmdArgs, options::OPT_d_Flag); |
| 1174 | Args.AddAllArgs(CmdArgs, options::OPT_s); |
| 1175 | Args.AddAllArgs(CmdArgs, options::OPT_t); |
| 1176 | Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag); |
| 1177 | Args.AddAllArgs(CmdArgs, options::OPT_u_Group); |
| 1178 | Args.AddAllArgs(CmdArgs, options::OPT_A); |
| 1179 | Args.AddLastArg(CmdArgs, options::OPT_e); |
| 1180 | Args.AddAllArgs(CmdArgs, options::OPT_m_Separate); |
| 1181 | Args.AddAllArgs(CmdArgs, options::OPT_r); |
| 1182 | |
| 1183 | // FIXME: This is just being pedantically bug compatible, gcc |
| 1184 | // doesn't *mean* to forward this, it just does (yay for pattern |
| 1185 | // matching). It doesn't work, of course. |
| 1186 | Args.AddAllArgs(CmdArgs, options::OPT_object); |
| 1187 | |
| 1188 | CmdArgs.push_back("-o"); |
| 1189 | CmdArgs.push_back(Output.getFilename()); |
| 1190 | |
| 1191 | unsigned MacosxVersion[3]; |
| 1192 | if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ)) { |
| 1193 | bool HadExtra; |
| 1194 | if (!Driver::GetReleaseVersion(A->getValue(Args), MacosxVersion[0], |
| 1195 | MacosxVersion[1], MacosxVersion[2], |
| 1196 | HadExtra) || |
| 1197 | HadExtra) { |
| 1198 | const Driver &D = getToolChain().getHost().getDriver(); |
| 1199 | D.Diag(clang::diag::err_drv_invalid_version_number) |
| 1200 | << A->getAsString(Args); |
| 1201 | } |
| 1202 | } else { |
| 1203 | getDarwinToolChain().getMacosxVersion(MacosxVersion); |
| 1204 | } |
| 1205 | |
| 1206 | if (!Args.hasArg(options::OPT_A) && |
| 1207 | !Args.hasArg(options::OPT_nostdlib) && |
| 1208 | !Args.hasArg(options::OPT_nostartfiles)) { |
| 1209 | // Derived from startfile spec. |
| 1210 | if (Args.hasArg(options::OPT_dynamiclib)) { |
| 1211 | // Derived from darwin_dylib1 spec. |
| 1212 | if (Args.hasArg(options::OPT_miphoneos_version_min_EQ) || |
| 1213 | isMacosxVersionLT(MacosxVersion, 10, 5)) |
| 1214 | CmdArgs.push_back("-ldylib1.o"); |
| 1215 | else |
| 1216 | CmdArgs.push_back("-ldylib1.10.5.o"); |
| 1217 | } else { |
| 1218 | if (Args.hasArg(options::OPT_bundle)) { |
| 1219 | if (!Args.hasArg(options::OPT_static)) |
| 1220 | CmdArgs.push_back("-lbundle1.o"); |
| 1221 | } else { |
| 1222 | if (Args.hasArg(options::OPT_pg)) { |
| 1223 | if (Args.hasArg(options::OPT_static) || |
| 1224 | Args.hasArg(options::OPT_object) || |
| 1225 | Args.hasArg(options::OPT_preload)) { |
| 1226 | CmdArgs.push_back("-lgcrt0.o"); |
| 1227 | } else { |
| 1228 | CmdArgs.push_back("-lgcrt1.o"); |
| 1229 | |
| 1230 | // darwin_crt2 spec is empty. |
| 1231 | } |
| 1232 | } else { |
| 1233 | if (Args.hasArg(options::OPT_static) || |
| 1234 | Args.hasArg(options::OPT_object) || |
| 1235 | Args.hasArg(options::OPT_preload)) { |
| 1236 | CmdArgs.push_back("-lcrt0.o"); |
| 1237 | } else { |
| 1238 | // Derived from darwin_crt1 spec. |
| 1239 | if (Args.hasArg(options::OPT_miphoneos_version_min_EQ) || |
| 1240 | isMacosxVersionLT(MacosxVersion, 10, 5)) { |
| 1241 | CmdArgs.push_back("-lcrt1.o"); |
| 1242 | } else { |
| 1243 | CmdArgs.push_back("-lcrt1.10.5.o"); |
| 1244 | |
| 1245 | // darwin_crt2 spec is empty. |
| 1246 | } |
| 1247 | } |
| 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | if (Args.hasArg(options::OPT_shared_libgcc) && |
| 1253 | !Args.hasArg(options::OPT_miphoneos_version_min_EQ) && |
| 1254 | isMacosxVersionLT(MacosxVersion, 10, 5)) { |
| 1255 | const char *Str = getToolChain().GetFilePath(C, "crt3.o").c_str(); |
| 1256 | CmdArgs.push_back(Args.MakeArgString(Str)); |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | Args.AddAllArgs(CmdArgs, options::OPT_L); |
| 1261 | |
| 1262 | if (Args.hasArg(options::OPT_fopenmp)) |
| 1263 | // This is more complicated in gcc... |
| 1264 | CmdArgs.push_back("-lgomp"); |
| 1265 | |
| 1266 | // FIXME: Derive these correctly. |
| 1267 | const char *TCDir = getDarwinToolChain().getToolChainDir().c_str(); |
| 1268 | if (getToolChain().getArchName() == "x86_64") { |
| 1269 | CmdArgs.push_back(MakeFormattedString(Args, |
| 1270 | llvm::format("-L/usr/lib/gcc/%s/x86_64", TCDir))); |
| 1271 | // Intentionally duplicated for (temporary) gcc bug compatibility. |
| 1272 | CmdArgs.push_back(MakeFormattedString(Args, |
| 1273 | llvm::format("-L/usr/lib/gcc/%s/x86_64", TCDir))); |
| 1274 | } |
| 1275 | CmdArgs.push_back(MakeFormattedString(Args, |
| 1276 | llvm::format("-L/usr/lib/%s", TCDir))); |
| 1277 | CmdArgs.push_back(MakeFormattedString(Args, |
| 1278 | llvm::format("-L/usr/lib/gcc/%s", TCDir))); |
| 1279 | // Intentionally duplicated for (temporary) gcc bug compatibility. |
| 1280 | CmdArgs.push_back(MakeFormattedString(Args, |
| 1281 | llvm::format("-L/usr/lib/gcc/%s", TCDir))); |
| 1282 | CmdArgs.push_back(MakeFormattedString(Args, |
| 1283 | llvm::format("-L/usr/lib/gcc/%s/../../../%s", TCDir, TCDir))); |
| 1284 | CmdArgs.push_back(MakeFormattedString(Args, |
| 1285 | llvm::format("-L/usr/lib/gcc/%s/../../..", TCDir))); |
| 1286 | |
| 1287 | for (InputInfoList::const_iterator |
| 1288 | it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { |
| 1289 | const InputInfo &II = *it; |
| 1290 | if (II.isFilename()) |
| 1291 | CmdArgs.push_back(II.getFilename()); |
| 1292 | else |
| 1293 | II.getInputArg().renderAsInput(Args, CmdArgs); |
| 1294 | } |
| 1295 | |
| 1296 | if (LinkingOutput) { |
| 1297 | CmdArgs.push_back("-arch_multiple"); |
| 1298 | CmdArgs.push_back("-final_output"); |
| 1299 | CmdArgs.push_back(LinkingOutput); |
| 1300 | } |
| 1301 | |
| 1302 | if (Args.hasArg(options::OPT_fprofile_arcs) || |
| 1303 | Args.hasArg(options::OPT_fprofile_generate) || |
| 1304 | Args.hasArg(options::OPT_fcreate_profile) || |
| 1305 | Args.hasArg(options::OPT_coverage)) |
| 1306 | CmdArgs.push_back("-lgcov"); |
| 1307 | |
| 1308 | if (Args.hasArg(options::OPT_fnested_functions)) |
| 1309 | CmdArgs.push_back("-allow_stack_execute"); |
| 1310 | |
| 1311 | if (!Args.hasArg(options::OPT_nostdlib) && |
| 1312 | !Args.hasArg(options::OPT_nodefaultlibs)) { |
| 1313 | // link_ssp spec is empty. |
| 1314 | |
| 1315 | // Derived from libgcc spec. |
| 1316 | if (Args.hasArg(options::OPT_static)) { |
| 1317 | CmdArgs.push_back("-lgcc_static"); |
| 1318 | } else if (Args.hasArg(options::OPT_static_libgcc)) { |
| 1319 | CmdArgs.push_back("-lgcc_eh"); |
| 1320 | CmdArgs.push_back("-lgcc"); |
| 1321 | } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) { |
| 1322 | // Derived from darwin_iphoneos_libgcc spec. |
| 1323 | CmdArgs.push_back("-lgcc_s.10.5"); |
| 1324 | CmdArgs.push_back("-lgcc"); |
| 1325 | } else if (Args.hasArg(options::OPT_shared_libgcc) || |
| 1326 | Args.hasArg(options::OPT_fexceptions) || |
| 1327 | Args.hasArg(options::OPT_fgnu_runtime)) { |
| 1328 | if (isMacosxVersionLT(MacosxVersion, 10, 5)) |
| 1329 | CmdArgs.push_back("-lgcc_s.10.4"); |
| 1330 | else |
| 1331 | CmdArgs.push_back("-lgcc_s.10.5"); |
| 1332 | CmdArgs.push_back("-lgcc"); |
| 1333 | } else { |
| 1334 | if (isMacosxVersionLT(MacosxVersion, 10, 5) && |
| 1335 | isMacosxVersionGTE(MacosxVersion, 10, 3, 9)) |
| 1336 | CmdArgs.push_back("-lgcc_s.10.4"); |
| 1337 | if (isMacosxVersionGTE(MacosxVersion, 10, 5)) |
| 1338 | CmdArgs.push_back("-lgcc_s.10.5"); |
| 1339 | CmdArgs.push_back("-lgcc"); |
| 1340 | } |
| 1341 | |
| 1342 | // Derived from lib spec. |
| 1343 | if (!Args.hasArg(options::OPT_static)) |
| 1344 | CmdArgs.push_back("-lSystem"); |
| 1345 | } |
| 1346 | |
| 1347 | if (!Args.hasArg(options::OPT_A) && |
| 1348 | !Args.hasArg(options::OPT_nostdlib) && |
| 1349 | !Args.hasArg(options::OPT_nostartfiles)) { |
| 1350 | // endfile_spec is empty. |
| 1351 | } |
| 1352 | |
| 1353 | Args.AddAllArgs(CmdArgs, options::OPT_T_Group); |
| 1354 | Args.AddAllArgs(CmdArgs, options::OPT_F); |
| 1355 | |
| 1356 | const char *Exec = |
| 1357 | Args.MakeArgString(getToolChain().GetProgramPath(C, "collect2").c_str()); |
| 1358 | Dest.addCommand(new Command(Exec, CmdArgs)); |
| 1359 | |
| 1360 | if (Args.getLastArg(options::OPT_g_Group) && |
| 1361 | !Args.getLastArg(options::OPT_gstabs) && |
| 1362 | !Args.getLastArg(options::OPT_g0)) { |
| 1363 | // FIXME: This is gross, but matches gcc. The test only considers |
| 1364 | // the suffix (not the -x type), and then only of the first |
| 1365 | // input. Awesome. |
| 1366 | const char *Suffix = strchr(Inputs[0].getBaseInput(), '.'); |
| 1367 | if (Suffix && isSourceSuffix(Suffix + 1)) { |
| 1368 | const char *Exec = |
| 1369 | Args.MakeArgString(getToolChain().GetProgramPath(C, "dsymutil").c_str()); |
| 1370 | ArgStringList CmdArgs; |
| 1371 | CmdArgs.push_back(Output.getFilename()); |
| 1372 | C.getJobs().addCommand(new Command(Exec, CmdArgs)); |
| 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | |
Daniel Dunbar | ff7488d | 2009-03-20 00:52:38 +0000 | [diff] [blame] | 1377 | void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA, |
Daniel Dunbar | 8cac5f7 | 2009-03-20 16:06:39 +0000 | [diff] [blame] | 1378 | Job &Dest, const InputInfo &Output, |
Daniel Dunbar | ff7488d | 2009-03-20 00:52:38 +0000 | [diff] [blame] | 1379 | const InputInfoList &Inputs, |
| 1380 | const ArgList &Args, |
| 1381 | const char *LinkingOutput) const { |
| 1382 | ArgStringList CmdArgs; |
| 1383 | |
| 1384 | CmdArgs.push_back("-create"); |
| 1385 | assert(Output.isFilename() && "Unexpected lipo output."); |
Daniel Dunbar | a428df8 | 2009-03-24 00:24:37 +0000 | [diff] [blame] | 1386 | |
| 1387 | CmdArgs.push_back("-output"); |
Daniel Dunbar | ff7488d | 2009-03-20 00:52:38 +0000 | [diff] [blame] | 1388 | CmdArgs.push_back(Output.getFilename()); |
Daniel Dunbar | a428df8 | 2009-03-24 00:24:37 +0000 | [diff] [blame] | 1389 | |
Daniel Dunbar | ff7488d | 2009-03-20 00:52:38 +0000 | [diff] [blame] | 1390 | for (InputInfoList::const_iterator |
| 1391 | it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { |
| 1392 | const InputInfo &II = *it; |
| 1393 | assert(II.isFilename() && "Unexpected lipo input."); |
| 1394 | CmdArgs.push_back(II.getFilename()); |
| 1395 | } |
| 1396 | const char *Exec = |
| 1397 | Args.MakeArgString(getToolChain().GetProgramPath(C, "lipo").c_str()); |
| 1398 | Dest.addCommand(new Command(Exec, CmdArgs)); |
| 1399 | } |