From: Richard Smith Date: Wed, 20 May 2015 22:48:44 +0000 (+0000) Subject: [OpenMP] Make default OpenMP library (the one selected with just -fopenmp) X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=090194f4ad7b59f5712a1a5ce15960f3a52dd947;p=clang [OpenMP] Make default OpenMP library (the one selected with just -fopenmp) configurable in the CMake build. There shouldn't be any change in default behavior. Derived from a patch by Daniel Jasper! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@237850 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ce621c04b..4a8d3339df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -441,6 +441,11 @@ if(CLANG_ENABLE_STATIC_ANALYZER) add_definitions(-DCLANG_ENABLE_STATIC_ANALYZER) endif() +set(OPENMP_DEFAULT_LIB "" CACHE STRING "OpenMP library used by default for -fopenmp.") +if(OPENMP_DEFAULT_LIB) + add_definitions(-DOPENMP_DEFAULT_LIB=${OPENMP_DEFAULT_LIB}) +endif() + # Clang version information set(CLANG_EXECUTABLE_VERSION "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index 138a31b7c9..a54a592643 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -6221,6 +6221,27 @@ enum LibOpenMP { LibIOMP5 }; +/// Map a -fopenmp= macro to the corresponding library. +static LibOpenMP getOpenMPLibByName(StringRef Name) { + return llvm::StringSwitch(Name).Case("libgomp", LibGOMP) + .Case("libiomp5", LibIOMP5) + .Default(LibUnknown); +} + +/// Get the default -l flag to use for -fopenmp, if no library is +/// specified. This can be overridden at configure time. +static const char *getDefaultOpenMPLibFlag() { +#ifndef OPENMP_DEFAULT_LIB +#define OPENMP_DEFAULT_LIB iomp5 +#endif + +#define STR2(lib) #lib +#define STR(lib) STR2(lib) + return "-l" STR(OPENMP_DEFAULT_LIB); +#undef STR +#undef STR2 +} + void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, @@ -6278,27 +6299,21 @@ void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA, Args.AddAllArgs(CmdArgs, options::OPT_L); - LibOpenMP UsedOpenMPLib = LibUnknown; if (const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ)) { - UsedOpenMPLib = llvm::StringSwitch(A->getValue()) - .Case("libgomp", LibGOMP) - .Case("libiomp5", LibIOMP5) - .Default(LibUnknown); - if (UsedOpenMPLib == LibUnknown) + switch (getOpenMPLibByName(A->getValue())) { + case LibGOMP: + CmdArgs.push_back("-lgomp"); + break; + case LibIOMP5: + CmdArgs.push_back("-liomp5"); + break; + case LibUnknown: getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument) << A->getOption().getName() << A->getValue(); + break; + } } else if (Args.hasArg(options::OPT_fopenmp)) { - UsedOpenMPLib = LibIOMP5; - } - switch (UsedOpenMPLib) { - case LibGOMP: - CmdArgs.push_back("-lgomp"); - break; - case LibIOMP5: - CmdArgs.push_back("-liomp5"); - break; - case LibUnknown: - break; + CmdArgs.push_back(getDefaultOpenMPLibFlag()); } AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs); @@ -8003,37 +8018,33 @@ void gnutools::Link::ConstructJob(Compilation &C, const JobAction &JA, if (NeedsSanitizerDeps) linkSanitizerRuntimeDeps(ToolChain, CmdArgs); - LibOpenMP UsedOpenMPLib = LibUnknown; + bool WantPthread = true; if (const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ)) { - UsedOpenMPLib = llvm::StringSwitch(A->getValue()) - .Case("libgomp", LibGOMP) - .Case("libiomp5", LibIOMP5) - .Default(LibUnknown); - if (UsedOpenMPLib == LibUnknown) + switch (getOpenMPLibByName(A->getValue())) { + case LibGOMP: + CmdArgs.push_back("-lgomp"); + + // FIXME: Exclude this for platforms with libgomp that don't require + // librt. Most modern Linux platforms require it, but some may not. + CmdArgs.push_back("-lrt"); + break; + case LibIOMP5: + CmdArgs.push_back("-liomp5"); + break; + case LibUnknown: D.Diag(diag::err_drv_unsupported_option_argument) << A->getOption().getName() << A->getValue(); + break; + } } else if (Args.hasArg(options::OPT_fopenmp)) { - UsedOpenMPLib = LibIOMP5; - } - switch (UsedOpenMPLib) { - case LibGOMP: - CmdArgs.push_back("-lgomp"); - - // FIXME: Exclude this for platforms with libgomp that don't require - // librt. Most modern Linux platforms require it, but some may not. - CmdArgs.push_back("-lrt"); - break; - case LibIOMP5: - CmdArgs.push_back("-liomp5"); - break; - case LibUnknown: - break; + CmdArgs.push_back(getDefaultOpenMPLibFlag()); + } else { + WantPthread = Args.hasArg(options::OPT_pthread) || + Args.hasArg(options::OPT_pthreads); } AddRunTimeLibs(ToolChain, D, CmdArgs, Args); - if ((Args.hasArg(options::OPT_pthread) || - Args.hasArg(options::OPT_pthreads) || UsedOpenMPLib != LibUnknown) && - !isAndroid) + if (WantPthread && !isAndroid) CmdArgs.push_back("-lpthread"); CmdArgs.push_back("-lc"); diff --git a/test/OpenMP/linking.c b/test/OpenMP/linking.c index a2757a512f..778216d723 100644 --- a/test/OpenMP/linking.c +++ b/test/OpenMP/linking.c @@ -1,18 +1,20 @@ // Test the that the driver produces reasonable linker invocations with // -fopenmp or -fopenmp|libgomp. // +// FIXME: Replace DEFAULT_OPENMP_LIB below with the value chosen at configure time. +// // RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \ // RUN: -fopenmp -target i386-unknown-linux \ // RUN: | FileCheck --check-prefix=CHECK-LD-32 %s // CHECK-LD-32: "{{.*}}ld{{(.exe)?}}" -// CHECK-LD-32: "-liomp5" "-lgcc" +// CHECK-LD-32: "-l[[DEFAULT_OPENMP_LIB:[^"]*]]" "-lgcc" // CHECK-LD-32: "-lpthread" "-lc" // // RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \ // RUN: -fopenmp -target x86_64-unknown-linux \ // RUN: | FileCheck --check-prefix=CHECK-LD-64 %s // CHECK-LD-64: "{{.*}}ld{{(.exe)?}}" -// CHECK-LD-64: "-liomp5" "-lgcc" +// CHECK-LD-64: "-l[[DEFAULT_OPENMP_LIB:[^"]*]]" "-lgcc" // CHECK-LD-64: "-lpthread" "-lc" // // RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \ @@ -33,14 +35,14 @@ // RUN: -fopenmp -target i386-unknown-linux \ // RUN: | FileCheck --check-prefix=CHECK-IOMP5-LD-32 %s // CHECK-IOMP5-LD-32: "{{.*}}ld{{(.exe)?}}" -// CHECK-IOMP5-LD-32: "-liomp5" "-lgcc" +// CHECK-IOMP5-LD-32: "-l[[DEFAULT_OPENMP_LIB:[^"]*]]" "-lgcc" // CHECK-IOMP5-LD-32: "-lpthread" "-lc" // // RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \ // RUN: -fopenmp -target x86_64-unknown-linux \ // RUN: | FileCheck --check-prefix=CHECK-IOMP5-LD-64 %s // CHECK-IOMP5-LD-64: "{{.*}}ld{{(.exe)?}}" -// CHECK-IOMP5-LD-64: "-liomp5" "-lgcc" +// CHECK-IOMP5-LD-64: "-l[[DEFAULT_OPENMP_LIB:[^"]*]]" "-lgcc" // CHECK-IOMP5-LD-64: "-lpthread" "-lc" // // RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \