summaryrefslogtreecommitdiffstats
path: root/cmake/QtProcessConfigureArgs.cmake
blob: 55c4fbfe052033437d054ad6784840fa72df50fd (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
# This script reads Qt configure arguments from config.opt,
# translates the arguments to CMake arguments and calls CMake.
#
# This file is to be used in CMake script mode with the following variables set:
# OPTFILE: A text file containing the options that were passed to configure
#          with one option per line.

cmake_policy(SET CMP0007 NEW)

set(cmake_args "")
macro(push)
    list(APPEND cmake_args ${ARGN})
endmacro()

macro(calculate_state)
    set(state ON)
    if(CMAKE_MATCH_1 MATCHES "no-?")
        set(state OFF)
    endif()
endmacro()

macro(pop_path_argument)
    list(POP_FRONT configure_args path)
    string(REGEX REPLACE "^\"(.*)\"$" "\\1" path "${path}")
    file(TO_CMAKE_PATH "${path}" path)
endmacro()

get_filename_component(source_dir ".." ABSOLUTE BASE_DIR "${CMAKE_CURRENT_LIST_DIR}")
file(STRINGS "${OPTFILE}" configure_args)
list(FILTER configure_args EXCLUDE REGEX "^[ \t]*$")
list(TRANSFORM configure_args STRIP)
unset(generator)
set(auto_detect_generator TRUE)
unset(build_configs)
unset(device_options)
unset(extra_rpaths)
while(configure_args)
    list(POP_FRONT configure_args arg)
    if(arg STREQUAL "-cmake")
        # ignore
    elseif(arg STREQUAL "-cmake-generator")
        list(POP_FRONT configure_args generator)
    elseif(arg STREQUAL "-cmake-use-default-generator")
        set(auto_detect_generator FALSE)
    elseif(arg STREQUAL "-top-level")
        get_filename_component(source_dir "../.." ABSOLUTE BASE_DIR "${CMAKE_CURRENT_LIST_DIR}")
    elseif(arg STREQUAL "-skip")
        list(POP_FRONT configure_args qtrepo)
        push("-DBUILD_${qtrepo}=OFF")
    elseif(arg STREQUAL "-opensource")
        # to be done
    elseif(arg STREQUAL "-commercial")
        # to be done
    elseif(arg STREQUAL "-confirm-license")
        # to be done
    elseif(arg MATCHES "^-(no)?make")
        calculate_state()
        list(POP_FRONT configure_args component)
        if(component STREQUAL "tests")
            push(-DBUILD_TESTING=${state})
        elseif(component STREQUAL "examples")
            push(-DBUILD_EXAMPLES=${state})
        else()
            string(TOUPPER "${component}" uc_component)
            push(-DQT_NO_MAKE_${uc_component}=${state})
        endif()
    elseif(arg MATCHES "^-(no-)?feature")
        calculate_state()
        list(POP_FRONT configure_args feature)
        push("-DFEATURE_${feature}=${state}")
    elseif(arg MATCHES "^-(no-)?pch")
        calculate_state()
        push("-DBUILD_WITH_PCH=${state}")
    elseif(arg MATCHES "^-system-(.*)")
        list(POP_FRONT configure_args lib)
        push("-DFEATURE_system_${lib}=ON")
    elseif(arg MATCHES "^-qt-(.*)")
        list(POP_FRONT configure_args lib)
        push("-DFEATURE_system_${lib}=OFF")
    elseif(arg MATCHES "^-sanitize=(.*)")
        push("-DECM_ENABLE_SANITIZERS=${CMAKE_MATCH_1}")
    elseif(arg STREQUAL "-ccache")
        push(-DQT_USE_CCACHE=ON)
    elseif(arg STREQUAL "-developer-build")
        push(-DFEATURE_developer_build=ON)
    elseif(arg STREQUAL "-prefix")
        pop_path_argument()
        push("-DCMAKE_INSTALL_PREFIX=${path}")
    elseif(arg STREQUAL "-extprefix")
        pop_path_argument()
        push("-DCMAKE_STAGING_PREFIX=${path}")
    elseif(arg STREQUAL "-hostprefix")
        message(FATAL_ERROR "${arg} is not supported in the CMake build.")
    elseif(arg STREQUAL "-external-hostbindir")
        # This points to the bin directory of the Qt installation.
        # This can be multiple levels deep and we cannot deduce the QT_HOST_PATH safely.
        message(FATAL_ERROR "${arg} is not supported anymore. Use -qt-host-path <dir> instead.")
    elseif(arg STREQUAL "-qt-host-path")
        pop_path_argument()
        push("-DQT_HOST_PATH=${path}")
    elseif(arg MATCHES "^-host.*dir")
        message(FATAL_ERROR "${arg} is not supported anymore.")
    elseif(arg MATCHES
            "^-(bin|lib|archdata|libexec|qml|data|doc|translation|sysconf|examples|tests)dir$")
        string(TOUPPER "${CMAKE_MATCH_1}" type)
        list(POP_FRONT configure_args dir)
        push("-DINSTALL_${type}DIR=${dir}")
    elseif(arg STREQUAL "-plugindir")
        list(POP_FRONT configure_args dir)
        push("-DINSTALL_PLUGINSDIR=${dir}")
    elseif(arg STREQUAL "-release")
        set(build_configs "Release")
    elseif(arg STREQUAL "-debug")
        set(build_configs "Debug")
    elseif(arg STREQUAL "-debug-and-release")
        set(build_configs Debug Release)
    elseif(arg STREQUAL "-force-debug-info")
        set(force_debug_info ON)
    elseif(arg STREQUAL "-shared")
        push("-DBUILD_SHARED_LIBS=ON")
    elseif(arg STREQUAL "-static")
        push("-DBUILD_SHARED_LIBS=OFF")
    elseif(arg MATCHES "^-(no-)?framework")
        calculate_state()
        push("-DFEATURE_framework=${state}")
    elseif(arg MATCHES "^-x?platform$")
        list(POP_FRONT configure_args mkspec)
        push("-DQT_QMAKE_TARGET_MKSPEC=${mkspec}")
    elseif(arg STREQUAL "-device")
        list(POP_FRONT configure_args mkspec)
        push("-DQT_QMAKE_TARGET_MKSPEC=devices/${mkspec}")
    elseif(arg STREQUAL "-device-option")
        list(POP_FRONT configure_args opt)
        list(APPEND device_options "${opt}")
    elseif(arg STREQUAL "-qtnamespace")
        list(POP_FRONT configure_args namespace)
        push("-DQT_NAMESPACE=${namespace}")
    elseif(arg STREQUAL "-c++std")
        list(POP_FRONT configure_args edition)
        string(REGEX REPLACE "^c\\+\\+" "" edition "${edition}")
        push("-DCMAKE_CXX_STANDARD=${edition}")
    elseif(arg STREQUAL "-rpath")
        pop_path_argument()
        list(APPEND extra_rpaths "${path}")
    elseif(arg STREQUAL "--")
        # Everything after this argument will be passed to CMake verbatim.
        push(${configure_args})
        break()
    else()
        message(FATAL_ERROR "Unknown configure argument: ${arg}")
    endif()
endwhile()

if(force_debug_info)
    list(TRANSFORM build_configs REPLACE "^Release$" "RelWithDebInfo")
endif()

list(LENGTH build_configs nr_of_build_configs)
if(nr_of_build_configs EQUAL 1)
    push("-DCMAKE_BUILD_TYPE=${build_configs}")
elseif(nr_of_build_configs GREATER 1)
    set(multi_config ON)
    string(REPLACE ";" "\\;" escaped_build_configs "${build_configs}")
    # We must not use the push macro here to avoid variable expansion.
    # That would destroy our escaping.
    list(APPEND cmake_args "-DCMAKE_CONFIGURATION_TYPES=${escaped_build_configs}")
endif()

if(device_options)
    unset(escaped_device_options)
    list(JOIN device_options "\\;" escaped_device_options)
    list(APPEND cmake_args "-DQT_QMAKE_DEVICE_OPTIONS=${escaped_device_options}")
endif()

if(extra_rpaths)
    unset(escaped_paths)
    list(JOIN extra_rpaths "\\;" escaped_paths)
    list(APPEND cmake_args "-DQT_EXTRA_RPATHS=${escaped_paths}")
endif()

if(NOT generator AND auto_detect_generator)
    find_program(ninja ninja)
    if(ninja)
        set(generator Ninja)
        if(multi_config)
            string(APPEND generator " Multi-Config")
        endif()
    else()
        if(CMAKE_HOST_UNIX)
            set(generator "Unix Makefiles")
        elseif(CMAKE_HOST_WINDOWS)
            find_program(msvc_compiler cl.exe)
            if(msvc_compiler)
                set(generator "NMake Makefiles")
                find_program(jom jom)
                if(jom)
                    string(APPEND generator " JOM")
                endif()
            else()
                set(generator "MinGW Makefiles")
            endif()
        endif()
    endif()
endif()
if(generator)
    push(-G "${generator}")
endif()

push("${source_dir}")

execute_process(COMMAND "${CMAKE_COMMAND}" ${cmake_args}
    COMMAND_ECHO STDOUT
    RESULT_VARIABLE exit_code)
if(NOT exit_code EQUAL 0)
    message(FATAL_ERROR "CMake exited with code ${exit_code}.")
endif()