1 #=============================================================================
2 # Copyright 2005-2011 Kitware, Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # * Neither the name of Kitware, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #=============================================================================
33 ######################################
35 # Macros for building Qt files
37 ######################################
39 include(CMakeParseArguments)
41 # macro used to create the names of output files preserving relative dirs
42 macro(QT5_MAKE_OUTPUT_FILE infile prefix ext outfile )
43 string(LENGTH ${CMAKE_CURRENT_BINARY_DIR} _binlength)
44 string(LENGTH ${infile} _infileLength)
45 set(_checkinfile ${CMAKE_CURRENT_SOURCE_DIR})
46 if(_infileLength GREATER _binlength)
47 string(SUBSTRING "${infile}" 0 ${_binlength} _checkinfile)
48 if(_checkinfile STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
49 file(RELATIVE_PATH rel ${CMAKE_CURRENT_BINARY_DIR} ${infile})
51 file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
54 file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
56 if(WIN32 AND rel MATCHES "^[a-zA-Z]:") # absolute path
57 string(REGEX REPLACE "^([a-zA-Z]):(.*)$" "\\1_\\2" rel "${rel}")
59 set(_outfile "${CMAKE_CURRENT_BINARY_DIR}/${rel}")
60 string(REPLACE ".." "__" _outfile ${_outfile})
61 get_filename_component(outpath ${_outfile} PATH)
62 get_filename_component(_outfile ${_outfile} NAME_WE)
63 file(MAKE_DIRECTORY ${outpath})
64 set(${outfile} ${outpath}/${prefix}${_outfile}.${ext})
68 macro(QT5_GET_MOC_FLAGS _moc_flags)
70 get_directory_property(_inc_DIRS INCLUDE_DIRECTORIES)
72 foreach(_current ${_inc_DIRS})
73 if("${_current}" MATCHES "\\.framework/?$")
74 string(REGEX REPLACE "/[^/]+\\.framework" "" framework_path "${_current}")
75 set(${_moc_flags} ${${_moc_flags}} "-F${framework_path}")
77 set(${_moc_flags} ${${_moc_flags}} "-I${_current}")
81 get_directory_property(_defines COMPILE_DEFINITIONS)
82 foreach(_current ${_defines})
83 set(${_moc_flags} ${${_moc_flags}} "-D${_current}")
87 set(${_moc_flags} ${${_moc_flags}} -DWIN32)
92 # helper macro to set up a moc rule
93 macro(QT5_CREATE_MOC_COMMAND infile outfile moc_flags moc_options)
94 # For Windows, create a parameters file to work around command line length limit
96 # Pass the parameters in a file. Set the working directory to
97 # be that containing the parameters file and reference it by
98 # just the file name. This is necessary because the moc tool on
99 # MinGW builds does not seem to handle spaces in the path to the
100 # file given with the @ syntax.
101 get_filename_component(_moc_outfile_name "${outfile}" NAME)
102 get_filename_component(_moc_outfile_dir "${outfile}" PATH)
104 set(_moc_working_dir WORKING_DIRECTORY ${_moc_outfile_dir})
106 set(_moc_parameters_file ${outfile}_parameters)
107 set(_moc_parameters ${moc_flags} ${moc_options} -o "${outfile}" "${infile}")
108 string(REPLACE ";" "\n" _moc_parameters "${_moc_parameters}")
109 file(WRITE ${_moc_parameters_file} "${_moc_parameters}")
110 add_custom_command(OUTPUT ${outfile}
111 COMMAND ${Qt5Core_MOC_EXECUTABLE} @${_moc_outfile_name}_parameters
116 add_custom_command(OUTPUT ${outfile}
117 COMMAND ${Qt5Core_MOC_EXECUTABLE}
118 ARGS ${moc_flags} ${moc_options} -o ${outfile} ${infile}
119 DEPENDS ${infile} VERBATIM)
124 function(QT5_GENERATE_MOC infile outfile )
125 # get include dirs and flags
126 qt5_get_moc_flags(moc_flags)
127 get_filename_component(abs_infile ${infile} ABSOLUTE)
128 set(_outfile "${outfile}")
129 if(NOT IS_ABSOLUTE "${outfile}")
130 set(_outfile "${CMAKE_CURRENT_BINARY_DIR}/${outfile}")
132 qt5_create_moc_command(${abs_infile} ${_outfile} "${moc_flags}" "")
133 set_source_files_properties(${outfile} PROPERTIES SKIP_AUTOMOC TRUE) # dont run automoc on this file
137 # qt5_wrap_cpp(outfiles inputfile ... )
139 function(QT5_WRAP_CPP outfiles )
141 qt5_get_moc_flags(moc_flags)
145 set(multiValueArgs OPTIONS)
147 cmake_parse_arguments(_WRAP_CPP "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
149 set(moc_files ${_WRAP_CPP_UNPARSED_ARGUMENTS})
150 set(moc_options ${_WRAP_CPP_OPTIONS})
151 foreach(it ${moc_files})
152 get_filename_component(it ${it} ABSOLUTE)
153 qt5_make_output_file(${it} moc_ cxx outfile)
154 qt5_create_moc_command(${it} ${outfile} "${moc_flags}" "${moc_options}")
155 list(APPEND ${outfiles} ${outfile})
157 set(${outfiles} ${${outfiles}} PARENT_SCOPE)
161 # qt5_add_resources(outfiles inputfile ... )
163 function(QT5_ADD_RESOURCES outfiles )
167 set(multiValueArgs OPTIONS)
169 cmake_parse_arguments(_RCC "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
171 set(rcc_files ${_RCC_UNPARSED_ARGUMENTS})
172 set(rcc_options ${_RCC_OPTIONS})
174 foreach(it ${rcc_files})
175 get_filename_component(outfilename ${it} NAME_WE)
176 get_filename_component(infile ${it} ABSOLUTE)
177 get_filename_component(rc_path ${infile} PATH)
178 set(outfile ${CMAKE_CURRENT_BINARY_DIR}/qrc_${outfilename}.cxx)
181 if(EXISTS "${infile}")
182 # parse file for dependencies
183 # all files are absolute paths or relative to the location of the qrc file
184 file(READ "${infile}" _RC_FILE_CONTENTS)
185 string(REGEX MATCHALL "<file[^<]+" _RC_FILES "${_RC_FILE_CONTENTS}")
186 foreach(_RC_FILE ${_RC_FILES})
187 string(REGEX REPLACE "^<file[^>]*>" "" _RC_FILE "${_RC_FILE}")
188 if(NOT IS_ABSOLUTE "${_RC_FILE}")
189 set(_RC_FILE "${rc_path}/${_RC_FILE}")
191 set(_RC_DEPENDS ${_RC_DEPENDS} "${_RC_FILE}")
193 # Since this cmake macro is doing the dependency scanning for these files,
194 # let's make a configured file and add it as a dependency so cmake is run
195 # again when dependencies need to be recomputed.
196 qt5_make_output_file("${infile}" "" "qrc.depends" out_depends)
197 configure_file("${infile}" "${out_depends}" COPY_ONLY)
199 # The .qrc file does not exist (yet). Let's add a dependency and hope
200 # that it will be generated later
204 add_custom_command(OUTPUT ${outfile}
205 COMMAND ${Qt5Core_RCC_EXECUTABLE}
206 ARGS ${rcc_options} -name ${outfilename} -o ${outfile} ${infile}
207 MAIN_DEPENDENCY ${infile}
208 DEPENDS ${_RC_DEPENDS} "${out_depends}" VERBATIM)
209 list(APPEND ${outfiles} ${outfile})
211 set(${outfiles} ${${outfiles}} PARENT_SCOPE)
215 if (NOT CMAKE_VERSION VERSION_LESS 2.8.9)
216 function(qt5_use_modules _target _link_type)
217 if ("${_link_type}" STREQUAL "LINK_PUBLIC" OR "${_link_type}" STREQUAL "LINK_PRIVATE" )
219 set(link_type ${_link_type})
221 set(modules ${_link_type} ${ARGN})
223 foreach(_module ${modules})
224 if (NOT Qt5${_module}_FOUND)
225 find_package(Qt5${_module} PATHS ${_qt5_corelib_install_prefix} NO_DEFAULT_PATH)
226 if (NOT Qt5${_module}_FOUND)
227 message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
230 target_link_libraries(${_target} ${link_type} ${Qt5${_module}_LIBRARIES})
231 set_property(TARGET ${_target} APPEND PROPERTY INCLUDE_DIRECTORIES ${Qt5${_module}_INCLUDE_DIRS})
232 set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS ${Qt5${_module}_COMPILE_DEFINITIONS})
234 if (Qt5_POSITION_INDEPENDENT_CODE)
235 set_property(TARGET ${_target} PROPERTY POSITION_INDEPENDENT_CODE ${Qt5_POSITION_INDEPENDENT_CODE})