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 | 5c1aaaf | 2009-04-07 19:18:24 +0000 | [diff] [blame] | 40 | const Driver &D = getToolChain().getHost().getDriver(); |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 41 | ArgStringList CmdArgs; |
| 42 | |
Daniel Dunbar | 077ba6a | 2009-03-31 20:53:55 +0000 | [diff] [blame] | 43 | assert(Inputs.size() == 1 && "Unable to handle multiple inputs."); |
| 44 | |
Daniel Dunbar | af07f93 | 2009-03-31 17:35:15 +0000 | [diff] [blame] | 45 | CmdArgs.push_back("-triple"); |
| 46 | const char *TripleStr = |
| 47 | Args.MakeArgString(getToolChain().getTripleString().c_str()); |
| 48 | CmdArgs.push_back(TripleStr); |
| 49 | |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 50 | if (isa<AnalyzeJobAction>(JA)) { |
| 51 | assert(JA.getType() == types::TY_Plist && "Invalid output type."); |
| 52 | CmdArgs.push_back("-analyze"); |
| 53 | } else if (isa<PreprocessJobAction>(JA)) { |
Daniel Dunbar | cd8e4c4 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 54 | if (Output.getType() == types::TY_Dependencies) |
| 55 | CmdArgs.push_back("-Eonly"); |
| 56 | else |
| 57 | CmdArgs.push_back("-E"); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 58 | } else if (isa<PrecompileJobAction>(JA)) { |
Daniel Dunbar | bb71b39 | 2009-04-01 03:28:10 +0000 | [diff] [blame] | 59 | CmdArgs.push_back("-emit-pth"); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 60 | } else { |
| 61 | assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool."); |
| 62 | |
| 63 | if (JA.getType() == types::TY_Nothing) { |
| 64 | CmdArgs.push_back("-fsyntax-only"); |
| 65 | } else if (JA.getType() == types::TY_LLVMAsm) { |
| 66 | CmdArgs.push_back("-emit-llvm"); |
| 67 | } else if (JA.getType() == types::TY_LLVMBC) { |
| 68 | CmdArgs.push_back("-emit-llvm-bc"); |
| 69 | } else if (JA.getType() == types::TY_PP_Asm) { |
| 70 | CmdArgs.push_back("-S"); |
| 71 | } |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 74 | // The make clang go fast button. |
| 75 | CmdArgs.push_back("-disable-free"); |
| 76 | |
| 77 | if (isa<AnalyzeJobAction>(JA)) { |
| 78 | // Add default argument set. |
| 79 | // |
| 80 | // FIXME: Move into clang? |
| 81 | CmdArgs.push_back("-warn-dead-stores"); |
| 82 | CmdArgs.push_back("-checker-cfref"); |
Ted Kremenek | 9b646da | 2009-03-25 00:38:14 +0000 | [diff] [blame] | 83 | CmdArgs.push_back("-analyzer-eagerly-assume"); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 84 | CmdArgs.push_back("-warn-objc-methodsigs"); |
| 85 | // Do not enable the missing -dealloc check. |
| 86 | // '-warn-objc-missing-dealloc', |
| 87 | CmdArgs.push_back("-warn-objc-unused-ivars"); |
| 88 | |
| 89 | CmdArgs.push_back("-analyzer-output=plist"); |
| 90 | |
| 91 | // Add -Xanalyzer arguments when running as analyzer. |
| 92 | Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer); |
| 93 | } else { |
| 94 | // Perform argument translation for LLVM backend. This |
| 95 | // takes some care in reconciling with llvm-gcc. The |
| 96 | // issue is that llvm-gcc translates these options based on |
| 97 | // the values in cc1, whereas we are processing based on |
| 98 | // the driver arguments. |
| 99 | // |
| 100 | // FIXME: This is currently broken for -f flags when -fno |
| 101 | // variants are present. |
| 102 | |
| 103 | // This comes from the default translation the driver + cc1 |
| 104 | // would do to enable flag_pic. |
| 105 | // |
| 106 | // FIXME: Centralize this code. |
| 107 | bool PICEnabled = (Args.hasArg(options::OPT_fPIC) || |
| 108 | Args.hasArg(options::OPT_fpic) || |
| 109 | Args.hasArg(options::OPT_fPIE) || |
| 110 | Args.hasArg(options::OPT_fpie)); |
| 111 | bool PICDisabled = (Args.hasArg(options::OPT_mkernel) || |
| 112 | Args.hasArg(options::OPT_static)); |
| 113 | const char *Model = getToolChain().GetForcedPicModel(); |
| 114 | if (!Model) { |
| 115 | if (Args.hasArg(options::OPT_mdynamic_no_pic)) |
| 116 | Model = "dynamic-no-pic"; |
| 117 | else if (PICDisabled) |
| 118 | Model = "static"; |
| 119 | else if (PICEnabled) |
| 120 | Model = "pic"; |
| 121 | else |
| 122 | Model = getToolChain().GetDefaultRelocationModel(); |
| 123 | } |
| 124 | CmdArgs.push_back("--relocation-model"); |
| 125 | CmdArgs.push_back(Model); |
| 126 | |
| 127 | if (Args.hasArg(options::OPT_ftime_report)) |
| 128 | CmdArgs.push_back("--time-passes"); |
| 129 | // FIXME: Set --enable-unsafe-fp-math. |
| 130 | if (!Args.hasArg(options::OPT_fomit_frame_pointer)) |
| 131 | CmdArgs.push_back("--disable-fp-elim"); |
| 132 | if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss, |
| 133 | options::OPT_fno_zero_initialized_in_bss, |
| 134 | true)) |
| 135 | CmdArgs.push_back("--nozero-initialized-in-bss"); |
Daniel Dunbar | b3fd500 | 2009-03-24 17:59:06 +0000 | [diff] [blame] | 136 | if (Args.hasArg(options::OPT_dA) || Args.hasArg(options::OPT_fverbose_asm)) |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 137 | CmdArgs.push_back("--asm-verbose"); |
| 138 | if (Args.hasArg(options::OPT_fdebug_pass_structure)) |
| 139 | CmdArgs.push_back("--debug-pass=Structure"); |
| 140 | if (Args.hasArg(options::OPT_fdebug_pass_arguments)) |
| 141 | CmdArgs.push_back("--debug-pass=Arguments"); |
| 142 | // FIXME: set --inline-threshhold=50 if (optimize_size || optimize |
| 143 | // < 3) |
| 144 | if (Args.hasFlag(options::OPT_funwind_tables, |
| 145 | options::OPT_fno_unwind_tables, |
| 146 | getToolChain().IsUnwindTablesDefault())) |
| 147 | CmdArgs.push_back("--unwind-tables=1"); |
| 148 | else |
| 149 | CmdArgs.push_back("--unwind-tables=0"); |
| 150 | if (!Args.hasFlag(options::OPT_mred_zone, |
| 151 | options::OPT_mno_red_zone, |
| 152 | true)) |
| 153 | CmdArgs.push_back("--disable-red-zone"); |
| 154 | if (Args.hasFlag(options::OPT_msoft_float, |
| 155 | options::OPT_mno_soft_float, |
| 156 | false)) |
| 157 | CmdArgs.push_back("--soft-float"); |
| 158 | |
| 159 | // FIXME: Need target hooks. |
| 160 | if (memcmp(getToolChain().getPlatform().c_str(), "darwin", 6) == 0) { |
| 161 | if (getToolChain().getArchName() == "x86_64") |
| 162 | CmdArgs.push_back("--mcpu=core2"); |
| 163 | else if (getToolChain().getArchName() == "i386") |
| 164 | CmdArgs.push_back("--mcpu=yonah"); |
| 165 | } |
| 166 | |
| 167 | // FIXME: Ignores ordering. Also, we need to find a realistic |
| 168 | // solution for this. |
| 169 | static const struct { |
| 170 | options::ID Pos, Neg; |
| 171 | const char *Name; |
| 172 | } FeatureOptions[] = { |
| 173 | { options::OPT_mmmx, options::OPT_mno_mmx, "mmx" }, |
| 174 | { options::OPT_msse, options::OPT_mno_sse, "sse" }, |
| 175 | { options::OPT_msse2, options::OPT_mno_sse2, "sse2" }, |
| 176 | { options::OPT_msse3, options::OPT_mno_sse3, "sse3" }, |
| 177 | { options::OPT_mssse3, options::OPT_mno_ssse3, "ssse3" }, |
| 178 | { options::OPT_msse41, options::OPT_mno_sse41, "sse41" }, |
| 179 | { options::OPT_msse42, options::OPT_mno_sse42, "sse42" }, |
| 180 | { options::OPT_msse4a, options::OPT_mno_sse4a, "sse4a" }, |
| 181 | { options::OPT_m3dnow, options::OPT_mno_3dnow, "3dnow" }, |
| 182 | { options::OPT_m3dnowa, options::OPT_mno_3dnowa, "3dnowa" } |
| 183 | }; |
| 184 | const unsigned NumFeatureOptions = |
| 185 | sizeof(FeatureOptions)/sizeof(FeatureOptions[0]); |
| 186 | |
| 187 | // FIXME: Avoid std::string |
| 188 | std::string Attrs; |
| 189 | for (unsigned i=0; i < NumFeatureOptions; ++i) { |
| 190 | if (Args.hasArg(FeatureOptions[i].Pos)) { |
Daniel Dunbar | 55b3b5f | 2009-03-19 17:36:04 +0000 | [diff] [blame] | 191 | if (!Attrs.empty()) |
| 192 | Attrs += ','; |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 193 | Attrs += '+'; |
| 194 | Attrs += FeatureOptions[i].Name; |
| 195 | } else if (Args.hasArg(FeatureOptions[i].Neg)) { |
Daniel Dunbar | 55b3b5f | 2009-03-19 17:36:04 +0000 | [diff] [blame] | 196 | if (!Attrs.empty()) |
| 197 | Attrs += ','; |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 198 | Attrs += '-'; |
| 199 | Attrs += FeatureOptions[i].Name; |
| 200 | } |
| 201 | } |
| 202 | if (!Attrs.empty()) { |
| 203 | CmdArgs.push_back("--mattr"); |
| 204 | CmdArgs.push_back(Args.MakeArgString(Attrs.c_str())); |
| 205 | } |
| 206 | |
| 207 | if (Args.hasFlag(options::OPT_fmath_errno, |
| 208 | options::OPT_fno_math_errno, |
| 209 | getToolChain().IsMathErrnoDefault())) |
| 210 | CmdArgs.push_back("--fmath-errno=1"); |
| 211 | else |
| 212 | CmdArgs.push_back("--fmath-errno=0"); |
| 213 | |
| 214 | if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) { |
| 215 | CmdArgs.push_back("--limit-float-precision"); |
| 216 | CmdArgs.push_back(A->getValue(Args)); |
| 217 | } |
| 218 | |
| 219 | // FIXME: Add --stack-protector-buffer-size=<xxx> on |
| 220 | // -fstack-protect. |
| 221 | |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 222 | // Handle dependency file generation. |
| 223 | Arg *A; |
| 224 | if ((A = Args.getLastArg(options::OPT_M)) || |
| 225 | (A = Args.getLastArg(options::OPT_MM)) || |
| 226 | (A = Args.getLastArg(options::OPT_MD)) || |
| 227 | (A = Args.getLastArg(options::OPT_MMD))) { |
| 228 | // Determine the output location. |
| 229 | const char *DepFile; |
Daniel Dunbar | cd8e4c4 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 230 | if (Output.getType() == types::TY_Dependencies) { |
| 231 | if (Output.isPipe()) |
| 232 | DepFile = "-"; |
| 233 | else |
| 234 | DepFile = Output.getFilename(); |
| 235 | } else if (Arg *MF = Args.getLastArg(options::OPT_MF)) { |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 236 | DepFile = MF->getValue(Args); |
| 237 | } else if (A->getOption().getId() == options::OPT_M || |
| 238 | A->getOption().getId() == options::OPT_MM) { |
| 239 | DepFile = "-"; |
| 240 | } else { |
| 241 | DepFile = darwin::CC1::getDependencyFileName(Args, Inputs); |
| 242 | } |
| 243 | CmdArgs.push_back("-dependency-file"); |
| 244 | CmdArgs.push_back(DepFile); |
| 245 | |
| 246 | // Add an -MT option if the user didn't specify their own. |
| 247 | // FIXME: This should use -MQ, when we support it. |
| 248 | if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) { |
| 249 | const char *DepTarget; |
| 250 | |
Daniel Dunbar | cd8e4c4 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 251 | // If user provided -o, that is the dependency target, except |
| 252 | // when we are only generating a dependency file. |
| 253 | Arg *OutputOpt = Args.getLastArg(options::OPT_o); |
| 254 | if (OutputOpt && Output.getType() != types::TY_Dependencies) { |
Daniel Dunbar | 46562b9 | 2009-03-30 17:59:58 +0000 | [diff] [blame] | 255 | DepTarget = OutputOpt->getValue(Args); |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 256 | } else { |
| 257 | // Otherwise derive from the base input. |
| 258 | // |
| 259 | // FIXME: This should use the computed output file location. |
| 260 | llvm::sys::Path P(Inputs[0].getBaseInput()); |
| 261 | |
| 262 | P.eraseSuffix(); |
| 263 | P.appendSuffix("o"); |
| 264 | DepTarget = Args.MakeArgString(P.getLast().c_str()); |
| 265 | } |
| 266 | |
| 267 | CmdArgs.push_back("-MT"); |
| 268 | CmdArgs.push_back(DepTarget); |
| 269 | } |
| 270 | |
| 271 | if (A->getOption().getId() == options::OPT_M || |
| 272 | A->getOption().getId() == options::OPT_MD) |
| 273 | CmdArgs.push_back("-sys-header-deps"); |
| 274 | } |
| 275 | |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 276 | Args.AddLastArg(CmdArgs, options::OPT_MP); |
| 277 | Args.AddAllArgs(CmdArgs, options::OPT_MT); |
| 278 | |
Daniel Dunbar | cd8e4c4 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 279 | Arg *Unsupported; |
| 280 | if ((Unsupported = Args.getLastArg(options::OPT_MG)) || |
Daniel Dunbar | 5c1aaaf | 2009-04-07 19:18:24 +0000 | [diff] [blame] | 281 | (Unsupported = Args.getLastArg(options::OPT_MQ))) |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 282 | D.Diag(clang::diag::err_drv_unsupported_opt) |
| 283 | << Unsupported->getOption().getName(); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | Args.AddAllArgs(CmdArgs, options::OPT_v); |
| 287 | Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U); |
| 288 | Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F); |
| 289 | Args.AddLastArg(CmdArgs, options::OPT_P); |
Daniel Dunbar | 2ac9fc2 | 2009-04-07 21:42:00 +0000 | [diff] [blame] | 290 | Args.AddLastArg(CmdArgs, options::OPT_mmacosx_version_min_EQ); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 291 | |
| 292 | // Special case debug options to only pass -g to clang. This is |
| 293 | // wrong. |
| 294 | if (Args.hasArg(options::OPT_g_Group)) |
| 295 | CmdArgs.push_back("-g"); |
| 296 | |
| 297 | Args.AddLastArg(CmdArgs, options::OPT_nostdinc); |
| 298 | |
Daniel Dunbar | 2ac9fc2 | 2009-04-07 21:42:00 +0000 | [diff] [blame] | 299 | Args.AddLastArg(CmdArgs, options::OPT_isysroot); |
| 300 | |
Daniel Dunbar | 049853d | 2009-03-20 19:38:56 +0000 | [diff] [blame] | 301 | // FIXME: Use iterator. |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 302 | |
Daniel Dunbar | 049853d | 2009-03-20 19:38:56 +0000 | [diff] [blame] | 303 | // Add -i* options, and automatically translate to -include-pth for |
| 304 | // transparent PCH support. It's wonky, but we include looking for |
| 305 | // .gch so we can support seamless replacement into a build system |
| 306 | // already set up to be generating .gch files. |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 307 | for (ArgList::const_iterator |
| 308 | it = Args.begin(), ie = Args.end(); it != ie; ++it) { |
| 309 | const Arg *A = *it; |
Daniel Dunbar | 2ac9fc2 | 2009-04-07 21:42:00 +0000 | [diff] [blame] | 310 | if (!A->getOption().matches(options::OPT_clang_i_Group)) |
Daniel Dunbar | 049853d | 2009-03-20 19:38:56 +0000 | [diff] [blame] | 311 | continue; |
| 312 | |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 313 | if (A->getOption().matches(options::OPT_include)) { |
Daniel Dunbar | 049853d | 2009-03-20 19:38:56 +0000 | [diff] [blame] | 314 | bool FoundPTH = false; |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 315 | llvm::sys::Path P(A->getValue(Args)); |
| 316 | P.appendSuffix("pth"); |
| 317 | if (P.exists()) { |
Daniel Dunbar | 049853d | 2009-03-20 19:38:56 +0000 | [diff] [blame] | 318 | FoundPTH = true; |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 319 | } else { |
| 320 | P.eraseSuffix(); |
| 321 | P.appendSuffix("gch"); |
Daniel Dunbar | 049853d | 2009-03-20 19:38:56 +0000 | [diff] [blame] | 322 | if (P.exists()) |
| 323 | FoundPTH = true; |
| 324 | } |
| 325 | |
| 326 | if (FoundPTH) { |
| 327 | A->claim(); |
| 328 | CmdArgs.push_back("-include-pth"); |
| 329 | CmdArgs.push_back(Args.MakeArgString(P.c_str())); |
| 330 | continue; |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
Daniel Dunbar | 049853d | 2009-03-20 19:38:56 +0000 | [diff] [blame] | 333 | |
| 334 | // Not translated, render as usual. |
| 335 | A->claim(); |
| 336 | A->render(Args, CmdArgs); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Daniel Dunbar | 337a627 | 2009-03-24 20:17:30 +0000 | [diff] [blame] | 339 | // Manually translate -O to -O1 and -O4 to -O3; let clang reject |
| 340 | // others. |
| 341 | if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { |
| 342 | if (A->getOption().getId() == options::OPT_O4) |
| 343 | CmdArgs.push_back("-O3"); |
| 344 | else if (A->getValue(Args)[0] == '\0') |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 345 | CmdArgs.push_back("-O1"); |
| 346 | else |
Daniel Dunbar | 5697aa0 | 2009-03-18 23:39:35 +0000 | [diff] [blame] | 347 | A->render(Args, CmdArgs); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Daniel Dunbar | ff7488d | 2009-03-20 00:52:38 +0000 | [diff] [blame] | 350 | Args.AddAllArgs(CmdArgs, options::OPT_clang_W_Group, |
| 351 | options::OPT_pedantic_Group); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 352 | Args.AddLastArg(CmdArgs, options::OPT_w); |
Daniel Dunbar | d573d26 | 2009-04-07 22:13:21 +0000 | [diff] [blame] | 353 | |
| 354 | // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi} |
| 355 | // (-ansi is equivalent to -std=c89). |
| 356 | // |
| 357 | // If a std is supplied, only add -trigraphs if it follows the |
| 358 | // option. |
| 359 | if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) { |
| 360 | if (Std->getOption().matches(options::OPT_ansi)) |
| 361 | CmdArgs.push_back("-std=c89"); |
| 362 | else |
| 363 | Std->render(Args, CmdArgs); |
| 364 | |
| 365 | if (Arg *A = Args.getLastArg(options::OPT_trigraphs)) |
| 366 | if (A->getIndex() > Std->getIndex()) |
| 367 | A->render(Args, CmdArgs); |
| 368 | } else |
| 369 | Args.AddLastArg(CmdArgs, options::OPT_trigraphs); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 370 | |
| 371 | if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) { |
| 372 | CmdArgs.push_back("-ftemplate-depth"); |
| 373 | CmdArgs.push_back(A->getValue(Args)); |
| 374 | } |
| 375 | |
Daniel Dunbar | 48d1ef7 | 2009-04-07 21:16:11 +0000 | [diff] [blame] | 376 | // Forward -f options which we can pass directly. |
Daniel Dunbar | 3aaf082 | 2009-04-07 21:51:40 +0000 | [diff] [blame] | 377 | Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls); |
| 378 | Args.AddLastArg(CmdArgs, options::OPT_fexceptions); |
| 379 | Args.AddLastArg(CmdArgs, options::OPT_ffreestanding); |
| 380 | Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions); |
| 381 | Args.AddLastArg(CmdArgs, options::OPT_fgnu_runtime); |
| 382 | Args.AddLastArg(CmdArgs, options::OPT_flax_vector_conversions); |
| 383 | Args.AddLastArg(CmdArgs, options::OPT_fms_extensions); |
| 384 | Args.AddLastArg(CmdArgs, options::OPT_fnext_runtime); |
| 385 | Args.AddLastArg(CmdArgs, options::OPT_fno_caret_diagnostics); |
| 386 | Args.AddLastArg(CmdArgs, options::OPT_fno_show_column); |
| 387 | Args.AddLastArg(CmdArgs, options::OPT_fobjc_gc_only); |
| 388 | Args.AddLastArg(CmdArgs, options::OPT_fobjc_gc); |
| 389 | // FIXME: Should we remove this? |
| 390 | Args.AddLastArg(CmdArgs, options::OPT_fobjc_nonfragile_abi); |
| 391 | Args.AddLastArg(CmdArgs, options::OPT_fprint_source_range_info); |
| 392 | Args.AddLastArg(CmdArgs, options::OPT_ftime_report); |
| 393 | Args.AddLastArg(CmdArgs, options::OPT_ftrapv); |
| 394 | Args.AddLastArg(CmdArgs, options::OPT_fvisibility_EQ); |
| 395 | Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 396 | |
Daniel Dunbar | 48d1ef7 | 2009-04-07 21:16:11 +0000 | [diff] [blame] | 397 | // Forward -f options with positive and negative forms; we translate |
| 398 | // these by hand. |
| 399 | |
| 400 | // -fbuiltin is default, only pass non-default. |
| 401 | if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin)) |
| 402 | CmdArgs.push_back("-fbuiltin=0"); |
| 403 | |
| 404 | // -fblocks default varies depending on platform and language; |
| 405 | // -always pass if specified. |
| 406 | if (Arg *A = Args.getLastArg(options::OPT_fblocks, options::OPT_fno_blocks)) { |
| 407 | if (A->getOption().matches(options::OPT_fblocks)) |
| 408 | CmdArgs.push_back("-fblocks"); |
| 409 | else |
| 410 | CmdArgs.push_back("-fblocks=0"); |
| 411 | } |
| 412 | |
| 413 | // -fno-pascal-strings is default, only pass non-default. |
| 414 | if (Args.hasFlag(options::OPT_fpascal_strings, |
| 415 | options::OPT_fno_pascal_strings)) |
| 416 | CmdArgs.push_back("-fpascal-strings"); |
| 417 | |
| 418 | // -fcommon is default, only pass non-default. |
| 419 | if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common)) |
| 420 | CmdArgs.push_back("-fno-common"); |
| 421 | |
Daniel Dunbar | b9f3a77 | 2009-03-27 15:22:28 +0000 | [diff] [blame] | 422 | // If tool chain translates fpascal-strings, we want to back |
| 423 | // translate here. |
| 424 | // FIXME: This is gross; that translation should be pulled from the |
| 425 | // tool chain. |
| 426 | if (Arg *A = Args.getLastArg(options::OPT_mpascal_strings, |
| 427 | options::OPT_mno_pascal_strings)) { |
| 428 | if (A->getOption().matches(options::OPT_mpascal_strings)) |
| 429 | CmdArgs.push_back("-fpascal-strings"); |
| 430 | else |
| 431 | CmdArgs.push_back("-fno-pascal-strings"); |
| 432 | } |
| 433 | |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 434 | Args.AddLastArg(CmdArgs, options::OPT_dM); |
| 435 | |
Daniel Dunbar | 077ba6a | 2009-03-31 20:53:55 +0000 | [diff] [blame] | 436 | // Add -Wp, and -Xassembler if using the preprocessor. |
| 437 | |
| 438 | // FIXME: There is a very unfortunate problem here, some troubled |
| 439 | // souls abuse -Wp, to pass preprocessor options in gcc syntax. To |
| 440 | // really support that we would have to parse and then translate |
| 441 | // those options. :( |
| 442 | if (types::getPreprocessedType(Inputs[0].getType()) != types::TY_INVALID) |
| 443 | Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA, |
| 444 | options::OPT_Xpreprocessor); |
| 445 | |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 446 | Args.AddAllArgValues(CmdArgs, options::OPT_Xclang); |
| 447 | |
Daniel Dunbar | cd8e4c4 | 2009-03-30 06:36:42 +0000 | [diff] [blame] | 448 | if (Output.getType() == types::TY_Dependencies) { |
| 449 | // Handled with other dependency code. |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 450 | } else if (Output.isPipe()) { |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 451 | CmdArgs.push_back("-o"); |
| 452 | CmdArgs.push_back("-"); |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 453 | } else if (Output.isFilename()) { |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 454 | CmdArgs.push_back("-o"); |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 455 | CmdArgs.push_back(Output.getFilename()); |
| 456 | } else { |
| 457 | assert(Output.isNothing() && "Invalid output."); |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 460 | for (InputInfoList::const_iterator |
| 461 | it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { |
| 462 | const InputInfo &II = *it; |
| 463 | CmdArgs.push_back("-x"); |
| 464 | CmdArgs.push_back(types::getTypeName(II.getType())); |
| 465 | if (II.isPipe()) |
| 466 | CmdArgs.push_back("-"); |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 467 | else if (II.isFilename()) |
| 468 | CmdArgs.push_back(II.getFilename()); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 469 | else |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 470 | II.getInputArg().renderAsInput(Args, CmdArgs); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | const char *Exec = |
Daniel Dunbar | d7d5f02 | 2009-03-24 02:24:46 +0000 | [diff] [blame] | 474 | Args.MakeArgString(getToolChain().GetProgramPath(C, "clang-cc").c_str()); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 475 | Dest.addCommand(new Command(Exec, CmdArgs)); |
Daniel Dunbar | a880db0 | 2009-03-23 19:03:36 +0000 | [diff] [blame] | 476 | |
Daniel Dunbar | 5c1aaaf | 2009-04-07 19:18:24 +0000 | [diff] [blame] | 477 | // Explicitly warn that these options are unsupported, even though |
| 478 | // we are allowing compilation to continue. |
| 479 | // FIXME: Use iterator. |
| 480 | for (ArgList::const_iterator |
| 481 | it = Args.begin(), ie = Args.end(); it != ie; ++it) { |
| 482 | const Arg *A = *it; |
| 483 | if (A->getOption().matches(options::OPT_pg)) { |
| 484 | A->claim(); |
| 485 | D.Diag(clang::diag::warn_drv_clang_unsupported) |
| 486 | << A->getAsString(Args); |
| 487 | } |
| 488 | } |
| 489 | |
Daniel Dunbar | 68fb469 | 2009-04-03 20:51:31 +0000 | [diff] [blame] | 490 | // Claim some arguments which clang supports automatically. |
| 491 | |
| 492 | // -fpch-preprocess is used with gcc to add a special marker in the |
| 493 | // -output to include the PCH file. Clang's PTH solution is |
| 494 | // -completely transparent, so we do not need to deal with it at |
| 495 | // -all. |
| 496 | Args.ClaimAllArgs(options::OPT_fpch_preprocess); |
| 497 | |
Daniel Dunbar | a880db0 | 2009-03-23 19:03:36 +0000 | [diff] [blame] | 498 | // Claim some arguments which clang doesn't support, but we don't |
| 499 | // care to warn the user about. |
| 500 | |
| 501 | // FIXME: Use iterator. |
| 502 | for (ArgList::const_iterator |
| 503 | it = Args.begin(), ie = Args.end(); it != ie; ++it) { |
| 504 | const Arg *A = *it; |
| 505 | if (A->getOption().matches(options::OPT_clang_ignored_W_Group) || |
Daniel Dunbar | 16fd3a9 | 2009-04-07 02:59:27 +0000 | [diff] [blame] | 506 | A->getOption().matches(options::OPT_clang_ignored_f_Group) || |
| 507 | A->getOption().matches(options::OPT_clang_ignored_m_Group)) |
Daniel Dunbar | a880db0 | 2009-03-23 19:03:36 +0000 | [diff] [blame] | 508 | A->claim(); |
| 509 | } |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 512 | void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA, |
| 513 | Job &Dest, |
| 514 | const InputInfo &Output, |
| 515 | const InputInfoList &Inputs, |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 516 | const ArgList &Args, |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 517 | const char *LinkingOutput) const { |
| 518 | ArgStringList CmdArgs; |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 519 | |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 520 | for (ArgList::const_iterator |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 521 | it = Args.begin(), ie = Args.end(); it != ie; ++it) { |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 522 | Arg *A = *it; |
Daniel Dunbar | 7587719 | 2009-03-19 07:55:12 +0000 | [diff] [blame] | 523 | if (A->getOption().hasForwardToGCC()) { |
| 524 | // It is unfortunate that we have to claim here, as this means |
| 525 | // we will basically never report anything interesting for |
| 526 | // platforms using a generic gcc. |
| 527 | A->claim(); |
Daniel Dunbar | 1d46033 | 2009-03-18 10:01:51 +0000 | [diff] [blame] | 528 | A->render(Args, CmdArgs); |
Daniel Dunbar | 7587719 | 2009-03-19 07:55:12 +0000 | [diff] [blame] | 529 | } |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | RenderExtraToolArgs(CmdArgs); |
| 533 | |
| 534 | // If using a driver driver, force the arch. |
| 535 | if (getToolChain().getHost().useDriverDriver()) { |
| 536 | CmdArgs.push_back("-arch"); |
Daniel Dunbar | bf54a06 | 2009-04-01 20:33:11 +0000 | [diff] [blame] | 537 | |
| 538 | // FIXME: Remove these special cases. |
| 539 | const char *Str = getToolChain().getArchName().c_str(); |
| 540 | if (strcmp(Str, "powerpc") == 0) |
| 541 | Str = "ppc"; |
| 542 | else if (strcmp(Str, "powerpc64") == 0) |
| 543 | Str = "ppc64"; |
| 544 | CmdArgs.push_back(Str); |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | if (Output.isPipe()) { |
| 548 | CmdArgs.push_back("-o"); |
| 549 | CmdArgs.push_back("-"); |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 550 | } else if (Output.isFilename()) { |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 551 | CmdArgs.push_back("-o"); |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 552 | CmdArgs.push_back(Output.getFilename()); |
| 553 | } else { |
| 554 | assert(Output.isNothing() && "Unexpected output"); |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 555 | CmdArgs.push_back("-fsyntax-only"); |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 556 | } |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 557 | |
| 558 | |
| 559 | // Only pass -x if gcc will understand it; otherwise hope gcc |
| 560 | // understands the suffix correctly. The main use case this would go |
| 561 | // wrong in is for linker inputs if they happened to have an odd |
| 562 | // suffix; really the only way to get this to happen is a command |
| 563 | // like '-x foobar a.c' which will treat a.c like a linker input. |
| 564 | // |
| 565 | // FIXME: For the linker case specifically, can we safely convert |
| 566 | // inputs into '-Wl,' options? |
| 567 | for (InputInfoList::const_iterator |
| 568 | it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { |
| 569 | const InputInfo &II = *it; |
| 570 | if (types::canTypeBeUserSpecified(II.getType())) { |
| 571 | CmdArgs.push_back("-x"); |
| 572 | CmdArgs.push_back(types::getTypeName(II.getType())); |
| 573 | } |
| 574 | |
| 575 | if (II.isPipe()) |
| 576 | CmdArgs.push_back("-"); |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 577 | else if (II.isFilename()) |
| 578 | CmdArgs.push_back(II.getFilename()); |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 579 | else |
Daniel Dunbar | 115a792 | 2009-03-19 07:29:38 +0000 | [diff] [blame] | 580 | // Don't render as input, we need gcc to do the translations. |
| 581 | II.getInputArg().render(Args, CmdArgs); |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Daniel Dunbar | 78d8a08 | 2009-04-01 23:34:41 +0000 | [diff] [blame] | 584 | const char *GCCName = |
| 585 | getToolChain().getHost().getDriver().CCCGenericGCCName.c_str(); |
Daniel Dunbar | 632f50e | 2009-03-18 21:34:08 +0000 | [diff] [blame] | 586 | const char *Exec = |
Daniel Dunbar | 78d8a08 | 2009-04-01 23:34:41 +0000 | [diff] [blame] | 587 | Args.MakeArgString(getToolChain().GetProgramPath(C, GCCName).c_str()); |
Daniel Dunbar | 632f50e | 2009-03-18 21:34:08 +0000 | [diff] [blame] | 588 | Dest.addCommand(new Command(Exec, CmdArgs)); |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 591 | void gcc::Preprocess::RenderExtraToolArgs(ArgStringList &CmdArgs) const { |
| 592 | CmdArgs.push_back("-E"); |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 595 | void gcc::Precompile::RenderExtraToolArgs(ArgStringList &CmdArgs) const { |
| 596 | // The type is good enough. |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 599 | void gcc::Compile::RenderExtraToolArgs(ArgStringList &CmdArgs) const { |
| 600 | CmdArgs.push_back("-S"); |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 603 | void gcc::Assemble::RenderExtraToolArgs(ArgStringList &CmdArgs) const { |
| 604 | CmdArgs.push_back("-c"); |
Daniel Dunbar | 47ac7d2 | 2009-03-18 06:00:36 +0000 | [diff] [blame] | 605 | } |
Daniel Dunbar | b488c1d | 2009-03-18 08:07:30 +0000 | [diff] [blame] | 606 | |
| 607 | void gcc::Link::RenderExtraToolArgs(ArgStringList &CmdArgs) const { |
| 608 | // The types are (hopefully) good enough. |
| 609 | } |
| 610 | |
Daniel Dunbar | 40f1265 | 2009-03-29 17:08:39 +0000 | [diff] [blame] | 611 | const char *darwin::CC1::getCC1Name(types::ID Type) const { |
| 612 | switch (Type) { |
| 613 | default: |
| 614 | assert(0 && "Unexpected type for Darwin CC1 tool."); |
| 615 | case types::TY_Asm: |
| 616 | case types::TY_C: case types::TY_CHeader: |
| 617 | case types::TY_PP_C: case types::TY_PP_CHeader: |
| 618 | return "cc1"; |
| 619 | case types::TY_ObjC: case types::TY_ObjCHeader: |
| 620 | case types::TY_PP_ObjC: case types::TY_PP_ObjCHeader: |
| 621 | return "cc1obj"; |
| 622 | case types::TY_CXX: case types::TY_CXXHeader: |
| 623 | case types::TY_PP_CXX: case types::TY_PP_CXXHeader: |
| 624 | return "cc1plus"; |
| 625 | case types::TY_ObjCXX: case types::TY_ObjCXXHeader: |
| 626 | case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXXHeader: |
| 627 | return "cc1objplus"; |
| 628 | } |
| 629 | } |
| 630 | |
Daniel Dunbar | a3ec60e | 2009-03-29 18:40:18 +0000 | [diff] [blame] | 631 | const char *darwin::CC1::getBaseInputName(const ArgList &Args, |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 632 | const InputInfoList &Inputs) { |
Daniel Dunbar | a3ec60e | 2009-03-29 18:40:18 +0000 | [diff] [blame] | 633 | llvm::sys::Path P(Inputs[0].getBaseInput()); |
| 634 | return Args.MakeArgString(P.getLast().c_str()); |
| 635 | } |
| 636 | |
| 637 | const char *darwin::CC1::getBaseInputStem(const ArgList &Args, |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 638 | const InputInfoList &Inputs) { |
Daniel Dunbar | a3ec60e | 2009-03-29 18:40:18 +0000 | [diff] [blame] | 639 | const char *Str = getBaseInputName(Args, Inputs); |
| 640 | |
| 641 | if (const char *End = strchr(Str, '.')) |
| 642 | return Args.MakeArgString(std::string(Str, End).c_str()); |
| 643 | |
| 644 | return Str; |
| 645 | } |
| 646 | |
| 647 | const char * |
| 648 | darwin::CC1::getDependencyFileName(const ArgList &Args, |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 649 | const InputInfoList &Inputs) { |
Daniel Dunbar | a3ec60e | 2009-03-29 18:40:18 +0000 | [diff] [blame] | 650 | // FIXME: Think about this more. |
| 651 | std::string Res; |
| 652 | |
| 653 | if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) { |
| 654 | std::string Str(OutputOpt->getValue(Args)); |
| 655 | |
| 656 | Res = Str.substr(0, Str.rfind('.')); |
| 657 | } else |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 658 | Res = darwin::CC1::getBaseInputStem(Args, Inputs); |
Daniel Dunbar | a3ec60e | 2009-03-29 18:40:18 +0000 | [diff] [blame] | 659 | |
| 660 | return Args.MakeArgString((Res + ".d").c_str()); |
| 661 | } |
| 662 | |
| 663 | void darwin::CC1::AddCC1Args(const ArgList &Args, |
| 664 | ArgStringList &CmdArgs) const { |
| 665 | // Derived from cc1 spec. |
| 666 | |
| 667 | // FIXME: -fapple-kext seems to disable this too. Investigate. |
| 668 | if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) && |
| 669 | !Args.hasArg(options::OPT_mdynamic_no_pic)) |
| 670 | CmdArgs.push_back("-fPIC"); |
| 671 | |
| 672 | // gcc has some code here to deal with when no -mmacosx-version-min |
| 673 | // and no -miphoneos-version-min is present, but this never happens |
| 674 | // due to tool chain specific argument translation. |
| 675 | |
| 676 | // FIXME: Remove mthumb |
| 677 | // FIXME: Remove mno-thumb |
| 678 | // FIXME: Remove faltivec |
| 679 | // FIXME: Remove mno-fused-madd |
| 680 | // FIXME: Remove mlong-branch |
| 681 | // FIXME: Remove mlongcall |
| 682 | // FIXME: Remove mcpu=G4 |
| 683 | // FIXME: Remove mcpu=G5 |
| 684 | |
| 685 | if (Args.hasArg(options::OPT_g_Flag) && |
| 686 | !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols)) |
| 687 | CmdArgs.push_back("-feliminate-unused-debug-symbols"); |
| 688 | } |
| 689 | |
| 690 | void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs, |
| 691 | const InputInfoList &Inputs, |
| 692 | const ArgStringList &OutputArgs) const { |
| 693 | const Driver &D = getToolChain().getHost().getDriver(); |
| 694 | |
| 695 | // Derived from cc1_options spec. |
| 696 | if (Args.hasArg(options::OPT_fast) || |
| 697 | Args.hasArg(options::OPT_fastf) || |
| 698 | Args.hasArg(options::OPT_fastcp)) |
| 699 | CmdArgs.push_back("-O3"); |
| 700 | |
| 701 | if (Arg *A = Args.getLastArg(options::OPT_pg)) |
| 702 | if (Args.hasArg(options::OPT_fomit_frame_pointer)) |
| 703 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 704 | << A->getAsString(Args) << "-fomit-frame-pointer"; |
| 705 | |
| 706 | AddCC1Args(Args, CmdArgs); |
| 707 | |
| 708 | if (!Args.hasArg(options::OPT_Q)) |
| 709 | CmdArgs.push_back("-quiet"); |
| 710 | |
| 711 | CmdArgs.push_back("-dumpbase"); |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 712 | CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs)); |
Daniel Dunbar | a3ec60e | 2009-03-29 18:40:18 +0000 | [diff] [blame] | 713 | |
| 714 | Args.AddAllArgs(CmdArgs, options::OPT_d_Group); |
| 715 | |
| 716 | Args.AddAllArgs(CmdArgs, options::OPT_m_Group); |
| 717 | Args.AddAllArgs(CmdArgs, options::OPT_a_Group); |
| 718 | |
| 719 | // FIXME: The goal is to use the user provided -o if that is our |
| 720 | // final output, otherwise to drive from the original input |
| 721 | // name. Find a clean way to go about this. |
| 722 | if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) && |
| 723 | Args.hasArg(options::OPT_o)) { |
| 724 | Arg *OutputOpt = Args.getLastArg(options::OPT_o); |
| 725 | CmdArgs.push_back("-auxbase-strip"); |
| 726 | CmdArgs.push_back(OutputOpt->getValue(Args)); |
| 727 | } else { |
| 728 | CmdArgs.push_back("-auxbase"); |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 729 | CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs)); |
Daniel Dunbar | a3ec60e | 2009-03-29 18:40:18 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | Args.AddAllArgs(CmdArgs, options::OPT_g_Group); |
| 733 | |
| 734 | Args.AddAllArgs(CmdArgs, options::OPT_O); |
| 735 | // FIXME: -Wall is getting some special treatment. Investigate. |
| 736 | Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group); |
| 737 | Args.AddLastArg(CmdArgs, options::OPT_w); |
| 738 | Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi, |
| 739 | options::OPT_trigraphs); |
| 740 | if (Args.hasArg(options::OPT_v)) |
| 741 | CmdArgs.push_back("-version"); |
| 742 | if (Args.hasArg(options::OPT_pg)) |
| 743 | CmdArgs.push_back("-p"); |
| 744 | Args.AddLastArg(CmdArgs, options::OPT_p); |
| 745 | |
| 746 | // The driver treats -fsyntax-only specially. |
| 747 | Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only); |
| 748 | |
| 749 | Args.AddAllArgs(CmdArgs, options::OPT_undef); |
| 750 | if (Args.hasArg(options::OPT_Qn)) |
| 751 | CmdArgs.push_back("-fno-ident"); |
| 752 | |
| 753 | // FIXME: This isn't correct. |
| 754 | //Args.AddLastArg(CmdArgs, options::OPT__help) |
| 755 | //Args.AddLastArg(CmdArgs, options::OPT__targetHelp) |
| 756 | |
| 757 | CmdArgs.append(OutputArgs.begin(), OutputArgs.end()); |
| 758 | |
| 759 | // FIXME: Still don't get what is happening here. Investigate. |
| 760 | Args.AddAllArgs(CmdArgs, options::OPT__param); |
| 761 | |
| 762 | if (Args.hasArg(options::OPT_fmudflap) || |
| 763 | Args.hasArg(options::OPT_fmudflapth)) { |
| 764 | CmdArgs.push_back("-fno-builtin"); |
| 765 | CmdArgs.push_back("-fno-merge-constants"); |
| 766 | } |
| 767 | |
| 768 | if (Args.hasArg(options::OPT_coverage)) { |
| 769 | CmdArgs.push_back("-fprofile-arcs"); |
| 770 | CmdArgs.push_back("-ftest-coverage"); |
| 771 | } |
| 772 | |
| 773 | if (types::isCXX(Inputs[0].getType())) |
| 774 | CmdArgs.push_back("-D__private_extern__=extern"); |
| 775 | } |
| 776 | |
| 777 | void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs, |
| 778 | const InputInfoList &Inputs, |
| 779 | const ArgStringList &OutputArgs) const { |
| 780 | // Derived from cpp_options |
| 781 | AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs); |
| 782 | |
| 783 | CmdArgs.append(OutputArgs.begin(), OutputArgs.end()); |
| 784 | |
| 785 | AddCC1Args(Args, CmdArgs); |
| 786 | |
| 787 | // NOTE: The code below has some commonality with cpp_options, but |
| 788 | // in classic gcc style ends up sending things in different |
| 789 | // orders. This may be a good merge candidate once we drop pedantic |
| 790 | // compatibility. |
| 791 | |
| 792 | Args.AddAllArgs(CmdArgs, options::OPT_m_Group); |
| 793 | Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi, |
| 794 | options::OPT_trigraphs); |
| 795 | Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group); |
| 796 | Args.AddLastArg(CmdArgs, options::OPT_w); |
| 797 | |
| 798 | // The driver treats -fsyntax-only specially. |
| 799 | Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only); |
| 800 | |
| 801 | if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) && |
| 802 | !Args.hasArg(options::OPT_fno_working_directory)) |
| 803 | CmdArgs.push_back("-fworking-directory"); |
| 804 | |
| 805 | Args.AddAllArgs(CmdArgs, options::OPT_O); |
| 806 | Args.AddAllArgs(CmdArgs, options::OPT_undef); |
| 807 | if (Args.hasArg(options::OPT_save_temps)) |
| 808 | CmdArgs.push_back("-fpch-preprocess"); |
| 809 | } |
| 810 | |
| 811 | void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args, |
| 812 | ArgStringList &CmdArgs, |
| 813 | const InputInfoList &Inputs) const |
| 814 | { |
| 815 | const Driver &D = getToolChain().getHost().getDriver(); |
| 816 | |
| 817 | // Derived from cpp_unique_options. |
| 818 | Arg *A; |
| 819 | if ((A = Args.getLastArg(options::OPT_C)) || |
| 820 | (A = Args.getLastArg(options::OPT_CC))) { |
| 821 | if (!Args.hasArg(options::OPT_E)) |
| 822 | D.Diag(clang::diag::err_drv_argument_only_allowed_with) |
| 823 | << A->getAsString(Args) << "-E"; |
| 824 | } |
| 825 | if (!Args.hasArg(options::OPT_Q)) |
| 826 | CmdArgs.push_back("-quiet"); |
| 827 | Args.AddAllArgs(CmdArgs, options::OPT_nostdinc); |
| 828 | Args.AddLastArg(CmdArgs, options::OPT_v); |
| 829 | Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F); |
| 830 | Args.AddLastArg(CmdArgs, options::OPT_P); |
| 831 | |
| 832 | // FIXME: Handle %I properly. |
| 833 | if (getToolChain().getArchName() == "x86_64") { |
| 834 | CmdArgs.push_back("-imultilib"); |
| 835 | CmdArgs.push_back("x86_64"); |
| 836 | } |
| 837 | |
| 838 | if (Args.hasArg(options::OPT_MD)) { |
| 839 | CmdArgs.push_back("-MD"); |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 840 | CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs)); |
Daniel Dunbar | a3ec60e | 2009-03-29 18:40:18 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | if (Args.hasArg(options::OPT_MMD)) { |
| 844 | CmdArgs.push_back("-MMD"); |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 845 | CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs)); |
Daniel Dunbar | a3ec60e | 2009-03-29 18:40:18 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | Args.AddLastArg(CmdArgs, options::OPT_M); |
| 849 | Args.AddLastArg(CmdArgs, options::OPT_MM); |
| 850 | Args.AddAllArgs(CmdArgs, options::OPT_MF); |
| 851 | Args.AddLastArg(CmdArgs, options::OPT_MG); |
| 852 | Args.AddLastArg(CmdArgs, options::OPT_MP); |
| 853 | Args.AddAllArgs(CmdArgs, options::OPT_MQ); |
| 854 | Args.AddAllArgs(CmdArgs, options::OPT_MT); |
| 855 | if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) && |
| 856 | (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) { |
| 857 | if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) { |
| 858 | CmdArgs.push_back("-MQ"); |
| 859 | CmdArgs.push_back(OutputOpt->getValue(Args)); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | Args.AddLastArg(CmdArgs, options::OPT_remap); |
| 864 | if (Args.hasArg(options::OPT_g3)) |
| 865 | CmdArgs.push_back("-dD"); |
| 866 | Args.AddLastArg(CmdArgs, options::OPT_H); |
| 867 | |
| 868 | AddCPPArgs(Args, CmdArgs); |
| 869 | |
| 870 | Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A); |
| 871 | Args.AddAllArgs(CmdArgs, options::OPT_i_Group); |
| 872 | |
| 873 | for (InputInfoList::const_iterator |
| 874 | it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { |
| 875 | const InputInfo &II = *it; |
| 876 | |
| 877 | if (II.isPipe()) |
| 878 | CmdArgs.push_back("-"); |
| 879 | else |
| 880 | CmdArgs.push_back(II.getFilename()); |
| 881 | } |
| 882 | |
| 883 | Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA, |
| 884 | options::OPT_Xpreprocessor); |
| 885 | |
| 886 | if (Args.hasArg(options::OPT_fmudflap)) { |
| 887 | CmdArgs.push_back("-D_MUDFLAP"); |
| 888 | CmdArgs.push_back("-include"); |
| 889 | CmdArgs.push_back("mf-runtime.h"); |
| 890 | } |
| 891 | |
| 892 | if (Args.hasArg(options::OPT_fmudflapth)) { |
| 893 | CmdArgs.push_back("-D_MUDFLAP"); |
| 894 | CmdArgs.push_back("-D_MUDFLAPTH"); |
| 895 | CmdArgs.push_back("-include"); |
| 896 | CmdArgs.push_back("mf-runtime.h"); |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | void darwin::CC1::AddCPPArgs(const ArgList &Args, |
| 901 | ArgStringList &CmdArgs) const { |
| 902 | // Derived from cpp spec. |
| 903 | |
| 904 | if (Args.hasArg(options::OPT_static)) { |
| 905 | // The gcc spec is broken here, it refers to dynamic but |
| 906 | // that has been translated. Start by being bug compatible. |
| 907 | |
| 908 | // if (!Args.hasArg(arglist.parser.dynamicOption)) |
| 909 | CmdArgs.push_back("-D__STATIC__"); |
| 910 | } else |
| 911 | CmdArgs.push_back("-D__DYNAMIC__"); |
| 912 | |
| 913 | if (Args.hasArg(options::OPT_pthread)) |
| 914 | CmdArgs.push_back("-D_REENTRANT"); |
| 915 | } |
| 916 | |
Daniel Dunbar | 40f1265 | 2009-03-29 17:08:39 +0000 | [diff] [blame] | 917 | void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA, |
| 918 | Job &Dest, const InputInfo &Output, |
| 919 | const InputInfoList &Inputs, |
| 920 | const ArgList &Args, |
| 921 | const char *LinkingOutput) const { |
| 922 | ArgStringList CmdArgs; |
| 923 | |
| 924 | assert(Inputs.size() == 1 && "Unexpected number of inputs!"); |
| 925 | |
| 926 | CmdArgs.push_back("-E"); |
| 927 | |
| 928 | if (Args.hasArg(options::OPT_traditional) || |
| 929 | Args.hasArg(options::OPT_ftraditional) || |
| 930 | Args.hasArg(options::OPT_traditional_cpp)) |
| 931 | CmdArgs.push_back("-traditional-cpp"); |
| 932 | |
| 933 | ArgStringList OutputArgs; |
| 934 | if (Output.isFilename()) { |
| 935 | OutputArgs.push_back("-o"); |
| 936 | OutputArgs.push_back(Output.getFilename()); |
| 937 | } else { |
| 938 | assert(Output.isPipe() && "Unexpected CC1 output."); |
| 939 | } |
| 940 | |
Daniel Dunbar | 9120f17 | 2009-03-29 22:27:40 +0000 | [diff] [blame] | 941 | if (Args.hasArg(options::OPT_E)) { |
| 942 | AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs); |
| 943 | } else { |
| 944 | AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList()); |
| 945 | CmdArgs.append(OutputArgs.begin(), OutputArgs.end()); |
| 946 | } |
Daniel Dunbar | 40f1265 | 2009-03-29 17:08:39 +0000 | [diff] [blame] | 947 | |
Daniel Dunbar | 8a2073a | 2009-04-03 01:27:06 +0000 | [diff] [blame] | 948 | Args.AddAllArgs(CmdArgs, options::OPT_d_Group); |
| 949 | |
Daniel Dunbar | 40f1265 | 2009-03-29 17:08:39 +0000 | [diff] [blame] | 950 | const char *CC1Name = getCC1Name(Inputs[0].getType()); |
| 951 | const char *Exec = |
| 952 | Args.MakeArgString(getToolChain().GetProgramPath(C, CC1Name).c_str()); |
| 953 | Dest.addCommand(new Command(Exec, CmdArgs)); |
| 954 | } |
| 955 | |
| 956 | void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA, |
| 957 | Job &Dest, const InputInfo &Output, |
| 958 | const InputInfoList &Inputs, |
| 959 | const ArgList &Args, |
| 960 | const char *LinkingOutput) const { |
| 961 | const Driver &D = getToolChain().getHost().getDriver(); |
| 962 | ArgStringList CmdArgs; |
| 963 | |
| 964 | assert(Inputs.size() == 1 && "Unexpected number of inputs!"); |
| 965 | |
| 966 | types::ID InputType = Inputs[0].getType(); |
| 967 | const Arg *A; |
| 968 | if ((A = Args.getLastArg(options::OPT_traditional)) || |
| 969 | (A = Args.getLastArg(options::OPT_ftraditional))) |
| 970 | D.Diag(clang::diag::err_drv_argument_only_allowed_with) |
| 971 | << A->getAsString(Args) << "-E"; |
| 972 | |
| 973 | if (Output.getType() == types::TY_LLVMAsm) |
| 974 | CmdArgs.push_back("-emit-llvm"); |
| 975 | else if (Output.getType() == types::TY_LLVMBC) |
| 976 | CmdArgs.push_back("-emit-llvm-bc"); |
| 977 | |
| 978 | ArgStringList OutputArgs; |
| 979 | if (Output.getType() != types::TY_PCH) { |
| 980 | OutputArgs.push_back("-o"); |
| 981 | if (Output.isPipe()) |
| 982 | OutputArgs.push_back("-"); |
| 983 | else if (Output.isNothing()) |
| 984 | OutputArgs.push_back("/dev/null"); |
| 985 | else |
| 986 | OutputArgs.push_back(Output.getFilename()); |
| 987 | } |
| 988 | |
| 989 | // There is no need for this level of compatibility, but it makes |
| 990 | // diffing easier. |
| 991 | bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) || |
| 992 | Args.hasArg(options::OPT_S)); |
| 993 | |
| 994 | if (types::getPreprocessedType(InputType) != types::TY_INVALID) { |
Daniel Dunbar | a3ec60e | 2009-03-29 18:40:18 +0000 | [diff] [blame] | 995 | AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs); |
Daniel Dunbar | 40f1265 | 2009-03-29 17:08:39 +0000 | [diff] [blame] | 996 | if (OutputArgsEarly) { |
| 997 | AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs); |
| 998 | } else { |
| 999 | AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList()); |
| 1000 | CmdArgs.append(OutputArgs.begin(), OutputArgs.end()); |
| 1001 | } |
| 1002 | } else { |
| 1003 | CmdArgs.push_back("-fpreprocessed"); |
| 1004 | |
| 1005 | // FIXME: There is a spec command to remove |
| 1006 | // -fpredictive-compilation args here. Investigate. |
| 1007 | |
| 1008 | for (InputInfoList::const_iterator |
| 1009 | it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { |
| 1010 | const InputInfo &II = *it; |
| 1011 | |
| 1012 | if (II.isPipe()) |
| 1013 | CmdArgs.push_back("-"); |
| 1014 | else |
| 1015 | CmdArgs.push_back(II.getFilename()); |
| 1016 | } |
| 1017 | |
| 1018 | if (OutputArgsEarly) { |
| 1019 | AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs); |
| 1020 | } else { |
| 1021 | AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList()); |
| 1022 | CmdArgs.append(OutputArgs.begin(), OutputArgs.end()); |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | if (Output.getType() == types::TY_PCH) { |
| 1027 | assert(Output.isFilename() && "Invalid PCH output."); |
| 1028 | |
| 1029 | CmdArgs.push_back("-o"); |
| 1030 | // NOTE: gcc uses a temp .s file for this, but there doesn't seem |
| 1031 | // to be a good reason. |
| 1032 | CmdArgs.push_back("/dev/null"); |
| 1033 | |
| 1034 | CmdArgs.push_back("--output-pch="); |
| 1035 | CmdArgs.push_back(Output.getFilename()); |
| 1036 | } |
| 1037 | |
| 1038 | const char *CC1Name = getCC1Name(Inputs[0].getType()); |
| 1039 | const char *Exec = |
| 1040 | Args.MakeArgString(getToolChain().GetProgramPath(C, CC1Name).c_str()); |
| 1041 | Dest.addCommand(new Command(Exec, CmdArgs)); |
| 1042 | } |
| 1043 | |
Daniel Dunbar | 8cac5f7 | 2009-03-20 16:06:39 +0000 | [diff] [blame] | 1044 | void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA, |
| 1045 | Job &Dest, const InputInfo &Output, |
| 1046 | const InputInfoList &Inputs, |
| 1047 | const ArgList &Args, |
| 1048 | const char *LinkingOutput) const { |
| 1049 | ArgStringList CmdArgs; |
| 1050 | |
| 1051 | assert(Inputs.size() == 1 && "Unexpected number of inputs."); |
| 1052 | const InputInfo &Input = Inputs[0]; |
| 1053 | |
| 1054 | // Bit of a hack, this is only used for original inputs. |
Daniel Dunbar | 8e4fea6 | 2009-04-01 00:27:44 +0000 | [diff] [blame] | 1055 | // |
| 1056 | // FIXME: This is broken for preprocessed .s inputs. |
Daniel Dunbar | 8cac5f7 | 2009-03-20 16:06:39 +0000 | [diff] [blame] | 1057 | if (Input.isFilename() && |
Daniel Dunbar | 8e4fea6 | 2009-04-01 00:27:44 +0000 | [diff] [blame] | 1058 | strcmp(Input.getFilename(), Input.getBaseInput()) == 0) { |
| 1059 | if (Args.hasArg(options::OPT_gstabs)) |
| 1060 | CmdArgs.push_back("--gstabs"); |
| 1061 | else if (Args.hasArg(options::OPT_g_Group)) |
| 1062 | CmdArgs.push_back("--gdwarf2"); |
| 1063 | } |
Daniel Dunbar | 8cac5f7 | 2009-03-20 16:06:39 +0000 | [diff] [blame] | 1064 | |
| 1065 | // Derived from asm spec. |
| 1066 | CmdArgs.push_back("-arch"); |
| 1067 | CmdArgs.push_back(getToolChain().getArchName().c_str()); |
| 1068 | |
| 1069 | CmdArgs.push_back("-force_cpusubtype_ALL"); |
| 1070 | if ((Args.hasArg(options::OPT_mkernel) || |
| 1071 | Args.hasArg(options::OPT_static) || |
| 1072 | Args.hasArg(options::OPT_fapple_kext)) && |
| 1073 | !Args.hasArg(options::OPT_dynamic)) |
| 1074 | CmdArgs.push_back("-static"); |
| 1075 | |
| 1076 | Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, |
| 1077 | options::OPT_Xassembler); |
| 1078 | |
| 1079 | assert(Output.isFilename() && "Unexpected lipo output."); |
| 1080 | CmdArgs.push_back("-o"); |
| 1081 | CmdArgs.push_back(Output.getFilename()); |
| 1082 | |
| 1083 | if (Input.isPipe()) { |
| 1084 | CmdArgs.push_back("-"); |
| 1085 | } else { |
| 1086 | assert(Input.isFilename() && "Invalid input."); |
| 1087 | CmdArgs.push_back(Input.getFilename()); |
| 1088 | } |
| 1089 | |
| 1090 | // asm_final spec is empty. |
| 1091 | |
| 1092 | const char *Exec = |
| 1093 | Args.MakeArgString(getToolChain().GetProgramPath(C, "as").c_str()); |
| 1094 | Dest.addCommand(new Command(Exec, CmdArgs)); |
| 1095 | } |
Daniel Dunbar | ff7488d | 2009-03-20 00:52:38 +0000 | [diff] [blame] | 1096 | |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 1097 | static const char *MakeFormattedString(const ArgList &Args, |
| 1098 | const llvm::format_object_base &Fmt) { |
| 1099 | std::string Str; |
| 1100 | llvm::raw_string_ostream(Str) << Fmt; |
| 1101 | return Args.MakeArgString(Str.c_str()); |
| 1102 | } |
| 1103 | |
| 1104 | /// Helper routine for seeing if we should use dsymutil; this is a |
| 1105 | /// gcc compatible hack, we should remove it and use the input |
| 1106 | /// type information. |
| 1107 | static bool isSourceSuffix(const char *Str) { |
| 1108 | // match: 'C', 'CPP', 'c', 'cc', 'cp', 'c++', 'cpp', 'cxx', 'm', |
| 1109 | // 'mm'. |
| 1110 | switch (strlen(Str)) { |
| 1111 | default: |
| 1112 | return false; |
| 1113 | case 1: |
| 1114 | return (memcmp(Str, "C", 1) == 0 || |
| 1115 | memcmp(Str, "c", 1) == 0 || |
| 1116 | memcmp(Str, "m", 1) == 0); |
| 1117 | case 2: |
| 1118 | return (memcmp(Str, "cc", 2) == 0 || |
| 1119 | memcmp(Str, "cp", 2) == 0 || |
| 1120 | memcmp(Str, "mm", 2) == 0); |
| 1121 | case 3: |
| 1122 | return (memcmp(Str, "CPP", 3) == 0 || |
| 1123 | memcmp(Str, "c++", 3) == 0 || |
| 1124 | memcmp(Str, "cpp", 3) == 0 || |
| 1125 | memcmp(Str, "cxx", 3) == 0); |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | static bool isMacosxVersionLT(unsigned (&A)[3], unsigned (&B)[3]) { |
| 1130 | for (unsigned i=0; i < 3; ++i) { |
| 1131 | if (A[i] > B[i]) return false; |
| 1132 | if (A[i] < B[i]) return true; |
| 1133 | } |
| 1134 | return false; |
| 1135 | } |
| 1136 | |
| 1137 | static bool isMacosxVersionLT(unsigned (&A)[3], |
| 1138 | unsigned V0, unsigned V1=0, unsigned V2=0) { |
| 1139 | unsigned B[3] = { V0, V1, V2 }; |
| 1140 | return isMacosxVersionLT(A, B); |
| 1141 | } |
| 1142 | |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 1143 | const toolchains::Darwin_X86 &darwin::Link::getDarwinToolChain() const { |
| 1144 | return reinterpret_cast<const toolchains::Darwin_X86&>(getToolChain()); |
| 1145 | } |
| 1146 | |
| 1147 | void darwin::Link::AddDarwinArch(const ArgList &Args, |
| 1148 | ArgStringList &CmdArgs) const { |
| 1149 | // Derived from darwin_arch spec. |
| 1150 | CmdArgs.push_back("-arch"); |
| 1151 | CmdArgs.push_back(getToolChain().getArchName().c_str()); |
| 1152 | } |
| 1153 | |
| 1154 | void darwin::Link::AddDarwinSubArch(const ArgList &Args, |
| 1155 | ArgStringList &CmdArgs) const { |
| 1156 | // Derived from darwin_subarch spec, not sure what the distinction |
| 1157 | // exists for but at least for this chain it is the same. |
| 1158 | AddDarwinArch(Args, CmdArgs); |
| 1159 | } |
| 1160 | |
| 1161 | void darwin::Link::AddLinkArgs(const ArgList &Args, |
| 1162 | ArgStringList &CmdArgs) const { |
| 1163 | const Driver &D = getToolChain().getHost().getDriver(); |
| 1164 | |
| 1165 | // Derived from the "link" spec. |
| 1166 | Args.AddAllArgs(CmdArgs, options::OPT_static); |
| 1167 | if (!Args.hasArg(options::OPT_static)) |
| 1168 | CmdArgs.push_back("-dynamic"); |
| 1169 | if (Args.hasArg(options::OPT_fgnu_runtime)) { |
| 1170 | // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu |
| 1171 | // here. How do we wish to handle such things? |
| 1172 | } |
| 1173 | |
| 1174 | if (!Args.hasArg(options::OPT_dynamiclib)) { |
| 1175 | if (Args.hasArg(options::OPT_force__cpusubtype__ALL)) { |
| 1176 | AddDarwinArch(Args, CmdArgs); |
| 1177 | CmdArgs.push_back("-force_cpusubtype_ALL"); |
| 1178 | } else |
| 1179 | AddDarwinSubArch(Args, CmdArgs); |
| 1180 | |
| 1181 | Args.AddLastArg(CmdArgs, options::OPT_bundle); |
| 1182 | Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader); |
| 1183 | Args.AddAllArgs(CmdArgs, options::OPT_client__name); |
| 1184 | |
| 1185 | Arg *A; |
| 1186 | if ((A = Args.getLastArg(options::OPT_compatibility__version)) || |
| 1187 | (A = Args.getLastArg(options::OPT_current__version)) || |
| 1188 | (A = Args.getLastArg(options::OPT_install__name))) |
| 1189 | D.Diag(clang::diag::err_drv_argument_only_allowed_with) |
| 1190 | << A->getAsString(Args) << "-dynamiclib"; |
| 1191 | |
| 1192 | Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace); |
| 1193 | Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs); |
| 1194 | Args.AddLastArg(CmdArgs, options::OPT_private__bundle); |
| 1195 | } else { |
| 1196 | CmdArgs.push_back("-dylib"); |
| 1197 | |
| 1198 | Arg *A; |
| 1199 | if ((A = Args.getLastArg(options::OPT_bundle)) || |
| 1200 | (A = Args.getLastArg(options::OPT_bundle__loader)) || |
| 1201 | (A = Args.getLastArg(options::OPT_client__name)) || |
| 1202 | (A = Args.getLastArg(options::OPT_force__flat__namespace)) || |
| 1203 | (A = Args.getLastArg(options::OPT_keep__private__externs)) || |
| 1204 | (A = Args.getLastArg(options::OPT_private__bundle))) |
| 1205 | D.Diag(clang::diag::err_drv_argument_not_allowed_with) |
| 1206 | << A->getAsString(Args) << "-dynamiclib"; |
| 1207 | |
| 1208 | Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version, |
| 1209 | "-dylib_compatibility_version"); |
| 1210 | Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version, |
| 1211 | "-dylib_current_version"); |
| 1212 | |
| 1213 | if (Args.hasArg(options::OPT_force__cpusubtype__ALL)) { |
| 1214 | AddDarwinArch(Args, CmdArgs); |
| 1215 | // NOTE: We don't add -force_cpusubtype_ALL on this path. Ok. |
| 1216 | } else |
| 1217 | AddDarwinSubArch(Args, CmdArgs); |
| 1218 | |
| 1219 | Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name, |
| 1220 | "-dylib_install_name"); |
| 1221 | } |
| 1222 | |
| 1223 | Args.AddLastArg(CmdArgs, options::OPT_all__load); |
| 1224 | Args.AddAllArgs(CmdArgs, options::OPT_allowable__client); |
| 1225 | Args.AddLastArg(CmdArgs, options::OPT_bind__at__load); |
| 1226 | Args.AddLastArg(CmdArgs, options::OPT_dead__strip); |
| 1227 | Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms); |
| 1228 | Args.AddAllArgs(CmdArgs, options::OPT_dylib__file); |
| 1229 | Args.AddLastArg(CmdArgs, options::OPT_dynamic); |
| 1230 | Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list); |
| 1231 | Args.AddLastArg(CmdArgs, options::OPT_flat__namespace); |
| 1232 | Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names); |
| 1233 | Args.AddAllArgs(CmdArgs, options::OPT_image__base); |
| 1234 | Args.AddAllArgs(CmdArgs, options::OPT_init); |
| 1235 | |
| 1236 | if (!Args.hasArg(options::OPT_mmacosx_version_min_EQ)) { |
| 1237 | if (!Args.hasArg(options::OPT_miphoneos_version_min_EQ)) { |
| 1238 | // FIXME: I don't understand what is going on here. This is |
| 1239 | // supposed to come from darwin_ld_minversion, but gcc doesn't |
| 1240 | // seem to be following that; it must be getting overridden |
| 1241 | // somewhere. |
| 1242 | CmdArgs.push_back("-macosx_version_min"); |
| 1243 | CmdArgs.push_back(getDarwinToolChain().getMacosxVersionStr()); |
| 1244 | } |
| 1245 | } else { |
| 1246 | // Adding all arguments doesn't make sense here but this is what |
| 1247 | // gcc does. |
| 1248 | Args.AddAllArgsTranslated(CmdArgs, options::OPT_mmacosx_version_min_EQ, |
| 1249 | "-macosx_version_min"); |
| 1250 | } |
| 1251 | |
| 1252 | Args.AddAllArgsTranslated(CmdArgs, options::OPT_miphoneos_version_min_EQ, |
| 1253 | "-iphoneos_version_min"); |
| 1254 | Args.AddLastArg(CmdArgs, options::OPT_nomultidefs); |
| 1255 | Args.AddLastArg(CmdArgs, options::OPT_multi__module); |
| 1256 | Args.AddLastArg(CmdArgs, options::OPT_single__module); |
| 1257 | Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined); |
| 1258 | Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused); |
| 1259 | |
| 1260 | if (Args.hasArg(options::OPT_fpie)) |
| 1261 | CmdArgs.push_back("-pie"); |
| 1262 | |
| 1263 | Args.AddLastArg(CmdArgs, options::OPT_prebind); |
| 1264 | Args.AddLastArg(CmdArgs, options::OPT_noprebind); |
| 1265 | Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding); |
| 1266 | Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules); |
| 1267 | Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs); |
| 1268 | Args.AddAllArgs(CmdArgs, options::OPT_sectcreate); |
| 1269 | Args.AddAllArgs(CmdArgs, options::OPT_sectorder); |
| 1270 | Args.AddAllArgs(CmdArgs, options::OPT_seg1addr); |
| 1271 | Args.AddAllArgs(CmdArgs, options::OPT_segprot); |
| 1272 | Args.AddAllArgs(CmdArgs, options::OPT_segaddr); |
| 1273 | Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr); |
| 1274 | Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr); |
| 1275 | Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table); |
| 1276 | Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename); |
| 1277 | Args.AddAllArgs(CmdArgs, options::OPT_sub__library); |
| 1278 | Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella); |
| 1279 | Args.AddAllArgsTranslated(CmdArgs, options::OPT_isysroot, "-syslibroot"); |
| 1280 | Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace); |
| 1281 | Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints); |
| 1282 | Args.AddAllArgs(CmdArgs, options::OPT_umbrella); |
| 1283 | Args.AddAllArgs(CmdArgs, options::OPT_undefined); |
| 1284 | Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list); |
| 1285 | Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches); |
| 1286 | |
| 1287 | if (!Args.hasArg(options::OPT_weak__reference__mismatches)) { |
| 1288 | CmdArgs.push_back("-weak_reference_mismatches"); |
| 1289 | CmdArgs.push_back("non-weak"); |
| 1290 | } |
| 1291 | |
| 1292 | Args.AddLastArg(CmdArgs, options::OPT_X_Flag); |
| 1293 | Args.AddAllArgs(CmdArgs, options::OPT_y); |
| 1294 | Args.AddLastArg(CmdArgs, options::OPT_w); |
| 1295 | Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size); |
| 1296 | Args.AddAllArgs(CmdArgs, options::OPT_segs__read__); |
| 1297 | Args.AddLastArg(CmdArgs, options::OPT_seglinkedit); |
| 1298 | Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit); |
| 1299 | Args.AddAllArgs(CmdArgs, options::OPT_sectalign); |
| 1300 | Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols); |
| 1301 | Args.AddAllArgs(CmdArgs, options::OPT_segcreate); |
| 1302 | Args.AddLastArg(CmdArgs, options::OPT_whyload); |
| 1303 | Args.AddLastArg(CmdArgs, options::OPT_whatsloaded); |
| 1304 | Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name); |
| 1305 | Args.AddLastArg(CmdArgs, options::OPT_dylinker); |
| 1306 | Args.AddLastArg(CmdArgs, options::OPT_Mach); |
| 1307 | } |
| 1308 | |
| 1309 | void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA, |
| 1310 | Job &Dest, const InputInfo &Output, |
| 1311 | const InputInfoList &Inputs, |
| 1312 | const ArgList &Args, |
| 1313 | const char *LinkingOutput) const { |
| 1314 | assert(Output.getType() == types::TY_Image && "Invalid linker output type."); |
| 1315 | // The logic here is derived from gcc's behavior; most of which |
| 1316 | // comes from specs (starting with link_command). Consult gcc for |
| 1317 | // more information. |
| 1318 | |
| 1319 | // FIXME: The spec references -fdump= which seems to have |
| 1320 | // disappeared? |
| 1321 | |
| 1322 | ArgStringList CmdArgs; |
| 1323 | |
| 1324 | // I'm not sure why this particular decomposition exists in gcc, but |
| 1325 | // we follow suite for ease of comparison. |
| 1326 | AddLinkArgs(Args, CmdArgs); |
| 1327 | |
| 1328 | // FIXME: gcc has %{x} in here. How could this ever happen? Cruft? |
| 1329 | Args.AddAllArgs(CmdArgs, options::OPT_d_Flag); |
| 1330 | Args.AddAllArgs(CmdArgs, options::OPT_s); |
| 1331 | Args.AddAllArgs(CmdArgs, options::OPT_t); |
| 1332 | Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag); |
| 1333 | Args.AddAllArgs(CmdArgs, options::OPT_u_Group); |
| 1334 | Args.AddAllArgs(CmdArgs, options::OPT_A); |
| 1335 | Args.AddLastArg(CmdArgs, options::OPT_e); |
| 1336 | Args.AddAllArgs(CmdArgs, options::OPT_m_Separate); |
| 1337 | Args.AddAllArgs(CmdArgs, options::OPT_r); |
| 1338 | |
| 1339 | // FIXME: This is just being pedantically bug compatible, gcc |
| 1340 | // doesn't *mean* to forward this, it just does (yay for pattern |
| 1341 | // matching). It doesn't work, of course. |
| 1342 | Args.AddAllArgs(CmdArgs, options::OPT_object); |
| 1343 | |
| 1344 | CmdArgs.push_back("-o"); |
| 1345 | CmdArgs.push_back(Output.getFilename()); |
| 1346 | |
| 1347 | unsigned MacosxVersion[3]; |
| 1348 | if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ)) { |
| 1349 | bool HadExtra; |
| 1350 | if (!Driver::GetReleaseVersion(A->getValue(Args), MacosxVersion[0], |
| 1351 | MacosxVersion[1], MacosxVersion[2], |
| 1352 | HadExtra) || |
| 1353 | HadExtra) { |
| 1354 | const Driver &D = getToolChain().getHost().getDriver(); |
| 1355 | D.Diag(clang::diag::err_drv_invalid_version_number) |
| 1356 | << A->getAsString(Args); |
| 1357 | } |
| 1358 | } else { |
| 1359 | getDarwinToolChain().getMacosxVersion(MacosxVersion); |
| 1360 | } |
| 1361 | |
| 1362 | if (!Args.hasArg(options::OPT_A) && |
| 1363 | !Args.hasArg(options::OPT_nostdlib) && |
| 1364 | !Args.hasArg(options::OPT_nostartfiles)) { |
| 1365 | // Derived from startfile spec. |
| 1366 | if (Args.hasArg(options::OPT_dynamiclib)) { |
| 1367 | // Derived from darwin_dylib1 spec. |
Daniel Dunbar | 8a8d8af | 2009-04-01 03:17:40 +0000 | [diff] [blame] | 1368 | if (isMacosxVersionLT(MacosxVersion, 10, 5)) |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 1369 | CmdArgs.push_back("-ldylib1.o"); |
Daniel Dunbar | 8a8d8af | 2009-04-01 03:17:40 +0000 | [diff] [blame] | 1370 | else if (isMacosxVersionLT(MacosxVersion, 10, 6)) |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 1371 | CmdArgs.push_back("-ldylib1.10.5.o"); |
| 1372 | } else { |
| 1373 | if (Args.hasArg(options::OPT_bundle)) { |
Daniel Dunbar | 8a8d8af | 2009-04-01 03:17:40 +0000 | [diff] [blame] | 1374 | if (!Args.hasArg(options::OPT_static)) { |
| 1375 | // Derived from darwin_bundle1 spec. |
| 1376 | if (isMacosxVersionLT(MacosxVersion, 10, 6)) |
| 1377 | CmdArgs.push_back("-lbundle1.o"); |
| 1378 | } |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 1379 | } else { |
| 1380 | if (Args.hasArg(options::OPT_pg)) { |
| 1381 | if (Args.hasArg(options::OPT_static) || |
| 1382 | Args.hasArg(options::OPT_object) || |
| 1383 | Args.hasArg(options::OPT_preload)) { |
| 1384 | CmdArgs.push_back("-lgcrt0.o"); |
| 1385 | } else { |
| 1386 | CmdArgs.push_back("-lgcrt1.o"); |
| 1387 | |
| 1388 | // darwin_crt2 spec is empty. |
| 1389 | } |
| 1390 | } else { |
| 1391 | if (Args.hasArg(options::OPT_static) || |
| 1392 | Args.hasArg(options::OPT_object) || |
| 1393 | Args.hasArg(options::OPT_preload)) { |
| 1394 | CmdArgs.push_back("-lcrt0.o"); |
| 1395 | } else { |
| 1396 | // Derived from darwin_crt1 spec. |
Daniel Dunbar | 8a8d8af | 2009-04-01 03:17:40 +0000 | [diff] [blame] | 1397 | if (isMacosxVersionLT(MacosxVersion, 10, 5)) |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 1398 | CmdArgs.push_back("-lcrt1.o"); |
Daniel Dunbar | 8a8d8af | 2009-04-01 03:17:40 +0000 | [diff] [blame] | 1399 | else if (isMacosxVersionLT(MacosxVersion, 10, 6)) |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 1400 | CmdArgs.push_back("-lcrt1.10.5.o"); |
Daniel Dunbar | 8a8d8af | 2009-04-01 03:17:40 +0000 | [diff] [blame] | 1401 | else |
| 1402 | CmdArgs.push_back("-lcrt1.10.6.o"); |
| 1403 | |
| 1404 | // darwin_crt2 spec is empty. |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 1405 | } |
| 1406 | } |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | if (Args.hasArg(options::OPT_shared_libgcc) && |
| 1411 | !Args.hasArg(options::OPT_miphoneos_version_min_EQ) && |
| 1412 | isMacosxVersionLT(MacosxVersion, 10, 5)) { |
| 1413 | const char *Str = getToolChain().GetFilePath(C, "crt3.o").c_str(); |
| 1414 | CmdArgs.push_back(Args.MakeArgString(Str)); |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | Args.AddAllArgs(CmdArgs, options::OPT_L); |
| 1419 | |
| 1420 | if (Args.hasArg(options::OPT_fopenmp)) |
| 1421 | // This is more complicated in gcc... |
| 1422 | CmdArgs.push_back("-lgomp"); |
| 1423 | |
| 1424 | // FIXME: Derive these correctly. |
| 1425 | const char *TCDir = getDarwinToolChain().getToolChainDir().c_str(); |
| 1426 | if (getToolChain().getArchName() == "x86_64") { |
| 1427 | CmdArgs.push_back(MakeFormattedString(Args, |
| 1428 | llvm::format("-L/usr/lib/gcc/%s/x86_64", TCDir))); |
| 1429 | // Intentionally duplicated for (temporary) gcc bug compatibility. |
| 1430 | CmdArgs.push_back(MakeFormattedString(Args, |
| 1431 | llvm::format("-L/usr/lib/gcc/%s/x86_64", TCDir))); |
| 1432 | } |
| 1433 | CmdArgs.push_back(MakeFormattedString(Args, |
| 1434 | llvm::format("-L/usr/lib/%s", TCDir))); |
| 1435 | CmdArgs.push_back(MakeFormattedString(Args, |
| 1436 | llvm::format("-L/usr/lib/gcc/%s", TCDir))); |
| 1437 | // Intentionally duplicated for (temporary) gcc bug compatibility. |
| 1438 | CmdArgs.push_back(MakeFormattedString(Args, |
| 1439 | llvm::format("-L/usr/lib/gcc/%s", TCDir))); |
| 1440 | CmdArgs.push_back(MakeFormattedString(Args, |
| 1441 | llvm::format("-L/usr/lib/gcc/%s/../../../%s", TCDir, TCDir))); |
| 1442 | CmdArgs.push_back(MakeFormattedString(Args, |
| 1443 | llvm::format("-L/usr/lib/gcc/%s/../../..", TCDir))); |
| 1444 | |
| 1445 | for (InputInfoList::const_iterator |
| 1446 | it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { |
| 1447 | const InputInfo &II = *it; |
| 1448 | if (II.isFilename()) |
| 1449 | CmdArgs.push_back(II.getFilename()); |
| 1450 | else |
| 1451 | II.getInputArg().renderAsInput(Args, CmdArgs); |
| 1452 | } |
| 1453 | |
| 1454 | if (LinkingOutput) { |
| 1455 | CmdArgs.push_back("-arch_multiple"); |
| 1456 | CmdArgs.push_back("-final_output"); |
| 1457 | CmdArgs.push_back(LinkingOutput); |
| 1458 | } |
| 1459 | |
| 1460 | if (Args.hasArg(options::OPT_fprofile_arcs) || |
| 1461 | Args.hasArg(options::OPT_fprofile_generate) || |
| 1462 | Args.hasArg(options::OPT_fcreate_profile) || |
| 1463 | Args.hasArg(options::OPT_coverage)) |
| 1464 | CmdArgs.push_back("-lgcov"); |
| 1465 | |
| 1466 | if (Args.hasArg(options::OPT_fnested_functions)) |
| 1467 | CmdArgs.push_back("-allow_stack_execute"); |
| 1468 | |
| 1469 | if (!Args.hasArg(options::OPT_nostdlib) && |
| 1470 | !Args.hasArg(options::OPT_nodefaultlibs)) { |
| 1471 | // link_ssp spec is empty. |
| 1472 | |
Daniel Dunbar | 8a8d8af | 2009-04-01 03:17:40 +0000 | [diff] [blame] | 1473 | // Derived from libgcc and lib specs but refactored. |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 1474 | if (Args.hasArg(options::OPT_static)) { |
| 1475 | CmdArgs.push_back("-lgcc_static"); |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 1476 | } else { |
Daniel Dunbar | 8a8d8af | 2009-04-01 03:17:40 +0000 | [diff] [blame] | 1477 | if (Args.hasArg(options::OPT_static_libgcc)) { |
| 1478 | CmdArgs.push_back("-lgcc_eh"); |
| 1479 | } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) { |
| 1480 | // Derived from darwin_iphoneos_libgcc spec. |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 1481 | CmdArgs.push_back("-lgcc_s.10.5"); |
Daniel Dunbar | 8a8d8af | 2009-04-01 03:17:40 +0000 | [diff] [blame] | 1482 | } else if (Args.hasArg(options::OPT_shared_libgcc) || |
| 1483 | Args.hasArg(options::OPT_fexceptions) || |
| 1484 | Args.hasArg(options::OPT_fgnu_runtime)) { |
| 1485 | // FIXME: This is probably broken on 10.3? |
| 1486 | if (isMacosxVersionLT(MacosxVersion, 10, 5)) |
| 1487 | CmdArgs.push_back("-lgcc_s.10.4"); |
| 1488 | else if (isMacosxVersionLT(MacosxVersion, 10, 6)) |
| 1489 | CmdArgs.push_back("-lgcc_s.10.5"); |
| 1490 | } else { |
| 1491 | if (isMacosxVersionLT(MacosxVersion, 10, 3, 9)) |
| 1492 | ; // Do nothing. |
| 1493 | else if (isMacosxVersionLT(MacosxVersion, 10, 5)) |
| 1494 | CmdArgs.push_back("-lgcc_s.10.4"); |
| 1495 | else if (isMacosxVersionLT(MacosxVersion, 10, 6)) |
| 1496 | CmdArgs.push_back("-lgcc_s.10.5"); |
| 1497 | } |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 1498 | |
Daniel Dunbar | 8a8d8af | 2009-04-01 03:17:40 +0000 | [diff] [blame] | 1499 | if (isMacosxVersionLT(MacosxVersion, 10, 6)) { |
| 1500 | CmdArgs.push_back("-lgcc"); |
| 1501 | CmdArgs.push_back("-lSystem"); |
| 1502 | } else { |
| 1503 | CmdArgs.push_back("-lSystem"); |
| 1504 | CmdArgs.push_back("-lgcc"); |
| 1505 | } |
| 1506 | } |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | if (!Args.hasArg(options::OPT_A) && |
| 1510 | !Args.hasArg(options::OPT_nostdlib) && |
| 1511 | !Args.hasArg(options::OPT_nostartfiles)) { |
| 1512 | // endfile_spec is empty. |
| 1513 | } |
| 1514 | |
| 1515 | Args.AddAllArgs(CmdArgs, options::OPT_T_Group); |
| 1516 | Args.AddAllArgs(CmdArgs, options::OPT_F); |
| 1517 | |
| 1518 | const char *Exec = |
| 1519 | Args.MakeArgString(getToolChain().GetProgramPath(C, "collect2").c_str()); |
| 1520 | Dest.addCommand(new Command(Exec, CmdArgs)); |
| 1521 | |
Daniel Dunbar | 0b46e1b | 2009-04-04 00:55:30 +0000 | [diff] [blame] | 1522 | // Find the first non-empty base input (we want to ignore linker |
| 1523 | // inputs). |
| 1524 | const char *BaseInput = ""; |
| 1525 | for (unsigned i = 0, e = Inputs.size(); i != e; ++i) { |
| 1526 | if (Inputs[i].getBaseInput()[0] != '\0') { |
| 1527 | BaseInput = Inputs[i].getBaseInput(); |
| 1528 | break; |
| 1529 | } |
| 1530 | } |
| 1531 | |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 1532 | if (Args.getLastArg(options::OPT_g_Group) && |
| 1533 | !Args.getLastArg(options::OPT_gstabs) && |
| 1534 | !Args.getLastArg(options::OPT_g0)) { |
| 1535 | // FIXME: This is gross, but matches gcc. The test only considers |
| 1536 | // the suffix (not the -x type), and then only of the first |
Daniel Dunbar | 0b46e1b | 2009-04-04 00:55:30 +0000 | [diff] [blame] | 1537 | // source input. Awesome. |
| 1538 | const char *Suffix = strrchr(BaseInput, '.'); |
Daniel Dunbar | 02633b5 | 2009-03-26 16:23:12 +0000 | [diff] [blame] | 1539 | if (Suffix && isSourceSuffix(Suffix + 1)) { |
| 1540 | const char *Exec = |
| 1541 | Args.MakeArgString(getToolChain().GetProgramPath(C, "dsymutil").c_str()); |
| 1542 | ArgStringList CmdArgs; |
| 1543 | CmdArgs.push_back(Output.getFilename()); |
| 1544 | C.getJobs().addCommand(new Command(Exec, CmdArgs)); |
| 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | |
Daniel Dunbar | ff7488d | 2009-03-20 00:52:38 +0000 | [diff] [blame] | 1549 | void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA, |
Daniel Dunbar | 8cac5f7 | 2009-03-20 16:06:39 +0000 | [diff] [blame] | 1550 | Job &Dest, const InputInfo &Output, |
Daniel Dunbar | ff7488d | 2009-03-20 00:52:38 +0000 | [diff] [blame] | 1551 | const InputInfoList &Inputs, |
| 1552 | const ArgList &Args, |
| 1553 | const char *LinkingOutput) const { |
| 1554 | ArgStringList CmdArgs; |
| 1555 | |
| 1556 | CmdArgs.push_back("-create"); |
| 1557 | assert(Output.isFilename() && "Unexpected lipo output."); |
Daniel Dunbar | a428df8 | 2009-03-24 00:24:37 +0000 | [diff] [blame] | 1558 | |
| 1559 | CmdArgs.push_back("-output"); |
Daniel Dunbar | ff7488d | 2009-03-20 00:52:38 +0000 | [diff] [blame] | 1560 | CmdArgs.push_back(Output.getFilename()); |
Daniel Dunbar | a428df8 | 2009-03-24 00:24:37 +0000 | [diff] [blame] | 1561 | |
Daniel Dunbar | ff7488d | 2009-03-20 00:52:38 +0000 | [diff] [blame] | 1562 | for (InputInfoList::const_iterator |
| 1563 | it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { |
| 1564 | const InputInfo &II = *it; |
| 1565 | assert(II.isFilename() && "Unexpected lipo input."); |
| 1566 | CmdArgs.push_back(II.getFilename()); |
| 1567 | } |
| 1568 | const char *Exec = |
| 1569 | Args.MakeArgString(getToolChain().GetProgramPath(C, "lipo").c_str()); |
| 1570 | Dest.addCommand(new Command(Exec, CmdArgs)); |
| 1571 | } |
Daniel Dunbar | 68a31d4 | 2009-03-31 17:45:15 +0000 | [diff] [blame] | 1572 | |
Ed Schouten | c66a5a3 | 2009-04-02 19:13:12 +0000 | [diff] [blame] | 1573 | |
Daniel Dunbar | 68a31d4 | 2009-03-31 17:45:15 +0000 | [diff] [blame] | 1574 | void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA, |
| 1575 | Job &Dest, const InputInfo &Output, |
| 1576 | const InputInfoList &Inputs, |
| 1577 | const ArgList &Args, |
| 1578 | const char *LinkingOutput) const |
| 1579 | { |
| 1580 | ArgStringList CmdArgs; |
| 1581 | |
Daniel Dunbar | 008f54a | 2009-04-01 19:36:32 +0000 | [diff] [blame] | 1582 | // When building 32-bit code on FreeBSD/amd64, we have to explicitly |
| 1583 | // instruct as in the base system to assemble 32-bit code. |
| 1584 | if (getToolChain().getArchName() == "i386") |
Daniel Dunbar | 68a31d4 | 2009-03-31 17:45:15 +0000 | [diff] [blame] | 1585 | CmdArgs.push_back("--32"); |
| 1586 | |
| 1587 | Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, |
| 1588 | options::OPT_Xassembler); |
| 1589 | |
| 1590 | CmdArgs.push_back("-o"); |
| 1591 | if (Output.isPipe()) |
| 1592 | CmdArgs.push_back("-"); |
| 1593 | else |
| 1594 | CmdArgs.push_back(Output.getFilename()); |
| 1595 | |
| 1596 | for (InputInfoList::const_iterator |
| 1597 | it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { |
| 1598 | const InputInfo &II = *it; |
| 1599 | if (II.isPipe()) |
| 1600 | CmdArgs.push_back("-"); |
| 1601 | else |
| 1602 | CmdArgs.push_back(II.getFilename()); |
| 1603 | } |
| 1604 | |
| 1605 | const char *Exec = |
| 1606 | Args.MakeArgString(getToolChain().GetProgramPath(C, "as").c_str()); |
| 1607 | Dest.addCommand(new Command(Exec, CmdArgs)); |
| 1608 | } |
Daniel Dunbar | 008f54a | 2009-04-01 19:36:32 +0000 | [diff] [blame] | 1609 | |
| 1610 | void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA, |
| 1611 | Job &Dest, const InputInfo &Output, |
| 1612 | const InputInfoList &Inputs, |
| 1613 | const ArgList &Args, |
| 1614 | const char *LinkingOutput) const |
| 1615 | { |
| 1616 | ArgStringList CmdArgs; |
| 1617 | |
| 1618 | if (Args.hasArg(options::OPT_static)) { |
| 1619 | CmdArgs.push_back("-Bstatic"); |
| 1620 | } else { |
| 1621 | CmdArgs.push_back("--eh-frame-hdr"); |
| 1622 | if (Args.hasArg(options::OPT_shared)) { |
| 1623 | CmdArgs.push_back("-Bshareable"); |
| 1624 | } else { |
| 1625 | CmdArgs.push_back("-dynamic-linker"); |
| 1626 | CmdArgs.push_back("/libexec/ld-elf.so.1"); |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | // When building 32-bit code on FreeBSD/amd64, we have to explicitly |
| 1631 | // instruct ld in the base system to link 32-bit code. |
| 1632 | if (getToolChain().getArchName() == "i386") { |
| 1633 | CmdArgs.push_back("-m"); |
| 1634 | CmdArgs.push_back("elf_i386_fbsd"); |
| 1635 | } |
| 1636 | |
| 1637 | if (Output.isPipe()) { |
| 1638 | CmdArgs.push_back("-o"); |
| 1639 | CmdArgs.push_back("-"); |
| 1640 | } else if (Output.isFilename()) { |
| 1641 | CmdArgs.push_back("-o"); |
| 1642 | CmdArgs.push_back(Output.getFilename()); |
| 1643 | } else { |
| 1644 | assert(Output.isNothing() && "Invalid output."); |
| 1645 | } |
| 1646 | |
| 1647 | if (!Args.hasArg(options::OPT_nostdlib) && |
| 1648 | !Args.hasArg(options::OPT_nostartfiles)) { |
| 1649 | if (!Args.hasArg(options::OPT_shared)) { |
| 1650 | CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crt1.o").c_str())); |
| 1651 | CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o").c_str())); |
| 1652 | CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbegin.o").c_str())); |
| 1653 | } else { |
| 1654 | CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o").c_str())); |
| 1655 | CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbeginS.o").c_str())); |
| 1656 | } |
| 1657 | } |
| 1658 | |
| 1659 | Args.AddAllArgs(CmdArgs, options::OPT_L); |
| 1660 | Args.AddAllArgs(CmdArgs, options::OPT_T_Group); |
| 1661 | Args.AddAllArgs(CmdArgs, options::OPT_e); |
| 1662 | |
| 1663 | for (InputInfoList::const_iterator |
| 1664 | it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) { |
| 1665 | const InputInfo &II = *it; |
| 1666 | if (II.isPipe()) |
| 1667 | CmdArgs.push_back("-"); |
| 1668 | else if (II.isFilename()) |
| 1669 | CmdArgs.push_back(II.getFilename()); |
| 1670 | else |
| 1671 | II.getInputArg().renderAsInput(Args, CmdArgs); |
| 1672 | } |
| 1673 | |
| 1674 | if (!Args.hasArg(options::OPT_nostdlib) && |
| 1675 | !Args.hasArg(options::OPT_nodefaultlibs)) { |
| 1676 | // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding |
| 1677 | // the default system libraries. Just mimic this for now. |
| 1678 | CmdArgs.push_back("-lgcc"); |
| 1679 | if (Args.hasArg(options::OPT_static)) { |
| 1680 | CmdArgs.push_back("-lgcc_eh"); |
| 1681 | } else { |
| 1682 | CmdArgs.push_back("--as-needed"); |
| 1683 | CmdArgs.push_back("-lgcc_s"); |
| 1684 | CmdArgs.push_back("--no-as-needed"); |
| 1685 | } |
| 1686 | |
| 1687 | if (Args.hasArg(options::OPT_pthread)) |
| 1688 | CmdArgs.push_back("-lpthread"); |
| 1689 | CmdArgs.push_back("-lc"); |
| 1690 | |
| 1691 | CmdArgs.push_back("-lgcc"); |
| 1692 | if (Args.hasArg(options::OPT_static)) { |
| 1693 | CmdArgs.push_back("-lgcc_eh"); |
| 1694 | } else { |
| 1695 | CmdArgs.push_back("--as-needed"); |
| 1696 | CmdArgs.push_back("-lgcc_s"); |
| 1697 | CmdArgs.push_back("--no-as-needed"); |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | if (!Args.hasArg(options::OPT_nostdlib) && |
| 1702 | !Args.hasArg(options::OPT_nostartfiles)) { |
| 1703 | if (!Args.hasArg(options::OPT_shared)) |
| 1704 | CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtend.o").c_str())); |
| 1705 | else |
| 1706 | CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtendS.o").c_str())); |
| 1707 | CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtn.o").c_str())); |
| 1708 | } |
| 1709 | |
| 1710 | const char *Exec = |
| 1711 | Args.MakeArgString(getToolChain().GetProgramPath(C, "ld").c_str()); |
| 1712 | Dest.addCommand(new Command(Exec, CmdArgs)); |
| 1713 | } |