xmake.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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("feature", {description = "Set OpenMP feature for msvc.", default = "default", type = "string", values = {"default", "experimental", "llvm"}})
  6. on_load(function (package)
  7. if package.has_tool then
  8. for _, toolkind in ipairs({"cc", "cxx", "fc"}) 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", "fc"}) do
  26. if toolkind == "cxx" then
  27. flagname = "cxxflags"
  28. elseif toolkind == "fc" then
  29. flagname = "fcflags"
  30. else
  31. flagname = "cflags"
  32. end
  33. if package:has_tool(toolkind, "cl", "clang_cl") then
  34. if package:config("feature") == "default" then
  35. result[flagname] = "/openmp"
  36. else
  37. result[flagname] = "/openmp:" .. package:config("feature")
  38. end
  39. if package:has_tool(toolkind, "clang_cl") then
  40. result.links = "libomp"
  41. end
  42. elseif package:has_tool(toolkind, "clang", "clangxx") then
  43. if package:is_plat("macosx") then
  44. result[flagname] = "-Xpreprocessor -fopenmp"
  45. else
  46. result[flagname] = "-fopenmp"
  47. end
  48. elseif package:has_tool(toolkind, "gcc", "gxx", "gfortran") then
  49. result[flagname] = "-fopenmp"
  50. elseif package:has_tool(toolkind, "icc", "icpc", "ifort") then
  51. result[flagname] = "-qopenmp"
  52. elseif package:has_tool(toolkind, "icl") then
  53. result[flagname] = "-Qopenmp"
  54. end
  55. if package:config("runtime") == "default" then
  56. if package:has_tool(toolkind, "clang", "clangxx") then
  57. if not package:is_plat("macosx") then
  58. result.ldflags = "-fopenmp"
  59. result.shflags = "-fopenmp"
  60. end
  61. elseif package:has_tool(toolkind, "gcc", "gxx", "gfortran") then
  62. result.ldflags = "-fopenmp"
  63. result.shflags = "-fopenmp"
  64. elseif package:has_tool(toolkind, "icc", "icpc") then
  65. result.ldflags = "-qopenmp"
  66. result.shflags = "-qopenmp"
  67. elseif package:has_tool(toolkind, "icl") then
  68. result.ldflags = "-Qopenmp"
  69. result.shflags = "-Qopenmp"
  70. end
  71. end
  72. if package:config("runtime") == "custom" then
  73. if package:has_tool(toolkind, "cl") then
  74. result.ldflags = "/nodefaultlib:vcomp"
  75. result.shflags = "/nodefaultlib:vcomp"
  76. end
  77. end
  78. end
  79. else
  80. raise("This package(openmp) requires xmake version 2.6.1 or newer.")
  81. end
  82. return (result.cflags or result.cxxflags or result.fcflags) and result
  83. end)
  84. on_install("linux", "macosx", "windows", "mingw@msys", function (package)
  85. -- we need not install anything because we need only compiler flags and deps
  86. end)
  87. on_test(function (package)
  88. assert(package:check_csnippets({test = [[
  89. #include <stdio.h>
  90. #include <omp.h>
  91. #ifndef _OPENMP
  92. # error missing openmp flags
  93. #endif
  94. static void test() {
  95. #pragma omp parallel
  96. {
  97. printf("hello %d\n", omp_get_thread_num());
  98. }
  99. }
  100. ]]}))
  101. end)