find_program(_MAKEHEADERS makeheaders) if(_MAKEHEADERS) add_executable(makeheaders IMPORTED) set_target_properties(makeheaders PROPERTIES IMPORTED_LOCATION ${_MAKEHEADERS}) else() add_executable(makeheaders ${CMAKE_CURRENT_LIST_DIR}/makeheaders.c) endif() unset(_MAKEHEADERS) function(target_makeheaders target) get_target_property(binary_dir ${target} BINARY_DIR) get_target_property(source_dir ${target} SOURCE_DIR) get_target_property(sources ${target} SOURCES) foreach(source ${sources}) cmake_path(REPLACE_EXTENSION source h OUTPUT_VARIABLE header) cmake_path(ABSOLUTE_PATH source BASE_DIRECTORY ${source_dir} NORMALIZE) list(APPEND makeheader_args ${source}:${header}) cmake_path(ABSOLUTE_PATH header BASE_DIRECTORY ${binary_dir} NORMALIZE) list(APPEND generated_headers ${header}) endforeach() add_custom_command( OUTPUT ${generated_headers} COMMAND makeheaders -local -- ${makeheader_args} DEPENDS ${sources} COMMENT "Generating headers for target ${target}" VERBATIM) target_sources(${target} PRIVATE ${generated_headers}) target_include_directories(${target} PRIVATE ${binary_dir}) endfunction()