Skip to content

Commit

Permalink
Merge pull request #542 from xmake-io/vs_unicode
Browse files Browse the repository at this point in the history
Improve vs unicode output for link link/cl
  • Loading branch information
waruqi authored Aug 21, 2019
2 parents a5def1e + 7ce304f commit 2c891f5
Show file tree
Hide file tree
Showing 12 changed files with 269 additions and 211 deletions.
3 changes: 1 addition & 2 deletions core/src/xmake/io/file_close.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ tb_int_t xm_io_file_close(lua_State* lua)
#endif

// close file
if (!tb_stream_clos(file->file_ref))
xm_io_file_return_error(lua, "failed to close file");
tb_stream_clos(file->file_ref);
file->file_ref = tb_null;

// exit fstream
Expand Down
20 changes: 0 additions & 20 deletions core/src/xmake/process/close.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,6 @@ tb_int_t xm_process_close(lua_State* lua)
tb_process_ref_t process = (tb_process_ref_t)lua_touserdata(lua, 1);
tb_check_return_val(process, 0);

// exit subprocess
xm_subprocess_t* subprocess = (xm_subprocess_t*)tb_process_priv(process);
if (subprocess)
{
if (subprocess->outtype == TB_PROCESS_REDIRECT_TYPE_FILE && subprocess->outfile)
{
tb_file_exit(subprocess->outfile);
subprocess->outfile = tb_null;
}

if (subprocess->errtype == TB_PROCESS_REDIRECT_TYPE_FILE && subprocess->errfile)
{
tb_file_exit(subprocess->errfile);
subprocess->errfile = tb_null;
}

tb_string_exit(&subprocess->vs_unicode_output);
tb_free(subprocess);
}

// exit process
tb_process_exit(process);

Expand Down
116 changes: 50 additions & 66 deletions core/src/xmake/process/open.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* includes
*/
#include "prefix.h"
#include "../io/prefix.h"

/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
Expand All @@ -53,7 +54,8 @@ tb_int_t xm_process_open(lua_State* lua)
tb_char_t const* envs[256] = {0};
tb_char_t const* outpath = tb_null;
tb_char_t const* errpath = tb_null;
tb_bool_t vs_unicode_output = tb_false;
xm_io_file_t* outfile = tb_null;
xm_io_file_t* errfile = tb_null;
if (lua_istable(lua, 2))
{
// get outpath
Expand All @@ -68,77 +70,24 @@ tb_int_t xm_process_open(lua_State* lua)
errpath = lua_tostring(lua, -1);
lua_pop(lua, 1);

// enable vs_unicode_output?
lua_pushstring(lua, "vs_unicode_output");
lua_gettable(lua, 2);
vs_unicode_output = lua_toboolean(lua, -1);
lua_pop(lua, 1);
}

// enable vs_unicode_output? @see https://github.com/xmake-io/xmake/issues/528
if (vs_unicode_output)
{
xm_subprocess_t* subprocess = tb_malloc0_type(xm_subprocess_t);
if (subprocess)
{
// init vs_unicode_output
tb_string_init(&subprocess->vs_unicode_output);

// redirect stdout?
if (outpath)
{
// redirect stdout to file
subprocess->outfile = tb_file_init(outpath, TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT);
subprocess->outtype = TB_PROCESS_REDIRECT_TYPE_FILE;
attr.outfile = subprocess->outfile;
attr.outtype = subprocess->outtype;

#ifdef TB_CONFIG_OS_WINDOWS
/* add environment value of vs_unicode_output
*
* @note we have to set it at the beginning, because opt.envs might also have this value.
*/
if (envn + 1 < tb_arrayn(envs))
envs[envn++] = tb_string_cstrfcpy(&subprocess->vs_unicode_output, "VS_UNICODE_OUTPUT=%zu", (tb_size_t)subprocess->outfile);
#endif
}

// redirect stderr?
if (errpath)
{
// redirect stderr to file
subprocess->errfile = tb_file_init(errpath, TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT);
subprocess->errtype = TB_PROCESS_REDIRECT_TYPE_FILE;
attr.errfile = subprocess->errfile;
attr.errtype = subprocess->errtype;
}
attr.priv = subprocess;
}
}
else
{
// redirect stdout?
if (outpath)
// get outfile
if (!outpath)
{
// redirect stdout to file
attr.outpath = outpath;
attr.outmode = TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT;
attr.outtype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
lua_pushstring(lua, "outfile");
lua_gettable(lua, 3);
outfile = (xm_io_file_t*)lua_touserdata(lua, -1);
lua_pop(lua, 1);
}

// redirect stderr?
if (errpath)
// get errfile
if (!errpath)
{
// redirect stderr to file
attr.errpath = errpath;
attr.errmode = TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT;
attr.errtype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
lua_pushstring(lua, "errfile");
lua_gettable(lua, 3);
errfile = (xm_io_file_t*)lua_touserdata(lua, -1);
lua_pop(lua, 1);
}
}

