xmake.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. if on_check then
  63. on_check(function (package)
  64. local working_dir = try {function() return os.iorunv("perl", {"-MFile::Spec::Functions=rel2abs", "-e", "print rel2abs('.')"}) end}
  65. assert(working_dir, "package(openssl): perl not found!")
  66. -- Check if Perl is using Unix-style paths
  67. local unix_perl = working_dir:find("/") == 1
  68. if (unix_perl and package:is_plat("windows")) or (not unix_perl and not package:is_plat("windows")) then
  69. wprint("package(openssl): Detected Perl may not match your build platform. "..
  70. "If you encounter build issues, ensure you have the correct Perl installed "..
  71. "and it takes priority in your system.")
  72. end
  73. if package:version():le("1.1.0") then
  74. 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.")
  75. end
  76. end)
  77. end
  78. on_install("windows", function (package)
  79. import("package.tools.jom", {try = true})
  80. import("package.tools.nmake")
  81. local configs = {"Configure"}
  82. if not package:version():le("1.1.0") then
  83. table.insert(configs, "no-tests")
  84. end
  85. local target
  86. if package:is_arch("x86", "i386") then
  87. target = "VC-WIN32"
  88. elseif package:is_arch("arm64") then
  89. target = "VC-WIN64-ARM"
  90. elseif package:is_arch("arm.*") then
  91. target = "VC-WIN32-ARM"
  92. else
  93. target = "VC-WIN64A"
  94. end
  95. table.insert(configs, target)
  96. table.insert(configs, package:config("shared") and "shared" or "no-shared")
  97. table.insert(configs, "--prefix=" .. package:installdir())
  98. table.insert(configs, "--openssldir=" .. package:installdir())
  99. if jom and not package:version():le("1.1.0") then
  100. table.insert(configs, "no-makedepend")
  101. table.insert(configs, "/FS")
  102. end
  103. import("configure.patch")(package)
  104. os.vrunv("perl", configs)
  105. if jom and not package:version():le("1.1.0") then
  106. jom.build(package)
  107. jom.make(package, {"install_sw"})
  108. else
  109. nmake.build(package)
  110. nmake.make(package, {"install_sw"})
  111. end
  112. end)
  113. on_install("linux", "macosx", "bsd", "cross", "android", "iphoneos", "mingw", function (package)
  114. -- https://wiki.openssl.org/index.php/Compilation_and_Installation#PREFIX_and_OPENSSLDIR
  115. local configs = (not package:version():le("1.1.0")) and {"no-tests"} or {}
  116. if package:is_cross() or package:is_plat("mingw") then
  117. local target_plat, target_arch
  118. if package:is_plat("macosx") then
  119. target_plat = "darwin64"
  120. target_arch = package:is_arch("arm64") and "arm64-cc" or "x86_64-cc"
  121. elseif package:is_plat("iphoneos") then
  122. local xcode = package:toolchain("xcode")
  123. local simulator = xcode and xcode:config("appledev") == "simulator"
  124. if simulator then
  125. target_plat = "iossimulator"
  126. target_arch = "xcrun"
  127. else
  128. if package:is_arch("arm64", "x86_64") then
  129. target_plat = "ios64"
  130. else
  131. target_plat = "ios"
  132. end
  133. target_arch = "cross"
  134. end
  135. elseif package:is_plat("mingw") then
  136. target_plat = package:is_arch("i386", "x86") and "mingw" or "mingw64"
  137. elseif package:is_plat("bsd") then
  138. target_plat = "BSD"
  139. if package:is_arch("i386") then
  140. target_arch = "x86"
  141. elseif package:is_arch("x86_64") then
  142. target_arch = "x86_64"
  143. elseif package:is_arch("arm64") then
  144. target_arch = "aarch64"
  145. elseif package:is_arch("riscv64") then
  146. target_arch = "riscv64"
  147. elseif package:is_arch(".*64") then
  148. target_arch = "generic64"
  149. end
  150. else
  151. target_plat = "linux"
  152. if package:is_arch("x86_64") then
  153. target_arch = "x86_64"
  154. elseif package:is_arch("i386", "x86") then
  155. target_arch = "x86"
  156. elseif package:is_arch("arm64", "arm64-v8a") then
  157. target_arch = "aarch64"
  158. elseif package:is_arch("arm.*") then
  159. target_arch = "armv4"
  160. elseif package:is_arch(".*64") then
  161. target_arch = "generic64"
  162. else
  163. target_arch = "generic32"
  164. end
  165. end
  166. table.insert(configs, target_arch and (target_plat .. "-" .. target_arch) or target_plat)
  167. end
  168. if package:is_plat("cross", "android") then
  169. table.insert(configs, "-DOPENSSL_NO_HEARTBEATS")
  170. table.insert(configs, "no-threads")
  171. end
  172. local installdir = package:installdir()
  173. -- Use MSYS2 paths instead of Windows paths
  174. if is_subhost("msys") then
  175. installdir = installdir:gsub("(%a):[/\\](.+)", "/%1/%2")
  176. end
  177. installdir = installdir:gsub("\\", "/")
  178. table.insert(configs, "--openssldir=" .. installdir)
  179. table.insert(configs, "--prefix=" .. installdir)
  180. table.insert(configs, package:config("shared") and "shared" or "no-shared")
  181. if package:debug() then
  182. table.insert(configs, "--debug")
  183. end
  184. local ldflags = {}
  185. if package:is_plat("mingw") and package:has_tool("mrc", "llvm_rc", "llvm-rc", "rc") then
  186. -- llvm-rc should be used with lld linker
  187. if package:has_tool("cc", "clang") then
  188. table.insert(ldflags, "-fuse-ld=lld")
  189. end
  190. end
  191. local buildenvs = import("package.tools.autoconf").buildenvs(package, {ldflags = ldflags})
  192. if is_host("windows") and package:is_plat("android") then
  193. buildenvs.CFLAGS = buildenvs.CFLAGS:gsub("\\", "/")
  194. buildenvs.CXXFLAGS = buildenvs.CXXFLAGS:gsub("\\", "/")
  195. buildenvs.CPPFLAGS = buildenvs.CPPFLAGS:gsub("\\", "/")
  196. buildenvs.ASFLAGS = buildenvs.ASFLAGS:gsub("\\", "/")
  197. end
  198. -- Check if ARFLAGS exists and is empty or contains only whitespace
  199. if buildenvs.ARFLAGS and buildenvs.ARFLAGS:match("^%s*$") then
  200. buildenvs.ARFLAGS = nil
  201. end
  202. if package:is_plat("mingw") then
  203. buildenvs.RC = package:build_getenv("mrc")
  204. if is_subhost("msys") and buildenvs.RC then
  205. buildenvs.RC = buildenvs.RC:gsub("(%a):[/\\](.+)", "/%1/%2"):gsub("\\", "/")
  206. end
  207. -- fix 'cp: directory fuzz does not exist'
  208. if package:config("shared") then
  209. os.mkdir("fuzz")
  210. end
  211. end
  212. import("configure.patch")(package)
  213. if package:is_cross() or package:is_plat("mingw") then
  214. os.vrunv("perl", table.join("./Configure", configs), {envs = buildenvs})
  215. else
  216. os.vrunv("./config", configs, {shell = true, envs = buildenvs})
  217. end
  218. import("makefile.patch")(package, {buildenvs = buildenvs})
  219. import("package.tools.make").build(package)
  220. import("package.tools.make").make(package, {"install_sw"})
  221. if package:config("shared") then
  222. os.tryrm(path.join(package:installdir("lib"), "*.a|*.dll.a"))
  223. end
  224. end)
  225. on_test(function (package)
  226. assert(package:check_csnippets({test = [[
  227. #include <openssl/ssl.h>
  228. int test(){
  229. SSL_library_init();
  230. SSL_load_error_strings();
  231. SSL_CTX *ctx = SSL_CTX_new(SSLv23_client_method());
  232. if(ctx == NULL){
  233. return 1;
  234. }
  235. SSL *ssl = SSL_new(ctx);
  236. if(ssl == NULL){
  237. SSL_CTX_free(ctx);
  238. return 1;
  239. }
  240. SSL_free(ssl);
  241. SSL_CTX_free(ctx);
  242. return 0;
  243. }
  244. ]]}))
  245. end)