xmake.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. includes(path.join(os.scriptdir(), "versions.lua"))
  2. package("libcurl")
  3. set_homepage("https://curl.haxx.se/")
  4. set_description("The multiprotocol file transfer library.")
  5. set_license("MIT")
  6. set_urls("https://curl.haxx.se/download/curl-$(version).tar.bz2")
  7. add_urls("https://github.com/curl/curl/releases/download/curl-$(version).tar.bz2",
  8. {version = function (version) return (version:gsub("%.", "_")) .. "/curl-" .. version end})
  9. if add_versionfiles then
  10. add_versionfiles("versions.txt")
  11. else
  12. add_versions_list()
  13. end
  14. add_patches("7.84.0", path.join(os.scriptdir(), "patches", "7.84.0", "sched.patch"), "e79f56f840cbc6996a153f19d9266bd46fe4154e6b494c8ee0478cb5b87662d3")
  15. add_patches("8.7.1", path.join(os.scriptdir(), "patches", "8.7.1", "android_armv7.patch"), "b172fd25063fcf4bce987b47a3d95d9d79bcf80f45e7e45dbf4aba72c685fb24")
  16. add_patches("8.9.0", path.join(os.scriptdir(), "patches", "8.9.0", "fix-apple-sdk-bug.patch"), "9503db07a76d828ab7d33565c6aa65c9df80626c11248a4b670aaf10b42e4de7")
  17. add_configs("cares", {description = "Enable c-ares support.", default = false, type = "boolean"})
  18. add_configs("openssl", {description = "Enable OpenSSL for SSL/TLS.", default = nil, type = "boolean"})
  19. add_configs("openssl3", {description = "Enable OpenSSL-3 for SSL/TLS.", default = nil, type = "boolean"})
  20. add_configs("mbedtls", {description = "Enable mbedTLS for SSL/TLS.", default = nil, type = "boolean"})
  21. add_configs("nghttp2", {description = "Use Nghttp2 library.", default = false, type = "boolean"})
  22. add_configs("openldap", {description = "Use OpenLDAP library.", default = false, type = "boolean"})
  23. add_configs("libidn2", {description = "Use Libidn2 for IDN support.", default = false, type = "boolean"})
  24. add_configs("zlib", {description = "Enable zlib support.", default = false, type = "boolean"})
  25. add_configs("zstd", {description = "Enable zstd support.", default = false, type = "boolean"})
  26. add_configs("brotli", {description = "Enable brotli support.", default = false, type = "boolean"})
  27. add_configs("libssh2", {description = "Use libSSH2 library.", default = false, type = "boolean"})
  28. add_configs("libpsl", {description = "Use libpsl library.", default = false, type = "boolean"})
  29. if is_plat("android") and is_host("windows") then
  30. add_deps("ninja")
  31. set_policy("package.cmake_generator.ninja", true)
  32. end
  33. -- we init all configurations in on_load, because package("curl") need it.
  34. on_load(function (package)
  35. if package:is_plat("linux", "bsd", "android", "cross") then
  36. -- if no TLS backend has been enabled nor disabled, prefer openssl3 on modern systems
  37. if package:config("openssl") == nil and package:config("openssl3") == nil and package:config("mbedtls") == nil then
  38. -- Default to OpenSSL 3.0+ on Linux systems (Ubuntu 22.04+, Fedora 36+, etc.)
  39. -- This helps avoid conflicts with other packages like libgit2 that use OpenSSL 3.0+
  40. if package:is_plat("linux") then
  41. package:config_set("openssl3", true)
  42. else
  43. package:config_set("openssl", true)
  44. end
  45. end
  46. end
  47. assert(not (package:config("openssl") and package:config("openssl3")), "OpenSSL and OpenSSL-3 cannot be enabled at the same time.")
  48. if package:config("libssh2") then
  49. package:config_set("zlib", true)
  50. end
  51. if package:is_plat("macosx", "iphoneos") then
  52. package:add("frameworks", "Security", "CoreFoundation", "SystemConfiguration")
  53. elseif package:is_plat("linux", "bsd") then
  54. package:add("syslinks", "pthread")
  55. elseif package:is_plat("windows", "mingw") then
  56. package:add("syslinks", "advapi32", "crypt32", "wldap32", "winmm", "ws2_32", "user32")
  57. end
  58. if package:is_plat("mingw") and is_subhost("msys") then
  59. package:add("extsources", "pacman::curl")
  60. elseif package:is_plat("linux", "bsd") then
  61. package:add("extsources", "pacman::curl", "apt::libcurl4-gnutls-dev", "apt::libcurl4-nss-dev", "apt::libcurl4-openssl-dev")
  62. elseif package:is_plat("macosx") then
  63. package:add("extsources", "brew::curl")
  64. end
  65. if package:is_plat("windows", "mingw") then
  66. if not package:config("shared") then
  67. package:add("defines", "CURL_STATICLIB")
  68. end
  69. end
  70. package:add("deps", "cmake")
  71. local configdeps = {cares = "c-ares",
  72. openssl = "openssl",
  73. openssl3 = "openssl3",
  74. mbedtls = "mbedtls",
  75. nghttp2 = "nghttp2",
  76. openldap = "openldap",
  77. libidn2 = "libidn2",
  78. libpsl = "libpsl",
  79. zlib = "zlib",
  80. zstd = "zstd",
  81. brotli = "brotli",
  82. libssh2 = "libssh2"}
  83. local has_deps = false
  84. for name, dep in pairs(configdeps) do
  85. if package:config(name) then
  86. package:add("deps", dep, {host = package:is_binary()})
  87. has_deps = true
  88. end
  89. end
  90. if has_deps and package:is_plat("linux", "bsd", "macosx") then
  91. package:add("deps", "pkg-config")
  92. end
  93. end)
  94. on_install("windows", "mingw", "linux", "bsd", "macosx", "iphoneos", "cross", "android", function (package)
  95. local version = package:version()
  96. local configs = {"-DBUILD_TESTING=OFF", "-DENABLE_MANUAL=OFF", "-DENABLE_CURL_MANUAL=OFF"}
  97. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
  98. table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  99. if (package:is_plat("mingw") and version:ge("7.85")) then
  100. package:add("syslinks", "bcrypt")
  101. end
  102. local configopts = {cares = "ENABLE_ARES",
  103. mbedtls = (version:ge("7.81") and "CURL_USE_MBEDTLS" or "CMAKE_USE_MBEDTLS"),
  104. nghttp2 = "USE_NGHTTP2",
  105. libidn2 = "USE_LIBIDN2",
  106. zlib = "CURL_ZLIB",
  107. zstd = "CURL_ZSTD",
  108. brotli = "CURL_BROTLI",
  109. libssh2 = (version:ge("7.81") and "CURL_USE_LIBSSH2" or "CMAKE_USE_LIBSSH2"),
  110. libpsl = "CURL_USE_LIBPSL"}
  111. for name, opt in pairs(configopts) do
  112. table.insert(configs, "-D" .. opt .. "=" .. (package:config(name) and "ON" or "OFF"))
  113. end
  114. table.insert(configs, "-D" .. (version:ge("7.81") and "CURL_USE_OPENSSL" or "CMAKE_USE_OPENSSL") .. "=" .. ((package:config("openssl") or package:config("openssl3")) and "ON" or "OFF"))
  115. if not package:config("openldap") then
  116. table.insert(configs, "-DCURL_DISABLE_LDAP=ON")
  117. end
  118. if package:is_plat("windows", "mingw") then
  119. table.insert(configs, (version:ge("7.80") and "-DCURL_USE_SCHANNEL=ON" or "-DCMAKE_USE_SCHANNEL=ON"))
  120. end
  121. if package:is_plat("macosx", "iphoneos") then
  122. table.insert(configs, (version:ge("7.65") and "-DCURL_USE_SECTRANSP=ON" or "-DCMAKE_USE_DARWINSSL=ON"))
  123. end
  124. if package:is_plat("windows") then
  125. table.insert(configs, "-DCURL_STATIC_CRT=" .. (package:config("vs_runtime"):startswith("MT") and "ON" or "OFF"))
  126. end
  127. if package:is_plat("mingw") and version:le("7.85.0") then
  128. io.replace("src/CMakeLists.txt", 'COMMAND ${CMAKE_COMMAND} -E echo "/* built-in manual is disabled, blank function */" > tool_hugehelp.c', "", {plain = true})
  129. end
  130. if package:is_plat("linux", "bsd", "cross") then
  131. io.replace("CMakeLists.txt", "list(APPEND CURL_LIBS OpenSSL::SSL OpenSSL::Crypto)", "list(APPEND CURL_LIBS OpenSSL::SSL OpenSSL::Crypto dl)", {plain = true})
  132. io.replace("CMakeLists.txt", "list(APPEND CURL_LIBS ${OPENSSL_LIBRARIES})", "list(APPEND CURL_LIBS ${OPENSSL_LIBRARIES} dl)", {plain = true})
  133. end
  134. local function handledependency(conf, depname, includeconfig, libconfig)
  135. if package:config(conf) then
  136. local dep = package:dep(depname)
  137. if dep and not dep:is_system() then
  138. local fetchinfo = dep:fetch({external = false})
  139. if fetchinfo then
  140. local includedirs = fetchinfo.includedirs or fetchinfo.sysincludedirs
  141. if includedirs and #includedirs > 0 then
  142. table.insert(configs, "-D" .. includeconfig .. "=" .. table.concat(includedirs, ";"):gsub("\\", "/"))
  143. end
  144. if type(libconfig) == "table" then
  145. if fetchinfo.libfiles then
  146. for _, libfile in ipairs(fetchinfo.libfiles) do
  147. local libname = path.basename(libfile)
  148. if libname:startswith("lib") then
  149. libname = libname:sub(4)
  150. end
  151. for opt, suffix in pairs(libconfig) do
  152. if libname:endswith(suffix) then
  153. table.insert(configs, "-D" .. opt .. "=" .. libfile:gsub("\\", "/"))
  154. end
  155. end
  156. end
  157. end
  158. else
  159. if fetchinfo.libfiles then
  160. table.insert(configs, "-D" .. libconfig .. "=" .. table.concat(fetchinfo.libfiles, ";"):gsub("\\", "/"))
  161. end
  162. end
  163. end
  164. end
  165. end
  166. end
  167. handledependency("brotli", "brotli", "BROTLI_INCLUDE_DIR", {BROTLICOMMON_LIBRARY = "brotlicommon", BROTLIDEC_LIBRARY = "brotlidec"})
  168. handledependency("openssl", "openssl", "OPENSSL_INCLUDE_DIR", {OPENSSL_CRYPTO_LIBRARY = "crypto", OPENSSL_SSL_LIBRARY = "ssl"})
  169. handledependency("openssl3", "openssl3", "OPENSSL_INCLUDE_DIR", {OPENSSL_CRYPTO_LIBRARY = "crypto", OPENSSL_SSL_LIBRARY = "ssl"})
  170. handledependency("mbedtls", "mbedtls", "MBEDTLS_INCLUDE_DIRS", {MBEDTLS_LIBRARY = "mbedtls", MBEDX509_LIBRARY = "mbedx509", MBEDCRYPTO_LIBRARY = "mbedcrypto"})
  171. handledependency("zlib", "zlib", "ZLIB_INCLUDE_DIR", "ZLIB_LIBRARY")
  172. handledependency("zstd", "zstd", "Zstd_INCLUDE_DIR", "Zstd_LIBRARY")
  173. local libssh2 = package:dep("libssh2")
  174. if libssh2 then
  175. local libssh2_deps = {"ZLIB::ZLIB"}
  176. local backend = libssh2:config("backend")
  177. if backend == "openssl" or backend == "openssl3" then
  178. table.join2(libssh2_deps, {"OpenSSL::SSL", "OpenSSL::Crypto"})
  179. elseif backend == "mbedtls" then
  180. table.join2(libssh2_deps, {"${MBEDTLS_LIBRARIES}"})
  181. end
  182. if package:is_plat("windows", "mingw") then
  183. table.join2(libssh2_deps, {"ws2_32", "user32", "crypt32", "advapi32"})
  184. end
  185. io.replace("CMakeLists.txt",
  186. "list(APPEND CURL_LIBS ${LIBSSH2_LIBRARIES})",
  187. format("list(APPEND CURL_LIBS ${LIBSSH2_LIBRARIES} %s)", table.concat(libssh2_deps, " ")), {plain = true})
  188. end
  189. local openssl = package:dep("openssl") or package:dep("openssl3")
  190. if openssl and not openssl:is_system() then
  191. table.insert(configs, "-DOPENSSL_ROOT_DIR=" .. openssl:installdir())
  192. end
  193. import("package.tools.cmake").install(package, configs, {buildir = "build"})
  194. end)
  195. on_test(function (package)
  196. assert(package:has_cfuncs("curl_version", {includes = "curl/curl.h"}))
  197. end)