Skip to content

Commit

Permalink
Revert "Remove build.rs (tokio-rs#657)"
Browse files Browse the repository at this point in the history
This reverts commit ac57645.
  • Loading branch information
mdrach committed Aug 4, 2022
1 parent 7dea058 commit 1869f5d
Show file tree
Hide file tree
Showing 33 changed files with 246 additions and 141 deletions.
34 changes: 18 additions & 16 deletions .github/workflows/continuous-integration-workflow.yaml
Expand Up @@ -37,10 +37,6 @@ jobs:
default: true
profile: minimal
components: clippy
- name: Install Protoc
uses: arduino/setup-protoc@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: install ninja
uses: seanmiddleditch/gha-setup-ninja@v3
- name: clippy
Expand All @@ -63,10 +59,6 @@ jobs:
- uses: actions/checkout@v3
with:
submodules: recursive
- name: Install Protoc
uses: arduino/setup-protoc@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: install ninja
uses: seanmiddleditch/gha-setup-ninja@v3
- name: cargo update -Zminimal-versions
Expand Down Expand Up @@ -104,10 +96,6 @@ jobs:
toolchain: ${{ matrix.toolchain }}
default: true
profile: minimal
- name: Install Protoc
uses: arduino/setup-protoc@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: install ninja
uses: seanmiddleditch/gha-setup-ninja@v3
- uses: Swatinem/rust-cache@v1
Expand Down Expand Up @@ -135,10 +123,6 @@ jobs:
toolchain: nightly
default: true
profile: minimal
- name: Install Protoc
uses: arduino/setup-protoc@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
- uses: Swatinem/rust-cache@v1
- name: install cargo-no-std-check
uses: actions-rs/cargo@v1
Expand All @@ -165,3 +149,21 @@ jobs:
with:
command: check
args: --manifest-path prost-build/Cargo.toml

vendored:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v2
with:
submodules: recursive
- name: install toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
default: true
profile: minimal
- uses: Swatinem/rust-cache@v1
- name: cargo check
run: cd test-vendored && cargo check

2 changes: 0 additions & 2 deletions .gitignore
@@ -1,4 +1,2 @@
target
Cargo.lock

.DS_Store
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -36,6 +36,7 @@ exclude = [
"fuzz",
# Same counts for the afl fuzz targets
"afl",
"test-vendored"
]

[lib]
Expand Down
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.
13 changes: 4 additions & 9 deletions bootstrap/src/lib.rs
Expand Up @@ -6,19 +6,14 @@ use std::fs;
use std::io::Read;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;

/// Test which bootstraps protobuf.rs and compiler.rs from the .proto definitions in the Protobuf
/// repo. Ensures that the checked-in compiled versions are up-to-date.
#[test]
fn bootstrap() {
let include = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.join("tests")
.join("src")
.join("include");
let protobuf = include.join("google").join("protobuf");
let protobuf = prost_build::protoc_include()
.join("google")
.join("protobuf");

let tempdir = tempfile::Builder::new()
.prefix("prost-types-bootstrap")
Expand All @@ -45,7 +40,7 @@ fn bootstrap() {
protobuf.join("timestamp.proto"),
protobuf.join("type.proto"),
],
&[include],
&[""],
)
.unwrap();

Expand Down
10 changes: 7 additions & 3 deletions prost-build/Cargo.toml
Expand Up @@ -16,6 +16,7 @@ rust-version = "1.56"

[features]
default = []
vendored = []
# When MSRV moves to 1.60, these can change to dep:
cleanup-markdown = ["pulldown-cmark", "pulldown-cmark-to-cmark"]

