summaryrefslogtreecommitdiff
path: root/xmake/rules/xcode/info_plist/xmake.lua
blob: 4743995f7fdecb09fa6c270cd284e1cb9ffaef2d (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
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author      ruki
-- @file        xmake.lua
--

rule("xcode.info_plist")
    set_extensions(".plist")
    on_build_file(function (target, sourcefile, opt)

        -- imports
        import("core.base.option")
        import("core.theme.theme")
        import("core.project.depend")
        import("core.tool.toolchain")
        import("utils.progress")

        -- check, e.g. Info.plist, xxx-Info.plist
        assert(path.filename(sourcefile):endswith("Info.plist"), "we only support Info.plist file!")

        -- get contents and resources directory
        local contentsdir = assert(target:data("xcode.bundle.contentsdir"), "contents directory not found!")
        local resourcesdir = assert(target:data("xcode.bundle.resourcesdir"), "resources directory not found!")

        -- need re-compile it?
        local dependfile = target:dependfile(sourcefile)
        local dependinfo = target:is_rebuilt() and {} or (depend.load(dependfile) or {})
        if not depend.is_changed(dependinfo, {lastmtime = os.mtime(dependfile)}) then
            return
        end

        -- trace progress info
        progress.show(opt.progress, "${color.build.object}processing.xcode.$(mode) %s", sourcefile)

        -- process and generate Info.plist
        -- https://github.com/xmake-io/xmake/issues/2765#issuecomment-1251738622
        local info_plist_file
        if target:rule("xcode.framework") and target:is_plat("macosx") then
            info_plist_file = path.join(resourcesdir, path.filename(sourcefile))
        else
            info_plist_file = path.join(contentsdir, path.filename(sourcefile))
        end
        local maps =
        {
            DEVELOPMENT_LANGUAGE = "en",
            EXECUTABLE_NAME = target:basename(),
            PRODUCT_BUNDLE_IDENTIFIER = target:values("xcode.bundle_identifier") or get_config("xcode_bundle_identifier") or "io.xmake." .. target:name(),
            PRODUCT_NAME = target:name(),
            PRODUCT_DISPLAY_NAME = target:name(),
            CURRENT_PROJECT_VERSION = target:version() and tostring(target:version()) or "1.0",
        }
        if target:is_plat("macosx") then
            local toolchain_xcode = toolchain.load("xcode", {plat = target:plat(), arch = target:arch()})
            if toolchain_xcode then
                maps.MACOSX_DEPLOYMENT_TARGET = toolchain_xcode:config("target_minver")
            end
        end
        if target:rule("xcode.bundle") then
            maps.PRODUCT_BUNDLE_PACKAGE_TYPE = "BNDL"
        elseif target:rule("xcode.framework") then
            maps.PRODUCT_BUNDLE_PACKAGE_TYPE = "FMWK"
        elseif target:rule("xcode.application") then
            maps.PRODUCT_BUNDLE_PACKAGE_TYPE = "APPL"
        end

        os.vcp(sourcefile, info_plist_file)
        io.gsub(info_plist_file, "(%$%((.-)%))", function (_, variable)
            return maps[variable]
        end)

        -- patch some entries for mac catalyst
        local xcode = target:toolchain("xcode")
        if xcode and xcode:config("appledev") == "catalyst" then
            -- remove entry for "LSRequiresIPhoneOS" - not supported on macOS
            --
            -- <key>LSRequiresIPhoneOS</key>
            -- <true/>
            io.replace(info_plist_file, "<key>LSRequiresIPhoneOS</key>.-<true/>", "")
        end

        -- update files and values to the dependent file
        dependinfo.files = {sourcefile}
        depend.save(dependinfo, dependfile)
    end)