-
Notifications
You must be signed in to change notification settings - Fork 10.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add #_objectFileFormat compilation conditional #80212
base: main
Are you sure you want to change the base?
Conversation
@swift-ci please test |
lib/Basic/LangOptions.cpp
Outdated
// Set the "_objectFileFormat" platform condition. | ||
if (Target.isOSBinFormatMachO()) { | ||
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "MachO"); | ||
} else if (Target.isOSBinFormatELF()) { | ||
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "ELF"); | ||
} else if (Target.isOSBinFormatCOFF()) { | ||
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "COFF"); | ||
} else if (Target.isOSBinFormatWasm()) { | ||
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "Wasm"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we rewrite this as:
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat,
llvm::StringSwitch<llvm::Triple::ObjectFormatType>(Target.getObjectFormat())
.Case(llvm::Triple::COFF, "COFF")
.Case(llvm::Triple::ELF, "ELF")
.Case(llvm::Triple::MachO, "MachO")
.Case(llvm::Triple::Wasm, "Wasm"));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, not really, because we're not switching over strings here, but over the enum cases... I could do a regular switch, but that reads worse than the chain of ifs:
StringRef FileFormatName;
switch (Target.getObjectFormat()) {
case llvm::Triple::COFF:
FileFormatName = "COFF";
break;
case llvm::Triple::ELF:
FileFormatName = "ELF";
break;
case llvm::Triple::MachO:
FileFormatName = "MachO";
break;
case llvm::Triple::Wasm:
FileFormatName = "Wasm";
break;
default:
break;
}
if (!FileFormatName.empty()) {
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat,
FileFormatName);
}
Should we consider gating this behind an experimental feature flag? |
I'm happy to do that if we deem that necessary, though all the other existing not-yet-ratified compilation conditions (e.g. pointerBitWidth) don't do that and just use an underscore. |
@swift-ci please test |
Swift Testing will need to make use of this to support Embedded Swift, and requiring an experimental flag will make it more difficult to do so. (When it gets formally featurized, we can eagerly adopt the final version.) |
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "COFF"); | ||
} else if (Target.isOSBinFormatWasm()) { | ||
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "Wasm"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add an else
clause here that diagnoses the target is unknown? That way if we add a new port, we'll know to update this code.
ea66110
to
4b27ab7
Compare
@swift-ci please test |
@swift-ci please test |
This implements the relevant part of the "Section Placement" proposal (draft: https://github.com/kubamracek/swift-evolution/blob/section-placement-control/proposals/0nnn-section-control.md, forum thread: https://forums.swift.org/t/pitch-3-section-placement-control/77435). Namely the ability to conditionalize on the object file format that the compiler is targeting (MachO, ELF, COFF of Wasm).
Adding with an underscored name (
#_objectFileFormat
) until the proposal is codified.rdar://147505228