aee40288f16029059cf54d4ae5a1cafe04e217ba
[platform/upstream/cmake.git] / Modules / Qt4Macros.cmake
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 #.rst:
5 # Qt4Macros
6 # ---------
7 #
8 #
9 #
10 # This file is included by FindQt4.cmake, don't include it directly.
11
12 ######################################
13 #
14 #       Macros for building Qt files
15 #
16 ######################################
17
18
19 macro (QT4_EXTRACT_OPTIONS _qt4_files _qt4_options _qt4_target)
20   set(${_qt4_files})
21   set(${_qt4_options})
22   set(_QT4_DOING_OPTIONS FALSE)
23   set(_QT4_DOING_TARGET FALSE)
24   foreach(_currentArg ${ARGN})
25     if ("x${_currentArg}" STREQUAL "xOPTIONS")
26       set(_QT4_DOING_OPTIONS TRUE)
27     elseif ("x${_currentArg}" STREQUAL "xTARGET")
28       set(_QT4_DOING_TARGET TRUE)
29     else ()
30       if(_QT4_DOING_TARGET)
31         set(${_qt4_target} "${_currentArg}")
32       elseif(_QT4_DOING_OPTIONS)
33         list(APPEND ${_qt4_options} "${_currentArg}")
34       else()
35         list(APPEND ${_qt4_files} "${_currentArg}")
36       endif()
37     endif ()
38   endforeach()
39 endmacro ()
40
41
42 # macro used to create the names of output files preserving relative dirs
43 macro (QT4_MAKE_OUTPUT_FILE infile prefix ext outfile )
44   string(LENGTH ${CMAKE_CURRENT_BINARY_DIR} _binlength)
45   string(LENGTH ${infile} _infileLength)
46   set(_checkinfile ${CMAKE_CURRENT_SOURCE_DIR})
47   if(_infileLength GREATER _binlength)
48     string(SUBSTRING "${infile}" 0 ${_binlength} _checkinfile)
49     if(_checkinfile STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
50       file(RELATIVE_PATH rel ${CMAKE_CURRENT_BINARY_DIR} ${infile})
51     else()
52       file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
53     endif()
54   else()
55     file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
56   endif()
57   if(WIN32 AND rel MATCHES "^([a-zA-Z]):(.*)$") # absolute path
58     set(rel "${CMAKE_MATCH_1}_${CMAKE_MATCH_2}")
59   endif()
60   set(_outfile "${CMAKE_CURRENT_BINARY_DIR}/${rel}")
61   string(REPLACE ".." "__" _outfile ${_outfile})
62   get_filename_component(outpath ${_outfile} PATH)
63   get_filename_component(_outfile ${_outfile} NAME_WE)
64   file(MAKE_DIRECTORY ${outpath})
65   set(${outfile} ${outpath}/${prefix}${_outfile}.${ext})
66 endmacro ()
67
68
69 macro (QT4_GET_MOC_FLAGS _moc_flags)
70   set(${_moc_flags})
71   get_directory_property(_inc_DIRS INCLUDE_DIRECTORIES)
72
73   foreach(_current ${_inc_DIRS})
74     if("${_current}" MATCHES "\\.framework/?$")
75       string(REGEX REPLACE "/[^/]+\\.framework" "" framework_path "${_current}")
76       set(${_moc_flags} ${${_moc_flags}} "-F${framework_path}")
77     else()
78       set(${_moc_flags} ${${_moc_flags}} "-I${_current}")
79     endif()
80   endforeach()
81
82   get_directory_property(_defines COMPILE_DEFINITIONS)
83   foreach(_current ${_defines})
84     set(${_moc_flags} ${${_moc_flags}} "-D${_current}")
85   endforeach()
86
87   if(Q_WS_WIN)
88     set(${_moc_flags} ${${_moc_flags}} -DWIN32)
89   endif()
90
91 endmacro()
92
93
94 # helper macro to set up a moc rule
95 function (QT4_CREATE_MOC_COMMAND infile outfile moc_flags moc_options moc_target)
96   # For Windows, create a parameters file to work around command line length limit
97   # Pass the parameters in a file.  Set the working directory to
98   # be that containing the parameters file and reference it by
99   # just the file name.  This is necessary because the moc tool on
100   # MinGW builds does not seem to handle spaces in the path to the
101   # file given with the @ syntax.
102   get_filename_component(_moc_outfile_name "${outfile}" NAME)
103   get_filename_component(_moc_outfile_dir "${outfile}" PATH)
104   if(_moc_outfile_dir)
105     set(_moc_working_dir WORKING_DIRECTORY ${_moc_outfile_dir})
106   endif()
107   set (_moc_parameters_file ${outfile}_parameters)
108   set (_moc_parameters ${moc_flags} ${moc_options} -o "${outfile}" "${infile}")
109   string (REPLACE ";" "\n" _moc_parameters "${_moc_parameters}")
110
111   if(moc_target)
112     set (_moc_parameters_file ${_moc_parameters_file}$<$<BOOL:$<CONFIGURATION>>:_$<CONFIGURATION>>)
113     set(targetincludes "$<TARGET_PROPERTY:${moc_target},INCLUDE_DIRECTORIES>")
114     set(targetdefines "$<TARGET_PROPERTY:${moc_target},COMPILE_DEFINITIONS>")
115
116     set(targetincludes "$<$<BOOL:${targetincludes}>:-I$<JOIN:${targetincludes},\n-I>\n>")
117     set(targetdefines "$<$<BOOL:${targetdefines}>:-D$<JOIN:${targetdefines},\n-D>\n>")
118
119     file (GENERATE
120       OUTPUT ${_moc_parameters_file}
121       CONTENT "${targetdefines}${targetincludes}${_moc_parameters}\n"
122     )
123
124     set(targetincludes)
125     set(targetdefines)
126   else()
127     set(CMAKE_CONFIGURABLE_FILE_CONTENT "${_moc_parameters}")
128     configure_file("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
129                    "${_moc_parameters_file}" @ONLY)
130   endif()
131
132   set(_moc_extra_parameters_file @${_moc_parameters_file})
133   add_custom_command(OUTPUT ${outfile}
134                       COMMAND Qt4::moc ${_moc_extra_parameters_file}
135                       DEPENDS ${infile} ${_moc_parameters_file}
136                       ${_moc_working_dir}
137                       VERBATIM)
138 endfunction ()
139
140
141 macro (QT4_GENERATE_MOC infile outfile )
142 # get include dirs and flags
143    QT4_GET_MOC_FLAGS(moc_flags)
144    get_filename_component(abs_infile ${infile} ABSOLUTE)
145    set(_outfile "${outfile}")
146    if(NOT IS_ABSOLUTE "${outfile}")
147      set(_outfile "${CMAKE_CURRENT_BINARY_DIR}/${outfile}")
148    endif()
149
150    if (${ARGC} GREATER 3 AND "x${ARGV2}" STREQUAL "xTARGET")
151       set(moc_target ${ARGV3})
152    endif()
153    QT4_CREATE_MOC_COMMAND(${abs_infile} ${_outfile} "${moc_flags}" "" "${moc_target}")
154    set_source_files_properties(${outfile} PROPERTIES SKIP_AUTOMOC TRUE)  # don't run automoc on this file
155 endmacro ()
156
157
158 # QT4_WRAP_CPP(outfiles inputfile ... )
159
160 macro (QT4_WRAP_CPP outfiles )
161   # get include dirs
162   QT4_GET_MOC_FLAGS(moc_flags)
163   QT4_EXTRACT_OPTIONS(moc_files moc_options moc_target ${ARGN})
164
165   foreach (it ${moc_files})
166     get_filename_component(it ${it} ABSOLUTE)
167     QT4_MAKE_OUTPUT_FILE(${it} moc_ cxx outfile)
168     QT4_CREATE_MOC_COMMAND(${it} ${outfile} "${moc_flags}" "${moc_options}" "${moc_target}")
169     set(${outfiles} ${${outfiles}} ${outfile})
170   endforeach()
171
172 endmacro ()
173
174
175 # QT4_WRAP_UI(outfiles inputfile ... )
176
177 macro (QT4_WRAP_UI outfiles )
178   QT4_EXTRACT_OPTIONS(ui_files ui_options ui_target ${ARGN})
179
180   foreach (it ${ui_files})
181     get_filename_component(outfile ${it} NAME_WE)
182     get_filename_component(infile ${it} ABSOLUTE)
183     set(outfile ${CMAKE_CURRENT_BINARY_DIR}/ui_${outfile}.h)
184     add_custom_command(OUTPUT ${outfile}
185       COMMAND Qt4::uic
186       ARGS ${ui_options} -o ${outfile} ${infile}
187       MAIN_DEPENDENCY ${infile} VERBATIM)
188     set(${outfiles} ${${outfiles}} ${outfile})
189   endforeach ()
190
191 endmacro ()
192
193
194 # QT4_ADD_RESOURCES(outfiles inputfile ... )
195
196 macro (QT4_ADD_RESOURCES outfiles )
197   QT4_EXTRACT_OPTIONS(rcc_files rcc_options rcc_target ${ARGN})
198
199   foreach (it ${rcc_files})
200     get_filename_component(outfilename ${it} NAME_WE)
201     get_filename_component(infile ${it} ABSOLUTE)
202     get_filename_component(rc_path ${infile} PATH)
203     set(outfile ${CMAKE_CURRENT_BINARY_DIR}/qrc_${outfilename}.cxx)
204
205     set(_RC_DEPENDS)
206     if(EXISTS "${infile}")
207       #  parse file for dependencies
208       #  all files are absolute paths or relative to the location of the qrc file
209       file(READ "${infile}" _RC_FILE_CONTENTS)
210       string(REGEX MATCHALL "<file[^<]+" _RC_FILES "${_RC_FILE_CONTENTS}")
211       foreach(_RC_FILE ${_RC_FILES})
212         string(REGEX REPLACE "^<file[^>]*>" "" _RC_FILE "${_RC_FILE}")
213         if(NOT IS_ABSOLUTE "${_RC_FILE}")
214           set(_RC_FILE "${rc_path}/${_RC_FILE}")
215         endif()
216         set(_RC_DEPENDS ${_RC_DEPENDS} "${_RC_FILE}")
217       endforeach()
218       unset(_RC_FILES)
219       unset(_RC_FILE_CONTENTS)
220       # Since this cmake macro is doing the dependency scanning for these files,
221       # let's make a configured file and add it as a dependency so cmake is run
222       # again when dependencies need to be recomputed.
223       QT4_MAKE_OUTPUT_FILE("${infile}" "" "qrc.depends" out_depends)
224       configure_file("${infile}" "${out_depends}" COPYONLY)
225     else()
226       # The .qrc file does not exist (yet). Let's add a dependency and hope
227       # that it will be generated later
228       set(out_depends)
229     endif()
230
231     add_custom_command(OUTPUT ${outfile}
232       COMMAND Qt4::rcc
233       ARGS ${rcc_options} -name ${outfilename} -o ${outfile} ${infile}
234       MAIN_DEPENDENCY ${infile}
235       DEPENDS ${_RC_DEPENDS} "${out_depends}" VERBATIM)
236     set(${outfiles} ${${outfiles}} ${outfile})
237   endforeach ()
238
239 endmacro ()
240
241
242 macro(QT4_ADD_DBUS_INTERFACE _sources _interface _basename)
243   get_filename_component(_infile ${_interface} ABSOLUTE)
244   set(_header "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.h")
245   set(_impl   "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.cpp")
246   set(_moc    "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.moc")
247
248   get_source_file_property(_nonamespace ${_interface} NO_NAMESPACE)
249   if(_nonamespace)
250     set(_params -N -m)
251   else()
252     set(_params -m)
253   endif()
254
255   get_source_file_property(_classname ${_interface} CLASSNAME)
256   if(_classname)
257     set(_params ${_params} -c ${_classname})
258   endif()
259
260   get_source_file_property(_include ${_interface} INCLUDE)
261   if(_include)
262     set(_params ${_params} -i ${_include})
263   endif()
264
265   add_custom_command(OUTPUT "${_impl}" "${_header}"
266       COMMAND Qt4::qdbusxml2cpp ${_params} -p ${_basename} ${_infile}
267       DEPENDS ${_infile} VERBATIM)
268
269   set_source_files_properties("${_impl}" PROPERTIES SKIP_AUTOMOC TRUE)
270
271   QT4_GENERATE_MOC("${_header}" "${_moc}")
272
273   list(APPEND ${_sources} "${_impl}" "${_header}" "${_moc}")
274   MACRO_ADD_FILE_DEPENDENCIES("${_impl}" "${_moc}")
275
276 endmacro()
277
278
279 macro(QT4_ADD_DBUS_INTERFACES _sources)
280   foreach (_current_FILE ${ARGN})
281     get_filename_component(_infile ${_current_FILE} ABSOLUTE)
282     get_filename_component(_basename ${_current_FILE} NAME)
283     # get the part before the ".xml" suffix
284     string(TOLOWER ${_basename} _basename)
285     string(REGEX REPLACE "(.*\\.)?([^\\.]+)\\.xml" "\\2" _basename ${_basename})
286     QT4_ADD_DBUS_INTERFACE(${_sources} ${_infile} ${_basename}interface)
287   endforeach ()
288 endmacro()
289
290
291 macro(QT4_GENERATE_DBUS_INTERFACE _header) # _customName OPTIONS -some -options )
292   QT4_EXTRACT_OPTIONS(_customName _qt4_dbus_options _qt4_dbus_target ${ARGN})
293
294   get_filename_component(_in_file ${_header} ABSOLUTE)
295   get_filename_component(_basename ${_header} NAME_WE)
296
297   if (_customName)
298     if (IS_ABSOLUTE ${_customName})
299       get_filename_component(_containingDir ${_customName} PATH)
300       if (NOT EXISTS ${_containingDir})
301         file(MAKE_DIRECTORY "${_containingDir}")
302       endif()
303       set(_target ${_customName})
304     else()
305       set(_target ${CMAKE_CURRENT_BINARY_DIR}/${_customName})
306     endif()
307   else ()
308     set(_target ${CMAKE_CURRENT_BINARY_DIR}/${_basename}.xml)
309   endif ()
310
311   add_custom_command(OUTPUT ${_target}
312       COMMAND Qt4::qdbuscpp2xml ${_qt4_dbus_options} ${_in_file} -o ${_target}
313       DEPENDS ${_in_file} VERBATIM
314   )
315 endmacro()
316
317
318 macro(QT4_ADD_DBUS_ADAPTOR _sources _xml_file _include _parentClass) # _optionalBasename _optionalClassName)
319   get_filename_component(_infile ${_xml_file} ABSOLUTE)
320
321   unset(_optionalBasename)
322   if(${ARGC} GREATER 4)
323     set(_optionalBasename "${ARGV4}")
324   endif()
325   if (_optionalBasename)
326     set(_basename ${_optionalBasename} )
327   else ()
328     string(REGEX REPLACE "(.*[/\\.])?([^\\.]+)\\.xml" "\\2adaptor" _basename ${_infile})
329     string(TOLOWER ${_basename} _basename)
330   endif ()
331
332   unset(_optionalClassName)
333   if(${ARGC} GREATER 5)
334     set(_optionalClassName "${ARGV5}")
335   endif()
336   set(_header "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.h")
337   set(_impl   "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.cpp")
338   set(_moc    "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.moc")
339
340   if(_optionalClassName)
341     add_custom_command(OUTPUT "${_impl}" "${_header}"
342        COMMAND Qt4::qdbusxml2cpp -m -a ${_basename} -c ${_optionalClassName} -i ${_include} -l ${_parentClass} ${_infile}
343        DEPENDS ${_infile} VERBATIM
344     )
345   else()
346     add_custom_command(OUTPUT "${_impl}" "${_header}"
347        COMMAND Qt4::qdbusxml2cpp -m -a ${_basename} -i ${_include} -l ${_parentClass} ${_infile}
348        DEPENDS ${_infile} VERBATIM
349      )
350   endif()
351
352   QT4_GENERATE_MOC("${_header}" "${_moc}")
353   set_source_files_properties("${_impl}" PROPERTIES SKIP_AUTOMOC TRUE)
354   MACRO_ADD_FILE_DEPENDENCIES("${_impl}" "${_moc}")
355
356   list(APPEND ${_sources} "${_impl}" "${_header}" "${_moc}")
357 endmacro()
358
359
360 macro(QT4_AUTOMOC)
361   if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.11)
362     message(DEPRECATION "The qt4_automoc macro is obsolete. Use the CMAKE_AUTOMOC feature instead.")
363   endif()
364   QT4_GET_MOC_FLAGS(_moc_INCS)
365
366   set(_matching_FILES )
367   foreach (_current_FILE ${ARGN})
368
369     get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
370     # if "SKIP_AUTOMOC" is set to true, we will not handle this file here.
371     # This is required to make uic work correctly:
372     # we need to add generated .cpp files to the sources (to compile them),
373     # but we cannot let automoc handle them, as the .cpp files don't exist yet when
374     # cmake is run for the very first time on them -> however the .cpp files might
375     # exist at a later run. at that time we need to skip them, so that we don't add two
376     # different rules for the same moc file
377     get_source_file_property(_skip ${_abs_FILE} SKIP_AUTOMOC)
378
379     if ( NOT _skip AND EXISTS ${_abs_FILE} )
380
381       file(READ ${_abs_FILE} _contents)
382
383       get_filename_component(_abs_PATH ${_abs_FILE} PATH)
384
385       string(REGEX MATCHALL "# *include +[^ ]+\\.moc[\">]" _match "${_contents}")
386       if(_match)
387         foreach (_current_MOC_INC ${_match})
388           string(REGEX MATCH "[^ <\"]+\\.moc" _current_MOC "${_current_MOC_INC}")
389
390           get_filename_component(_basename ${_current_MOC} NAME_WE)
391           if(EXISTS ${_abs_PATH}/${_basename}.hpp)
392             set(_header ${_abs_PATH}/${_basename}.hpp)
393           else()
394             set(_header ${_abs_PATH}/${_basename}.h)
395           endif()
396           set(_moc    ${CMAKE_CURRENT_BINARY_DIR}/${_current_MOC})
397           QT4_CREATE_MOC_COMMAND(${_header} ${_moc} "${_moc_INCS}" "" "")
398           MACRO_ADD_FILE_DEPENDENCIES(${_abs_FILE} ${_moc})
399         endforeach ()
400       endif()
401     endif ()
402   endforeach ()
403 endmacro()
404
405
406 macro(QT4_CREATE_TRANSLATION _qm_files)
407    QT4_EXTRACT_OPTIONS(_lupdate_files _lupdate_options _lupdate_target ${ARGN})
408    set(_my_sources)
409    set(_my_dirs)
410    set(_my_tsfiles)
411    set(_ts_pro)
412    foreach (_file ${_lupdate_files})
413      get_filename_component(_ext ${_file} EXT)
414      get_filename_component(_abs_FILE ${_file} ABSOLUTE)
415      if(_ext MATCHES "ts")
416        list(APPEND _my_tsfiles ${_abs_FILE})
417      else()
418        if(NOT _ext)
419          list(APPEND _my_dirs ${_abs_FILE})
420        else()
421          list(APPEND _my_sources ${_abs_FILE})
422        endif()
423      endif()
424    endforeach()
425    foreach(_ts_file ${_my_tsfiles})
426      if(_my_sources)
427        # make a .pro file to call lupdate on, so we don't make our commands too
428        # long for some systems
429        get_filename_component(_ts_name ${_ts_file} NAME_WE)
430        set(_ts_pro ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_ts_name}_lupdate.pro)
431        set(_pro_srcs)
432        foreach(_pro_src ${_my_sources})
433          string(APPEND _pro_srcs " \\\n  \"${_pro_src}\"")
434        endforeach()
435        set(_pro_includes)
436        get_directory_property(_inc_DIRS INCLUDE_DIRECTORIES)
437        list(REMOVE_DUPLICATES _inc_DIRS)
438        foreach(_pro_include ${_inc_DIRS})
439          get_filename_component(_abs_include "${_pro_include}" ABSOLUTE)
440          string(APPEND _pro_includes " \\\n  \"${_abs_include}\"")
441        endforeach()
442        file(GENERATE OUTPUT ${_ts_pro} CONTENT "SOURCES =${_pro_srcs}\nINCLUDEPATH =${_pro_includes}\n")
443      endif()
444      add_custom_command(OUTPUT ${_ts_file}
445         COMMAND Qt4::lupdate
446         ARGS ${_lupdate_options} ${_ts_pro} ${_my_dirs} -ts ${_ts_file}
447         DEPENDS ${_my_sources} ${_ts_pro} VERBATIM)
448    endforeach()
449    QT4_ADD_TRANSLATION(${_qm_files} ${_my_tsfiles})
450 endmacro()
451
452
453 macro(QT4_ADD_TRANSLATION _qm_files)
454   foreach (_current_FILE ${ARGN})
455     get_filename_component(_abs_FILE ${_current_FILE} ABSOLUTE)
456     get_filename_component(qm ${_abs_FILE} NAME_WE)
457     get_source_file_property(output_location ${_abs_FILE} OUTPUT_LOCATION)
458     if(output_location)
459       file(MAKE_DIRECTORY "${output_location}")
460       set(qm "${output_location}/${qm}.qm")
461     else()
462       set(qm "${CMAKE_CURRENT_BINARY_DIR}/${qm}.qm")
463     endif()
464
465     add_custom_command(OUTPUT ${qm}
466        COMMAND Qt4::lrelease
467        ARGS ${_abs_FILE} -qm ${qm}
468        DEPENDS ${_abs_FILE} VERBATIM
469     )
470     set(${_qm_files} ${${_qm_files}} ${qm})
471   endforeach ()
472 endmacro()
473
474 function(qt4_use_modules _target _link_type)
475   if(NOT CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 2.8.11)
476     message(DEPRECATION "The qt4_use_modules function is obsolete. Use target_link_libraries with IMPORTED targets instead.")
477   endif()
478   if ("${_link_type}" STREQUAL "LINK_PUBLIC" OR "${_link_type}" STREQUAL "LINK_PRIVATE")
479     set(modules ${ARGN})
480     set(link_type ${_link_type})
481   else()
482     set(modules ${_link_type} ${ARGN})
483   endif()
484   foreach(_module ${modules})
485     string(TOUPPER ${_module} _ucmodule)
486     set(_targetPrefix QT_QT${_ucmodule})
487     if (_ucmodule STREQUAL QAXCONTAINER OR _ucmodule STREQUAL QAXSERVER)
488       if (NOT QT_Q${_ucmodule}_FOUND)
489         message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
490       endif()
491       set(_targetPrefix QT_Q${_ucmodule})
492     else()
493       if (NOT QT_QT${_ucmodule}_FOUND)
494         message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
495       endif()
496       if ("${_ucmodule}" STREQUAL "MAIN")
497         message(FATAL_ERROR "Can not use \"${_module}\" module with qt4_use_modules.")
498       endif()
499     endif()
500     target_link_libraries(${_target} ${link_type} ${${_targetPrefix}_LIBRARIES})
501     set_property(TARGET ${_target} APPEND PROPERTY INCLUDE_DIRECTORIES ${${_targetPrefix}_INCLUDE_DIR} ${QT_HEADERS_DIR} ${QT_MKSPECS_DIR}/default)
502     set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS ${${_targetPrefix}_COMPILE_DEFINITIONS})
503   endforeach()
504 endfunction()