Skip to content

Commit a6d9c2e

Browse files
authored
[workerd-cxx] eliminate source copying (#28)
* [workerd-cxx] eliminate source copying The original cxx crate uses symlinks to share code between its sub-crates. I assume this is because of cargo limitation that forces you to share such shared code as an independent crate that anyone can use. Symlinks don't work well on windows, so we got rid of them by copying the code during compilation. This has the unfortunate effect of rust-analyzer (and other tools) not working properly. We currently consume workerd-cxx only through bazel and we do not have such limitations. This change eliminates the copy and adjusts visibilities (and some minor detail of implementation) for things to compile. I hope that usability gains will overweight the additional merge difficulties. * clippy
1 parent 9aaac34 commit a6d9c2e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+533
-542
lines changed

BUILD.bazel

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
load("@rules_cc//cc:defs.bzl", "cc_library")
22
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_proc_macro")
3-
load("@bazel_skylib//lib:paths.bzl", "paths")
4-
load(":copy_srcs.bzl", "copy_srcs")
53

64
rust_library(
75
name = "cxx",
@@ -27,17 +25,41 @@ alias(
2725
visibility = ["//visibility:public"],
2826
)
2927

30-
copy_srcs(
31-
name = "cxxbridge-cmd-srcs",
32-
srcs = glob(["gen/cmd/src/*.rs"])
28+
rust_library(
29+
name = "syntax",
30+
srcs = glob(["syntax/*.rs"]),
31+
crate_root = "syntax/mod.rs",
32+
edition = "2021",
33+
deps = [
34+
"@crates.io//:proc-macro2",
35+
"@crates.io//:quote",
36+
"@crates.io//:syn",
37+
],
38+
)
39+
40+
rust_library(
41+
name = "gen",
42+
srcs = glob(["gen/src/*.rs"]),
43+
compile_data = ["include/cxx.h"],
44+
crate_root = "gen/src/mod.rs",
45+
edition = "2021",
46+
deps = [
47+
":syntax",
48+
"@crates.io//:codespan-reporting",
49+
"@crates.io//:proc-macro2",
50+
"@crates.io//:quote",
51+
"@crates.io//:syn",
52+
],
3353
)
3454

3555
rust_binary(
3656
name = "cxxbridge",
37-
srcs = [":cxxbridge-cmd-srcs"],
57+
srcs = glob(["gen/cmd/src/*.rs"]),
3858
compile_data = ["include/cxx.h"],
3959
edition = "2021",
4060
deps = [
61+
":gen",
62+
":syntax",
4163
"@crates.io//:clap",
4264
"@crates.io//:codespan-reporting",
4365
"@crates.io//:proc-macro2",
@@ -61,38 +83,32 @@ cc_library(
6183
linkstatic = True,
6284
)
6385

64-
copy_srcs(
65-
name = "cxxbridge-macro-srcs",
66-
srcs = glob(["macro/src/*.rs"])
67-
)
68-
6986
rust_proc_macro(
7087
name = "cxxbridge-macro",
71-
srcs = [":cxxbridge-macro-srcs"],
88+
srcs = glob(["macro/src/*.rs"]),
7289
edition = "2021",
7390
proc_macro_deps = [
7491
"@crates.io//:rustversion",
7592
],
7693
deps = [
94+
":gen",
95+
":syntax",
7796
"@crates.io//:proc-macro2",
7897
"@crates.io//:quote",
7998
"@crates.io//:syn",
8099
],
81100
)
82101

83-
copy_srcs(
84-
name = "cxx-build-srcs",
85-
srcs = glob(["gen/build/src/*.rs"])
86-
)
87-
88102
rust_library(
89103
name = "cxx-build",
90-
srcs = [":cxx-build-srcs"],
104+
srcs = glob(["gen/build/src/*.rs"]),
91105
compile_data = [
92106
"include/cxx.h",
93107
],
94108
edition = "2021",
95109
deps = [
110+
":gen",
111+
":syntax",
96112
"@crates.io//:cc",
97113
"@crates.io//:codespan-reporting",
98114
"@crates.io//:proc-macro2",
@@ -102,20 +118,16 @@ rust_library(
102118
],
103119
)
104120

105-
copy_srcs(
106-
name = "cxx-gen-srcs",
107-
srcs = glob(["gen/lib/src/*.rs"])
108-
)
109-
110121
rust_library(
111122
name = "cxx-gen",
112-
srcs = [":cxx-gen-srcs"],
123+
srcs = glob(["gen/lib/src/*.rs"]),
113124
compile_data = ["include/cxx.h"],
114125
edition = "2021",
115126
visibility = ["//visibility:public"],
116127
deps = [
128+
":gen",
129+
":syntax",
117130
"@crates.io//:cc",
118-
"@crates.io//:codespan-reporting",
119131
"@crates.io//:proc-macro2",
120132
"@crates.io//:quote",
121133
"@crates.io//:syn",

copy_srcs.bzl

Lines changed: 0 additions & 38 deletions
This file was deleted.

gen/build/src/cargo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::gen::{CfgEvaluator, CfgResult};
1+
use gen::{CfgEvaluator, CfgResult};
22
use std::borrow::Borrow;
33
use std::cmp::Ordering;
44
use std::collections::{BTreeMap as Map, BTreeSet as Set};

gen/build/src/cfg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ pub use self::r#impl::Cfg::CFG;
342342
#[cfg(not(doc))]
343343
mod r#impl {
344344
use crate::intern::{intern, InternedString};
345-
use crate::syntax::map::UnorderedMap as Map;
345+
use syntax::map::UnorderedMap as Map;
346346
use crate::vec::{self, InternedVec as _};
347347
use std::cell::RefCell;
348348
use std::fmt::{self, Debug};

gen/build/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::cfg::CFG;
2-
use crate::gen::fs;
2+
use gen::fs;
33
use std::error::Error as StdError;
44
use std::ffi::OsString;
55
use std::fmt::{self, Display};

gen/build/src/intern.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::syntax::set::UnorderedSet as Set;
1+
use syntax::set::UnorderedSet as Set;
22
use std::sync::{Mutex, OnceLock, PoisonError};
33

44
#[derive(Copy, Clone, Default)]

gen/build/src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,19 @@ mod cargo;
8181
mod cfg;
8282
mod deps;
8383
mod error;
84-
mod gen;
8584
mod intern;
8685
mod out;
8786
mod paths;
88-
mod syntax;
8987
mod target;
9088
mod vec;
9189

9290
use crate::cargo::CargoEnvCfgEvaluator;
9391
use crate::deps::{Crate, HeaderDir};
9492
use crate::error::{Error, Result};
95-
use crate::gen::error::report;
96-
use crate::gen::Opt;
93+
use gen::error::report;
94+
use gen::Opt;
9795
use crate::paths::PathExt;
98-
use crate::syntax::map::{Entry, UnorderedMap};
96+
use syntax::map::{Entry, UnorderedMap};
9997
use crate::target::TargetDir;
10098
use cc::Build;
10199
use std::collections::BTreeSet;
@@ -425,7 +423,7 @@ fn generate_bridge(prj: &Project, build: &mut Build, rust_source_file: &Path) ->
425423
}
426424

427425
fn best_effort_copy_headers(src: &Path, dst: &Path, max_depth: usize) {
428-
// Not using crate::gen::fs because we aren't reporting the errors.
426+
// Not using gen::fs because we aren't reporting the errors.
429427
use std::fs;
430428

431429
let mut dst_created = false;

gen/build/src/out.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::error::{Error, Result};
2-
use crate::gen::fs;
2+
use gen::fs;
33
use crate::paths;
44
use std::path::{Component, Path, PathBuf};
55
use std::{env, io};

gen/build/src/paths.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::error::Result;
2-
use crate::gen::fs;
2+
use gen::fs;
33
use std::ffi::OsStr;
44
use std::path::{Component, Path, PathBuf};
55

gen/cmd/src/app.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ mod test;
44

55
use super::{Opt, Output};
66
use crate::cfg::{self, CfgValue};
7-
use crate::gen::include::Include;
8-
use crate::syntax::IncludeKind;
7+
use gen::include::Include;
8+
use syntax::IncludeKind;
99
use clap::builder::{ArgAction, ValueParser};
1010
use clap::{Arg, Command};
1111
use std::collections::{BTreeMap as Map, BTreeSet as Set};

0 commit comments

Comments
 (0)