summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-05-24 14:32:08 +0800
committerruki <[email protected]>2017-05-24 14:32:08 +0800
commit7ef69739513b4b4431f6f683fe2a9d297658be02 (patch)
tree14fd2789a956cbcb79e1ea217541bfc6e75bd99e
parent4e52205dc696ade5ba01c8d6ccf9831b59ad6196 (diff)
remove xmake variable
-rw-r--r--core/src/xmake/machine.c119
-rw-r--r--xmake/core/_xmake_main.lua1
-rw-r--r--xmake/core/base/os.lua14
-rw-r--r--xmake/core/main.lua8
-rw-r--r--xmake/core/project/project.lua3
-rw-r--r--xmake/core/project/task.lua3
-rw-r--r--xmake/core/sandbox/modules/os.lua33
7 files changed, 105 insertions, 76 deletions
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index ccfd91254..6baf7f214 100644
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -206,7 +206,7 @@ static luaL_Reg const g_readline_functions[] =
/* //////////////////////////////////////////////////////////////////////////////////////
* private implementation
*/
-static tb_bool_t xm_machine_main_save_arguments(xm_machine_impl_t* impl, tb_int_t argc, tb_char_t** argv)
+static tb_bool_t xm_machine_save_arguments(xm_machine_impl_t* impl, tb_int_t argc, tb_char_t** argv)
{
// check
tb_assert_and_check_return_val(impl && impl->lua && argc >= 1 && argv, tb_false);
@@ -229,7 +229,7 @@ static tb_bool_t xm_machine_main_save_arguments(xm_machine_impl_t* impl, tb_int_
// ok
return tb_true;
}
-static tb_bool_t xm_machine_main_get_program_directory(xm_machine_impl_t* impl, tb_char_t* path, tb_size_t maxn)
+static tb_size_t xm_machine_get_program_file(xm_machine_impl_t* impl, tb_char_t* path, tb_size_t maxn)
{
// check
tb_assert_and_check_return_val(impl && path && maxn, tb_false);
@@ -238,15 +238,6 @@ static tb_bool_t xm_machine_main_get_program_directory(xm_machine_impl_t* impl,
tb_bool_t ok = tb_false;
do
{
- // get it from the environment variable first
- tb_char_t data[TB_PATH_MAXN] = {0};
- if (tb_environment_first("XMAKE_PROGRAM_DIR", data, sizeof(data)) && tb_path_absolute(data, path, maxn))
- {
- // ok
- ok = tb_true;
- break;
- }
-
#if defined(TB_CONFIG_OS_WINDOWS)
// get the executale file path as program directory
tb_size_t size = (tb_size_t)GetModuleFileName(tb_null, path, (DWORD)maxn);
@@ -255,18 +246,9 @@ static tb_bool_t xm_machine_main_get_program_directory(xm_machine_impl_t* impl,
// end
path[size] = '\0';
- // get the directory
- while (size-- > 0)
- {
- if (path[size] == '\\')
- {
- path[size] = '\0';
- break;
- }
- }
-
// ok
ok = tb_true;
+
#elif defined(TB_CONFIG_OS_MACOSX)
/*
* _NSGetExecutablePath() copies the path of the main executable into the buffer. The bufsize parameter
@@ -278,25 +260,9 @@ static tb_bool_t xm_machine_main_get_program_directory(xm_machine_impl_t* impl,
* That is the path may be a symbolic link and not the real file. With deep directories the total bufsize
* needed could be more than MAXPATHLEN.
*/
- tb_uint32_t size = (tb_uint32_t)maxn;
- if (!_NSGetExecutablePath(path, &size))
- {
- // get path size
- size = tb_strlen(path);
-
- // get the directory
- while (size-- > 0)
- {
- if (path[size] == '/')
- {
- path[size] = '\0';
- break;
- }
- }
-
- // ok
+ tb_uint32_t bufsize = (tb_uint32_t)maxn;
+ if (!_NSGetExecutablePath(path, &bufsize))
ok = tb_true;
- }
#elif defined(TB_CONFIG_OS_LINUX)
// get the executale file path as program directory
ssize_t size = readlink("/proc/self/exe", path, (size_t)maxn);
@@ -305,20 +271,64 @@ static tb_bool_t xm_machine_main_get_program_directory(xm_machine_impl_t* impl,
// end
path[size] = '\0';
- // get the directory
- while (size-- > 0)
+ // ok
+ ok = tb_true;
+ }
+#endif
+
+ } while (0);
+
+ // ok?
+ if (ok)
+ {
+ // trace
+ tb_trace_d("programfile: %s", path);
+
+ // save the directory to the global variable: _PROGRAM_FILE
+ lua_pushstring(impl->lua, path);
+ lua_setglobal(impl->lua, "_PROGRAM_FILE");
+ }
+
+ // ok?
+ return ok;
+}
+static tb_bool_t xm_machine_get_program_directory(xm_machine_impl_t* impl, tb_char_t* path, tb_size_t maxn, tb_char_t const* programfile)
+{
+ // check
+ tb_assert_and_check_return_val(impl && path && maxn, tb_false);
+
+ // done
+ tb_bool_t ok = tb_false;
+ do
+ {
+ // get it from the environment variable first
+ tb_char_t data[TB_PATH_MAXN] = {0};
+ if (tb_environment_first("XMAKE_PROGRAM_DIR", data, sizeof(data)) && tb_path_absolute(data, path, maxn))
+ {
+ // ok
+ ok = tb_true;
+ break;
+ }
+
+ // get it from program file path
+ if (programfile)
+ {
+ tb_size_t size = tb_strlcpy(data, programfile, sizeof(data));
+ if (size < sizeof(data))
{
- if (path[size] == '/')
+ // get the directory
+ while (size-- > 0)
{
- path[size] = '\0';
- break;
+ if (data[size] == '\\' || data[size] == '/')
+ {
+ data[size] = '\0';
+ tb_strlcpy(path, data, maxn);
+ ok = tb_true;
+ break;
+ }
}
}
-
- // ok
- ok = tb_true;
}
-#endif
} while (0);
@@ -326,7 +336,7 @@ static tb_bool_t xm_machine_main_get_program_directory(xm_machine_impl_t* impl,
if (ok)
{
// trace
- tb_trace_d("program: %s", path);
+ tb_trace_d("programdir: %s", path);
// save the directory to the global variable: _PROGRAM_DIR
lua_pushstring(impl->lua, path);
@@ -336,7 +346,7 @@ static tb_bool_t xm_machine_main_get_program_directory(xm_machine_impl_t* impl,
// ok?
return ok;
}
-static tb_bool_t xm_machine_main_get_project_directory(xm_machine_impl_t* impl, tb_char_t* path, tb_size_t maxn)
+static tb_bool_t xm_machine_get_project_directory(xm_machine_impl_t* impl, tb_char_t* path, tb_size_t maxn)
{
// check
tb_assert_and_check_return_val(impl && path && maxn, tb_false);
@@ -504,14 +514,17 @@ tb_int_t xm_machine_main(xm_machine_ref_t machine, tb_int_t argc, tb_char_t** ar
tb_assert_and_check_return_val(impl && impl->lua, -1);
// save main arguments to the global variable: _ARGV
- if (!xm_machine_main_save_arguments(impl, argc, argv)) return -1;
+ if (!xm_machine_save_arguments(impl, argc, argv)) return -1;
// get the project directory
tb_char_t path[TB_PATH_MAXN] = {0};
- if (!xm_machine_main_get_project_directory(impl, path, sizeof(path))) return -1;
+ if (!xm_machine_get_project_directory(impl, path, sizeof(path))) return -1;
+
+ // get the program file
+ if (!xm_machine_get_program_file(impl, path, sizeof(path))) return -1;
// get the program directory
- if (!xm_machine_main_get_program_directory(impl, path, sizeof(path))) return -1;
+ if (!xm_machine_get_program_directory(impl, path, sizeof(path), path)) return -1;
// append the main script path
tb_strcat(path, "/core/_xmake_main.lua");
diff --git a/xmake/core/_xmake_main.lua b/xmake/core/_xmake_main.lua
index 1f169e4af..39bb5f225 100644
--- a/xmake/core/_xmake_main.lua
+++ b/xmake/core/_xmake_main.lua
@@ -31,6 +31,7 @@ xmake._NULDEV = _NULDEV
xmake._VERSION = _VERSION
xmake._VERSION_SHORT = _VERSION_SHORT
xmake._PROGRAM_DIR = _PROGRAM_DIR
+xmake._PROGRAM_FILE = _PROGRAM_FILE
xmake._PROJECT_DIR = _PROJECT_DIR
xmake._CORE_DIR = _PROGRAM_DIR .. "/core"
xmake._TOOLS_DIR = _PROGRAM_DIR .. "/tools"
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 95ebfa79f..5c7ca78e2 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -680,6 +680,20 @@ function os.addenv(name, ...)
end
+-- get the program directory
+function os.programdir()
+
+ -- get it
+ return xmake._PROGRAM_DIR
+end
+
+-- get the program file
+function os.programfile()
+
+ -- get it
+ return xmake._PROGRAM_FILE
+end
+
-- get version info
function os.versioninfo()
diff --git a/xmake/core/main.lua b/xmake/core/main.lua
index cda338294..62800d2be 100644
--- a/xmake/core/main.lua
+++ b/xmake/core/main.lua
@@ -126,6 +126,14 @@ function main._init()
break
end
end
+
+ -- add the directory of the program file (xmake) to $PATH environment
+ local programfile = os.programfile()
+ if programfile and os.isfile(programfile) then
+ os.addenv("PATH", path.directory(programfile))
+ else
+ os.addenv("PATH", os.programdir())
+ end
end
-- the main function
diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua
index 6dff6d1a4..191e53871 100644
--- a/xmake/core/project/project.lua
+++ b/xmake/core/project/project.lua
@@ -319,8 +319,7 @@ function project._interpreter()
, configdir = config.directory()
, projectdir = project.directory()
, packagedir = package.directory()
- , programdir = xmake._PROGRAM_DIR
- , xmake = path.join(xmake._PROGRAM_DIR, "xmake")
+ , programdir = os.programdir()
}
-- map it
diff --git a/xmake/core/project/task.lua b/xmake/core/project/task.lua
index 0111dd6e9..a162d84e4 100644
--- a/xmake/core/project/task.lua
+++ b/xmake/core/project/task.lua
@@ -203,8 +203,7 @@ function task._interpreter()
, configdir = config.directory()
, projectdir = xmake._PROJECT_DIR
, packagedir = package.directory()
- , programdir = xmake._PROGRAM_DIR
- , xmake = path.join(xmake._PROGRAM_DIR, "xmake")
+ , programdir = os.programdir()
}
-- map it
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index 00e22bfbb..4b31128d2 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -34,18 +34,20 @@ local vformat = require("sandbox/modules/vformat")
local sandbox_os = sandbox_os or {}
-- inherit some builtin interfaces
-sandbox_os.host = os.host
-sandbox_os.arch = os.arch
-sandbox_os.exit = os.exit
-sandbox_os.date = os.date
-sandbox_os.time = os.time
-sandbox_os.argv = os.argv
-sandbox_os.argw = os.argw
-sandbox_os.mtime = os.mtime
-sandbox_os.sleep = os.sleep
-sandbox_os.mclock = os.mclock
-sandbox_os.nuldev = os.nuldev
-sandbox_os.emptydir = os.emptydir
+sandbox_os.host = os.host
+sandbox_os.arch = os.arch
+sandbox_os.exit = os.exit
+sandbox_os.date = os.date
+sandbox_os.time = os.time
+sandbox_os.argv = os.argv
+sandbox_os.argw = os.argw
+sandbox_os.mtime = os.mtime
+sandbox_os.sleep = os.sleep
+sandbox_os.mclock = os.mclock
+sandbox_os.nuldev = os.nuldev
+sandbox_os.emptydir = os.emptydir
+sandbox_os.programdir = os.programdir
+sandbox_os.programfile = os.programfile
-- copy file or directory
function sandbox_os.cp(...)
@@ -202,13 +204,6 @@ function sandbox_os.toolsdir()
return xmake._TOOLS_DIR
end
--- get the program directory
-function sandbox_os.programdir()
-
- -- get it
- return xmake._PROGRAM_DIR
-end
-
-- get the script directory
function sandbox_os.scriptdir()