Skip to content

Instantly share code, notes, and snippets.

@mcejp
Created November 4, 2023 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcejp/06d225ae1620bdf0148eee6ec9db8e3b to your computer and use it in GitHub Desktop.
Save mcejp/06d225ae1620bdf0148eee6ec9db8e3b to your computer and use it in GitHub Desktop.
Custom Python step in CMake build
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