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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// Copyright 2022 Gregory Szorc.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Data structures in Apple SDKs.

use {
    crate::{AppleSdk, Error, Platform, SdkPath, SdkVersion, SimpleSdk},
    serde::Deserialize,
    std::{
        collections::HashMap,
        path::{Path, PathBuf},
    },
};

/// Represents the DefaultProperties key in a SDKSettings.json file.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub struct SdkSettingsJsonDefaultProperties {
    pub platform_name: String,
}

/// Represents a SupportedTargets value in a SDKSettings.json file.
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct SupportedTarget {
    /// Names of machine architectures that can be targeted.
    ///
    /// e.g. `x86_64`, `arm64`, `arm64e`.
    pub archs: Vec<String>,

    /// Default deployment target version.
    ///
    /// Likely corresponds to the OS version this SDK is associated with.
    /// e.g. the macOS 12.3 SDK would target `12.3` by default.
    pub default_deployment_target: String,

    /// The name of the settings variant to use by default.
    pub default_variant: Option<String>,

    /// The name of the toolchain setting that influences which deployment target version is used.
    ///
    /// e.g. on macOS this will be `MACOSX_DEPLOYMENT_TARGET`. This represents an
    /// environment variable that can be set to influence which deployment target
    /// version to use.
    pub deployment_target_setting_name: Option<String>,

    /// The lowest version of a platform that this SDK can target.
    ///
    /// Using this SDK, it is possible to emit code that will support running
    /// down to the OS version specified by this value. e.g. `10.9` is a
    /// common value for macOS SDKs.
    pub minimum_deployment_target: String,

    /// A name given to the platform.
    ///
    /// e.g. `macOS`.
    pub platform_family_name: Option<String>,

    /// List of platform versions that this SDK can target.
    ///
    /// This is likely a range of all major versions between `minimum_deployment_target`
    /// and `default_deployment_target`.
    pub valid_deployment_targets: Vec<String>,
}

impl SupportedTarget {
    /// Obtain [SdkVersion] for each deployment target this target supports.
    pub fn deployment_targets_versions(&self) -> Vec<SdkVersion> {
        self.valid_deployment_targets
            .iter()
            .map(SdkVersion::from)
            .collect::<Vec<_>>()
    }
}

/// Used for deserializing a SDKSettings.json file in an SDK directory.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct SdkSettingsJson {
    pub canonical_name: String,
    pub default_deployment_target: String,
    pub default_properties: SdkSettingsJsonDefaultProperties,
    pub default_variant: Option<String>,
    pub display_name: String,
    pub maximum_deployment_target: String,
    pub minimal_display_name: String,
    pub supported_targets: HashMap<String, SupportedTarget>,
    pub version: String,
}

/// An Apple SDK with parsed settings.
///
/// Unlike [SimpleSdk], this type gives you access to rich metadata about the
/// Apple SDK. This includes things like targeting capabilities.
#[derive(Clone, Debug)]
pub struct ParsedSdk {
    /// Root directory of the SDK.
    path: PathBuf,

    /// Whether the root directory is a symlink to another path.
    is_symlink: bool,

    /// The platform this SDK belongs to.
    platform: Platform,

    version: SdkVersion,

    /// The name of the platform.
    ///
    /// This is likely the part before the `*.platform` in the platform directory in which
    /// this SDK is located. e.g. `macosx`.
    pub platform_name: String,

    /// The canonical name of the SDK. e.g. `macosx12.3`.
    pub name: String,

    /// Version of the default deployment target for this SDK.
    ///
    /// This is likely the OS version the SDK came from. e.g. `12.3`.
    pub default_deployment_target: String,

    /// Name of default settings variant for this SDK.
    ///
    /// Some SDKs have named variants defining targeting settings. This field holds
    /// the name of the default variant.
    ///
    /// For example, macOS SDKs have a `macos` variant for targeting macOS and an
    /// `iosmac` variant for targeting iOS running on macOS.
    pub default_variant: Option<String>,

    /// Human friendly name of this SDK.
    ///
    /// e.g. `macOS 12.3`.
    pub display_name: String,

