Skip to content

Commit

Permalink
added shared object option to CDTMacros
Browse files Browse the repository at this point in the history
  • Loading branch information
dimas1185 committed Dec 15, 2022
1 parent ea6d926 commit 7efd258
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 14 deletions.
17 changes: 16 additions & 1 deletion modules/CDTMacros.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ endmacro()

macro (add_native_library TARGET)
add_library( ${TARGET} ${ARGN} )
target_compile_options( ${TARGET} PUBLIC -fnative )
target_compile_options( ${TARGET} PUBLIC -fnative -fPIC )
endmacro()

macro (add_native_executable TARGET)
Expand All @@ -38,3 +38,18 @@ macro (add_native_executable TARGET)
endif()
endmacro()

macro (add_native_shared_lib TARGET)
cmake_policy(SET CMP0002 OLD)
set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)
add_library( ${TARGET} SHARED ${ARGN} )
target_compile_options( ${TARGET} PUBLIC -fnative )
get_target_property(BINOUTPUT ${TARGET} BINARY_DIR)
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug" AND APPLE)
target_compile_options( ${TARGET} PUBLIC -g )
find_program ( name NAMES "dsymutil" )
if ( name )
add_custom_command( TARGET ${TARGET} POST_BUILD COMMAND dsymutil ${BINOUTPUT}/${TARGET} )
endif()
endif()
endmacro()

1 change: 1 addition & 0 deletions modules/CDTWasmToolchain.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(WASM_LINKER "@CDT_ROOT_DIR@/bin/cdt-ld")

