xmake.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package("openssl")
  2. set_homepage("https://www.openssl.org/")
  3. set_description("A robust, commercial-grade, and full-featured toolkit for TLS and SSL.")
  4. set_license("Apache-2.0")
  5. add_urls("https://github.com/openssl/openssl/archive/refs/tags/OpenSSL_$(version).zip", {version = function (version)
  6. return version:gsub("^(%d+)%.(%d+)%.(%d+)-?(%a*)$", "%1_%2_%3%4")
  7. end, excludes = "*/fuzz/*"})
  8. add_versions("1.1.1-w", "c543d84c06e909bfa862d5256f6d9dad502352a580b1193ae262874e4be4d7f7")
  9. add_versions("1.1.1-t", "9422774f3ce0f9cb4db5d862efbf43b4fcc096b37b4ac7157e7c5172113a8a22")
  10. add_patches("1.1.1-t", path.join(os.scriptdir(), "patches", "1.1.1t.diff"), "8009edde46e5577213212ec68f8a40451ceb514fddd892240b1465213b7d2d11")
  11. add_versions("1.1.1-s", "aa76dc0488bc67f5c964d085e37a0f5f452e45c68967816a336fa00f537f5cc5")
  12. add_versions("1.1.1-r", "20af2fac5dff04e71c2f2d6729ee84befbea741e3aaeba18a1a541e04f3f1997")
  13. add_versions("1.1.1-q", "df86e6adcff1c91a85cef139dd061ea40b7e49005e8be16522cf4864bfcf5eb8")
  14. add_patches("1.1.1-q", path.join(os.scriptdir(), "patches", "1.1.1q.diff"), "cfe6929f9db2719e695be0b61f8c38fe8132544c5c58ca8d07383bfa6c675b7b")
  15. add_versions("1.1.1-p", "7fe975ffe91d8343ebd021059eeea3c6b1d236c3826b3a08ef59fcbe75069f5b")
  16. add_patches("1.1.1-p", path.join(os.scriptdir(), "patches", "1.1.1p.diff"), "f102fed5867e143ae3ace1febd0b2725358c614c86328e68022f1ea21491b42c")
  17. add_versions("1.1.1-o", "1cd761790cf576e7f45c17798c47064f0f6756d4fcdbc1e657b0a4d9c60f3a52")
  18. add_versions("1.1.1-n", "614d69141fd622bc3db2adf7c824eaa19c7e532937b2cd7144b850d692f9f150")
  19. add_versions("1.1.1-m", "dab2287910427d82674618d512ba2571401539ca6ed12ab3c3143a0db9fad542")
  20. add_versions("1.1.1-l", "23d8908e82b63af754018256a4eb02f13965f10067969f6a63f497960c11dbeb")
  21. add_versions("1.1.1-k", "255c038f5861616f67b527434475d226f5fe00522fbd21fafd3df32019edd202")
  22. add_versions("1.1.1-h", "0a976b769bdb26470971a184f5263d0c3256152d5671ed7287cf17acc4698afc")
  23. add_versions("1.1.0-l", "a305d4af4b442ad61ba3d7e82905d09bfbd80424e132e10df4899d064aa47ce2")
  24. add_versions("1.0.2-u", "493f8b34574d0cf8598adbdec33c84b8a06f0617787c3710d20827c01291c09c")
  25. add_versions("1.0.0", "9b67e5ad1a4234c1170ada75b66321e914da4f3ebaeaef6b28400173aaa6b378")
  26. on_fetch("fetch")
  27. on_load(function (package)
  28. if not package:is_precompiled() then
  29. if package:is_plat("android") and is_subhost("windows") and os.arch() == "x64" then
  30. -- when building for android on windows, use msys2 perl instead of strawberry-perl to avoid configure issue
  31. package:add("deps", "msys2", {configs = {msystem = "MINGW64", base_devel = true}, private = true})
  32. elseif is_subhost("windows") then
  33. package:add("deps", "strawberry-perl", { private = true })
  34. if package:is_plat("windows") then
  35. package:add("deps", "nasm", { private = true })
  36. -- check xmake tool jom
  37. import("package.tools.jom", {try = true})
  38. if jom then
  39. package:add("deps", "jom", {private = true})
  40. end
  41. else
  42. wprint("package(openssl): OpenSSL should be built in a Unix-like shell (e.g., MSYS2, Git Bash) if not targeting MSVC. " ..
  43. "It seems you are not in such an environment. If you encounter build errors, please consider trying again in a Unix-like shell.")
  44. end
  45. end
  46. end
  47. -- @note we must use package:is_plat() instead of is_plat in description for supporting add_deps("openssl", {host = true}) in python
  48. if package:is_plat("windows") then
  49. package:add("links", "libssl", "libcrypto")
  50. else
  51. package:add("links", "ssl", "crypto")
  52. end
  53. if package:is_plat("windows", "mingw") then
  54. package:add("syslinks", "ws2_32", "user32", "crypt32", "advapi32")
  55. elseif package:is_plat("linux", "cross") then
  56. package:add("syslinks", "pthread", "dl")
  57. end
  58. if package:is_plat("linux") then
  59. package:add("extsources", "apt::libssl-dev")
  60. end
  61. end)
  62. on_install("windows", function (package)
  63. -- Check if Perl is using Unix-style paths
  64. local working_dir = os.iorunv("perl", {"-MFile::Spec::Functions=rel2abs", "-e", "print rel2abs('.')"})
  65. local unix_perl = working_dir:find("/") == 1
  66. if (unix_perl and package:is_plat("windows")) or (not unix_perl and not package:is_plat("windows")) then
  67. wprint("package(openssl): Detected Perl may not match your build platform. "..
  68. "If you encounter build issues, ensure you have the correct Perl installed "..
  69. "and it takes priority in your system.")
  70. end
  71. if package:version():le("1.1.0") then
  72. wprint("package(openssl): Building OpenSSL versions earlier than 1.1.1 may fail due to unresolved bugs. If you encounter build issues, please consider using a newer version.")
  73. end
  74. import("package.tools.jom", {try = true})
  75. import("package.tools.nmake")
  76. local configs = {"Configure"}
  77. if not package:version():le("1.1.0") then
  78. table.insert(configs, "no-tests")
  79. end
  80. local target
  81. if package:is_arch("x86", "i386") then
  82. target = "VC-WIN32"
  83. elseif package:is_arch("arm64") then
  84. target = "VC-WIN64-ARM"
  85. elseif package:is_arch("arm.*") then
  86. target = "VC-WIN32-ARM"
  87. else
  88. target = "VC-WIN64A"
  89. end
  90. table.insert(configs, target)
  91. table.insert(configs, package:config("shared") and "shared" or "no-shared")
  92. table.insert(configs, "--prefix=" .. package:installdir())
  93. table.insert(configs, "--openssldir=" .. package:installdir())
  94. if jom and not package:version():le("1.1.0") then
  95. table.insert(configs, "no-makedepend")
  96. table.insert(configs, "/FS")
  97. end
  98. import("configure.patch")(package)
  99. os.vrunv("perl", configs)
  100. if jom and not package:version():le("1.1.0") then
  101. jom.build(package)
  102. jom.make(package, {"install_sw"})
  103. else
  104. nmake.build(package)
  105. nmake.make(package, {"install_sw"})
  106. end
  107. end)
  108. on_install("linux", "macosx", "bsd", "cross", "android", "iphoneos", "mingw", function (package)
  109. -- https://wiki.openssl.org/index.php/Compilation_and_Installation#PREFIX_and_OPENSSLDIR
  110. local configs = (not package:version():le("1.1.0")) and {"no-tests"} or {}
  111. if package:is_cross() or package:is_plat("mingw") then
  112. local target_plat, target_arch
  113. if package:is_plat("macosx") then
  114. target_plat = "darwin64"
  115. target_arch = package:is_arch("arm64") and "arm64-cc" or "x86_64-cc"
  116. elseif package:is_plat("iphoneos") then
  117. local xcode = package:toolchain("xcode")
  118. local simulator = xcode and xcode:config("appledev") == "simulator"
  119. if simulator then
  120. target_plat = "iossimulator"
  121. target_arch = "xcrun"
  122. else
  123. if package:is_arch("arm64", "x86_64") then
  124. target_plat = "ios64"
  125. else
  126. target_plat = "ios"
  127. end
  128. target_arch = "cross"
  129. end
  130. elseif package:is_plat("mingw") then
  131. target_plat = package:is_arch("i386", "x86") and "mingw" or "mingw64"
  132. elseif package:is_plat("bsd") then
  133. target_plat = "BSD"
  134. if package:is_arch("i386") then
  135. target_arch = "x86"
  136. elseif package:is_arch("x86_64") then
  137. target_arch = "x86_64"
  138. elseif package:is_arch("arm64") then
  139. target_arch = "aarch64"
  140. elseif package:is_arch("riscv64") then
  141. target_arch = "riscv64"
  142. elseif package:is_arch(".*64") then
  143. target_arch = "generic64"
  144. end
  145. else
  146. target_plat = "linux"
  147. if package:is_arch("x86_64") then
  148. target_arch = "x86_64"
  149. elseif package:is_arch("i386", "x86") then
  150. target_arch = "x86"
  151. elseif package:is_arch("arm64", "arm64-v8a") then
  152. target_arch = "aarch64"
  153. elseif package:is_arch("arm.*") then
  154. target_arch = "armv4"
  155. elseif package:is_arch(".*64") then
  156. target_arch = "generic64"
  157. else
  158. target_arch = "generic32"
  159. end
  160. end
  161. table.insert(configs, target_arch and (target_plat .. "-" .. target_arch) or target_plat)
  162. end
  163. if package:is_plat("cross", "android") then
  164. table.insert(configs, "-DOPENSSL_NO_HEARTBEATS")
  165. table.insert(configs, "no-threads")
  166. end
  167. local installdir = package:installdir()
  168. -- Use MSYS2 paths instead of Windows paths
  169. if is_subhost("msys") then
  170. installdir = installdir:gsub("(%a):[/\\](.+)", "/%1/%2")
  171. end
  172. installdir = installdir:gsub("\\", "/")
  173. table.insert(configs, "--openssldir=" .. installdir)
  174. table.insert(configs, "--prefix=" .. installdir)
  175. table.insert(configs, package:config("shared") and "shared" or "no-shared")
  176. if package:debug() then
  177. table.insert(configs, "--debug")
  178. end
  179. local ldflags = {}
  180. if package:is_plat("mingw") and package:has_tool("mrc", "llvm_rc", "llvm-rc", "rc") then
  181. -- llvm-rc should be used with lld linker
  182. if package:has_tool("cc", "clang") then
  183. table.insert(ldflags, "-fuse-ld=lld")
  184. end
  185. end
  186. local buildenvs = import("package.tools.autoconf").buildenvs(package, {ldflags = ldflags})
  187. if is_host("windows") and package:is_plat("android") then
  188. buildenvs.CFLAGS = buildenvs.CFLAGS:gsub("\\", "/")
  189. buildenvs.CXXFLAGS = buildenvs.CXXFLAGS:gsub("\\", "/")
  190. buildenvs.CPPFLAGS = buildenvs.CPPFLAGS:gsub("\\", "/")
  191. buildenvs.ASFLAGS = buildenvs.ASFLAGS:gsub("\\", "/")
  192. end
  193. -- Check if ARFLAGS exists and is empty or contains only whitespace
  194. if buildenvs.ARFLAGS and buildenvs.ARFLAGS:match("^%s*$") then
  195. buildenvs.ARFLAGS = nil
  196. end
  197. if package:is_plat("mingw") then
  198. buildenvs.RC = package:build_getenv("mrc")
  199. if is_subhost("msys") and buildenvs.RC then
  200. buildenvs.RC = buildenvs.RC:gsub("(%a):[/\\](.+)", "/%1/%2"):gsub("\\", "/")
  201. end
  202. -- fix 'cp: directory fuzz does not exist'
  203. if package:config("shared") then
  204. os.mkdir("fuzz")
  205. end
  206. end
  207. import("configure.patch")(package)
  208. if package:is_cross() or package:is_plat("mingw") then
  209. os.vrunv("perl", table.join("./Configure", configs), {envs = buildenvs})
  210. else
  211. os.vrunv("./config", configs, {shell = true, envs = buildenvs})
  212. end
  213. import("makefile.patch")(package, {buildenvs = buildenvs})
  214. import("package.tools.make").build(package)
  215. import("package.tools.make").make(package, {"install_sw"})
  216. if package:config("shared") then
  217. os.tryrm(path.join(package:installdir("lib"), "*.a|*.dll.a"))
  218. end
  219. end)
  220. on_test(function (package)
  221. assert(package:check_csnippets({test = [[
  222. #include <openssl/ssl.h>
  223. int test(){
  224. SSL_library_init();
  225. SSL_load_error_strings();
  226. SSL_CTX *ctx = SSL_CTX_new(SSLv23_client_method());
  227. if(ctx == NULL){
  228. return 1;
  229. }
  230. SSL *ssl = SSL_new(ctx);
  231. if(ssl == NULL){
  232. SSL_CTX_free(ctx);
  233. return 1;
  234. }
  235. SSL_free(ssl);
  236. SSL_CTX_free(ctx);
  237. return 0;
  238. }
  239. ]]}))
  240. end)