Skip to content

Commit

Permalink
Merge pull request #14 from A2va/install-packages
Browse files Browse the repository at this point in the history
Install target packages
  • Loading branch information
waruqi authored Oct 9, 2024
2 parents 3024106 + 12f72b7 commit b70538c
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> 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() {

Expand All @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> targets = taskContext.targets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,36 @@ class XMakePlugin implements Plugin<Project> {
}

private registerXMakeInstallTasks(Project project, XMakePluginExtension extension, XMakeLogger logger) {
def installTask = project.tasks.register("xmakeInstall", XMakeInstallTask, new Action<XMakeInstallTask>() {
for (String name : forNames) {
def installTask = project.tasks.register("xmakeInstallFor" + name, XMakeInstallTask, new Action<XMakeInstallTask>() {
@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<XMakeInstallTask>() {
@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")
}
}
}

Expand Down
81 changes: 40 additions & 41 deletions gradle-xmake-plugin/src/main/resources/lua/install_artifacts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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(...)
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit b70538c

Please sign in to comment.