From c70a6dcee1ce9790c6eb682c5b0643994b4293cd Mon Sep 17 00:00:00 2001 From: Bryan Henry Date: Fri, 18 Sep 2020 18:19:22 -0700 Subject: [PATCH 1/2] Add macros for Bazel and Buck to generate a cc_library from Rust bridge sources --- tests/BUCK | 36 ++++-------------- tests/BUILD | 48 ++++-------------------- tools/bazel/cxx_cc_library.bzl | 67 ++++++++++++++++++++++++++++++++++ tools/buck/cxx_cxx_library.bzl | 58 +++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+), 68 deletions(-) create mode 100644 tools/bazel/cxx_cc_library.bzl create mode 100644 tools/buck/cxx_cxx_library.bzl diff --git a/tests/BUCK b/tests/BUCK index a96bfb8d0..0197fa758 100644 --- a/tests/BUCK +++ b/tests/BUCK @@ -1,3 +1,5 @@ +load("//tools/buck:cxx_cxx_library.bzl", "cxx_cxx_library") + rust_test( name = "test", srcs = ["test.rs"], @@ -17,38 +19,16 @@ rust_library( ], ) -cxx_library( +cxx_cxx_library( name = "impl", - srcs = [ - "ffi/tests.cc", - ":gen-lib-source", - ":gen-module-source", + crate_name = "cxx-test-suite", + bridge_srcs = [ + "ffi/lib.rs", + "ffi/module.rs", ], - header_namespace = "cxx-test-suite", + srcs = ["ffi/tests.cc"], headers = { - "lib.rs.h": ":gen-lib-header", "tests.h": "ffi/tests.h", }, deps = ["//:core"], ) - -genrule( - name = "gen-lib-header", - srcs = ["ffi/lib.rs"], - out = "lib.rs.h", - cmd = "$(exe //:codegen) --header ${SRCS} > ${OUT}", -) - -genrule( - name = "gen-lib-source", - srcs = ["ffi/lib.rs"], - out = "lib.rs.cc", - cmd = "$(exe //:codegen) ${SRCS} > ${OUT}", -) - -genrule( - name = "gen-module-source", - srcs = ["ffi/module.rs"], - out = "module.rs.cc", - cmd = "$(exe //:codegen) ${SRCS} > ${OUT}", -) diff --git a/tests/BUILD b/tests/BUILD index 29cc79ce5..71483ef0e 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -1,3 +1,4 @@ +load("//tools/bazel:cxx_cc_library.bzl", "cxx_cc_library") load("//tools/bazel:rust.bzl", "rust_library", "rust_test") rust_test( @@ -18,48 +19,15 @@ rust_library( ], ) -cc_library( +cxx_cc_library( name = "impl", - srcs = [ - "ffi/tests.cc", - ":gen-lib-source", - ":gen-module-source", + crate_name = "cxx-test-suite", + bridge_srcs = [ + "ffi/lib.rs", + "ffi/module.rs", ], + srcs = ["ffi/tests.cc"], hdrs = ["ffi/tests.h"], - include_prefix = "cxx-test-suite", strip_include_prefix = "ffi", - deps = [ - ":lib-include", - "//:core", - ], -) - -genrule( - name = "gen-lib-header", - srcs = ["ffi/lib.rs"], - outs = ["lib.rs.h"], - cmd = "$(location //:codegen) --header $< > $@", - tools = ["//:codegen"], -) - -genrule( - name = "gen-lib-source", - srcs = ["ffi/lib.rs"], - outs = ["lib.rs.cc"], - cmd = "$(location //:codegen) $< > $@", - tools = ["//:codegen"], -) - -cc_library( - name = "lib-include", - hdrs = [":gen-lib-header"], - include_prefix = "cxx-test-suite", -) - -genrule( - name = "gen-module-source", - srcs = ["ffi/module.rs"], - outs = ["module.rs.cc"], - cmd = "$(location //:codegen) $< > $@", - tools = ["//:codegen"], + deps = ["//:core"], ) diff --git a/tools/bazel/cxx_cc_library.bzl b/tools/bazel/cxx_cc_library.bzl new file mode 100644 index 000000000..362c04bda --- /dev/null +++ b/tools/bazel/cxx_cc_library.bzl @@ -0,0 +1,67 @@ +"""cxx_cc_library macro""" + +load("@rules_cc//cc:defs.bzl", "cc_library") + +def cxx_cc_library( + name, + crate_name, + bridge_srcs, + srcs = None, + hdrs = None, + deps = None, + strip_include_prefix = None, + visibility = None): + """Generate C++ source for one or more Rust cxx::bridges and bundle into a cc_library. + + Args: + name: A unique name for the cc_library rule. + crate_name: The name of the corresponding Rust crate, used as the include_prefix. + bridge_srcs: One or more Rust source files containing cxx::bridge modules. + srcs: Forwarded to cc_library unmodified. + hdrs: Forwarded to cc_library unmodified. + deps: Forwarded to cc_library unmodified + strip_include_prefix: Forwarded to cc_library unmodified + visibility: Visibility for all rules generated by this macro. + """ + + gen_srcs = [] + gen_hdrs = [] + for bridge_source in bridge_srcs: + source_basename = bridge_source.rsplit("/", 1)[-1] + header_target = source_basename + "-gen-header" + source_target = source_basename + "-gen-source" + + header_out = source_basename + ".h" + if strip_include_prefix: + header_out = strip_include_prefix + "/" + header_out + + native.genrule( + name = header_target, + srcs = [bridge_source], + outs = [header_out], + cmd = "$(location //:codegen) --header $< > $@", + tools = ["//:codegen"], + visibility = visibility, + ) + + native.genrule( + name = source_target, + srcs = [bridge_source], + outs = [source_basename + ".cc"], + cmd = "$(location //:codegen) $< > $@", + tools = ["//:codegen"], + visibility = visibility, + ) + + gen_hdrs.append(header_target) + gen_srcs.append(source_target) + + cc_library( + name = name, + srcs = srcs + gen_srcs, + hdrs = hdrs + gen_hdrs, + include_prefix = crate_name, + strip_include_prefix = strip_include_prefix, + deps = deps, + visibility = visibility, + ) diff --git a/tools/buck/cxx_cxx_library.bzl b/tools/buck/cxx_cxx_library.bzl new file mode 100644 index 000000000..58c60f5d8 --- /dev/null +++ b/tools/buck/cxx_cxx_library.bzl @@ -0,0 +1,58 @@ +"""cxx_cxx_library macro""" + +def cxx_cxx_library( + name, + crate_name, + bridge_srcs, + srcs = None, + headers = None, + deps = None, + visibility = None): + """Generate C++ source for one or more Rust cxx::bridges and bundle into a cc_library. + + Args: + name: A unique name for the cc_library rule. + crate_name: The name of the corresponding Rust crate, used as the include_prefix. + bridge_srcs: One or more Rust source files containing cxx::bridge modules. + srcs: Forwarded to cc_library unmodified. + headers: Forwarded to cc_library unmodified. + deps: Forwarded to cc_library unmodified + visibility: Visibility for all rules generated by this macro. + """ + + gen_srcs = [] + gen_hdrs = {} + for bridge_source in bridge_srcs: + source_basename = bridge_source.rsplit("/", 1)[-1] + header_target = source_basename + "-gen-header" + source_target = source_basename + "-gen-source" + + hdr_out = source_basename + ".h" + native.genrule( + name = header_target, + srcs = [bridge_source], + out = hdr_out, + cmd = "$(exe //:codegen) --header ${SRCS} > ${OUT}", + visibility = visibility, + ) + + native.genrule( + name = source_target, + srcs = [bridge_source], + out = source_basename + ".cc", + cmd = "$(exe //:codegen) ${SRCS} > ${OUT}", + visibility = visibility, + ) + + gen_hdrs[hdr_out] = ":" + header_target + gen_srcs.append(":" + source_target) + + cxx_library( + name = name, + srcs = srcs + gen_srcs, + headers = flatten_dicts(headers, gen_hdrs), + header_namespace = crate_name, + deps = deps, + visibility = visibility, + ) + From abc1b4c02371057573fa30e1e3f39761141397ad Mon Sep 17 00:00:00 2001 From: Bryan Henry Date: Sat, 19 Sep 2020 14:03:40 -0700 Subject: [PATCH 2/2] Better names for Bazel/Buck templates, bit of cleanup --- tests/BUCK | 19 +++++++++--------- tests/BUILD | 19 +++++++++--------- ...c_library.bzl => cxxbridge_cc_library.bzl} | 16 +++++++-------- ..._library.bzl => cxxbridge_cxx_library.bzl} | 20 +++++++++---------- 4 files changed, 36 insertions(+), 38 deletions(-) rename tools/bazel/{cxx_cc_library.bzl => cxxbridge_cc_library.bzl} (82%) rename tools/buck/{cxx_cxx_library.bzl => cxxbridge_cxx_library.bzl} (75%) diff --git a/tests/BUCK b/tests/BUCK index 0197fa758..42f780f19 100644 --- a/tests/BUCK +++ b/tests/BUCK @@ -1,4 +1,4 @@ -load("//tools/buck:cxx_cxx_library.bzl", "cxx_cxx_library") +load("//tools/buck:cxxbridge_cxx_library.bzl", "cxxbridge_cxx_library") rust_test( name = "test", @@ -6,12 +6,14 @@ rust_test( deps = [":ffi"], ) +bridge_srcs = [ + "ffi/lib.rs", + "ffi/module.rs", +] + rust_library( name = "ffi", - srcs = [ - "ffi/lib.rs", - "ffi/module.rs", - ], + srcs = bridge_srcs, crate = "cxx_test_suite", deps = [ ":impl", @@ -19,13 +21,10 @@ rust_library( ], ) -cxx_cxx_library( +cxxbridge_cxx_library( name = "impl", crate_name = "cxx-test-suite", - bridge_srcs = [ - "ffi/lib.rs", - "ffi/module.rs", - ], + bridge_srcs = bridge_srcs, srcs = ["ffi/tests.cc"], headers = { "tests.h": "ffi/tests.h", diff --git a/tests/BUILD b/tests/BUILD index 71483ef0e..fb554b80b 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -1,4 +1,4 @@ -load("//tools/bazel:cxx_cc_library.bzl", "cxx_cc_library") +load("//tools/bazel:cxxbridge_cc_library.bzl", "cxxbridge_cc_library") load("//tools/bazel:rust.bzl", "rust_library", "rust_test") rust_test( @@ -7,25 +7,24 @@ rust_test( deps = [":cxx_test_suite"], ) +bridge_srcs = [ + "ffi/lib.rs", + "ffi/module.rs", +] + rust_library( name = "cxx_test_suite", - srcs = [ - "ffi/lib.rs", - "ffi/module.rs", - ], + srcs = bridge_srcs, deps = [ ":impl", "//:cxx", ], ) -cxx_cc_library( +cxxbridge_cc_library( name = "impl", crate_name = "cxx-test-suite", - bridge_srcs = [ - "ffi/lib.rs", - "ffi/module.rs", - ], + bridge_srcs = bridge_srcs, srcs = ["ffi/tests.cc"], hdrs = ["ffi/tests.h"], strip_include_prefix = "ffi", diff --git a/tools/bazel/cxx_cc_library.bzl b/tools/bazel/cxxbridge_cc_library.bzl similarity index 82% rename from tools/bazel/cxx_cc_library.bzl rename to tools/bazel/cxxbridge_cc_library.bzl index 362c04bda..f158c8644 100644 --- a/tools/bazel/cxx_cc_library.bzl +++ b/tools/bazel/cxxbridge_cc_library.bzl @@ -1,8 +1,8 @@ -"""cxx_cc_library macro""" +"""cxxbridge_cc_library macro""" load("@rules_cc//cc:defs.bzl", "cc_library") -def cxx_cc_library( +def cxxbridge_cc_library( name, crate_name, bridge_srcs, @@ -17,10 +17,10 @@ def cxx_cc_library( name: A unique name for the cc_library rule. crate_name: The name of the corresponding Rust crate, used as the include_prefix. bridge_srcs: One or more Rust source files containing cxx::bridge modules. - srcs: Forwarded to cc_library unmodified. - hdrs: Forwarded to cc_library unmodified. - deps: Forwarded to cc_library unmodified - strip_include_prefix: Forwarded to cc_library unmodified + srcs: Additional sources. Forwarded to cc_library unmodified. + hdrs: Additional headers. Forwarded to cc_library unmodified. + deps: Additional deps. Forwarded to cc_library unmodified. + strip_include_prefix: Forwarded to cc_library unmodified. visibility: Visibility for all rules generated by this macro. """ @@ -28,8 +28,8 @@ def cxx_cc_library( gen_hdrs = [] for bridge_source in bridge_srcs: source_basename = bridge_source.rsplit("/", 1)[-1] - header_target = source_basename + "-gen-header" - source_target = source_basename + "-gen-source" + header_target = name + "-" + source_basename + "-gen-header" + source_target = name + "-" + source_basename + "-gen-source" header_out = source_basename + ".h" if strip_include_prefix: diff --git a/tools/buck/cxx_cxx_library.bzl b/tools/buck/cxxbridge_cxx_library.bzl similarity index 75% rename from tools/buck/cxx_cxx_library.bzl rename to tools/buck/cxxbridge_cxx_library.bzl index 58c60f5d8..e747cfe36 100644 --- a/tools/buck/cxx_cxx_library.bzl +++ b/tools/buck/cxxbridge_cxx_library.bzl @@ -1,6 +1,6 @@ -"""cxx_cxx_library macro""" +"""cxxbridge_cxx_library macro""" -def cxx_cxx_library( +def cxxbridge_cxx_library( name, crate_name, bridge_srcs, @@ -8,15 +8,15 @@ def cxx_cxx_library( headers = None, deps = None, visibility = None): - """Generate C++ source for one or more Rust cxx::bridges and bundle into a cc_library. + """Generate C++ source for one or more Rust cxx::bridges and bundle into a cxx_library. Args: name: A unique name for the cc_library rule. crate_name: The name of the corresponding Rust crate, used as the include_prefix. bridge_srcs: One or more Rust source files containing cxx::bridge modules. - srcs: Forwarded to cc_library unmodified. - headers: Forwarded to cc_library unmodified. - deps: Forwarded to cc_library unmodified + srcs: Additional sources. Forwarded to cxx_library unmodified. + headers: Additional headers. Forwarded to cxx_library unmodified. + deps: Additional deps. Forwarded to cxx_library unmodified. visibility: Visibility for all rules generated by this macro. """ @@ -24,11 +24,11 @@ def cxx_cxx_library( gen_hdrs = {} for bridge_source in bridge_srcs: source_basename = bridge_source.rsplit("/", 1)[-1] - header_target = source_basename + "-gen-header" - source_target = source_basename + "-gen-source" + header_target = name + "-" + source_basename + "-gen-header" + source_target = name + "-" + source_basename + "-gen-source" hdr_out = source_basename + ".h" - native.genrule( + genrule( name = header_target, srcs = [bridge_source], out = hdr_out, @@ -36,7 +36,7 @@ def cxx_cxx_library( visibility = visibility, ) - native.genrule( + genrule( name = source_target, srcs = [bridge_source], out = source_basename + ".cc",