Skip to content

Commit

Permalink
Create self-contained cxx_zig_toolchain (#22)
Browse files Browse the repository at this point in the history
Summary:
Closes #20

Adds `prelude//toolchains/cxx/zig` with a C/C++ toolchain based on `zig cc`.
The doc-string of the added module contains a description and examples
configurations.

Also adds an example to the repository that builds a C++ library and a binary
depending on that library using this toolchain.

Note, for that example to work in the open source buck2 tree the following additional patch needs to be applied:
```diff
 diff --git a/.buckconfig b/.buckconfig
deleted file mode 100644
index c32af92..0000000
 --- a/.buckconfig
+++ /dev/null
@@ -1,19 +0,0 @@
-[repositories]
-root = .
-prelude = prelude
-ovr_config = prelude
-shim = shim
-toolchains = shim
-fbcode = shim
-fbcode_macros = shim
-fbsource = shim
-buck = shim
-
-[buildfile]
-name = TARGETS
-
-[build]
-execution_platforms = ovr_config//platforms:default
-
-[parser]
-target_platform_detector_spec = target://...->ovr_config//platforms:default
 diff --git a/prelude/cxx/tools/TARGETS.v2 b/prelude/cxx/tools/TARGETS.v2
index 2030d2f..5db1689 100644
 --- a/prelude/cxx/tools/TARGETS.v2
+++ b/prelude/cxx/tools/TARGETS.v2
@@ -1,4 +1,3 @@
-load("fbcode_macros//build_defs/lib:python_common.bzl", "get_ldflags", "get_strip_mode")
 load(":defs.bzl", "cxx_hacks", "omnibus_environment")

 prelude = native
@@ -538,7 +537,7 @@ omnibus_environment(
     # We override the binary-level ldflags with library level ldfalgs for
     # shared roots. We include 2 important things: stripping binaries, and not
     # discarding GPU code.
-    shared_root_ld_flags = get_ldflags("", "", "omnibus_root", get_strip_mode("", ""), "") + [
+    shared_root_ld_flags = [
         "-Wl,--no-discard-section=.nv_fatbin",
         "-Wl,--no-discard-section=.nvFatBinSegment",
         # Reorder nv_fatbin after the bss section to avoid overflow
```

Pull Request resolved: #22

Reviewed By: arlyon

Differential Revision: D40179209

Pulled By: arlyon

fbshipit-source-id: d334f2ad4563281f78ba3f479b8defd09616cabf
  • Loading branch information
aherrmann authored and facebook-github-bot committed Oct 10, 2022
1 parent 0e49ce5 commit b8e5cf0
Show file tree
Hide file tree
Showing 12 changed files with 1,138 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ Preludeless examples for those wanting to use buck2 with their own
rules and toolchains. In here you can learn about how BUILD
files interact with rules, and how the provider abstraction can be
used to encapsulate build logic.

## toolchains

Examples testing the various toolchains included in the prelude.
18 changes: 18 additions & 0 deletions examples/toolchains/cxx_zig_toolchain/.buckconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[repositories]
self = .
ovr_config = prelude
prelude = prelude
toolchains = toolchains
buck = no
fbcode = no
fbcode_macros = no
fbsource = no

[buildfile]
name = BUILD

[build]
execution_platforms = ovr_config//platforms:default

[parser]
target_platform_detector_spec = target://...->ovr_config//platforms:default
13 changes: 13 additions & 0 deletions examples/toolchains/cxx_zig_toolchain/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@prelude//platforms:defs.bzl", "execution_platform")

cxx_library(
name = "lib",
srcs = ["lib.cc"],
headers = ["lib.h"],
)

cxx_binary(
name = "main",
srcs = ["main.cc"],
deps = [":lib"],
)
6 changes: 6 additions & 0 deletions examples/toolchains/cxx_zig_toolchain/lib.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "lib.h"
#include <iostream>

void hello() {
std::cout << "Hello World!\n";
}
1 change: 1 addition & 0 deletions examples/toolchains/cxx_zig_toolchain/lib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void hello();
5 changes: 5 additions & 0 deletions examples/toolchains/cxx_zig_toolchain/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "lib.h"

int main() {
hello();
}
6 changes: 6 additions & 0 deletions examples/toolchains/cxx_zig_toolchain/toolchains/.buckconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[repositories]
toolchains = .
prelude = ../prelude

[buildfile]
name = BUILD
19 changes: 19 additions & 0 deletions examples/toolchains/cxx_zig_toolchain/toolchains/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@prelude//toolchains/cxx/zig:defs.bzl", "download_zig_distribution", "cxx_zig_toolchain")
# TODO Replace by prelude toolchain once available
load("@toolchains//python:defs.bzl", "system_python_bootstrap_toolchain")

download_zig_distribution(
name = "zig",
version = "0.9.1",
)

cxx_zig_toolchain(
name = "cxx",
distribution = ":zig",
visibility = ["PUBLIC"],
)

system_python_bootstrap_toolchain(
name = "python_bootstrap",
visibility = ["PUBLIC"],
)
32 changes: 32 additions & 0 deletions examples/toolchains/cxx_zig_toolchain/toolchains/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
This example tests the `zig cc` based self-contained C/C++ toolchain.

To build it within the open source tree of buck2 to you need to
* Create a symlink for the prelude
```
ln -s ../../../prelude prelude
```
* Remove the top-level `.buckconfig`
```
rm ../../../.buckconfig
```
* Apply the following patch to the prelude
```
diff --git a/prelude/cxx/tools/TARGETS.v2 b/prelude/cxx/tools/TARGETS.v2
index 2030d2f..5db1689 100644
--- a/prelude/cxx/tools/TARGETS.v2
+++ b/prelude/cxx/tools/TARGETS.v2
@@ -1,4 +1,3 @@
-load("@fbcode_macros//build_defs/lib:python_common.bzl", "get_ldflags", "get_strip_mode")
load(":defs.bzl", "cxx_hacks", "omnibus_environment")
prelude = native
@@ -538,7 +537,7 @@ omnibus_environment(
# We override the binary-level ldflags with library level ldfalgs for
# shared roots. We include 2 important things: stripping binaries, and not
# discarding GPU code.
- shared_root_ld_flags = get_ldflags("", "", "omnibus_root", get_strip_mode("", ""), "") + [
+ shared_root_ld_flags = [
"-Wl,--no-discard-section=.nv_fatbin",
"-Wl,--no-discard-section=.nvFatBinSegment",
# Reorder nv_fatbin after the bss section to avoid overflow
```
19 changes: 19 additions & 0 deletions examples/toolchains/cxx_zig_toolchain/toolchains/python/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load(
"@prelude//python_bootstrap:python_bootstrap.bzl",
"PythonBootstrapToolchainInfo",
)

def _system_python_bootstrap_toolchain(_ctx):
return [
DefaultInfo(),
PythonBootstrapToolchainInfo(
interpreter = RunInfo(args = ["python3"]),
),
]

system_python_bootstrap_toolchain = rule(
impl = _system_python_bootstrap_toolchain,
attrs = {
},
is_toolchain_rule = True,
)
Loading

0 comments on commit b8e5cf0

Please sign in to comment.