diff options
author | John Hodge <tpg@mutabah.net> | 2019-10-26 17:15:23 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2019-10-26 17:15:23 +0800 |
commit | 99904636d2e1e335238552cbb5b8b23dd06b7295 (patch) | |
tree | 5fd6470fb702b2a26253d380d47d5b6fd232607f /tools/minicargo/build.cpp | |
parent | 7ca04e2d668fa44a1663158f844cb912e396cb32 (diff) | |
parent | ebd8edeb4f1861943cc82d310564b1f592e63272 (diff) | |
download | mrust-99904636d2e1e335238552cbb5b8b23dd06b7295.tar.gz |
Merge remote-tracking branch 'remotes/origin/master' into nightly-1.29
Diffstat (limited to 'tools/minicargo/build.cpp')
-rw-r--r-- | tools/minicargo/build.cpp | 76 |
1 files changed, 54 insertions, 22 deletions
diff --git a/tools/minicargo/build.cpp b/tools/minicargo/build.cpp index 04a9a507..5eb07104 100644 --- a/tools/minicargo/build.cpp +++ b/tools/minicargo/build.cpp @@ -31,6 +31,7 @@ extern int _putenv_s(const char*, const char*); # include <mutex> # include <condition_variable> #endif +#include <fstream> #include <climits> #include <cassert> #ifdef _WIN32 @@ -67,6 +68,9 @@ class Builder ::helpers::path m_compiler_path; size_t m_total_targets; mutable size_t m_targets_built; +#ifndef _WIN32 + mutable ::std::mutex chdir_mutex; +#endif public: Builder(const BuildOptions& opts, size_t total_targets); @@ -78,7 +82,7 @@ public: private: ::helpers::path get_crate_path(const PackageManifest& manifest, const PackageTarget& target, bool is_for_host, const char** crate_type, ::std::string* out_crate_suffix) const; bool spawn_process_mrustc(const StringList& args, StringListKV env, const ::helpers::path& logfile) const; - bool spawn_process(const char* exe_name, const StringList& args, const StringListKV& env, const ::helpers::path& logfile) const; + bool spawn_process(const char* exe_name, const StringList& args, const StringListKV& env, const ::helpers::path& logfile, const ::helpers::path& working_directory={}) const; ::helpers::path build_and_run_script(const PackageManifest& manifest, bool is_for_host) const; @@ -566,6 +570,11 @@ Builder::Builder(const BuildOptions& opts, size_t total_targets): m_total_targets(total_targets), m_targets_built(0) { + if( const char* override_path = getenv("MRUSTC_PATH") ) { + m_compiler_path = override_path; + return ; + } + // TODO: Clean this stuff up #ifdef _WIN32 char buf[1024]; size_t s = GetModuleFileName(NULL, buf, sizeof(buf)-1); @@ -1000,6 +1009,10 @@ bool Builder::build_target(const PackageManifest& manifest, const PackageTarget& args.push_back("--crate-type"); args.push_back("bin"); args.push_back("-o"); args.push_back(outfile); args.push_back("-L"); args.push_back(this->get_output_dir(true).str()); // NOTE: Forces `is_for_host` to true here. + if( true ) + { + args.push_back("-g"); + } for(const auto& d : m_opts.lib_search_dirs) { args.push_back("-L"); @@ -1102,23 +1115,31 @@ bool Builder::build_target(const PackageManifest& manifest, const PackageTarget& } //auto _ = ScopedChdir { manifest.directory() }; - #if _WIN32 - #else - auto fd_cwd = open(".", O_DIRECTORY); - chdir(manifest.directory().str().c_str()); - #endif - if( !this->spawn_process(script_exe_abs.str().c_str(), {}, env, out_file) ) + if( !this->spawn_process(script_exe_abs.str().c_str(), {}, env, out_file, /*working_directory=*/manifest.directory()) ) { - rename(out_file.str().c_str(), (out_file+"_failed").str().c_str()); + auto failed_filename = out_file+"_failed.txt"; + remove(failed_filename.str().c_str()); + rename(out_file.str().c_str(), failed_filename.str().c_str()); + + ::std::cerr << "Calling " << script_exe_abs << " failed" << ::std::endl; + { + ::std::ifstream ifs(failed_filename); + char linebuf[1024]; + while( ifs.good() && !ifs.eof() ) + { + ifs.getline(linebuf, sizeof(linebuf)-1); + if( strncmp(linebuf, "cargo:", 6) == 0 ) { + continue; + } + ::std::cerr << "> " << linebuf << ::std::endl; + } + } + // Build failed, return an invalid path - return ::helpers::path();; + return ::helpers::path(); } - #if _WIN32 - #else - fchdir(fd_cwd); - #endif } - + return out_file; } bool Builder::build_library(const PackageManifest& manifest, bool is_for_host, size_t index) const @@ -1154,7 +1175,7 @@ bool Builder::spawn_process_mrustc(const StringList& args, StringListKV env, con //env.push_back("MRUSTC_DEBUG", ""); return spawn_process(m_compiler_path.str().c_str(), args, env, logfile); } -bool Builder::spawn_process(const char* exe_name, const StringList& args, const StringListKV& env, const ::helpers::path& logfile) const +bool Builder::spawn_process(const char* exe_name, const StringList& args, const StringListKV& env, const ::helpers::path& logfile, const ::helpers::path& working_directory/*={}*/) const { #ifdef _WIN32 ::std::stringstream cmdline; @@ -1206,14 +1227,14 @@ bool Builder::spawn_process(const char* exe_name, const StringList& args, const WriteFile(si.hStdOutput, "\n", 1, &tmp, NULL); } PROCESS_INFORMATION pi = { 0 }; - CreateProcessA(exe_name, (LPSTR)cmdline_str.c_str(), NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi); + CreateProcessA(exe_name, (LPSTR)cmdline_str.c_str(), NULL, NULL, TRUE, 0, NULL, (working_directory != ::helpers::path() ? working_directory.str().c_str() : NULL), &si, &pi); CloseHandle(si.hStdOutput); WaitForSingleObject(pi.hProcess, INFINITE); DWORD status = 1; GetExitCodeProcess(pi.hProcess, &status); if (status != 0) { - DEBUG("Compiler exited with non-zero exit status " << status); + DEBUG("Process exited with non-zero exit status " << status); return false; } #else @@ -1270,12 +1291,23 @@ bool Builder::spawn_process(const char* exe_name, const StringList& args, const // }); envp.push_back(nullptr); - if( posix_spawn(&pid, exe_name, &fa, /*attr=*/nullptr, (char* const*)argv.data(), (char* const*)envp.get_vec().data()) != 0 ) + // TODO: Acquire a lock { - ::std::cerr << "Unable to run process '" << exe_name << "' - " << strerror(errno) << ::std::endl; - DEBUG("Unable to spawn executable"); - posix_spawn_file_actions_destroy(&fa); - return false; + ::std::lock_guard<::std::mutex> lh { this->chdir_mutex }; + auto fd_cwd = open(".", O_DIRECTORY); + if( working_directory != ::helpers::path() ) { + chdir(working_directory.str().c_str()); + } + if( posix_spawn(&pid, exe_name, &fa, /*attr=*/nullptr, (char* const*)argv.data(), (char* const*)envp.get_vec().data()) != 0 ) + { + ::std::cerr << "Unable to run process '" << exe_name << "' - " << strerror(errno) << ::std::endl; + DEBUG("Unable to spawn executable"); + posix_spawn_file_actions_destroy(&fa); + return false; + } + if( working_directory != ::helpers::path() ) { + fchdir(fd_cwd); + } } posix_spawn_file_actions_destroy(&fa); int status = -1; |