diff options
author | John Hodge <tpg@ucc.asn.au> | 2019-06-29 21:35:21 +0800 |
---|---|---|
committer | John Hodge <tpg@ucc.asn.au> | 2019-06-29 21:35:21 +0800 |
commit | 10d87a39f610641ef97aeac1f34c9d041c469909 (patch) | |
tree | e582fe2788fbceeb10cd6cb46392e19ced2867a6 | |
parent | 64af65a27097e17811c943dbac551ae741413954 (diff) | |
download | mrust-10d87a39f610641ef97aeac1f34c9d041c469909.tar.gz |
All - Working dylib support (not used yet, needs rpath/absolute)
-rw-r--r-- | src/ast/crate.cpp | 23 | ||||
-rw-r--r-- | src/hir/deserialise.cpp | 1 | ||||
-rw-r--r-- | src/main.cpp | 6 | ||||
-rw-r--r-- | src/trans/codegen_c.cpp | 29 | ||||
-rw-r--r-- | tools/minicargo/build.cpp | 1 |
5 files changed, 54 insertions, 6 deletions
diff --git a/src/ast/crate.cpp b/src/ast/crate.cpp index 8743ae2b..726a00c6 100644 --- a/src/ast/crate.cpp +++ b/src/ast/crate.cpp @@ -146,16 +146,22 @@ RcString Crate::load_extern_crate(Span sp, const RcString& name, const ::std::st else { ::std::vector<::std::string> paths; - auto direct_filename = FMT("lib" << name.c_str() << ".rlib"); +#define RLIB_SUFFIX ".rlib" +#define RDYLIB_SUFFIX ".so" + auto direct_filename = FMT("lib" << name.c_str() << RLIB_SUFFIX); + auto direct_filename_so = FMT("lib" << name.c_str() << RDYLIB_SUFFIX); auto name_prefix = FMT("lib" << name.c_str() << "-"); // Search a list of load paths for the crate for(const auto& p : g_crate_load_dirs) { - path = p + "/" + direct_filename; if( ::std::ifstream(path).good() ) { paths.push_back(path); } + path = p + "/" + direct_filename_so; + if( ::std::ifstream(path).good() ) { + paths.push_back(path); + } path = ""; // Search for `p+"/lib"+name+"-*.rlib" (which would match e.g. libnum-0.11.rlib) @@ -168,8 +174,16 @@ RcString Crate::load_extern_crate(Span sp, const RcString& name, const ::std::st { // AND the start is "lib"+name size_t len = strlen(ent->d_name); - if( len <= 5 || strcmp(ent->d_name + len - 5, ".rlib") != 0 ) + if( len > (sizeof(RLIB_SUFFIX)-1) && strcmp(ent->d_name + len - (sizeof(RLIB_SUFFIX)-1), RLIB_SUFFIX) == 0 ) + { + } + else if( len > (sizeof(RDYLIB_SUFFIX)-1) && strcmp(ent->d_name + len - (sizeof(RDYLIB_SUFFIX)-1), RDYLIB_SUFFIX) == 0 ) + { + } + else + { continue ; + } DEBUG(ent->d_name << " vs " << name_prefix); // Check if the entry ends with .rlib @@ -202,7 +216,8 @@ RcString Crate::load_extern_crate(Span sp, const RcString& name, const ::std::st } auto& ext_crate = res.first->second; // Move the external list out (doesn't need to be kept in the nested crate) - auto crate_ext_list = mv$( ext_crate.m_hir->m_ext_crates ); + //auto crate_ext_list = mv$( ext_crate.m_hir->m_ext_crates ); + const auto& crate_ext_list = ext_crate.m_hir->m_ext_crates; // Load referenced crates for( const auto& ext : crate_ext_list ) diff --git a/src/hir/deserialise.cpp b/src/hir/deserialise.cpp index 5ab24c0f..5795c809 100644 --- a/src/hir/deserialise.cpp +++ b/src/hir/deserialise.cpp @@ -1350,6 +1350,7 @@ auto ext_crate_file = m_in.read_string(); auto ext_crate = ::HIR::ExternCrate {}; ext_crate.m_basename = ext_crate_file; + ext_crate.m_path = ext_crate_file; rv.m_ext_crates.insert( ::std::make_pair( mv$(ext_crate_name), mv$(ext_crate) ) ); } } diff --git a/src/main.cpp b/src/main.cpp index f910ff56..dd208cbe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -712,7 +712,11 @@ int main(int argc, char *argv[]) break; case ::AST::Crate::Type::RustDylib: // Save a loadable HIR dump - CompilePhaseV("HIR Serialise", [&]() { HIR_Serialise(params.outfile + ".hir", *hir_crate); }); + CompilePhaseV("HIR Serialise", [&]() { + //auto saved_ext_crates = ::std::move(hir_crate->m_ext_crates); + HIR_Serialise(params.outfile + ".hir", *hir_crate); + //hir_crate->m_ext_crates = ::std::move(saved_ext_crates); + }); // Generate a .so CompilePhaseV("Trans Codegen", [&]() { Trans_Codegen(params.outfile, CodegenOutput::DynamicLibrary, trans_opt, *hir_crate, items, params.outfile + ".hir"); }); break; diff --git a/src/trans/codegen_c.cpp b/src/trans/codegen_c.cpp index b9f3f3ac..c9e12185 100644 --- a/src/trans/codegen_c.cpp +++ b/src/trans/codegen_c.cpp @@ -801,6 +801,7 @@ namespace { { args.push_back("-g"); } + args.push_back("-fPIC"); args.push_back("-o"); switch(out_ty) { @@ -821,7 +822,33 @@ namespace { case CodegenOutput::Executable: for( const auto& crate : m_crate.m_ext_crates ) { - args.push_back(crate.second.m_path + ".o"); + bool is_dylib = (crate.second.m_path.compare(crate.second.m_path.size() - 5, 5, ".rlib") != 0); + //::std::cout << crate.first << " references " << FMT_CB(ss, for(const auto& sc : crate.second.m_data->m_ext_crates) ss << sc.second.m_path << ",";) << ::std::endl; + // TODO: if this crate is included in a dylib crate, ignore it + bool is_in_dylib = false; + for( const auto& crate2 : m_crate.m_ext_crates ) + { + bool is_dylib = (crate2.second.m_path.compare(crate2.second.m_path.size() - 5, 5, ".rlib") != 0); + if( is_dylib ) + { + for(const auto& subcrate : crate2.second.m_data->m_ext_crates) + { + if( subcrate.second.m_path == crate.second.m_path ) { + is_in_dylib = true; + } + } + } + if( is_in_dylib ) + break; + } + if( is_in_dylib ) { + } + else if( crate.second.m_path.compare(crate.second.m_path.size() - 5, 5, ".rlib") == 0 ) { + args.push_back(crate.second.m_path + ".o"); + } + else { + args.push_back(crate.second.m_path); + } } for(const auto& path : link_dirs ) { diff --git a/tools/minicargo/build.cpp b/tools/minicargo/build.cpp index c63a7115..88c105ea 100644 --- a/tools/minicargo/build.cpp +++ b/tools/minicargo/build.cpp @@ -647,6 +647,7 @@ Builder::Builder(const BuildOptions& opts, size_t total_targets): outfile /= ::format("lib", target.m_name, crate_suffix, "-plugin" EXESUF); break; case PackageTarget::CrateType::dylib: + // TODO: Enable this once mrustc can set rpath or absolute paths //if(crate_type) *crate_type = "dylib"; //outfile /= ::format("lib", target.m_name, crate_suffix, DLLSUF); //break; |