Skip to content

Commit 8424775

Browse files
authored
build(protobuf): Use crate cmake (#1137)
Let the crate `cmake` handle the interaction with CMake executable. This simplifies our codebase and improves compatibility with other platforms. `CMAKE_BUILD_TYPE` is handle automatically based on `opt-level`. Set `CMAKE_GENERATOR` environment variable in CI instead of hardcoded dependency on `ninja-build`.
1 parent 21208ab commit 8424775

File tree

3 files changed

+18
-27
lines changed

3 files changed

+18
-27
lines changed

.github/actions/setup-ninja/action.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ runs:
33
steps:
44
- name: install ninja
55
if: runner.os == 'macOS'
6-
run: brew install ninja
6+
run: |
7+
brew install ninja
8+
echo "CMAKE_GENERATOR=Ninja" >> "$GITHUB_ENV"
79
shell: bash
810

911
- name: install ninja
1012
if: runner.os == 'Windows'
11-
run: choco install ninja
13+
run: |
14+
choco install ninja
15+
echo "CMAKE_GENERATOR=Ninja" >> "$GITHUB_ENV"
1216
shell: bash
1317

1418
- name: install ninja
1519
if: runner.os == 'Linux'
16-
run: sudo apt-get install ninja-build
20+
run: |
21+
sudo apt-get install ninja-build
22+
echo "CMAKE_GENERATOR=Ninja" >> "$GITHUB_ENV"
1723
shell: bash

protobuf/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ prost-types = { path = "../prost-types" }
1313
anyhow = "1.0.1"
1414
prost-build = { path = "../prost-build" }
1515
tempfile = "3"
16+
cmake = "0.1.51"
1617

1718
[package.metadata.cargo-machete]
1819
ignored = ["prost", "prost-types"]

protobuf/build.rs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -118,35 +118,19 @@ fn install_conformance_test_runner(
118118
prefix_dir: &Path,
119119
) -> Result<()> {
120120
// Build and install protoc, the protobuf libraries, and the conformance test runner.
121-
let rc = Command::new("cmake")
122-
.arg("-GNinja")
123-
.arg(src_dir.join("cmake"))
124-
.arg("-DCMAKE_BUILD_TYPE=DEBUG")
125-
.arg(format!("-DCMAKE_INSTALL_PREFIX={}", prefix_dir.display()))
126-
.arg("-Dprotobuf_BUILD_CONFORMANCE=ON")
127-
.arg("-Dprotobuf_BUILD_TESTS=OFF")
128-
.current_dir(build_dir)
129-
.status()
130-
.context("failed to execute CMake")?;
131-
assert!(rc.success(), "protobuf CMake failed");
132-
133-
let num_jobs = env::var("NUM_JOBS").context("NUM_JOBS environment variable not set")?;
134-
135-
let rc = Command::new("ninja")
136-
.arg("-j")
137-
.arg(&num_jobs)
138-
.arg("install")
139-
.current_dir(build_dir)
140-
.status()
141-
.context("failed to execute ninja protobuf")?;
142-
ensure!(rc.success(), "failed to make protobuf");
121+
cmake::Config::new(src_dir.join("cmake"))
122+
.define("CMAKE_INSTALL_PREFIX", prefix_dir)
123+
.define("protobuf_BUILD_CONFORMANCE", "ON")
124+
.define("protobuf_BUILD_TESTS", "OFF")
125+
.out_dir(build_dir)
126+
.build();
143127

144128
// Install the conformance-test-runner binary, since it isn't done automatically.
145129
fs::copy(
146-
build_dir.join("conformance_test_runner"),
130+
build_dir.join("build").join("conformance_test_runner"),
147131
prefix_dir.join("bin").join("conformance-test-runner"),
148132
)
149-
.context("failed to move conformance-test-runner")?;
133+
.context("failed to copy conformance-test-runner")?;
150134

151135
Ok(())
152136
}

0 commit comments

Comments
 (0)