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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// 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.

use {
    crate::{
        command_line_tools_sdks_directory, AppleSdk, DeveloperDirectory, Error, Platform,
        PlatformDirectory, SdkPath, SdkVersion,
    },
    std::{
        cmp::Ordering,
        collections::HashSet,
        fmt::{Display, Formatter},
        path::PathBuf,
    },
};

/// The search location that a [SdkSearchLocation] normalizes to.
enum SdkSearchResolvedLocation {
    /// Nothing.
    None,
    /// A collection of platform directories.
    PlatformDirectories(Vec<PlatformDirectory>),
    /// A directory holding SDKs.
    SdksDirectory(PathBuf),
    /// A specific directory with an SDK.
    SdkDirectory(PathBuf),
    /// A specified directory with an SDK excluded from SDK filtering.
    SdkDirectoryUnfiltered(PathBuf),
}

impl SdkSearchResolvedLocation {
    fn apply_sdk_filter(&self) -> bool {
        !matches!(self, Self::SdkDirectoryUnfiltered(_))
    }
}

/// Represents a location to search for SDKs.
#[derive(Clone, Debug)]
pub enum SdkSearchLocation {
    /// Use the path specified by the `SDKROOT` environment variable.
    ///
    /// If this environment variable is defined and the path is not valid, an error
    /// occurs.
    ///
    /// An SDK yielded from this location skips search filtering. This is because the
    /// semantic intent of the `SDKROOT` environment variable is to force usage of a
    /// specific SDK.
    ///
    /// If this location yields an SDK, the SDK search will be aborted and subsequent
    /// locations will not be searched. This effectively honors the intent of `SDKROOT`
    /// to force usage of a given SDK.
    ///
    /// If this behavior is not desirable, construct an [SdkSearch] with a
    /// [SdkSearchLocation::Sdk] using the value of `SDKROOT`.
    SdkRootEnv,

    /// Use the Developer Directory specified by the `DEVELOPER_DIR` environment variable.
    ///
    /// If this environment variable is defined and the path is not valid, an error
    /// occurs.
    ///
    /// If this location yields an SDK, the SDK search will be aborted and subsequent
    /// locations will not be searched. This effectively honors the intent of `DEVELOPER_DIR`
    /// to explicitly define a developer directory to use for SDK searching.
    ///
    /// If this behavior is not desirable, construct an [SdkSearch] with a
    /// [SdkSearchLocation::Developer] using the value of `DEVELOPER_DIR`.
    DeveloperDirEnv,

    /// Look for SDKs within the system installed `Xcode` application.
    ///
    /// This effectively controls whether the Developer Directory resolved by
    /// [DeveloperDirectory::default_xcode()] will be searched, if available.
    SystemXcode,

    /// Look for SDKs within the system install `Xcode Command Line Tools` installation.
    ///
    /// This effectively uses the directory returned by [command_line_tools_sdks_directory()],
    /// if available.
    CommandLineTools,

    /// Invoke `xcode-select` to find a *Developer Directory* to search.
    ///
    /// This mechanism is intended as a fallback in case other (pure Rust) mechanisms for locating
    /// the default *Developer Directory* fail. If you find yourself needing this, it likely
    /// points to a gap in our feature coverage to locate the default *Developer Directory* without
    /// running external tools. Consider filing a bug against this crate to track closing the
    /// feature gap.
    XcodeSelect,

    /// Look for SDKs within all system installed `Xcode` applications.
    ///
    /// This effectively controls whether the paths resolved by
    /// [DeveloperDirectory::find_system_xcodes()] will be searched, if present.
    ///
    /// Many macOS systems only have a single Xcode application at `/Applications/Xcode.app`.
    /// However, environments like CI workers and developers having beta versions of Xcode installed
    /// may have multiple versions of Xcode available. This option can enable multiple copies
    /// of Xcode to be used.
    SystemXcodes,

