Skip to content

Commit 2141db4

Browse files
committed
Address review comments
1 parent 335e310 commit 2141db4

File tree

1 file changed

+55
-38
lines changed

1 file changed

+55
-38
lines changed

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,8 @@ static std::string getSystemOrSDKMacOSVersion(StringRef MacOSSDKVersion) {
16811681

16821682
namespace {
16831683

1684-
/// The Darwin OS that was selected or inferred from arguments / environment.
1684+
/// The Darwin OS and version that was selected or inferred from arguments or
1685+
/// environment.
16851686
struct DarwinPlatform {
16861687
enum SourceKind {
16871688
/// The OS was specified using the -target argument.
@@ -1709,16 +1710,27 @@ struct DarwinPlatform {
17091710
Environment = Kind;
17101711
InferSimulatorFromArch = false;
17111712
}
1712-
const VersionTuple &getOSVersion() const { return ResolvedOSVersion; }
1713+
1714+
const VersionTuple getOSVersion() const {
1715+
return UnderlyingOSVersion.value_or(VersionTuple());
1716+
}
1717+
1718+
VersionTuple takeOSVersion() {
1719+
assert(UnderlyingOSVersion.has_value() &&
1720+
"attempting to get an unset OS version");
1721+
VersionTuple Result = *UnderlyingOSVersion;
1722+
UnderlyingOSVersion.reset();
1723+
return Result;
1724+
}
17131725

17141726
VersionTuple getCanonicalOSVersion() const {
17151727
return llvm::Triple::getCanonicalVersionForOS(getOSFromPlatform(Platform),
1716-
ResolvedOSVersion);
1728+
getOSVersion());
17171729
}
17181730

1719-
void setOSVersion(VersionTuple Version) { ResolvedOSVersion = Version; }
1731+
void setOSVersion(VersionTuple Version) { UnderlyingOSVersion = Version; }
17201732

1721-
bool providedOSVersion() const { return ProvidedOSVersion; }
1733+
bool hasOSVersion() const { return UnderlyingOSVersion.has_value(); }
17221734

17231735
VersionTuple getZipperedOSVersion() const {
17241736
assert(Environment == DarwinEnvironmentKind::MacCatalyst &&
@@ -1738,7 +1750,8 @@ struct DarwinPlatform {
17381750

17391751
/// Adds the -m<os>-version-min argument to the compiler invocation.
17401752
void addOSVersionMinArgument(DerivedArgList &Args, const OptTable &Opts) {
1741-
if (Argument)
1753+
auto [Arg, OSVersionStr] = Arguments;
1754+
if (Arg)
17421755
return;
17431756
assert(Kind != TargetArg && Kind != MTargetOSArg && Kind != OSVersionArg &&
17441757
"Invalid kind");
@@ -1763,25 +1776,24 @@ struct DarwinPlatform {
17631776
// DriverKit always explicitly provides a version in the triple.
17641777
return;
17651778
}
1766-
Argument = Args.MakeJoinedArg(nullptr, Opts.getOption(Opt),
1767-
ResolvedOSVersion.getAsString());
1768-
Args.append(Argument);
1779+
Arg = Args.MakeJoinedArg(nullptr, Opts.getOption(Opt), OSVersionStr);
1780+
Args.append(Arg);
17691781
}
17701782

17711783
/// Returns the OS version with the argument / environment variable that
17721784
/// specified it.
17731785
std::string getAsString(DerivedArgList &Args, const OptTable &Opts) {
1786+
auto [Arg, OSVersionStr] = Arguments;
17741787
switch (Kind) {
17751788
case TargetArg:
17761789
case MTargetOSArg:
17771790
case OSVersionArg:
17781791
case InferredFromSDK:
17791792
case InferredFromArch:
1780-
assert(Argument && "OS version argument not yet inferred");
1781-
return Argument->getAsString(Args);
1793+
assert(Arg && "OS version argument not yet inferred");
1794+
return Arg->getAsString(Args);
17821795
case DeploymentTargetEnv:
1783-
return (llvm::Twine(EnvVarName) + "=" + ResolvedOSVersion.getAsString())
1784-
.str();
1796+
return (llvm::Twine(EnvVarName) + "=" + OSVersionStr).str();
17851797
}
17861798
llvm_unreachable("Unsupported Darwin Source Kind");
17871799
}
@@ -1797,7 +1809,7 @@ struct DarwinPlatform {
17971809
Environment = DarwinEnvironmentKind::MacCatalyst;
17981810
// The minimum native macOS target for MacCatalyst is macOS 10.15.
17991811
ZipperedOSVersion = VersionTuple(10, 15);
1800-
if (providedOSVersion() && SDKInfo) {
1812+
if (hasOSVersion() && SDKInfo) {
18011813
if (const auto *MacCatalystToMacOSMapping = SDKInfo->getVersionMapping(
18021814
DarwinSDKInfo::OSEnvPair::macCatalystToMacOSPair())) {
18031815
if (auto MacOSVersion = MacCatalystToMacOSMapping->map(
@@ -1879,19 +1891,23 @@ struct DarwinPlatform {
18791891
/// the platform from the SDKPath.
18801892
DarwinSDKInfo inferSDKInfo() {
18811893
assert(Kind == InferredFromSDK && "can infer SDK info only");
1882-
return DarwinSDKInfo(ResolvedOSVersion,
1894+
return DarwinSDKInfo(getOSVersion(),
18831895
/*MaximumDeploymentTarget=*/
1884-
VersionTuple(ResolvedOSVersion.getMajor(), 0, 99),
1896+
VersionTuple(getOSVersion().getMajor(), 0, 99),
18851897
getOSFromPlatform(Platform));
18861898
}
18871899

18881900
private:
18891901
DarwinPlatform(SourceKind Kind, DarwinPlatformKind Platform, Arg *Argument)
1890-
: Kind(Kind), Platform(Platform), Argument(Argument) {}
1902+
: Kind(Kind), Platform(Platform),
1903+
Arguments({Argument, VersionTuple().getAsString()}) {}
18911904
DarwinPlatform(SourceKind Kind, DarwinPlatformKind Platform,
18921905
VersionTuple Value, Arg *Argument = nullptr)
1893-
: Kind(Kind), Platform(Platform), ResolvedOSVersion(Value),
1894-
ProvidedOSVersion(!Value.empty()), Argument(Argument) {}
1906+
: Kind(Kind), Platform(Platform),
1907+
Arguments({Argument, Value.getAsString()}) {
1908+
if (!Value.empty())
1909+
UnderlyingOSVersion = Value;
1910+
}
18951911

18961912
static VersionTuple getVersionFromString(const StringRef Input) {
18971913
llvm::VersionTuple Version;
@@ -1947,14 +1963,12 @@ struct DarwinPlatform {
19471963
// OSVersion tied to the main target value.
19481964
VersionTuple ZipperedOSVersion;
19491965
// We allow multiple ways to set or default the OS
1950-
// version used for compilation. The ResolvedOSVersion always represents what
1951-
// will be used.
1952-
VersionTuple ResolvedOSVersion;
1953-
// Track whether the OS deployment version was explicitly set on creation.
1954-
// This can be used for overidding the resolved version or error reporting.
1955-
bool ProvidedOSVersion = false;
1966+
// version used for compilation. When set, UnderlyingOSVersion represents
1967+
// the intended version to match the platform information computed from
1968+
// arguments.
1969+
std::optional<VersionTuple> UnderlyingOSVersion;
19561970
bool InferSimulatorFromArch = true;
1957-
Arg *Argument;
1971+
std::pair<Arg *, std::string> Arguments;
19581972
StringRef EnvVarName;
19591973
// When compiling for a zippered target, this value represents the target
19601974
// triple encoded in the target variant.
@@ -2151,9 +2165,10 @@ inferDeploymentTargetFromSDK(DerivedArgList &Args,
21512165
// The SDK can be an SDK variant with a name like `<prefix>.<platform>`.
21522166
return CreatePlatformFromSDKName(dropSDKNamePrefix(SDK));
21532167
}
2154-
2155-
VersionTuple getOSVersion(llvm::Triple::OSType OS, const llvm::Triple &Triple,
2156-
const Driver &TheDriver) {
2168+
// Compute & get the OS Version when the target triple omitted one.
2169+
VersionTuple getInferredOSVersion(llvm::Triple::OSType OS,
2170+
const llvm::Triple &Triple,
2171+
const Driver &TheDriver) {
21572172
VersionTuple OsVersion;
21582173
llvm::Triple SystemTriple(llvm::sys::getProcessTriple());
21592174
switch (OS) {
@@ -2215,8 +2230,8 @@ inferDeploymentTargetFromArch(DerivedArgList &Args, const Darwin &Toolchain,
22152230
OSTy = llvm::Triple::MacOSX;
22162231
if (OSTy == llvm::Triple::UnknownOS)
22172232
return std::nullopt;
2218-
return DarwinPlatform::createFromArch(OSTy,
2219-
getOSVersion(OSTy, Triple, TheDriver));
2233+
return DarwinPlatform::createFromArch(
2234+
OSTy, getInferredOSVersion(OSTy, Triple, TheDriver));
22202235
}
22212236

22222237
/// Returns the deployment target that's specified using the -target option.
@@ -2228,7 +2243,6 @@ std::optional<DarwinPlatform> getDeploymentTargetFromTargetArg(
22282243
if (Triple.getOS() == llvm::Triple::Darwin ||
22292244
Triple.getOS() == llvm::Triple::UnknownOS)
22302245
return std::nullopt;
2231-
VersionTuple OSVersion = getOSVersion(Triple.getOS(), Triple, TheDriver);
22322246
std::optional<llvm::Triple> TargetVariantTriple;
22332247
for (const Arg *A : Args.filtered(options::OPT_darwin_target_variant)) {
22342248
llvm::Triple TVT(A->getValue());
@@ -2258,9 +2272,6 @@ std::optional<DarwinPlatform> getDeploymentTargetFromTargetArg(
22582272
Triple, Args.getLastArg(options::OPT_target), TargetVariantTriple,
22592273
SDKInfo);
22602274

2261-
// Override the OSVersion if it doesn't match the one from the triple.
2262-
if (Triple.getOSVersion() != OSVersion)
2263-
PlatformAndVersion.setOSVersion(OSVersion);
22642275
return PlatformAndVersion;
22652276
}
22662277

@@ -2350,6 +2361,12 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
23502361
getDriver().Diag(diag::err_drv_cannot_mix_options)
23512362
<< TargetArgStr << MTargetOSArgStr;
23522363
}
2364+
// Implicitly allow resolving the OS version when it wasn't explicitly set.
2365+
bool TripleProvidedOSVersion = PlatformAndVersion->hasOSVersion();
2366+
if (!TripleProvidedOSVersion)
2367+
PlatformAndVersion->setOSVersion(
2368+
getInferredOSVersion(getTriple().getOS(), getTriple(), getDriver()));
2369+
23532370
std::optional<DarwinPlatform> PlatformAndVersionFromOSVersionArg =
23542371
getDeploymentTargetFromOSVersionArg(Args, getDriver());
23552372
if (PlatformAndVersionFromOSVersionArg) {
@@ -2372,7 +2389,7 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
23722389
// the -target does not include an OS version.
23732390
if (PlatformAndVersion->getPlatform() ==
23742391
PlatformAndVersionFromOSVersionArg->getPlatform() &&
2375-
!PlatformAndVersion->providedOSVersion()) {
2392+
!TripleProvidedOSVersion) {
23762393
PlatformAndVersion->setOSVersion(
23772394
PlatformAndVersionFromOSVersionArg->getOSVersion());
23782395
} else {
@@ -2451,8 +2468,8 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
24512468
bool HadExtra;
24522469
// The major version should not be over this number.
24532470
const unsigned MajorVersionLimit = 1000;
2454-
const std::string OSVersionStr =
2455-
PlatformAndVersion->getOSVersion().getAsString();
2471+
const VersionTuple OSVersion = PlatformAndVersion->takeOSVersion();
2472+
const std::string OSVersionStr = OSVersion.getAsString();
24562473
// Set the tool chain target information.
24572474
if (Platform == MacOS) {
24582475
if (!Driver::GetReleaseVersion(OSVersionStr, Major, Minor, Micro,

0 commit comments

Comments
 (0)