set(CMAKE_C_LINK_EXECUTABLE "${WASM_LINKER} <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
set(CMAKE_CXX_LINK_EXECUTABLE "${WASM_LINKER} <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
set(CMAKE_CXX_CREATE_SHARED_LIBRARY "${WASM_LINKER} -shared <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")

set(CMAKE_EXECUTABLE_SUFFIX_C ".wasm")
set(CMAKE_EXECUTABLE_SUFFIX_CXX ".wasm")
Expand Down
64 changes: 51 additions & 13 deletions tools/include/compiler_options.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ static cl::opt<bool> fnative_opt(
cl::desc("Compile and link for x86-64"),
cl::ZeroOrMore,
cl::cat(LD_CAT));
static cl::opt<bool> fshared_opt(
"shared",
cl::desc("Make shared object native library"),
cl::ZeroOrMore,
cl::cat(LD_CAT));
static cl::opt<bool> fuse_main_opt(
"fuse-main",
cl::desc("Use main as entry"),
Expand Down Expand Up @@ -355,6 +360,11 @@ static cl::opt<bool> warn_action_read_only_opt(
"warn-action-read-only",
cl::desc("Issue a warning if a read-only action uses a write API and continue compilation"),
cl::cat(EosioCompilerToolCategory));
static cl::opt<bool> fPIC_opt(
"fPIC",
cl::desc("Generate position independent code. This option is used for shared libraries"),
cl::Optional,
cl::cat(LD_CAT));
/// end c/c++ options

/// begin c++ options
Expand Down Expand Up @@ -391,6 +401,7 @@ struct Options {
std::vector<std::string> abigen_resources;
bool debug;
bool native;
bool shared_lib;
std::pair<int, int> abi_version;
bool has_o_opt;
bool has_contract_opt;
Expand All @@ -404,7 +415,7 @@ static void GetCompDefaults(std::vector<std::string>& copts) {
copts.emplace_back(std::string("-D__eosio_cdt_major__=")+"${VERSION_MAJOR}");
copts.emplace_back(std::string("-D__eosio_cdt_minor__=")+"${VERSION_MINOR}");
copts.emplace_back(std::string("-D__eosio_cdt_patchlevel__=")+"${VERSION_PATCH}");
if (!fnative_opt) {
if (!fnative_opt && !fshared_opt) {
copts.emplace_back("--target=wasm32");
copts.emplace_back("-ffreestanding");
copts.emplace_back("-nostdlib");
Expand Down Expand Up @@ -439,7 +450,7 @@ static void GetCompDefaults(std::vector<std::string>& copts) {
copts.emplace_back("-DBOOST_DISABLE_ASSERTS");
copts.emplace_back("-DBOOST_EXCEPTION_DISABLE");
copts.emplace_back("-U__APPLE__");
if (!fnative_opt) {
if (!fnative_opt && !fshared_opt) {
copts.emplace_back("-Xclang");
copts.emplace_back("-load");
copts.emplace_back("-Xclang");
Expand All @@ -466,7 +477,7 @@ static void GetCompDefaults(std::vector<std::string>& copts) {

#ifdef ONLY_LD
static void GetLdDefaults(std::vector<std::string>& ldopts) {
if (!fnative_opt) {
if (!fnative_opt && !fshared_opt) {
if (!allow_names_opt) {
ldopts.emplace_back("--gc-sections");
ldopts.emplace_back("--strip-all");
Expand Down Expand Up @@ -536,13 +547,22 @@ static void GetLdDefaults(std::vector<std::string>& ldopts) {
#ifdef __APPLE__
ldopts.insert(ldopts.end(), {"-arch", "x86_64", "-macosx_version_min", "10.13", "-framework", "Foundation", "-framework", "System"});
#endif
ldopts.emplace_back("-static");
ldopts.insert(ldopts.end(), {"-lnative_c++", "-lnative_c", "-lnative_eosio", "-lnative", "-lnative_rt"});
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"});
}
}
#endif

static Options CreateOptions(bool add_defaults=true) {
if (fshared_opt && fnative_opt) {
throw std::runtime_error("Both -fnative and -fshared was specified. Only one of them should be present.");
}

std::string output_fn;
std::vector<std::string> inputs;
std::vector<std::string> agresources;
Expand Down Expand Up @@ -722,7 +742,7 @@ static Options CreateOptions(bool add_defaults=true) {
copts.emplace_back("-I"+sysroot_opt+"/include/libc");

// only allow capi for native builds and for eosio-cc
if (fnative_opt) {
if (fnative_opt || fshared_opt) {
copts.emplace_back("-I"+sysroot_opt+"/include/eosiolib/capi");
copts.emplace_back("-I"+sysroot_opt+"/include/eosiolib/native");
}
Expand All @@ -748,7 +768,7 @@ static Options CreateOptions(bool add_defaults=true) {
agopts.emplace_back("--sysroot="+eosio::cdt::whereami::where()+"/../");
ldopts.emplace_back("-L"+eosio::cdt::whereami::where()+"/../lib");

if (fnative_opt) {
if (fnative_opt || fshared_opt) {
copts.emplace_back("-I"+eosio::cdt::whereami::where()+"/../include/eosiolib/capi");
copts.emplace_back("-I"+eosio::cdt::whereami::where()+"/../include/eosiolib/native");
}
Expand Down Expand Up @@ -787,8 +807,12 @@ static Options CreateOptions(bool add_defaults=true) {
copts.emplace_back("-W"+warn);
}

if (fPIC_opt) {
copts.emplace_back("-fPIC");
}

#endif
if (!fnative_opt) {
if (!fnative_opt && !fshared_opt) {
#ifdef ONLY_LD
if (stack_canary_opt) {
ldopts.emplace_back("--stack-canary");
Expand Down Expand Up @@ -828,11 +852,22 @@ static Options CreateOptions(bool add_defaults=true) {
for ( auto library : l_opt ) {
ldopts.emplace_back("-l"+library);
}
auto replace_extension = [&](auto& str){
if (fnative_opt) {
llvm::sys::path::replace_extension(str, "");
}
else if (fshared_opt) {
llvm::sys::path::replace_extension(str, ".so");
}
else {
llvm::sys::path::replace_extension(str, ".wasm");
}
};
if (o_opt.empty()) {
#ifndef ONLY_LD
if (inputs.size() == 1) {
llvm::SmallString<256> fn = llvm::sys::path::filename(inputs[0]);
llvm::sys::path::replace_extension(fn, fnative_opt ? "" : ".wasm");
replace_extension(fn);
output_fn = fn.str();
} else {
ldopts.emplace_back("a.out");
Expand All @@ -841,7 +876,7 @@ static Options CreateOptions(bool add_defaults=true) {
if (inputs.size() == 1) {
llvm::SmallString<256> fn = llvm::sys::path::filename(inputs[0]);
llvm::sys::path::replace_extension(fn, "");
llvm::sys::path::replace_extension(fn, fnative_opt ? "" : ".wasm");
replace_extension(fn);
output_fn = fn.str();
ldopts.emplace_back("-o"+output_fn);
} else {
Expand All @@ -858,7 +893,7 @@ static Options CreateOptions(bool add_defaults=true) {
has_o_opt = true;
}

if (!fnative_opt) {
if (!fnative_opt && !fshared_opt) {
#ifndef ONLY_LD
if (!imports_opt.empty()) {
ldopts.emplace_back("-imports="+imports_opt);
Expand Down Expand Up @@ -915,6 +950,9 @@ static Options CreateOptions(bool add_defaults=true) {
}
if (fnative_opt)
ldopts.emplace_back("-fnative");
else if (fshared_opt) {
ldopts.emplace_back("-shared");
}
if (fuse_main_opt)
ldopts.emplace_back("-fuse-main");

Expand All @@ -937,8 +975,8 @@ static Options CreateOptions(bool add_defaults=true) {
}

#ifndef ONLY_LD
return {output_fn, inputs, link, abigen, no_missing_ricardian_clause_opt, pp_only, pp_dir, abigen_output, abigen_contract, copts, ldopts, agopts, agresources, debug, fnative_opt, {abi_version_major, abi_version_minor}, has_o_opt, has_contract_opt, warn_action_read_only};
return {output_fn, inputs, link, abigen, no_missing_ricardian_clause_opt, pp_only, pp_dir, abigen_output, abigen_contract, copts, ldopts, agopts, agresources, debug, fnative_opt, fshared_opt, {abi_version_major, abi_version_minor}, has_o_opt, has_contract_opt, warn_action_read_only};
#else
return {output_fn, {}, link, abigen, no_missing_ricardian_clause_opt, pp_only, pp_dir, abigen_output, abigen_contract, copts, ldopts, agopts, agresources, debug, fnative_opt, {abi_version_major, abi_version_minor}, has_o_opt, has_contract_opt, warn_action_read_only};
return {output_fn, {}, link, abigen, no_missing_ricardian_clause_opt, pp_only, pp_dir, abigen_output, abigen_contract, copts, ldopts, agopts, agresources, debug, fnative_opt, fshared_opt, {abi_version_major, abi_version_minor}, has_o_opt, has_contract_opt, warn_action_read_only};
#endif
}

0 comments on commit 7efd258

Please sign in to comment.