Expand All @@ -31,12 +32,15 @@ prost-types = { version = "0.11.0", path = "../prost-types", default-features =
tempfile = "3"
lazy_static = "1.4.0"
regex = { version = "1.5.5", default-features = false, features = ["std", "unicode-bool"] }
which = "4"

# These two must be kept in sync, used for `cleanup-markdown` feature.
# These two must be kept in sync
pulldown-cmark = { version = "0.9.1", optional = true, default-features = false }
pulldown-cmark-to-cmark = { version = "10.0.1", optional = true }

[build-dependencies]
which = { version = "4", default-features = false }
cfg-if = "1"
cmake = "0.1"

[dev-dependencies]
env_logger = { version = "0.8", default-features = false }

120 changes: 120 additions & 0 deletions prost-build/build.rs
@@ -0,0 +1,120 @@
//! Finds the appropriate `protoc` binary and Protobuf include directory for this host, and outputs
//! build directives so that the main `prost-build` crate can use them.
//!
//! This build script attempts to find `protoc` in a few ways:
//!
//! 1. If `PROTOC_NO_VENDOR` is enabled, it will check the `PROTOC` environment variable
//! then check the `PATH` for a `protoc` or `protoc.exe`.
//! 2. If the `vendored` feature flag is enabled or `protoc` can't be found via the environment
//! variable or in the `PATH` then `prost-build` will attempt to build `protoc` from the
//! bundled source code.
//! 3. Otherwise, it will attempt to execute from the `PATH` and fail if it does not exist.
//!
//! The following locations are checked for the Protobuf include directory in decreasing priority:
//!
//! 1. The `PROTOC_INCLUDE` environment variable.
//! 2. The bundled Protobuf include directory.
//!

use cfg_if::cfg_if;
use std::env;
use std::path::PathBuf;
use which::which;

/// Returns the path to the location of the bundled Protobuf artifacts.
fn bundle_path() -> PathBuf {
env::current_dir().unwrap().join("third-party")
}

/// Returns the path to the Protobuf include directory pointed to by the `PROTOC_INCLUDE`
/// environment variable, if it is set.
fn env_protoc_include() -> Option<PathBuf> {
let protoc_include = match env::var_os("PROTOC_INCLUDE") {
Some(path) => PathBuf::from(path),
None => return None,
};

if !protoc_include.exists() {
panic!(
"PROTOC_INCLUDE environment variable points to non-existent directory ({:?})",
protoc_include
);
}
if !protoc_include.is_dir() {
panic!(
"PROTOC_INCLUDE environment variable points to a non-directory file ({:?})",
protoc_include
);
}

Some(protoc_include)
}

/// Returns the path to the bundled Protobuf include directory.
fn bundled_protoc_include() -> PathBuf {
bundle_path().join("include")
}

/// Check for `protoc` via the `PROTOC` env var or in the `PATH`.
fn path_protoc() -> Option<PathBuf> {
env::var_os("PROTOC")
.map(PathBuf::from)
.or_else(|| which("protoc").ok())
}

/// Returns true if the vendored flag is enabled.
fn vendored() -> bool {
cfg_if! {
if #[cfg(feature = "vendored")] {
true
} else {
false
}
}
}

/// Compile `protoc` via `cmake`.
fn compile() -> Option<PathBuf> {
let protobuf_src = bundle_path().join("protobuf").join("cmake");

println!("cargo:rerun-if-changed={}", protobuf_src.display());

let dst = cmake::Config::new(protobuf_src)
.define("protobuf_BUILD_TESTS", "OFF")
.build();

Some(dst.join("bin").join("protoc"))
}

/// Try to find a `protoc` through a few methods.
///
/// Check module docs for more info.
fn protoc() -> Option<PathBuf> {
if env::var_os("PROTOC_NO_VENDOR").is_some() {
path_protoc()
} else if vendored() {
compile()
} else {
path_protoc().or_else(compile)
}
}

fn main() {
let protoc = protoc().expect(
"Failed to find or build the protoc binary. The PROTOC environment \
is not set, `protoc` is not in PATH or you are missing the requirements to compile protobuf \
from source. \n \
Check out the `prost-build` README for instructions on the requirements: \
https://github.com/tokio-rs/prost#generated-code",
);

let protoc_include = env_protoc_include().unwrap_or_else(bundled_protoc_include);

println!("cargo:rustc-env=PROTOC={}", protoc.display());
println!(
"cargo:rustc-env=PROTOC_INCLUDE={}",
protoc_include.display()
);
println!("cargo:rerun-if-env-changed=PROTOC");
println!("cargo:rerun-if-env-changed=PROTOC_INCLUDE");
}

0 comments on commit 1869f5d

Please sign in to comment.