xmake.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package("acl-dev")
  2. set_homepage("https://acl-dev.cn")
  3. set_description("C/C++ server and network library, including coroutine, redis client, http/https/websocket, mqtt, mysql/postgresql/sqlite client with C/C++ for Linux, Android, iOS, MacOS, Windows.")
  4. set_license("LGPL-3.0")
  5. add_urls("https://github.com/acl-dev/acl/archive/refs/tags/$(version).tar.gz",
  6. "https://github.com/acl-dev/acl.git")
  7. add_versions("v3.6.5", "dba2fe5c70b34d75e2f2ca642bdcb5bad1abe53f116e8162939ecfd6579adabd")
  8. add_versions("v3.6.4", "2c98f4ff58f774c6dd5e8753a6a32db2045a2d40b77d65b0e5ebdaaffa348285")
  9. add_versions("v3.6.2", "888fd9b8fb19db4f8e7760a12a28f37f24ba0a2952bb0409b8380413a4b6506b")
  10. add_versions("v3.6.3", "4c1fe78cc3dbf2843aab440ca638464d1d1e490e81e904115b8f96a88a3b44de")
  11. add_patches(">=3.6.2", "patches/v3.6.2/build_install_only_static_or_shared.diff", "179136ceec3a54c9d8a60d92bc67d691271ffcf8214160224b0b9339a26cd0a1")
  12. add_patches(">=3.6.2", "patches/v3.6.2/export_unix.diff", "13376d9374de1b97ec25f709205f927a7157852075c2583e57615b617c45c62d")
  13. add_patches(">=3.6.2", "patches/v3.6.2/fix_android_install_path.diff", "19917bd1852af4ddecc27ef402ecf9806b89ec78d91e62c806ba00fc05f41e94")
  14. add_patches(">=3.6.2", "patches/v3.6.2/debundle_zlib.diff", "43043fb8fe84ef8f37a6a637e0447a849d38155e6d6ca20a9512c38023077a04")
  15. if is_plat("windows") then
  16. add_configs("vs", {description = "Use Visual Studio buildsystem (.sln/.vcxproj)", default = false, type = "boolean"})
  17. end
  18. add_includedirs("include", "include/acl-lib")
  19. add_deps("cmake")
  20. if not is_plat("windows") then
  21. add_links("protocol", "acl_cpp", "fiber_cpp", "fiber", "acl")
  22. end
  23. if not is_plat("windows") then
  24. add_deps("zlib")
  25. end
  26. if is_plat("iphoneos", "macosx", "bsd") then
  27. add_deps("libiconv")
  28. end
  29. if is_plat("windows") then
  30. add_syslinks("ws2_32", "iphlpapi", "kernel32", "user32", "gdi32")
  31. elseif is_plat("linux", "bsd", "cross") then
  32. add_syslinks("pthread", "dl")
  33. if is_plat("bsd") then
  34. add_syslinks("execinfo")
  35. end
  36. end
  37. on_load(function (package)
  38. if package:is_plat("iphoneos", "macosx", "bsd") then
  39. package:add("patches", ">=3.6.2", "patches/v3.6.2/debundle_iconv.diff", "03db2a366167c865eb6bcd73d06b5d87fa3ed87307aa86bc2d0de9528dd29e10")
  40. end
  41. if package:is_plat("android") then
  42. package:add("defines", "ANDROID")
  43. elseif package:is_plat("macosx") then
  44. package:add("defines", "MACOSX")
  45. elseif package:is_plat("linux", "cross") then
  46. package:add("defines", "LINUX2")
  47. elseif package:is_plat("bsd") then
  48. package:add("defines", "FREEBSD")
  49. end
  50. end)
  51. on_install("windows", "android", "iphoneos", "macosx", "linux", "cross", "bsd", function (package)
  52. if package:is_plat("windows") and package:config("vs") then
  53. import("package.tools.msbuild")
  54. for _, vcxproj in ipairs(os.files("**.vcxproj")) do
  55. -- Switch vs_runtime MD / MDd -> MT / MTd
  56. if package:has_runtime("MT", "MTd") then
  57. io.replace(vcxproj, "MultiThreadedDebugDLL", "MultiThreadedDebug", {plain = true})
  58. io.replace(vcxproj, "MultiThreadedDLL", "MultiThreaded", {plain = true})
  59. io.replace(vcxproj, "<IgnoreSpecificDefaultLibraries>libcmt;libc</IgnoreSpecificDefaultLibraries>", "", {plain = true})
  60. io.replace(vcxproj, "<IgnoreSpecificDefaultLibraries>libcmtd;libcmt;libc</IgnoreSpecificDefaultLibraries>", "", {plain = true})
  61. end
  62. -- Disble LTCG
  63. io.replace(vcxproj, "<WholeProgramOptimization>true</WholeProgramOptimization>", "<WholeProgramOptimization>false</WholeProgramOptimization>", {plain = true})
  64. end
  65. os.cp("lib_fiber/c/include/fiber/**", package:installdir("include/acl-lib/fiber"))
  66. os.cp("lib_protocol/include/**", package:installdir("include/acl-lib/protocol"))
  67. os.cp("lib_acl_cpp/include/acl_cpp/**", package:installdir("include/acl-lib/acl_cpp"))
  68. os.cp("lib_acl/include/**", package:installdir("include/acl-lib/acl"))
  69. os.cp("lib_fiber/cpp/include/fiber/**", package:installdir("include/acl-lib/fiber_cpp"))
  70. local arch = package:is_arch("x64") and "x64" or "Win32"
  71. if package:is_arch("arm64") then
  72. arch = "ARM64"
  73. end
  74. local mode = package:is_debug() and "Debug" or "Release"
  75. if package:config("shared") then
  76. mode = package:is_debug() and "DebugDll" or "ReleaseDll"
  77. end
  78. local configs = {"acl_cpp_vc2022.sln", "/t:lib_acl;libfiber;lib_protocol;lib_acl_cpp;libfiber_cpp"}
  79. table.insert(configs, "/p:Configuration=" .. mode)
  80. table.insert(configs, "/p:Platform=" .. arch)
  81. msbuild.build(package, configs)
  82. os.cp("**.lib", package:installdir("lib"))
  83. if package:config("shared") then
  84. for _, dll in ipairs(os.files("**.dll")) do
  85. if not dll:lower():find("71.dll") and not dll:lower():find("vld.dll") then
  86. os.cp(dll, package:installdir("bin"))
  87. end
  88. end
  89. end
  90. else
  91. -- Fix windows .pch file
  92. io.replace("lib_acl_cpp/CMakeLists.txt", [["-Ycacl_stdafx.hpp"]], [[]], {plain = true})
  93. io.replace("lib_acl_cpp/CMakeLists.txt", [[add_library(acl_cpp_static STATIC ${lib_src})]],
  94. "add_library(acl_cpp_static STATIC ${lib_src})\ntarget_precompile_headers(acl_cpp_static PRIVATE src/acl_stdafx.hpp)", {plain = true})
  95. io.replace("lib_acl_cpp/CMakeLists.txt", [[add_library(acl_cpp_shared SHARED ${lib_src})]],
  96. "add_library(acl_cpp_shared SHARED ${lib_src})\ntarget_precompile_headers(acl_cpp_shared PRIVATE src/acl_stdafx.hpp)", {plain = true})
  97. -- Remove global FIBER_DLL definitions to fix armasm64 build
  98. io.replace("lib_fiber/c/CMakeLists.txt", [[add_definitions("-DFIBER_DLL -DFIBER_EXPORTS")]], [[]], {plain = true})
  99. io.replace("lib_fiber/c/CMakeLists.txt", "VERSION 2.8.0", "VERSION 3.15.0", {plain = true})
  100. if package:config("shared") then
  101. -- Add FIBER_DLL only for C/C++ sources
  102. io.replace("lib_fiber/c/CMakeLists.txt", [[add_library(fiber_shared SHARED ${lib_src})]],
  103. [[add_library(fiber_shared SHARED ${lib_src})
  104. if(MSVC)
  105. target_compile_definitions(fiber_shared PRIVATE $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:FIBER_DLL> $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:FIBER_EXPORTS>)
  106. endif()]], {plain = true})
  107. end
  108. if package:config("shared") then
  109. io.replace("lib_fiber/c/CMakeLists.txt", [["-D_WINSOCK_DEPRECATED_NO_WARNINGS"]], [["-DBOOST_CONTEXT_DYN_LINK" "-DBOOST_CONTEXT_EXPORT=EXPORT"
  110. "-D_WINSOCK_DEPRECATED_NO_WARNINGS"]], {plain = true})
  111. else
  112. io.replace("lib_fiber/c/CMakeLists.txt", [["-D_WINSOCK_DEPRECATED_NO_WARNINGS"]], [["-DBOOST_CONTEXT_STATIC_LINK" "-DBOOST_CONTEXT_EXPORT="
  113. "-D_WINSOCK_DEPRECATED_NO_WARNINGS"]], {plain = true})
  114. end
  115. if package:is_plat("windows") then
  116. -- Do not build .gas on windows
  117. if not package:is_arch("arm.*") then
  118. io.replace("lib_fiber/c/CMakeLists.txt", [[enable_language(C CXX ASM)]], [[enable_language(C CXX ASM_MASM)]], {plain = true})
  119. if package:check_sizeof("void*") == "8" then
  120. io.replace("lib_fiber/c/CMakeLists.txt",
  121. [[list(APPEND lib_src ${src}/fiber/boost/make_gas.S]],
  122. [[list(APPEND lib_src ${src}/fiber/boost/make_x86_64_ms_pe_masm.asm]], {plain = true})
  123. io.replace("lib_fiber/c/CMakeLists.txt",
  124. [[${src}/fiber/boost/jump_gas.S)]],
  125. [[${src}/fiber/boost/jump_x86_64_ms_pe_masm.asm)]], {plain = true})
  126. else
  127. io.replace("lib_fiber/c/CMakeLists.txt",
  128. [[list(APPEND lib_src ${src}/fiber/boost/make_gas.S]],
  129. [[list(APPEND lib_src ${src}/fiber/boost/make_i386_ms_pe_masm.asm]], {plain = true})
  130. io.replace("lib_fiber/c/CMakeLists.txt",
  131. [[${src}/fiber/boost/jump_gas.S)]],
  132. [[${src}/fiber/boost/jump_i386_ms_pe_masm.asm)]], {plain = true})
  133. end
  134. else
  135. io.replace("lib_fiber/c/CMakeLists.txt", [[enable_language(C CXX ASM)]], [[enable_language(C CXX ASM_MARMASM)]], {plain = true})
  136. if package:check_sizeof("void*") == "8" then
  137. os.cp(path.join(package:scriptdir(), "port", "ontop_arm64_aapcs_pe_armasm.asm"), "lib_fiber/c/src/fiber/boost/ontop_arm64_aapcs_pe_armasm.asm")
  138. os.cp(path.join(package:scriptdir(), "port", "jump_arm64_aapcs_pe_armasm.asm"), "lib_fiber/c/src/fiber/boost/jump_arm64_aapcs_pe_armasm.asm")
  139. os.cp(path.join(package:scriptdir(), "port", "make_arm64_aapcs_pe_armasm.asm"), "lib_fiber/c/src/fiber/boost/make_arm64_aapcs_pe_armasm.asm")
  140. io.replace("lib_fiber/c/CMakeLists.txt",
  141. [[list(APPEND lib_src ${src}/fiber/boost/make_gas.S]],
  142. [[list(APPEND lib_src ${src}/fiber/boost/make_arm64_aapcs_pe_armasm.asm]], {plain = true})
  143. io.replace("lib_fiber/c/CMakeLists.txt",
  144. [[${src}/fiber/boost/jump_gas.S)]],
  145. [[${src}/fiber/boost/jump_arm64_aapcs_pe_armasm.asm)]], {plain = true})
  146. else
  147. io.replace("lib_fiber/c/CMakeLists.txt",
  148. [[list(APPEND lib_src ${src}/fiber/boost/make_gas.S]],
  149. [[list(APPEND lib_src ${src}/fiber/boost/make_arm_aapcs_pe_armasm.asm]], {plain = true})
  150. io.replace("lib_fiber/c/CMakeLists.txt",
  151. [[${src}/fiber/boost/jump_gas.S)]],
  152. [[${src}/fiber/boost/jump_arm_aapcs_pe_armasm.asm)]], {plain = true})
  153. end
  154. end
  155. else
  156. io.replace("CMakeLists.txt", "project(acl)", "project(acl)\nfind_package(ZLIB)", {plain = true})
  157. end
  158. if package:is_plat("iphoneos", "macosx", "bsd") then
  159. if package:is_plat("bsd") then
  160. -- FreeBSD enforce fallback to system iconv
  161. io.replace("lib_acl_cpp/CMakeLists.txt", [[elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")]],
  162. [[elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
  163. add_definitions("-DUSE_SYS_ICONV")]], {plain = true})
  164. end
  165. io.replace("CMakeLists.txt", "project(acl)", "project(acl)\nfind_package(Iconv)", {plain = true})
  166. end
  167. for _, file in ipairs(os.files("**.txt")) do
  168. -- Disable -Wstrict-prototypes -Werror -Qunused-arguments
  169. io.replace(file, [["-Wstrict-prototypes"]], "", {plain = true})
  170. io.replace(file, [["-Werror"]], "", {plain = true})
  171. io.replace(file, [[-Qunused-arguments]], [[]], {plain = true})
  172. -- Do not enforce LTO
  173. io.replace(file, [[add_definitions("-flto")]], [[]], {plain = true})
  174. io.replace(file, [[-flto]], [[]], {plain = true})
  175. if package:is_plat("windows") then
  176. -- Cleanup ZLIB after patch for Windows OS
  177. io.replace(file, [[ZLIB::ZLIB]], [[]], {plain = true})
  178. end
  179. end
  180. local configs = {"-DCMAKE_POLICY_DEFAULT_CMP0057=NEW"}
  181. if package:is_plat("iphoneos") then
  182. table.insert(configs, "-DCMAKE_SYSTEM_NAME=Darwin")
  183. end
  184. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:is_debug() and "DEBUG" or "RELEASE"))
  185. table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  186. table.insert(configs, "-DACL_BUILD_SHARED=" .. (package:config("shared") and "YES" or "NO"))
  187. import("package.tools.cmake").install(package, configs)
  188. if package:is_plat("windows") then
  189. if package:config("shared") then
  190. os.vcp(path.join(package:buildir(), "*/shared/**.lib"), package:installdir("lib"))
  191. os.vcp(path.join(package:buildir(), "*/shared/**.dll"), package:installdir("bin"))
  192. else
  193. os.vcp(path.join(package:buildir(), "*/static/**.lib"), package:installdir("lib"))
  194. end
  195. end
  196. end
  197. end)
  198. on_test(function (package)
  199. assert(package:has_cfuncs("acl_fiber_recv", {includes = "fiber/lib_fiber.h"}))
  200. assert(package:check_cxxsnippets({test = [[
  201. void test() {
  202. const char* redis_addr = "127.0.0.1:7000";
  203. int max_conns = 100;
  204. acl::redis_client_cluster cluster;
  205. cluster.set(redis_addr, max_conns);
  206. }
  207. ]]}, {includes = "acl_cpp/lib_acl.hpp"}))
  208. end)