summaryrefslogtreecommitdiffstats
path: root/src/corelib/Qt6AndroidPermissionHelpers.cmake
blob: 7f851e146673079bb6a77285dff45a97a1a5b65c (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
# Copyright (C) 2025 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause

# Generates the generator expression that converts the 'target'
# QT_ANDROID_PERMISSIONS property to the specific 'type'.
#
# It's expected that each element in QT_ANDROID_PERMISSIONS list has specific
# format:
# <name>\;<permission>\\\;<extra1>\;<value>\\\;<extra2>\;<value>
#
# Synopsis
#   _qt_internal_android_convert_permissions(out_var target <JSON|XML>)
#
# Arguments
#
# `out_var`
#    The name of the variable where the resulting generator expression is
#    stored.
#
# `target`
#    The name of the target.
#
# `JSON`
#   Generate JSON array known by androiddeployqt.
#
# `XML`
#   Generate XML content compatible with AndroidManifest.xml.
function(_qt_internal_android_convert_permissions out_var target type)
    set(permissions_property "$<TARGET_PROPERTY:${target},QT_ANDROID_PERMISSIONS>")
    set(permissions_genex "$<$<BOOL:${permissions_property}>:")
    if(type STREQUAL "JSON")
        set(pref "{ \"")
        set(post "\" }")
        set(indent "\n      ")
        string(APPEND permissions_genex
            "[${indent}$<JOIN:"
                "$<JOIN:"
                    "${pref}$<JOIN:"
                        "${permissions_property},"
                        "${post}$<COMMA>${indent}${pref}"
                    ">${post},"
                    "\": \""
                ">,"
                "\"$<COMMA> \""
            ">\n    ]"
        )
    elseif(type STREQUAL "XML")
        set(pref "<uses-permission\n android:")
        set(post "' /$<ANGLE-R>\n")
        string(APPEND permissions_genex
            "$<JOIN:"
                "$<JOIN:"
                    "${pref}$<JOIN:"
                        "${permissions_property},"
                        "${post}${pref}"
                    ">${post}\n,"
                    "='"
                ">,"
                "' android:"
            ">"
        )
    else()
        message(FATAL_ERROR "Invalid type ${type}. Supported types: JSON, XML")
    endif()
    string(APPEND permissions_genex ">")

    set(${out_var} "${permissions_genex}" PARENT_SCOPE)
endfunction()

# Add the specific Android permission to the target. The permission is stored
# in the QT_ANDROID_PERMISSIONS property(the property is not a public API)
# and has the following format:
#    <name>\;<permission>\\\;<extra1>\;<value>\\\;<extra2>\;<value>
#
# Synopsis
#   _qt_internal_add_android_permission(target NAME <permission>
#       ATTRIBUTES <extra1> <value1>
#           [<extra2> <value2>]...
#   )
#
# Arguments
#
# `target`
#   The Android target.
#
# `NAME`
#   The permission name. E.g. 'android.permission.CAMERA'.
#
# `ATTRIBUTES`
#   Extra permission attribute key-value pairs.
#   See https://developer.android.com/guide/topics/manifest/uses-permission-element
#   for details.
function(_qt_internal_add_android_permission target)
    if(NOT TARGET ${target})
        message(FATAL_ERROR "Empty or invalid target for adding Android permission: (${target})")
    endif()

    cmake_parse_arguments(arg "" "NAME" "ATTRIBUTES" ${ARGN})

    if(NOT arg_NAME)
        message(FATAL_ERROR "NAME for adding Android permission cannot be empty (${target})")
    endif()

    set(permission_entry "name\;${arg_NAME}")
    if(arg_ATTRIBUTES)
        # Permission with additional attributes
        list(LENGTH arg_ATTRIBUTES attributes_len)
        math(EXPR attributes_modulus "${attributes_len} % 2")
        if(NOT (attributes_len GREATER 1 AND attributes_modulus EQUAL 0))
            message(FATAL_ERROR "Android permission: ${arg_NAME} attributes: ${arg_ATTRIBUTES}"
                                " must be name-value pairs (for example: minSdkVersion 30)")
        endif()
        # Combine name-value pairs
        set(index 0)
        while(index LESS attributes_len)
            list(GET arg_ATTRIBUTES ${index} name)
            math(EXPR index "${index} + 1")
            list(GET arg_ATTRIBUTES ${index} value)
            string(APPEND permission_entry "\\\;${name}\;${value}")
            math(EXPR index "${index} + 1")
        endwhile()
    endif()

    # Append the permission to the target's property
    set_property(TARGET ${target} APPEND PROPERTY QT_ANDROID_PERMISSIONS "${permission_entry}")
endfunction()