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/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") + } } } 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 d749124..667f290 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(...) @@ -28,16 +30,10 @@ function _get_targets(...) 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, {installdir = installdir, libdir = arch}) end end @@ -83,7 +79,6 @@ function _install_cxxstl(installdir, arch) -- install stl shared library local ndk = get_config("ndk") local ndk_cxxstl = get_config("runtimes") or get_config("ndk_cxxstl") - arch = arch or get_config("arch") if ndk and ndk_cxxstl and ndk_cxxstl:endswith("_shared") and arch then -- get llvm c++ stl sdk directory @@ -143,50 +138,54 @@ 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 -end --- main entry -function main(libsdir, installdir, archs, ...) + if os.emptydir(installdir) then + os.rmdir(installdir) + end +end - -- check - assert(libsdir and installdir and archs) +function main(installdir, arch, clean, ...) + 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") + + arch = arch or get_config("arch") + 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