-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
1988508
commit d12d95b
Showing
20 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[buildfile] | ||
name=BUILD | ||
|
||
[repositories] | ||
root = . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
void print_hello(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include "func.hpp" | ||
|
||
int main() { | ||
print_hello(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
void print_hello(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[buildfile] | ||
name=BUILD | ||
|
||
[repositories] | ||
root = . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[buildfile] | ||
name=BUILD | ||
|
||
[repositories] | ||
root = . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
println!("hello from rustc"); | ||
} |