    /// Use an explicit *Developer Directory*.
    ///
    /// This can be used to point a search at a non-standard location holding a *Developer
    /// Directory*. A common use case for this is when cross-compiling or using hermetic / chroot /
    /// container build environments that don't resemble a common macOS system layout and therefore
    /// prohibit use of mechanisms for locating a *Developer Directory* in default locations.
    Developer(DeveloperDirectory),

    /// Use an explicit directory holding SDKs.
    ///
    /// This is similar to [Self::Developer] with regards to its intended use cases. The difference
    /// is the path is a directory holding `*.sdk` directories, not a *Developer Directory*.
    Sdks(PathBuf),

    /// Use an explicit directory holding an SDK.
    Sdk(PathBuf),
}

impl Display for SdkSearchLocation {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::SdkRootEnv => f.write_str("SDKROOT environment variable"),
            Self::DeveloperDirEnv => f.write_str("DEVELOPER_DIR environment variable"),
            Self::SystemXcode => f.write_str("System-installed Xcode application"),
            Self::CommandLineTools => f.write_str("Xcode Command Line Tools installation"),
            Self::XcodeSelect => f.write_str("xcode-select"),
            Self::SystemXcodes => f.write_str("All system-installed Xcode applications"),
            Self::Developer(dir) => {
                f.write_fmt(format_args!("Developer Directory {}", dir.path().display()))
            }
            Self::Sdks(path) => f.write_fmt(format_args!("SDKs directory {}", path.display())),
            Self::Sdk(path) => f.write_fmt(format_args!("SDK directory {}", path.display())),
        }
    }
}

impl SdkSearchLocation {
    /// Whether this search location is terminal.
    fn is_terminal(&self) -> bool {
        matches!(self, Self::SdkRootEnv | Self::DeveloperDirEnv)
    }

    fn resolve_location(&self) -> Result<SdkSearchResolvedLocation, Error> {
        match self {
            Self::SdkRootEnv => {
                if let Some(path) = std::env::var_os("SDKROOT") {
                    let path = PathBuf::from(path);

                    if path.exists() {
                        Ok(SdkSearchResolvedLocation::SdkDirectoryUnfiltered(path))
                    } else {
                        Err(Error::PathNotSdk(path))
                    }
                } else {
                    Ok(SdkSearchResolvedLocation::None)
                }
            }
            Self::DeveloperDirEnv => {
                if let Some(dir) = DeveloperDirectory::from_env()? {
                    Ok(SdkSearchResolvedLocation::PlatformDirectories(
                        dir.platforms()?,
                    ))
                } else {
                    Ok(SdkSearchResolvedLocation::None)
                }
            }
            Self::SystemXcode => {
                if let Some(dir) = DeveloperDirectory::default_xcode() {
                    Ok(SdkSearchResolvedLocation::PlatformDirectories(
                        dir.platforms()?,
                    ))
                } else {
                    Ok(SdkSearchResolvedLocation::None)
                }
            }
            Self::CommandLineTools => {
                if let Some(path) = command_line_tools_sdks_directory() {
                    Ok(SdkSearchResolvedLocation::SdksDirectory(path))
                } else {
                    Ok(SdkSearchResolvedLocation::None)
                }
            }
            Self::XcodeSelect => Ok(SdkSearchResolvedLocation::PlatformDirectories(
                DeveloperDirectory::from_xcode_select()?.platforms()?,
            )),
            Self::SystemXcodes => Ok(SdkSearchResolvedLocation::PlatformDirectories(
                DeveloperDirectory::find_system_xcodes()?
                    .into_iter()
                    .map(|dir| dir.platforms())
                    .collect::<Result<Vec<_>, Error>>()?
                    .into_iter()
                    .flatten()
                    .collect::<Vec<_>>(),
            )),
            Self::Developer(dir) => Ok(SdkSearchResolvedLocation::PlatformDirectories(
                dir.platforms()?,
            )),
            Self::Sdks(path) => Ok(SdkSearchResolvedLocation::SdksDirectory(path.clone())),
            Self::Sdk(path) => Ok(SdkSearchResolvedLocation::SdkDirectory(path.clone())),
        }
    }
}

