package An official xmake package repository https://xrepo.xmake.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
7.0 KiB

3 years ago
import("core.base.option")
import("core.base.semver")
import("core.base.json")
import("lib.detect.find_tool")
3 years ago
import("lib.detect.find_file")
3 years ago
import("net.http")
3 years ago
import("devel.git")
3 years ago
import("utils.archive")
3 years ago
local options = {
{nil, "repo", "v", nil, "Set repository name.",
"e.g. ",
" - github:xmake-io/xmake",
" - brew:zlib"}
}
function _generate_package_from_github(reponame)
-- get repository info
local gh = assert(find_tool("gh"), "gh not found!")
local repoinfo = os.iorunv(gh.program, {"repo", "view", reponame, "--json",
"description,homepageUrl,licenseInfo,url,sshUrl,name,latestRelease"})
if repoinfo then
repoinfo = json.decode(repoinfo)
end
3 years ago
vprint(repoinfo)
3 years ago
-- generate package header
local packagename = assert(repoinfo.name, "package name not found!"):lower()
local packagefile = path.join("packages", packagename:sub(1, 1), packagename, "xmake.lua")
local file = io.open(packagefile, "w")
file:print('package("%s")', packagename)
3 years ago
local homepage = repoinfo.homepageUrl
if homepage == nil or homepage == "" then
homepage = repoinfo.url
end
3 years ago
if homepage then
file:print(' set_homepage("%s")', homepage)
end
local description = repoinfo.description or ("The " .. packagename .. " package")
file:print(' set_description("%s")', description)
3 years ago
local licensekey = type(repoinfo.licenseInfo) == "table" and repoinfo.licenseInfo.key
3 years ago
if licensekey then
local licenses = {
3 years ago
["apache-2.0"] = "Apache-2.0",
["lgpl-2.0"] = "LGPL-2.0",
["lgpl-2.1"] = "LGPL-2.1",
zlib = "zlib",
mit = "MIT"
3 years ago
}
local license = licenses[licensekey]
if license then
file:print(' set_license("%s")', license)
end
end
file:print("")
-- generate package urls and versions
3 years ago
local repodir
3 years ago
local has_xmake
local has_cmake
local has_meson
local has_autoconf
3 years ago
local need_autogen
3 years ago
local latest_release = repoinfo.latestRelease
3 years ago
if type(latest_release) == "table" then
3 years ago
local url = ("https://github.com/%s/archive/refs/tags/%s.tar.gz"):format(reponame, latest_release.tagName)
local giturl = ("https://github.com/%s.git"):format(reponame)
3 years ago
file:write(' add_urls("https://github.com/' .. reponame .. '/archive/refs/tags/$(version).tar.gz",\n')
3 years ago
file:print(' "%s")', giturl)
3 years ago
local tmpfile = os.tmpfile({ramdisk = false}) .. ".tar.gz"
3 years ago
repodir = tmpfile .. ".dir"
3 years ago
print("downloading %s", url)
http.download(url, tmpfile)
file:print(' add_versions("%s", "%s")', latest_release.tagName, hash.sha256(tmpfile))
3 years ago
archive.extract(tmpfile, repodir)
os.rm(tmpfile)
else
local giturl = ("https://github.com/%s.git"):format(reponame)
repodir = os.tmpfile({ramdisk = false})
file:print(' add_urls("%s")', giturl)
print("downloading %s", giturl)
git.clone(giturl, {outputdir = repodir, depth = 1})
local commit = git.lastcommit({repodir = repodir})
local version = try{ function() return os.iorunv("git", {"log", "-1", "--date=format:%Y.%m.%d", "--format=%ad"}, {curdir = repodir}) end}
if version then
file:print(' add_versions("%s", "%s")', version:trim(), commit)
end
end
-- detect build system
if repodir then
local files = os.files(path.join(repodir, "*")) or {}
table.join2(files, os.files(path.join(repodir, "*", "*")))
3 years ago
for _, file in ipairs(files) do
local filename = path.filename(file)
if filename == "xmake.lua" then
has_xmake = true
elseif filename == "CMakeLists.txt" then
has_cmake = true
3 years ago
elseif filename == "configure" then
has_autoconf = true
elseif filename == "autogen.sh" then
need_autogen = true
3 years ago
has_autoconf = true
elseif filename == "meson.build" then
has_meson = true
3 years ago
end
end
3 years ago
os.rm(repodir)
3 years ago
end
3 years ago
-- add dependencies
if has_cmake then
file:print("")
file:print(' add_deps("cmake")')
elseif has_meson then
file:print("")
file:print(' add_deps("meson", "ninja")')
elseif need_autogen then
file:print("")
file:print(' add_deps("autoconf", "automake")')
end
3 years ago
-- generate install scripts
file:print("")
file:print(" on_install(function (package)")
file:print(" local configs = {}")
3 years ago
if has_cmake then
3 years ago
file:print(' table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))')
file:print(' table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))')
file:print(' import("package.tools.cmake").install(package, configs)')
3 years ago
elseif has_autoconf then
3 years ago
file:print(' table.insert(configs, "--enable-shared=" .. (package:config("shared") and "yes" or "no"))')
file:print(' if package:debug() then')
file:print(' table.insert(configs, "--enable-debug")')
file:print(' end')
file:print(' if package:is_plat("linux") and package:config("pic") ~= false then')
file:print(' table.insert(configs, "--with-pic")')
file:print(' end')
3 years ago
elseif has_meson then
file:print(' table.insert(configs, "-Ddefault_library=" .. (package:config("shared") and "shared" or "static"))')
file:print(' import("package.tools.meson").install(package, configs)')
3 years ago
else
file:print(' io.writefile("xmake.lua", [[')
file:print(' add_rules("mode.release", "mode.debug")')
file:print(' target("%s")', packagename)
file:write(' set_kind("$(kind)")\n')
file:print(' add_files("src/*.c")')
file:print(' ]])')
file:print(' if package:config("shared") then')
file:print(' configs.kind = "shared"')
file:print(' end')
file:print(' import("package.tools.xmake").install(package, configs)')
end
3 years ago
file:print(" end)")
-- generate test scripts
file:print("")
file:print(" on_test(function (package)")
file:print(' assert(package:has_cfuncs("foo", {includes = "foo.h"}))')
file:print(" end)")
file:close()
io.cat(packagefile)
cprint("${bright}%s generated!", packagefile)
end
function main(...)
local opt = option.parse(table.pack(...), options, "New a package.", "",
"Usage: xmake l scripts/new.lua [options]")
local repo = opt.repo
if repo and repo:startswith("github:") then
_generate_package_from_github(repo:sub(8))
else
raise("we need set repository name first!")
end
end