mz_build_tools/lib.rs
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
12use cfg_if::cfg_if;
13use std::path::PathBuf;
14
15/// Returns the path to `protoc`.
16///
17/// Looks for `protoc` in the following places:
18///
19/// * Bootstraps `protoc` via protobuf-src, if default features are enabled.
20/// * `PROTOC` environment variable, if it's set.
21/// * The system's `$PATH`, via [`which`].
22///
23/// If `protoc` can't be found then this function will panic.
24pub fn protoc() -> PathBuf {
25 cfg_if! {
26 if #[cfg(feature = "protobuf-src")] {
27 protobuf_src::protoc()
28 } else {
29 // If we're not building with Bazel, nor have the `protobuf-src`
30 // feature specified, then try using the system's `protoc`.
31 match std::option_env!("PROTOC") {
32 Some(path) => PathBuf::from(path),
33 None => which::which("protoc").expect("protoc to exist on system"),
34 }
35 }
36 }
37}
38
39/// Returns the path to the protobuf includes directory.
40///
41/// Note: this is primarily used to include "well known types".
42pub fn protoc_include() -> PathBuf {
43 cfg_if! {
44 if #[cfg(feature = "protobuf-src")] {
45 protobuf_src::include()
46 } else {
47 let path = std::option_env!("PROTOC_INCLUDE").unwrap_or_default();
48 PathBuf::from(path)
49 }
50 }
51}