Skip to content

Commit

Permalink
Merge pull request #317 from dtolnay/build
Browse files Browse the repository at this point in the history
Factor out genrules to rust_cxx_bridge.bzl
  • Loading branch information
dtolnay authored Sep 24, 2020
2 parents f337a20 + 717c7e6 commit 6140a3f
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 98 deletions.
32 changes: 4 additions & 28 deletions demo/BUCK
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//tools/buck:genrule.bzl", "genrule")
load("//tools/buck:rust_cxx_bridge.bzl", "rust_cxx_bridge")

rust_binary(
name = "demo",
Expand All @@ -10,34 +10,10 @@ rust_binary(
],
)

cxx_library(
rust_cxx_bridge(
name = "bridge",
srcs = [":bridge/source"],
deps = [
":bridge/include",
":demo-include",
],
)

genrule(
name = "bridge/header",
srcs = ["src/main.rs"],
out = "src/main.rs.h",
cmd = "$(exe //:codegen) --header ${SRCS} > ${OUT}",
type = "cxxbridge",
)

genrule(
name = "bridge/source",
srcs = ["src/main.rs"],
out = "src/main.rs.cc",
cmd = "$(exe //:codegen) ${SRCS} > ${OUT}",
type = "cxxbridge",
)

cxx_library(
name = "bridge/include",
exported_headers = [":bridge/header"],
src = "src/main.rs",
deps = [":demo-include"],
)

cxx_library(
Expand Down
31 changes: 4 additions & 27 deletions demo/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("//tools/bazel:rust.bzl", "rust_binary")
load("//tools/bazel:rust_cxx_bridge.bzl", "rust_cxx_bridge")

rust_binary(
name = "demo",
Expand All @@ -11,34 +12,10 @@ rust_binary(
],
)

cc_library(
rust_cxx_bridge(
name = "bridge",
srcs = [":bridge/source"],
deps = [
":bridge/include",
":demo-include",
],
)

genrule(
name = "bridge/header",
srcs = ["src/main.rs"],
outs = ["src/main.rs.h"],
cmd = "$(location //:codegen) --header $< > $@",
tools = ["//:codegen"],
)

genrule(
name = "bridge/source",
srcs = ["src/main.rs"],
outs = ["src/main.rs.cc"],
cmd = "$(location //:codegen) $< > $@",
tools = ["//:codegen"],
)

cc_library(
name = "bridge/include",
hdrs = [":bridge/header"],
src = "src/main.rs",
deps = [":demo-include"],
)

cc_library(
Expand Down
25 changes: 7 additions & 18 deletions tests/BUCK
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//tools/buck:genrule.bzl", "genrule")
load("//tools/buck:rust_cxx_bridge.bzl", "rust_cxx_bridge")

rust_test(
name = "test",
Expand Down Expand Up @@ -34,23 +34,12 @@ cxx_library(
deps = ["//:core"],
)

genrule(
name = "bridge/header",
srcs = ["ffi/lib.rs"],
out = "ffi/lib.rs.h",
cmd = "$(exe //:codegen) --header ${SRCS} > ${OUT}",
rust_cxx_bridge(
name = "bridge",
src = "ffi/lib.rs",
)

genrule(
name = "bridge/source",
srcs = ["ffi/lib.rs"],
out = "ffi/lib.rs.cc",
cmd = "$(exe //:codegen) ${SRCS} > ${OUT}",
)

genrule(
name = "module/source",
srcs = ["ffi/module.rs"],
out = "ffi/module.rs.cc",
cmd = "$(exe //:codegen) ${SRCS} > ${OUT}",
rust_cxx_bridge(
name = "module",
src = "ffi/module.rs",
)
36 changes: 11 additions & 25 deletions tests/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("//tools/bazel:rust.bzl", "rust_library", "rust_test")
load("//tools/bazel:rust_cxx_bridge.bzl", "rust_cxx_bridge")

rust_test(
name = "test",
Expand Down Expand Up @@ -36,33 +37,18 @@ cc_library(
],
)

genrule(
name = "bridge/header",
srcs = ["ffi/lib.rs"],
outs = ["ffi/lib.rs.h"],
cmd = "$(location //:codegen) --header $< > $@",
tools = ["//:codegen"],
)

genrule(
name = "bridge/source",
srcs = ["ffi/lib.rs"],
outs = ["ffi/lib.rs.cc"],
cmd = "$(location //:codegen) $< > $@",
tools = ["//:codegen"],
)

cc_library(
name = "bridge/include",
hdrs = [":bridge/header"],
rust_cxx_bridge(
name = "bridge",
src = "ffi/lib.rs",
include_prefix = "cxx-test-suite",
strip_include_prefix = "ffi",
deps = [":impl"],
)

genrule(
name = "module/source",
srcs = ["ffi/module.rs"],
outs = ["ffi/module.rs.cc"],
cmd = "$(location //:codegen) $< > $@",
tools = ["//:codegen"],
rust_cxx_bridge(
name = "module",
src = "ffi/module.rs",
include_prefix = "cxx-test-suite",
strip_include_prefix = "ffi",
deps = [":impl"],
)
36 changes: 36 additions & 0 deletions tools/bazel/rust_cxx_bridge.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
load("@rules_cc//cc:defs.bzl", "cc_library")

def rust_cxx_bridge(
name,
src,
include_prefix = None,
strip_include_prefix = None,
deps = []):
native.genrule(
name = "%s/header" % name,
srcs = [src],
outs = [src + ".h"],
cmd = "$(location //:codegen) --header $< > $@",
tools = ["//:codegen"],
)

native.genrule(
name = "%s/source" % name,
srcs = [src],
outs = [src + ".cc"],
cmd = "$(location //:codegen) $< > $@",
tools = ["//:codegen"],
)

cc_library(
name = name,
srcs = [":%s/source" % name],
deps = deps + [":%s/include" % name],
)

cc_library(
name = "%s/include" % name,
hdrs = [":%s/header" % name],
include_prefix = include_prefix,
strip_include_prefix = strip_include_prefix,
)
30 changes: 30 additions & 0 deletions tools/buck/rust_cxx_bridge.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
load("//tools/buck:genrule.bzl", "genrule")

def rust_cxx_bridge(name, src, deps = []):
genrule(
name = "%s/header" % name,
srcs = [src],
out = src + ".h",
cmd = "$(exe //:codegen) --header ${SRCS} > ${OUT}",
type = "cxxbridge",
)

genrule(
name = "%s/source" % name,
srcs = [src],
out = src + ".cc",
cmd = "$(exe //:codegen) ${SRCS} > ${OUT}",
type = "cxxbridge",
)

cxx_library(
name = name,
srcs = [":%s/source" % name],
preferred_linkage = "static",
deps = deps + [":%s/include" % name],
)

cxx_library(
name = "%s/include" % name,
exported_headers = [":%s/header" % name],
)

0 comments on commit 6140a3f

Please sign in to comment.