diff options
Diffstat (limited to 'TQtMacros.cmake')
-rw-r--r-- | TQtMacros.cmake | 307 |
1 files changed, 307 insertions, 0 deletions
diff --git a/TQtMacros.cmake b/TQtMacros.cmake new file mode 100644 index 00000000..3aa50e9f --- /dev/null +++ b/TQtMacros.cmake @@ -0,0 +1,307 @@ +################################################# +# # +# Auxiliary macros for TQt # +# # +################################################# + +include( TDEMacros ) + + +################################################# +##### +##### tqt_install_includes +##### +##### The macro is used to determine the headers that are installed, +##### while the symlinks in the binary include directory are created. +##### +##### Syntax: +##### tqt_install_includes( +##### [FILES] include_name [include_name] +##### [DESTINATION subdir] +##### [ONLY_SYMLINK] +##### ) + +macro( tqt_install_includes ) + + unset( _files ) + unset( _dest ) + unset( _only_symlink ) + set( _var _files ) + + foreach( _arg ${ARGN} ) + + # found directive "FILES" + if( "+${_arg}" STREQUAL "+FILES" ) + unset( _files ) + set( _var _files ) + set( _directive 1 ) + endif( ) + + # found directive "DESTINATION" + if( "+${_arg}" STREQUAL "+DESTINATION" ) + unset( _dest ) + set( _var _dest ) + set( _directive 1 ) + endif( ) + + # found directive "ONLY_SYMLINK" + if( "+${_arg}" STREQUAL "+ONLY_SYMLINK" ) + unset( _var ) + set( _only_symlink 1 ) + set( _directive 1 ) + endif( ) + + # collect data + if( _directive ) + unset( _directive ) + elseif( _var ) + list( APPEND ${_var} ${_arg} ) + endif( ) + + endforeach( ) + + # determine destination directory + if( NOT IS_ABSOLUTE( "${_dest}" ) ) + set( _dest "${QT_INSTALL_HEADERS}/${_dest}" ) + endif() + file( RELATIVE_PATH _dest_sub "${QT_INSTALL_HEADERS}" "${_dest}" ) + file( MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/include/${_dest_sub}" ) + + # process files + foreach( _file IN LISTS _files ) + if( NOT TARGET tqt-includes ) + add_custom_target( tqt-includes + WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" + COMMENT "Prepare includes..." ) + endif() + + get_filename_component( _source_name "${_file}" NAME ) + get_filename_component( _source_file "${_file}" ABSOLUTE ) + file( RELATIVE_PATH _target_name "${CMAKE_SOURCE_DIR}" "${_source_file}" ) + string( REPLACE "/" "+" _target_name "${_target_name}" ) + + file( RELATIVE_PATH _link_source "${CMAKE_BINARY_DIR}/include/${_dest_sub}" ${_source_file} ) + file( RELATIVE_PATH _link_dest "${QT_INSTALL_HEADERS}" "${_dest}/${_source_name}" ) + + if( NOT TARGET ${_target_name} ) + add_custom_command( + OUTPUT ${CMAKE_BINARY_DIR}/include/${_link_dest} + COMMAND + ${CMAKE_COMMAND} -E create_symlink + ${_link_source} ${CMAKE_BINARY_DIR}/include/${_link_dest} + COMMENT "Include file ${_link_dest}" + ) + add_custom_target( ${_target_name} + DEPENDS ${CMAKE_BINARY_DIR}/include/${_link_dest} + ) + add_dependencies( tqt-includes ${_target_name} ) + endif() + + if( NOT "${_only_symlink}" ) + install( FILES ${_file} DESTINATION ${_dest} ) + endif() + endforeach( _file ) + +endmacro( tqt_install_includes ) + + +################################################# +##### +##### tqt_automoc +##### +##### The macro is used for 'moc' processing specifically for TQt as such +##### and adding them to an existing target. +##### +##### Syntax: +##### tqt_moc( +##### [TARGET] target +##### [INCLUDES include_name [include_name]] +##### ) + +macro( tqt_automoc ) + + unset( _target ) + set( _includes AUTO ) + set( _var _target ) + + foreach( _arg ${ARGN} ) + + # found directive "TARGET" + if( "+${_arg}" STREQUAL "+TARGET" ) + unset( _target ) + set( _var _target ) + set( _directive 1 ) + endif( ) + + # found directive "INCLUDES" + if( "+${_arg}" STREQUAL "+INCLUDES" ) + unset( _includes ) + set( _var _includes ) + set( _directive 1 ) + endif( ) + + # collect data + if( _directive ) + unset( _directive ) + elseif( _var ) + list( APPEND ${_var} ${_arg} ) + endif( ) + + endforeach( ) + + # target must already exist + if( NOT TARGET ${_target} ) + tde_message_fatal( "The specified target does not exists." ) + endif() + + # search include files suitable for processing + if( _includes STREQUAL "AUTO" ) + file( GLOB _includes RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/*.h ) + foreach( _include IN LISTS _includes ) + file( READ "${CMAKE_CURRENT_SOURCE_DIR}/${_include}" _src_content ) + string( REGEX REPLACE "(^|\n)[\t ]*#[^\n]*" "" _src_content "${_src_content}" ) + string( REGEX REPLACE "(//|/\\*)[^\n]*T?Q_OBJECT[^\n]*" "" _src_content "${_src_content}" ) + if( NOT _src_content MATCHES "[\n\t ]+T?Q_OBJECT[\n\t ]+" ) + list( REMOVE_ITEM _includes "${_include}" ) + endif() + endforeach() + elseif( _includes STREQUAL "-" ) + set( _includes "" ) + endif() + + # check tmoc executable + if( NOT DEFINED TMOC_EXECUTABLE ) + find_program( TMOC_EXECUTABLE + NAMES tmoc + HINT ${QT_INSTALL_BINS} + ) + if( "${TMOC_EXECUTABLE}" STREQUAL "TMOC_EXECUTABLE-NOTFOUND" ) + tde_message_fatal( "tmoc is required but not found" ) + endif( ) + endif() + + # processing sources of specified target + get_property( _sources TARGET ${_target} PROPERTY SOURCES ) + foreach( _src_file IN LISTS _sources ) + + get_filename_component( _src_file "${_src_file}" ABSOLUTE ) + + 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 source 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 ) + if( NOT TQT_FOUND ) + set( _moc_source "${_src_path}/${_src_header}.cpp" ) + else() + set( _moc_source "${_src_path}/${_src_header}.h" ) + endif() + + # if header doesn't exists, check in META_INCLUDES + if( NOT EXISTS "${_moc_source}" ) + unset( _found ) + foreach( _src_path ${_meta_includes} ) + set( _moc_source "${_src_path}/${_src_header}.h" ) + if( EXISTS "${_moc_source}" ) + 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_file}.h' not found." ) + endif( ) + endif( ) + + # moc-ing source + add_custom_command( OUTPUT ${_moc_file} + COMMAND ${TMOC_EXECUTABLE} ${_moc_source} -o ${_moc_file} + DEPENDS ${_moc_source} + ) + + # create dependency between source file and moc file + set_property( SOURCE ${_src_file} APPEND PROPERTY OBJECT_DEPENDS ${_moc_file} ) + + # remove from includes for processing + file( RELATIVE_PATH _moc_source ${CMAKE_CURRENT_SOURCE_DIR} ${_moc_source} ) + list( REMOVE_ITEM _includes "${_moc_source}" ) + + endforeach( _moc_file ) + + endif( _moc_includes ) + + endif( EXISTS "${_src_file}" ) + + endforeach( _src_file ) + + # processing headers + foreach( _include_file IN LISTS _includes ) + get_filename_component( _include_name "${_include_file}" NAME_WE ) + set( _moc_file ${CMAKE_CURRENT_BINARY_DIR}/moc_${_include_name}.cpp ) + + # moc-ing source + add_custom_command( OUTPUT ${_moc_file} + COMMAND ${TMOC_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/${_include_file} -o ${_moc_file} + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_include_file} + ) + set_property( TARGET ${_target} APPEND PROPERTY SOURCES ${_moc_file} ) + endforeach( _include_file ) + +endmacro( tqt_automoc ) + + +################################################# +##### +##### tqt_create_translation +##### +##### The macro is used for create binary files for translations +##### +##### Syntax: +##### tqt_create_translation( +##### ) + +macro( tqt_create_translation ) + + file( GLOB _srcs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*_*.ts ) + list( SORT _srcs ) + + # generate *.qm files + foreach( _src ${_srcs} ) + + get_filename_component( _src ${_src} ABSOLUTE ) + + get_filename_component( _out ${_src} NAME_WE ) + string( REPLACE "@" "_" _target ${_out} ) + set( _out_filename "${_out}.qm" ) + set( _install_filename "${_out}.qm" ) + + add_custom_command( + OUTPUT ${_out_filename} + COMMAND tqlrelease ${_src} -qm ${_out_filename} + COMMENT "Build translation ${_out}" + DEPENDS ${_src} + ) + add_custom_target( "${_target}-translation" ALL DEPENDS ${_out_filename} ) + install( + FILES ${CMAKE_CURRENT_BINARY_DIR}/${_out_filename} + RENAME ${_install_filename} + DESTINATION ${QT_INSTALL_TRANSLATIONS} + ) + + endforeach( ) + +endmacro( tqt_create_translation ) |