/// Sorting strategy to apply to SDK searches.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SdkSorting {
    /// Do not apply any sorting.
    ///
    /// This will return SDKs in the order they are discovered from the input
    /// paths.
    None,

    /// Order SDKs by their version in descending order.
    ///
    /// Newer SDKs will come before older SDKs.
    VersionDescending,

    /// Order SDKs by their version in ascending order.
    ///
    /// Older SDKs will come before newer SDKs.
    VersionAscending,
}

impl Display for SdkSorting {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Self::None => "nothing",
            Self::VersionDescending => "descending version",
            Self::VersionAscending => "ascending version",
        })
    }
}

impl SdkSorting {
    pub fn compare_version(&self, a: Option<&SdkVersion>, b: Option<&SdkVersion>) -> Ordering {
        match self {
            Self::None => Ordering::Equal,
            Self::VersionAscending => match (a, b) {
                (Some(a), Some(b)) => a.cmp(b),
                (Some(_), None) => Ordering::Greater,
                (None, Some(_)) => Ordering::Less,
                (None, None) => Ordering::Equal,
            },
            Self::VersionDescending => match (a, b) {
                (Some(a), Some(b)) => b.cmp(a),
                (Some(_), None) => Ordering::Less,
                (None, Some(_)) => Ordering::Greater,
                (None, None) => Ordering::Equal,
            },
        }
    }
}

/// Describes an event during SDK discovery.
///
/// This events are sent to the progress callback to allow monitoring and debugging
/// of SDK searching activity.
pub enum SdkSearchEvent {
    /// Beginning a search of a given location.
    SearchingLocation(SdkSearchLocation),
    PlatformDirectoryInclude(PathBuf),
    PlatformDirectoryExclude(PathBuf),
    SdkFilterSkip(SdkPath),
    SdkFilterMatch(SdkPath),
    SdkFilterExclude(SdkPath, String),
    Sorting(usize, SdkSorting),
}

impl Display for SdkSearchEvent {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::SearchingLocation(location) => {
                f.write_fmt(format_args!("searching {location}"))
            }
            Self::PlatformDirectoryInclude(path) => f.write_fmt(format_args!(
                "searching Platform directory {}",
                path.display()
            )),
            Self::PlatformDirectoryExclude(path) => f.write_fmt(format_args!(
                "excluding Platform directory {}",
                path.display()
            )),
            Self::SdkFilterSkip(sdk) => f.write_fmt(format_args!("SDK {sdk} bypasses filter")),
            Self::SdkFilterMatch(sdk) => {
                f.write_fmt(format_args!("SDK {sdk} matches search filter"))
            }
            Self::SdkFilterExclude(sdk, reason) => {
                f.write_fmt(format_args!("SDK {sdk} discarded because {reason}"))
            }
            Self::Sorting(count, sorting) => {
                f.write_fmt(format_args!("sorting {count} SDKs by {sorting}"))
            }
        }
    }
}

/// A callable that receives progress during an SDK search.
pub type SdkProgressCallback = fn(SdkSearchEvent);

