From 04d71730755f636ec24795e214ddd70fb4bf068b Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Thu, 14 Sep 2023 22:25:19 -0400 Subject: [PATCH 1/2] fix -fnative linking on macos --- tools/include/compiler_options.hpp.in | 20 +++++++++++++++++++- tools/include/eosio/utils.hpp | 12 ++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/tools/include/compiler_options.hpp.in b/tools/include/compiler_options.hpp.in index 2556101970..d4cd380da1 100644 --- a/tools/include/compiler_options.hpp.in +++ b/tools/include/compiler_options.hpp.in @@ -6,6 +6,7 @@ #include #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" +#include "llvm/ADT/ScopeExit.h" #ifdef ONLY_LD #define LD_CAT EosioLdToolCategory @@ -546,6 +547,23 @@ static void GetLdDefaults(std::vector& ldopts) { } else { #ifdef __APPLE__ ldopts.insert(ldopts.end(), {"-arch", "x86_64", "-macosx_version_min", "10.13", "-framework", "Foundation", "-framework", "System"}); + + llvm::SmallString<256> Output; + if(llvm::sys::fs::createTemporaryFile("cdtsdkroot", ".path", Output)) + throw std::runtime_error("Failed to create tmp file"); + auto DeleteOutput = llvm::make_scope_exit([&](){ llvm::sys::fs::remove(Output); }); + + if(!eosio::cdt::environment::exec_subprogram("xcrun", {"--show-sdk-path"}, true, llvm::None, {Output.str()})) + throw std::runtime_error("Failed to run xcrun --show-sdk-path"); + + auto Buf = llvm::MemoryBuffer::getFile(Output); + if(!Buf) + throw std::runtime_error("Failed to open tmp file"); + + std::string sdkpath = Buf.get()->getBuffer(); + //remove newline + sdkpath.resize(sdkpath.size()-1); + ldopts.insert(ldopts.end(), {"-syslibroot", sdkpath}); #endif if (fshared_opt) { ldopts.emplace_back("-shared"); @@ -553,7 +571,7 @@ static void GetLdDefaults(std::vector& ldopts) { else { ldopts.emplace_back("-static"); } - ldopts.insert(ldopts.end(), {"-Bstatic", "-lnative_c++", "-lnative_c", "-lnative_eosio", "-lnative", "-lnative_rt"}); + ldopts.insert(ldopts.end(), {"-lnative_c++", "-lnative_c", "-lnative_eosio", "-lnative", "-lnative_rt"}); } } #endif diff --git a/tools/include/eosio/utils.hpp b/tools/include/eosio/utils.hpp index 586ddf5fc5..3aa29b8b1d 100644 --- a/tools/include/eosio/utils.hpp +++ b/tools/include/eosio/utils.hpp @@ -133,7 +133,8 @@ struct environment { return env_table; } static bool exec_subprogram(const std::string prog, std::vector options, bool root=false, - llvm::Optional stdin_file = llvm::None) { + llvm::Optional stdin_file = llvm::None, + llvm::Optional stdout_file = llvm::None) { std::vector args; args.push_back(prog); args.insert(args.end(), options.begin(), options.end()); @@ -142,9 +143,12 @@ struct environment { find_path = "/usr/bin"; if ( const auto& path = llvm::sys::findProgramByName(prog.c_str(), {find_path}) ) { std::vector> redirects; - if(stdin_file) { - redirects = { llvm::StringRef{*stdin_file}, llvm::None, llvm::None }; - } + if(stdin_file || stdout_file) + redirects = { llvm::None, llvm::None, llvm::None }; + if(stdin_file) + redirects[0] = llvm::StringRef{*stdin_file}; + if(stdout_file) + redirects[1] = llvm::StringRef{*stdout_file}; return llvm::sys::ExecuteAndWait(*path, args, {}, redirects, 0, 0, nullptr, nullptr) == 0; } else From b1d9be8ada9ef526bbfaffb55b0e460e947034d2 Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Tue, 19 Sep 2023 17:04:04 -0400 Subject: [PATCH 2/2] tweaks to variable names --- tools/include/compiler_options.hpp.in | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tools/include/compiler_options.hpp.in b/tools/include/compiler_options.hpp.in index d4cd380da1..038e52f911 100644 --- a/tools/include/compiler_options.hpp.in +++ b/tools/include/compiler_options.hpp.in @@ -548,19 +548,19 @@ static void GetLdDefaults(std::vector& ldopts) { #ifdef __APPLE__ ldopts.insert(ldopts.end(), {"-arch", "x86_64", "-macosx_version_min", "10.13", "-framework", "Foundation", "-framework", "System"}); - llvm::SmallString<256> Output; - if(llvm::sys::fs::createTemporaryFile("cdtsdkroot", ".path", Output)) + llvm::SmallString<256> output; + if(llvm::sys::fs::createTemporaryFile("cdtsdkroot", ".path", output)) throw std::runtime_error("Failed to create tmp file"); - auto DeleteOutput = llvm::make_scope_exit([&](){ llvm::sys::fs::remove(Output); }); + auto delete_output = llvm::make_scope_exit([&](){ llvm::sys::fs::remove(output); }); - if(!eosio::cdt::environment::exec_subprogram("xcrun", {"--show-sdk-path"}, true, llvm::None, {Output.str()})) + if(!eosio::cdt::environment::exec_subprogram("xcrun", {"--show-sdk-path"}, true, llvm::None, {output.str()})) throw std::runtime_error("Failed to run xcrun --show-sdk-path"); - auto Buf = llvm::MemoryBuffer::getFile(Output); - if(!Buf) + auto buf = llvm::MemoryBuffer::getFile(output); + if(!buf) throw std::runtime_error("Failed to open tmp file"); - std::string sdkpath = Buf.get()->getBuffer(); + std::string sdkpath = buf.get()->getBuffer(); //remove newline sdkpath.resize(sdkpath.size()-1); ldopts.insert(ldopts.end(), {"-syslibroot", sdkpath});