    /// Maximum deployment target version this SDK supports.
    ///
    /// This is a very string denoting the maximum version this SDK can target.
    /// e.g. a `12.3` would list `12.3.99`.
    pub maximum_deployment_target: String,

    /// Human friendly value for name (probably just version string).
    ///
    /// A shortened display name. e.g. `12.3`.
    pub minimal_display_name: String,

    /// Describes named target configurations this SDK supports.
    ///
    /// SDKs can have multiple named targets defining pre-canned default targeting
    /// settings. This field holds these data structures.
    ///
    /// Example keys are `macosx` and `iosmac`. Use the [Self::default_variant]
    /// field to access the default target.
    pub supported_targets: HashMap<String, SupportedTarget>,
}

impl AsRef<Path> for ParsedSdk {
    fn as_ref(&self) -> &Path {
        &self.path
    }
}

impl AppleSdk for ParsedSdk {
    fn from_directory(path: &Path) -> Result<Self, Error> {
        let sdk = SdkPath::from_path(path)?;

        // Need to call symlink_metadata so symlinks aren't followed.
        let metadata = std::fs::symlink_metadata(path)?;

        let is_symlink = metadata.file_type().is_symlink();

        let json_path = path.join("SDKSettings.json");
        let plist_path = path.join("SDKSettings.plist");

        if json_path.exists() {
            let fh = std::fs::File::open(&json_path)?;
            let value: SdkSettingsJson = serde_json::from_reader(fh)?;

            Self::from_json(path.to_path_buf(), is_symlink, sdk.platform, value)
        } else if plist_path.exists() {
            let value = plist::Value::from_file(&plist_path)?;

            Self::from_plist(path.to_path_buf(), is_symlink, sdk.platform, value)
        } else {
            Err(Error::PathNotSdk(path.to_path_buf()))
        }
    }

    fn is_symlink(&self) -> bool {
        self.is_symlink
    }

    fn platform(&self) -> &Platform {
        &self.platform
    }

    fn version(&self) -> Option<&SdkVersion> {
        Some(&self.version)
    }

    /// Whether this SDK supports the given deployment target.
    ///
    /// This API does not work reliably on SDKs loaded from plists because the plist metadata
    /// lacks the required version constraint annotations.
    fn supports_deployment_target(
        &self,
        target_name: &str,
        target_version: &SdkVersion,
    ) -> Result<bool, Error> {
        Ok(
            if let Some(target) = self.supported_targets.get(target_name) {
                target
                    .deployment_targets_versions()
                    .contains(target_version)
            } else {
                false
            },
        )
    }
}

impl ParsedSdk {
    /// Construct an instance by parsing an `SDKSettings.json` file in a directory.
    ///
    /// These files are only available in more modern SDKs. For macOS, that's 10.14+.
    /// For more reliably SDK construction, use [Self::from_plist()].
    pub fn from_json(
        path: PathBuf,
        is_symlink: bool,
        platform: Platform,
        value: SdkSettingsJson,
    ) -> Result<Self, Error> {
        Ok(Self {
            path,
            is_symlink,
            platform,
            version: value.version.into(),
            platform_name: value.default_properties.platform_name,
            name: value.canonical_name,
            default_deployment_target: value.default_deployment_target,
            default_variant: value.default_variant,
            display_name: value.display_name,
            maximum_deployment_target: value.maximum_deployment_target,
            minimal_display_name: value.minimal_display_name,
            supported_targets: value.supported_targets,
        })
    }

