firebase_utils.cmake 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. # Join all the input arguments together using the <glue> string and store the
  15. # result in the named <output_variable>.
  16. #
  17. # TODO: Delete this function once cmake_minimum_required() is 3.12 or greater,
  18. # in which case the built-in list(JOIN ...) command should be used instead.
  19. # See https://cmake.org/cmake/help/v3.12/command/string.html#join
  20. function(firebase_string_join glue output_variable)
  21. list(LENGTH ARGN ARGN_LENGTH)
  22. if(ARGN_LENGTH EQUAL 0)
  23. set("${output_variable}" "" PARENT_SCOPE)
  24. return()
  25. endif()
  26. list(GET ARGN 0 result_string)
  27. list(REMOVE_AT ARGN 0)
  28. foreach(argv_element ${ARGN})
  29. string(APPEND result_string "${glue}")
  30. string(APPEND result_string "${argv_element}")
  31. endforeach()
  32. set("${output_variable}" "${result_string}" PARENT_SCOPE)
  33. endfunction(firebase_string_join)
  34. # A wrapper around the built-in execute_process() function that adds some
  35. # additional functionality.
  36. #
  37. # In addition to calling the built-in execute_process() function, this function
  38. # also does the following:
  39. # 1. Logs the arguments of the process being executed.
  40. # 2. Fails if the process completes with a non-zero exit code.
  41. function(firebase_execute_process)
  42. cmake_parse_arguments(
  43. "ARG" # prefix
  44. "" # options
  45. "WORKING_DIRECTORY" # one_value_keywords
  46. "COMMAND" # multi_value_keywords
  47. ${ARGN}
  48. )
  49. list(LENGTH ARG_COMMAND ARG_COMMAND_LENGTH)
  50. if(ARG_COMMAND_LENGTH EQUAL 0)
  51. message(
  52. FATAL_ERROR
  53. "firebase_execute_process() COMMAND must be given at least one value."
  54. )
  55. endif()
  56. set(execute_process_args "")
  57. list(APPEND execute_process_args "COMMAND" ${ARG_COMMAND})
  58. if("${ARG_WORKING_DIRECTORY}" STREQUAL "")
  59. set(LOG_SUFFIX "")
  60. else()
  61. set(LOG_SUFFIX " (working directory: ${ARG_WORKING_DIRECTORY})")
  62. list(APPEND execute_process_args "WORKING_DIRECTORY" "${ARG_WORKING_DIRECTORY}")
  63. endif()
  64. firebase_string_join(" " ARG_COMMAND_STR ${ARG_COMMAND})
  65. message(
  66. STATUS
  67. "firebase_execute_process(): "
  68. "running command: ${ARG_COMMAND_STR}${LOG_SUFFIX}"
  69. )
  70. execute_process(
  71. ${execute_process_args}
  72. RESULT_VARIABLE process_exit_code
  73. )
  74. if(NOT process_exit_code EQUAL 0)
  75. message(
  76. FATAL_ERROR
  77. "firebase_execute_process(): command failed with non-zero exit code "
  78. "${process_exit_code}: ${ARG_COMMAND_STR}${LOG_SUFFIX}"
  79. )
  80. endif()
  81. endfunction(firebase_execute_process)