aboutsummaryrefslogtreecommitdiffstats
path: root/share/qbs/modules/xcode/xcode.qbs
blob: aeb0760ac9e5f89d82d47daf2d18acac9e1582a3 (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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import qbs.BundleTools
import qbs.Environment
import qbs.File
import qbs.FileInfo
import qbs.DarwinTools
import qbs.ModUtils
import qbs.Probes
import qbs.PropertyList
import qbs.Utilities
import 'xcode.js' as Xcode

Module {
    id: xcodeModule

    Probes.XcodeLocationProbe {
        id: xcodeLocationProbe
        condition: !xcodeModule.developerPath
    }

    Probes.XcodeProbe {
        id: xcodeProbe
        developerPath: xcodeModule.developerPath
        platformType: xcodeModule.platformType
        platformPath: xcodeModule.platformPath
        devicePlatformPath: xcodeModule.devicePlatformPath
        xcodebuildPath: xcodeModule.xcodebuildPath
        sdksPath: xcodeModule.sdksPath
    }

    condition: qbs.targetOS.contains("darwin") &&
               qbs.toolchain && qbs.toolchain.contains("xcode")

    version: xcodeProbe.xcodeVersion

    property path developerPath: xcodeLocationProbe.found
                                 ? xcodeLocationProbe.developerPath
                                 : undefined
    property string sdk: DarwinTools.applePlatformName(qbs.targetOS, platformType)
    property stringList targetDevices: DarwinTools.targetDevices(qbs.targetOS)

    property string platformType: {
        if (qbs.targetOS.containsAny(["ios-simulator", "tvos-simulator", "watchos-simulator"]))
            return "simulator";
        if (qbs.targetOS.containsAny(["ios", "tvos", "watchos"]))
            return "device";
    }

    readonly property string sdkName: {
        if (_sdkSettings) {
            return _sdkSettings["CanonicalName"];
        }
    }

    readonly property string sdkVersion: {
        if (_sdkSettings) {
            return _sdkSettings["Version"];
        }
    }
    readonly property string shortSdkVersion: {
        var v = sdkVersion;
        if (v && v.split('.').length > 2)
            v = v.slice(0, v.lastIndexOf('.'));
        return v;
    }

    readonly property string latestSdkName: {
        if (_latestSdk) {
            return _latestSdk["CanonicalName"];
        }
    }

    readonly property string latestSdkVersion: {
        if (_latestSdk) {
            return _latestSdk["Version"];
        }
    }

    readonly property stringList availableSdkNames: {
        if (_availableSdks) {
            return _availableSdks.map(function (obj) { return obj["CanonicalName"]; });
        }
    }

    readonly property stringList availableSdkVersions: {
        if (_availableSdks) {
            return _availableSdks.map(function (obj) { return obj["Version"]; });
        }
    }

    property string signingIdentity
    readonly property string actualSigningIdentity: {
        if (_actualSigningIdentity && _actualSigningIdentity.length === 2)
            return _actualSigningIdentity[0];
    }

    readonly property string actualSigningIdentityDisplayName: {
        if (_actualSigningIdentity && _actualSigningIdentity.length === 2)
            return _actualSigningIdentity[1];
    }

    property string signingTimestamp: "none"

    property string provisioningProfile

    property string xcodebuildName: "xcodebuild"
    property string xcodebuildPath: FileInfo.joinPaths(developerPath, "usr", "bin", xcodebuildName)

    property string securityName: "security"
    property string securityPath: securityName

    property string codesignName: "codesign"
    property string codesignPath: codesignName
    property stringList codesignFlags

    readonly property path toolchainPath: FileInfo.joinPaths(toolchainsPath,
                                                             "XcodeDefault" + ".xctoolchain")
    readonly property path platformPath: FileInfo.joinPaths(platformsPath,
                                                            DarwinTools.applePlatformDirectoryName(
                                                                qbs.targetOS, platformType)
                                                            + ".platform")
    readonly property path devicePlatformPath: FileInfo.joinPaths(
                                                   platformsPath,
                                                   DarwinTools.applePlatformDirectoryName(
                                                       qbs.targetOS, "device")
                                                   + ".platform")
    readonly property path simulatorPlatformPath: FileInfo.joinPaths(
                                                      platformsPath,
                                                      DarwinTools.applePlatformDirectoryName(
                                                          qbs.targetOS, "simulator")
                                                      + ".platform")
    readonly property path sdkPath: FileInfo.joinPaths(sdksPath,
                                                       DarwinTools.applePlatformDirectoryName(
                                                           qbs.targetOS, platformType,
                                                           shortSdkVersion) + ".sdk")

    // private properties
    readonly property path toolchainsPath: FileInfo.joinPaths(developerPath, "Toolchains")
    readonly property path platformsPath: FileInfo.joinPaths(developerPath, "Platforms")
    readonly property path sdksPath: FileInfo.joinPaths(platformPath, "Developer", "SDKs")

    readonly property path platformInfoPlist: FileInfo.joinPaths(platformPath, "Info.plist")
    readonly property path sdkSettingsPlist: FileInfo.joinPaths(sdkPath, "SDKSettings.plist")
    readonly property path toolchainInfoPlist: FileInfo.joinPaths(toolchainPath,
                                                                  "ToolchainInfo.plist")

    readonly property stringList _actualSigningIdentity: {
        if (/^[A-Fa-f0-9]{40}$/.test(signingIdentity)) {
            return [signingIdentity, signingIdentity];
        }

        var result = [];

        if (signingIdentity) {
            var identities = Utilities.signingIdentities();
            for (var key in identities) {
                if (identities[key].subjectInfo.CN === signingIdentity) {
                    result.push([key, signingIdentity]);
                }
            }

            if (result.length == 0) {
                throw "Unable to find signingIdentity '" + signingIdentity + "'";
            }

            if (result.length > 1) {
                throw "Signing identity '" + signingIdentity + "' is ambiguous";
            }
        }

        return result[0];
    }

    property path provisioningProfilesPath: {
        return FileInfo.joinPaths(Environment.getEnv("HOME"), "Library/MobileDevice/Provisioning Profiles");
    }

    readonly property stringList standardArchitectures: _architectureSettings["ARCHS_STANDARD"]

    readonly property var _architectureSettings: xcodeProbe.architectureSettings

    readonly property var _availableSdks: xcodeProbe.availableSdks

    readonly property var _latestSdk: _availableSdks[_availableSdks.length - 1]

    readonly property var _sdkSettings: {
        if (_availableSdks) {
            for (var i in _availableSdks) {
                if (_availableSdks[i]["Version"] === sdk)
                    return _availableSdks[i];
                if (_availableSdks[i]["CanonicalName"] === sdk)
                    return _availableSdks[i];
            }

            // Latest SDK available for the platform
            if (DarwinTools.applePlatformName(qbs.targetOS, platformType) === sdk)
                return _latestSdk;
        }
    }

    qbs.sysroot: sdkPath

    validate: {
        if (!_availableSdks) {
            throw "There are no SDKs available for this platform in the Xcode installation.";
        }

        if (!_sdkSettings) {
            throw "There is no matching SDK available for " + sdk + ".";
        }

        var validator = new ModUtils.PropertyValidator("xcode");
        validator.setRequiredProperty("developerPath", developerPath);
        validator.setRequiredProperty("sdk", sdk);
        validator.setRequiredProperty("sdkName", sdkName);
        validator.setRequiredProperty("sdkVersion", sdkVersion);
        validator.setRequiredProperty("toolchainsPath", toolchainsPath);
        validator.setRequiredProperty("toolchainPath", toolchainPath);
        validator.setRequiredProperty("platformsPath", platformsPath);
        validator.setRequiredProperty("platformPath", platformPath);
        validator.setRequiredProperty("sdksPath", sdkPath);
        validator.setRequiredProperty("sdkPath", sdkPath);
        validator.addVersionValidator("sdkVersion", sdkVersion, 2, 3);
        validator.addCustomValidator("sdkName", sdkName, function (value) {
            return value === DarwinTools.applePlatformDirectoryName(
                        qbs.targetOS, platformType, shortSdkVersion, false).toLowerCase();
        }, "is '" + sdkName + "', but target OS is [" + qbs.targetOS.join(",")
        + "] and Xcode SDK version is '" + sdkVersion + "'");
        validator.addCustomValidator("sdk", sdk, function (value) {
            return value === sdkName || (value + shortSdkVersion) === sdkName;
        }, "is '" + sdk + "', but canonical SDK name is '" + sdkName + "'");
        validator.validate();
    }

    property var buildEnv: {
        var env = {
            "DEVELOPER_DIR": developerPath,
            "SDKROOT": sdkPath
        };

        var prefixes = [platformPath + "/Developer", toolchainPath, developerPath];
        for (var i = 0; i < prefixes.length; ++i) {
            var codesign_allocate = prefixes[i] + "/usr/bin/codesign_allocate";
            if (File.exists(codesign_allocate)) {
                env["CODESIGN_ALLOCATE"] = codesign_allocate;
                break;
            }
        }

        return env;
    }

    setupBuildEnvironment: {
        var v = new ModUtils.EnvironmentVariable("PATH", product.qbs.pathListSeparator, false);
        v.prepend(product.xcode.platformPath + "/Developer/usr/bin");
        v.prepend(product.xcode.developerPath + "/usr/bin");
        v.set();

        for (var key in product.xcode.buildEnv) {
            v = new ModUtils.EnvironmentVariable(key);
            v.value = product.xcode.buildEnv[key];
            v.set();
        }
    }

    Group {
        name: "Provisioning Profiles"
        prefix: xcode.provisioningProfilesPath + "/"
        files: ["*.mobileprovision", "*.provisionprofile"]
        fileTags: ["xcode.provisioningprofile"]
    }

    FileTagger {
        fileTags: ["xcode.entitlements"]
        patterns: ["*.entitlements"]
    }

    FileTagger {
        fileTags: ["xcode.provisioningprofile"]
        patterns: ["*.mobileprovision", "*.provisionprofile"]
    }

    Rule {
        inputs: ["xcode.provisioningprofile"]

        Artifact {
            filePath: FileInfo.joinPaths("provisioning-profiles", input.fileName + ".xml")
            fileTags: ["xcode.provisioningprofile.data"]
        }

        prepare: {
            var cmds = [];

            var cmd = new Command("openssl", ["smime", "-verify", "-noverify", "-inform", "DER",
                                  "-in", input.filePath, "-out", output.filePath]);
            cmd.silent = true;
            cmd.stderrFilterFunction = function (output) {
                return output.replace("Verification successful\n", "");
            };
            cmds.push(cmd);

            cmd = new JavaScriptCommand();
            cmd.silent = true;
            cmd.inputFilePath = input.filePath;
            cmd.outputFilePath = output.filePath;
            cmd.sourceCode = function() {
                var propertyList = new PropertyList();
                try {
                    propertyList.readFromFile(outputFilePath);
                    propertyList.readFromObject({
                        data: propertyList.toObject(),
                        fileName: FileInfo.fileName(inputFilePath),
                        filePath: inputFilePath
                    });
                    propertyList.writeToFile(outputFilePath, "xml1");
                } finally {
                    propertyList.clear();
                }
            };
            cmds.push(cmd);

            return cmds;
        }
    }

    Rule {
        multiplex: true
        inputs: ["xcode.provisioningprofile.data"]
        outputFileTags: ["xcode.provisioningprofile.main", "xcode.provisioningprofile.data.main"]

        outputArtifacts: {
            var artifacts = [];
            for (var i = 0; i < inputs["xcode.provisioningprofile.data"].length; ++i) {
                var dataFile = inputs["xcode.provisioningprofile.data"][i].filePath;
                var query = product.moduleProperty("xcode", "provisioningProfile");
                var obj = Xcode.provisioningProfilePlistContents(dataFile);
                if (obj && obj.data && (obj.data.UUID === query || obj.data.Name === query)) {
                    console.log("Using provisioning profile: " + obj.filePath);
                    artifacts.push({
                        filePath: obj.fileName,
                        fileTags: ["xcode.provisioningprofile.main"],
                        qbs: { _inputFilePath: obj.filePath }
                    });

                    artifacts.push({
                        filePath: obj.fileName + ".xml",
                        fileTags: ["xcode.provisioningprofile.data.main"],
                        qbs: { _inputFilePath: dataFile }
                    });
                }
            }
            return artifacts;
        }

        prepare: {
            var cmds = [];
            for (var tag in outputs) {
                for (var i = 0; i < outputs[tag].length; ++i) {
                    var output = outputs[tag][i];
                    var cmd = new JavaScriptCommand();
                    cmd.silent = true;
                    cmd.inputFilePath = output.qbs._inputFilePath; // there's no such prop in qbs, see QBS-754
                    cmd.outputFilePath = output.filePath;
                    cmd.sourceCode = function() {
                        File.copy(inputFilePath, outputFilePath);
                    };
                    cmds.push(cmd);
                }
            }
            return cmds;
        }
    }

    Rule {
        inputs: ["xcode.entitlements", "xcode.provisioningprofile.data.main"]

        Artifact {
            filePath: FileInfo.joinPaths(product.destinationDirectory,
                                         product.targetName + ".xcent")
            fileTags: ["xcent"]
        }

        prepare: {
            var cmd = new JavaScriptCommand();
            cmd.description = "generating entitlements";
            cmd.highlight = "codegen";
            cmd.bundleIdentifier = product.moduleProperty("bundle", "identifier");
            cmd.signingEntitlements = inputs["xcode.entitlements"]
                ? inputs["xcode.entitlements"].map(function (a) { return a.filePath; })
                : [];
            cmd.platformPath = ModUtils.moduleProperty(product, "platformPath");
            cmd.sdkPath = ModUtils.moduleProperty(product, "sdkPath");
            cmd.sourceCode = function() {
                var i;
                var provData = Xcode.provisioningProfilePlistContents(input.filePath);
                if (provData)
                    provData = provData.data;

                var aggregateEntitlements = {};

                // Start building up an aggregate entitlements plist from the files in the SDKs,
                // which contain placeholders in the same manner as Info.plist
                function entitlementsFileContents(path) {
                    return File.exists(path) ? BundleTools.infoPlistContents(path) : undefined;
                }
                var entitlementsSources = [
                    entitlementsFileContents(FileInfo.joinPaths(platformPath, "Entitlements.plist")),
                    entitlementsFileContents(FileInfo.joinPaths(sdkPath, "Entitlements.plist"))
                ];

                for (i = 0; i < signingEntitlements.length; ++i) {
                    entitlementsSources.push(entitlementsFileContents(signingEntitlements[i]));
                }

                for (i = 0; i < entitlementsSources.length; ++i) {
                    var contents = entitlementsSources[i];
                    for (var key in contents) {
                        if (contents.hasOwnProperty(key))
                            aggregateEntitlements[key] = contents[key];
                    }
                }

                contents = provData["Entitlements"];
                for (key in contents) {
                    if (contents.hasOwnProperty(key) && !aggregateEntitlements.hasOwnProperty(key))
                        aggregateEntitlements[key] = contents[key];
                }

                // Expand entitlements variables with data from the provisioning profile
                var env = {
                    "AppIdentifierPrefix": provData["ApplicationIdentifierPrefix"] + ".",
                    "CFBundleIdentifier": bundleIdentifier
                };
                DarwinTools.expandPlistEnvironmentVariables(aggregateEntitlements, env, true);

                // Anything with an undefined or otherwise empty value should be removed
                // Only JSON-formatted plists can have null values, other formats error out
                // This also follows Xcode behavior
                DarwinTools.cleanPropertyList(aggregateEntitlements);

                var plist = new PropertyList();
                try {
                    plist.readFromObject(aggregateEntitlements);
                    plist.writeToFile(outputs.xcent[0].filePath, "xml1");
                } finally {
                    plist.clear();
                }
            };
            return [cmd];
        }
    }
}