xmake.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package("openmp")
  2. set_kind("library", {headeronly = true})
  3. set_homepage("https://openmp.org/")
  4. set_description("The OpenMP API specification for parallel programming")
  5. add_configs("runtime", {description = "Set OpenMP runtime for gcc/clang like compilers.", default = "default", type = "string", values = {"default", "custom"}})
  6. add_configs("feature", {description = "Set OpenMP feature for msvc.", default = "default", type = "string", values = {"default", "experimental", "llvm"}})
  7. on_load(function (package)
  8. if package.has_tool then
  9. for _, toolkind in ipairs({"cc", "cxx", "fc"}) do
  10. if package:config("runtime") == "default" then
  11. if package:has_tool(toolkind, "clang", "clangxx") then
  12. package:add("deps", "libomp")
  13. end
  14. end
  15. end
  16. end
  17. end)
  18. on_fetch(function (package)
  19. local libomp = package:dep("libomp")
  20. if libomp and not libomp:fetch() then
  21. return
  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", "icx", "icpx", "ifx") then
  51. result[flagname] = "-qopenmp"
  52. elseif package:has_tool(toolkind, "icl") then
  53. result[flagname] = "-Qopenmp"
  54. end
  55. end
  56. for _, toolkind in ipairs({"ld", "fcld"}) do
  57. if package:config("runtime") == "default" then
  58. if package:has_tool(toolkind, "clang", "clangxx") then
  59. if not package:is_plat("macosx") then
  60. result.ldflags = "-fopenmp"
  61. result.shflags = "-fopenmp"
  62. end
  63. elseif package:has_tool(toolkind, "gcc", "gxx") then
  64. result.ldflags = "-fopenmp"
  65. result.shflags = "-fopenmp"
  66. elseif package:has_tool(toolkind, "icc", "icpc", "icx", "icpx") then
  67. result.ldflags = "-qopenmp"
  68. result.shflags = "-qopenmp"
  69. elseif package:has_tool(toolkind, "icl") then
  70. result.ldflags = "-Qopenmp"
  71. result.shflags = "-Qopenmp"
  72. elseif package:has_tool(toolkind, "gfortran") then
  73. result.fcldflags = "-fopenmp"
  74. result.fcshflags = "-fopenmp"
  75. end
  76. end
  77. if package:config("runtime") == "custom" then
  78. if package:has_tool(toolkind, "link") then
  79. result.ldflags = "/nodefaultlib:vcomp"
  80. result.shflags = "/nodefaultlib:vcomp"
  81. end
  82. end
  83. end
  84. else
  85. raise("This package(openmp) requires xmake version 2.6.1 or newer.")
  86. end
  87. if package:has_tool("cu", "nvcc") and result.cxxflags then
  88. result.cuflags = "-Xcompiler " .. result.cxxflags
  89. end
  90. return (result.cflags or result.cxxflags or result.fcflags) and result
  91. end)
  92. on_install("linux", "macosx", "windows", "mingw@msys", function (package)
  93. -- we need not install anything because we need only compiler flags and deps
  94. end)
  95. on_test(function (package)
  96. assert(package:check_csnippets({test = [[
  97. #include <stdio.h>
  98. #include <omp.h>
  99. #ifndef _OPENMP
  100. # error missing openmp flags
  101. #endif
  102. static void test() {
  103. #pragma omp parallel
  104. {
  105. printf("hello %d\n", omp_get_thread_num());
  106. }
  107. }
  108. ]]}))
  109. end)