Created
November 4, 2023 09:54
-
-
Save mcejp/06d225ae1620bdf0148eee6ec9db8e3b to your computer and use it in GitHub Desktop.
Custom Python step in CMake build
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro(cmake_path_ABSOLUTE_PATH VARIABLE_NAME) | |
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.20.0") | |
cmake_path(ABSOLUTE_PATH ${VARIABLE_NAME}) | |
else() | |
# polyfill for CMake < 3.20 | |
get_filename_component(${VARIABLE_NAME} ${${VARIABLE_NAME}} ABSOLUTE) | |
endif() | |
endmacro() | |
function(my_build_step SOURCE OUTPUT) | |
cmake_path_ABSOLUTE_PATH(SOURCE) | |
find_package(Python3 REQUIRED COMPONENTS Interpreter) | |
# Not so fast! This breaks if multiple targets reference $OUTPUT: | |
# https://cmake.org/cmake/help/latest/command/add_custom_command.html#example-generating-files-for-multiple-targets | |
add_custom_command( | |
OUTPUT | |
"${OUTPUT}" | |
COMMAND | |
"${Python3_EXECUTABLE}" | |
"${CMAKE_CURRENT_FUNCTION_LIST_DIR}/my-build-step.py" | |
"${SOURCE}" | |
-o "${OUTPUT}" | |
DEPENDS | |
"${SOURCE}" | |
"${CMAKE_CURRENT_FUNCTION_LIST_DIR}/my-build-step.py" | |
) | |
endfunction() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment