summaryrefslogtreecommitdiffstats
path: root/src/corelib/Qt5CoreMacros.cmake
blob: bc4c02082604fe1105b6471119fc8b4f6b677546 (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
#=============================================================================
# Copyright 2005-2011 Kitware, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
#
# * Neither the name of Kitware, Inc. nor the names of its
#   contributors may be used to endorse or promote products derived
#   from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#=============================================================================

######################################
#
#       Macros for building Qt files
#
######################################

include(CMakeParseArguments)

# macro used to create the names of output files preserving relative dirs
macro(QT5_MAKE_OUTPUT_FILE infile prefix ext outfile )
    string(LENGTH ${CMAKE_CURRENT_BINARY_DIR} _binlength)
    string(LENGTH ${infile} _infileLength)
    set(_checkinfile ${CMAKE_CURRENT_SOURCE_DIR})
    if(_infileLength GREATER _binlength)
        string(SUBSTRING "${infile}" 0 ${_binlength} _checkinfile)
        if(_checkinfile STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
            file(RELATIVE_PATH rel ${CMAKE_CURRENT_BINARY_DIR} ${infile})
        else()
            file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
        endif()
    else()
        file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
    endif()
    if(WIN32 AND rel MATCHES "^[a-zA-Z]:") # absolute path
        string(REGEX REPLACE "^([a-zA-Z]):(.*)$" "\\1_\\2" rel "${rel}")
    endif()
    set(_outfile "${CMAKE_CURRENT_BINARY_DIR}/${rel}")
    string(REPLACE ".." "__" _outfile ${_outfile})
    get_filename_component(outpath ${_outfile} PATH)
    get_filename_component(_outfile ${_outfile} NAME_WE)
    file(MAKE_DIRECTORY ${outpath})
    set(${outfile} ${outpath}/${prefix}${_outfile}.${ext})
endmacro()


macro(QT5_GET_MOC_FLAGS _moc_flags)
    set(${_moc_flags})
    get_directory_property(_inc_DIRS INCLUDE_DIRECTORIES)

    foreach(_current ${_inc_DIRS})
        if("${_current}" MATCHES "\\.framework/?$")
            string(REGEX REPLACE "/[^/]+\\.framework" "" framework_path "${_current}")
            set(${_moc_flags} ${${_moc_flags}} "-F${framework_path}")
        else()
            set(${_moc_flags} ${${_moc_flags}} "-I${_current}")
        endif()
    endforeach()

    get_directory_property(_defines COMPILE_DEFINITIONS)
    foreach(_current ${_defines})
        set(${_moc_flags} ${${_moc_flags}} "-D${_current}")
    endforeach()

    if(WIN32)
        set(${_moc_flags} ${${_moc_flags}} -DWIN32)
    endif()
endmacro()


# helper macro to set up a moc rule
macro(QT5_CREATE_MOC_COMMAND infile outfile moc_flags moc_options)
    # For Windows, create a parameters file to work around command line length limit
    if(WIN32)
        # Pass the parameters in a file.  Set the working directory to
        # be that containing the parameters file and reference it by
        # just the file name.  This is necessary because the moc tool on
        # MinGW builds does not seem to handle spaces in the path to the
        # file given with the @ syntax.
        get_filename_component(_moc_outfile_name "${outfile}" NAME)
        get_filename_component(_moc_outfile_dir "${outfile}" PATH)
        if(_moc_outfile_dir)
          set(_moc_working_dir WORKING_DIRECTORY ${_moc_outfile_dir})
        endif()
        set(_moc_parameters_file ${outfile}_parameters)
        set(_moc_parameters ${moc_flags} ${moc_options} -o "${outfile}" "${infile}")
        string(REPLACE ";" "\n" _moc_parameters "${_moc_parameters}")
        file(WRITE ${_moc_parameters_file} "${_moc_parameters}")
        add_custom_command(OUTPUT ${outfile}
                           COMMAND ${QT_MOC_EXECUTABLE} @${_moc_outfile_name}_parameters
                           DEPENDS ${infile}
                           ${_moc_working_dir}
                           VERBATIM)
    else()
        add_custom_command(OUTPUT ${outfile}
                           COMMAND ${QT_MOC_EXECUTABLE}
                           ARGS ${moc_flags} ${moc_options} -o ${outfile} ${infile}
                           DEPENDS ${infile} VERBATIM)
    endif()
endmacro()


function(QT5_GENERATE_MOC infile outfile )
    # get include dirs and flags
    qt5_get_moc_flags(moc_flags)
    get_filename_component(abs_infile ${infile} ABSOLUTE)
    set(_outfile "${outfile}")
    if(NOT IS_ABSOLUTE "${outfile}")
        set(_outfile "${CMAKE_CURRENT_BINARY_DIR}/${outfile}")
    endif()
    qt5_create_moc_command(${abs_infile} ${_outfile} "${moc_flags}" "")
    set_source_files_properties(${outfile} PROPERTIES SKIP_AUTOMOC TRUE)  # dont run automoc on this file
endfunction()


# qt5_wrap_cpp(outfiles inputfile ... )

function(QT5_WRAP_CPP outfiles )
    # get include dirs
    qt5_get_moc_flags(moc_flags)

    set(options)
    set(oneValueArgs)
    set(multiValueArgs OPTIONS)

    cmake_parse_arguments(_WRAP_CPP "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    set(moc_files ${_WRAP_CPP_UNPARSED_ARGUMENTS})
    set(moc_options ${_WRAP_CPP_OPTIONS})
    foreach(it ${moc_files})
        get_filename_component(it ${it} ABSOLUTE)
        qt5_make_output_file(${it} moc_ cxx outfile)
        qt5_create_moc_command(${it} ${outfile} "${moc_flags}" "${moc_options}")
        list(APPEND ${outfiles} ${outfile})
    endforeach()
    set(${outfiles} ${${outfiles}} PARENT_SCOPE)
endfunction()


# qt5_add_resources(outfiles inputfile ... )

function(QT5_ADD_RESOURCES outfiles )

    set(options)
    set(oneValueArgs)
    set(multiValueArgs OPTIONS)

    cmake_parse_arguments(_RCC "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    set(rcc_files ${_RCC_UNPARSED_ARGUMENTS})
    set(rcc_options ${_RCC_OPTIONS})

    foreach(it ${rcc_files})
        get_filename_component(outfilename ${it} NAME_WE)
        get_filename_component(infile ${it} ABSOLUTE)
        get_filename_component(rc_path ${infile} PATH)
        set(outfile ${CMAKE_CURRENT_BINARY_DIR}/qrc_${outfilename}.cxx)
        #  parse file for dependencies
        #  all files are absolute paths or relative to the location of the qrc file
        file(READ "${infile}" _RC_FILE_CONTENTS)
        string(REGEX MATCHALL "<file[^<]+" _RC_FILES "${_RC_FILE_CONTENTS}")
        set(_RC_DEPENDS)
        foreach(_RC_FILE ${_RC_FILES})
            string(REGEX REPLACE "^<file[^>]*>" "" _RC_FILE "${_RC_FILE}")
            if(NOT IS_ABSOLUTE "${_RC_FILE}")
                set(_RC_FILE "${rc_path}/${_RC_FILE}")
            endif()
            set(_RC_DEPENDS ${_RC_DEPENDS} "${_RC_FILE}")
        endforeach()
        # Since this cmake function is doing the dependency scanning for these files,
        # let's make a configured file and add it as a dependency so cmake is run
        # again when dependencies need to be recomputed.
        qt5_make_output_file("${infile}" "" "qrc.depends" out_depends)
        configure_file("${infile}" "${out_depends}" COPY_ONLY)
        add_custom_command(OUTPUT ${outfile}
                           COMMAND ${QT_RCC_EXECUTABLE}
                           ARGS ${rcc_options} -name ${outfilename} -o ${outfile} ${infile}
                           MAIN_DEPENDENCY ${infile}
                           DEPENDS ${_RC_DEPENDS} "${out_depends}" VERBATIM)
        list(APPEND ${outfiles} ${outfile})
    endforeach()
    set(${outfiles} ${${outfiles}} PARENT_SCOPE)
endfunction()


if (NOT CMAKE_VERSION VERSION_LESS 2.8.8)
    function(qt5_use_modules _target _link_type)
        if ("${_link_type}" STREQUAL "LINK_PUBLIC" OR "${_link_type}" STREQUAL "LINK_PRIVATE" )
            set(modules ${ARGN})
            set(link_type ${_link_type})
        else()
            set(modules ${_link_type} ${ARGN})
        endif()
        foreach(_module ${modules})
            if (NOT Qt5${_module}_FOUND)
                find_package(Qt5${_module} PATHS ${_qt5_corelib_install_prefix} NO_DEFAULT_PATH)
                if (NOT Qt5${_module}_FOUND)
                    message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
                endif()
            endif()
            target_link_libraries(${_target} ${link_type} ${Qt5${_module}_LIBRARIES})
            set_property(TARGET ${_target} APPEND PROPERTY INCLUDE_DIRECTORIES ${Qt5${_module}_INCLUDE_DIRS})
            set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS ${Qt5${_module}_COMPILE_DEFINITIONS})

            # We can't just append to the COMPILE_FLAGS property. That creats a ';' separated list
            # which breaks the compile commmand line.
            # Ensure non-duplication here manually instead.
            get_property(_target_type TARGET ${_target} PROPERTY TYPE)
            if ("${_target_type}" STREQUAL "EXECUTABLE" AND Qt5${_module}_EXECUTABLE_COMPILE_FLAGS)
              get_target_property(_flags ${_target} COMPILE_FLAGS)
              string(FIND "${_flags}" "${Qt5${_module}_EXECUTABLE_COMPILE_FLAGS}" _find_result)
              if (NOT _find_result)
                set_target_properties(${_target} PROPERTIES COMPILE_FLAGS "${_flags} ${Qt5${_module}_EXECUTABLE_COMPILE_FLAGS}")
              endif()
            endif()
        endforeach()
    endfunction()
endif()