From b0dae1a2dd879908ae3a86428f62e903a3c21ce1 Mon Sep 17 00:00:00 2001 From: Steven Watanabe Date: Mon, 3 Aug 2020 10:35:22 -0400 Subject: [PATCH 1/4] Fix dependency output. --- tools/cc/cdt-cpp.cpp.in | 8 ++++---- tools/include/compiler_options.hpp.in | 2 +- tools/include/eosio/codegen.hpp | 23 +++++++++-------------- tools/include/eosio/utils.hpp | 9 +++++++-- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/tools/cc/cdt-cpp.cpp.in b/tools/cc/cdt-cpp.cpp.in index b9b949fecc..e835a45b70 100644 --- a/tools/cc/cdt-cpp.cpp.in +++ b/tools/cc/cdt-cpp.cpp.in @@ -156,12 +156,12 @@ int main(int argc, const char **argv) { new_opts.insert(new_opts.begin(), {"-o", output}); outputs.push_back(output); } - new_opts.insert(new_opts.begin(), input); - if (llvm::sys::path::extension(input).equals(".c")) - new_opts.insert(new_opts.begin(), "-xc++"); + new_opts.insert(new_opts.begin(), "-"); + + new_opts.insert(new_opts.begin(), "-xc++"); - if (!eosio::cdt::environment::exec_subprogram("clang-9", new_opts)) { + if (!eosio::cdt::environment::exec_subprogram("clang-9", new_opts, false, input)) { llvm::sys::fs::remove(tmp_file); return -1; } diff --git a/tools/include/compiler_options.hpp.in b/tools/include/compiler_options.hpp.in index 875ed5c538..b593cc3279 100644 --- a/tools/include/compiler_options.hpp.in +++ b/tools/include/compiler_options.hpp.in @@ -702,7 +702,7 @@ static Options CreateOptions(bool add_defaults=true) { copts.emplace_back("-MMD"); } if (!MT_opt.empty()) { - copts.emplace_back("-MT "+MT_opt); + copts.insert(copts.end(), { "-MT", MT_opt }); } if (finline_functions_opt) { copts.emplace_back("-finline-functions"); diff --git a/tools/include/eosio/codegen.hpp b/tools/include/eosio/codegen.hpp index 54c0c4d3d8..7f68ff759a 100644 --- a/tools/include/eosio/codegen.hpp +++ b/tools/include/eosio/codegen.hpp @@ -88,7 +88,7 @@ namespace eosio { namespace cdt { codegen& cg = codegen::get(); FileID main_fid; StringRef main_name; - Rewriter rewriter; + std::stringstream ss; CompilerInstance* ci; bool apply_was_found = false; @@ -107,7 +107,6 @@ namespace eosio { namespace cdt { : generation_utils(), ci(CI) { cg.ast_context = &(CI->getASTContext()); cg.codegen_ci = CI; - rewriter.setSourceMgr(CI->getASTContext().getSourceManager(), CI->getASTContext().getLangOpts()); get_error_emitter().set_compiler_instance(CI); } @@ -119,9 +118,7 @@ namespace eosio { namespace cdt { main_name = mn; } - Rewriter& get_rewriter() { - return rewriter; - } + auto& get_ss() { return ss; } bool is_datastream(const QualType& qt) { auto str_name = qt.getAsString(); @@ -151,7 +148,6 @@ namespace eosio { namespace cdt { template void create_dispatch(const std::string& attr, const std::string& func_name, F&& get_str, CXXMethodDecl* decl) { constexpr static uint32_t max_stack_size = 512; - std::stringstream ss; codegen& cg = codegen::get(); std::string nm = decl->getNameAsString()+"_"+decl->getParent()->getNameAsString(); if (cg.is_eosio_contract(decl, cg.contract_name)) { @@ -212,7 +208,6 @@ namespace eosio { namespace cdt { } ss << "}}\n"; - rewriter.InsertTextAfter(ci->getSourceManager().getLocForEndOfFile(main_fid), ss.str()); } } @@ -481,9 +476,10 @@ namespace eosio { namespace cdt { llvm::sys::path::system_temp_directory(true, res); std::ofstream out(std::string(res.c_str())+"/"+llvm::sys::path::filename(main_fe->getName()).str()); - for (auto inc : global_includes[main_file]) { - visitor->get_rewriter().ReplaceText(inc.range, - std::string("\"")+inc.file_name+"\"\n"); + { + llvm::SmallString<64> abs_file_path(main_fe->getName()); + llvm::sys::fs::make_absolute(abs_file_path); + out << "#include \"" << abs_file_path.c_str() << "\"\n"; } const auto& quoted = [](const std::string& s) { std::stringstream ss; @@ -496,7 +492,7 @@ namespace eosio { namespace cdt { }; // generate apply stub with abi - std::stringstream ss; + std::stringstream& ss = visitor->get_ss(); ss << "\n"; ss << "extern \"C\" {\n"; ss << "__attribute__((eosio_wasm_import))\n"; @@ -508,9 +504,8 @@ namespace eosio { namespace cdt { ss << "eosio_assert_code(false, 1);"; ss << "}\n"; ss << "}"; - visitor->get_rewriter().InsertTextAfter(ci->getSourceManager().getLocForEndOfFile(fid), ss.str()); - auto& RewriteBuf = visitor->get_rewriter().getEditBuffer(fid); - out << std::string(RewriteBuf.begin(), RewriteBuf.end()); + + out << ss.rdbuf(); cg.tmp_files.emplace(main_file, fn.str()); out.close(); } catch (...) { diff --git a/tools/include/eosio/utils.hpp b/tools/include/eosio/utils.hpp index 5db9acfc80..586ddf5fc5 100644 --- a/tools/include/eosio/utils.hpp +++ b/tools/include/eosio/utils.hpp @@ -132,7 +132,8 @@ struct environment { } return env_table; } - static bool exec_subprogram(const std::string prog, std::vector options, bool root=false) { + static bool exec_subprogram(const std::string prog, std::vector options, bool root=false, + llvm::Optional stdin_file = llvm::None) { std::vector args; args.push_back(prog); args.insert(args.end(), options.begin(), options.end()); @@ -140,7 +141,11 @@ struct environment { if (root) find_path = "/usr/bin"; if ( const auto& path = llvm::sys::findProgramByName(prog.c_str(), {find_path}) ) { - return llvm::sys::ExecuteAndWait(*path, args, {}, {}, 0, 0, nullptr, nullptr) == 0; + std::vector> redirects; + if(stdin_file) { + redirects = { llvm::StringRef{*stdin_file}, llvm::None, llvm::None }; + } + return llvm::sys::ExecuteAndWait(*path, args, {}, redirects, 0, 0, nullptr, nullptr) == 0; } else return false; From 61bace3d3eeefed9c54d240ee0962719d21031db Mon Sep 17 00:00:00 2001 From: Steven Watanabe Date: Tue, 8 Sep 2020 14:43:28 -0400 Subject: [PATCH 2/4] Use proper temporary file names to avoid races. --- tools/cc/cdt-cpp.cpp.in | 33 +++++++++++++++++++++------------ tools/include/eosio/codegen.hpp | 6 ++---- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/tools/cc/cdt-cpp.cpp.in b/tools/cc/cdt-cpp.cpp.in index e835a45b70..4921fc5b9e 100644 --- a/tools/cc/cdt-cpp.cpp.in +++ b/tools/cc/cdt-cpp.cpp.in @@ -126,9 +126,6 @@ int main(int argc, const char **argv) { try { for (auto input : opts.inputs) { std::vector new_opts = opts.comp_options; - SmallString<64> res; - llvm::sys::path::system_temp_directory(true, res); - std::string tmp_file = std::string(res.c_str())+"/"+llvm::sys::path::filename(input).str(); std::string output; if (!opts.pp_only) { @@ -144,28 +141,40 @@ int main(int argc, const char **argv) { std::string source_path = src.str().empty() ? "." : src.str(); new_opts.insert(new_opts.begin(), "-I" + source_path); - if (llvm::sys::fs::exists(tmp_file)) { - input = tmp_file; - } - output = tmp_file+".o"; - if (!opts.link) { output = opts.output_fn.empty() ? "a.out" : opts.output_fn; + } else { + SmallString<64> res; + llvm::sys::fs::createTemporaryFile("eosio", ".o", res); + output = res.c_str(); } new_opts.insert(new_opts.begin(), {"-o", output}); outputs.push_back(output); } - new_opts.insert(new_opts.begin(), "-"); + llvm::Optional stdin_redirect; + llvm::SmallString<64> abs_input(input.c_str()); + llvm::sys::fs::make_absolute(abs_input); + auto file_iter = codegen::get().tmp_files.find(abs_input.c_str()); + if (file_iter != codegen::get().tmp_files.end()) { + stdin_redirect = file_iter->second; + new_opts.insert(new_opts.begin(), "-"); + } else { + new_opts.insert(new_opts.begin(), input); + } new_opts.insert(new_opts.begin(), "-xc++"); - if (!eosio::cdt::environment::exec_subprogram("clang-9", new_opts, false, input)) { - llvm::sys::fs::remove(tmp_file); + if (!eosio::cdt::environment::exec_subprogram("clang-9", new_opts, false, stdin_redirect)) { + if(stdin_redirect) { + llvm::sys::fs::remove(*stdin_redirect); + } return -1; } - llvm::sys::fs::remove(tmp_file); + if(stdin_redirect) { + llvm::sys::fs::remove(*stdin_redirect); + } } } catch (std::runtime_error& err) { llvm::errs() << err.what() << '\n'; diff --git a/tools/include/eosio/codegen.hpp b/tools/include/eosio/codegen.hpp index 7f68ff759a..fad00d9ffc 100644 --- a/tools/include/eosio/codegen.hpp +++ b/tools/include/eosio/codegen.hpp @@ -469,13 +469,11 @@ namespace eosio { namespace cdt { return; } - int fd; llvm::SmallString<128> fn; try { - SmallString<64> res; - llvm::sys::path::system_temp_directory(true, res); + llvm::sys::fs::createTemporaryFile("eosio", ".cpp", fn); - std::ofstream out(std::string(res.c_str())+"/"+llvm::sys::path::filename(main_fe->getName()).str()); + std::ofstream out(fn.c_str()); { llvm::SmallString<64> abs_file_path(main_fe->getName()); llvm::sys::fs::make_absolute(abs_file_path); From aeb707b16be8a8131411f8a7fc6f7f264177dadf Mon Sep 17 00:00:00 2001 From: Steven Watanabe Date: Mon, 30 Nov 2020 08:09:47 -0500 Subject: [PATCH 3/4] Fix default output file. --- tools/include/compiler_options.hpp.in | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/include/compiler_options.hpp.in b/tools/include/compiler_options.hpp.in index b593cc3279..2556101970 100644 --- a/tools/include/compiler_options.hpp.in +++ b/tools/include/compiler_options.hpp.in @@ -870,8 +870,10 @@ static Options CreateOptions(bool add_defaults=true) { replace_extension(fn); output_fn = fn.str(); } else { - ldopts.emplace_back("a.out"); + output_fn = "a.out"; } + ldopts.emplace_back("-o"); + ldopts.emplace_back(output_fn); #else if (inputs.size() == 1) { llvm::SmallString<256> fn = llvm::sys::path::filename(inputs[0]); From 9b1eb599648bbc18deadd3f94e21f42ad2ca70c9 Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Tue, 4 Apr 2023 18:56:18 -0400 Subject: [PATCH 4/4] use `antelope` not `eosio` for temp file prefix --- tools/cc/cdt-cpp.cpp.in | 2 +- tools/include/eosio/codegen.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/cc/cdt-cpp.cpp.in b/tools/cc/cdt-cpp.cpp.in index 4921fc5b9e..568ec9d708 100644 --- a/tools/cc/cdt-cpp.cpp.in +++ b/tools/cc/cdt-cpp.cpp.in @@ -145,7 +145,7 @@ int main(int argc, const char **argv) { output = opts.output_fn.empty() ? "a.out" : opts.output_fn; } else { SmallString<64> res; - llvm::sys::fs::createTemporaryFile("eosio", ".o", res); + llvm::sys::fs::createTemporaryFile("antelope", ".o", res); output = res.c_str(); } diff --git a/tools/include/eosio/codegen.hpp b/tools/include/eosio/codegen.hpp index fad00d9ffc..4d1c4adbe9 100644 --- a/tools/include/eosio/codegen.hpp +++ b/tools/include/eosio/codegen.hpp @@ -471,7 +471,7 @@ namespace eosio { namespace cdt { llvm::SmallString<128> fn; try { - llvm::sys::fs::createTemporaryFile("eosio", ".cpp", fn); + llvm::sys::fs::createTemporaryFile("antelope", ".cpp", fn); std::ofstream out(fn.c_str()); {