|
| 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) |
0 commit comments