Skip to content

Commit

Permalink
Provide cfg option to disable use of cc.
Browse files Browse the repository at this point in the history
In the Chromium build system, we automatically convert Cargo.toml rules into
rules for our build system, gn. We also run build.rs files as part of the
Chromium build (because it turns out that the vast majority of them just set
some feature flags for feature detection).

The build.rs for cxx does more sophisticated things - including building C++
code using cc. That's appropriate in a cargo context, but presents difficulty
for our build system where C++ must be built using static build rules beyond the
realm of cargo. At the moment we carry a patch for this, but it would be great
not to have to do so.

This patch would enable us to use the Cargo.toml and build.rs for cxx without
change. It's only one out of four patches we currently carry for cxx, but it
would be great to get rid of it! Obviously this may seem like a bit of a niche
use-case, but hopefully this helps anyone else who builds cxx using non-Cargo
build systems, but reusing the Cargo.toml and build.rs.
  • Loading branch information
adetaylor committed Nov 8, 2023
1 parent 306019c commit cff0928
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@ repository = "https://github.com/dtolnay/cxx"
rust-version = "1.60"

[features]
default = ["std", "cxxbridge-flags/default"] # c++11
"c++14" = ["cxxbridge-flags/c++14"]
"c++17" = ["cxxbridge-flags/c++17"]
"c++20" = ["cxxbridge-flags/c++20"]
default = ["std", "cxxbridge-flags/default", "cc"] # c++11
"c++14" = ["cc", "cxxbridge-flags/c++14"]
"c++17" = ["cc", "cxxbridge-flags/c++17"]
"c++20" = ["cc", "cxxbridge-flags/c++20"]
alloc = []
std = ["alloc"]
cc = [ "dep:cc", "dep:cxxbridge-flags" ]

[dependencies]
cxxbridge-macro = { version = "=1.0.110", path = "macro" }
link-cplusplus = "1.0.9"

[build-dependencies]
cc = "1.0.79"
cxxbridge-flags = { version = "=1.0.110", path = "flags", default-features = false }
cc = { version="1.0.79", optional=true }
cxxbridge-flags = { version = "=1.0.110", path = "flags", default-features = false, optional=true }

[dev-dependencies]
cxx-build = { version = "=1.0.110", path = "gen/build" }
Expand Down
47 changes: 27 additions & 20 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;
#[cfg(feature="cc")]
use std::path::{Path, PathBuf};

fn main() {
let manifest_dir_opt = env::var_os("CARGO_MANIFEST_DIR").map(PathBuf::from);
let manifest_dir = manifest_dir_opt.as_deref().unwrap_or(Path::new(""));

cc::Build::new()
.file(manifest_dir.join("src/cxx.cc"))
.cpp(true)
.cpp_link_stdlib(None) // linked via link-cplusplus crate
.flag_if_supported(cxxbridge_flags::STD)
.warnings_into_errors(cfg!(deny_warnings))
.compile("cxxbridge1");

println!("cargo:rerun-if-changed=src/cxx.cc");
println!("cargo:rerun-if-changed=include/cxx.h");
println!("cargo:rustc-cfg=built_with_cargo");

if let Some(manifest_dir) = &manifest_dir_opt {
let cxx_h = manifest_dir.join("include").join("cxx.h");
println!("cargo:HEADER={}", cxx_h.to_string_lossy());
}
#[cfg(feature="cc")]
build_cc();

if let Some(rustc) = rustc_version() {
if rustc.minor < 60 {
Expand Down Expand Up @@ -50,3 +34,26 @@ fn rustc_version() -> Option<RustVersion> {
let minor = pieces.next()?.parse().ok()?;
Some(RustVersion { version, minor })
}

#[cfg(feature="cc")]
fn build_cc() {
let manifest_dir_opt = env::var_os("CARGO_MANIFEST_DIR").map(PathBuf::from);
let manifest_dir = manifest_dir_opt.as_deref().unwrap_or(Path::new(""));

cc::Build::new()
.file(manifest_dir.join("src/cxx.cc"))
.cpp(true)
.cpp_link_stdlib(None) // linked via link-cplusplus crate
.flag_if_supported(cxxbridge_flags::STD)
.warnings_into_errors(cfg!(deny_warnings))
.compile("cxxbridge1");

println!("cargo:rerun-if-changed=src/cxx.cc");
println!("cargo:rerun-if-changed=include/cxx.h");
println!("cargo:rustc-cfg=built_with_cargo");

if let Some(manifest_dir) = &manifest_dir_opt {
let cxx_h = manifest_dir.join("include").join("cxx.h");
println!("cargo:HEADER={}", cxx_h.to_string_lossy());
}
}

0 comments on commit cff0928

Please sign in to comment.