diff options
| author | ruki <[email protected]> | 2020-03-26 00:07:17 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-03-25 18:43:45 +0800 |
| commit | 493fffe734e944e153c04bbe46e77c2069581f47 (patch) | |
| tree | 456f14038827fad4f33f1cc64a7c8d9d01e9ca79 | |
| parent | c570330bbc7fd11410985a46486e7c5edc66d9bf (diff) | |
improve os.args
| -rw-r--r-- | core/src/xmake/os/args.c | 22 | ||||
| -rw-r--r-- | tests/modules/os/test.lua | 1 | ||||
| -rw-r--r-- | xmake/modules/core/tools/ar.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/core/tools/cl.lua | 4 | ||||
| -rw-r--r-- | xmake/modules/core/tools/link.lua | 4 | ||||
| -rw-r--r-- | xmake/modules/core/tools/llvm_ar.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/core/tools/ml.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/core/tools/sdar.lua | 2 | ||||
| -rw-r--r-- | xmake/plugins/project/clang/compile_commands.lua | 4 |
9 files changed, 28 insertions, 15 deletions
diff --git a/core/src/xmake/os/args.c b/core/src/xmake/os/args.c index 0f907bb8a..aabede414 100644 --- a/core/src/xmake/os/args.c +++ b/core/src/xmake/os/args.c @@ -33,7 +33,7 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_void_t tb_os_args_append(tb_string_ref_t result, tb_char_t const* cstr, tb_size_t size) +static tb_void_t tb_os_args_append(tb_string_ref_t result, tb_char_t const* cstr, tb_size_t size, tb_bool_t escape) { // check tb_assert_and_check_return(size < TB_PATH_MAXN); @@ -47,8 +47,8 @@ static tb_void_t tb_os_args_append(tb_string_ref_t result, tb_char_t const* cstr tb_size_t m = tb_arrayn(buff); while ((ch = *p) && n < m) { - // escape '"' - if (ch == '\"') + // escape '"' or '\\' + if (ch == '\"' || (escape && ch == '\\')) { if (n < m) buff[n++] = '\\'; } @@ -80,11 +80,23 @@ static tb_void_t tb_os_args_append(tb_string_ref_t result, tb_char_t const* cstr /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ +// os.args({"xx", "yy"}, {escape = true}) tb_int_t xm_os_args(lua_State* lua) { // check tb_assert_and_check_return_val(lua, 0); + // escape '\\' characters in global? + tb_bool_t escape = tb_false; + if (lua_istable(lua, 2)) + { + // is detached? + lua_pushstring(lua, "escape"); + lua_gettable(lua, 2); + escape = lua_toboolean(lua, -1); + lua_pop(lua, 1); + } + // init result tb_string_t result; tb_string_init(&result); @@ -105,7 +117,7 @@ tb_int_t xm_os_args(lua_State* lua) size_t size = 0; tb_char_t const* cstr = luaL_checklstring(lua, -1, &size); if (cstr && size) - tb_os_args_append(&result, cstr, size); + tb_os_args_append(&result, cstr, size, escape); lua_pop(lua, 1); } } @@ -114,7 +126,7 @@ tb_int_t xm_os_args(lua_State* lua) size_t size = 0; tb_char_t const* cstr = luaL_checklstring(lua, 1, &size); if (cstr && size) - tb_os_args_append(&result, cstr, size); + tb_os_args_append(&result, cstr, size, escape); } // return result diff --git a/tests/modules/os/test.lua b/tests/modules/os/test.lua index 02d027e25..407421a5c 100644 --- a/tests/modules/os/test.lua +++ b/tests/modules/os/test.lua @@ -83,4 +83,5 @@ function test_args(t) t:are_equal(os.args({"aa\\bb/cc dd", "ee"}), "\"aa\\\\bb/cc dd\" ee") t:are_equal(os.args({"aa\\bb/cc (dd)", "ee"}), "\"aa\\\\bb/cc (dd)\" ee") t:are_equal(os.args("-D__prefix__=\"tbox\""), "-D__prefix__=\\\"tbox\\\"") + t:are_equal(os.args({"aa\\bb/cc", "dd"}, {escape = true}), "aa\\\\bb/cc dd") end diff --git a/xmake/modules/core/tools/ar.lua b/xmake/modules/core/tools/ar.lua index 5260970d1..b3a034aac 100644 --- a/xmake/modules/core/tools/ar.lua +++ b/xmake/modules/core/tools/ar.lua @@ -52,7 +52,7 @@ function linkargv(self, objectfiles, targetkind, targetfile, flags, opt) -- too long arguments for windows? if is_host("windows") then opt = opt or {} - local args = os.args(argv) + local args = os.args(argv, {escape = true}) if #args > 1024 and not opt.rawargs then local argsfile = os.tmpfile(args) .. ".args.txt" io.writefile(argsfile, args) diff --git a/xmake/modules/core/tools/cl.lua b/xmake/modules/core/tools/cl.lua index e0b271d88..7284e973b 100644 --- a/xmake/modules/core/tools/cl.lua +++ b/xmake/modules/core/tools/cl.lua @@ -310,7 +310,7 @@ function _compargv_pch(self, pcheaderfile, pcoutputfile, flags) end -- make the compile arguments list - return self:program(), table.join("-c", "-Yc", pchflags, "-Fp" .. pcoutputfile, "-Fo" .. pcoutputfile .. ".obj", pcheaderfile) + return self:program(), table.join("-c", "-Yc", pchflags, "-Fp" .. os.args(pcoutputfile), "-Fo" .. os.args(pcoutputfile .. ".obj"), pcheaderfile) end -- make the compile arguments list @@ -323,7 +323,7 @@ function compargv(self, sourcefile, objectfile, flags) end -- make the compile arguments list - return self:program(), table.join("-c", flags, "-Fo" .. objectfile, sourcefile) + return self:program(), table.join("-c", flags, "-Fo" .. os.args(objectfile), sourcefile) end -- compile the source file diff --git a/xmake/modules/core/tools/link.lua b/xmake/modules/core/tools/link.lua index b45f3c7a9..ccf679c20 100644 --- a/xmake/modules/core/tools/link.lua +++ b/xmake/modules/core/tools/link.lua @@ -94,11 +94,11 @@ end function linkargv(self, objectfiles, targetkind, targetfile, flags, opt) -- init arguments - local argv = table.join(flags, "-out:" .. targetfile, objectfiles) + local argv = table.join(flags, "-out:" .. os.args(targetfile), objectfiles) -- too long arguments for windows? opt = opt or {} - local args = os.args(argv) + local args = os.args(argv, {escape = true}) if #args > 1024 and not opt.rawargs then local argsfile = os.tmpfile(args) .. ".args.txt" io.writefile(argsfile, args) diff --git a/xmake/modules/core/tools/llvm_ar.lua b/xmake/modules/core/tools/llvm_ar.lua index fd5e32333..0d890bd5c 100644 --- a/xmake/modules/core/tools/llvm_ar.lua +++ b/xmake/modules/core/tools/llvm_ar.lua @@ -52,7 +52,7 @@ function linkargv(self, objectfiles, targetkind, targetfile, flags, opt) -- too long arguments for windows? if is_host("windows") then opt = opt or {} - local args = os.args(argv) + local args = os.args(argv, {escape = true}) if #args > 1024 and not opt.rawargs then local argsfile = os.tmpfile(args) .. ".args.txt" io.writefile(argsfile, args) diff --git a/xmake/modules/core/tools/ml.lua b/xmake/modules/core/tools/ml.lua index 24ee3ecab..c1bea35b3 100644 --- a/xmake/modules/core/tools/ml.lua +++ b/xmake/modules/core/tools/ml.lua @@ -92,7 +92,7 @@ end -- make the compile arguments list function _compargv1(self, sourcefile, objectfile, flags) - return self:program(), table.join("-c", flags, "-Fo" .. objectfile, sourcefile) + return self:program(), table.join("-c", flags, "-Fo" .. os.args(objectfile), sourcefile) end -- compile the source file diff --git a/xmake/modules/core/tools/sdar.lua b/xmake/modules/core/tools/sdar.lua index ba991e780..be8cdae6c 100644 --- a/xmake/modules/core/tools/sdar.lua +++ b/xmake/modules/core/tools/sdar.lua @@ -54,7 +54,7 @@ function linkargv(self, objectfiles, targetkind, targetfile, flags, opt) -- too long arguments for windows? if is_host("windows") then opt = opt or {} - local args = os.args(argv) + local args = os.args(argv, {escape = true}) if #args > 1024 and not opt.rawargs then local argsfile = os.tmpfile(args) .. ".args.txt" io.writefile(argsfile, args) diff --git a/xmake/plugins/project/clang/compile_commands.lua b/xmake/plugins/project/clang/compile_commands.lua index 894740411..4ded1e11a 100644 --- a/xmake/plugins/project/clang/compile_commands.lua +++ b/xmake/plugins/project/clang/compile_commands.lua @@ -40,7 +40,7 @@ function _make_object(jsonfile, target, sourcefile, objectfile) -- escape '"', '\' local arguments_escape = {} for _, arg in ipairs(arguments) do - table.insert(arguments_escape, (arg:gsub("[\"\\]", "\\%1"))) + table.insert(arguments_escape, os.args(arg, {escape = true})) end -- make body @@ -49,7 +49,7 @@ function _make_object(jsonfile, target, sourcefile, objectfile) "directory": "%s", "arguments": ["%s"], "file": "%s" -}]], ifelse(_g.firstline, "", ",\n"), os.args(os.projectdir()), table.concat(arguments_escape, "\", \""), os.args(sourcefile)) +}]], (_g.firstline and "" or ",\n"), os.args(os.projectdir(), {escape = true}), table.concat(arguments_escape, "\", \""), os.args(sourcefile, {escape = true})) -- clear first line marks _g.firstline = false |
