|
| 1 | + |
| 2 | +# |
| 3 | +# CMake Generator Expressions Utility by Parra Studios |
| 4 | +# Expands generator expressions during configuration phase. |
| 5 | +# |
| 6 | +# Copyright (C) 2016 - 2020 Vicente Eduardo Ferrer Garcia <[email protected]> |
| 7 | +# |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +# you may not use this file except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +# See the License for the specific language governing permissions and |
| 18 | +# limitations under the License. |
| 19 | +# |
| 20 | + |
| 21 | +# Usage: |
| 22 | +# cmake_expand_generator_expressions(EXPANSION |
| 23 | +# $<$<BOOL:${OPTION_BUILD_LOADERS_RB}>:rb_loader> |
| 24 | +# $<$<BOOL:${OPTION_BUILD_LOADERS_PY}>:py_loader> |
| 25 | +# ) |
| 26 | +# |
| 27 | +# Result will be stored in EXPANSION variable: |
| 28 | +# message(STATUS "${EXPANSION}") |
| 29 | +# |
| 30 | +# Note: |
| 31 | +# This macro is very slow when run on the first time, |
| 32 | +# but at least is something meanwhile this PR is not implemented: |
| 33 | +# https://gitlab.kitware.com/cmake/cmake/-/issues/19467 |
| 34 | + |
| 35 | +macro(cmake_expand_generator_expressions output) |
| 36 | + set(CMAKE_GENERATOR_EXPRESSION_LIST "${ARGN}") |
| 37 | + set(GENERATOR_EXPRESSION_TEMP_PATH "${CMAKE_CURRENT_BINARY_DIR}/cmake_expand_generator_expressions") |
| 38 | + string(REGEX REPLACE ";" " " CMAKE_GENERATOR_EXPRESSION_LIST "${CMAKE_GENERATOR_EXPRESSION_LIST}") |
| 39 | + string(REGEX REPLACE "\n" " " CMAKE_GENERATOR_EXPRESSION_LIST "${CMAKE_GENERATOR_EXPRESSION_LIST}") |
| 40 | + set(contents |
| 41 | + "file(GENERATE" |
| 42 | + " OUTPUT \"${GENERATOR_EXPRESSION_TEMP_PATH}/output.tmp\"" |
| 43 | + " CONTENT \"${CMAKE_GENERATOR_EXPRESSION_LIST}\"" |
| 44 | + ")" |
| 45 | + ) |
| 46 | + file(WRITE |
| 47 | + "${GENERATOR_EXPRESSION_TEMP_PATH}/CMakeLists.txt" |
| 48 | + ${contents} |
| 49 | + ) |
| 50 | + execute_process(COMMAND ${CMAKE_COMMAND} -Wno-dev ${GENERATOR_EXPRESSION_TEMP_PATH} |
| 51 | + WORKING_DIRECTORY ${GENERATOR_EXPRESSION_TEMP_PATH} |
| 52 | + OUTPUT_VARIABLE discard |
| 53 | + ) |
| 54 | + file(READ |
| 55 | + "${GENERATOR_EXPRESSION_TEMP_PATH}/output.tmp" |
| 56 | + GENERATED_OUTPUT |
| 57 | + ) |
| 58 | + # This is to avoid possible side effects, but I decided to remove it for caching purposes |
| 59 | + # file(REMOVE_RECURSE "${GENERATOR_EXPRESSION_TEMP_PATH}") |
| 60 | + string(REGEX REPLACE " " ";" GENERATED_OUTPUT "${GENERATED_OUTPUT}") |
| 61 | + set(${output} "${GENERATED_OUTPUT}") |
| 62 | +endmacro() |
0 commit comments