2
0

xmake.lua 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package("libxml2")
  2. set_homepage("http://xmlsoft.org/")
  3. set_description("The XML C parser and toolkit of Gnome.")
  4. set_license("MIT")
  5. add_urls("https://download.gnome.org/sources/libxml2/$(version).tar.xz", {version = function (version) return format("%d.%d/libxml2-%s", version:major(), version:minor(), version) end})
  6. add_urls("https://gitlab.gnome.org/GNOME/libxml2.git")
  7. add_versions("2.10.3", "5d2cc3d78bec3dbe212a9d7fa629ada25a7da928af432c93060ff5c17ee28a9c")
  8. add_versions("2.13.2", "e7c8f5e0b5542159e0ddc409c22c9164304b581eaa9930653a76fb845b169263")
  9. add_configs("iconv", {description = "Enable libiconv support.", default = false, type = "boolean"})
  10. add_configs("python", {description = "Enable the python interface.", default = false, type = "boolean"})
  11. add_includedirs("include/libxml2")
  12. if is_plat("windows") then
  13. add_syslinks("wsock32", "ws2_32", "bcrypt")
  14. else
  15. add_links("xml2")
  16. end
  17. if is_plat("linux") then
  18. add_extsources("pkgconfig::libxml-2.0", "apt::libxml2-dev", "pacman::libxml2")
  19. add_syslinks("m")
  20. end
  21. on_load("windows", "macosx", "linux", "iphoneos", "android", "bsd", function (package)
  22. if package:is_plat("windows") then
  23. if not package:config("shared") then
  24. package:add("defines", "LIBXML_STATIC")
  25. end
  26. else
  27. if package:gitref() then
  28. package:add("deps", "autoconf", "automake", "libtool", "pkg-config")
  29. end
  30. end
  31. if package:config("python") then
  32. if package:is_cross() then
  33. raise("libxml2 python interface does not support cross-compilation")
  34. end
  35. if not package:config("iconv") then
  36. raise("libxml2 python interface requires iconv to be enabled")
  37. end
  38. package:add("deps", "python 3.x")
  39. end
  40. if package:config("iconv") then
  41. package:add("deps", "libiconv")
  42. end
  43. end)
  44. on_install("windows", function (package)
  45. os.cd("win32")
  46. local args = {"configure.js", "iso8859x=yes", "lzma=no", "zlib=no", "compiler=msvc"}
  47. table.insert(args, "cruntime=/" .. package:config("vs_runtime"))
  48. table.insert(args, "debug=" .. (package:debug() and "yes" or "no"))
  49. table.insert(args, "iconv=" .. (package:config("iconv") and "yes" or "no"))
  50. table.insert(args, "python=" .. (package:config("python") and "yes" or "no"))
  51. table.insert(args, "prefix=" .. package:installdir())
  52. if package:config("iconv") then
  53. table.insert(args, "include=" .. package:dep("libiconv"):installdir("include"))
  54. table.insert(args, "lib=" .. package:dep("libiconv"):installdir("lib"))
  55. end
  56. os.vrunv("cscript", args)
  57. import("package.tools.nmake").install(package, {"/f", "Makefile.msvc"})
  58. os.tryrm(path.join(package:installdir("bin"), "run*.exe"))
  59. os.tryrm(path.join(package:installdir("bin"), "test*.exe"))
  60. os.tryrm(path.join(package:installdir("lib"), "libxml2_a_dll.lib"))
  61. if package:config("shared") then
  62. os.tryrm(path.join(package:installdir("lib"), "libxml2_a.lib"))
  63. else
  64. os.tryrm(path.join(package:installdir("lib"), "libxml2.lib"))
  65. os.tryrm(path.join(package:installdir("bin"), "libxml2.dll"))
  66. end
  67. package:addenv("PATH", package:installdir("bin"))
  68. if package:config("python") then
  69. os.cd("../python")
  70. io.replace("libxml_wrap.h", "XML_IGNORE_PEDANTIC_WARNINGS", "XML_IGNORE_DEPRECATION_WARNINGS")
  71. io.replace("setup.py", "[xml_includes]", "[xml_includes,\"" .. package:dep("libiconv"):installdir("include"):gsub("\\", "\\\\") .. "\"]", {plain = true})
  72. io.replace("setup.py", "WITHDLLS = 1", "WITHDLLS = 0", {plain = true})
  73. if not package:config("shared") then
  74. io.replace("setup.py", "libdirs = [", format("libdirs = [\n'%s',", package:dep("libiconv"):installdir("lib"):gsub("\\", "\\\\")), {plain = true})
  75. io.replace("setup.py", "platformLibs = []", "platformLibs = ['iconv','wsock32','ws2_32']", {plain = true})
  76. io.replace("setup.py", "\"xml2\"", "\"xml2_a\"", {plain = true})
  77. io.replace("setup.py", "macros = []", "macros = [('LIBXML_STATIC','1')]", {plain = true})
  78. else
  79. os.cp(path.join(package:installdir("bin"), "libxml2.dll"), path.join(package:installdir("lib"), "site-packages", "libxml2.dll"))
  80. end
  81. os.mkdir(path.join(package:installdir("lib"), "site-packages"))
  82. os.vrunv("python", {"-m", "pip", "install", "--prefix=" .. package:installdir(), "."}, {envs = {PYTHONPATH = path.join(package:installdir("lib"), "site-packages")}})
  83. package:addenv("PYTHONPATH", path.join(package:installdir("lib"), "site-packages"))
  84. end
  85. end)
  86. on_install("macosx", "linux", "iphoneos", "android@!windows", "bsd", function (package)
  87. if package:is_plat("iphoneos") then
  88. io.replace("dict.c", "defined(HAVE_GETENTROPY)", "0", {plain = true}) -- getentropy is private on iOS
  89. end
  90. import("package.tools.autoconf")
  91. local configs = {"--disable-dependency-tracking",
  92. "--without-lzma",
  93. "--without-zlib"}
  94. if package:config("shared") then
  95. table.insert(configs, "--enable-shared=yes")
  96. table.insert(configs, "--enable-static=no")
  97. else
  98. table.insert(configs, "--enable-shared=no")
  99. table.insert(configs, "--enable-static=yes")
  100. end
  101. if package:config("iconv") then
  102. local iconvdir
  103. local iconv = package:dep("libiconv"):fetch()
  104. if iconv then
  105. iconvdir = table.wrap(iconv.sysincludedirs or iconv.includedirs)[1]
  106. end
  107. if iconvdir then
  108. table.insert(configs, "--with-iconv=" .. path.directory(iconvdir))
  109. else
  110. table.insert(configs, "--with-iconv")
  111. end
  112. else
  113. table.insert(configs, "--without-iconv")
  114. end
  115. if package:config("pic") ~= false then
  116. table.insert(configs, "--with-pic")
  117. end
  118. local envs = autoconf.buildenvs(package)
  119. if package:config("python") then
  120. table.insert(configs, "--with-python")
  121. table.insert(configs, "--with-ftp")
  122. table.insert(configs, "--with-legacy")
  123. local python = package:dep("python"):fetch()
  124. if python then
  125. local cflags, ldflags
  126. for _, includedir in ipairs(python.sysincludedirs or python.includedirs) do
  127. cflags = (cflags or "") .. " -I" .. includedir
  128. end
  129. for _, linkdir in ipairs(python.linkdirs) do
  130. ldflags = (ldflags or "") .. " -L" .. linkdir
  131. end
  132. envs.PYTHON_CFLAGS = cflags
  133. envs.PYTHON_LIBS = ldflags
  134. end
  135. else
  136. table.insert(configs, "--without-python")
  137. end
  138. autoconf.install(package, configs, {envs = envs})
  139. package:addenv("PATH", package:installdir("bin"))
  140. if package:config("python") then
  141. os.cd("python")
  142. io.replace("setup.py", "[xml_includes]", "[xml_includes,\"" .. package:dep("libiconv"):installdir("include") .. "\"]", {plain = true})
  143. if not package:config("shared") then
  144. io.replace("setup.py", "libdirs = [", format("libdirs = [\n'%s',", package:dep("libiconv"):installdir("lib")), {plain = true})
  145. io.replace("setup.py", "platformLibs = [\"m\",\"z\"]", "platformLibs = [\"iconv\",\"m\"]", {plain = true})
  146. else
  147. io.replace("setup.py", "platformLibs = [\"m\",\"z\"]", "platformLibs = [\"m\"]", {plain = true})
  148. end
  149. local python = package:dep("python")
  150. local pythonver = nil
  151. if python:is_system() then
  152. pythonver = import("core.base.semver").new(python:fetch().version)
  153. else
  154. pythonver = python:version()
  155. end
  156. os.vrunv("python3", {"-m", "pip", "install", "--prefix=" .. package:installdir(), "."}, {envs = {PYTHONPATH = path.join(package:installdir("lib"), format("python%s.%s", pythonver:major(), pythonver:minor()), "site-packages")}})
  157. package:addenv("PYTHONPATH", path.join(package:installdir("lib"), format("python%s.%s", pythonver:major(), pythonver:minor()), "site-packages"))
  158. end
  159. end)
  160. on_test(function (package)
  161. if package:config("python") then
  162. if package:is_plat("windows") then
  163. os.vrun("python -c \"import libxml2\"")
  164. else
  165. os.vrun("python3 -c \"import libxml2\"")
  166. end
  167. end
  168. assert(package:has_cfuncs("xmlNewNode", {includes = {"libxml/parser.h", "libxml/tree.h"}}))
  169. end)