xmake.lua 4.7 KB

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