Skip to content

Commit

Permalink
Add some basic examples (#2)
Browse files Browse the repository at this point in the history
Summary:
This adds prelude-free examples for cpp using your local toolchain, and for go using a precompiled toolchain with a particular version / architecture. I have not had a chance to confirm whether multiple arches works correctly, but may attempt to do so on the M1 macOS machine.

There are some more examples that have not been included in this PR directly, such as a semi-functional cpp toolchain example using LLVM precompiled releases, and cpp static linking example which needs some more polish.

- [x] cpp binary
- [x] cpp library
- [ ] cpp static linked binary (using the lib)
- [ ] cpp toolchain
- [x] go toolchain
- [x] rustc

Pull Request resolved: #2

Reviewed By: themarwhal

Differential Revision: D37852433

Pulled By: ndmitchell

fbshipit-source-id: 4c6ff29de7c0151381a7e83179abf3749941e8cc
  • Loading branch information
arlyon authored and facebook-github-bot committed Jul 26, 2022
1 parent 1988508 commit d12d95b
Show file tree
Hide file tree
Showing 20 changed files with 248 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# buck2 examples

In these folders are some examples on how to get buck2 working with
your favorite languages and tools.

## no_prelude

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.
5 changes: 5 additions & 0 deletions examples/no_prelude/cpp/.buckconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[buildfile]
name=BUILD

[repositories]
root = .
15 changes: 15 additions & 0 deletions examples/no_prelude/cpp/hello-world/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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",
deps = [],
)
5 changes: 5 additions & 0 deletions examples/no_prelude/cpp/hello-world/src/func.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <iostream>

void print_hello() {
std::cout << "hellp from cpp" << std::endl;
}
1 change: 1 addition & 0 deletions examples/no_prelude/cpp/hello-world/src/func.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void print_hello();
5 changes: 5 additions & 0 deletions examples/no_prelude/cpp/hello-world/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "func.hpp"

int main() {
print_hello();
}
9 changes: 9 additions & 0 deletions examples/no_prelude/cpp/library/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("//:rules.bzl", "cpp_library")

cpp_library(
name = "library",
srcs = glob(["src/**/*.cpp"]),
headers = glob(["src/**/*.hpp"]),
visibility = ["PUBLIC"],
deps = [],
)
5 changes: 5 additions & 0 deletions examples/no_prelude/cpp/library/src/library.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <iostream>

void print_hello() {
std::cout << "hello from library" << std::endl;
}
1 change: 1 addition & 0 deletions examples/no_prelude/cpp/library/src/library.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void print_hello();
46 changes: 46 additions & 0 deletions examples/no_prelude/cpp/rules.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
load("//toolchain.bzl", "CxxCompilerInfo")

CxxLibraryInfo = provider(fields = ["headers", "objects", "include_folders"])

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

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

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

return [
DefaultInfo(default_outputs = [out]),
RunInfo(args = cmd_args(out)),
]

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

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

