|
| 1 | +use std::env; |
| 2 | +use std::fs; |
| 3 | +use std::io::Write; |
| 4 | +use std::path::PathBuf; |
| 5 | + |
| 6 | +fn main() { |
| 7 | + let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); |
| 8 | + let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); |
| 9 | + let profile = env::var("PROFILE").unwrap(); |
| 10 | + let test_artifacts_dir = manifest_dir.join(format!("target/cxxbridge/{}", profile)); |
| 11 | + fs::create_dir_all(&test_artifacts_dir).unwrap(); |
| 12 | + |
| 13 | + // generate the bridge using cxx_gen to get symbols and thunks |
| 14 | + let lib_source = fs::read_to_string(manifest_dir.join("lib.rs")).unwrap(); |
| 15 | + let lib_tokens: proc_macro2::TokenStream = lib_source.parse().unwrap(); |
| 16 | + let generated = cxx_gen::generate_header_and_cc(lib_tokens, &cxx_gen::Opt::default()).unwrap(); |
| 17 | + |
| 18 | + // write header and implementation |
| 19 | + fs::write(out_dir.join("lib.h"), &generated.header).unwrap(); |
| 20 | + fs::write(out_dir.join("lib.cc"), &generated.implementation).unwrap(); |
| 21 | + |
| 22 | + // create EXE symbols file in OS-appropriate format |
| 23 | + // Use TARGET env var to get the target OS, not the host OS |
| 24 | + let target = env::var("TARGET").unwrap(); |
| 25 | + let target_os = TargetOs::from(target.as_str()); |
| 26 | + |
| 27 | + let exe_symbols_content = cxx_gen::format_import_symbols_for_linker( |
| 28 | + &generated.import_symbols(), |
| 29 | + target_os.as_str(), |
| 30 | + ); |
| 31 | + |
| 32 | + let exe_symbols_path = match target_os { |
| 33 | + TargetOs::Windows => out_dir.join("exe.def"), |
| 34 | + TargetOs::Macos => out_dir.join("exe_undefined.txt"), |
| 35 | + TargetOs::Linux => out_dir.join("exe.dynamic"), |
| 36 | + }; |
| 37 | + fs::write(&exe_symbols_path, exe_symbols_content).unwrap(); |
| 38 | + |
| 39 | + // compile C++ code needed by the library |
| 40 | + // On Windows: compile thunks (which call back to the exe via GetProcAddress) |
| 41 | + // On Unix: no C++ code needed in the library (generate_import_thunks returns empty string) |
| 42 | + let thunks = generated.generate_import_thunks(target_os.as_str()); |
| 43 | + if !thunks.is_empty() { |
| 44 | + // write thunks that will be compiled into the DLL |
| 45 | + let thunks_path = out_dir.join("thunks.cc"); |
| 46 | + fs::write(&thunks_path, &thunks).unwrap(); |
| 47 | + |
| 48 | + let mut build = cc::Build::new(); |
| 49 | + build |
| 50 | + .cpp(true) |
| 51 | + .flag("/EHsc") |
| 52 | + .file(&thunks_path) |
| 53 | + .include(&out_dir) |
| 54 | + .include(manifest_dir.parent().unwrap().join("tests")); // for exe_functions.h |
| 55 | + build.compile("cxx-test-shared-library"); |
| 56 | + } |
| 57 | + |
| 58 | + // on Windows, use the .def file for exports |
| 59 | + if target_os == TargetOs::Windows { |
| 60 | + // create DLL .def file with export symbols (functions the DLL exports) |
| 61 | + let dll_def_content = cxx_gen::format_export_symbols_for_linker( |
| 62 | + &generated.export_symbols(), |
| 63 | + "windows", |
| 64 | + ); |
| 65 | + let dll_def_path = out_dir.join("library.def"); |
| 66 | + fs::write(&dll_def_path, dll_def_content).unwrap(); |
| 67 | + |
| 68 | + println!("cargo:rustc-cdylib-link-arg=/DEF:{}", dll_def_path.display()); |
| 69 | + |
| 70 | + // copy for the test to use |
| 71 | + fs::copy(&dll_def_path, test_artifacts_dir.join("library.def")).unwrap(); |
| 72 | + } else if target_os == TargetOs::Macos { |
| 73 | + // Per ld(1) man page: "-U symbol_name: Specified that it is ok for symbol_name to |
| 74 | + // have no definition. With -two_levelnamespace, the resulting symbol will be marked |
| 75 | + // dynamic_lookup which means dyld will search all loaded images." |
| 76 | + // |
| 77 | + // The Rust code in the library calls the cxxbridge wrapper functions (import_symbols), |
| 78 | + // which are implemented in lib.cc that's compiled into the executable. |
| 79 | + println!("cargo:rustc-cdylib-link-arg=-Wl,@{}", exe_symbols_path.display()); |
| 80 | + } else { |
| 81 | + // on Linux, create a version script to export symbols and allow undefined symbols |
| 82 | + let mut version_script = Vec::new(); |
| 83 | + writeln!(version_script, "{{").unwrap(); |
| 84 | + writeln!(version_script, " global:").unwrap(); |
| 85 | + for sym in &generated.export_symbols() { |
| 86 | + writeln!(version_script, " {};", sym).unwrap(); |
| 87 | + } |
| 88 | + writeln!(version_script, " local: *;").unwrap(); |
| 89 | + writeln!(version_script, "}};").unwrap(); |
| 90 | + let version_script_path = out_dir.join("libtest_library.version"); |
| 91 | + fs::write(&version_script_path, version_script).unwrap(); |
| 92 | + |
| 93 | + println!("cargo:rustc-cdylib-link-arg=-Wl,--version-script={}", version_script_path.display()); |
| 94 | + println!("cargo:rustc-cdylib-link-arg=-Wl,--allow-shlib-undefined"); |
| 95 | + } |
| 96 | + |
| 97 | + // expose paths for the test to use |
| 98 | + println!("cargo:rustc-env=EXE_SYMBOLS_PATH={}", exe_symbols_path.display()); |
| 99 | + |
| 100 | + // copy generated files to a predictable location for the test to find |
| 101 | + fs::copy(&exe_symbols_path, test_artifacts_dir.join(exe_symbols_path.file_name().unwrap())).unwrap(); |
| 102 | + fs::copy(out_dir.join("lib.h"), test_artifacts_dir.join("lib.h")).unwrap(); |
| 103 | + fs::copy(out_dir.join("lib.cc"), test_artifacts_dir.join("lib.cc")).unwrap(); |
| 104 | + |
| 105 | + println!("cargo:rerun-if-changed=lib.rs"); |
| 106 | +} |
| 107 | + |
| 108 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 109 | +enum TargetOs { |
| 110 | + Windows, |
| 111 | + Macos, |
| 112 | + Linux, |
| 113 | +} |
| 114 | + |
| 115 | +impl From<&str> for TargetOs { |
| 116 | + fn from(target: &str) -> Self { |
| 117 | + if target.contains("windows") { |
| 118 | + TargetOs::Windows |
| 119 | + } else if target.contains("darwin") || target.contains("ios") { |
| 120 | + TargetOs::Macos |
| 121 | + } else { |
| 122 | + TargetOs::Linux |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl TargetOs { |
| 128 | + fn as_str(self) -> &'static str { |
| 129 | + match self { |
| 130 | + TargetOs::Windows => "windows", |
| 131 | + TargetOs::Macos => "macos", |
| 132 | + TargetOs::Linux => "linux", |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments