xmake.lua 12 KB

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