Skip to content

Commit c858525

Browse files
committed
build: resolve protoc from vcpkg at runtime
1 parent 716daa5 commit c858525

3 files changed

Lines changed: 78 additions & 13 deletions

File tree

CMakeLists.txt

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,26 +104,21 @@ set(RUST_FFI_DEPENDS
104104
${CMAKE_CURRENT_LIST_DIR}/rust/lib.rs
105105
${CMAKE_CURRENT_LIST_DIR}/rust/scanner.rs
106106
${CMAKE_CURRENT_LIST_DIR}/rust/types.rs
107-
${CMAKE_CURRENT_LIST_DIR}/rust/writer.rs)
107+
${CMAKE_CURRENT_LIST_DIR}/rust/writer.rs
108+
${CMAKE_CURRENT_LIST_DIR}/scripts/protoc_wrapper.sh
109+
${CMAKE_CURRENT_LIST_DIR}/scripts/protoc_wrapper.cmd)
108110

109111
set(PROTOC_BIN "")
110-
if(DEFINED ENV{VCPKG_ROOT})
111-
if(DEFINED ENV{VCPKG_HOST_TRIPLET})
112-
set(PROTOC_BIN "$ENV{VCPKG_ROOT}/installed/$ENV{VCPKG_HOST_TRIPLET}/tools/protobuf/protoc")
113-
elseif(DEFINED ENV{VCPKG_TARGET_TRIPLET})
114-
set(PROTOC_BIN "$ENV{VCPKG_ROOT}/installed/$ENV{VCPKG_TARGET_TRIPLET}/tools/protobuf/protoc")
115-
endif()
116-
endif()
117-
if(WIN32 AND NOT PROTOC_BIN STREQUAL "" AND NOT PROTOC_BIN MATCHES "\\.exe$")
118-
set(PROTOC_BIN "${PROTOC_BIN}.exe")
112+
if(WIN32)
113+
set(PROTOC_BIN "${CMAKE_CURRENT_LIST_DIR}/scripts/protoc_wrapper.cmd")
114+
else()
115+
set(PROTOC_BIN "${CMAKE_CURRENT_LIST_DIR}/scripts/protoc_wrapper.sh")
119116
endif()
120117

121118
set(RUST_CARGO_ENV
122119
CARGO_TARGET_DIR=${RUST_TARGET_DIR}
123120
MACOSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET})
124-
if(NOT PROTOC_BIN STREQUAL "")
125-
list(APPEND RUST_CARGO_ENV PROTOC=${PROTOC_BIN})
126-
endif()
121+
list(APPEND RUST_CARGO_ENV PROTOC=${PROTOC_BIN})
127122

128123
# Building both debug and release Rust artifacts is expensive and can trigger OOMs in CI.
129124
# For single-config generators (Ninja/Makefiles), only build the active configuration's artifact.

scripts/protoc_wrapper.cmd

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
@echo off
2+
setlocal
3+
4+
if not "%PROTOC_REAL%"=="" (
5+
if exist "%PROTOC_REAL%" (
6+
"%PROTOC_REAL%" %*
7+
exit /b %errorlevel%
8+
)
9+
)
10+
11+
set "VCPKG_ROOT=%VCPKG_ROOT%"
12+
if "%VCPKG_ROOT%"=="" goto :fallback
13+
14+
set "TRIPLET=%VCPKG_HOST_TRIPLET%"
15+
if "%TRIPLET%"=="" set "TRIPLET=%VCPKG_TARGET_TRIPLET%"
16+
if "%TRIPLET%"=="" goto :fallback
17+
18+
set "BASE1=%VCPKG_ROOT%\\installed\\%TRIPLET%\\tools\\protobuf"
19+
for %%F in ("%BASE1%\\protoc.exe" "%BASE1%\\protoc-*.exe") do (
20+
if exist "%%~fF" (
21+
"%%~fF" %*
22+
exit /b %errorlevel%
23+
)
24+
)
25+
26+
set "BASE2=%VCPKG_ROOT%\\packages\\protobuf_%TRIPLET%\\tools\\protobuf"
27+
for %%F in ("%BASE2%\\protoc.exe" "%BASE2%\\protoc-*.exe") do (
28+
if exist "%%~fF" (
29+
"%%~fF" %*
30+
exit /b %errorlevel%
31+
)
32+
)
33+
34+
:fallback
35+
where protoc >nul 2>nul
36+
if %errorlevel%==0 (
37+
protoc %*
38+
exit /b %errorlevel%
39+
)
40+
41+
echo protoc_wrapper: protoc not found 1>&2
42+
exit /b 127

scripts/protoc_wrapper.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env sh
2+
set -eu
3+
4+
if [ -n "${PROTOC_REAL:-}" ] && [ -x "${PROTOC_REAL}" ]; then
5+
exec "${PROTOC_REAL}" "$@"
6+
fi
7+
8+
vcpkg_root="${VCPKG_ROOT:-}"
9+
host_triplet="${VCPKG_HOST_TRIPLET:-${VCPKG_TARGET_TRIPLET:-}}"
10+
11+
if [ -n "${vcpkg_root}" ] && [ -n "${host_triplet}" ]; then
12+
for candidate in \
13+
"${vcpkg_root}/installed/${host_triplet}/tools/protobuf/protoc" \
14+
"${vcpkg_root}/installed/${host_triplet}/tools/protobuf/protoc-"* \
15+
"${vcpkg_root}/packages/protobuf_${host_triplet}/tools/protobuf/protoc" \
16+
"${vcpkg_root}/packages/protobuf_${host_triplet}/tools/protobuf/protoc-"*; do
17+
if [ -x "${candidate}" ]; then
18+
exec "${candidate}" "$@"
19+
fi
20+
done
21+
fi
22+
23+
if command -v protoc >/dev/null 2>&1; then
24+
exec protoc "$@"
25+
fi
26+
27+
echo "protoc_wrapper: protoc not found (VCPKG_ROOT=${vcpkg_root} VCPKG_HOST_TRIPLET=${host_triplet})" >&2
28+
exit 127

0 commit comments

Comments
 (0)