|
| 1 | +// Copyright Materialize, Inc. and contributors. All rights reserved. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the LICENSE file. |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0. |
| 9 | + |
| 10 | +//! Provides access to tools required in build scripts. |
| 11 | +//! |
| 12 | +//! For example, many crates have a build script that depends on the Protobuf |
| 13 | +//! compiler, `protoc`. If we're building with Cargo we'll bootstrap `protoc` |
| 14 | +//! by compiling it with [`protobuf-src`], but if we're building with Bazel |
| 15 | +//! then we'll use the version of `protoc` included in the runfiles. |
| 16 | +
|
| 17 | +use cfg_if::cfg_if; |
| 18 | +use std::path::PathBuf; |
| 19 | + |
| 20 | +// Note: This crate's BUILD.bazel compiles with the rustc flag `--cfg=bazel`. |
| 21 | + |
| 22 | +// Runfiles are a Bazel concept, they're a way to provide files at execution |
| 23 | +// time. This dependency is provided only by the Bazel build. |
| 24 | +#[cfg(bazel)] |
| 25 | +extern crate runfiles; |
| 26 | + |
| 27 | +/// Returns the path to `protoc`. |
| 28 | +/// |
| 29 | +/// Looks for `protoc` in the following places: |
| 30 | +/// |
| 31 | +/// * Bazel runfiles, if we're building with Bazel. |
| 32 | +/// * Bootstraps `protoc` via protobuf-src, if default features are enabled. |
| 33 | +/// * `PROTOC` environment variable, if it's set. |
| 34 | +/// * The system's `$PATH`, via [`which`]. |
| 35 | +/// |
| 36 | +/// If `protoc` can't be found then this function will panic. |
| 37 | +pub fn protoc() -> PathBuf { |
| 38 | + cfg_if! { |
| 39 | + if #[cfg(bazel)] { |
| 40 | + let r = runfiles::Runfiles::create().unwrap(); |
| 41 | + r.rlocation("protobuf/protoc") |
| 42 | + } else if #[cfg(feature = "protobuf-src")] { |
| 43 | + protobuf_src::protoc() |
| 44 | + } else { |
| 45 | + // If we're not building with Bazel, nor have the `protobuf-src` |
| 46 | + // feature specified, then try using the system's `protoc`. |
| 47 | + match std::option_env!("PROTOC") { |
| 48 | + Some(path) => PathBuf::from(path), |
| 49 | + None => which::which("protoc").expect("protoc to exist on system"), |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/// Returns the path to the protobuf includes directory. |
| 56 | +/// |
| 57 | +/// Note: this is primarily used to include "well known types". |
| 58 | +pub fn protoc_include() -> PathBuf { |
| 59 | + cfg_if! { |
| 60 | + if #[cfg(bazel)] { |
| 61 | + let r = runfiles::Runfiles::create().unwrap(); |
| 62 | + r.rlocation("protobuf/src") |
| 63 | + } else if #[cfg(feature = "protobuf-src")] { |
| 64 | + protobuf_src::include() |
| 65 | + } else { |
| 66 | + let path = std::option_env!("PROTOC_INCLUDE").unwrap_or_default(); |
| 67 | + PathBuf::from(path) |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments