Skip to content

Commit

Permalink
Split into multiple examples, one for hybrid Cargo + Buck projects
Browse files Browse the repository at this point in the history
  • Loading branch information
cormacrelf committed Jan 13, 2025
1 parent cd39aa4 commit ea56b0e
Show file tree
Hide file tree
Showing 34 changed files with 306 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions examples/hybrid/.buckconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[repositories]
root = .
prelude = prelude
toolchains = toolchains
none = none

[repository_aliases]
config = prelude
fbcode = none
fbsource = none
buck = none

[external_cells]
prelude = bundled

[parser]
target_platform_detector_spec = target:root//...->prelude//platforms:default

[rust]
default_edition = 2021
remap_src_paths = yes
Empty file added examples/hybrid/.buckroot
Empty file.
1 change: 1 addition & 0 deletions examples/hybrid/.buckversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
latest
7 changes: 7 additions & 0 deletions examples/hybrid/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/buck-out
/target
.cargo

# You can also preserve config.toml, if you need to change cargo settings.
# !.cargo/config.toml

16 changes: 16 additions & 0 deletions examples/hybrid/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions examples/hybrid/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[workspace]

members = ["project"]
resolver = "2"

# Local patches - typically git references
[patch.crates-io]
36 changes: 36 additions & 0 deletions examples/hybrid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Hybrid Cargo + Buck project

This example is for using both Cargo and Buck on the same project. This is
useful if you want to:

- use Cargo to do things like `cargo fmt`;
- have it work out of the box with `rust-analyzer`, so it's easy to contribute;
- ultimately build your code with Buck;
- or support building with either.

The key is the `reindeer.toml` in the root, which stitches together a Cargo
workspace and a Buck package (`third-party`) to buckify Cargo dependencies
into.

## Running the example

```sh
cargo install --path .
cd examples/hybrid

# Add a dependency
cargo add -p project serde --features derive

# Buckify deps. Do this every time you modify the Cargo.tomls, in general.
reindeer buckify

# Note that both buck and cargo can build this project
buck2 run //project:test
cargo run -p project
```

## Missing from this example

This example does no vendoring or anything fancy with fixups. You will have to
follow the other examples to get a non-trivial Cargo project built with Buck.

7 changes: 7 additions & 0 deletions examples/hybrid/project/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rust_binary(
name = "test",
srcs = ["src/main.rs"],
deps = [
"//third-party:once_cell",
],
)
14 changes: 14 additions & 0 deletions examples/hybrid/project/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
# Pseudo-package whose dependencies are imported and buckified
name = "project"
version = "0.0.0"
edition = "2021"
publish = false

# List of packages to be imported, with version constraints, features
# and all options Cargo supports.
[dependencies]
once_cell = "1.4"

# Local patches - typically git references
[patch.crates-io]
16 changes: 16 additions & 0 deletions examples/hybrid/project/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

use once_cell::sync::Lazy;

const MAGIC: &str = "this is a magic string";

static SPECIAL: Lazy<String> = Lazy::new(|| MAGIC.to_string());

fn main() {
println!("{}", &*SPECIAL);
}
97 changes: 97 additions & 0 deletions examples/hybrid/reindeer.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
##
## Reindeer Config for a hybrid Cargo + Buck project
##

# Paths
#
# This is a workspace Cargo.toml. For all default workspace members, it will
# recursively buckify their dependencies to third-party/BUCK.
manifest_path = "Cargo.toml"
third_party_dir = "third-party"
include_top_level = false
fixup_templates = true

# Platforms (see other examples)
#
[platform.linux-x86_64]
x86_64-unknown-linux-gnu = [] # true for a boolean test
target_arch = ["x86_64"]
target_endian = ["little"]
target_env = ["gnu"]
target_family = ["unix"]
target_feature = ["fxsr", "sse", "sse2"]
target_has_atomic = ["8", "16", "32", "64", "ptr"]
target_os = ["linux"]
target_pointer_width = ["64"]
target_vendor = ["unknown"]

# Fortanix SGX
[platform.fortanix-sgx]
x86_64-fortanix-unknown-sgx = []
target_arch = ["x86_64"]
target_endian = ["little"]
target_env = ["sgx"]
target_feature = ["fxsr", "rdrand", "rdseed", "sse", "sse2"]
target_has_atomic = ["8", "16", "32", "64", "ptr"]
target_os = ["unknown"]
target_pointer_width = ["64"]
target_vendor = ["fortanix"]

