Skip to content

Commit 66eef68

Browse files
authored
Merge pull request #80212 from kubamracek/object-file-format
Add #objectFormat compilation conditional (SE-0492)
2 parents 6f1936f + 4923db8 commit 66eef68

File tree

7 files changed

+89
-6
lines changed

7 files changed

+89
-6
lines changed

include/swift/AST/PlatformConditionKinds.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,8 @@ PLATFORM_CONDITION_(PtrAuth, "ptrauth")
4949
/// The active arch target's max atomic bit width.
5050
PLATFORM_CONDITION_(HasAtomicBitWidth, "hasAtomicBitWidth")
5151

52+
/// The active target's file format (Mach-O, ELF, COFF, WASM)
53+
PLATFORM_CONDITION(ObjectFileFormat, "objectFormat")
54+
5255
#undef PLATFORM_CONDITION
5356
#undef PLATFORM_CONDITION_

lib/ASTGen/Sources/ASTGen/CompilerBuildConfiguration.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ struct CompilerBuildConfiguration: BuildConfiguration {
120120
staticBuildConfiguration.targetPointerBitWidth
121121
}
122122

123+
func isActiveTargetObjectFormat(name: String) throws -> Bool {
124+
try staticBuildConfiguration.isActiveTargetObjectFormat(name: name)
125+
}
126+
123127
var targetAtomicBitWidths: [Int] {
124128
staticBuildConfiguration.targetAtomicBitWidths
125129
}

lib/ASTGen/Sources/ASTGen/EmbeddedSupport.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ struct EmbeddedBuildConfiguration: BuildConfiguration {
124124
return configuration.targetPointerBitWidth
125125
}
126126

127+
func isActiveTargetObjectFormat(name: String) throws -> Bool {
128+
return try configuration.isActiveTargetObjectFormat(name: name)
129+
}
130+
127131
var targetAtomicBitWidths: [Int] {
128132
return configuration.targetAtomicBitWidths
129133
}

lib/Basic/LangOptions.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,20 @@ static const SupportedConditionalValue SupportedConditionalCompilationHasAtomicB
141141
"_128"
142142
};
143143

144+
static const SupportedConditionalValue SupportedConditionalCompilationObjectFileFormats[] = {
145+
"MachO",
146+
"ELF",
147+
"COFF",
148+
"Wasm",
149+
};
150+
144151
static const PlatformConditionKind AllPublicPlatformConditionKinds[] = {
145152
#define PLATFORM_CONDITION(LABEL, IDENTIFIER) PlatformConditionKind::LABEL,
146153
#define PLATFORM_CONDITION_(LABEL, IDENTIFIER)
147154
#include "swift/AST/PlatformConditionKinds.def"
148155
};
149156

