new.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import("core.base.option")
  2. import("core.base.semver")
  3. import("core.base.json")
  4. import("lib.detect.find_tool")
  5. import("lib.detect.find_file")
  6. import("net.http")
  7. import("devel.git")
  8. import("utils.archive")
  9. local options = {
  10. {nil, "repo", "v", nil, "Set repository name.",
  11. "e.g. ",
  12. " - github:xmake-io/xmake",
  13. " - brew:zlib"}
  14. }
  15. function _generate_package_from_github(reponame)
  16. -- get repository info
  17. local gh = assert(find_tool("gh"), "gh not found!")
  18. local repoinfo = os.iorunv(gh.program, {"repo", "view", reponame, "--json",
  19. "description,homepageUrl,licenseInfo,url,sshUrl,name,latestRelease"})
  20. if repoinfo then
  21. repoinfo = json.decode(repoinfo)
  22. end
  23. vprint(repoinfo)
  24. -- generate package header
  25. local packagename = assert(repoinfo.name, "package name not found!"):lower()
  26. local packagefile = path.join("packages", packagename:sub(1, 1), packagename, "xmake.lua")
  27. local file = io.open(packagefile, "w")
  28. file:print('package("%s")', packagename)
  29. local homepage = repoinfo.homepageUrl
  30. if homepage == nil or homepage == "" then
  31. homepage = repoinfo.url
  32. end
  33. if homepage then
  34. file:print(' set_homepage("%s")', homepage)
  35. end
  36. local description = repoinfo.description or ("The " .. packagename .. " package")
  37. file:print(' set_description("%s")', description)
  38. local licensekey = type(repoinfo.licenseInfo) == "table" and repoinfo.licenseInfo.key
  39. if licensekey then
  40. local licenses = {
  41. ["apache-2.0"] = "Apache-2.0",
  42. ["lgpl-2.0"] = "LGPL-2.0",
  43. ["lgpl-2.1"] = "LGPL-2.1",
  44. zlib = "zlib",
  45. mit = "MIT"
  46. }
  47. local license = licenses[licensekey]
  48. if license then
  49. file:print(' set_license("%s")', license)
  50. end
  51. end
  52. file:print("")
  53. -- generate package urls and versions
  54. local repodir
  55. local has_xmake
  56. local has_cmake
  57. local has_meson
  58. local has_autoconf
  59. local need_autogen
  60. local latest_release = repoinfo.latestRelease
  61. if type(latest_release) == "table" then
  62. local url = ("https://github.com/%s/archive/refs/tags/%s.tar.gz"):format(reponame, latest_release.tagName)
  63. local giturl = ("https://github.com/%s.git"):format(reponame)
  64. file:write(' add_urls("https://github.com/' .. reponame .. '/archive/refs/tags/$(version).tar.gz",\n')
  65. file:print(' "%s")', giturl)
  66. local tmpfile = os.tmpfile({ramdisk = false}) .. ".tar.gz"
  67. repodir = tmpfile .. ".dir"
  68. print("downloading %s", url)
  69. http.download(url, tmpfile)
  70. file:print(' add_versions("%s", "%s")', latest_release.tagName, hash.sha256(tmpfile))
  71. archive.extract(tmpfile, repodir)
  72. os.rm(tmpfile)
  73. else
  74. local giturl = ("https://github.com/%s.git"):format(reponame)
  75. repodir = os.tmpfile({ramdisk = false})
  76. file:print(' add_urls("%s")', giturl)
  77. print("downloading %s", giturl)
  78. git.clone(giturl, {outputdir = repodir, depth = 1})
  79. local commit = git.lastcommit({repodir = repodir})
  80. local version = try{ function() return os.iorunv("git", {"log", "-1", "--date=format:%Y.%m.%d", "--format=%ad"}, {curdir = repodir}) end}
  81. if version then
  82. file:print(' add_versions("%s", "%s")', version:trim(), commit)
  83. end
  84. end
  85. -- detect build system
  86. if repodir then
  87. local files = os.files(path.join(repodir, "*")) or {}
  88. table.join2(files, os.files(path.join(repodir, "*", "*")))
  89. for _, file in ipairs(files) do
  90. local filename = path.filename(file)
  91. if filename == "xmake.lua" then
  92. has_xmake = true
  93. elseif filename == "CMakeLists.txt" then
  94. has_cmake = true
  95. elseif filename == "configure" then
  96. has_autoconf = true
  97. elseif filename == "autogen.sh" or filename == "configure.ac" then
  98. need_autogen = true
  99. has_autoconf = true
  100. elseif filename == "meson.build" then
  101. has_meson = true
  102. end
  103. end
  104. os.rm(repodir)
  105. end
  106. -- add dependencies
  107. if has_cmake then
  108. file:print("")
  109. file:print(' add_deps("cmake")')
  110. elseif has_meson then
  111. file:print("")
  112. file:print(' add_deps("meson", "ninja")')
  113. elseif need_autogen then
  114. file:print("")
  115. file:print(' add_deps("autoconf", "automake", "libtool")')
  116. end
  117. -- generate install scripts
  118. file:print("")
  119. file:print(" on_install(function (package)")
  120. file:print(" local configs = {}")
  121. if has_cmake then
  122. file:print(' table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))')
  123. file:print(' table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))')
  124. file:print(' import("package.tools.cmake").install(package, configs)')
  125. elseif has_autoconf then
  126. file:print(' table.insert(configs, "--enable-shared=" .. (package:config("shared") and "yes" or "no"))')
  127. file:print(' if package:debug() then')
  128. file:print(' table.insert(configs, "--enable-debug")')
  129. file:print(' end')
  130. file:print(' if package:is_plat("linux") and package:config("pic") ~= false then')
  131. file:print(' table.insert(configs, "--with-pic")')
  132. file:print(' end')
  133. file:print(' import("package.tools.autoconf").install(package, configs)')
  134. elseif has_meson then
  135. file:print(' table.insert(configs, "-Ddefault_library=" .. (package:config("shared") and "shared" or "static"))')
  136. file:print(' import("package.tools.meson").install(package, configs)')
  137. else
  138. file:print(' io.writefile("xmake.lua", [[')
  139. file:print(' add_rules("mode.release", "mode.debug")')
  140. file:print(' target("%s")', packagename)
  141. file:write(' set_kind("$(kind)")\n')
  142. file:print(' add_files("src/*.c")')
  143. file:print(' ]])')
  144. file:print(' if package:config("shared") then')
  145. file:print(' configs.kind = "shared"')
  146. file:print(' end')
  147. file:print(' import("package.tools.xmake").install(package, configs)')
  148. end
  149. file:print(" end)")
  150. -- generate test scripts
  151. file:print("")
  152. file:print(" on_test(function (package)")
  153. file:print(' assert(package:has_cfuncs("foo", {includes = "foo.h"}))')
  154. file:print(" end)")
  155. file:close()
  156. io.cat(packagefile)
  157. cprint("${bright}%s generated!", packagefile)
  158. end
  159. function main(...)
  160. local opt = option.parse(table.pack(...), options, "New a package.", "",
  161. "Usage: xmake l scripts/new.lua [options]")
  162. local repo = opt.repo
  163. if repo and repo:startswith("github:") then
  164. _generate_package_from_github(repo:sub(8))
  165. else
  166. raise("we need set repository name first!")
  167. end
  168. end