/// Search parameters for locating an Apple SDK.
///
/// This type can be used to construct a search for an Apple SDK given user chosen
/// search parameters.
///
/// The search algorithm is essentially:
///
/// 1. Iterate through each registered search location.
/// 2. Discover candidate SDKs and filter.
/// 3. Globally sort (if enabled).
///
/// # Search Locations
///
/// Search mechanisms / locations are represented via [SdkSearchLocation] and internally
/// the searcher maintains a vector of locations. The default search locations are:
///
/// 1. Use path specified by `SDKROOT` environment variable, if defined.
/// 2. Find SDKs within the Developer Directory defined by the `DEVELOPER_DIR` environment
///    variable.
/// 3. Find SDKs within the system installed `Xcode` application.
/// 4. Find SDKs within the system installed Xcode Command Line Tools.
///
/// Simply call [Self::location()] to register a new location. If the default locations
/// are not desirable, construct an empty instance via [Self::empty()] and register your
/// explicit list of locations.
///
/// An attempt is made to only search a given location at most once. This is done in
/// order to avoid redundant work. If a location is specified multiple times - even via
/// different [SdkSearchLocation] variants - subsequent searches of that location will
/// yield no distinct results. Duplicate SDKs can occur in the returned list.
///
/// # Filtering
///
/// Filters can be registered to control which SDKs are emitted from the search.
///
/// By default, no filtering is performed. This means all SDKs in all search locations
/// are returned. This can return SDKs belonging to multiple platforms (e.g. macOS and iOS).
///
/// The following functions control filtering:
///
/// * [Self::platform()]
/// * [Self::minimum_version()]
/// * [Self::maximum_version()]
/// * [Self::deployment_target()]
///
/// If you are looking for an SDK to use (e.g. for compilation), you should at least use a
/// platform filter. Otherwise you may see SDKs for platforms you aren't targeting! It is
/// also an encouraged practice to specify a minimum or maximum SDK version to use.
///
/// If you know you are targeting a specific OS version, applying a targeting filter
/// via [Self::deployment_target()] is recommended. However, this filter is not always
/// reliable. See the caveats in its documentation.
///
/// # Sorting
///
/// By default, the returned list of SDKs is the chained result of SDKs discovered
/// in all registered search locations. The order of the SDK within each search
/// location is likely the sorted order of directory names as they appear on the filesystem.
///
/// If using an SDK for compilation, sorting by the SDK version is likely desired.
/// Using the latest/newest SDK that supports a given deployment target is generally
/// a best practice.
#[derive(Clone)]
pub struct SdkSearch {
    progress_callback: Option<SdkProgressCallback>,
    locations: Vec<SdkSearchLocation>,
    platform: Option<Platform>,
    minimum_version: Option<SdkVersion>,
    maximum_version: Option<SdkVersion>,
    deployment_target: Option<(String, SdkVersion)>,
    sorting: SdkSorting,
}

impl Default for SdkSearch {
    fn default() -> Self {
        Self {
            progress_callback: None,
            locations: vec![
                SdkSearchLocation::SdkRootEnv,
                SdkSearchLocation::DeveloperDirEnv,
                SdkSearchLocation::SystemXcode,
                SdkSearchLocation::CommandLineTools,
            ],
            platform: None,
            minimum_version: None,
            maximum_version: None,
            deployment_target: None,
            sorting: SdkSorting::None,
        }
    }
}

impl SdkSearch {
    /// Obtain an instance with an empty set of search locations.
    ///
    /// The search will not resolve any SDKs unless a search location is registered
    /// with the instance.
    pub fn empty() -> Self {
        let mut s = Self::default();
        s.locations.clear();
        s
    }

    /// Define a function that will be called to provide updates on SDK search status.
    pub fn progress_callback(mut self, callback: SdkProgressCallback) -> Self {
        self.progress_callback = Some(callback);
        self
    }

    /// Add a location to search.
    ///
    /// The location will be appended to the current search location list.
    pub fn location(mut self, location: SdkSearchLocation) -> Self {
        self.locations.push(location);
        self
    }

    /// Set the SDK platform to search for.
    ///
    /// If you do not call this, SDKs for all platforms are returned.
    ///
    /// If you are looking for a specific SDK to use, you probably want to call this.
    /// If you are searching for all available SDKs, you probably don't want to call this.
    pub fn platform(mut self, platform: Platform) -> Self {
        self.platform = Some(platform);
        self
    }