// append other environments after setting VS_UNICODE_OUTPUT
if (lua_istable(lua, 2))
{
// get environments
lua_pushstring(lua, "envs");
lua_gettable(lua, 2);
Expand Down Expand Up @@ -182,6 +131,41 @@ tb_int_t xm_process_open(lua_State* lua)
lua_pop(lua, 1);
}

// redirect stdout?
if (outpath)
{
// redirect stdout to file
attr.outpath = outpath;
attr.outmode = TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT;
attr.outtype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
}
else if (outfile && xm_io_file_is_file(outfile))
{
tb_file_ref_t rawfile = tb_null;
if (tb_stream_ctrl(outfile->stream, TB_STREAM_CTRL_FILE_GET_FILE, &rawfile) && rawfile)
{
attr.outfile = rawfile;
attr.outtype = TB_PROCESS_REDIRECT_TYPE_FILE;
}
}

// redirect stderr?
if (errpath)
{
// redirect stderr to file
attr.errpath = errpath;
attr.errmode = TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT;
attr.errtype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
}
else if (errfile && xm_io_file_is_file(errfile))
{
tb_file_ref_t rawfile = tb_null;
if (tb_stream_ctrl(errfile->stream, TB_STREAM_CTRL_FILE_GET_FILE, &rawfile) && rawfile)
{
attr.errfile = rawfile;
attr.errtype = TB_PROCESS_REDIRECT_TYPE_FILE;
}
}

// set the new environments
if (envn > 0) attr.envp = envs;
Expand Down
119 changes: 52 additions & 67 deletions core/src/xmake/process/openv.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
* includes
*/
#include "prefix.h"
#include "../io/prefix.h"

/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/

