summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2015-05-16 19:19:39 +0800
committerruki <[email protected]>2015-05-16 19:19:39 +0800
commit94c489e0b0c07d8187d96ca7659357c5c71d5944 (patch)
tree6e14f916b501f6e9a1c045cd05e67c9f32335485
parent5fefc1a0bc38a6917c38590d08bf14a4c43dd886 (diff)
add script for the configure value
-rwxr-xr-xcore/src/xmake/preprocessor/loadx.c75
-rw-r--r--tests/console/xmake.xproj9
-rw-r--r--xmake/scripts/base/preprocessor.lua37
3 files changed, 108 insertions, 13 deletions
diff --git a/core/src/xmake/preprocessor/loadx.c b/core/src/xmake/preprocessor/loadx.c
index 81763aa4c..a529b97d6 100755
--- a/core/src/xmake/preprocessor/loadx.c
+++ b/core/src/xmake/preprocessor/loadx.c
@@ -51,23 +51,80 @@ static tb_char_t const* xm_preprocessor_loadx_to_string(tb_stream_ref_t stream,
// done
tb_bool_t is_value = tb_false;
tb_bool_t is_values = tb_false;
+ tb_size_t is_script = 0;
+ tb_bool_t is_comment = tb_false;
+ tb_bool_t is_value_string = tb_false;
+ tb_size_t is_value_script = 0;
while (!tb_stream_beof(stream))
{
// read character
tb_char_t ch = (tb_char_t)tb_stream_bread_u8(stream);
if (ch)
{
+ // is comment? skip it
+ if (is_comment)
+ {
+ // newline? leave comment
+ if (ch == '\n') is_comment = tb_false;
+ }
+ // is script? skip it
+ else if (is_script)
+ {
+ // is ']'?
+ if (ch == ']')
+ {
+ // leave bracket?
+ is_script--;
+
+ // append it if is script now
+ if (is_script) tb_string_chrcat(string, ch);
+ }
+ else
+ {
+ // enter bracket?
+ if (ch == '[') is_script++;
+
+ // append it
+ tb_string_chrcat(string, ch);
+ }
+ }
// is values?
- if (is_values)
+ else if (is_values)
{
+ // enter or leave string
+ if (ch == '\"' || ch == '\'') is_value_string = !is_value_string;
+
+ // enter or leave script?
+ if (ch == '[') is_value_script++;
+ else if (ch == ']' && is_value_script) is_value_script--;
+
+ // is string or script value?
+ if (is_value && (is_value_string || is_value_script))
+ {
+ // append to value
+ tb_string_chrcat(&value, ch);
+ }
// is value?
- if (is_value && ch != ',' && ch != ':' && ch != '{' && ch != '}' && ch != '\r' && ch != '\n')
+ else if ( is_value
+ && ch != ','
+ && ch != ':'
+ && ch != '{'
+ && ch != '}'
+ && ch != '\r'
+ && ch != '\n'
+ && ch != '#')
{
// append to value
tb_string_chrcat(&value, ch);
}
- // ',' or ':' or '{' or newline?
- else if (ch == ',' || ch == ':' || ch == '{' || ch == '}' || ch == '\r' || ch == '\n')
+ // ',' or ':' or '{' or '#' or newline?
+ else if ( ch == ','
+ || ch == ':'
+ || ch == '{'
+ || ch == '}'
+ || ch == '\r'
+ || ch == '\n'
+ || ch == '#')
{
// has value?
if (is_value)
@@ -85,8 +142,8 @@ static tb_char_t const* xm_preprocessor_loadx_to_string(tb_stream_ref_t stream,
// wrap "xxx"
if (tb_string_size(&value))
{
- // it has been "xxx"? only copy it
- if (tb_string_charat(&value, 0) == '\"')
+ // it has been "xxx" or 'xxx'? only copy it
+ if (tb_string_charat(&value, 0) == '\"' || tb_string_charat(&value, 0) == '\'')
tb_string_strcpy(&value_temp, &value);
// wrap it
else
@@ -146,6 +203,8 @@ static tb_char_t const* xm_preprocessor_loadx_to_string(tb_stream_ref_t stream,
}
// skip newline
else if (ch == '\r' || ch == '\n') ;
+ // enter comment?
+ else if (ch == '#' && !is_comment) is_comment = tb_true;
// append to string
else tb_string_chrcat(string, ch);
}
@@ -169,6 +228,10 @@ static tb_char_t const* xm_preprocessor_loadx_to_string(tb_stream_ref_t stream,
}
// replace '}' and newline to _end
else if (ch == '}') tb_string_cstrcat(string, "\n_end()");
+ // enter comment? skip it
+ else if (ch == '#' && !is_comment) is_comment = tb_true;
+ // enter script? skip it
+ else if (ch == '[' && !is_script) is_script++;
// append it
else tb_string_chrcat(string, ch);
}
diff --git a/tests/console/xmake.xproj b/tests/console/xmake.xproj
index 7af84782a..940feae74 100644
--- a/tests/console/xmake.xproj
+++ b/tests/console/xmake.xproj
@@ -1,3 +1,9 @@
+[
+ -- the custom script
+ function add(a, b)
+ return a + b
+ end
+]
project: console
{
target: hello1
@@ -52,7 +58,7 @@ project: console
links: hello3
linkdirs: $(buildir)/lib
includedirs: $(buildir)/inc
- mflags: -DHELLO3, -DPLAT="macosx"
+ mflags: -DHELLO3, -DPLAT="macosx", -DARCH='armv7, arm64', -DVERSION=[return add(1, 2)]
}
target: demo_objcpp
@@ -63,6 +69,7 @@ project: console
mxflags: -DHELLO3
ldflags: -lhello3, -L$(buildir)/lib
+ # the linux platform
platforms: linux
{
defines: PLAT="linux"
diff --git a/xmake/scripts/base/preprocessor.lua b/xmake/scripts/base/preprocessor.lua
index ed651932e..7f2cd8c02 100644
--- a/xmake/scripts/base/preprocessor.lua
+++ b/xmake/scripts/base/preprocessor.lua
@@ -24,13 +24,38 @@
local preprocessor = preprocessor or {}
-- filter value
-function preprocessor._filter(value, filter)
+function preprocessor._filter(env, value, filter)
-- the value is string?
- if filter and type(value) == "string" then
+ if type(value) == "string" then
- -- replace $(variable)
- value = value:gsub("%$%((.*)%)", filter)
+ -- init
+ local _v = nil
+ local _n = 0
+
+ -- replace it for filter
+ if filter then
+ -- replace $(variable)
+ _v, _n = value:gsub("%$%((.*)%)", filter)
+ end
+
+ -- replace it for script
+ if _n == 0 then
+ _v, _n = value:gsub("%[(.*)%]", function (v)
+
+ -- load the script
+ local script = assert(loadstring(v))
+
+ -- bind the envirnoment
+ setfenv(script, env)
+
+ -- done it
+ return script()
+ end)
+ end
+
+ -- update value
+ value = _v
end
-- ok
@@ -64,11 +89,11 @@ function preprocessor._register(env, names, filter)
_current[name] = nil
elseif table.getn(arg) == 1 then
-- save only one argument
- _current[name] = preprocessor._filter(arg[1], filter)
+ _current[name] = preprocessor._filter(env, arg[1], filter)
else
-- save all arguments
for i, v in ipairs(arg) do
- _current[name][i] = preprocessor._filter(v, filter)
+ _current[name][i] = preprocessor._filter(env, v, filter)
end
end
end