summaryrefslogtreecommitdiffstats
path: root/modules/tde_automoc.cmake
blob: 0c91ca6bc18ab7d2c010ffa51663d98a8b7353e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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 ${TQMOC_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()