cmd = cmd_args(["clang++", "-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,
attrs = {
"deps": attr.list(attr.dep()),
"headers": attr.list(attr.source()),
"srcs": attr.list(attr.source()),
},
)
15 changes: 15 additions & 0 deletions examples/no_prelude/cpp/toolchain.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CxxCompilerInfo = provider(
doc = "Information about how to invoke the cpp compiler.",
fields = ["compiler_path", "include_directories", "lib_directories"],
)

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

cpp_local_toolchain = rule(
implementation = _cpp_local_toolchain_impl,
is_toolchain_rule = True,
attrs = {
"command": attr.string(),
},
)
5 changes: 5 additions & 0 deletions examples/no_prelude/go/.buckconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[buildfile]
name=BUILD

[repositories]
root = .
16 changes: 16 additions & 0 deletions examples/no_prelude/go/binary-toolchain/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("//:rules.bzl", "go_binary")
load("//:toolchain.bzl", "go_toolchain")

go_toolchain(
name = "go_linux",
platform = "linux-amd64",
sha1 = "3511fcb34e0162abdcdeea0ab532f0264943e3d8",
version = "1.18.3",
)

go_binary(
name = "main",
srcs = glob(["*.go"]),
toolchain = ":go_linux",
deps = [],
)
7 changes: 7 additions & 0 deletions examples/no_prelude/go/binary-toolchain/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt"

func main() {
fmt.Println("hello from go toolchain")
}
23 changes: 23 additions & 0 deletions examples/no_prelude/go/rules.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("//toolchain.bzl", "GoCompilerInfo")

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

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

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

return [
DefaultInfo(default_outputs = [out]),
RunInfo(args = cmd_args(out)),
]

go_binary = rule(
implementation = _go_binary_impl,
attrs = {
"deps": attr.list(attr.dep()),
"srcs": attr.list(attr.source()),
"toolchain": attr.dep(),
},
)
49 changes: 49 additions & 0 deletions examples/no_prelude/go/toolchain.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
GoCompilerInfo = provider(
doc = "Information about how to invoke the go compiler.",
fields = ["compiler_path", "GOROOT"],
)

def _go_toolchain_impl(ctx):
url = "https://go.dev/dl/go" + ctx.attr.version + "." + ctx.attr.platform + ".tar.gz"

download = http_archive_impl(ctx, url, ctx.attr.sha1)

compiler_dst = ctx.actions.declare_output("compiler")
compiler_src = cmd_args(download[0].default_outputs[0], format = "{}/go/bin/go")
ctx.actions.run(["ln", "-srf", compiler_src, compiler_dst.as_output()], category = "cp_compiler")

return download + [GoCompilerInfo(compiler_path = compiler_dst, GOROOT = "")]

go_toolchain = rule(
implementation = _go_toolchain_impl,
attrs = {
"platform": attr.string(),
"sha1": attr.string(),
"version": attr.string(),
},
)

def http_archive_impl(ctx: "context", url, sha1) -> ["provider"]:
# Download archive.
archive = ctx.actions.declare_output("archive.tar.gz")
ctx.actions.download_file(archive.as_output(), url, sha1 = sha1, is_deferrable = True)

# Unpack archive to output directory.
compress_flag = "-z"

output = ctx.actions.declare_output(ctx.label.name)
script, hidden = ctx.actions.write(
"unpack.sh",
[
cmd_args(output, format = "mkdir -p {}"),
cmd_args(output, format = "cd {}"),
cmd_args(["tar", compress_flag, "-x", "-f", archive], delimiter = " ").relative_to(output),
],
is_executable = True,
allow_args = True,
)

ctx.actions.run(cmd_args(["/bin/sh", script])
.hidden(hidden + [archive, output.as_output()]), category = "http_archive")

return [DefaultInfo(default_outputs = [output])]
5 changes: 5 additions & 0 deletions examples/no_prelude/rust/.buckconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[buildfile]
name=BUILD

[repositories]
root = .
16 changes: 16 additions & 0 deletions examples/no_prelude/rust/rules.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def _rust_binary_impl(ctx):
file = ctx.attr.file
out = ctx.actions.declare_output("main")

cmd = cmd_args(["rustc", "--crate-type=bin", file, "-o", out.as_output()])

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

return [DefaultInfo(default_outputs = [out]), RunInfo(args = cmd_args([out]))]

rust_binary = rule(
implementation = _rust_binary_impl,
attrs = {
"file": attr.source(),
},
)
6 changes: 6 additions & 0 deletions examples/no_prelude/rust/rustc/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("//:rules.bzl", "rust_binary")

rust_binary(
name = "main",
file = "./main.rs",
)
3 changes: 3 additions & 0 deletions examples/no_prelude/rust/rustc/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("hello from rustc");
}

0 comments on commit d12d95b

Please sign in to comment.