// p = process.openv(shellname, argv, {outpath = "", errpath = "", envs = {"PATH=xxx", "XXX=yyy"})
// p = process.openv(shellname, argv, {outpath = "", errpath = "", outfile = , errfile = , envs = {"PATH=xxx", "XXX=yyy"})
tb_int_t xm_process_openv(lua_State* lua)
{
// check
Expand Down Expand Up @@ -95,7 +96,8 @@ tb_int_t xm_process_openv(lua_State* lua)
tb_char_t const* envs[256] = {0};
tb_char_t const* outpath = tb_null;
tb_char_t const* errpath = tb_null;
tb_bool_t vs_unicode_output = tb_false;
xm_io_file_t* outfile = tb_null;
xm_io_file_t* errfile = tb_null;
if (lua_istable(lua, 3))
{
// get outpath
Expand All @@ -110,77 +112,24 @@ tb_int_t xm_process_openv(lua_State* lua)
errpath = lua_tostring(lua, -1);
lua_pop(lua, 1);

// enable vs_unicode_output?
lua_pushstring(lua, "vs_unicode_output");
lua_gettable(lua, 3);
vs_unicode_output = lua_toboolean(lua, -1);
lua_pop(lua, 1);
}

// enable vs_unicode_output? @see https://github.com/xmake-io/xmake/issues/528
if (vs_unicode_output)
{
xm_subprocess_t* subprocess = tb_malloc0_type(xm_subprocess_t);
if (subprocess)
// get outfile
if (!outpath)
{
// init vs_unicode_output
tb_string_init(&subprocess->vs_unicode_output);

// redirect stdout?
if (outpath)
{
// redirect stdout to file
subprocess->outfile = tb_file_init(outpath, TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT);
subprocess->outtype = TB_PROCESS_REDIRECT_TYPE_FILE;
attr.outfile = subprocess->outfile;
attr.outtype = subprocess->outtype;

#ifdef TB_CONFIG_OS_WINDOWS
/* add environment value of vs_unicode_output
*
* @note we have to set it at the beginning, because opt.envs might also have this value.
*/
if (envn + 1 < tb_arrayn(envs))
envs[envn++] = tb_string_cstrfcpy(&subprocess->vs_unicode_output, "VS_UNICODE_OUTPUT=%zu", (tb_size_t)subprocess->outfile);
#endif
}

// redirect stderr?
if (errpath)
{
// redirect stderr to file
subprocess->errfile = tb_file_init(errpath, TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT);
subprocess->errtype = TB_PROCESS_REDIRECT_TYPE_FILE;
attr.errfile = subprocess->errfile;
attr.errtype = subprocess->errtype;
}
attr.priv = subprocess;
}
}
else
{
// redirect stdout?
if (outpath)
{
// redirect stdout to file
attr.outpath = outpath;
attr.outmode = TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT;
attr.outtype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
lua_pushstring(lua, "outfile");
lua_gettable(lua, 3);
outfile = (xm_io_file_t*)lua_touserdata(lua, -1);
lua_pop(lua, 1);
}

// redirect stderr?
if (errpath)
// get errfile
if (!errpath)
{
// redirect stderr to file
attr.errpath = errpath;
attr.errmode = TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT;
attr.errtype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
lua_pushstring(lua, "errfile");
lua_gettable(lua, 3);
errfile = (xm_io_file_t*)lua_touserdata(lua, -1);
lua_pop(lua, 1);
}
}

// append other environments after setting VS_UNICODE_OUTPUT
if (lua_istable(lua, 3))
{
// get environments
lua_pushstring(lua, "envs");
lua_gettable(lua, 3);
Expand Down Expand Up @@ -223,6 +172,42 @@ tb_int_t xm_process_openv(lua_State* lua)
}
lua_pop(lua, 1);
}

// redirect stdout?
if (outpath)
{
// redirect stdout to file
attr.outpath = outpath;
attr.outmode = TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT;
attr.outtype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
}
else if (outfile && xm_io_file_is_file(outfile))
{
tb_file_ref_t rawfile = tb_null;
if (tb_stream_ctrl(outfile->stream, TB_STREAM_CTRL_FILE_GET_FILE, &rawfile) && rawfile)
{
attr.outfile = rawfile;
attr.outtype = TB_PROCESS_REDIRECT_TYPE_FILE;
}
}

// redirect stderr?
if (errpath)
{
// redirect stderr to file
attr.errpath = errpath;
attr.errmode = TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT;
attr.errtype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
}
else if (errfile && xm_io_file_is_file(errfile))
{
tb_file_ref_t rawfile = tb_null;
if (tb_stream_ctrl(errfile->stream, TB_STREAM_CTRL_FILE_GET_FILE, &rawfile) && rawfile)
{
attr.errfile = rawfile;
attr.errtype = TB_PROCESS_REDIRECT_TYPE_FILE;
}
}

// set the new environments
if (envn > 0) attr.envp = envs;
Expand Down
37 changes: 0 additions & 37 deletions core/src/xmake/process/prefix.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,6 @@
*/
#include "../prefix.h"

/* //////////////////////////////////////////////////////////////////////////////////////
* types
*/

// the subprocess type
typedef struct __xm_subprocess_t
{
/// the stdout redirect type
tb_uint16_t outtype;

/// the stderr redirect type
tb_uint16_t errtype;

union
{
/// the stdout pipe
tb_pipe_file_ref_t outpipe;

/// the stdout file
tb_file_ref_t outfile;
};

union
{
/// the strerr pipe
tb_pipe_file_ref_t errpipe;

/// the stderr file
tb_file_ref_t errfile;
};

// vs unicode output environment variable
tb_string_t vs_unicode_output;

}xm_subprocess_t;


#endif


Loading

0 comments on commit 2c891f5

Please sign in to comment.