diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 000000000000..d60c5d299b29 --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1 @@ +buck-out diff --git a/examples/cpp/.buckconfig b/examples/cpp/.buckconfig new file mode 100644 index 000000000000..761b6ee921a4 --- /dev/null +++ b/examples/cpp/.buckconfig @@ -0,0 +1,5 @@ +[buildfile] +name=TARGETS + +[repositories] +root = . diff --git a/examples/cpp/hello-world/TARGETS b/examples/cpp/hello-world/TARGETS new file mode 100644 index 000000000000..9790cc0b270c --- /dev/null +++ b/examples/cpp/hello-world/TARGETS @@ -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", +) diff --git a/examples/cpp/hello-world/src/func.cpp b/examples/cpp/hello-world/src/func.cpp new file mode 100644 index 000000000000..7a09ee44b3ab --- /dev/null +++ b/examples/cpp/hello-world/src/func.cpp @@ -0,0 +1,6 @@ +#include + +void print_hello() +{ + std::cout << "hellp from cpp" << std::endl; +} diff --git a/examples/cpp/hello-world/src/func.hpp b/examples/cpp/hello-world/src/func.hpp new file mode 100644 index 000000000000..8c659df73a25 --- /dev/null +++ b/examples/cpp/hello-world/src/func.hpp @@ -0,0 +1 @@ +void print_hello(); diff --git a/examples/cpp/hello-world/src/main.cpp b/examples/cpp/hello-world/src/main.cpp new file mode 100644 index 000000000000..c65daaf1f7d1 --- /dev/null +++ b/examples/cpp/hello-world/src/main.cpp @@ -0,0 +1,6 @@ +#include "func.hpp" + +int main() +{ + print_hello(); +} diff --git a/examples/cpp/library/TARGETS b/examples/cpp/library/TARGETS new file mode 100644 index 000000000000..7d07f90e9bb9 --- /dev/null +++ b/examples/cpp/library/TARGETS @@ -0,0 +1,9 @@ +load("//:rules.bzl", "cpp_library") + +cpp_library( + name = "library", + visibility = ["//..."], + srcs = glob(["src/**/*.cpp"]), + headers = glob(["src/**/*.hpp"]), + deps = [], +) diff --git a/examples/cpp/library/src/library.cpp b/examples/cpp/library/src/library.cpp new file mode 100644 index 000000000000..8e097e420056 --- /dev/null +++ b/examples/cpp/library/src/library.cpp @@ -0,0 +1,6 @@ +#include + +void print_hello() +{ + std::cout << "hello from library" << std::endl; +} diff --git a/examples/cpp/library/src/library.hpp b/examples/cpp/library/src/library.hpp new file mode 100644 index 000000000000..8c659df73a25 --- /dev/null +++ b/examples/cpp/library/src/library.hpp @@ -0,0 +1 @@ +void print_hello(); diff --git a/examples/cpp/rules.bzl b/examples/cpp/rules.bzl new file mode 100644 index 000000000000..a9f456ad7e3b --- /dev/null +++ b/examples/cpp/rules.bzl @@ -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()), + }, +) diff --git a/examples/cpp/toolchain.bzl b/examples/cpp/toolchain.bzl new file mode 100644 index 000000000000..7561d3f8b2f4 --- /dev/null +++ b/examples/cpp/toolchain.bzl @@ -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(), + }, +) diff --git a/examples/go/.buckconfig b/examples/go/.buckconfig new file mode 100644 index 000000000000..761b6ee921a4 --- /dev/null +++ b/examples/go/.buckconfig @@ -0,0 +1,5 @@ +[buildfile] +name=TARGETS + +[repositories] +root = . diff --git a/examples/go/binary-toolchain/TARGETS b/examples/go/binary-toolchain/TARGETS new file mode 100644 index 000000000000..e9f1d93416d3 --- /dev/null +++ b/examples/go/binary-toolchain/TARGETS @@ -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", +) diff --git a/examples/go/binary-toolchain/main.go b/examples/go/binary-toolchain/main.go new file mode 100644 index 000000000000..f2ab1c904dbd --- /dev/null +++ b/examples/go/binary-toolchain/main.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("hello from go toolchain") +} diff --git a/examples/go/rules.bzl b/examples/go/rules.bzl new file mode 100644 index 000000000000..95ab3caf0463 --- /dev/null +++ b/examples/go/rules.bzl @@ -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(), + }, +) diff --git a/examples/go/toolchain.bzl b/examples/go/toolchain.bzl new file mode 100644 index 000000000000..916af00a7b0b --- /dev/null +++ b/examples/go/toolchain.bzl @@ -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) + + 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])] diff --git a/examples/readme.md b/examples/readme.md new file mode 100644 index 000000000000..062ce40d5f01 --- /dev/null +++ b/examples/readme.md @@ -0,0 +1,4 @@ +# buck2 examples + +In these folders are some examples on how to get buck2 working with +your favorite languages and tools. diff --git a/examples/rust/.buckconfig b/examples/rust/.buckconfig new file mode 100644 index 000000000000..761b6ee921a4 --- /dev/null +++ b/examples/rust/.buckconfig @@ -0,0 +1,5 @@ +[buildfile] +name=TARGETS + +[repositories] +root = . diff --git a/examples/rust/rules.bzl b/examples/rust/rules.bzl new file mode 100644 index 000000000000..1bda4f4e3c13 --- /dev/null +++ b/examples/rust/rules.bzl @@ -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(), + }, +) diff --git a/examples/rust/rustc/TARGETS b/examples/rust/rustc/TARGETS new file mode 100644 index 000000000000..629692b97fe3 --- /dev/null +++ b/examples/rust/rustc/TARGETS @@ -0,0 +1,6 @@ +load("//:rules.bzl", "rust_binary") + +rust_binary( + name = "main", + file = "./main.rs", +) diff --git a/examples/rust/rustc/main.rs b/examples/rust/rustc/main.rs new file mode 100644 index 000000000000..d0bacf60ab26 --- /dev/null +++ b/examples/rust/rustc/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("hello from rustc"); +}