Skip to content

Commit

Permalink
Support platform specific dependencies (#9)
Browse files Browse the repository at this point in the history
- Supports Linux and Mac OS (64-bit only for now)
- Adds CI run for Mac OS
- Deterministic ordering of dependencies
  • Loading branch information
wildarch authored Oct 22, 2020
1 parent 8dae1e5 commit a99442e
Show file tree
Hide file tree
Showing 30 changed files with 1,458 additions and 137 deletions.
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
startup --windows_enable_symlinks
build --enable_runfiles
31 changes: 15 additions & 16 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
runs-on: ${{ matrix.os }}
strategy:
# Keep running for other platforms when one fails
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
include:
- os: ubuntu-latest
PLATFORM: linux
- os: macos-latest
PLATFORM: darwin
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

- name: Mount bazel cache
Expand All @@ -28,18 +28,17 @@ jobs:
path: "/home/runner/.cache/bazel"
key: bazel

# Runs a single command using the runners shell
- name: Setup bazel
shell: bash
run: |
mkdir bin
cd bin
wget https://github.com/bazelbuild/bazelisk/releases/download/v1.6.1/bazelisk-linux-amd64
chmod +x bazelisk-linux-amd64
mv bazelisk-linux-amd64 bazel
curl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.6.1/bazelisk-${{matrix.PLATFORM}}-amd64 -o bazel
chmod +x bazel
export PATH="$(readlink -e .):$PATH"
# Runs a set of commands using the runners shell
- name: Run tests
shell: bash
run: |
cd $GITHUB_WORKSPACE
./tests/run.sh
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/bazel-*
*.swp
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ rust_library(
"@blackjack_crates_io_serde//:serde",
"@blackjack_crates_io_serde_json//:serde_json",
"@blackjack_crates_io_cargo_lock//:cargo_lock",
"@blackjack_crates_io_cfg_expr//:cfg_expr",
],
edition = "2018",
visibility = ["//visibility:public"],
Expand Down
16 changes: 16 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ cargo_metadata = "0.10"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
cargo-lock = "4.0"
cfg-expr = "0.4"

[package.metadata.blackjack]
prefix = "blackjack_crates_io"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ prefix = "blackjack_crates_io"
Now instead of `@crates_io_serde//:serde`, use `blackjack_crates_io_serde//:serde`.

# Things that don't work yet (but would gladly accept a PR for)
* Support for Windows and Mac. For the moment everything assumes your host and target is `x86_64-unknown-linux-gnu`
* Support for Windows. All that is really needed here is to add it to the list of supported platforms, and fix the platform specific workspace test.
6 changes: 3 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "io_bazel_rules_rust",
strip_prefix = "rules_rust-fdf9655ba95616e0314b4e0ebab40bb0c5fe005c",
url = "https://github.com/bazelbuild/rules_rust/archive/fdf9655ba95616e0314b4e0ebab40bb0c5fe005c.zip",
sha256 = "83727c04706f441f8dfbfd262b0c69e19764a93ce9700167b1bdcf5831bf65e8",
strip_prefix = "rules_rust-3c9acce229b425e583dab7cf9523260bb05ecac3",
url = "https://github.com/bazelbuild/rules_rust/archive/3c9acce229b425e583dab7cf9523260bb05ecac3.zip",
sha256 = "e54a1445656fdea6b50ba5fac92433ce7ca09dc47dc627f80397e7ffe2c0bde7",
)
http_archive(
name = "bazel_skylib",
Expand Down
8 changes: 6 additions & 2 deletions blackjack.bzl
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary")

def blackjack(name=None, manifest="Cargo.toml"):
def blackjack(name=None, manifest="//:Cargo.toml"):
if not name:
fail(msg="'name' must be set when calling blackjack(..)")

rust_binary(
name = name,
srcs = ["@blackjack//:src/bin/blackjack.rs"],
aliases = {"@blackjack//:blackjack_lib": "blackjack"},
args = [
"$(location @blackjack_cargo//:blackjack_cargo.exe)",
"$(location %s) " % manifest,
],
deps = [
"@blackjack_crates_io_cargo_metadata//:cargo_metadata",
"@blackjack_crates_io_cargo_lock//:cargo_lock",
Expand All @@ -16,7 +20,7 @@ def blackjack(name=None, manifest="Cargo.toml"):
edition = "2018",
visibility = ["//visibility:public"],
data = [
"@blackjack_cargo//:cargo",
"@blackjack_cargo//:blackjack_cargo.exe",
manifest,
],
)
Expand Down
50 changes: 50 additions & 0 deletions cargo_dependencies.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,31 @@ rust_library(
)


http_archive(
name = "blackjack_crates_io_cfg_expr",
url = "https://crates.io/api/v1/crates/cfg-expr/0.4.1/download",
sha256 = "6c2be76f06820200669a77ae59a8328c6b8fe4496e8fb7fed02f2806a442c5ff",
strip_prefix = "cfg-expr-0.4.1",
type = "tar.gz",
build_file_content = """
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library")
rust_library(
name = "cfg_expr",
aliases = {},
srcs = glob(["**/*.rs"]),
crate_type = "lib",
deps = ["@blackjack_crates_io_smallvec_1.4.2//:smallvec"],
proc_macro_deps = [],
edition = "2018",
crate_features = ["default"],
rustc_flags = ["--cap-lints=allow"] + [],
visibility = ["//visibility:public"],
)
""",
)


http_archive(
name = "blackjack_crates_io_fixedbitset_0.2.0",
url = "https://crates.io/api/v1/crates/fixedbitset/0.2.0/download",
Expand Down Expand Up @@ -533,6 +558,31 @@ rust_library(
)


http_archive(
name = "blackjack_crates_io_smallvec_1.4.2",
url = "https://crates.io/api/v1/crates/smallvec/1.4.2/download",
sha256 = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252",
strip_prefix = "smallvec-1.4.2",
type = "tar.gz",
build_file_content = """
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library")
rust_library(
name = "smallvec",
aliases = {},
srcs = glob(["**/*.rs"]),
crate_type = "lib",
deps = [],
proc_macro_deps = [],
edition = "2018",
crate_features = [],
rustc_flags = ["--cap-lints=allow"] + [],
visibility = ["//visibility:public"],
)
""",
)


http_archive(
name = "blackjack_crates_io_syn_1.0.32",
url = "https://crates.io/api/v1/crates/syn/1.0.32/download",
Expand Down
51 changes: 18 additions & 33 deletions src/bin/blackjack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,31 @@ use blackjack::Blackjack;
use cargo_lock::Lockfile;
use cargo_metadata::MetadataCommand;
use std::io::Write;
use std::path::{Path, PathBuf};

const CARGO_TOML_RUNFILES_PATH: &str = "Cargo.toml";
const CARGO_RUNFILES_PATH: &str = "external/blackjack_cargo/cargo";

fn workspace_path() -> PathBuf {
// This is somewhat of an implementation detail
let mut cargo_toml_path = std::fs::read_link(CARGO_TOML_RUNFILES_PATH)
.unwrap_or_else(|_| PathBuf::from("Cargo.toml"));
cargo_toml_path.pop();
cargo_toml_path
}

fn set_cargo_path(metadata: &mut MetadataCommand) {
let cargo_runfiles_path = Path::new(CARGO_RUNFILES_PATH);
if cargo_runfiles_path.exists() {
eprintln!("Found cargo in runfiles: {}", cargo_runfiles_path.display());
metadata.cargo_path(cargo_runfiles_path);
} else {
eprintln!(
"Using default cargo in path. Working dir: {}",
std::env::current_dir().unwrap().display()
);
}
}
use std::path::PathBuf;

fn main() {
let workspace_path = workspace_path();
let cargo_toml_path = workspace_path.join("Cargo.toml");
let mut args = std::env::args().skip(1);
let cargo_path = args.next().expect("No cargo path provided (1st argument)");
let mut cargo_toml_path: PathBuf = args
.next()
.expect("No Cargo.toml path provided (2nd argument)")
.into();
// If the Cargo.toml path is a symlink, resolve it first
if let Ok(p) = std::fs::read_link(&cargo_toml_path) {
cargo_toml_path = p;
}

let mut metadata = MetadataCommand::new();
metadata.manifest_path(&cargo_toml_path).other_options(vec![
// TODO make this configurable
"--filter-platform".to_string(),
"x86_64-unknown-linux-gnu".to_string(),
]);
set_cargo_path(&mut metadata);
metadata
.cargo_path(cargo_path)
.manifest_path(&cargo_toml_path);

eprintln!("Blackjack will run `cargo metadata`, which may update your `Cargo.lock` file if it is not up to date");

let workspace_path = {
cargo_toml_path.pop();
cargo_toml_path
};
let output_path = workspace_path.join("cargo_dependencies.bzl");
eprintln!("Writing output to {}", output_path.display());
eprintln!("Press enter to continue, or Ctrl-C to abort");
Expand Down
Loading

0 comments on commit a99442e

Please sign in to comment.