diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 000000000000..e94df49c536e --- /dev/null +++ b/examples/README.md @@ -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. diff --git a/examples/no_prelude/cpp/.buckconfig b/examples/no_prelude/cpp/.buckconfig new file mode 100644 index 000000000000..0427950719b2 --- /dev/null +++ b/examples/no_prelude/cpp/.buckconfig @@ -0,0 +1,5 @@ +[buildfile] +name=BUILD + +[repositories] +root = . diff --git a/examples/no_prelude/cpp/hello-world/BUILD b/examples/no_prelude/cpp/hello-world/BUILD new file mode 100644 index 000000000000..99c296a9413b --- /dev/null +++ b/examples/no_prelude/cpp/hello-world/BUILD @@ -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 = [], +) diff --git a/examples/no_prelude/cpp/hello-world/src/func.cpp b/examples/no_prelude/cpp/hello-world/src/func.cpp new file mode 100644 index 000000000000..e9d214567734 --- /dev/null +++ b/examples/no_prelude/cpp/hello-world/src/func.cpp @@ -0,0 +1,5 @@ +#include + +void print_hello() { + std::cout << "hellp from cpp" << std::endl; +} diff --git a/examples/no_prelude/cpp/hello-world/src/func.hpp b/examples/no_prelude/cpp/hello-world/src/func.hpp new file mode 100644 index 000000000000..8c659df73a25 --- /dev/null +++ b/examples/no_prelude/cpp/hello-world/src/func.hpp @@ -0,0 +1 @@ +void print_hello(); diff --git a/examples/no_prelude/cpp/hello-world/src/main.cpp b/examples/no_prelude/cpp/hello-world/src/main.cpp new file mode 100644 index 000000000000..dd06eee87b92 --- /dev/null +++ b/examples/no_prelude/cpp/hello-world/src/main.cpp @@ -0,0 +1,5 @@ +#include "func.hpp" + +int main() { + print_hello(); +} diff --git a/examples/no_prelude/cpp/library/BUILD b/examples/no_prelude/cpp/library/BUILD new file mode 100644 index 000000000000..e809793a0762 --- /dev/null +++ b/examples/no_prelude/cpp/library/BUILD @@ -0,0 +1,9 @@ +load("//:rules.bzl", "cpp_library") + +cpp_library( + name = "library", + srcs = glob(["src/**/*.cpp"]), + headers = glob(["src/**/*.hpp"]), + visibility = ["PUBLIC"], + deps = [], +) diff --git a/examples/no_prelude/cpp/library/src/library.cpp b/examples/no_prelude/cpp/library/src/library.cpp new file mode 100644 index 000000000000..088a1ecc2e2d --- /dev/null +++ b/examples/no_prelude/cpp/library/src/library.cpp @@ -0,0 +1,5 @@ +#include + +void print_hello() { + std::cout << "hello from library" << std::endl; +} diff --git a/examples/no_prelude/cpp/library/src/library.hpp b/examples/no_prelude/cpp/library/src/library.hpp new file mode 100644 index 000000000000..8c659df73a25 --- /dev/null +++ b/examples/no_prelude/cpp/library/src/library.hpp @@ -0,0 +1 @@ +void print_hello(); diff --git a/examples/no_prelude/cpp/rules.bzl b/examples/no_prelude/cpp/rules.bzl new file mode 100644 index 000000000000..793c41e407d5 --- /dev/null +++ b/examples/no_prelude/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 = { + "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()), + }, +) diff --git a/examples/no_prelude/cpp/toolchain.bzl b/examples/no_prelude/cpp/toolchain.bzl new file mode 100644 index 000000000000..0bbf8ab69361 --- /dev/null +++ b/examples/no_prelude/cpp/toolchain.bzl @@ -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(), + }, +) diff --git a/examples/no_prelude/go/.buckconfig b/examples/no_prelude/go/.buckconfig new file mode 100644 index 000000000000..0427950719b2 --- /dev/null +++ b/examples/no_prelude/go/.buckconfig @@ -0,0 +1,5 @@ +[buildfile] +name=BUILD + +[repositories] +root = . diff --git a/examples/no_prelude/go/binary-toolchain/BUILD b/examples/no_prelude/go/binary-toolchain/BUILD new file mode 100644 index 000000000000..9d23a8e49d76 --- /dev/null +++ b/examples/no_prelude/go/binary-toolchain/BUILD @@ -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 = [], +) diff --git a/examples/no_prelude/go/binary-toolchain/main.go b/examples/no_prelude/go/binary-toolchain/main.go new file mode 100644 index 000000000000..2bb96fc5c970 --- /dev/null +++ b/examples/no_prelude/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/no_prelude/go/rules.bzl b/examples/no_prelude/go/rules.bzl new file mode 100644 index 000000000000..e6f7682fbf08 --- /dev/null +++ b/examples/no_prelude/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 = { + "deps": attr.list(attr.dep()), + "srcs": attr.list(attr.source()), + "toolchain": attr.dep(), + }, +) diff --git a/examples/no_prelude/go/toolchain.bzl b/examples/no_prelude/go/toolchain.bzl new file mode 100644 index 000000000000..8177268c2dc8 --- /dev/null +++ b/examples/no_prelude/go/toolchain.bzl @@ -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])] diff --git a/examples/no_prelude/rust/.buckconfig b/examples/no_prelude/rust/.buckconfig new file mode 100644 index 000000000000..0427950719b2 --- /dev/null +++ b/examples/no_prelude/rust/.buckconfig @@ -0,0 +1,5 @@ +[buildfile] +name=BUILD + +[repositories] +root = . diff --git a/examples/no_prelude/rust/rules.bzl b/examples/no_prelude/rust/rules.bzl new file mode 100644 index 000000000000..1bda4f4e3c13 --- /dev/null +++ b/examples/no_prelude/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/no_prelude/rust/rustc/BUILD b/examples/no_prelude/rust/rustc/BUILD new file mode 100644 index 000000000000..629692b97fe3 --- /dev/null +++ b/examples/no_prelude/rust/rustc/BUILD @@ -0,0 +1,6 @@ +load("//:rules.bzl", "rust_binary") + +rust_binary( + name = "main", + file = "./main.rs", +) diff --git a/examples/no_prelude/rust/rustc/main.rs b/examples/no_prelude/rust/rustc/main.rs new file mode 100644 index 000000000000..d0bacf60ab26 --- /dev/null +++ b/examples/no_prelude/rust/rustc/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("hello from rustc"); +}