Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some basic examples #2

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
buck-out
5 changes: 5 additions & 0 deletions examples/cpp/.buckconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[buildfile]
name=TARGETS

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

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

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

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

void print_hello()
{
std::cout << "hello from library" << std::endl;
}
1 change: 1 addition & 0 deletions examples/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/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 = {
"srcs": attr.list(attr.source()),
"headers": attr.list(attr.source()),
"deps": attr.list(attr.dep()),
"toolchain": attr.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 = {
"srcs": attr.list(attr.source()),
"headers": attr.list(attr.source()),
"deps": attr.list(attr.dep()),
},
)
14 changes: 14 additions & 0 deletions examples/cpp/toolchain.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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,
attrs = {
"command": attr.string(),
},
)
5 changes: 5 additions & 0 deletions examples/go/.buckconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[buildfile]
name=TARGETS

[repositories]
root = .
16 changes: 16 additions & 0 deletions examples/go/binary-toolchain/TARGETS
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",
version = "1.18.3",
platform = "linux-amd64",
sha1 = "3511fcb34e0162abdcdeea0ab532f0264943e3d8",
)

go_binary(
name = "main",
srcs = glob(["*.go"]),
deps = [],
toolchain = ":go_linux",
)
7 changes: 7 additions & 0 deletions examples/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/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 = {
"srcs": attr.list(attr.source()),
"deps": attr.list(attr.dep()),
"toolchain": attr.dep(),
},
)
53 changes: 53 additions & 0 deletions examples/go/toolchain.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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")

# ctx.actions.symlink_file(compiler_dst, compiler_src)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ndmitchell I couldn't get it to symlink files so I just set it up to use the underlying OS ln.


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

go_toolchain = rule(
implementation = _go_toolchain_impl,
attrs = {
"version": attr.string(),
"platform": attr.string(),
"sha1": 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,
)

print(dir(output), output.short_path, str(output))

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

return [DefaultInfo(default_outputs = [output])]
4 changes: 4 additions & 0 deletions examples/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# buck2 examples

In these folders are some examples on how to get buck2 working with
your favorite languages and tools.
5 changes: 5 additions & 0 deletions examples/rust/.buckconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[buildfile]
name=TARGETS

[repositories]
root = .
16 changes: 16 additions & 0 deletions examples/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/rust/rustc/TARGETS
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/rust/rustc/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("hello from rustc");
}