    /// Minimum SDK version to require.
    ///
    /// Effectively imposes a `>=` filter on found SDKs.
    ///
    /// If using `SimpleSdk` and the SDK version could not be determined from
    /// the filesystem path, the version is assumed to be `0.0` and this filter
    /// will likely exclude the SDK.
    pub fn minimum_version(mut self, version: impl Into<SdkVersion>) -> Self {
        self.minimum_version = Some(version.into());
        self
    }

    /// Maximum SDK version to return.
    ///
    /// Effectively imposes a `<=` filter on found SDKs.
    pub fn maximum_version(mut self, version: impl Into<SdkVersion>) -> Self {
        self.maximum_version = Some(version.into());
        self
    }

    /// Deployment target that the SDK must support.
    ///
    /// When set, only SDKs that support targeting the given target-version pair will
    /// be returned. Example values are (`macosx`, `10.15`).
    ///
    /// Only modern SDKs with `SDKSettings.json` files advertise their targeting settings
    /// in a way that allows this filter to work.
    ///
    /// Attempting to use this filter on `SimpleSdk` will result in a run-time
    /// error at search time since these SDKs do not parse `SDKSettings` files.
    pub fn deployment_target(
        mut self,
        target: impl ToString,
        version: impl Into<SdkVersion>,
    ) -> Self {
        self.deployment_target = Some((target.to_string(), version.into()));
        self
    }

    /// Define the sorting order for returned SDKs.
    ///
    /// Default is [SdkSorting::None].
    pub fn sorting(mut self, sorting: SdkSorting) -> Self {
        self.sorting = sorting;
        self
    }

    /// Perform a search, yielding found SDKs sorted by the search's preferences.
    ///
    /// May return an empty vector.
    pub fn search<SDK: AppleSdk>(&self) -> Result<Vec<SDK>, Error> {
        let mut sdks = vec![];

        // Track searched locations to avoid redundant work.
        let mut searched_platform_dirs = HashSet::new();
        let mut searched_sdks_dirs = HashSet::new();

        for location in &self.locations {
            if let Some(cb) = &self.progress_callback {
                cb(SdkSearchEvent::SearchingLocation(location.clone()));
            }

            // Expand each location to SDKs.
            let resolved = location.resolve_location()?;

            let candidate_sdks = match &resolved {
                SdkSearchResolvedLocation::None => {
                    vec![]
                }
                SdkSearchResolvedLocation::PlatformDirectories(dirs) => dirs
                    .iter()
                    // Apply platform filter.
                    .filter(|dir| {
                        if let Some(wanted_platform) = &self.platform {
                            if &dir.platform == wanted_platform {
                                if let Some(cb) = &self.progress_callback {
                                    cb(SdkSearchEvent::PlatformDirectoryInclude(dir.path.clone()));
                                }

                                true
                            } else {
                                if let Some(cb) = &self.progress_callback {
                                    cb(SdkSearchEvent::PlatformDirectoryExclude(dir.path.clone()));
                                }

                                false
                            }
                        } else {
                            if let Some(cb) = &self.progress_callback {
                                cb(SdkSearchEvent::PlatformDirectoryInclude(dir.path.clone()));
                            }

                            true
                        }
                    })
                    // Apply duplicate search filter.
                    .filter(|dir| {
                        if searched_platform_dirs.contains(dir.path()) {
                            false
                        } else {
                            searched_platform_dirs.insert(dir.path().to_path_buf());
                            true
                        }
                    })
                    .map(|dir| dir.find_sdks::<SDK>())
                    .collect::<Result<Vec<_>, Error>>()?
                    .into_iter()
                    .flatten()
                    .collect::<Vec<_>>(),
                SdkSearchResolvedLocation::SdksDirectory(path) => {
                    if searched_sdks_dirs.contains(path) {
                        vec![]
                    } else {
                        searched_sdks_dirs.insert(path.clone());
                        SDK::find_in_directory(path)?
                    }
                }
                SdkSearchResolvedLocation::SdkDirectory(path)
                | SdkSearchResolvedLocation::SdkDirectoryUnfiltered(path) => {
                    vec![SDK::from_directory(path)?]
                }
            };

            let mut added_count = 0;

            for sdk in candidate_sdks {
                let include = if resolved.apply_sdk_filter() {
                    self.filter_sdk(&sdk)?
                } else {
                    if let Some(cb) = &self.progress_callback {
                        cb(SdkSearchEvent::SdkFilterSkip(sdk.sdk_path()));
                    }

                    true
                };

                if include {
                    sdks.push(sdk);
                    added_count += 1;
                }
            }

            if location.is_terminal() && added_count > 0 {
                break;
            }
        }

        // Sorting should be stable with None variant. But we can avoid the
        // overhead.
        if self.sorting != SdkSorting::None {
            sdks.sort_by(|a, b| self.sorting.compare_version(a.version(), b.version()))
        }

        Ok(sdks)
    }