    /// Construct an instance by parsing an `SDKSettings.plist` file in a directory.
    ///
    /// Plist files are the legacy mechanism for defining SDK settings. JSON files
    /// are preferred, as they are newer. However, older SDKs lack `SDKSettings.json`
    /// files.
    pub fn from_plist(
        path: PathBuf,
        is_symlink: bool,
        platform: Platform,
        value: plist::Value,
    ) -> Result<Self, Error> {
        let value = value.into_dictionary().ok_or(Error::PlistNotDictionary)?;

        let get_string = |dict: &plist::Dictionary, key: &str| -> Result<String, Error> {
            Ok(dict
                .get(key)
                .ok_or_else(|| Error::PlistKeyMissing(key.to_string()))?
                .as_string()
                .ok_or_else(|| Error::PlistKeyNotString(key.to_string()))?
                .to_string())
        };

        let name = get_string(&value, "CanonicalName")?;
        let display_name = get_string(&value, "DisplayName")?;
        let maximum_deployment_target = get_string(&value, "MaximumDeploymentTarget")?;
        let minimal_display_name = get_string(&value, "MinimalDisplayName")?;
        let version = get_string(&value, "Version")?;

        let props = value
            .get("DefaultProperties")
            .ok_or_else(|| Error::PlistKeyMissing("DefaultProperties".to_string()))?
            .as_dictionary()
            .ok_or_else(|| Error::PlistKeyNotDictionary("DefaultProperties".to_string()))?;

        let platform_name = get_string(props, "PLATFORM_NAME")?;

        // The default deployment target can be specified a number of ways.
        //
        // Some SDKs have a property specifying the property defining it. That takes precedence, as
        // explicit > implicit.
        //
        // Otherwise we have to fall back to a heuristic.
        //
        // First we try {platform_name}_DEPLOYMENT_TARGET. Then LLVM target triple + _DEPLOYMENT_TARGET.
        // This heuristic appears to always work.
        let default_deployment_target =
            if let Ok(setting_name) = get_string(props, "DEPLOYMENT_TARGET_SETTING_NAME") {
                get_string(props, &setting_name)?
            } else if let Ok(value) = get_string(
                props,
                &format!("{}_DEPLOYMENT_TARGET", platform_name.to_ascii_uppercase()),
            ) {
                value
            } else {
                let supported_targets = value
                    .get("SupportedTargets")
                    .ok_or_else(|| Error::PlistKeyMissing("SupportedTargets".to_string()))?
                    .as_dictionary()
                    .ok_or_else(|| Error::PlistKeyNotDictionary("SupportedTargets".to_string()))?;

                let default_target = supported_targets
                    .get(&platform_name)
                    .ok_or_else(|| Error::PlistKeyMissing(platform_name.clone()))?
                    .as_dictionary()
                    .ok_or_else(|| Error::PlistKeyNotDictionary(platform_name.clone()))?;

                let llvm_target_triple = get_string(default_target, "LLVMTargetTripleSys")?;

                get_string(
                    props,
                    &format!(
                        "{}_DEPLOYMENT_TARGET",
                        llvm_target_triple.to_ascii_uppercase()
                    ),
                )?
            };

        Ok(Self {
            path,
            is_symlink,
            platform,
            version: version.into(),
            platform_name,
            name,
            default_deployment_target,
            default_variant: None,
            display_name,
            maximum_deployment_target,
            minimal_display_name,
            supported_targets: HashMap::new(),
        })
    }
}

impl TryFrom<SimpleSdk> for ParsedSdk {
    type Error = Error;

    fn try_from(v: SimpleSdk) -> Result<Self, Self::Error> {
        Self::from_directory(v.path())
    }
}

#[cfg(test)]
mod test {
    use {
        super::*,
        crate::{
            DeveloperDirectory, SdkSearch, SdkSearchLocation, COMMAND_LINE_TOOLS_DEFAULT_PATH,
        },
    };

    const MACOSX_10_9_SETTINGS_PLIST: &[u8] = include_bytes!("testfiles/macosx10.9-settings.plist");
    const MACOSX_10_10_SETTINGS_PLIST: &[u8] =
        include_bytes!("testfiles/macosx10.10-settings.plist");
    const MACOSX_10_15_SETTINGS_JSON: &[u8] = include_bytes!("testfiles/macosx10.15-settings.json");
    const MACOSX_11_3_SETTINGS_JSON: &[u8] = include_bytes!("testfiles/macosx11.3-settings.json");

    fn macosx_10_9() -> Result<ParsedSdk, Error> {
        let value = plist::Value::from_reader(std::io::Cursor::new(MACOSX_10_9_SETTINGS_PLIST))?;

        ParsedSdk::from_plist(
            PathBuf::from("MacOSX10.9.sdk"),
            false,
            Platform::MacOsX,
            value,
        )
    }

