xmake.lua 8.6 KB

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