    /// Whether an SDK matches our search filter.
    ///
    /// This is exposed as a convenience method to allow custom implementations of
    /// SDK searching using the filtering logic on this type.
    pub fn filter_sdk<SDK: AppleSdk>(&self, sdk: &SDK) -> Result<bool, Error> {
        let sdk_path = sdk.sdk_path();

        if let Some(wanted_platform) = &self.platform {
            if sdk.platform() != wanted_platform {
                if let Some(cb) = &self.progress_callback {
                    cb(SdkSearchEvent::SdkFilterExclude(
                        sdk_path,
                        format!(
                            "platform {} != {}",
                            sdk.platform().filesystem_name(),
                            wanted_platform.filesystem_name()
                        ),
                    ));
                }

                return Ok(false);
            }
        }

        if let Some(min_version) = &self.minimum_version {
            if let Some(sdk_version) = sdk.version() {
                if sdk_version < min_version {
                    if let Some(cb) = &self.progress_callback {
                        cb(SdkSearchEvent::SdkFilterExclude(
                            sdk_path,
                            format!(
                                "SDK version {sdk_version} < minimum version {min_version}"
                            ),
                        ));
                    }

                    return Ok(false);
                }
            } else {
                // SDKs without a version always fail.
                if let Some(cb) = &self.progress_callback {
                    cb(SdkSearchEvent::SdkFilterExclude(
                        sdk_path,
                        format!(
                            "Unknown SDK version fails to meet minimum version {min_version}"
                        ),
                    ));
                }

                return Ok(false);
            }
        }

        if let Some(max_version) = &self.maximum_version {
            if let Some(sdk_version) = sdk.version() {
                if sdk_version > max_version {
                    if let Some(cb) = &self.progress_callback {
                        cb(SdkSearchEvent::SdkFilterExclude(
                            sdk_path,
                            format!(
                                "SDK version {sdk_version} > maximum version {max_version}"
                            ),
                        ));
                    }

                    return Ok(false);
                }
            } else {
                // SDKs without a version always fail.

                if let Some(cb) = &self.progress_callback {
                    cb(SdkSearchEvent::SdkFilterExclude(
                        sdk_path,
                        format!(
                            "Unknown SDK version fails to meet maximum version {max_version}"
                        ),
                    ));
                }

                return Ok(false);
            }
        }

        if let Some((target, version)) = &self.deployment_target {
            if !sdk.supports_deployment_target(target, version)? {
                if let Some(cb) = &self.progress_callback {
                    cb(SdkSearchEvent::SdkFilterExclude(
                        sdk_path,
                        format!("does not support deployment target {target}:{version}"),
                    ));
                }

                return Ok(false);
            }
        }

        if let Some(cb) = &self.progress_callback {
            cb(SdkSearchEvent::SdkFilterMatch(sdk_path));
        }

        Ok(true)
    }
}