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

[4.0 -> main] fix -fnative linking on macOS #229

Merged
merged 4 commits into from
Sep 25, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion tools/include/compiler_options.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string>
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/ADT/ScopeExit.h"

#ifdef ONLY_LD
#define LD_CAT EosioLdToolCategory
Expand Down Expand Up @@ -546,14 +547,31 @@ static void GetLdDefaults(std::vector<std::string>& 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 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()}))
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");
}
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
Expand Down
12 changes: 8 additions & 4 deletions tools/include/eosio/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ struct environment {
return env_table;
}
static bool exec_subprogram(const std::string prog, std::vector<std::string> options, bool root=false,
llvm::Optional<std::string> stdin_file = llvm::None) {
llvm::Optional<std::string> stdin_file = llvm::None,
llvm::Optional<std::string> stdout_file = llvm::None) {
std::vector<llvm::StringRef> args;
args.push_back(prog);
args.insert(args.end(), options.begin(), options.end());
Expand All @@ -142,9 +143,12 @@ struct environment {
find_path = "/usr/bin";
if ( const auto& path = llvm::sys::findProgramByName(prog.c_str(), {find_path}) ) {
std::vector<llvm::Optional<llvm::StringRef>> 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
Expand Down