r237850 - [OpenMP] Make default OpenMP library (the one selected with just -fopenmp)

Richard Smith richard-llvm at metafoo.co.uk
Wed May 20 15:48:44 PDT 2015


Author: rsmith
Date: Wed May 20 17:48:44 2015
New Revision: 237850

URL: http://llvm.org/viewvc/llvm-project?rev=237850&view=rev
Log:
[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!

Modified:
    cfe/trunk/CMakeLists.txt
    cfe/trunk/lib/Driver/Tools.cpp
    cfe/trunk/test/OpenMP/linking.c

Modified: cfe/trunk/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CMakeLists.txt?rev=237850&r1=237849&r2=237850&view=diff
==============================================================================
--- cfe/trunk/CMakeLists.txt (original)
+++ cfe/trunk/CMakeLists.txt Wed May 20 17:48:44 2015
@@ -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

Modified: cfe/trunk/lib/Driver/Tools.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/Tools.cpp?rev=237850&r1=237849&r2=237850&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/Tools.cpp (original)
+++ cfe/trunk/lib/Driver/Tools.cpp Wed May 20 17:48:44 2015
@@ -6221,6 +6221,27 @@ enum LibOpenMP {
   LibIOMP5
 };
 
+/// Map a -fopenmp=<blah> macro to the corresponding library.
+static LibOpenMP getOpenMPLibByName(StringRef Name) {
+  return llvm::StringSwitch<LibOpenMP>(Name).Case("libgomp", LibGOMP)
+                                            .Case("libiomp5", LibIOMP5)
+                                            .Default(LibUnknown);
+}
+
+/// Get the default -l<blah> 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(Compilat
 
   Args.AddAllArgs(CmdArgs, options::OPT_L);
 
-  LibOpenMP UsedOpenMPLib = LibUnknown;
   if (const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ)) {
-    UsedOpenMPLib = llvm::StringSwitch<LibOpenMP>(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(Compil
       if (NeedsSanitizerDeps)
         linkSanitizerRuntimeDeps(ToolChain, CmdArgs);
 
-      LibOpenMP UsedOpenMPLib = LibUnknown;
+      bool WantPthread = true;
       if (const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ)) {
-        UsedOpenMPLib = llvm::StringSwitch<LibOpenMP>(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");

Modified: cfe/trunk/test/OpenMP/linking.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/OpenMP/linking.c?rev=237850&r1=237849&r2=237850&view=diff
==============================================================================
--- cfe/trunk/test/OpenMP/linking.c (original)
+++ cfe/trunk/test/OpenMP/linking.c Wed May 20 17:48:44 2015
@@ -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 \





More information about the cfe-commits mailing list