diff options
| author | ruki <[email protected]> | 2015-04-25 17:58:04 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2015-04-25 17:58:04 +0800 |
| commit | de9eb6a0e554b73c8b52c7e222b376d5494ac6e3 (patch) | |
| tree | eabac8c0cf186d4650d73e6e6c4609ad137ee22d | |
| parent | aea3dbeeeabd51d16a41d73a203f0467f8ad6cf0 (diff) | |
get the install and project directory
| -rw-r--r-- | core/pkg/luajit.pkg/lib/mac/x64/libluajit.a | bin | 739440 -> 739440 bytes | |||
| -rw-r--r-- | core/pkg/tbox.pkg/inc/mac/x64/tbox.config.h | 2 | ||||
| -rwxr-xr-x | core/pkg/tbox.pkg/inc/tbox/platform/directory.h | 4 | ||||
| -rwxr-xr-x | core/pkg/tbox.pkg/inc/tbox/platform/environment.h | 224 | ||||
| -rwxr-xr-x | core/pkg/tbox.pkg/inc/tbox/platform/platform.h | 1 | ||||
| -rwxr-xr-x | core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/kernel32.h | 12 | ||||
| -rwxr-xr-x | core/plat/mac/prefix.mak | 2 | ||||
| -rwxr-xr-x | core/src/demo/makefile | 2 | ||||
| -rwxr-xr-x | core/src/demo/xmake.c | 30 | ||||
| -rwxr-xr-x | core/src/xmake/machine.c | 145 | ||||
| -rwxr-xr-x | core/src/xmake/machine.h | 3 |
11 files changed, 400 insertions, 25 deletions
diff --git a/core/pkg/luajit.pkg/lib/mac/x64/libluajit.a b/core/pkg/luajit.pkg/lib/mac/x64/libluajit.a Binary files differindex 78efe56e1..17b2666cf 100644 --- a/core/pkg/luajit.pkg/lib/mac/x64/libluajit.a +++ b/core/pkg/luajit.pkg/lib/mac/x64/libluajit.a diff --git a/core/pkg/tbox.pkg/inc/mac/x64/tbox.config.h b/core/pkg/tbox.pkg/inc/mac/x64/tbox.config.h index 2c47f78ca..1a64bb537 100644 --- a/core/pkg/tbox.pkg/inc/mac/x64/tbox.config.h +++ b/core/pkg/tbox.pkg/inc/mac/x64/tbox.config.h @@ -14,7 +14,7 @@ #define TB_CONFIG_VERSION_ALTER 8 // build version -#define TB_CONFIG_VERSION_BUILD 201502271621 +#define TB_CONFIG_VERSION_BUILD 201504251218 // small #define TB_CONFIG_SMALL (0) diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/directory.h b/core/pkg/tbox.pkg/inc/tbox/platform/directory.h index 7ea461938..31dded3ca 100755 --- a/core/pkg/tbox.pkg/inc/tbox/platform/directory.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/directory.h @@ -68,7 +68,7 @@ tb_bool_t tb_directory_remove(tb_char_t const* path); * * @return the directory path size */ -tb_size_t tb_directory_temp(tb_char_t* path, tb_size_t maxn); +tb_size_t tb_directory_temporary(tb_char_t* path, tb_size_t maxn); /*! the current directory * @@ -77,7 +77,7 @@ tb_size_t tb_directory_temp(tb_char_t* path, tb_size_t maxn); * * @return the directory path size */ -tb_size_t tb_directory_curt(tb_char_t* path, tb_size_t maxn); +tb_size_t tb_directory_current(tb_char_t* path, tb_size_t maxn); /*! the directory walk * diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/environment.h b/core/pkg/tbox.pkg/inc/tbox/platform/environment.h new file mode 100755 index 000000000..238e368af --- /dev/null +++ b/core/pkg/tbox.pkg/inc/tbox/platform/environment.h @@ -0,0 +1,224 @@ +/*!The Treasure Box Library + * + * TBox 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. + * + * TBox 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 TBox; + * If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a> + * + * Copyright (C) 2009 - 2015, ruki All rights reserved. + * + * @author ruki + * @file environment.h + * @ingroup platform + * + */ +#ifndef TB_PLATFORM_ENVIRONMENT_H +#define TB_PLATFORM_ENVIRONMENT_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#include "../container/iterator.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_enter__ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +/// the environment variable ref type +typedef tb_iterator_ref_t tb_environment_ref_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/*! init the environment variable + * + * @return the environment variable + */ +tb_environment_ref_t tb_environment_init(tb_noarg_t); + +/*! exit the environment variable + * + * @param environment the environment variable + */ +tb_void_t tb_environment_exit(tb_environment_ref_t environment); + +/*! the environment variable count + * + * @param environment the environment variable + * + * @return the environment variable count + */ +tb_size_t tb_environment_size(tb_environment_ref_t environment); + +/*! load the environment variable from the given name + * + * @code + * + // init environment + tb_environment_ref_t environment = tb_environment_init(); + if (environment) + { + // load variable + if (tb_environment_load(environment, "PATH")) + { + // trace + tb_trace_i("%s:", name); + + // dump values + tb_for_all_if (tb_char_t const*, value, environment, value) + { + // trace + tb_trace_i(" %s", value); + } + } + + // exit environment + tb_environment_exit(environment); + } + * @endcode + * + * @param environment the environment variable + * @param name the variable name + * + * @return the count of the variable value + */ +tb_size_t tb_environment_load(tb_environment_ref_t environment, tb_char_t const* name); + +/*! save the environment variable and will overwrite it + * + * we will remove this environment variable if environment is null or empty + * + * @code + * + // init environment + tb_environment_ref_t environment = tb_environment_init(); + if (environment) + { + // set values + tb_environment_set(environment, "/xxx/0", tb_false); + tb_environment_set(environment, "/xxx/1", tb_false); + tb_environment_set(environment, "/xxx/2", tb_false); + tb_environment_set(environment, "/xxx/3", tb_false); + + // save variable + tb_environment_save(environment, "PATH"); + + // exit environment + tb_environment_exit(environment); + } + + * @endcode + * + * @param environment the environment variable + * @param name the variable name + * + * @return tb_true or tb_false + */ +tb_bool_t tb_environment_save(tb_environment_ref_t environment, tb_char_t const* name); + +/*! get the environment variable from the given index + * + * @code + * + // init environment + tb_environment_ref_t environment = tb_environment_init(); + if (environment) + { + // load variable + if (tb_environment_load(environment, "PATH")) + { + tb_char_t const* value = tb_environment_get(environment, 0); + if (value) + { + // ... + } + } + + // exit environment + tb_environment_exit(environment); + } + * @endcode + * + * + * @param environment the environment variable + * @param index the variable index + * + * @return the variable value + */ +tb_char_t const* tb_environment_get(tb_environment_ref_t environment, tb_size_t index); + +/*! set the environment variable and will overwrite it + * + * we will clear environment if value == null and overwrite + * + * @param environment the environment variable + * @param value the variable value + * @param overwrite is overwrite or insert value into the head? + * + * @return tb_true or tb_false + */ +tb_bool_t tb_environment_set(tb_environment_ref_t environment, tb_char_t const* value, tb_bool_t overwrite); + +#ifdef __tb_debug__ +/*! dump the environment variable + * + * @param environment the environment variable + * @param name the variable name + */ +tb_void_t tb_environment_dump(tb_environment_ref_t environment, tb_char_t const* name); +#endif + +/*! get the first environment variable value + * + * @code + + tb_char_t value[TB_PATH_MAXN]; + if (tb_environment_get_one("HOME", value, sizeof(value))) + { + // ... + } + + * @endcode + * + * @param name the variable name + * @param value the variable value + * @param maxn the variable value maxn + * + * @return the variable value size + */ +tb_size_t tb_environment_get_one(tb_char_t const* name, tb_char_t* value, tb_size_t maxn); + +/*! set the environment variable value + * + * we will set only one value and overwrite it, + * and remove this environment variable if the value is null + * + * @param name the variable name + * @param value the variable value + * + * @return tb_true or tb_false + */ +tb_bool_t tb_environment_set_one(tb_char_t const* name, tb_char_t const* value); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * extern + */ +__tb_extern_c_leave__ + +#endif diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/platform.h b/core/pkg/tbox.pkg/inc/tbox/platform/platform.h index e1e9b1f79..3b759e2f7 100755 --- a/core/pkg/tbox.pkg/inc/tbox/platform/platform.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/platform.h @@ -55,6 +55,7 @@ #include "directory.h" #include "exception.h" #include "cache_time.h" +#include "environment.h" #include "thread_pool.h" #include "thread_store.h" diff --git a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/kernel32.h b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/kernel32.h index 4130165bb..aafd97c91 100755 --- a/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/kernel32.h +++ b/core/pkg/tbox.pkg/inc/tbox/platform/windows/interface/kernel32.h @@ -62,6 +62,12 @@ typedef BOOL (WINAPI* tb_kernel32_GetFileSizeEx_t)(HANDLE hFile, PLARGE_INTEGER // the InterlockedCompareExchange64 func type typedef LONGLONG (WINAPI* tb_kernel32_InterlockedCompareExchange64_t)(LONGLONG __tb_volatile__* Destination, LONGLONG Exchange, LONGLONG Comparand); +// the GetEnvironmentVariableW func type +typedef DWORD (WINAPI* tb_kernel32_GetEnvironmentVariableW_t)(LPCWSTR lpName, LPWSTR lpBuffer, DWORD nSize); + +// the SetEnvironmentVariableW func type +typedef BOOL (WINAPI* tb_kernel32_SetEnvironmentVariableW_t)(LPCWSTR lpName, LPCWSTR lpValue); + // the kernel32 interfaces type typedef struct __tb_kernel32_t { @@ -80,6 +86,12 @@ typedef struct __tb_kernel32_t // InterlockedCompareExchange64 tb_kernel32_InterlockedCompareExchange64_t InterlockedCompareExchange64; + // GetEnvironmentVariableW + tb_kernel32_GetEnvironmentVariableW_t GetEnvironmentVariableW; + + // SetEnvironmentVariableW + tb_kernel32_SetEnvironmentVariableW_t SetEnvironmentVariableW; + }tb_kernel32_t,*tb_kernel32_ref_t; /* ////////////////////////////////////////////////////////////////////////////////////// diff --git a/core/plat/mac/prefix.mak b/core/plat/mac/prefix.mak index 819aaa2ab..e3675eec6 100755 --- a/core/plat/mac/prefix.mak +++ b/core/plat/mac/prefix.mak @@ -129,7 +129,7 @@ MMFLAGS = # ldflags LDFLAGS_RELEASE = LDFLAGS_DEBUG = -LDFLAGS = -m$(BITS) -all_load +LDFLAGS = -m$(BITS) -all_load -pagezero_size 10000 -image_base 100000000 LDFLAGS-L = -L LDFLAGS-l = -l LDFLAGS-f = diff --git a/core/src/demo/makefile b/core/src/demo/makefile index 277c3d6ed..8426ce7de 100755 --- a/core/src/demo/makefile +++ b/core/src/demo/makefile @@ -19,7 +19,7 @@ demo_PKGS-$(BASE) += base demo_LIBS += xmake$(DTYPE) demo_INC_DIRS += ../ demo_LIB_DIRS += ../xmake -demo_CXFLAGS += -D__tb_prefix__=\"demo\" +demo_CXFLAGS += -D__tb_prefix__=\"xmake\" # suffix include $(PRO_DIR)/suffix.mak diff --git a/core/src/demo/xmake.c b/core/src/demo/xmake.c index 5b69e36e3..9c2d489d1 100755 --- a/core/src/demo/xmake.c +++ b/core/src/demo/xmake.c @@ -8,23 +8,27 @@ */ tb_int_t main(tb_int_t argc, tb_char_t** argv) { - // init xmake - if (!xm_init()) return 0; + // init ok + tb_int_t ok = -1; - // init machine - xm_machine_ref_t machine = xm_machine_init(); - if (machine) + // init xmake + if (xm_init()) { - // done machine - xm_machine_main(machine, argc, argv, "xmake_main.lua"); + // init machine + xm_machine_ref_t machine = xm_machine_init(); + if (machine) + { + // done machine + ok = xm_machine_main(machine, argc, argv); - // exit machine - xm_machine_exit(machine); - } + // exit machine + xm_machine_exit(machine); + } - // exit xmake - xm_exit(); + // exit xmake + xm_exit(); + } // ok? - return 0; + return ok; } diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index 5ed83df09..26229abb7 100755 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -17,11 +17,17 @@ * Copyright (C) 2009 - 2015, ruki All rights reserved. * * @author ruki - * @file xmake.c + * @file machine.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "machine" +#define TB_TRACE_MODULE_DEBUG (1) + +/* ////////////////////////////////////////////////////////////////////////////////////// * includes */ #include "xmake.h" @@ -40,6 +46,115 @@ typedef struct __xm_machine_impl_t }xm_machine_impl_t; /* ////////////////////////////////////////////////////////////////////////////////////// + * private implementation + */ +static tb_bool_t xm_machine_main_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); + + // put a new table into the stack + lua_newtable(impl->lua); + + // save all arguments to the new table + tb_int_t i = 0; + for (i = 1; i < argc; i++) + { + // get the install directory + if (!tb_strncmp(argv[i], "--install=", 10)) + { + // save it to the environment variable + tb_environment_set_one("XMAKE_INSTALL_DIR", argv[i] + 10); + continue ; + } + + // get the project directory + if (!tb_strncmp(argv[i], "--project=", 10)) + { + // save it to the environment variable + tb_environment_set_one("XMAKE_PROJECT_DIR", argv[i] + 10); + continue ; + } + + // table_new[table.getn(table_new) + 1] = argv[i] + lua_pushstring(impl->lua, argv[i]); + lua_rawseti(impl->lua, -2, luaL_getn(impl->lua, -2) + 1); + } + + // _ARGV = table_new + lua_setglobal(impl->lua, "_ARGV"); + + // ok + return tb_true; +} +static tb_bool_t xm_machine_main_get_install_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); + + // done + tb_bool_t ok = tb_false; + do + { + // attempt to get it from the environment variable first + tb_char_t data[TB_PATH_MAXN] = {0}; + if ( !tb_environment_get_one("XMAKE_INSTALL_DIR", data, sizeof(data)) + || !tb_path_full(data, path, maxn)) + { + // TODO + break; + } + + // trace + tb_trace_d("install: %s", path); + + // save the directory to the global variable: _INSTALL_DIR + lua_pushstring(impl->lua, path); + lua_setglobal(impl->lua, "_INSTALL_DIR"); + + // ok + ok = tb_true; + + } while (0); + + // 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) +{ + // check + tb_assert_and_check_return_val(impl && path && maxn, tb_false); + + // done + tb_bool_t ok = tb_false; + do + { + // 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)) + || !tb_path_full(data, path, maxn)) + { + // get it from the current directory + if (!tb_directory_current(path, maxn)) break; + } + + // trace + tb_trace_d("project: %s", path); + + // save the directory to the global variable: _PROJECT_DIR + lua_pushstring(impl->lua, path); + lua_setglobal(impl->lua, "_PROJECT_DIR"); + + // ok + ok = tb_true; + + } while (0); + + // ok? + return ok; +} + +/* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ xm_machine_ref_t xm_machine_init() @@ -60,7 +175,7 @@ xm_machine_ref_t xm_machine_init() // open lua libraries luaL_openlibs(impl->lua); - // TODO + // TODO: bind lua interfaces // ... // ok @@ -91,11 +206,31 @@ tb_void_t xm_machine_exit(xm_machine_ref_t machine) // exit it tb_free(impl); } -tb_int_t xm_machine_main(xm_machine_ref_t machine, tb_int_t argc, tb_char_t** argv, tb_char_t const* code_path) +tb_int_t xm_machine_main(xm_machine_ref_t machine, tb_int_t argc, tb_char_t** argv) { // check xm_machine_impl_t* impl = (xm_machine_impl_t*)machine; - tb_assert_and_check_return_val(impl && impl->lua && code_path, -1); + 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; + + // 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; + + // get the install directory + if (!xm_machine_main_get_install_directory(impl, path, sizeof(path))) return -1; + + // append the main script path + tb_strcat(path, "/scripts/xmake_main.lua"); + + // exists this script? + if (!tb_file_info(path, tb_null)) return -1; + + // trace + tb_trace_d("main: %s", path); - return 0; + // exec the script file + return luaL_dofile(impl->lua, path); } diff --git a/core/src/xmake/machine.h b/core/src/xmake/machine.h index a64999ef1..1c7f895bb 100755 --- a/core/src/xmake/machine.h +++ b/core/src/xmake/machine.h @@ -61,11 +61,10 @@ tb_void_t xm_machine_exit(xm_machine_ref_t machine); * @param machine the machine * @param argc the argument count of the console * @param argv the argument list of the console - * @param code_path the main code file path * * @return the error code of main() */ -tb_int_t xm_machine_main(xm_machine_ref_t machine, tb_int_t argc, tb_char_t** argv, tb_char_t const* code_path); +tb_int_t xm_machine_main(xm_machine_ref_t machine, tb_int_t argc, tb_char_t** argv); /* ////////////////////////////////////////////////////////////////////////////////////// * extern |
