7cccefd0149694c6ef84de194f80b7003074947d
[profile/ivi/qtbase.git] / src / corelib / Qt5CoreMacros.cmake
1 #=============================================================================
2 # Copyright 2005-2011 Kitware, Inc.
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # * Redistributions of source code must retain the above copyright
10 #   notice, this list of conditions and the following disclaimer.
11 #
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.
15 #
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.
19 #
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 #=============================================================================
32
33 ######################################
34 #
35 #       Macros for building Qt files
36 #
37 ######################################
38
39 include(CMakeParseArguments)
40
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})
50         else()
51             file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
52         endif()
53     else()
54         file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
55     endif()
56     if(WIN32 AND rel MATCHES "^[a-zA-Z]:") # absolute path
57         string(REGEX REPLACE "^([a-zA-Z]):(.*)$" "\\1_\\2" rel "${rel}")
58     endif()
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})
65 endmacro()
66
67
68 macro(QT5_GET_MOC_FLAGS _moc_flags)
69     set(${_moc_flags})
70     get_directory_property(_inc_DIRS INCLUDE_DIRECTORIES)
71
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}")
76         else()
77             set(${_moc_flags} ${${_moc_flags}} "-I${_current}")
78         endif()
79     endforeach()
80
81     get_directory_property(_defines COMPILE_DEFINITIONS)
82     foreach(_current ${_defines})
83         set(${_moc_flags} ${${_moc_flags}} "-D${_current}")
84     endforeach()
85
86     if(WIN32)
87         set(${_moc_flags} ${${_moc_flags}} -DWIN32)
88     endif()
89 endmacro()
90
91
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
95     if(WIN32)
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)
103         if(_moc_outfile_dir)
104           set(_moc_working_dir WORKING_DIRECTORY ${_moc_outfile_dir})
105         endif()
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
112                            DEPENDS ${infile}
113                            ${_moc_working_dir}
114                            VERBATIM)
115     else()
116         add_custom_command(OUTPUT ${outfile}
117                            COMMAND ${Qt5Core_MOC_EXECUTABLE}
118                            ARGS ${moc_flags} ${moc_options} -o ${outfile} ${infile}
119                            DEPENDS ${infile} VERBATIM)
120     endif()
121 endmacro()
122
123
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}")
131     endif()
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
134 endfunction()
135
136
137 # qt5_wrap_cpp(outfiles inputfile ... )
138
139 function(QT5_WRAP_CPP outfiles )
140     # get include dirs
141     qt5_get_moc_flags(moc_flags)
142
143     set(options)
144     set(oneValueArgs)
145     set(multiValueArgs OPTIONS)
146
147     cmake_parse_arguments(_WRAP_CPP "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
148
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})
156     endforeach()
157     set(${outfiles} ${${outfiles}} PARENT_SCOPE)
158 endfunction()
159
160
161 # qt5_add_resources(outfiles inputfile ... )
162
163 function(QT5_ADD_RESOURCES outfiles )
164
165     set(options)
166     set(oneValueArgs)
167     set(multiValueArgs OPTIONS)
168
169     cmake_parse_arguments(_RCC "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
170
171     set(rcc_files ${_RCC_UNPARSED_ARGUMENTS})
172     set(rcc_options ${_RCC_OPTIONS})
173
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)
179
180         set(_RC_DEPENDS)
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}")
190                 endif()
191                 set(_RC_DEPENDS ${_RC_DEPENDS} "${_RC_FILE}")
192             endforeach()
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)
198         else()
199             # The .qrc file does not exist (yet). Let's add a dependency and hope
200             # that it will be generated later
201             set(out_depends)
202         endif()
203
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})
210     endforeach()
211     set(${outfiles} ${${outfiles}} PARENT_SCOPE)
212 endfunction()
213
214
215 if (NOT CMAKE_VERSION VERSION_LESS 2.8.8)
216     function(qt5_use_modules _target _link_type)
217         if ("${_link_type}" STREQUAL "LINK_PUBLIC" OR "${_link_type}" STREQUAL "LINK_PRIVATE" )
218             set(modules ${ARGN})
219             set(link_type ${_link_type})
220         else()
221             set(modules ${_link_type} ${ARGN})
222         endif()
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.")
228                 endif()
229             endif()
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})
233
234             if (Qt5_POSITION_INDEPENDENT_CODE)
235                 set_property(TARGET ${_target} PROPERTY POSITION_INDEPENDENT_CODE ${Qt5_POSITION_INDEPENDENT_CODE})
236                 if (CMAKE_VERSION VERSION_LESS 2.8.9)
237                     # We can't just append to the COMPILE_FLAGS property. That creats a ';' separated list
238                     # which breaks the compile commmand line.
239                     # Ensure non-duplication here manually instead.
240                     get_property(_target_type TARGET ${_target} PROPERTY TYPE)
241                     if ("${_target_type}" STREQUAL "EXECUTABLE" AND Qt5${_module}_EXECUTABLE_COMPILE_FLAGS)
242                         get_target_property(_flags ${_target} COMPILE_FLAGS)
243                         string(FIND "${_flags}" "${Qt5${_module}_EXECUTABLE_COMPILE_FLAGS}" _find_result)
244                         if (NOT _find_result)
245                             set_target_properties(${_target} PROPERTIES COMPILE_FLAGS "${_flags} ${Qt5${_module}_EXECUTABLE_COMPILE_FLAGS}")
246                         endif()
247                     endif()
248                 endif()
249             endif()
250         endforeach()
251     endfunction()
252 endif()