terminal/tools/Generate-FeatureStagingHeader.ps1

207 lines
5.9 KiB
PowerShell
Raw Normal View History

Add support for branch- and branding-based feature flagging (#10361) This pull request implements a "feature flagging" system that will let us turn Terminal and conhost features on/off by branch, "release" status or branding (Dev, Preview, etc.). It's loosely modelled after the Windows OS concept of "Velocity," but only insofar as it is driven by an XML document and there's a tool that emits a header file for you to include. It only supports toggling features at compile time, and the feature flag evaluators are intended to be fully constant expressions. Features are added to `src\features.xml` and marked with a "stage". For now, the only stages available are `AlwaysDisabled` and `AlwaysEnabled`. Features can be toggled to different states using branch and branding tokens, as documented in the included feature flag docs. For a given feature Feature_XYZ, we will emit two fixtures visible to the compiler: 1. A preprocessor define `TIL_FEATURE_XYZ_ENABLED` (usable from MIDL, C++ and C) 2. A feature class type `Feature_XYZ` with a static constexpr member `IsEnabled()` (usable from C++, designed for `if constexpr()`). Like Velocity, we rely on the compiler to eliminate dead code caused by things that compile down to `if constexpr (false)`. :) Michael suggested that we could use `WindowsInbox` as a branding to determine when we were being built inside Windows to supplant our use of the `__INSIDE_WINDOWS` preprocessor token. It was brilliant. Design Decisions ---------------- * Emitting the header as part of an MSBuild project * WHY: This allows the MSBuild engine to ensure that the build is only run once, even in a parallel build situation. * Only having one feature flag document for the entire project * WHY: Ease. * Forcibly including `TilFeatureStaging` with `/FI` for all CL compiler invocations. * WHY: If this is a project-wide feature system, we should make it as easy as possible to use. * Emitting preprocessor definitions instead of constexpr/consteval * WHY: Removing entire functions/includes is impossible with `if constexpr`. * WHY: MIDL cannot use a `static constexpr bool`, but it can rely on the C preprocessor to remove text. * Using MSBuild to emit the text instead of PowerShell * WHY: This allows us to leverage MSBuild's `WriteOnlyWhenDifferent` task parameter to avoid changing the file's modification time when it would have resulted in the same contents. This lets us use the same FeatureStaging header across multiple builds and multiple branches and brandings _assuming that they do not result in a feature flag change_. * The risk in using a force-include is always that it, for some reason, determines that the entire project is out of date. We've gone to great lengths to make sure that it only does so if the features _actually materially changed_.
2021-06-11 01:09:52 +02:00
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
################################################################################
# This script generates a header describing which Terminal/Console features
# should be compiled-in, based on an XML document describing them.
[CmdletBinding()]
Param(
[Parameter(Position=0, Mandatory=$True)]
[ValidateScript({ Test-Path $_ })]
[string]$Path,
[ValidateSet("Dev", "Preview", "Release", "WindowsInbox")]
[string]$Branding = "Dev",
[string]$BranchOverride = $Null,
[string]$OutputPath
)
Enum Stage {
AlwaysDisabled;
AlwaysEnabled;
}
Function ConvertTo-FeatureStage([string]$stage) {
Switch($stage) {
"AlwaysEnabled" { [Stage]::AlwaysEnabled; Return }
"AlwaysDisabled" { [Stage]::AlwaysDisabled; Return }
}
Throw "Invalid feature stage $stage"
}
Class Feature {
[string]$Name
[Stage]$Stage
[System.Collections.Generic.Dictionary[string, Stage]]$BranchTokenStages
[System.Collections.Generic.Dictionary[string, Stage]]$BrandingTokenStages
[bool]$DisabledReleaseToken
Feature([System.Xml.XmlElement]$entry) {
$this.Name = $entry.name
$this.Stage = ConvertTo-FeatureStage $entry.stage
$this.BranchTokenStages = [System.Collections.Generic.Dictionary[string, Stage]]::new()
$this.BrandingTokenStages = [System.Collections.Generic.Dictionary[string, Stage]]::new()
$this.DisabledReleaseToken = $Null -Ne $entry.alwaysDisabledReleaseTokens
ForEach ($b in $entry.alwaysDisabledBranchTokens.branchToken) {
$this.BranchTokenStages[$b] = [Stage]::AlwaysDisabled
}
# AlwaysEnabled branches win over AlwaysDisabled branches
ForEach ($b in $entry.alwaysEnabledBranchTokens.branchToken) {
$this.BranchTokenStages[$b] = [Stage]::AlwaysEnabled
}
ForEach ($b in $entry.alwaysDisabledBrandingTokens.brandingToken) {
$this.BrandingTokenStages[$b] = [Stage]::AlwaysDisabled
}
# AlwaysEnabled brandings win over AlwaysDisabled brandings
ForEach ($b in $entry.alwaysEnabledBrandingTokens.brandingToken) {
$this.BrandingTokenStages[$b] = [Stage]::AlwaysEnabled
}
}
[string] PreprocessorName() {
return "TIL_$($this.Name.ToUpper())_ENABLED"
}
}
class FeatureComparer : System.Collections.Generic.IComparer[Feature] {
[int] Compare([Feature]$a, [Feature]$b) {
If ($a.Name -lt $b.Name) {
Return -1
} ElseIf ($a.Name -gt $b.Name) {
Return 1
} Else {
Return 0
}
}
}
Function Resolve-FinalFeatureStage {
Param(
[Feature]$Feature,
[string]$Branch,
[string]$Branding
)
# RELEASE=DISABLED wins all checks
# Then, branch match by most-specific branch
# Then, branding type (if no overriding branch match)
If ($Branding -Eq "Release" -And $Feature.DisabledReleaseToken) {
[Stage]::AlwaysDisabled
Return
}
If (-Not [String]::IsNullOrEmpty($Branch)) {
$lastMatchLen = 0
$branchStage = $Null
ForEach ($branchToken in $Feature.BranchTokenStages.Keys) {
# Match the longest branch token -- it should be the most specific
If ($Branch -Like $branchToken -And $branchToken.Length -Gt $lastMatchLen) {
$lastMatchLen = $branchToken.Length
$branchStage = $Feature.BranchTokenStages[$branchToken]
}
}
If ($Null -Ne $branchStage) {
$branchStage
Return
}
}
$BrandingStage = $Feature.BrandingTokenStages[$Branding]
If ($Null -Ne $BrandingStage) {
$BrandingStage
Return
}
$Feature.Stage
}
$ErrorActionPreference = "Stop"
$x = [xml](Get-Content $Path -EA:Stop)
$x.Schemas.Add('http://microsoft.com/TilFeatureStaging-Schema.xsd', (Resolve-Path (Join-Path $PSScriptRoot "FeatureStagingSchema.xsd")).Path) | Out-Null
$x.Validate($null)
$featureComparer = [FeatureComparer]::new()
$features = [System.Collections.Generic.List[Feature]]::new(16)
ForEach ($entry in $x.featureStaging.feature) {
$features.Add([Feature]::new($entry))
}
$features.Sort($featureComparer)
$featureFinalStages = [System.Collections.Generic.Dictionary[string, Stage]]::new(16)
$branch = $BranchOverride
If ([String]::IsNullOrEmpty($branch)) {
Try {
$branch = & git branch --show-current 2>$Null
} Catch {
Try {
$branch = & git rev-parse --abbrev-ref HEAD 2>$Null
} Catch {
Write-Verbose "Cannot determine current Git branch; skipping branch validation"
}
}
}
ForEach ($feature in $features) {
$featureFinalStages[$feature.Name] = Resolve-FinalFeatureStage -Feature $feature -Branch $branch -Branding $Branding
}
### CODE GENERATION
$script:Output = ""
Function AddOutput($s) {
$script:Output += $s
}
AddOutput @"
// THIS FILE IS AUTOMATICALLY GENERATED; DO NOT EDIT IT
// INPUT FILE: $Path
"@
ForEach ($feature in $features) {
$stage = $featureFinalStages[$feature.Name]
AddOutput @"
#define $($feature.PreprocessorName()) $(If ($stage -eq [Stage]::AlwaysEnabled) { "1" } Else { "0" })
"@
}
AddOutput @"
#if defined(__cplusplus)
"@
ForEach ($feature in $features) {
AddOutput @"
__pragma(detect_mismatch("ODR_violation_$($feature.PreprocessorName())_mismatch", "$($feature.Stage)"))
struct $($feature.Name)
{
static constexpr bool IsEnabled() { return $($feature.PreprocessorName()) == 1; }
};
"@
}
AddOutput @"
#endif
"@
If ([String]::IsNullOrEmpty($OutputPath)) {
$script:Output
} Else {
Out-File -Encoding UTF8 -FilePath $OutputPath -InputObject $script:Output
}