Skip to content

Commit deda107

Browse files
authored
cmake: fail fast when execute_process() fails (#10566)
1 parent 67ae955 commit deda107

File tree

3 files changed

+102
-19
lines changed

3 files changed

+102
-19
lines changed

cmake/external_rules.cmake

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
include("${CMAKE_CURRENT_LIST_DIR}/firebase_utils.cmake")
16+
1517
function(download_external_sources)
1618
file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/external)
1719

@@ -37,7 +39,7 @@ function(download_external_sources)
3739
)
3840
endif()
3941

40-
execute_process(
42+
firebase_execute_process(
4143
COMMAND
4244
${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}"
4345
-DFIREBASE_DOWNLOAD_DIR=${FIREBASE_DOWNLOAD_DIR}
@@ -56,7 +58,7 @@ function(download_external_sources)
5658
set(cmake_build_args -j)
5759
endif()
5860

59-
execute_process(
61+
firebase_execute_process(
6062
COMMAND ${CMAKE_COMMAND} --build . -- ${cmake_build_args}
6163
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/external
6264
)

cmake/firebase_utils.cmake

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Join all the input arguments together using the <glue> string and store the
16+
# result in the named <output_variable>.
17+
#
18+
# TODO: Delete this function once cmake_minimum_required() is 3.12 or greater,
19+
# in which case the built-in list(JOIN ...) command should be used instead.
20+
# See https://cmake.org/cmake/help/v3.12/command/string.html#join
21+
function(firebase_string_join glue output_variable)
22+
list(LENGTH ARGN ARGN_LENGTH)
23+
24+
if(ARGN_LENGTH EQUAL 0)
25+
set("${output_variable}" "" PARENT_SCOPE)
26+
return()
27+
endif()
28+
29+
list(GET ARGN 0 result_string)
30+
list(REMOVE_AT ARGN 0)
31+
32+
foreach(argv_element ${ARGN})
33+
string(APPEND result_string "${glue}")
34+
string(APPEND result_string "${argv_element}")
35+
endforeach()
36+
37+
set("${output_variable}" "${result_string}" PARENT_SCOPE)
38+
endfunction(firebase_string_join)
39+
40+
# A wrapper around the built-in execute_process() function that adds some
41+
# additional functionality.
42+
#
43+
# In addition to calling the built-in execute_process() function, this function
44+
# also does the following:
45+
# 1. Logs the arguments of the process being executed.
46+
# 2. Fails if the process completes with a non-zero exit code.
47+
function(firebase_execute_process)
48+
cmake_parse_arguments(
49+
"ARG" # prefix
50+
"" # options
51+
"WORKING_DIRECTORY" # one_value_keywords
52+
"COMMAND" # multi_value_keywords
53+
${ARGN}
54+
)
55+
56+
list(LENGTH ARG_COMMAND ARG_COMMAND_LENGTH)
57+
if(ARG_COMMAND_LENGTH EQUAL 0)
58+
message(
59+
FATAL_ERROR
60+
"firebase_execute_process() COMMAND must be given at least one value."
61+
)
62+
endif()
63+
64+
set(execute_process_args "")
65+
list(APPEND execute_process_args "COMMAND" ${ARG_COMMAND})
66+
67+
if("${ARG_WORKING_DIRECTORY}" STREQUAL "")
68+
set(LOG_SUFFIX "")
69+
else()
70+
set(LOG_SUFFIX " (working directory: ${ARG_WORKING_DIRECTORY})")
71+
list(APPEND execute_process_args "WORKING_DIRECTORY" "${ARG_WORKING_DIRECTORY}")
72+
endif()
73+
74+
firebase_string_join(" " ARG_COMMAND_STR ${ARG_COMMAND})
75+
message(
76+
STATUS
77+
"firebase_execute_process(): "
78+
"running command: ${ARG_COMMAND_STR}${LOG_SUFFIX}"
79+
)
80+
81+
execute_process(
82+
${execute_process_args}
83+
RESULT_VARIABLE process_exit_code
84+
)
85+
86+
if(NOT process_exit_code EQUAL 0)
87+
message(
88+
FATAL_ERROR
89+
"firebase_execute_process(): command failed with non-zero exit code "
90+
"${process_exit_code}: ${ARG_COMMAND_STR}${LOG_SUFFIX}"
91+
)
92+
endif()
93+
94+
endfunction(firebase_execute_process)

cmake/python_setup.cmake

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
include("${CMAKE_CURRENT_LIST_DIR}/firebase_utils.cmake")
16+
1517
# Sets up an isolated Python interpreter, installing required dependencies.
1618
#
1719
# This function does the following:
@@ -120,20 +122,13 @@ function(FirebaseSetupPythonInterpreter)
120122
"using ${FIREBASE_PYTHON_HOST_EXECUTABLE}"
121123
)
122124
file(REMOVE_RECURSE "${PYVENV_DIRECTORY}")
123-
execute_process(
125+
firebase_execute_process(
124126
COMMAND
125127
"${FIREBASE_PYTHON_HOST_EXECUTABLE}"
126128
-m
127129
venv
128130
"${PYVENV_DIRECTORY}"
129-
RESULT_VARIABLE
130-
FIREBASE_PYVENV_CREATE_RESULT
131131
)
132-
if(NOT FIREBASE_PYVENV_CREATE_RESULT EQUAL 0)
133-
message(FATAL_ERROR
134-
"Failed to create a Python virtualenv in ${PYVENV_DIRECTORY} "
135-
"using ${FIREBASE_PYTHON_HOST_EXECUTABLE}")
136-
endif()
137132

138133
# Find the Python interpreter in the virtualenv.
139134
find_program(
@@ -157,22 +152,14 @@ function(FirebaseSetupPythonInterpreter)
157152
"${LOG_PREFIX}: Installing Python dependencies into "
158153
"${PYVENV_DIRECTORY}: ${ARG_REQUIREMENTS}"
159154
)
160-
execute_process(
155+
firebase_execute_process(
161156
COMMAND
162157
"${PYTHON_EXECUTABLE}"
163158
-m
164159
pip
165160
install
166161
${ARG_REQUIREMENTS}
167-
RESULT_VARIABLE
168-
PIP_INSTALL_RESULT
169162
)
170-
if(NOT PIP_INSTALL_RESULT EQUAL 0)
171-
message(FATAL_ERROR
172-
"Failed to install Python dependencies into "
173-
"${PYVENV_DIRECTORY}: ${ARG_REQUIREMENTS}"
174-
)
175-
endif()
176163
endif()
177164

178165
# Write the stamp files.

0 commit comments

Comments
 (0)