summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2015-05-13 22:05:42 +0800
committerruki <[email protected]>2015-05-13 22:05:42 +0800
commit74ece0defa314bab78262d95bee517bdc09df689 (patch)
tree950684880d0193e60956a9b695cab50861208ff4 /core/src
parent1f45e3bf9b41a413804305fc23810639b977a9f1 (diff)
add preprocessor and update tbox
Diffstat (limited to 'core/src')
-rwxr-xr-xcore/src/xmake/machine.c58
-rwxr-xr-xcore/src/xmake/makefile3
-rwxr-xr-xcore/src/xmake/preprocessor/load_xproj.c133
-rw-r--r--core/src/xmake/preprocessor/prefix.h35
4 files changed, 199 insertions, 30 deletions
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index a1988203a..50b8984a4 100755
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -43,9 +43,6 @@ typedef struct __xm_machine_impl_t
// the lua
lua_State* lua;
- // print verbose info?
- tb_bool_t verbose;
-
}xm_machine_impl_t;
/* //////////////////////////////////////////////////////////////////////////////////////
@@ -60,6 +57,9 @@ tb_int_t xm_path_is_absolute(lua_State* lua);
// the string functions
tb_int_t xm_string_startswith(lua_State* lua);
+// the preprocessor functions
+tb_int_t xm_preprocessor_load_xproj(lua_State* lua);
+
/* //////////////////////////////////////////////////////////////////////////////////////
* globals
*/
@@ -76,8 +76,15 @@ static luaL_Reg const g_path_functions[] =
// the string functions
static luaL_Reg const g_string_functions[] =
{
- { "startswith", xm_string_startswith }
-, { tb_null, tb_null }
+ { "startswith", xm_string_startswith }
+, { tb_null, tb_null }
+};
+
+// the preprocessor functions
+static luaL_Reg const g_preprocessor_functions[] =
+{
+ { "load_xproj", xm_preprocessor_load_xproj }
+, { tb_null, tb_null }
};
/* //////////////////////////////////////////////////////////////////////////////////////
@@ -95,10 +102,6 @@ static tb_bool_t xm_machine_main_save_arguments(xm_machine_impl_t* impl, tb_int_
tb_int_t i = 0;
for (i = 1; i < argc; i++)
{
- // print verbose info
- if (!tb_strcmp(argv[i], "-v") || !tb_strcmp(argv[i], "--verbose"))
- impl->verbose = tb_true;
-
// 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);
@@ -123,8 +126,8 @@ static tb_bool_t xm_machine_main_get_program_directory(xm_machine_impl_t* impl,
tb_char_t data[TB_PATH_MAXN] = {0};
if (!tb_environment_get_one("XMAKE_PROGRAM_DIR", data, sizeof(data)))
{
- // trace
- if (impl->verbose) tb_trace_i("please set XMAKE_PROGRAM_DIR first!");
+ // error
+ tb_printf("error: please set XMAKE_PROGRAM_DIR first!\n");
break;
}
@@ -177,7 +180,7 @@ static tb_bool_t xm_machine_main_get_project_directory(xm_machine_impl_t* impl,
} while (0);
// failed?
- if (!ok && impl->verbose) tb_trace_i("not found the project directory!");
+ if (!ok) tb_printf("error: not found the project directory!\n");
// ok?
return ok;
@@ -210,6 +213,9 @@ xm_machine_ref_t xm_machine_init()
// bind string functions
luaL_register(impl->lua, "string", g_string_functions);
+ // bind preprocessor functions
+ luaL_register(impl->lua, "preprocessor", g_preprocessor_functions);
+
// init host
#if defined(TB_CONFIG_OS_WINDOWS)
lua_pushstring(impl->lua, "windows");
@@ -242,9 +248,6 @@ xm_machine_ref_t xm_machine_init()
lua_newtable(impl->lua);
lua_setglobal(impl->lua, "xmake");
- // init verbose
- impl->verbose = tb_false;
-
// ok
ok = tb_true;
@@ -295,8 +298,10 @@ tb_int_t xm_machine_main(xm_machine_ref_t machine, tb_int_t argc, tb_char_t** ar
// exists this script?
if (!tb_file_info(path, tb_null))
{
- // trace
- if (impl->verbose) tb_trace_i("not found main script: %s", path);
+ // error
+ tb_printf("not found main script: %s\n", path);
+
+ // failed
return -1;
}
@@ -306,28 +311,23 @@ tb_int_t xm_machine_main(xm_machine_ref_t machine, tb_int_t argc, tb_char_t** ar
// load and execute the main script
if (luaL_dofile(impl->lua, path))
{
- // trace
- if (impl->verbose) tb_trace_i("%s", lua_tostring(impl->lua, -1));
+ // error
+ tb_printf("error: %s\n", lua_tostring(impl->lua, -1));
// failed
return -1;
}
// set the error function
- tb_int_t errfunc = 0;
- if (impl->verbose)
- {
- lua_getglobal(impl->lua, "debug");
- lua_getfield(impl->lua, -1, "traceback");
- errfunc = -2;
- }
+ lua_getglobal(impl->lua, "debug");
+ lua_getfield(impl->lua, -1, "traceback");
// call the main function
lua_getglobal(impl->lua, "_xmake_main");
- if (lua_pcall(impl->lua, 0, 1, errfunc))
+ if (lua_pcall(impl->lua, 0, 1, -2))
{
- // trace
- if (impl->verbose) tb_trace_i("%s", lua_tostring(impl->lua, -1));
+ // error
+ tb_printf("error: %s\n", lua_tostring(impl->lua, -1));
// failed
return -1;
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index aaa94682e..20b4706e3 100755
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -17,7 +17,8 @@ xmake_C_FILES += \
path/absolute \
path/translate \
path/is_absolute \
- string/startswith
+ string/startswith \
+ preprocessor/load_xproj
# flags
diff --git a/core/src/xmake/preprocessor/load_xproj.c b/core/src/xmake/preprocessor/load_xproj.c
new file mode 100755
index 000000000..1c03d563a
--- /dev/null
+++ b/core/src/xmake/preprocessor/load_xproj.c
@@ -0,0 +1,133 @@
+/*!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) 2009 - 2015, ruki All rights reserved.
+ *
+ * @author ruki
+ * @file load_xproj.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "load_xproj"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * private implementation
+ */
+static tb_char_t const* xm_preprocessor_load_xproj_to_string(tb_stream_ref_t stream, tb_string_ref_t string)
+{
+ // check
+ tb_assert_and_check_return_val(stream && string, tb_null);
+
+ // done
+ tb_bool_t is_values = tb_false;
+ while (!tb_stream_beof(stream))
+ {
+ // read character
+ tb_char_t ch = (tb_char_t)tb_stream_bread_u8(stream);
+ if (ch)
+ {
+ // skip '{'
+ if (ch == '{') ;
+ // replace '}' to scopend
+ else if (ch == '}') tb_string_cstrcat(string, "scopend");
+ // enter values?
+ else if (ch == ':')
+ {
+ // values now
+ is_values = tb_true;
+ }
+ // append it
+ else tb_string_chrcat(string, ch);
+ }
+ else
+ {
+ // end
+ break;
+ }
+ }
+
+ // ok?
+ return tb_string_size(string)? tb_string_cstr(string) : tb_null;
+}
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+tb_int_t xm_preprocessor_load_xproj(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get the xmake.xproj path
+ tb_char_t const* path = luaL_checkstring(lua, 1);
+ tb_check_return_val(path, 0);
+
+ // done
+ tb_int_t ok = 0;
+ tb_stream_ref_t stream = tb_null;
+ tb_string_t string;
+ do
+ {
+ // init string
+ if (!tb_string_init(&string)) break;
+
+ // init stream
+ stream = tb_stream_init_from_file(path, TB_FILE_MODE_RO);
+ tb_assert_and_check_break(stream);
+
+ // open stream
+ if (!tb_stream_open(stream)) break;
+
+ // load xmake.xproj to the string and preprocess it
+ tb_char_t const* xproj = xm_preprocessor_load_xproj_to_string(stream, &string);
+ tb_assert_and_check_break(xproj);
+
+ tb_trace_i("%s", xproj);
+
+ // load xmake.xproj string to script
+ if (luaL_loadstring(lua, xproj))
+ {
+ // error
+ tb_printf("error: %s\n", lua_tostring(lua, -1));
+
+ // failed
+ break;
+ }
+
+ // ok
+ ok = 1;
+
+ } while (0);
+
+ // exit stream
+ if (stream) tb_stream_exit(stream);
+ stream = tb_null;
+
+ // exit string
+ tb_string_exit(&string);
+
+ // ok?
+ return ok;
+}
diff --git a/core/src/xmake/preprocessor/prefix.h b/core/src/xmake/preprocessor/prefix.h
new file mode 100644
index 000000000..443833645
--- /dev/null
+++ b/core/src/xmake/preprocessor/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) 2009 - 2015, ruki All rights reserved.
+ *
+ * @author ruki
+ * @file prefix.h
+ *
+ */
+#ifndef XM_PREPROCESSOR_PREFIX_H
+#define XM_PREPROCESSOR_PREFIX_H
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "../prefix.h"
+#include "luajit/luajit.h"
+
+
+#endif
+
+