summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-04-26 22:54:39 +0800
committerruki <[email protected]>2019-04-26 11:27:59 +0800
commit695a5e99906bbf0c4f11b68c9339222af3ebfb7d (patch)
treed14e58887134182842053d8c6618a9b1a1a152dd
parent344413267ec742dcec4d398f6f6e744ecd324d6b (diff)
pass envs to os.execv
-rw-r--r--core/src/xmake/process/openv.c48
-rw-r--r--xmake/core/base/os.lua4
2 files changed, 48 insertions, 4 deletions
diff --git a/core/src/xmake/process/openv.c b/core/src/xmake/process/openv.c
index 6a6737195..d5539512a 100644
--- a/core/src/xmake/process/openv.c
+++ b/core/src/xmake/process/openv.c
@@ -34,13 +34,13 @@
* implementation
*/
-// p = process.openv(shellname, argv, outfile, errfile)
+// p = process.openv(shellname, argv, outfile, errfile, envs)
tb_int_t xm_process_openv(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
- // check table
+ // check argv
if (!lua_istable(lua, 2))
{
// error
@@ -55,6 +55,47 @@ tb_int_t xm_process_openv(lua_State* lua)
tb_char_t const* errfile = lua_tostring(lua, 4);
tb_check_return_val(shellname, 0);
+ // get environments
+ tb_char_t const* envs[256] = {0};
+ tb_size_t envn = 0;
+ if (lua_istable(lua, 5))
+ {
+ // get environment variables count
+ envn = (tb_size_t)lua_objlen(lua, 5);
+
+ // get all passed environment variables
+ tb_size_t i;
+ for (i = 0; i < envn; i++)
+ {
+ // get envs[i]
+ lua_pushinteger(lua, i + 1);
+ lua_gettable(lua, 5);
+
+ // is string?
+ if (lua_isstring(lua, -1))
+ {
+ // add this environment value
+ if (i + 1 < tb_arrayn(envs))
+ envs[i] = lua_tostring(lua, -1);
+ else
+ {
+ // error
+ lua_pushfstring(lua, "envs is too large(%lu > %d) for process.openv", envn, tb_arrayn(envs) - 1);
+ lua_error(lua);
+ }
+ }
+ else
+ {
+ // error
+ lua_pushfstring(lua, "invalid envs[%ld] type(%s) for process.openv", i, luaL_typename(lua, -1));
+ lua_error(lua);
+ }
+
+ // pop it
+ lua_pop(lua, 1);
+ }
+ }
+
// get the arguments count
tb_long_t argn = lua_objlen(lua, 2);
tb_check_return_val(argn >= 0, 0);
@@ -92,6 +133,9 @@ tb_int_t xm_process_openv(lua_State* lua)
// init attributes
tb_process_attr_t attr = {0};
+ // set the new environments
+ if (envn > 0) attr.envp = envs;
+
// redirect stdout?
if (outfile)
{
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index dac5a3e44..b69dd1754 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -579,7 +579,7 @@ end
-- @param program "clang", "xcrun -sdk macosx clang", "~/dir/test\ xxx/clang"
-- filename "clang", "xcrun"", "~/dir/test\ xxx/clang"
-- @param argv the arguments
--- @param opt the option, .e.g {wildcards = false, stdout = outfile, stderr = errfile}
+-- @param opt the options, .e.g {wildcards = false, stdout = outfile, stderr = errfile, envs = {"PATH=xxx;xx", "CFLAGS=xx"}}
--
function os.execv(program, argv, opt)
@@ -609,7 +609,7 @@ function os.execv(program, argv, opt)
-- open command
local ok = -1
- local proc = process.openv(filename, argv, opt.stdout, opt.stderr)
+ local proc = process.openv(filename, argv, opt.stdout, opt.stderr, opt.envs)
if proc ~= nil then
-- wait process