Created
March 24, 2018 23:57
-
-
Save AustinBrunkhorst/38a3ca236238604ee32ef58d2e9c6e90 to your computer and use it in GitHub Desktop.
C++ Reflection | CMake Prebuild Example
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
set(PROJECT_NAME "Example") | |
# assume this contains header files for this project | |
set(PROJECT_HEADER_FILES ...) | |
# assume this contains source files for this project | |
set(PROJECT_SOURCE_FILES ...) | |
# generated file names, in the build directory | |
set(META_GENERATED_HEADER "${CMAKE_CURRENT_BINARY_DIR}/Meta.Generated.h") | |
set(META_GENERATED_SOURCE "${CMAKE_CURRENT_BINARY_DIR}/Meta.Generated.cpp") | |
# create the project target | |
add_executable(${PROJECT_NAME} | |
${PROJECT_HEADER_FILES} | |
${PROJECT_SOURCE_FILES} | |
# make sure the generated header and source are included | |
${META_GENERATED_HEADER} | |
${META_GENERATED_SOURCE} | |
) | |
# path to the reflection parser executable | |
set(PARSE_TOOL_EXE "ReflectionParser.exe") | |
# input source file to pass to the reflection parser compiler | |
# in this file, include any files that you want exposed to the parser | |
set(PROJECT_META_HEADER "Reflection.h") | |
# fetch all include directories for the project target | |
get_property(DIRECTORIES TARGET ${PROJECT_NAME} PROPERTY INCLUDE_DIRECTORIES) | |
# flags that will eventually be based to the reflection parser compiler | |
set(META_FLAGS "") | |
# build the include directory flags | |
foreach (DIRECTORY ${DIRECTORIES}) | |
set(META_FLAGS ${meta_flags} "\\\\-I${DIRECTORY}") | |
endforeach () | |
# include the system directories | |
if (MSVC) | |
# assume ${VS_VERSION} is the version of Visual Studio being used to compile | |
set(META_FLAGS ${META_FLAGS} | |
"\\\\-IC:/Program Files (x86)/Microsoft Visual Studio ${VS_VERSION}/VC/include" | |
) | |
else () | |
# you can figure it out for other compilers :) | |
message(FATAL_ERROR "System include directories not implemented for this compiler.") | |
endif () | |
# add the command that invokes the reflection parser executable | |
# whenever a header file in your project has changed | |
add_custom_command( | |
OUTPUT ${META_GENERATED_HEADER} ${META_GENERATED_SOURCE} | |
DEPENDS ${PROJECT_HEADER_FILES} | |
COMMAND call "${PARSE_TOOL_EXE}" | |
--target-name "${PROJECT_NAME}" | |
--in-source "${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_SOURCE_DIR}/${PROJECT_META_HEADER}" | |
--out-header "${META_GENERATED_HEADER}" | |
--out-source "${META_GENERATED_SOURCE}" | |
--flags ${META_FLAGS} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment