summaryrefslogtreecommitdiffstats
path: root/TQtMacros.cmake
blob: 3aa50e9f49ff9b71ecc72191d610fcc0adaaf7ed (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
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 )