Skip to content

Commit 763b92f

Browse files
Be-ingBe
authored and
Be
committed
cxx-build: symlink headers to GENERATED_HEADER_DIR
Symlink (or copy on Windows) generated headers to a directory specified by the GENERATED_HEADER_DIR environment variable, if this variable is set. This makes cxx-build usable by C++ build systems if they set GENERATED_HEADER_DIR before calling cargo and add it to their include paths. This is a better solution than the CLI tool for several reasons: * Don't need to build/distribute/install a separate program * Saves build time because cxx crates are not built redundantly for the CLI tool and the macro * No risk of version mismatches between CLI tool and cxx macro crate * One library crate that uses cxx can be usable in applications built either with Cargo or a C++ build system Fixes #462
1 parent c7060d4 commit 763b92f

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

gen/build/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ impl Project {
179179
TargetDir::Unknown => scratch::path("cxxbridge"),
180180
};
181181

182+
println!("cargo:rerun-if-env-changed=GENERATED_HEADER_DIR");
183+
182184
Ok(Project {
183185
include_prefix,
184186
manifest_dir,
@@ -424,6 +426,16 @@ fn generate_bridge(prj: &Project, build: &mut Build, rust_source_file: &Path) ->
424426
let shared_cc = prj.shared_dir.join(&prj.include_prefix).join(rel_path_cc);
425427
let _ = out::symlink_file(header_path, shared_h);
426428
let _ = out::symlink_file(implementation_path, shared_cc);
429+
430+
if let Ok(export_path) = env::var("GENERATED_HEADER_DIR") {
431+
out::symlink_file(
432+
header_path,
433+
PathBuf::from(export_path)
434+
.join(&prj.include_prefix)
435+
.join(rel_path_h),
436+
)?;
437+
}
438+
427439
Ok(())
428440
}
429441

0 commit comments

Comments
 (0)