From 8974f94266bab46502bbcbc988d75ff065e7a1c6 Mon Sep 17 00:00:00 2001 From: A2va <49582555+A2va@users.noreply.github.com> Date: Sun, 15 Sep 2024 15:16:17 +0200 Subject: [PATCH 1/4] Install target packages --- .../main/resources/lua/install_artifacts.lua | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/gradle-xmake-plugin/src/main/resources/lua/install_artifacts.lua b/gradle-xmake-plugin/src/main/resources/lua/install_artifacts.lua index 4ff6946..82509d7 100644 --- a/gradle-xmake-plugin/src/main/resources/lua/install_artifacts.lua +++ b/gradle-xmake-plugin/src/main/resources/lua/install_artifacts.lua @@ -27,6 +27,15 @@ function _get_targets(...) return targets end +-- retrieves a value from the specified target, using the given name and scope. +-- unpack the multiple return values into a single table. +function _get_from_target(target, name, scope) + local result, _ = target:get_from(name, scope) + result = result or {} + result = table.join(table.unpack(result)) + return table.wrap(result) +end + -- install artifacts function _install_artifacts(libsdir, installdir, targets, arch) libsdir = path.join(libsdir, arch, "lib") @@ -38,6 +47,15 @@ function _install_artifacts(libsdir, installdir, targets, arch) end for _, target in ipairs(targets) do os.vcp(path.join(libsdir, path.filename(target:targetfile())), installdir) + + -- install target packages + local package_installdirs = _get_from_target(target, "linkdirs", "package::*") + for _, package_installdir in ipairs(installdirs) do + local files = os.files(path.translate(format("%s/*.so", package_installdir))) + for _, file in ipairs(files) do + os.vcp(file, installdir) + end + end end end From 10885fd52d5c095ebc28f62f45291fc7bc84cc2a Mon Sep 17 00:00:00 2001 From: A2va <49582555+A2va@users.noreply.github.com> Date: Mon, 7 Oct 2024 09:52:55 +0200 Subject: [PATCH 2/4] Split install task for each archs --- .../org/tboox/gradle/XMakePlugin.groovy | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/gradle-xmake-plugin/src/main/groovy/org/tboox/gradle/XMakePlugin.groovy b/gradle-xmake-plugin/src/main/groovy/org/tboox/gradle/XMakePlugin.groovy index 177e14a..7fe0854 100644 --- a/gradle-xmake-plugin/src/main/groovy/org/tboox/gradle/XMakePlugin.groovy +++ b/gradle-xmake-plugin/src/main/groovy/org/tboox/gradle/XMakePlugin.groovy @@ -181,14 +181,36 @@ class XMakePlugin implements Plugin { } private registerXMakeInstallTasks(Project project, XMakePluginExtension extension, XMakeLogger logger) { - def installTask = project.tasks.register("xmakeInstall", XMakeInstallTask, new Action() { + for (String name : forNames) { + def installTask = project.tasks.register("xmakeInstallFor" + name, XMakeInstallTask, new Action() { + @Override + void execute(XMakeInstallTask task) { + String forName = task.name.split("For")[1] + task.taskContext = new XMakeTaskContext(extension, project, logger, archMaps[forName]) + } + }) + installTask.configure { Task task -> + String forName = task.name.split("For")[1] + task.dependsOn("xmakeBuildFor" + forName) + } + } + def installTask = project.tasks.register("xmakeInstall", XMakeBuildTask, new Action() { @Override void execute(XMakeInstallTask task) { - task.taskContext = new XMakeTaskContext(extension, project, logger, null) } }) installTask.configure { Task task -> - task.dependsOn("xmakeBuild") + if (projectContext.abiFilters != null) { + for (String filter: projectContext.abiFilters) { + String forName = forNameMaps[filter] + if (forName == null) { + throw new GradleException("invalid abiFilter: " + filter) + } + task.dependsOn("xmakeInstallFor" + forName) + } + } else { + task.dependsOn("xmakeInstallForArmv7") + } } } From 06462b7090d5ba7e88a49f328f76d8110b6ee2e9 Mon Sep 17 00:00:00 2001 From: A2va <49582555+A2va@users.noreply.github.com> Date: Mon, 7 Oct 2024 13:47:21 +0200 Subject: [PATCH 3/4] Use install and uninstall actions in install_artifacts.lua --- .../org/tboox/gradle/XMakeCleanTask.groovy | 48 +++++++++- .../org/tboox/gradle/XMakeInstallTask.groovy | 19 +--- .../main/resources/lua/install_artifacts.lua | 94 ++++++++----------- 3 files changed, 90 insertions(+), 71 deletions(-) diff --git a/gradle-xmake-plugin/src/main/groovy/org/tboox/gradle/XMakeCleanTask.groovy b/gradle-xmake-plugin/src/main/groovy/org/tboox/gradle/XMakeCleanTask.groovy index 9ac0332..abe53d9 100644 --- a/gradle-xmake-plugin/src/main/groovy/org/tboox/gradle/XMakeCleanTask.groovy +++ b/gradle-xmake-plugin/src/main/groovy/org/tboox/gradle/XMakeCleanTask.groovy @@ -61,6 +61,50 @@ class XMakeCleanTask extends DefaultTask { return parameters; } + private void uninstallArtifacts() { + // uninstall artifacts to the native libs directory + File installArtifactsScriptFile = new File(taskContext.buildDirectory, "install_artifacts.lua") + installArtifactsScriptFile.withWriter { out -> + String text = getClass().getClassLoader().getResourceAsStream("lua/install_artifacts.lua").getText() + out.write(text) + } + + List parameters = new ArrayList<>(); + parameters.add(taskContext.program) + parameters.add("lua"); + switch (taskContext.logLevel) { + case "verbose": + parameters.add("-v") + break + case "debug": + parameters.add("-vD") + break + default: + break + } + parameters.add(installArtifactsScriptFile.absolutePath) + + // pass app/libs directory + parameters.add(taskContext.nativeLibsDir.absolutePath) + + // pass arch + parameters.add(taskContext.buildArch) + // pass clean + parameters.add("true") + + // pass targets + Set targets = taskContext.targets + if (targets != null && targets.size() > 0) { + for (String target: targets) { + parameters.add(target) + } + } + + // do uninstall + XMakeExecutor executor = new XMakeExecutor(taskContext.logger) + executor.exec(parameters, taskContext.projectDirectory) + } + @TaskAction void clean() { @@ -74,8 +118,10 @@ class XMakeCleanTask extends DefaultTask { throw new GradleException(TAG + taskContext.projectFile.absolutePath + " not found!") } - // do build + // do clean XMakeExecutor executor = new XMakeExecutor(taskContext.logger) executor.exec(buildCmdLine(), taskContext.projectDirectory) + + uninstallArtifacts() } } diff --git a/gradle-xmake-plugin/src/main/groovy/org/tboox/gradle/XMakeInstallTask.groovy b/gradle-xmake-plugin/src/main/groovy/org/tboox/gradle/XMakeInstallTask.groovy index 6e3b0e4..3312110 100644 --- a/gradle-xmake-plugin/src/main/groovy/org/tboox/gradle/XMakeInstallTask.groovy +++ b/gradle-xmake-plugin/src/main/groovy/org/tboox/gradle/XMakeInstallTask.groovy @@ -55,24 +55,13 @@ class XMakeInstallTask extends DefaultTask { } parameters.add(installArtifactsScriptFile.absolutePath) - // pass build/libs directory - parameters.add(new File(taskContext.buildDirectory, "libs").path) - // pass app/libs directory parameters.add(taskContext.nativeLibsDir.absolutePath) - // pass abiFilters - int i = 0 - StringBuilder abiFiltersList = new StringBuilder("") - Set abiFilters = taskContext.abiFilters - for (String filter: abiFilters) { - if (i > 0) { - abiFiltersList.append(",") - } - abiFiltersList.append(filter) - i++ - } - parameters.add(abiFiltersList.toString()) + // pass arch + parameters.add(taskContext.buildArch) + // pass clean + parameters.add("false") // pass targets Set targets = taskContext.targets diff --git a/gradle-xmake-plugin/src/main/resources/lua/install_artifacts.lua b/gradle-xmake-plugin/src/main/resources/lua/install_artifacts.lua index 82509d7..7f72cf8 100644 --- a/gradle-xmake-plugin/src/main/resources/lua/install_artifacts.lua +++ b/gradle-xmake-plugin/src/main/resources/lua/install_artifacts.lua @@ -2,6 +2,8 @@ import("core.project.config") import("core.project.project") import("core.tool.toolchain") +import("target.action.install") +import("target.action.uninstall") -- get targets function _get_targets(...) @@ -27,35 +29,11 @@ function _get_targets(...) return targets end --- retrieves a value from the specified target, using the given name and scope. --- unpack the multiple return values into a single table. -function _get_from_target(target, name, scope) - local result, _ = target:get_from(name, scope) - result = result or {} - result = table.join(table.unpack(result)) - return table.wrap(result) -end - -- install artifacts -function _install_artifacts(libsdir, installdir, targets, arch) - libsdir = path.join(libsdir, arch, "lib") - installdir = path.join(installdir, arch) - - -- install targets - if not os.isdir(installdir) then - os.mkdir(installdir) - end +function _install_artifacts(installdir, targets, arch) + assert(xmake.version():satisfies(">= 2.9.5"), "please update xmake to >= 2.9.5") for _, target in ipairs(targets) do - os.vcp(path.join(libsdir, path.filename(target:targetfile())), installdir) - - -- install target packages - local package_installdirs = _get_from_target(target, "linkdirs", "package::*") - for _, package_installdir in ipairs(installdirs) do - local files = os.files(path.translate(format("%s/*.so", package_installdir))) - for _, file in ipairs(files) do - os.vcp(file, installdir) - end - end + install(target, {installdir = installdir, libdir = arch}) end end @@ -161,49 +139,55 @@ end -- clean artifacts function _clean_artifacts(installdir, targets, arch) + assert(xmake.version():satisfies(">= 2.9.5"), "please update xmake to >= 2.9.5") + for _, target in ipairs(targets) do + uninstall(target, {installdir = installdir, libdir = arch}) + end - -- append arch sub-directory - installdir = path.join(installdir, arch) + local installdir = path.join(installdir, arch) - -- clean targets artifacts in the install directory - for _, target in ipairs(targets) do - os.tryrm(path.join(installdir, path.filename(target:targetfile()))) - if os.emptydir(installdir) then - os.tryrm(installdir) + -- clean cxxstl + local ndk_cxxstl = get_config("ndk_cxxstl") + if ndk_cxxstl then + local cxxstl_filename + if ndk_cxxstl == "c++_shared" then + cxxstl_filename = "libc++_shared.so" + elseif ndk_cxxstl == "gnustl_shared" then + cxxstl_filename = "libgnustl_shared.so" + elseif ndk_cxxstl == "stlport_shared" then + cxxstl_filename = "libstlport_shared.so" end + os.tryrm(installdir) + end + + if os.emptydir(installdir) then + os.rmdir(installdir) end end -- main entry -function main(libsdir, installdir, archs, ...) +function main(installdir, arch, clean, ...) -- check - assert(libsdir and installdir and archs) + assert(installdir and clean) -- load config config.load() - -- get abi filters - local abi_filters = {} - for _, arch in ipairs(archs:split(',', {plain = true})) do - abi_filters[arch] = true - end - -- do install or clean local targets = _get_targets(...) - if targets and #targets > 0 then - for _, arch in ipairs({"armeabi", "armeabi-v7a", "arm64-v8a", "x86", "x86_64"}) do - if abi_filters[arch] then - _install_artifacts(libsdir, installdir, targets, arch) - - if get_config("ndkver") >= 25 then - _install_cxxstl_newer_ndk(installdir, arch) - else - _install_cxxstl(installdir, arch) - end - else - _clean_artifacts(installdir, targets, arch) - end + assert(not targets or #targets == 0, "no targets provided, make sure to have at least one shared target in your xmake.lua or to provide one") + + if clean == "false" then + _install_artifacts(libsdir, installdir, targets, arch) + + if get_config("ndkver") >= 25 then + _install_cxxstl_newer_ndk(installdir, arch) + else + _install_cxxstl(installdir, arch) end + else + _clean_artifacts(installdir, targets, arch) end + end From 6d3c618e3bd7374934a4f01e42c73d5c2cd1e4c7 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 9 Oct 2024 10:00:53 +0800 Subject: [PATCH 4/4] Update install_artifacts.lua --- .../src/main/resources/lua/install_artifacts.lua | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/gradle-xmake-plugin/src/main/resources/lua/install_artifacts.lua b/gradle-xmake-plugin/src/main/resources/lua/install_artifacts.lua index 7f72cf8..8d876d5 100644 --- a/gradle-xmake-plugin/src/main/resources/lua/install_artifacts.lua +++ b/gradle-xmake-plugin/src/main/resources/lua/install_artifacts.lua @@ -42,7 +42,7 @@ function _install_cxxstl_newer_ndk(installdir, arch) installdir = path.join(installdir, arch) local ndk = get_config("ndk") - local ndk_cxxstl = get_config("ndk_cxxstl") + local ndk_cxxstl = get_config("runtimes") or get_config("ndk_cxxstl") if ndk and ndk_cxxstl and ndk_cxxstl:endswith("_shared") and arch then -- get the toolchains arch @@ -78,8 +78,7 @@ function _install_cxxstl(installdir, arch) -- install stl shared library local ndk = get_config("ndk") - local ndk_cxxstl = get_config("ndk_cxxstl") - arch = arch or get_config("arch") + local ndk_cxxstl = get_config("runtimes") or get_config("ndk_cxxstl") if ndk and ndk_cxxstl and ndk_cxxstl:endswith("_shared") and arch then -- get llvm c++ stl sdk directory @@ -165,10 +164,7 @@ function _clean_artifacts(installdir, targets, arch) end end --- main entry function main(installdir, arch, clean, ...) - - -- check assert(installdir and clean) -- load config @@ -177,7 +173,8 @@ function main(installdir, arch, clean, ...) -- do install or clean local targets = _get_targets(...) assert(not targets or #targets == 0, "no targets provided, make sure to have at least one shared target in your xmake.lua or to provide one") - + + arch = arch or get_config("arch") if clean == "false" then _install_artifacts(libsdir, installdir, targets, arch)