summaryrefslogtreecommitdiffstats
path: root/cmake/QtFeature.cmake
blob: a4d9421935f5bf24133b371b48da0d9a7c35f177 (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
function(qt_feature_module_begin)
    qt_parse_all_arguments(_arg "qt_feature_module_begin"
        "" "LIBRARY;PRIVATE_FILE;PUBLIC_FILE" "PUBLIC_DEPENDENCIES;PRIVATE_DEPENDENCIES" ${ARGN})

    if ("${_arg_LIBRARY}" STREQUAL "")
        message(FATAL_ERROR "qt_feature_begin_module needs a LIBRARY name!")
    endif()
    if ("${_arg_PUBLIC_FILE}" STREQUAL "")
        message(FATAL_ERROR "qt_feature_begin_module needs a PUBLIC_FILE name!")
    endif()
    if ("${_arg_PRIVATE_FILE}" STREQUAL "")
        message(FATAL_ERROR "qt_feature_begin_module needs a PRIVATE_FILE name!")
    endif()

    set(__QtFeature_library "${_arg_LIBRARY}" PARENT_SCOPE)
    set(__QtFeature_private_features "" PARENT_SCOPE)
    set(__QtFeature_public_features "" PARENT_SCOPE)

    set(__QtFeature_private_file "${_arg_PRIVATE_FILE}" PARENT_SCOPE)
    set(__QtFeature_public_file "${_arg_PUBLIC_FILE}" PARENT_SCOPE)

    set(__QtFeature_private_extra "" PARENT_SCOPE)
    set(__QtFeature_public_extra "" PARENT_SCOPE)

    qt_push_features_into_parent_scope(PUBLIC_FEATURES ${_arg_PUBLIC_DEPENDENCIES})
    qt_push_features_into_parent_scope(PRIVATE_FEATURES ${_arg_PRIVATE_DEPENDENCIES})
endfunction()

function(qt_feature _feature)
    set(QT_FEATURE_DEFINITION_${_feature} ${ARGN} PARENT_SCOPE)
    qt_parse_all_arguments(_arg "qt_feature"
        "PRIVATE;PUBLIC"
        "LABEL;PURPOSE;SECTION;" "AUTODETECT;CONDITION;ENABLE;DISABLE;EMIT_IF" ${ARGN})

    if("${_arg_EMIT_IF}" STREQUAL "")
        set(_arg_EMIT_IF 1)
    endif()

    if (${_arg_EMIT_IF})
        set(QT_FEATURE_${_feature} "UNSET" CACHE STRING "${_arg_LABEL}")
        set_property(CACHE QT_FEATURE_${_feature} PROPERTY STRINGS UNSET ON OFF)

        # Register feature for future use:
        if (_arg_PUBLIC)
            list(APPEND __QtFeature_public_features "${_feature}")
        endif()
        if (_arg_PRIVATE)
            list(APPEND __QtFeature_private_features "${_feature}")
        endif()
    endif()
    set(__QtFeature_public_features ${__QtFeature_public_features} PARENT_SCOPE)
    set(__QtFeature_private_features ${__QtFeature_private_features} PARENT_SCOPE)
endfunction()

function(qt_evaluate_to_boolean expressionVar)
    if(${${expressionVar}})
        set(${expressionVar} ON PARENT_SCOPE)
    else()
        set(${expressionVar} OFF PARENT_SCOPE)
    endif()
endfunction()

function(qt_evaluate_config_expression resultVar)
    set(result "")
    set(nestingLevel 0)
    set(skipNext OFF)
    set(expression "${ARGN}")
    list(LENGTH expression length)

    math(EXPR length "${length}-1")
    foreach(memberIdx RANGE ${length})
        if(${skipNext})
            set(skipNext OFF)
            continue()
        endif()

        list(GET expression ${memberIdx} member)

        if("${member}" STREQUAL "(")
            if(${nestingLevel} GREATER 0)
                list(APPEND result ${member})
            endif()
            math(EXPR nestingLevel "${nestingLevel} + 1")
            continue()
        elseif("${member}" STREQUAL ")")
            math(EXPR nestingLevel "${nestingLevel} - 1")
            if(nestingLevel LESS 0)
                break()
            endif()
            if(${nestingLevel} EQUAL 0)
                qt_evaluate_config_expression(result ${result})
            else()
                list(APPEND result ${member})
            endif()
            continue()
        elseif(${nestingLevel} GREATER 0)
            list(APPEND result ${member})
            continue()
        elseif("${member}" STREQUAL "NOT")
            list(APPEND result ${member})
            continue()
        elseif("${member}" STREQUAL "AND")
            qt_evaluate_to_boolean(result)
            if(NOT ${result})
                break()
            endif()
            set(result "")
        elseif("${member}" STREQUAL "OR")
            qt_evaluate_to_boolean(result)
            if(${result})
                break()
            endif()
            set(result "")
        elseif("${member}" STREQUAL "STREQUAL" AND memberIdx LESS ${length})
            # Unfortunately the semantics for STREQUAL in if() are broken when the
            # RHS is an empty string and the parameters to if are coming through a variable.
            # So we expect people two write the empty string with single quotes and then we
            # do the comparison manually here.
            list(LENGTH result lhsIndex)
            math(EXPR lhsIndex "${lhsIndex}-1")
            list(GET result ${lhsIndex} lhs)
            list(REMOVE_AT result ${lhsIndex})
            set(lhs "${${lhs}}")

            math(EXPR rhsIndex "${memberIdx}+1")
            set(skipNext ON)

            list(GET expression ${rhsIndex} rhs)
            # We can't pass through an empty string with double quotes through various
            # stages of substitution, so instead it is represented using single quotes
            # and resolve here.
            string(REGEX REPLACE "'(.*)'" "\\1" rhs "${rhs}")

            string(COMPARE EQUAL "${lhs}" "${rhs}" stringCompareResult)
            list(APPEND result ${stringCompareResult})
        else()
            string(FIND "${member}" "QT_FEATURE_" idx)
            if(idx EQUAL 0)
                # Remove the QT_FEATURE_ prefix
                string(SUBSTRING "${member}" 11 -1 feature)
                qt_evaluate_feature(${feature})
            endif()

            list(APPEND result ${member})
        endif()
    endforeach()

    if("${result}" STREQUAL "")
        set(result ON)
    else()
        qt_evaluate_to_boolean(result)
    endif()

    set(${resultVar} ${result} PARENT_SCOPE)
endfunction()

function(qt_evaluate_feature _feature)
    # If the feature was set explicitly by the user to be on or off, in the cache, then
    # there's nothing for us to do.
    if(NOT QT_FEATURE_${_feature} STREQUAL UNSET)
        return()
    endif()

    if(NOT DEFINED QT_FEATURE_DEFINITION_${_feature})
        message(FATAL_ERROR "Attempting to evaluate feature ${_feature} but its definition is missing. Either the feature does not exist or a dependency to the module that defines it is missing")
    endif()

    cmake_parse_arguments(_arg
        "PRIVATE;PUBLIC"
        "LABEL;PURPOSE;SECTION;" "AUTODETECT;CONDITION;ENABLE;DISABLE;EMIT_IF" ${QT_FEATURE_DEFINITION_${_feature}})

    if(DEFINED QT_FEATURE_COMPUTED_VALUE_${_feature})
        set(QT_FEATURE_${_feature} ${QT_FEATURE_COMPUTED_VALUE_${_feature}} PARENT_SCOPE)
        return()
    endif()

    if("${_arg_ENABLE}" STREQUAL "")
        set(_arg_ENABLE OFF)
    endif()

    if("${_arg_DISABLE}" STREQUAL "")
        set(_arg_DISABLE OFF)
    endif()

    if("${_arg_AUTODETECT}" STREQUAL "")
        set(_arg_AUTODETECT ON)
    endif()

    if("${_arg_CONDITION}" STREQUAL "")
        set(_arg_CONDITION ON)
    endif()

    if(${_arg_DISABLE})
        set(result OFF)
    elseif((${_arg_ENABLE}) OR (${_arg_AUTODETECT}))
        qt_evaluate_config_expression(result ${_arg_CONDITION})
    else()
        # feature not auto-detected and not explicitly enabled
        set(result OFF)
    endif()

    set(QT_FEATURE_COMPUTED_VALUE_${_feature} "${result}" CACHE INTERNAL "${_arg_LABEL}")
    set(QT_FEATURE_${_feature} "${result}" PARENT_SCOPE)
endfunction()

function(qt_feature_definition _feature _name)
    qt_parse_all_arguments(_arg "qt_feature_definition" "NEGATE" "VALUE" "" ${ARGN})

    # Generate code:
    set(_expected 1)
    if (_arg_NEGATE)
        set(_expected -1)
    endif()
    set(_msg "\n#if defined(QT_FEATURE_${_feature}) && QT_FEATURE_${_feature} == ${_expected}\n")
    if (_arg_VALUE)
        string(APPEND _msg "#  define ${_name} ${_arg_VALUE}\n")
    else()
        string(APPEND _msg "#  define ${_name}\n")
    endif()
    string(APPEND _msg "#endif\n")

    # Store for later use:
    list(FIND __QtFeature_public_features "${_feature}" _public_index)
    if (_public_index GREATER -1)
        string(APPEND __QtFeature_public_extra "${_msg}")
    endif()
    list(FIND __QtFeature_private_features "${_feature}" _private_index)
    if (_private_index GREATER -1)
        string(APPEND __QtFeature_private_extra "${_msg}")
    endif()

    set(__QtFeature_public_extra ${__QtFeature_public_extra} PARENT_SCOPE)
    set(__QtFeature_private_extra ${__QtFeature_private_extra} PARENT_SCOPE)
endfunction()

function(qt_extra_definition _name _value)
    qt_parse_all_arguments(_arg "qt_extra_definition" "PUBLIC;PRIVATE" "" "" ${ARGN})

    if (_arg_PUBLIC)
        string(APPEND __QtFeature_public_extra "\n#define ${_name} ${_value}\n")
    elseif(_arg_PRIVATE)
        string(APPEND __QtFeature_private_extra "\n#define ${_name} ${_value}\n")
    endif()

    set(__QtFeature_public_extra ${__QtFeature_public_extra} PARENT_SCOPE)
    set(__QtFeature_private_extra ${__QtFeature_private_extra} PARENT_SCOPE)
endfunction()

function(_qt_generate_feature_line _line _feature)
    set(_line "")
    if (QT_FEATURE_${_feature} STREQUAL "ON")
        set(_line "#define QT_FEATURE_${_feature} 1\n\n" PARENT_SCOPE)
    elseif(QT_FEATURE_${_feature} STREQUAL "OFF")
        set(_line "#define QT_FEATURE_${_feature} -1\n\n" PARENT_SCOPE)
    elseif(QT_FEATURE_${_feature} STREQUAL "UNSET")
        set(_line "#define QT_FEATURE_${_feature} 0\n\n" PARENT_SCOPE)
    else()
        message(FATAL_ERROR "${_feature} has unexpected value \"${QT_FEATURE_${_feature}}\"!")
    endif()
endfunction()

function(_qt_feature_write_file _file _features _extra)
    message("Generating file ${_file}.")
    set(_contents "")
    foreach(_it ${_features})
        _qt_generate_feature_line(_line "${_it}")
        string(APPEND _contents "${_line}")
    endforeach()
    string(APPEND _contents "${_extra}")

    file(GENERATE OUTPUT "${_file}" CONTENT "${_contents}")
endfunction()

function(qt_feature_module_end target)
    set(all_features ${__QtFeature_public_features} ${__QtFeature_private_features})
    list(REMOVE_DUPLICATES all_features)

    foreach(feature ${all_features})
        unset(QT_FEATURE_COMPUTED_VALUE_${feature} CACHE)
    endforeach()

    foreach(feature ${all_features})
        qt_evaluate_feature(${feature})

        set(QT_FEATURE_${feature} ${QT_FEATURE_${feature}} PARENT_SCOPE)
    endforeach()

    set(enabled_public_features "")
    set(disabled_public_features "")
    set(enabled_private_features "")
    set(disabled_private_features "")

    foreach(feature ${__QtFeature_public_features})
       if(QT_FEATURE_${feature})
          list(APPEND enabled_public_features ${feature})
       else()
          list(APPEND disabled_public_features ${feature})
       endif()
    endforeach()

    foreach(feature ${__QtFeature_private_features})
       if(QT_FEATURE_${feature})
          list(APPEND enabled_private_features ${feature})
       else()
          list(APPEND disabled_private_features ${feature})
       endif()
    endforeach()

    foreach(feature ${all_features})
        unset(QT_FEATURE_COMPUTED_VALUE_${feature} CACHE)
        unset(QT_FEATURE_DEFINITION_${feature} CACHE)
    endforeach()

    _qt_feature_write_file("${CMAKE_CURRENT_BINARY_DIR}/${__QtFeature_private_file}"
        "${__QtFeature_private_features}" "${__QtFeature_private_extra}"
    )
    qt_generate_forwarding_headers("${__QtFeature_library}" SOURCE "${__QtFeature_private_file}" PRIVATE)

    _qt_feature_write_file("${CMAKE_CURRENT_BINARY_DIR}/${__QtFeature_public_file}"
        "${__QtFeature_public_features}" "${__QtFeature_public_extra}"
    )
    qt_generate_forwarding_headers("${__QtFeature_library}" SOURCE "${__QtFeature_public_file}")

    get_target_property(targetType "${target}" TYPE)
    if("${targetType}" STREQUAL "INTERFACE_LIBRARY")
        set(propertyPrefix "INTERFACE_")
    else()
        set(propertyPrefix "")
        set_target_properties("${target}" PROPERTIES EXPORT_PROPERTIES "QT_ENABLED_PUBLIC_FEATURES;QT_DISABLED_PUBLIC_FEATURES;QT_ENABLED_PRIVATE_FEATURES;QT_DISABLED_PRIVATE_FEATURES")
    endif()
    foreach(visibility public private)
        string(TOUPPER "${visibility}" capitalVisibility)
        foreach(state enabled disabled)
            string(TOUPPER "${state}" capitalState)

            set_property(TARGET "${target}" PROPERTY ${propertyPrefix}QT_${capitalState}_${capitalVisibility}_FEATURES "${${state}_${visibility}_features}")
        endforeach()
    endforeach()

    unset(__QtFeature_library PARENT_SCOPE)
    unset(__QtFeature_private_features PARENT_SCOPE)
    unset(__QtFeature_public_features PARENT_SCOPE)

    unset(__QtFeature_private_file PARENT_SCOPE)
    unset(__QtFeature_public_file PARENT_SCOPE)

    unset(__QtFeature_private_extra PARENT_SCOPE)
    unset(__QtFeature_public_extra PARENT_SCOPE)
endfunction()

function(qt_config_compile_test name)
    cmake_parse_arguments(_arg "" "LABEL" "" ${ARGN})

    check_cxx_source_compiles("${_arg_UNPARSED_ARGUMENTS}" HAVE_${name})
    set(TEST_${name} "${HAVE_${name}}" CACHE INTERNAL "${_arg_LABEL}")
endfunction()

function(qt_config_compile_test_x86simd extension label)
    string(TOUPPER ${extension} extension_uppercase)
    try_compile(TEST_X86SIMD_${extension} "${CMAKE_CURRENT_BINARY_DIR}"
        "${CMAKE_CURRENT_SOURCE_DIR}/config.tests/x86_simd/main.cpp"
        COMPILE_DEFINITIONS -DQT_COMPILER_SUPPORTS_${extension_uppercase}
        OUTPUT_VARIABLE foo
    )
    set(TEST_subarch_${extension} "${TEST_X86SIMD_${extension}}" CACHE INTERNAL "${label}" )
endfunction()

function(qt_pull_features_into_current_scope)
    cmake_parse_arguments(__arg "PUBLIC_FEATURES;PRIVATE_FEATURES" "FEATURE_PROPERTY_INFIX" "" ${ARGN})
    foreach(__target IN ITEMS ${__arg_UNPARSED_ARGUMENTS})
        if(NOT TARGET ${__target})
            continue()
        endif()
        get_target_property(__target_type "${__target}" TYPE)
        if("${__target_type}" STREQUAL "INTERFACE_LIBRARY")
            set(__property_prefix "INTERFACE_")
        else()
            set(__property_prefix "")
        endif()
        foreach(__visibility PUBLIC PRIVATE)
            set(__value ON)
            foreach(__state ENABLED DISABLED)
                if(NOT ${__arg_${__visibility}_FEATURES})
                    continue()
                endif()
                get_target_property(__features "${__target}" ${__property_prefix}QT_${__arg_FEATURE_PROPERTY_INFIX}${__state}_${__visibility}_FEATURES)
                if("${__features}" STREQUAL "__features-NOTFOUND")
                    continue()
                endif()
                foreach(__feature ${__features})
                    set(QT_FEATURE_${__feature} ${__value} PARENT_SCOPE)
                endforeach()
                set(__value OFF)
            endforeach()
        endforeach()
    endforeach()
endfunction()

macro(qt_push_features_into_parent_scope)
    cmake_parse_arguments(__arg "PUBLIC_FEATURES;PRIVATE_FEATURES" "FEATURE_PROPERTY_INFIX" "" ${ARGN})
    foreach(__target IN ITEMS ${__arg_UNPARSED_ARGUMENTS})
        if(NOT TARGET ${__target})
            continue()
        endif()
        get_target_property(__target_type "${__target}" TYPE)
        if("${__target_type}" STREQUAL "INTERFACE_LIBRARY")
            set(__property_prefix "INTERFACE_")
        else()
            set(__property_prefix "")
        endif()
        foreach(__visibility PUBLIC PRIVATE)
            set(__value ON)
            foreach(__state ENABLED DISABLED)
                if(NOT ${__arg_${__visibility}_FEATURES})
                    continue()
                endif()
                get_target_property(__features "${__target}" ${__property_prefix}QT_${__arg_FEATURE_PROPERTY_INFIX}${__state}_${__visibility}_FEATURES)
                if("${__features}" STREQUAL "__features-NOTFOUND")
                    continue()
                endif()
                foreach(__feature ${__features})
                    set(QT_FEATURE_${__feature} ${__value} PARENT_SCOPE)
                endforeach()
                set(__value OFF)
            endforeach()
        endforeach()
    endforeach()
endmacro()

macro(qt_load_global_features)
    if(NOT TARGET Qt::Core)
       find_package(Qt${PROJECT_VERSION_MAJOR}Core QUIET)
    endif()
    qt_pull_features_into_current_scope(PUBLIC_FEATURES PRIVATE_FEATURES FEATURE_PROPERTY_INFIX "GLOBAL_" Qt::Core)
endmacro()