Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicate exe name with zig run on Windows #2665

Merged
merged 1 commit into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8801,6 +8801,7 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {

Termination term;
ZigList<const char *> args = {};
args.append(buf_ptr(self_exe_path));
args.append("cc");

Buf *out_dep_path = buf_sprintf("%s.d", buf_ptr(out_obj_path));
Expand All @@ -8819,7 +8820,7 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
if (g->verbose_cc) {
print_zig_cc_cmd("zig", &args);
}
os_spawn_process(buf_ptr(self_exe_path), args, &term);
os_spawn_process(args, &term);
if (term.how != TerminationIdClean || term.code != 0) {
fprintf(stderr, "\nThe following command failed:\n");
print_zig_cc_cmd(buf_ptr(self_exe_path), &args);
Expand Down
6 changes: 4 additions & 2 deletions src/libc_installation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, b
const char *cc_exe = getenv("CC");
cc_exe = (cc_exe == nullptr) ? CC_EXE : cc_exe;
ZigList<const char *> args = {};
args.append(cc_exe);
args.append("-E");
args.append("-Wp,-v");
args.append("-xc");
Expand All @@ -166,7 +167,7 @@ static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, b
Buf *out_stderr = buf_alloc();
Buf *out_stdout = buf_alloc();
Error err;
if ((err = os_exec_process(cc_exe, args, &term, out_stderr, out_stdout))) {
if ((err = os_exec_process(args, &term, out_stderr, out_stdout))) {
if (verbose) {
fprintf(stderr, "unable to determine libc include path: executing '%s': %s\n", cc_exe, err_str(err));
}
Expand Down Expand Up @@ -277,12 +278,13 @@ Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirnam
const char *cc_exe = getenv("CC");
cc_exe = (cc_exe == nullptr) ? CC_EXE : cc_exe;
ZigList<const char *> args = {};
args.append(cc_exe);
args.append(buf_ptr(buf_sprintf("-print-file-name=%s", o_file)));
Termination term;
Buf *out_stderr = buf_alloc();
Buf *out_stdout = buf_alloc();
Error err;
if ((err = os_exec_process(cc_exe, args, &term, out_stderr, out_stdout))) {
if ((err = os_exec_process(args, &term, out_stderr, out_stdout))) {
if (err == ErrorFileNotFound)
return ErrorNoCCompilerInstalled;
if (verbose) {
Expand Down
3 changes: 2 additions & 1 deletion src/link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1721,10 +1721,11 @@ void codegen_link(CodeGen *g) {
if (g->system_linker_hack && g->zig_target->os == OsMacOSX) {
Termination term;
ZigList<const char *> args = {};
args.append("ld");
for (size_t i = 1; i < lj.args.length; i += 1) {
args.append(lj.args.at(i));
}
os_spawn_process("ld", args, &term);
os_spawn_process(args, &term);
if (term.how != TerminationIdClean || term.code != 0) {
exit(1);
}
Expand Down
28 changes: 12 additions & 16 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ int main(int argc, char **argv) {
init_all_targets();

ZigList<const char *> args = {0};
args.append(NULL); // placeholder
args.append(zig_exe_path);
args.append(NULL); // placeholder
args.append(NULL); // placeholder
Expand Down Expand Up @@ -525,8 +526,8 @@ int main(int argc, char **argv) {
g->enable_time_report = timing_info;
codegen_set_out_name(g, buf_create_from_str("build"));

args.items[1] = buf_ptr(&build_file_dirname);
args.items[2] = buf_ptr(&full_cache_dir);
args.items[2] = buf_ptr(&build_file_dirname);
args.items[3] = buf_ptr(&full_cache_dir);

bool build_file_exists;
if ((err = os_file_exists(&build_file_abs, &build_file_exists))) {
Expand Down Expand Up @@ -580,12 +581,14 @@ int main(int argc, char **argv) {
codegen_build_and_link(g);

Termination term;
os_spawn_process(buf_ptr(&g->output_file_path), args, &term);
args.items[0] = buf_ptr(&g->output_file_path);
os_spawn_process(args, &term);
if (term.how != TerminationIdClean || term.code != 0) {
fprintf(stderr, "\nBuild failed. The following command failed:\n");
fprintf(stderr, "%s", buf_ptr(&g->output_file_path));
const char *prefix = "";
for (size_t i = 0; i < args.length; i += 1) {
fprintf(stderr, " %s", args.at(i));
fprintf(stderr, "%s%s", prefix, args.at(i));
prefix = " ";
}
fprintf(stderr, "\n");
}
Expand Down Expand Up @@ -1161,7 +1164,7 @@ int main(int argc, char **argv) {

args.pop();
Termination term;
os_spawn_process(exec_path, args, &term);
os_spawn_process(args, &term);
return term.code;
} else if (cmd == CmdBuild) {
if (g->enable_cache) {
Expand Down Expand Up @@ -1213,17 +1216,10 @@ int main(int argc, char **argv) {
}

Termination term;
if (test_exec_args.length > 0) {
ZigList<const char *> rest_args = {0};
for (size_t i = 1; i < test_exec_args.length; i += 1) {
rest_args.append(test_exec_args.at(i));
}
os_spawn_process(test_exec_args.items[0], rest_args, &term);
} else {
ZigList<const char *> no_args = {0};
os_spawn_process(buf_ptr(test_exe_path), no_args, &term);
if (test_exec_args.length == 0) {
test_exec_args.append(buf_ptr(test_exe_path));
}

os_spawn_process(test_exec_args, &term);
if (term.how != TerminationIdClean || term.code != 0) {
fprintf(stderr, "\nTests failed. Use the following command to reproduce the failure:\n");
fprintf(stderr, "%s\n", buf_ptr(test_exe_path));
Expand Down
56 changes: 27 additions & 29 deletions src/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,15 @@ static void populate_termination(Termination *term, int status) {
}
}

static void os_spawn_process_posix(const char *exe, ZigList<const char *> &args, Termination *term) {
const char **argv = allocate<const char *>(args.length + 2);
argv[0] = exe;
argv[args.length + 1] = nullptr;
static void os_spawn_process_posix(ZigList<const char *> &args, Termination *term) {
const char **argv = allocate<const char *>(args.length + 1);
for (size_t i = 0; i < args.length; i += 1) {
argv[i + 1] = args.at(i);
argv[i] = args.at(i);
}
argv[args.length] = nullptr;

pid_t pid;
int rc = posix_spawnp(&pid, exe, nullptr, nullptr, const_cast<char *const*>(argv), environ);
int rc = posix_spawnp(&pid, args.at(0), nullptr, nullptr, const_cast<char *const*>(argv), environ);
if (rc != 0) {
zig_panic("posix_spawn failed: %s", strerror(rc));
}
Expand All @@ -126,16 +125,14 @@ static void os_spawn_process_posix(const char *exe, ZigList<const char *> &args,
#endif

#if defined(ZIG_OS_WINDOWS)
static void os_windows_create_command_line(Buf *command_line, const char *exe, ZigList<const char *> &args) {
buf_resize(command_line, 0);

buf_append_char(command_line, '\"');
buf_append_str(command_line, exe);
buf_append_char(command_line, '\"');

static void os_windows_create_command_line(Buf *command_line, ZigList<const char *> &args) {
buf_resize(command_line, 0);
char *prefix = "\"";
for (size_t arg_i = 0; arg_i < args.length; arg_i += 1) {
buf_append_str(command_line, " \"");
const char *arg = args.at(arg_i);
buf_append_str(command_line, prefix);
prefix = " \"";
size_t arg_len = strlen(arg);
for (size_t c_i = 0; c_i < arg_len; c_i += 1) {
if (arg[c_i] == '\"') {
Expand All @@ -147,14 +144,15 @@ static void os_windows_create_command_line(Buf *command_line, const char *exe, Z
}
}

static void os_spawn_process_windows(const char *exe, ZigList<const char *> &args, Termination *term) {
static void os_spawn_process_windows(ZigList<const char *> &args, Termination *term) {
Buf command_line = BUF_INIT;
os_windows_create_command_line(&command_line, exe, args);
os_windows_create_command_line(&command_line, args);

PROCESS_INFORMATION piProcInfo = {0};
STARTUPINFO siStartInfo = {0};
siStartInfo.cb = sizeof(STARTUPINFO);

const char *exe = args.at(0);
BOOL success = CreateProcessA(exe, buf_ptr(&command_line), nullptr, nullptr, TRUE, 0, nullptr, nullptr,
&siStartInfo, &piProcInfo);

Expand All @@ -173,11 +171,11 @@ static void os_spawn_process_windows(const char *exe, ZigList<const char *> &arg
}
#endif

void os_spawn_process(const char *exe, ZigList<const char *> &args, Termination *term) {
void os_spawn_process(ZigList<const char *> &args, Termination *term) {
#if defined(ZIG_OS_WINDOWS)
os_spawn_process_windows(exe, args, term);
os_spawn_process_windows(args, term);
#elif defined(ZIG_OS_POSIX)
os_spawn_process_posix(exe, args, term);
os_spawn_process_posix(args, term);
#else
#error "missing os_spawn_process implementation"
#endif
Expand Down Expand Up @@ -785,7 +783,7 @@ Error os_file_exists(Buf *full_path, bool *result) {
}

#if defined(ZIG_OS_POSIX)
static Error os_exec_process_posix(const char *exe, ZigList<const char *> &args,
static Error os_exec_process_posix(ZigList<const char *> &args,
Termination *term, Buf *out_stderr, Buf *out_stdout)
{
int stdin_pipe[2];
Expand Down Expand Up @@ -817,13 +815,12 @@ static Error os_exec_process_posix(const char *exe, ZigList<const char *> &args,
if (dup2(stderr_pipe[1], STDERR_FILENO) == -1)
zig_panic("dup2 failed");

const char **argv = allocate<const char *>(args.length + 2);
argv[0] = exe;
argv[args.length + 1] = nullptr;
const char **argv = allocate<const char *>(args.length + 1);
argv[args.length] = nullptr;
for (size_t i = 0; i < args.length; i += 1) {
argv[i + 1] = args.at(i);
argv[i] = args.at(i);
}
execvp(exe, const_cast<char * const *>(argv));
execvp(argv[0], const_cast<char * const *>(argv));
Error report_err = ErrorUnexpected;
if (errno == ENOENT) {
report_err = ErrorFileNotFound;
Expand Down Expand Up @@ -874,11 +871,11 @@ static Error os_exec_process_posix(const char *exe, ZigList<const char *> &args,
// LocalFree(messageBuffer);
//}

static Error os_exec_process_windows(const char *exe, ZigList<const char *> &args,
static Error os_exec_process_windows(ZigList<const char *> &args,
Termination *term, Buf *out_stderr, Buf *out_stdout)
{
Buf command_line = BUF_INIT;
os_windows_create_command_line(&command_line, exe, args);
os_windows_create_command_line(&command_line, args);

HANDLE g_hChildStd_IN_Rd = NULL;
HANDLE g_hChildStd_IN_Wr = NULL;
Expand Down Expand Up @@ -925,6 +922,7 @@ static Error os_exec_process_windows(const char *exe, ZigList<const char *> &arg
siStartInfo.hStdInput = g_hChildStd_IN_Rd;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;

const char *exe = args.at(0);
BOOL success = CreateProcess(exe, buf_ptr(&command_line), nullptr, nullptr, TRUE, 0, nullptr, nullptr,
&siStartInfo, &piProcInfo);

Expand Down Expand Up @@ -1005,13 +1003,13 @@ Error os_execv(const char *exe, const char **argv) {
#endif
}

Error os_exec_process(const char *exe, ZigList<const char *> &args,
Error os_exec_process(ZigList<const char *> &args,
Termination *term, Buf *out_stderr, Buf *out_stdout)
{
#if defined(ZIG_OS_WINDOWS)
return os_exec_process_windows(exe, args, term, out_stderr, out_stdout);
return os_exec_process_windows(args, term, out_stderr, out_stdout);
#elif defined(ZIG_OS_POSIX)
return os_exec_process_posix(exe, args, term, out_stderr, out_stdout);
return os_exec_process_posix(args, term, out_stderr, out_stdout);
#else
#error "missing os_exec_process implementation"
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/os.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ struct OsFileAttr {

int os_init(void);

void os_spawn_process(const char *exe, ZigList<const char *> &args, Termination *term);
Error os_exec_process(const char *exe, ZigList<const char *> &args,
void os_spawn_process(ZigList<const char *> &args, Termination *term);
Error os_exec_process(ZigList<const char *> &args,
Termination *term, Buf *out_stderr, Buf *out_stdout);
Error os_execv(const char *exe, const char **argv);

Expand Down