summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-04-23 22:41:05 +0800
committerruki <[email protected]>2020-04-23 10:06:32 +0800
commitd41e6aeb8aa516742895d64f36025db50e8f857f (patch)
tree6e791fa39be295ccfa59503a372a9a7e6f83c5e4
parentceff124c76b8968b0009066c6fcf03a6f31fb180 (diff)
improve programdir
-rw-r--r--core/src/xmake/engine.c12
-rw-r--r--xmake/core/_xmake_main.lua2
-rw-r--r--xmake/core/main.lua4
-rw-r--r--xmake/rules/xmake_cli/xmake.lua40
4 files changed, 55 insertions, 3 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index 14dc27ed4..a39eb4ac5 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -50,6 +50,9 @@ typedef struct __xm_engine_t
// the lua
lua_State* lua;
+ // the engine name
+ tb_char_t name[64];
+
}xm_engine_t;
/* //////////////////////////////////////////////////////////////////////////////////////
@@ -528,11 +531,15 @@ static tb_bool_t xm_engine_get_program_directory(xm_engine_t* engine, tb_char_t*
tb_char_t const* rootdir = tb_path_directory(programpath, data, sizeof(data));
tb_assert_and_check_break(rootdir);
+ // init share/name sub-directory
+ tb_char_t sharedir[128];
+ tb_snprintf(sharedir, sizeof(sharedir), "../share/%s", engine->name);
+
// find the program (lua) directory
tb_size_t i;
tb_file_info_t info;
tb_char_t scriptpath[TB_PATH_MAXN];
- tb_char_t const* subdirs[] = {"", "../share/xmake"};
+ tb_char_t const* subdirs[] = {"", sharedir};
for (i = 0; i < tb_arrayn(subdirs); i++)
{
// get program directory
@@ -754,6 +761,9 @@ xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_c
engine = tb_malloc0_type(xm_engine_t);
tb_assert_and_check_break(engine);
+ // init name
+ tb_strlcpy(engine->name, name, sizeof(engine->name));
+
// init lua
engine->lua = lua_open();
tb_assert_and_check_break(engine->lua);
diff --git a/xmake/core/_xmake_main.lua b/xmake/core/_xmake_main.lua
index 883e71f4c..cdb3cc1de 100644
--- a/xmake/core/_xmake_main.lua
+++ b/xmake/core/_xmake_main.lua
@@ -20,7 +20,7 @@
-- init namespace: xmake
xmake = xmake or {}
-xmake._NAME = _NAME
+xmake._NAME = _NAME or "xmake"
xmake._ARGV = _ARGV
xmake._HOST = _HOST
xmake._ARCH = _ARCH
diff --git a/xmake/core/main.lua b/xmake/core/main.lua
index 5da559fc1..11dd59ec8 100644
--- a/xmake/core/main.lua
+++ b/xmake/core/main.lua
@@ -179,6 +179,10 @@ function main._init()
if os.isdir(os.projectdir()) then
os.cd(os.projectdir())
end
+ else
+ -- patch a fake project file and directory for other lua programs with xmake/engine
+ xmake._PROJECT_DIR = path.join(os.tmpdir(), "local")
+ xmake._PROJECT_FILE = path.join(xmake._PROJECT_DIR, xmake._NAME .. ".lua")
end
-- add the directory of the program file (xmake) to $PATH environment
diff --git a/xmake/rules/xmake_cli/xmake.lua b/xmake/rules/xmake_cli/xmake.lua
index a8d891ab1..455a67365 100644
--- a/xmake/rules/xmake_cli/xmake.lua
+++ b/xmake/rules/xmake_cli/xmake.lua
@@ -24,10 +24,48 @@ rule("xmake.cli")
target:set("kind", "binary")
assert(target:pkg("libxmake"), 'please add_packages("libxmake") to target(%s) first!', target:name())
end)
+ after_install(function (target)
+ -- install xmake/cli program
+ --
+ -- - bin
+ -- - hello
+ -- - share
+ -- - hello
+ -- - lua
+ -- - main.lua
+ -- - core
+ -- - ..
+ --
+ local scriptdir = path.join(target:scriptdir(), "src")
+ if not os.isfile(path.join(scriptdir, "lua", "main.lua")) then
+ import("lib.detect.find_path")
+ scriptdir = assert(find_path("lua/main.lua", path.join(target:scriptdir(), "**")), "lua/main.lua not found!")
+ end
+ local installdir = path.join(target:installdir(), "share", target:name())
+ if not os.isdir(installdir) then
+ os.mkdir(installdir)
+ end
+ os.vcp(path.join(scriptdir, "lua"), installdir)
+
+ -- install xmake-core lua scripts
+ local libxmake = target:pkg("libxmake")
+ local programdir = path.join(path.directory(libxmake:get("linkdirs")), "share", "xmake")
+ assert(os.isdir(programdir), "%s not found!", programdir)
+ local coredir = path.join(installdir, "core")
+ if not os.isdir(coredir) then
+ os.mkdir(coredir)
+ end
+ os.vcp(path.join(programdir, "*"), coredir)
+ end)
before_run(function (target)
+ local scriptdir = path.join(target:scriptdir(), "src")
+ if not os.isfile(path.join(scriptdir, "lua", "main.lua")) then
+ import("lib.detect.find_path")
+ scriptdir = assert(find_path("lua/main.lua", path.join(target:scriptdir(), "**")), "lua/main.lua not found!")
+ end
local libxmake = target:pkg("libxmake")
local programdir = path.join(path.directory(libxmake:get("linkdirs")), "share", "xmake")
assert(os.isdir(programdir), "%s not found!", programdir)
os.setenv("XMAKE_PROGRAM_DIR", programdir)
- os.setenv("XMAKE_MODULES_DIR", path.join(target:scriptdir(), "src"))
+ os.setenv("XMAKE_MODULES_DIR", scriptdir)
end)