Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install target packages #14

Merged
merged 5 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XMakeBuildTask -> XMakeInstallTask

it will break, did you test it?

@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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ndk_cxxstl has been deprecated, please use runtimes.

    local ndk_cxxstl = get_config("runtimes") or 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, ...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then xmake l install_artfacts.lua --installdir=xxx --arch=xxx --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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here it's always false.


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