# macOS - x86_64-apple-darwin
[platform.macos]
x86_64-apple-darwin = []
target_arch = ["x86_64"]
target_endian = ["little"]
target_env = []
target_family = ["unix"]
target_feature = ["cmpxchg16b", "fxsr", "sse", "sse2", "sse3", "ssse3"]
target_has_atomic = ["8", "16", "32", "64", "128", "ptr"]
target_os = ["macos"]
target_pointer_width = ["64"]
target_vendor = ["apple"]

# Windows with MSVC toolchain - x86_64-pc-windows-msvc
[platform.windows]
x86_64-pc-windows-msvc = []
target_arch = ["x86_64"]
target_endian = ["little"]
target_env = ["msvc"]
target_family = ["windows"]
target_feature = ["fxsr", "sse", "sse2"]
target_has_atomic = ["8", "16", "32", "64", "ptr"]
target_os = ["windows"]
target_pointer_width = ["64"]
target_vendor = ["pc"]

# Windows with GNU toolchain - x86_64-pc-windows-gnu
[platform.windows-gnu]
x86_64-pc-windows-gnu = []
target_arch = ["x86_64"]
target_endian = ["little"]
target_env = ["gnu"]
target_family = ["windows"]
target_feature = ["fxsr", "sse", "sse2"]
target_has_atomic = ["8", "16", "32", "64", "ptr"]
target_os = ["windows"]
target_pointer_width = ["64"]
target_vendor = ["pc"]

# Configuration for generated BUCK file
# See other examples
[buck]
file_name = "BUCK"
rust_library = "cargo.rust_library"
rust_binary = "cargo.rust_binary"
buildscript_genrule = "buildscript_run"
buckfile_imports = """
load("@prelude//rust:cargo_buildscript.bzl", "buildscript_run")
load("@prelude//rust:cargo_package.bzl", "cargo")
"""
generated_file_header = """
##
## \u0040generated by reindeer
## Do not edit by hand.
##
## See README.md for directions on how to update this.
##
"""
31 changes: 31 additions & 0 deletions examples/hybrid/third-party/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Ignore Cargo-related stuff
.cargo/registry
.cargo/git
/registry
/git
.package-cache

# Various cruft in vendored packages
vendor/*/target
vendor/*/Cargo.lock
vendor/**/.buckconfig
vendor/**/BUCK
vendor/**/OWNERS
vendor/**/*~
vendor/**/*.bzl
vendor/*/.github/**
vendor/*/.appveyor.yml
vendor/*/.travis.yml
/target/**

# Bad Windows names - oh for case-insensitive regex matching!
vendor/**/[Aa][Uu][Xx]
vendor/**/[Aa][Uu][Xx].*
vendor/**/[Cc][Oo][Mm][1-9]
vendor/**/[Cc][Oo][Mm][1-9].*
vendor/**/[Cc][Oo][Nn]
vendor/**/[Cc][Oo][Nn].*
vendor/**/[Ll][Pp][Tt][1-9]
vendor/**/[Ll][Pp][Tt][1-9].*
vendor/**/[Nn][Uu][Ll]
vendor/**/[Nn][Uu][Ll].*
48 changes: 48 additions & 0 deletions examples/hybrid/third-party/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
##
## @generated by reindeer
## Do not edit by hand.
##
## See README.md for directions on how to update this.
##

load("@prelude//rust:cargo_buildscript.bzl", "buildscript_run")
load("@prelude//rust:cargo_package.bzl", "cargo")

alias(
name = "once_cell",
actual = ":once_cell-1.20.2",
visibility = ["PUBLIC"],
)

http_archive(
name = "once_cell-1.20.2.crate",
sha256 = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775",
strip_prefix = "once_cell-1.20.2",
urls = ["https://static.crates.io/crates/once_cell/1.20.2/download"],
visibility = [],
)

cargo.rust_library(
name = "once_cell-1.20.2",
srcs = [":once_cell-1.20.2.crate"],
crate = "once_cell",
crate_root = "once_cell-1.20.2.crate/src/lib.rs",
edition = "2021",
features = [
"alloc",
"default",
"race",
"std",
],
visibility = [],
)

cargo.rust_binary(
name = "project-0.0.0-project",
srcs = ["../project/src/main.rs"],
crate = "project",
crate_root = "../project/src/main.rs",
edition = "2021",
visibility = [],
deps = [":once_cell-1.20.2"],
)
Empty file.
2 changes: 2 additions & 0 deletions examples/hybrid/toolchains/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
load("@prelude//toolchains:demo.bzl", "system_demo_toolchains")
system_demo_toolchains()
3 changes: 3 additions & 0 deletions examples/hybrid/top/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Dummy source to keep Cargo happy

fn main() {}

0 comments on commit ea56b0e

Please sign in to comment.