150-
ArrayRef<SupportedConditionalValue> getSupportedConditionalCompilationValues(const PlatformConditionKind &Kind) {
157+
static ArrayRef<SupportedConditionalValue> getSupportedConditionalCompilationValues(const PlatformConditionKind &Kind) {
151158
switch (Kind) {
152159
case PlatformConditionKind::OS:
153160
return SupportedConditionalCompilationOSs;
@@ -167,12 +174,14 @@ ArrayRef<SupportedConditionalValue> getSupportedConditionalCompilationValues(con
167174
return SupportedConditionalCompilationPtrAuthSchemes;
168175
case PlatformConditionKind::HasAtomicBitWidth:
169176
return SupportedConditionalCompilationHasAtomicBitWidths;
177+
case PlatformConditionKind::ObjectFileFormat:
178+
return SupportedConditionalCompilationObjectFileFormats;
170179
}
171180
llvm_unreachable("Unhandled PlatformConditionKind in switch");
172181
}
173182

174-
PlatformConditionKind suggestedPlatformConditionKind(PlatformConditionKind Kind, const StringRef &V,
175-
std::vector<StringRef> &suggestedValues) {
183+
static PlatformConditionKind suggestedPlatformConditionKind(PlatformConditionKind Kind, const StringRef &V,
184+
std::vector<StringRef> &suggestedValues) {
176185
std::string lower = V.lower();
177186
for (const PlatformConditionKind& candidateKind : AllPublicPlatformConditionKinds) {
178187
if (candidateKind != Kind) {
@@ -191,8 +200,8 @@ PlatformConditionKind suggestedPlatformConditionKind(PlatformConditionKind Kind,
191200
return Kind;
192201
}
193202

194-
bool isMatching(PlatformConditionKind Kind, const StringRef &V,
195-
PlatformConditionKind &suggestedKind, std::vector<StringRef> &suggestions) {
203+
static bool isMatching(PlatformConditionKind Kind, const StringRef &V,
204+
PlatformConditionKind &suggestedKind, std::vector<StringRef> &suggestions) {
196205
// Compare against known values, ignoring case to avoid penalizing
197206
// characters with incorrect case.
198207
unsigned minDistance = std::numeric_limits<unsigned>::max();
@@ -231,6 +240,7 @@ checkPlatformConditionSupported(PlatformConditionKind Kind, StringRef Value,
231240
case PlatformConditionKind::TargetEnvironment:
232241
case PlatformConditionKind::PtrAuth:
233242
case PlatformConditionKind::HasAtomicBitWidth:
243+
case PlatformConditionKind::ObjectFileFormat:
234244
return isMatching(Kind, Value, suggestedKind, suggestedValues);
235245
case PlatformConditionKind::CanImport:
236246
// All importable names are valid.
@@ -649,6 +659,17 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
649659
addPlatformConditionValue(PlatformConditionKind::PointerBitWidth, "_64");
650660
}
651661

662+
// Set the "objectFormat" platform condition.
663+
if (Target.isOSBinFormatMachO()) {
664+
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "MachO");
665+
} else if (Target.isOSBinFormatELF()) {
666+
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "ELF");
667+
} else if (Target.isOSBinFormatCOFF()) {
668+
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "COFF");
669+
} else if (Target.isOSBinFormatWasm()) {
670+
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "Wasm");
671+
}
672+
652673
// Set the "runtime" platform condition.
653674
addPlatformConditionValue(PlatformConditionKind::Runtime,
654675
EnableObjCInterop ? "_ObjC" : "_Native");

lib/Basic/LangOptionsBridging.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ void BridgedLangOptions_enumerateBuildConfigurationEntries(
228228

229229
case PlatformConditionKind::Endianness:
230230
case PlatformConditionKind::PointerBitWidth:
231+
case PlatformConditionKind::ObjectFileFormat:
231232
case PlatformConditionKind::CanImport:
232233
case PlatformConditionKind::HasAtomicBitWidth:
233234
// Handled separately.

lib/Parse/ParseIfConfig.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ class ValidateIfConfigCondition :
385385
return E;
386386
}
387387

388-
// ( 'os' | 'arch' | '_endian' | '_pointerBitWidth' | '_runtime' | '_hasAtomicBitWidth' ) '(' identifier ')''
388+
// ( 'os' | 'arch' | '_endian' | '_pointerBitWidth' | '_runtime' | '_hasAtomicBitWidth' | 'objectFormat' ) '(' identifier ')''
389389
auto Kind = getPlatformConditionKind(*KindName);
390390
if (!Kind.has_value()) {
391391
D.diagnose(E->getLoc(), diag::unsupported_platform_condition_expression);
@@ -429,6 +429,8 @@ class ValidateIfConfigCondition :
429429
DiagName = "pointer authentication scheme"; break;
430430
case PlatformConditionKind::HasAtomicBitWidth:
431431
DiagName = "has atomic bit width"; break;
432+
case PlatformConditionKind::ObjectFileFormat:
433+
DiagName = "object file format"; break;
432434
case PlatformConditionKind::Runtime:
433435
llvm_unreachable("handled above");
434436
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// RUN: %swift -typecheck %s -target arm64-apple-none-macho -parse-stdlib 2>&1 | %FileCheck -check-prefix CHECK-MACHO %s
2+
// RUN: %swift -typecheck %s -target arm64-apple-none-elf -parse-stdlib 2>&1 | %FileCheck -check-prefix CHECK-ELF %s
3+
// RUN: %swift -typecheck %s -target wasm32-unknown-wasi -parse-stdlib 2>&1 | %FileCheck -check-prefix CHECK-WASM %s
4+
// RUN: %swift -typecheck %s -target x86_64-unknown-windows-msvc -parse-stdlib 2>&1 | %FileCheck -check-prefix CHECK-COFF %s
5+
6+
#if objectFormat(MachO)
7+
#warning("I'm MachO")
8+
#else
9+
#warning("I'm not MachO")
10+
#endif
11+
12+
#if objectFormat(ELF)
13+
#warning("I'm ELF")
14+
#else
15+
#warning("I'm not ELF")
16+
#endif
17+
18+
#if objectFormat(Wasm)
19+
#warning("I'm Wasm")
20+
#else
21+
#warning("I'm not Wasm")
22+
#endif
23+
24+
#if objectFormat(COFF)
25+
#warning("I'm COFF")
26+
#else
27+
#warning("I'm not COFF")
28+
#endif
29+
30+
// CHECK-MACHO: I'm MachO
31+
// CHECK-MACHO: I'm not ELF
32+
// CHECK-MACHO: I'm not Wasm
33+
// CHECK-MACHO: I'm not COFF
34+
35+
// CHECK-ELF: I'm not MachO
36+
// CHECK-ELF: I'm ELF
37+
// CHECK-ELF: I'm not Wasm
38+
// CHECK-ELF: I'm not COFF
39+
40+
// CHECK-WASM: I'm not MachO
41+
// CHECK-WASM: I'm not ELF
42+
// CHECK-WASM: I'm Wasm
43+
// CHECK-WASM: I'm not COFF
44+
45+
// CHECK-COFF: I'm not MachO
46+
// CHECK-COFF: I'm not ELF
47+
// CHECK-COFF: I'm not Wasm
48+
// CHECK-COFF: I'm COFF

0 commit comments

Comments
 (0)