tde_automoc: Add support for processing automoc for generated files.

Signed-off-by: Slávek Banko <slavek.banko@axis.cz>
(cherry picked from commit efec901634)
r14.0.x
Slávek Banko 3 years ago
parent 321dec8aae
commit 2878ceef0c
No known key found for this signature in database
GPG Key ID: 608F5293A04BE668

@ -498,13 +498,15 @@ macro( tde_automoc )
endforeach( )
if( NOT _found )
get_filename_component( _moc_file "${_moc_file}" NAME )
tde_message_fatal( "AUTOMOC error: '${_moc_file}' cannot be generated.\n Reason: '${_src_file}.h' not found." )
tde_message_fatal( "AUTOMOC error: '${_moc_file}' cannot be generated.\n Reason: '${_src_header}.h' not found." )
endif( )
endif( )
# moc-ing header
file( RELATIVE_PATH _moc_file_relative "${CMAKE_BINARY_DIR}" "${_moc_file}" )
add_custom_command( OUTPUT ${_moc_file}
COMMAND ${TMOC_EXECUTABLE} ${_header_file} -o ${_moc_file}
COMMENT "Generating ${_moc_file_relative}"
DEPENDS ${_header_file} )
# create dependency between source file and moc file
@ -514,6 +516,44 @@ macro( tde_automoc )
endif( _moc_includes )
else( EXISTS "${_src_file}" )
# for generated file, the path must match the binary directory
string( LENGTH "${CMAKE_BINARY_DIR}" CMAKE_BINARY_DIR_LEN )
string( LENGTH "${_src_file}" _basename_len )
if( ${_basename_len} LESS ${CMAKE_BINARY_DIR_LEN} )
set( _basedir_prefix "${CMAKE_SOURCE_DIR}" )
else( )
string( SUBSTRING "${_src_file}" 0 ${CMAKE_BINARY_DIR_LEN} _basedir_prefix )
endif( )
if( ${_basedir_prefix} STREQUAL "${CMAKE_BINARY_DIR}" )
file( RELATIVE_PATH _sourcename "${CMAKE_BINARY_DIR}" "${_src_file}" )
file( RELATIVE_PATH _basename "${CMAKE_CURRENT_BINARY_DIR}" "${_src_file}" )
else( )
file( RELATIVE_PATH _sourcename "${CMAKE_SOURCE_DIR}" "${_src_file}" )
file( RELATIVE_PATH _basename "${CMAKE_CURRENT_SOURCE_DIR}" "${_src_file}" )
endif( )
# automocing generated file
get_filename_component( _automoc_file "${_src_file}+automoc" NAME )
set( _automoc_file "${CMAKE_CURRENT_BINARY_DIR}/${_automoc_file}" )
add_custom_command( OUTPUT ${_automoc_file}
COMMAND ${CMAKE_COMMAND}
-DTMOC_EXECUTABLE:FILEPATH=${TMOC_EXECUTABLE}
-DSRC_FILE:FILEPATH=${CMAKE_CURRENT_BINARY_DIR}/${_basename}
-DMETA_INCLUDES:STRING="${_meta_includes}"
-DMASTER_SOURCE_DIR:FILEPATH=${CMAKE_SOURCE_DIR}
-DMASTER_BINARY_DIR:FILEPATH=${CMAKE_BINARY_DIR}
-P ${TDE_CMAKE_MODULES}/tde_automoc.cmake
COMMENT "Automocing ${_sourcename}"
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${_basename}"
)
# create dependency between source file and moc file
set_property( SOURCE "${CMAKE_CURRENT_BINARY_DIR}/${_basename}"
APPEND PROPERTY OBJECT_DEPENDS ${_automoc_file} )
endif( EXISTS "${_src_file}" )
endforeach( _src_file )

@ -0,0 +1,76 @@
#################################################
#
# (C) 2022 Slávek Banko
# slavek (DOT) banko (AT) axis.cz
#
# Improvements and feedback are welcome
#
# This file is released under GPL >= 2
#
#################################################
if( NOT ${CMAKE_CURRENT_LIST_DIR} STREQUAL ${CMAKE_ROOT}/Modules )
set( CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}" )
endif()
include( TDEMacros )
get_filename_component( _src_file "${SRC_FILE}" ABSOLUTE )
set( _meta_includes ${META_INCLUES} )
unset( _moc_headers )
if( EXISTS "${_src_file}" )
# read source file and check if have moc include
file( READ "${_src_file}" _src_content )
string( REGEX MATCHALL "#include +[^ ]+\\.moc[\">]" _moc_includes "${_src_content}" )
# found included moc(s)?
if( _moc_includes )
foreach( _moc_file ${_moc_includes} )
# extracting moc filename
string( REGEX MATCH "[^ <\"]+\\.moc" _moc_file "${_moc_file}" )
set( _moc_file "${CMAKE_CURRENT_BINARY_DIR}/${_moc_file}" )
# create header filename
get_filename_component( _src_path "${_src_file}" ABSOLUTE )
get_filename_component( _src_path "${_src_path}" PATH )
get_filename_component( _src_header "${_moc_file}" NAME_WE )
set( _header_file "${_src_path}/${_src_header}.h" )
# if header doesn't exists, check in META_INCLUDES
if( NOT EXISTS "${_header_file}" )
unset( _found )
foreach( _src_path ${_meta_includes} )
set( _header_file "${_src_path}/${_src_header}.h" )
if( EXISTS "${_header_file}" )
set( _found 1 )
break( )
endif( )
endforeach( )
if( NOT _found )
get_filename_component( _moc_file "${_moc_file}" NAME )
tde_message_fatal( "AUTOMOC error: '${_moc_file}' cannot be generated.\n Reason: '${_src_header}.h' not found." )
endif( )
endif( )
# moc-ing header
execute_process( COMMAND ${TMOC_EXECUTABLE} ${_header_file} -o ${_moc_file} )
list( APPEND _moc_headers "${_src_header}.h" )
endforeach( _moc_file )
endif( _moc_includes )
else()
tde_message_fatal( "AUTOMOC error: '${_src_file}' not found!" )
endif( EXISTS "${_src_file}" )
get_filename_component( _automoc_file "${_src_file}+automoc" NAME )
if( DEFINED _moc_headers )
string( REPLACE ";" "\n * " _moc_headers "${_moc_headers}" )
file( WRITE "${_automoc_file}" "/*\n * processed:\n * ${_moc_headers}\n */" )
else()
file( WRITE "${_automoc_file}" "/* processed - no moc files */" )
endif()
Loading…
Cancel
Save