xmake.lua 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package("openmp")
  2. set_homepage("https://openmp.org/")
  3. set_description("The OpenMP API specification for parallel programming")
  4. add_configs("runtime", {description = "Set OpenMP runtime for gcc/clang like compilers.", default = "default", type = "string", values = {"default", "custom"}})
  5. add_configs("experimental", {description = "Enable experimental OpenMP feature for msvc.", default = false, type = boolean})
  6. on_load(function (package)
  7. if package.has_tool then
  8. for _, toolkind in ipairs({"cc", "cxx"}) do
  9. if package:config("runtime") == "default" then
  10. if package:has_tool(toolkind, "clang", "clangxx") then
  11. package:add("deps", "libomp")
  12. end
  13. end
  14. end
  15. end
  16. end)
  17. on_fetch(function (package)
  18. for _, dep in ipairs(package:orderdeps()) do
  19. if not dep:fetch() then
  20. return
  21. end
  22. end
  23. local result = {}
  24. if package.has_tool then
  25. for _, toolkind in ipairs({"cc", "cxx"}) do
  26. local flagname = toolkind == "cxx" and "cxxflags" or "cflags"
  27. if package:has_tool(toolkind, "cl") then
  28. result[flagname] = (package:config("experimental") and "/openmp:experimental" or "/openmp")
  29. elseif package:has_tool(toolkind, "clang", "clangxx") then
  30. if package:is_plat("macosx") then
  31. result[flagname] = "-Xpreprocessor -fopenmp"
  32. else
  33. result[flagname] = "-fopenmp"
  34. end
  35. elseif package:has_tool(toolkind, "gcc", "gxx") then
  36. result[flagname] = "-fopenmp"
  37. elseif package:has_tool(toolkind, "icc", "icpc") then
  38. result[flagname] = "-qopenmp"
  39. elseif package:has_tool(toolkind, "icl") then
  40. result[flagname] = "-Qopenmp"
  41. end
  42. if package:config("runtime") == "default" then
  43. if package:has_tool(toolkind, "clang", "clangxx") then
  44. if not package:is_plat("macosx") then
  45. result.ldflags = "-fopenmp"
  46. end
  47. elseif package:has_tool(toolkind, "gcc", "gxx") then
  48. result.ldflags = "-fopenmp"
  49. elseif package:has_tool(toolkind, "icc", "icpc") then
  50. result.ldflags = "-qopenmp"
  51. elseif package:has_tool(toolkind, "icl") then
  52. result.ldflags = "-Qopenmp"
  53. end
  54. end
  55. if package:config("runtime") == "custom" then
  56. if package:has_tool(toolkind, "cl") then
  57. result.ldflags = "/nodefaultlib:vcomp"
  58. end
  59. end
  60. end
  61. else
  62. raise("This package(openmp) requires xmake version 2.6.1 or newer.")
  63. end
  64. return (result.cflags or result.cxxflags) and result
  65. end)
  66. on_install("linux", "macosx", "windows", "mingw@msys", function (package)
  67. -- we need not install anything because we need only compiler flags and deps
  68. end)
  69. on_test(function (package)
  70. assert(package:check_csnippets({test = [[
  71. #include <stdio.h>
  72. #include <omp.h>
  73. #ifndef _OPENMP
  74. # error missing openmp flags
  75. #endif
  76. static void test() {
  77. #pragma omp parallel
  78. {
  79. printf("hello %d\n", omp_get_thread_num());
  80. }
  81. }
  82. ]]}))
  83. end)