    fn macosx_10_10() -> Result<ParsedSdk, Error> {
        let value = plist::Value::from_reader(std::io::Cursor::new(MACOSX_10_10_SETTINGS_PLIST))?;

        ParsedSdk::from_plist(
            PathBuf::from("MacOSX10.10.sdk"),
            false,
            Platform::MacOsX,
            value,
        )
    }

    fn macosx_10_15() -> Result<ParsedSdk, Error> {
        let value = serde_json::from_slice::<SdkSettingsJson>(MACOSX_10_15_SETTINGS_JSON)?;

        ParsedSdk::from_json(
            PathBuf::from("MacOSX10.15.sdk"),
            false,
            Platform::MacOsX,
            value,
        )
    }

    fn macosx_11_3() -> Result<ParsedSdk, Error> {
        let value = serde_json::from_slice::<SdkSettingsJson>(MACOSX_11_3_SETTINGS_JSON)?;

        ParsedSdk::from_json(
            PathBuf::from("MacOSX11.3.sdk"),
            false,
            Platform::MacOsX,
            value,
        )
    }

    fn all_test_sdks() -> Result<Vec<ParsedSdk>, Error> {
        Ok(vec![
            macosx_10_9()?,
            macosx_10_10()?,
            macosx_10_15()?,
            macosx_11_3()?,
        ])
    }

    #[test]
    fn find_default_sdks() -> Result<(), Error> {
        if let Ok(developer_dir) = DeveloperDirectory::find_default_required() {
            assert!(!developer_dir.sdks::<ParsedSdk>()?.is_empty());
        }

        Ok(())
    }

    #[test]
    fn find_command_line_tools_sdks() -> Result<(), Error> {
        let sdk_path = PathBuf::from(COMMAND_LINE_TOOLS_DEFAULT_PATH).join("SDKs");

        let res = ParsedSdk::find_command_line_tools_sdks()?;

        if sdk_path.exists() {
            assert!(res.is_some());
            assert!(!res.unwrap().is_empty());
        } else {
            assert!(res.is_none());
        }

        Ok(())
    }

    #[test]
    fn find_all_sdks() -> Result<(), Error> {
        for dir in DeveloperDirectory::find_system_xcodes()? {
            for sdk in dir.sdks::<ParsedSdk>()? {
                assert!(!matches!(sdk.platform(), Platform::Unknown(_)));
                assert!(sdk.version().is_some());
            }
        }

        SdkSearch::default()
            .location(SdkSearchLocation::SystemXcodes)
            .search::<ParsedSdk>()?;

        Ok(())
    }

    #[test]
    fn parse_test_sdks() -> Result<(), Error> {
        all_test_sdks()?;

        Ok(())
    }

    #[test]
    fn supports_deployment_target() -> Result<(), Error> {
        let sdk = macosx_10_15()?;

        assert!(!sdk.supports_deployment_target("ios", &SdkVersion::from("55.0"))?);
        assert!(!sdk.supports_deployment_target("macosx", &SdkVersion::from("10.5"))?);
        assert!(!sdk.supports_deployment_target("macosx", &SdkVersion::from("10.16"))?);
        assert!(!sdk.supports_deployment_target("macosx", &SdkVersion::from("11.0"))?);

        let mut versions = vec!["10.9", "10.10", "10.11", "10.12", "10.13", "10.14", "10.15"];

        for version in &versions {
            assert!(sdk.supports_deployment_target("macosx", &SdkVersion::from(*version))?);
        }

        let sdk = macosx_11_3()?;
        versions.extend(["11.0", "11.1", "11.2", "11.3"]);

        for version in &versions {
            assert!(sdk.supports_deployment_target("macosx", &SdkVersion::from(*version))?);
        }

        // API doesn't work for plists.
        assert!(!macosx_10_9()?.supports_deployment_target("macosx", &SdkVersion::from("10.9"))?);
        assert!(!macosx_10_10()?.supports_deployment_target("macosx", &SdkVersion::from("10.9"))?);

        Ok(())
    }
}