xmake.lua 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. local dep_packages = {}
  2. local options = {{name = "udp", package = "kcp"},
  3. {name = "http", package = "http_parser"},
  4. {name = "zlib", package = is_plat("android", "windows") and "" or "zlib"},
  5. {name = "brotli", package = "brotli"},
  6. {name = "ssl", package = ""},
  7. {name = "iconv", package = ""}}
  8. local winCommonSrcPath = (get_config("hpversion") == "v5.7.3") and "Windows/Common/Src/" or "Windows/Src/Common/"
  9. local winBuiltinDependentLibPath = (get_config("hpversion") == "v5.7.3") and "Windows/Common/Lib/" or "Windows/Dependent/"
  10. for _, opt in ipairs(options) do
  11. local opt_name = "no_" .. opt.name
  12. option(opt_name)
  13. set_default(false)
  14. set_showmenu(true)
  15. set_category("option")
  16. set_description("Build hpsocket without " .. opt.name)
  17. add_defines("_" .. string.upper(opt.name) .. "_DISABLED")
  18. option_end()
  19. if not has_config(opt_name) and opt.package ~= "" then
  20. add_requires(opt.package, is_plat("windows") and {} or {configs = {cxflags = "-fpic"}})
  21. table.insert(dep_packages, opt.package)
  22. end
  23. end
  24. option("no_4c")
  25. set_default(false)
  26. set_showmenu(true)
  27. set_category("option")
  28. set_description("Build hpsocket without C interface")
  29. option_end()
  30. option("unicode")
  31. set_default(false)
  32. set_showmenu(true)
  33. set_category("option")
  34. set_description("Build hpsocket with unicode character set")
  35. option_end()
  36. option("hpversion")
  37. set_default("v5.9.1")
  38. set_showmenu(true)
  39. set_category("option")
  40. set_description("The version of HP-Socket")
  41. option_end()
  42. add_rules("mode.debug", "mode.release")
  43. target("hpsocket")
  44. before_build(function (target)
  45. if is_plat("windows") then
  46. io.writefile("stdafx.h", [[
  47. #pragma once
  48. #include "]] .. winCommonSrcPath .. [[GeneralHelper.h"
  49. ]])
  50. io.writefile("stdafx.cpp", [[
  51. #include "stdafx.h"
  52. ]])
  53. end
  54. end)
  55. set_kind("$(kind)")
  56. for _, opt in ipairs(options) do
  57. add_options("no_" .. opt.name)
  58. end
  59. for _, pkg in ipairs(dep_packages) do
  60. add_packages(pkg)
  61. end
  62. local exclude_file
  63. local install_files = {}
  64. local no_4c = has_config("no_4c")
  65. set_basename(no_4c and "hpsocket" or "hpsocket4c")
  66. exclude_file = no_4c and "HPSocket4C.*|HPSocket4C-SSL.*" or "HPSocket.*|HPSocket-SSL.*"
  67. if is_plat("windows") then
  68. add_syslinks("ws2_32", "user32", "kernel32")
  69. if not has_config("no_ssl") then
  70. add_syslinks("crypt32")
  71. end
  72. elseif is_plat("linux") then
  73. add_syslinks("pthread", "dl", "rt")
  74. elseif is_plat("android") then
  75. add_syslinks("dl")
  76. if not has_config("no_zlib") then
  77. add_syslinks("z")
  78. end
  79. end
  80. if is_plat("windows") then
  81. if has_config("unicode") then
  82. add_defines("UNICODE", "_UNICODE")
  83. end
  84. set_pcxxheader("stdafx.h")
  85. add_files("stdafx.cpp")
  86. add_files(path.join(winCommonSrcPath, "http/*.c"))
  87. add_files(path.join(winCommonSrcPath, "*.cpp"))
  88. add_files("Windows/Src/*.cpp|" .. exclude_file)
  89. add_headerfiles("Windows/Include/HPSocket/*.h|" .. exclude_file)
  90. add_defines(is_kind("shared") and "HPSOCKET_EXPORTS" or "HPSOCKET_STATIC_LIB")
  91. local vs = get_config("vs")
  92. local vs_ver = "10.0"
  93. if vs == "2015" then vs_ver = "14.0"
  94. elseif vs == "2017" then vs_ver = "15.0"
  95. elseif vs == "2019" then vs_ver = "16.0"
  96. elseif vs == "2022" then vs_ver = "17.0"
  97. end
  98. if get_config("hpversion") >= "v5.9.1" then
  99. vs_ver = (vs == "2015" and "100" or "14x")
  100. end
  101. local arch = "x64"
  102. if is_arch("x64", "x86_64", "arm64.*") then
  103. arch = "x64"
  104. else
  105. arch = "x86"
  106. end
  107. add_includedirs(".")
  108. add_includedirs(path.join(winBuiltinDependentLibPath, "openssl", vs_ver, arch, "include"))
  109. ssllinkdir = path.join(winBuiltinDependentLibPath, "openssl", vs_ver, arch, "lib")
  110. add_linkdirs(ssllinkdir)
  111. add_includedirs(path.join(winBuiltinDependentLibPath, "zlib", vs_ver, arch, "include"))
  112. zliblinkdir = path.join(winBuiltinDependentLibPath, "zlib", vs_ver, arch, "lib")
  113. add_linkdirs(zliblinkdir)
  114. if not has_config("no_ssl") then
  115. add_links("libssl", "libcrypto")
  116. if is_kind("static") then
  117. table.insert(install_files, path.join(ssllinkdir, "*.lib"))
  118. end
  119. end
  120. if not has_config("no_zlib") then
  121. add_links("zlib")
  122. if is_kind("static") then
  123. table.insert(install_files, path.join(zliblinkdir, "*.lib"))
  124. end
  125. end
  126. elseif is_plat("linux", "android") then
  127. add_cxflags("-fpic", {force = true})
  128. add_files("Linux/src/common/crypto/*.cpp")
  129. add_files("Linux/src/common/http/*.c")
  130. add_files("Linux/src/common/*.cpp")
  131. add_files("Linux/src/*.cpp|" .. exclude_file)
  132. add_headerfiles("Linux/include/hpsocket/*.h|" .. exclude_file)
  133. add_headerfiles("Linux/include/hpsocket/(common/*.h)")
  134. if is_plat("android") then
  135. add_includedirs("Linux/dependent/android-ndk/$(arch)/include")
  136. linkdir = "Linux/dependent/android-ndk/$(arch)/lib"
  137. add_linkdirs(linkdir)
  138. if not has_config("no_iconv") then
  139. add_links("charset", "iconv")
  140. if is_kind("static") then
  141. table.insert(install_files, path.join(linkdir, "libcharset.a"))
  142. table.insert(install_files, path.join(linkdir, "libiconv.a"))
  143. end
  144. end
  145. else
  146. local arch = is_arch("x64", "x86_64") and "x64" or "x86"
  147. if is_arch("arm64.*") then
  148. arch = "arm64"
  149. elseif is_arch("arm.*") then
  150. arch = "arm"
  151. end
  152. add_includedirs(path.join("Linux/dependent", arch, "include"))
  153. linkdir = path.join("Linux/dependent", arch, "lib")
  154. add_linkdirs(linkdir)
  155. add_links("jemalloc_pic")
  156. if is_kind("static") then
  157. table.insert(install_files, path.join(linkdir, "libjemalloc_pic.a"))
  158. end
  159. end
  160. if not has_config("no_ssl") then
  161. add_links("ssl", "crypto")
  162. if is_kind("static") then
  163. table.insert(install_files, path.join(linkdir, "libssl.a"))
  164. table.insert(install_files, path.join(linkdir, "libcrypto.a"))
  165. end
  166. end
  167. end
  168. for _, file in ipairs(install_files) do
  169. add_installfiles(file, {prefixdir = "lib"})
  170. end