xmake.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package("shaderc")
  2. set_homepage("https://github.com/google/shaderc")
  3. set_description("A collection of tools, libraries, and tests for Vulkan shader compilation.")
  4. set_license("Apache-2.0")
  5. add_urls("https://github.com/google/shaderc/archive/refs/tags/$(version).tar.gz",
  6. "https://github.com/google/shaderc.git")
  7. add_versions("v2024.1", "eb3b5f0c16313d34f208d90c2fa1e588a23283eed63b101edd5422be6165d528")
  8. add_versions("v2024.0", "c761044e4e204be8e0b9a2d7494f08671ca35b92c4c791c7049594ca7514197f")
  9. add_versions("v2022.2", "517d36937c406858164673db696dc1d9c7be7ef0960fbf2965bfef768f46b8c0")
  10. add_configs("binaryonly", {description = "Only use binary program.", default = false, type = "boolean"})
  11. add_configs("exceptions", {description = "Enable exception handling", default = true, type = "boolean"})
  12. if is_plat("windows", "wasm") then
  13. add_configs("shared", {description = "Build shared library.", default = false, type = "boolean", readonly = true})
  14. end
  15. add_deps("cmake")
  16. if is_plat("linux") then
  17. add_syslinks("pthread")
  18. end
  19. on_load(function (package)
  20. if package:config("binaryonly") then
  21. package:set("kind", "binary")
  22. end
  23. if package:version():ge("2022.3") then
  24. package:add("deps", "glslang")
  25. else
  26. -- real version: glslang <1.3.231.0
  27. package:add("deps", "glslang <=1.3.211")
  28. end
  29. package:add("deps", "spirv-tools", "spirv-headers")
  30. if package:config("shared") then
  31. package:add("links", "shaderc_shared")
  32. else
  33. package:add("links", "shaderc_combined")
  34. end
  35. end)
  36. on_fetch(function (package, opt)
  37. if opt.system and package:is_binary() then
  38. return package:find_tool("glslc")
  39. end
  40. end)
  41. on_install(function (package)
  42. local opt = {}
  43. opt.packagedeps = {"glslang", "spirv-tools", "spirv-headers"}
  44. io.replace("CMakeLists.txt", "add_subdirectory(third_party)", "", {plain = true})
  45. io.replace("libshaderc_util/src/compiler.cc", "SPIRV/GlslangToSpv.h", "glslang/SPIRV/GlslangToSpv.h", {plain = true})
  46. if not package:has_tool("sh", "link") then
  47. local links = {}
  48. for _, dep in ipairs({"glslang", "spirv-tools"}) do
  49. local fetchinfo = package:dep(dep):fetch()
  50. if fetchinfo then
  51. for _, link in ipairs(fetchinfo.links) do
  52. table.insert(links, link)
  53. end
  54. end
  55. end
  56. if package:version():ge("2023.8") then
  57. io.replace("libshaderc_util/CMakeLists.txt", "glslang SPIRV", table.concat(links, " "), {plain = true})
  58. else
  59. io.replace("glslc/CMakeLists.txt", "glslang OSDependent OGLCompiler HLSL glslang SPIRV", "", {plain = true})
  60. io.replace("libshaderc_util/CMakeLists.txt", "glslang OSDependent OGLCompiler HLSL glslang SPIRV", table.concat(links, " "), {plain = true})
  61. end
  62. links = table.join({"shaderc", "shaderc_util"}, links)
  63. io.replace("glslc/CMakeLists.txt", "shaderc_util shaderc", table.concat(links, " "), {plain = true})
  64. end
  65. -- glslc --version
  66. local version_str = format("shaderc %s\nspirv-tools %s\nglslang %s\0",
  67. package:version(),
  68. package:dep("spirv-tools"):version(),
  69. package:dep("glslang"):version()
  70. )
  71. -- const char[] = {'s', 'h' ...};
  72. local version_c_array = "{"
  73. for i = 1, #version_str do
  74. local char = version_str:sub(i, i)
  75. if char == "\n" then
  76. char = [[\n]]
  77. end
  78. if char == "\0" then
  79. char = [[\0]]
  80. end
  81. version_c_array = format([[%s'%s',]], version_c_array, char)
  82. end
  83. version_c_array = version_c_array .. "}"
  84. -- remove python codegen
  85. io.writefile("glslc/src/build-version.inc", version_c_array)
  86. io.replace("glslc/CMakeLists.txt", "add_dependencies(glslc_exe build-version)", "", {plain = true})
  87. local configs = {
  88. "-DSHADERC_SKIP_EXAMPLES=ON",
  89. "-DSHADERC_SKIP_TESTS=ON",
  90. "-DSHADERC_ENABLE_COPYRIGHT_CHECK=OFF"
  91. }
  92. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
  93. table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  94. if package:is_plat("windows") then
  95. io.replace("CMakeLists.txt", [[set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")]], "", {plain = true})
  96. io.replace("CMakeLists.txt", [[set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")]], "", {plain = true})
  97. table.insert(configs, "-DSHADERC_ENABLE_SHARED_CRT=" .. (package:config("vs_runtime"):startswith("MT") and "OFF" or "ON"))
  98. end
  99. if package:config("exceptions") then
  100. table.insert(configs, "-DDISABLE_EXCEPTIONS=OFF")
  101. if package:is_plat("windows") and package:has_tool("cxx", "cl", "clang_cl") then
  102. opt.cxflags = "/EHsc"
  103. end
  104. else
  105. table.insert(configs, "-DDISABLE_EXCEPTIONS=ON")
  106. end
  107. import("package.tools.cmake").install(package, configs, opt)
  108. package:addenv("PATH", "bin")
  109. end)
  110. on_test(function (package)
  111. if not package:is_cross() then
  112. os.vrun("glslc --version")
  113. end
  114. if not package:is_binary() then
  115. assert(package:has_cfuncs("shaderc_compiler_initialize", {includes = "shaderc/shaderc.h"}))
  116. end
  117. end)