Skip to content

Commit

Permalink
Fix examples/no-prelude/cpp (#13)
Browse files Browse the repository at this point in the history
Summary:
The example included some old code that needed to be updated for compatibility with recent Starlark API changes.
At the moment the build does also have an implicit dependency on the prelude, so until that's fixed it's worked around by linking the prelude into the project.
Finally, the compiler toolchain introduced in the example was only used by the binary rule, but not by the library rule. This PR fixes that to use the toolchain consistently.

Pull Request resolved: #13

Reviewed By: arlyon

Differential Revision: D39724852

Pulled By: arlyon

fbshipit-source-id: bd8ac4d3ce188dcb6b1f7cc98a09dad7c578242f
  • Loading branch information
aherrmann authored and facebook-github-bot committed Sep 22, 2022
1 parent fceff44 commit ff449c3
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 24 deletions.
6 changes: 6 additions & 0 deletions examples/no_prelude/cpp/.buckconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ name=BUILD

[repositories]
root = .
prelude = prelude
buck = no
fbcode = no
fbsource = no
ovr_config = no
toolchains = no
7 changes: 7 additions & 0 deletions examples/no_prelude/cpp/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("//:toolchain.bzl", "cpp_local_toolchain")

cpp_local_toolchain(
name = "clang",
command = "clang++",
visibility = ["PUBLIC"],
)
8 changes: 1 addition & 7 deletions examples/no_prelude/cpp/hello-world/BUILD
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
load("//:rules.bzl", "cpp_binary")
load("//:toolchain.bzl", "cpp_local_toolchain")

cpp_local_toolchain(
name = "clang",
command = "clang++",
)

cpp_binary(
name = "main",
srcs = glob(["src/**/*.cpp"]),
headers = glob(["src/**/*.hpp"]),
toolchain = ":clang",
toolchain = "//:clang",
deps = [],
)
1 change: 1 addition & 0 deletions examples/no_prelude/cpp/library/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ cpp_library(
srcs = glob(["src/**/*.cpp"]),
headers = glob(["src/**/*.hpp"]),
visibility = ["PUBLIC"],
toolchain = "//:clang",
deps = [],
)
1 change: 1 addition & 0 deletions examples/no_prelude/cpp/prelude
29 changes: 15 additions & 14 deletions examples/no_prelude/cpp/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ load("//toolchain.bzl", "CxxCompilerInfo")
CxxLibraryInfo = provider(fields = ["headers", "objects", "include_folders"])

def _cpp_binary_impl(ctx: "context") -> ["provider"]:
sources = ctx.attr.srcs
sources = ctx.attrs.srcs
out = ctx.actions.declare_output("main")

cmd = cmd_args([ctx.attr.toolchain[CxxCompilerInfo].compiler_path, "-o", out.as_output()] + sources)
cmd = cmd_args([ctx.attrs.toolchain[CxxCompilerInfo].compiler_path, "-o", out.as_output()] + sources)

ctx.actions.run(cmd, category = "compile")

Expand All @@ -16,31 +16,32 @@ def _cpp_binary_impl(ctx: "context") -> ["provider"]:
]

cpp_binary = rule(
implementation = _cpp_binary_impl,
impl = _cpp_binary_impl,
attrs = {
"deps": attr.list(attr.dep()),
"headers": attr.list(attr.source()),
"srcs": attr.list(attr.source()),
"toolchain": attr.toolchain_dep(),
"deps": attrs.list(attrs.dep()),
"headers": attrs.list(attrs.source()),
"srcs": attrs.list(attrs.source()),
"toolchain": attrs.toolchain_dep(),
},
)

def _cpp_library_impl(ctx: "context") -> ["provider"]:
sources = ctx.attr.srcs
headers = ctx.attr.headers
sources = ctx.attrs.srcs
headers = ctx.attrs.headers
out = ctx.actions.declare_output("lib.so")

cmd = cmd_args(["clang++", "-shared", "-undefined", "dynamic_lookup", "-o", out.as_output()] + sources)
cmd = cmd_args([ctx.attrs.toolchain[CxxCompilerInfo].compiler_path, "-shared", "-undefined", "dynamic_lookup", "-o", out.as_output()] + sources)

ctx.actions.run(cmd, category = "compile")

return [DefaultInfo(default_outputs = [out]), CxxLibraryInfo(objects = [out], headers = headers)]

cpp_library = rule(
implementation = _cpp_library_impl,
impl = _cpp_library_impl,
attrs = {
"deps": attr.list(attr.dep()),
"headers": attr.list(attr.source()),
"srcs": attr.list(attr.source()),
"deps": attrs.list(attrs.dep()),
"headers": attrs.list(attrs.source()),
"srcs": attrs.list(attrs.source()),
"toolchain": attrs.toolchain_dep(),
},
)
6 changes: 3 additions & 3 deletions examples/no_prelude/cpp/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ CxxCompilerInfo = provider(
)

def _cpp_local_toolchain_impl(ctx):
return [DefaultInfo(), CxxCompilerInfo(compiler_path = ctx.attr.command)]
return [DefaultInfo(), CxxCompilerInfo(compiler_path = ctx.attrs.command)]

cpp_local_toolchain = rule(
implementation = _cpp_local_toolchain_impl,
impl = _cpp_local_toolchain_impl,
is_toolchain_rule = True,
attrs = {
"command": attr.string(),
"command": attrs.string(),
},
)

0 comments on commit ff449c3

Please sign in to comment.