diff --git a/clang/lib/Driver/Tools.cpp b/clang/lib/Driver/Tools.cpp index 542c150983c6..705b8779df70 100644 --- a/clang/lib/Driver/Tools.cpp +++ b/clang/lib/Driver/Tools.cpp @@ -155,19 +155,30 @@ static bool isObjCAutoRefCount(const ArgList &Args) { } static void addProfileRT(const ToolChain &TC, const ArgList &Args, - ArgStringList &CmdArgs) { - if (Args.hasArg(options::OPT_fprofile_arcs) || - Args.hasArg(options::OPT_fprofile_generate) || - Args.hasArg(options::OPT_fcreate_profile) || - Args.hasArg(options::OPT_coverage)) { - // GCC links libgcov.a by adding -L/gcc/lib/gcc// -lgcov - // to the link line. We cannot do the same thing because unlike gcov - // there is a libprofile_rt.so. We used to use the -l:libprofile_rt.a - // syntax, but that is not supported by old linkers. - const char *lib = Args.MakeArgString(TC.getDriver().Dir + "/../lib/" + - "libprofile_rt.a"); - CmdArgs.push_back(lib); + ArgStringList &CmdArgs, + llvm::Triple Triple) { + if (!(Args.hasArg(options::OPT_fprofile_arcs) || + Args.hasArg(options::OPT_fprofile_generate) || + Args.hasArg(options::OPT_fcreate_profile) || + Args.hasArg(options::OPT_coverage))) + return; + + // GCC links libgcov.a by adding -L/gcc/lib/gcc// -lgcov to + // the link line. We cannot do the same thing because unlike gcov there is a + // libprofile_rt.so. We used to use the -l:libprofile_rt.a syntax, but that is + // not supported by old linkers. + llvm::Twine ProfileRT = + llvm::Twine(TC.getDriver().Dir) + "/../lib/" + "libprofile_rt.a"; + + if (Triple.getOS() == llvm::Triple::Darwin) { + // On Darwin, if the static library doesn't exist try the dylib. + bool Exists; + if (llvm::sys::fs::exists(ProfileRT.str(), Exists) || !Exists) + ProfileRT = + llvm::Twine(TC.getDriver().Dir) + "/../lib/" + "libprofile_rt.dylib"; } + + CmdArgs.push_back(Args.MakeArgString(ProfileRT)); } void Clang::AddPreprocessingOptions(const Driver &D, @@ -3241,7 +3252,7 @@ void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA, // endfile_spec is empty. } - addProfileRT(getToolChain(), Args, CmdArgs); + addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple()); Args.AddAllArgs(CmdArgs, options::OPT_T_Group); Args.AddAllArgs(CmdArgs, options::OPT_F); @@ -3400,7 +3411,7 @@ void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA, getToolChain().GetFilePath("crtend.o"))); } - addProfileRT(getToolChain(), Args, CmdArgs); + addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple()); const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld")); @@ -3708,7 +3719,7 @@ void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA, "crtn.o"))); } - addProfileRT(getToolChain(), Args, CmdArgs); + addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple()); const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld")); @@ -3863,7 +3874,7 @@ void netbsd::Link::ConstructJob(Compilation &C, const JobAction &JA, "crtn.o"))); } - addProfileRT(getToolChain(), Args, CmdArgs); + addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple()); const char *Exec = Args.MakeArgString(FindTargetProgramPath(getToolChain(), ToolTriple.getTriple(), @@ -4087,7 +4098,7 @@ void linuxtools::Link::ConstructJob(Compilation &C, const JobAction &JA, } } - addProfileRT(getToolChain(), Args, CmdArgs); + addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple()); if (Args.hasArg(options::OPT_use_gold_plugin)) { CmdArgs.push_back("-plugin"); @@ -4171,7 +4182,7 @@ void minix::Link::ConstructJob(Compilation &C, const JobAction &JA, "/usr/gnu/lib/libend.a"))); } - addProfileRT(getToolChain(), Args, CmdArgs); + addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple()); const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("/usr/gnu/bin/gld")); @@ -4328,7 +4339,7 @@ void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA, getToolChain().GetFilePath("crtn.o"))); } - addProfileRT(getToolChain(), Args, CmdArgs); + addProfileRT(getToolChain(), Args, CmdArgs, getToolChain().getTriple()); const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("ld"));