summaryrefslogtreecommitdiffstats
path: root/TQtMacros.cmake
blob: d28ea8c82e00ebb866153250e30650d6435da4cf (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#################################################
#                                               #
#  Auxiliary macros for TQt                     #
#                                               #
#################################################

include( TDEMacros )

#################################################
#####
##### tqt_expert_config
#####
##### If WITH_UNSUPORTED_CONFIG is not supplied prints messages and exits
#####
##### Syntax:
#####   tqt_requires( opt [msg ...] )

macro( tqt_expert_config _opt )
  if( NOT WITH_UNSUPORTED_CONFIG )
    tde_message_fatal(
      "${_opt} is not well-supported."
      ${ARGN}
      "Use -DWITH_UNSUPORTED_CONFIG=ON if you know what you are doing."
    )
   else( )
    message( STATUS "Using expert configuration option: ${_opt}" )
  endif( )
endmacro( tqt_expert_config )


#################################################
#####
##### tqt_requires
#####
##### Asserts that if opt1 is set opt2 is set also
#####
##### Syntax:
#####   tqt_requires( opt1 opt2 )

macro( tqt_requires _opt1 _opt2 )
  if( ${_opt1} AND NOT ${_opt2} )
    tde_message_fatal( "${_opt1}=ON requires ${_opt2}=ON to build" )
  endif( )
endmacro( tqt_requires )


#################################################
#####
##### 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_header_for_source
#####
##### The macro search appropriate header files for the given list of sources and
##### append them to the list specified by _include_out
#####
##### Syntax:
#####   tqt_header_for_source ( header_list source [source ...] )

macro( tqt_header_for_source _include_out )
  foreach( _src ${ARGN} )
    # As for now tqt has an "nt" prefix before filenames, so in order to handle all the casses
    # we will have to do some pattern matching
    get_filename_component( _src_path "${_src}" ABSOLUTE )
    get_filename_component( _src_path "${_src_path}" PATH )
    get_filename_component( _src_filename "${_src}" NAME_WE )
    file( GLOB _header_candidates  RELATIVE "${_src_path}/" "${_src_path}/*${_src_filename}*.h*" )

    # filter out headers without TQ_OBJECT
    unset( _filtered_header_candidates )
    foreach( _header IN LISTS _header_candidates )
      if( NOT _header MATCHES "^(n?t)?${_src_filename}(_p)?\\.(h|hpp)$" )
        continue()
      endif()
      file( STRINGS "${_src_path}/${_header}" _has_tq_object REGEX "T?Q_OBJECT" )
      if( _has_tq_object )
        list( APPEND _filtered_header_candidates "${_header}" )
      endif( _has_tq_object )
    endforeach( )
    set( _header_candidates "${_filtered_header_candidates}" )

    list( LENGTH _header_candidates _header_candidates_num )
    if( ${_header_candidates_num} EQUAL 1 )
      list( APPEND "${_include_out}" "${_src_path}/${_header_candidates}" )
    elseif( "${_header_candidates_num}" GREATER 1 )
      tde_message_fatal( "More than one candidates to automoc for ${_src_file}:"
                         " ${_header_candidates}" )
    else( )
      # do nothing, skip the file
    endif( )
  endforeach( _src )
endmacro( tqt_header_for_source )

#################################################
#####
##### tqt_automoc
#####
##### The macro is used for 'moc' processing specifically for TQt as such
##### and adding them to an existing target.
#####
##### Syntax:
#####   tqt_automoc(
#####     [TARGET] target
#####     [INCLUDES (AUTO|include_name) [include_name ...]]
#####   )

macro( tqt_automoc )
  unset( _target )
  unset( _includes )
  unset( _auto_includes )
  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( )

  # handle AUTO includes
  if( NOT _includes )
    set( _auto_includes 1 )
  elseif( AUTO IN_LIST _includes )
    set( _auto_includes 1 )
    list( REMOVE_ITEM _includes AUTO )
  endif()

  # target must already exist
  if( NOT TARGET ${_target} )
    tde_message_fatal( "The specified target does not exists." )
  endif()

  # check tmoc executable
  if( NOT TMOC_EXECUTABLE )
    tde_message_fatal( "tmoc is required but not found" )
  endif()

  # get list of sources of specified target
  get_target_property( _sources "${_target}" SOURCES )

  # automoc source files
  foreach( _src_file IN LISTS _sources )
    get_filename_component( _src_file "${_src_file}" ABSOLUTE )

    if( EXISTS "${_src_file}" )
      # get some essential  variables
      get_filename_component( _src_path "${_src_file}" ABSOLUTE )
      get_filename_component( _src_path "${_src_path}" PATH )
      get_filename_component( _src_filename "${_src_file}" NAME_WE )
      file( RELATIVE_PATH _src_path_relative "${CMAKE_SOURCE_DIR}" "${_src_path}" )

      # read source file and check if it has a moc include
      file( STRINGS "${_src_file}" _moc_includes REGEX
        "^#[ \t]*include[ \t]*[\"<]${_src_filename}\\.moc[\">]" )

      # found included moc(s)?
      if( _moc_includes ) # file has a moc include; we should moc src file itself
        set( _moc_file_relative "${_src_path_relative}/${_src_filename}.moc" )
        set( _moc_file "${CMAKE_BINARY_DIR}/${_moc_file_relative}" )
        add_custom_command( OUTPUT "${_moc_file}"
          COMMAND "${TMOC_EXECUTABLE}" "${_src_file}" -o "${_moc_file}"
          COMMENT "Generating ${_moc_file_relative}"
          DEPENDS "${_src_file}" )
        set_property( SOURCE "${_src_file}" APPEND PROPERTY OBJECT_DEPENDS "${_moc_file}" )
      endif( )
    endif( )
  endforeach( _src_file )

  # Get list of headers associated with sources
  if( _auto_includes )
    tqt_header_for_source( _includes ${_sources} )
  endif( )
  # processing headers
  foreach( _inc IN LISTS _includes )
    get_filename_component( _inc "${_inc}" ABSOLUTE )
    get_filename_component( _inc_name "${_inc}" NAME_WE )
    file( RELATIVE_PATH _inc_path_relative "${CMAKE_SOURCE_DIR}" "${_inc}" )
    get_filename_component( _inc_path_relative "${_inc_path}" PATH )

    set( _moc_file_relative "${_inc_path_relative}/moc_${_inc_name}.cpp" )
    set( _moc_file "${CMAKE_CURRENT_BINARY_DIR}/${_moc_file_relative}" )

    # moc-ing source
    add_custom_command( OUTPUT ${_moc_file}
      COMMAND "${TMOC_EXECUTABLE}" "${_inc}" -o "${_moc_file}"
      COMMENT "Generating ${_moc_file_relative}"
      DEPENDS "${_inc}"
    )
    set_property( TARGET ${_target} APPEND PROPERTY SOURCES ${_moc_file} )
  endforeach( _inc )
endmacro( tqt_automoc )


#################################################
#####
##### tqt_uic_embed
#####
##### The macro embeds given list of FILES into cpp source file OUTPUT and then adds
##### sad file to the dependencies of the TARGET (if specified). Either TARGET or
##### PROJECT and OUTPUT must be specified.
#####
##### Syntax:
#####   tqt_uic_embed (
#####     [[TARGET] target]
#####     [OUTPUT out]
#####     [PROJECT project]
#####     FILES file [file ...]
#####   )
#####
##### @arg TARGET  a name of the target the giles should be add as a dependencies to
##### @arg OUTPUT  a fileneme the resulting cpp file will be writen to. If not
#####              specified the name will be autoselected based on TARGET.
##### @arg PROJECT a suffix of a classname which will be used to access the
#####              files from source code, if not specified TARGET is used.
##### @arg FILES   a list of files to embed.
#####

function(tqt_uic_embed)
  unset( _target )
  unset( _project )
  unset( _output )
  unset( _files )
  unset( _directive )
  set( _var _target )

  foreach( _arg ${ARGN} )

    # found directive "TARGET"
    if( "+${_arg}" STREQUAL "+TARGET" )
      unset( _target )
      set( _var _target )
      set( _directive 1 )
    endif( )

    # found directive "OUTPUT"
    if( "+${_arg}" STREQUAL "+OUTPUT" )
      unset( _output )
      set( _var _output )
      set( _directive 1 )
    endif( )

    # found directive "PROJECT"
    if( "+${_arg}" STREQUAL "+PROJECT" )
      unset( _project )
      set( _var _project )
      set( _directive 1 )
    endif( )

    # found directive "FILES"
    if( "+${_arg}" STREQUAL "+FILES" )
      unset( _files )
      set( _var _files )
      set( _directive 1 )
    endif( )

    # collect data
    if( _directive )
      unset( _directive )
    elseif( _var )
      list( APPEND ${_var} ${_arg} )
    endif( )

  endforeach( )

  # check argument sanity
  if( NOT _output AND NOT _target )
    tde_message_fatal( "tqt_uic_embed() requires either TARGET or OUTPUT to be specified" )
   elseif( _target AND NOT TARGET ${_target} )
    tde_message_fatal( "The specified target does not exists." )
   elseif( NOT _files )
    tde_message_fatal( "At least one file to embed must be specified" )
   elseif( NOT TMOC_EXECUTABLE )
    tde_message_fatal( "tmoc is required but not found" )
  endif( )

  # default for _project
  if( NOT _project )
    set( _project "${_target}" )
  endif( )

  # choose output name if not specified
  if( NOT _output )
    set( _output "${CMAKE_CURRENT_BINARY_DIR}/${_target}_embedded_files.cpp" )
  endif( )

  # sanitize output to be an absolute path
  get_filename_component( _output ${_output} ABSOLUTE )

  add_custom_command( OUTPUT "${_output}"
    COMMAND "${UIC_EXECUTABLE}"
      -embed "${_project}" ${_files}
      -o "${_output}"
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    COMMENT "Generating embed images for ${_project}"
  )

  if( _target )
    set_property( TARGET "${_target}" APPEND PROPERTY SOURCES ${_output} )
  endif( )

endfunction(tqt_uic_embed)


#################################################
#####
##### 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 )