Skip to content

Instantly share code, notes, and snippets.

@Steve132
Created March 10, 2021 04:54
Show Gist options
  • Save Steve132/f21655ea5277fb8f6fbfaf2deb60e35e to your computer and use it in GitHub Desktop.
Save Steve132/f21655ea5277fb8f6fbfaf2deb60e35e to your computer and use it in GitHub Desktop.
find_package(Python COMPONENTS Interpreter) #make sure you have python and populate Python::Interpreter from the system.
add_executable(sometool sometool.cpp) #the target for the tool that generates the code.
#I assume that somescript.py accepts two arguments: the path to sometool.exe and the path to the output
set(MY_SCRIPT_OUTPUT "${CMAKE_CURRENT_BUILD_DIR}/generated.cpp")
add_custom_command(OUTPUT "${CMAKE_CURRENT_BUILD_DIR}/generated.cpp" #...additional outputs here if necessary
COMMAND Python::Interpreter
ARGS "${CMAKE_CURRENT_SOURCE_DIR}/somescript.py" "$<TARGET_FILE:sometool>" "${CMAKE_CURRENT_BUILD_DIR}/generated.cpp"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/somescript.py" #rerun if somescript.py changes.
)
#if you want to do it all at once, this is valid. The dependency gets added automatically
add_library(somecode
"${CMAKE_CURRENT_BUILD_DIR}/generated.cpp"
othercode.cpp
othercode.hpp
)
#OR do this:
add_library(somecode
othercode.cpp
othercode.hpp
)
target_sources(somecode
"${CMAKE_CURRENT_BUILD_DIR}/generated.cpp"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment