diff options
| author | ruki <[email protected]> | 2016-05-19 15:47:46 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2016-05-19 15:47:46 +0800 |
| commit | f3ab20418a0b06f84c6afa22ecfebf7965fd646b (patch) | |
| tree | 91f7d98b3c4a5631897af10eda485f58a430724f /core/src | |
| parent | 3f36fc0bee466691f3d0400ae7b4d8259870a15a (diff) | |
impl multi-jobs for builder
Diffstat (limited to 'core/src')
| -rwxr-xr-x | core/src/xmake/machine.c | 23 | ||||
| -rwxr-xr-x | core/src/xmake/makefile | 6 | ||||
| -rwxr-xr-x | core/src/xmake/os/mclock.c | 50 | ||||
| -rwxr-xr-x | core/src/xmake/os/setenv.c | 79 | ||||
| -rwxr-xr-x | core/src/xmake/process/close.c | 61 | ||||
| -rwxr-xr-x | core/src/xmake/process/open.c | 87 | ||||
| -rw-r--r-- | core/src/xmake/process/prefix.h | 35 | ||||
| -rwxr-xr-x | core/src/xmake/process/wait.c | 66 |
8 files changed, 328 insertions, 79 deletions
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index e92b3814b..5f499a963 100755 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -57,6 +57,7 @@ tb_int_t xm_os_mkdir(lua_State* lua); tb_int_t xm_os_cpdir(lua_State* lua); tb_int_t xm_os_chdir(lua_State* lua); tb_int_t xm_os_mtime(lua_State* lua); +tb_int_t xm_os_mclock(lua_State* lua); tb_int_t xm_os_curdir(lua_State* lua); tb_int_t xm_os_tmpdir(lua_State* lua); tb_int_t xm_os_isfile(lua_State* lua); @@ -79,6 +80,11 @@ tb_int_t xm_path_is_absolute(lua_State* lua); tb_int_t xm_string_endswith(lua_State* lua); tb_int_t xm_string_startswith(lua_State* lua); +// the process functions +tb_int_t xm_process_open(lua_State* lua); +tb_int_t xm_process_wait(lua_State* lua); +tb_int_t xm_process_close(lua_State* lua); + /* ////////////////////////////////////////////////////////////////////////////////////// * globals */ @@ -93,6 +99,7 @@ static luaL_Reg const g_os_functions[] = , { "cpdir", xm_os_cpdir } , { "chdir", xm_os_chdir } , { "mtime", xm_os_mtime } +, { "mclock", xm_os_mclock } , { "curdir", xm_os_curdir } , { "tmpdir", xm_os_tmpdir } , { "isfile", xm_os_isfile } @@ -125,6 +132,15 @@ static luaL_Reg const g_string_functions[] = , { tb_null, tb_null } }; +// the process functions +static luaL_Reg const g_process_functions[] = +{ + { "open", xm_process_open } +, { "wait", xm_process_wait } +, { "close", xm_process_close } +, { tb_null, tb_null } +}; + /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ @@ -162,7 +178,7 @@ static tb_bool_t xm_machine_main_get_program_directory(xm_machine_impl_t* impl, { // get it from the environment variable tb_char_t data[TB_PATH_MAXN] = {0}; - if (!tb_environment_get_one("XMAKE_PROGRAM_DIR", data, sizeof(data))) + if (!tb_environment_first("XMAKE_PROGRAM_DIR", data, sizeof(data))) { // error tb_printf("error: please set XMAKE_PROGRAM_DIR first!\n"); @@ -198,7 +214,7 @@ static tb_bool_t xm_machine_main_get_project_directory(xm_machine_impl_t* impl, { // attempt to get it from the environment variable first tb_char_t data[TB_PATH_MAXN] = {0}; - if ( !tb_environment_get_one("XMAKE_PROJECT_DIR", data, sizeof(data)) + if ( !tb_environment_first("XMAKE_PROJECT_DIR", data, sizeof(data)) || !tb_path_absolute(data, path, maxn)) { // get it from the current directory @@ -254,6 +270,9 @@ xm_machine_ref_t xm_machine_init() // bind string functions luaL_register(impl->lua, "string", g_string_functions); + // bind process functions + luaL_register(impl->lua, "process", g_process_functions); + // init host #if defined(TB_CONFIG_OS_WINDOWS) lua_pushstring(impl->lua, "windows"); diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 105bc2eff..8d2ca97f4 100755 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -21,6 +21,7 @@ xmake_C_FILES += \ os/cpdir \ os/chdir \ os/mtime \ + os/mclock \ os/curdir \ os/tmpdir \ os/isfile \ @@ -37,7 +38,10 @@ xmake_C_FILES += \ path/translate \ path/is_absolute \ string/endswith \ - string/startswith + string/startswith \ + process/open \ + process/wait \ + process/close # flags diff --git a/core/src/xmake/os/mclock.c b/core/src/xmake/os/mclock.c new file mode 100755 index 000000000..5e2f6eb87 --- /dev/null +++ b/core/src/xmake/os/mclock.c @@ -0,0 +1,50 @@ +/*!The Automatic Cross-platform Build Tool + * + * XMake is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * XMake is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with XMake; + * If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a> + * + * Copyright (C) 2015 - 2016, ruki All rights reserved. + * + * @author ruki + * @file mclock.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "mclock" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// os.mclock() +tb_int_t xm_os_mclock(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // save result + lua_pushnumber(lua, tb_mclock()); + + // ok + return 1; +} diff --git a/core/src/xmake/os/setenv.c b/core/src/xmake/os/setenv.c index 381db6540..93376abb7 100755 --- a/core/src/xmake/os/setenv.c +++ b/core/src/xmake/os/setenv.c @@ -33,19 +33,9 @@ #include "prefix.h" /* ////////////////////////////////////////////////////////////////////////////////////// - * macros - */ - -// the separator -#ifdef TB_CONFIG_OS_WINDOWS -# define XM_OS_ENV_SEP ';' -#else -# define XM_OS_ENV_SEP ':' -#endif - -/* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ +// ok = os.setenv(name, value) tb_int_t xm_os_setenv(lua_State* lua) { // check @@ -57,71 +47,8 @@ tb_int_t xm_os_setenv(lua_State* lua) tb_char_t const* value = luaL_checklstring(lua, 2, &value_size); tb_check_return_val(name, 0); - // find the first separator position - tb_char_t const* p = value? tb_strchr(value, XM_OS_ENV_SEP) : tb_null; - if (p) - { - // init filter - tb_hash_set_ref_t filter = tb_hash_set_init(8, tb_element_str(tb_true)); - - // init environment - tb_char_t data[TB_PATH_MAXN]; - tb_environment_ref_t environment = tb_environment_init(); - if (environment) - { - // make environment - tb_char_t const* b = value; - tb_char_t const* e = b + value_size; - do - { - // not empty? - if (b < p) - { - // the size - tb_size_t size = tb_min(p - b, sizeof(data) - 1); - - // copy it - tb_strncpy(data, b, size); - data[size] = '\0'; - - // have been not inserted? - if (!filter || !tb_hash_set_get(filter, data)) - { - // append the environment - tb_environment_insert(environment, data, tb_false); - - // save it to the filter - tb_hash_set_insert(filter, data); - } - } - - // end? - tb_check_break(p + 1 < e); - - // find the next separator position - b = p + 1; - p = tb_strchr(b, XM_OS_ENV_SEP); - if (!p) p = e; - - } while (1); - - // done os.setenv(name, value) - lua_pushboolean(lua, tb_environment_save(environment, name)); - - // exit environment - tb_environment_exit(environment); - } - - // exit filter - if (filter) tb_hash_set_exit(filter); - filter = tb_null; - } - // only one? - else - { - // done os.setenv(name, value) - lua_pushboolean(lua, tb_environment_set_one(name, value)); - } + // set it + lua_pushboolean(lua, value? tb_environment_set(name, value) : tb_false); // ok return 1; diff --git a/core/src/xmake/process/close.c b/core/src/xmake/process/close.c new file mode 100755 index 000000000..97e7bff27 --- /dev/null +++ b/core/src/xmake/process/close.c @@ -0,0 +1,61 @@ +/*!The Automatic Cross-platform Build Tool + * + * XMake is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * XMake is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with XMake; + * If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a> + * + * Copyright (C) 2015 - 2016, ruki All rights reserved. + * + * @author ruki + * @file close.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "process.close" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// process.close(p) +tb_int_t xm_process_close(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // is user data? + if (!lua_isuserdata(lua, 1)) + return 0; + + // get the process + tb_process_ref_t process = (tb_process_ref_t)lua_touserdata(lua, 1); + tb_check_return_val(process, 0); + + // exit process + tb_process_exit(process); + + // save result: ok + lua_pushboolean(lua, tb_true); + + // ok + return 1; +} diff --git a/core/src/xmake/process/open.c b/core/src/xmake/process/open.c new file mode 100755 index 000000000..3d8a63c98 --- /dev/null +++ b/core/src/xmake/process/open.c @@ -0,0 +1,87 @@ +/*!The Automatic Cross-platform Build Tool + * + * XMake is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * XMake is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with XMake; + * If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a> + * + * Copyright (C) 2015 - 2016, ruki All rights reserved. + * + * @author ruki + * @file open.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "process.open" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// p = process.open(command) +tb_int_t xm_process_open(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // get the command + size_t command_size = 0; + tb_char_t const* command = luaL_checklstring(lua, 1, &command_size); + tb_char_t const* outfile = lua_tostring(lua, 2); + tb_char_t const* errfile = lua_tostring(lua, 3); + tb_check_return_val(command, 0); + + // init attributes + tb_process_attr_t attr = {0}; + + // redirect stdout? + if (outfile) + { + // redirect stdout to file + attr.outfile = outfile; + attr.outmode = TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_APPEND; + + // remove the outfile first + if (tb_file_info(outfile, tb_null)) + tb_file_remove(outfile); + } + + // redirect stderr? + if (errfile) + { + // redirect stderr to file + attr.errfile = errfile; + attr.errmode = TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_APPEND; + + // remove the errfile first + if (tb_file_info(errfile, tb_null)) + tb_file_remove(errfile); + } + + // init process + tb_process_ref_t process = tb_process_init_cmd(command, &attr); + + // save the process reference + lua_pushlightuserdata(lua, process); + + // ok + return 1; +} diff --git a/core/src/xmake/process/prefix.h b/core/src/xmake/process/prefix.h new file mode 100644 index 000000000..72eac8c62 --- /dev/null +++ b/core/src/xmake/process/prefix.h @@ -0,0 +1,35 @@ +/*!The Automatic Cross-platform Build Tool + * + * XMake is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * XMake is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with XMake; + * If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a> + * + * Copyright (C) 2015 - 2016, ruki All rights reserved. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef XM_PROCESS_PREFIX_H +#define XM_PROCESS_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#include "luajit/luajit.h" + + +#endif + + diff --git a/core/src/xmake/process/wait.c b/core/src/xmake/process/wait.c new file mode 100755 index 000000000..fe5d4db59 --- /dev/null +++ b/core/src/xmake/process/wait.c @@ -0,0 +1,66 @@ +/*!The Automatic Cross-platform Build Tool + * + * XMake is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * XMake is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with XMake; + * If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a> + * + * Copyright (C) 2015 - 2016, ruki All rights reserved. + * + * @author ruki + * @file wait.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "process.wait" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// ok, status = process.wait(p, timeout) +tb_int_t xm_process_wait(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // is user data? + if (!lua_isuserdata(lua, 1)) + return 0; + + // get the process + tb_process_ref_t process = (tb_process_ref_t)lua_touserdata(lua, 1); + tb_check_return_val(process, 0); + + // get the timeout + tb_long_t timeout = (tb_long_t)luaL_checkinteger(lua, 2); + + // wait it + tb_long_t status = 0; + tb_long_t ok = tb_process_wait(process, &status, timeout); + + // save result + lua_pushinteger(lua, ok); + lua_pushinteger(lua, status); + + // ok + return 2; +} |
