summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-04-02 22:49:44 +0800
committerruki <[email protected]>2024-04-02 22:49:44 +0800
commitf0cb5ad5d4aa420151f87ad497656bc68be182da (patch)
tree4180f030d8245945a5802bdeec247d25e5ef138a
parent6953ca0c43ff4cb08fc9f76eabb5415a892e5167 (diff)
call binary module
-rw-r--r--tests/projects/other/native_module/modules/binary/bar/src/add.cpp2
-rw-r--r--tests/projects/other/native_module/modules/binary/bar/src/sub.cpp2
-rw-r--r--tests/projects/other/native_module/xmake.lua4
-rw-r--r--xmake/core/sandbox/modules/import/core/sandbox/module.lua50
-rw-r--r--xmake/rules/module/xmake.lua2
5 files changed, 53 insertions, 7 deletions
diff --git a/tests/projects/other/native_module/modules/binary/bar/src/add.cpp b/tests/projects/other/native_module/modules/binary/bar/src/add.cpp
index 87ef97e6c..e19d07e12 100644
--- a/tests/projects/other/native_module/modules/binary/bar/src/add.cpp
+++ b/tests/projects/other/native_module/modules/binary/bar/src/add.cpp
@@ -4,6 +4,6 @@
int main(int argc, char** argv) {
int a = atoi(argv[1]);
int b = atoi(argv[2]);
- printf("%s\n", a + b);
+ printf("%d", a + b);
return 0;
}
diff --git a/tests/projects/other/native_module/modules/binary/bar/src/sub.cpp b/tests/projects/other/native_module/modules/binary/bar/src/sub.cpp
index 524930500..980289d1a 100644
--- a/tests/projects/other/native_module/modules/binary/bar/src/sub.cpp
+++ b/tests/projects/other/native_module/modules/binary/bar/src/sub.cpp
@@ -4,7 +4,7 @@
int main(int argc, char** argv) {
int a = atoi(argv[1]);
int b = atoi(argv[2]);
- printf("%s\n", a - b);
+ printf("%d", a - b);
return 0;
}
diff --git a/tests/projects/other/native_module/xmake.lua b/tests/projects/other/native_module/xmake.lua
index 44b310649..e5564cf4f 100644
--- a/tests/projects/other/native_module/xmake.lua
+++ b/tests/projects/other/native_module/xmake.lua
@@ -10,7 +10,7 @@ target("test")
import("binary.bar")
-- print("shared: 1 + 1 = %d", foo.add(1, 1))
-- print("shared: 1 - 1 = %d", foo.sub(1, 1))
- print("binary: 1 + 1 = %s", bar.add("1", "1"))
- print("binary: 1 - 1 = %s", bar.sub("1", "1"))
+ print("binary: 1 + 1 = %s", bar.add(1, 1))
+ print("binary: 1 - 1 = %s", bar.sub(1, 1))
end)
diff --git a/xmake/core/sandbox/modules/import/core/sandbox/module.lua b/xmake/core/sandbox/modules/import/core/sandbox/module.lua
index c1ee0d902..939478150 100644
--- a/xmake/core/sandbox/modules/import/core/sandbox/module.lua
+++ b/xmake/core/sandbox/modules/import/core/sandbox/module.lua
@@ -24,17 +24,19 @@ local core_sandbox_module = core_sandbox_module or {}
-- load modules
local os = require("base/os")
local path = require("base/path")
+local hash = require("base/hash")
local utils = require("base/utils")
local table = require("base/table")
local string = require("base/string")
local global = require("base/global")
+local config = require("project/config")
local memcache = require("cache/memcache")
local sandbox = require("sandbox/sandbox")
local raise = require("sandbox/modules/raise")
-- the module kinds
local MODULE_KIND_LUAFILE = 1
-local MODULE_KIND_LUADIR = 2
+local MODULE_KIND_LUADIR = 2
local MODULE_KIND_BINARY = 3
local MODULE_KIND_SHARED = 4
@@ -118,7 +120,7 @@ function core_sandbox_module._find(dir, name)
local module_projectfile = path.join(module_fullpath, "xmake.lua")
if os.isfile(module_projectfile) then -- native module? e.g. binary/shared modules
local content = io.readfile(module_projectfile)
- local kind = content:match("set_kind%(\"(.-)\"%)")
+ local kind = content:match("add_rules%(\"module.(.-)\"%)")
if kind == "binary" then
return modulekey, MODULE_KIND_BINARY
elseif kind == "shared" then
@@ -141,7 +143,7 @@ end
function core_sandbox_module._load_from_scriptdir(module_fullpath, opt)
local script
local module = opt.module
- local modulefiles = os.match(path.join(module_fullpath, "**.lua"))
+ local modulefiles = os.files(path.join(module_fullpath, "**.lua"))
if modulefiles then
for _, modulefile in ipairs(modulefiles) do
local result, errors = core_sandbox_module._loadfile(modulefile, opt.instance)
@@ -181,12 +183,54 @@ function core_sandbox_module._load_from_scriptdir(module_fullpath, opt)
return module, script
end
+-- build module
+function core_sandbox_module._build_module(module_fullpath)
+ local projectdir = path.normalize(path.absolute(module_fullpath))
+ local programdir = path.normalize(os.programdir())
+ local modulehash = hash.uuid4(projectdir):split("-", {plain = true})[1]:lower()
+ local buildir
+ if projectdir:startswith(programdir) then
+ buildir = path.join(global.directory(), "cache", "modules", modulehash)
+ elseif os.isfile(os.projectfile()) then
+ buildir = path.join(config.directory(), "cache", "modules", modulehash)
+ else
+ buildir = path.join(projectdir, "cache", "modules", modulehash)
+ end
+ local envs = {XMAKE_CONFIGDIR = buildir}
+ local argv = {"config", "-P", projectdir, "-o", buildir}
+ os.execv(os.programfile(), argv, {envs = envs, curdir = projectdir})
+ argv = {"-P", projectdir}
+ os.execv(os.programfile(), argv, {envs = envs, curdir = projectdir})
+ return buildir
+end
+
-- load module from the binary module
function core_sandbox_module._load_from_binary(module_fullpath, opt)
+ local module
+ local module_bindir = core_sandbox_module._build_module(module_fullpath)
+ local binaryfiles = os.files(path.join(module_bindir, "module_*"))
+ if binaryfiles then
+ module = {}
+ for _, binaryfile in ipairs(binaryfiles) do
+ local modulename = path.basename(binaryfile):sub(8)
+ module[modulename] = function (...)
+ local argv = {}
+ for _, arg in ipairs(table.pack(...)) do
+ table.insert(argv, tostring(arg))
+ end
+ local ok, outdata, errdata = os.iorunv(binaryfile, argv, {curdir = module_bindir})
+ if ok then
+ return outdata
+ end
+ end
+ end
+ end
+ return module
end
-- load module from the shared module
function core_sandbox_module._load_from_shared(module_fullpath, opt)
+ core_sandbox_module._build_module(module_fullpath)
end
-- load module
diff --git a/xmake/rules/module/xmake.lua b/xmake/rules/module/xmake.lua
index a3bc9b54f..dd2f810fe 100644
--- a/xmake/rules/module/xmake.lua
+++ b/xmake/rules/module/xmake.lua
@@ -22,6 +22,7 @@ rule("module.binary")
on_load(function (target)
import("core.project.config")
target:set("kind", "binary")
+ target:set("basename", "module_" .. target:name())
target:set("targetdir", config.buildir())
end)
@@ -29,6 +30,7 @@ rule("module.shared")
on_load(function (target)
import("core.project.config")
target:set("kind", "shared")
+ target:set("basename", "module_" .. target:name())
target:set("targetdir", config.buildir())
end)