tailscale/tool/gocross/gocross-wrapper.ps1
Will Norris 3ec5be3f51 all: remove AUTHORS file and references to it
This file was never truly necessary and has never actually been used in
the history of Tailscale's open source releases.

A Brief History of AUTHORS files
---

The AUTHORS file was a pattern developed at Google, originally for
Chromium, then adopted by Go and a bunch of other projects. The problem
was that Chromium originally had a copyright line only recognizing
Google as the copyright holder. Because Google (and most open source
projects) do not require copyright assignemnt for contributions, each
contributor maintains their copyright. Some large corporate contributors
then tried to add their own name to the copyright line in the LICENSE
file or in file headers. This quickly becomes unwieldy, and puts a
tremendous burden on anyone building on top of Chromium, since the
license requires that they keep all copyright lines intact.

The compromise was to create an AUTHORS file that would list all of the
copyright holders. The LICENSE file and source file headers would then
include that list by reference, listing the copyright holder as "The
Chromium Authors".

This also become cumbersome to simply keep the file up to date with a
high rate of new contributors. Plus it's not always obvious who the
copyright holder is. Sometimes it is the individual making the
contribution, but many times it may be their employer. There is no way
for the proejct maintainer to know.

Eventually, Google changed their policy to no longer recommend trying to
keep the AUTHORS file up to date proactively, and instead to only add to
it when requested: https://opensource.google/docs/releasing/authors.
They are also clear that:

> Adding contributors to the AUTHORS file is entirely within the
> project's discretion and has no implications for copyright ownership.

It was primarily added to appease a small number of large contributors
that insisted that they be recognized as copyright holders (which was
entirely their right to do). But it's not truly necessary, and not even
the most accurate way of identifying contributors and/or copyright
holders.

In practice, we've never added anyone to our AUTHORS file. It only lists
Tailscale, so it's not really serving any purpose. It also causes
confusion because Tailscalars put the "Tailscale Inc & AUTHORS" header
in other open source repos which don't actually have an AUTHORS file, so
it's ambiguous what that means.

Instead, we just acknowledge that the contributors to Tailscale (whoever
they are) are copyright holders for their individual contributions. We
also have the benefit of using the DCO (developercertificate.org) which
provides some additional certification of their right to make the
contribution.

The source file changes were purely mechanical with:

    git ls-files | xargs sed -i -e 's/\(Tailscale Inc &\) AUTHORS/\1 contributors/g'

Updates #cleanup

Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d
Signed-off-by: Will Norris <will@tailscale.com>
2026-01-23 15:49:45 -08:00

232 lines
8.6 KiB
PowerShell

# Copyright (c) Tailscale Inc & contributors
# SPDX-License-Identifier: BSD-3-Clause
#Requires -Version 7.4
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version 3.0
if (($Env:CI -eq 'true') -and ($Env:NOPWSHDEBUG -ne 'true')) {
Set-PSDebug -Trace 1
}
<#
.DESCRIPTION
Copies the script's $args variable into an array, which is easier to work with
when preparing to start child processes.
#>
function Copy-ScriptArgs {
$list = [System.Collections.Generic.List[string]]::new($Script:args.Count)
foreach ($arg in $Script:args) {
$list.Add($arg)
}
return $list.ToArray()
}
<#
.DESCRIPTION
Copies the current environment into a hashtable, which is easier to work with
when preparing to start child processes.
#>
function Copy-Environment {
$result = @{}
foreach ($pair in (Get-Item -Path Env:)) {
$result[$pair.Key] = $pair.Value
}
return $result
}
<#
.DESCRIPTION
Outputs the fully-qualified path to the repository's root directory. This
function expects to be run from somewhere within a git repository.
The directory containing the git executable must be somewhere in the PATH.
#>
function Get-RepoRoot {
Get-Command -Name 'git' | Out-Null
$repoRoot = & git rev-parse --show-toplevel
if ($LASTEXITCODE -ne 0) {
throw "failed obtaining repo root: git failed with code $LASTEXITCODE"
}
# Git outputs a path containing forward slashes. Canonicalize.
return [System.IO.Path]::GetFullPath($repoRoot)
}
<#
.DESCRIPTION
Runs the provided ScriptBlock in a child scope, restoring any changes to the
current working directory once the script block completes.
#>
function Start-ChildScope {
param (
[Parameter(Mandatory = $true)]
[ScriptBlock]$ScriptBlock
)
$initialLocation = Get-Location
try {
Invoke-Command -ScriptBlock $ScriptBlock
}
finally {
Set-Location -Path $initialLocation
}
}
<#
.SYNOPSIS
Write-Output with timestamps prepended to each line.
#>
function Write-Log {
param ($message)
$timestamp = (Get-Date).ToString('yyyy-MM-dd HH:mm:ss')
Write-Output "$timestamp - $message"
}
$bootstrapScriptBlock = {
$repoRoot = Get-RepoRoot
Set-Location -LiteralPath $repoRoot
switch -Wildcard -File .\go.toolchain.rev {
"/*" { $toolchain = $_ }
default {
$rev = $_
$tsgo = Join-Path $Env:USERPROFILE '.cache' 'tsgo'
$toolchain = Join-Path $tsgo $rev
if (-not (Test-Path -LiteralPath "$toolchain.extracted" -PathType Leaf -ErrorAction SilentlyContinue)) {
New-Item -Force -Path $tsgo -ItemType Directory | Out-Null
Remove-Item -Force -Recurse -LiteralPath $toolchain -ErrorAction SilentlyContinue
Write-Log "Downloading Go toolchain $rev"
# Values from https://web.archive.org/web/20250227081443/https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.architecture?view=net-9.0
$cpuArch = ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture | Out-String -NoNewline)
# Comparison in switch is case-insensitive by default.
switch ($cpuArch) {
'x86' { $goArch = '386' }
'x64' { $goArch = 'amd64' }
default { $goArch = $cpuArch }
}
Invoke-WebRequest -Uri "https://github.com/tailscale/go/releases/download/build-$rev/windows-$goArch.tar.gz" -OutFile "$toolchain.tar.gz"
try {
New-Item -Force -Path $toolchain -ItemType Directory | Out-Null
Start-ChildScope -ScriptBlock {
Set-Location -LiteralPath $toolchain
# Using an absolute path to the tar that ships with Windows
# to avoid conflicts with others (eg msys2).
$system32 = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::System)
$tar = Join-Path $system32 'tar.exe' -Resolve
& $tar --strip-components=1 -xf "$toolchain.tar.gz"
if ($LASTEXITCODE -ne 0) {
throw "tar failed with exit code $LASTEXITCODE"
}
}
$rev | Out-File -FilePath "$toolchain.extracted"
}
finally {
Remove-Item -Force "$toolchain.tar.gz" -ErrorAction Continue
}
# Cleanup old toolchains.
$maxDays = 90
$oldFiles = Get-ChildItem -Path $tsgo -Filter '*.extracted' -File -Recurse -Depth 1 | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$maxDays) }
foreach ($file in $oldFiles) {
Write-Log "Cleaning up old Go toolchain $($file.Basename)"
Remove-Item -LiteralPath $file.FullName -Force -ErrorAction Continue
$dirName = Join-Path $file.DirectoryName $file.Basename -Resolve -ErrorAction Continue
if ($dirName -and (Test-Path -LiteralPath $dirName -PathType Container -ErrorAction Continue)) {
Remove-Item -LiteralPath $dirName -Recurse -Force -ErrorAction Continue
}
}
}
}
}
if ($Env:TS_USE_GOCROSS -ne '1') {
return
}
if (Test-Path -LiteralPath $toolchain -PathType Container -ErrorAction SilentlyContinue) {
$goMod = Join-Path $repoRoot 'go.mod' -Resolve
$goLine = Get-Content -LiteralPath $goMod | Select-String -Pattern '^go (.*)$' -List
$wantGoMinor = $goLine.Matches.Groups[1].Value.split('.')[1]
$versionFile = Join-Path $toolchain 'VERSION'
if (Test-Path -LiteralPath $versionFile -PathType Leaf -ErrorAction SilentlyContinue) {
try {
$haveGoMinor = ((Get-Content -LiteralPath $versionFile -TotalCount 1).split('.')[1]) -replace 'rc.*', ''
}
catch {
}
}
if ([string]::IsNullOrEmpty($haveGoMinor) -or ($haveGoMinor -lt $wantGoMinor)) {
Remove-Item -Force -Recurse -LiteralPath $toolchain -ErrorAction Continue
Remove-Item -Force -LiteralPath "$toolchain.extracted" -ErrorAction Continue
}
}
$wantVer = & git rev-parse HEAD
$gocrossOk = $false
$gocrossPath = '.\gocross.exe'
if (Get-Command -Name $gocrossPath -CommandType Application -ErrorAction SilentlyContinue) {
$gotVer = & $gocrossPath gocross-version 2> $null
if ($gotVer -eq $wantVer) {
$gocrossOk = $true
}
}
if (-not $gocrossOk) {
$goBuildEnv = Copy-Environment
$goBuildEnv['CGO_ENABLED'] = '0'
# Start-Process's -Environment arg applies diffs, so instead of removing
# these variables from $goBuildEnv, we must set them to $null to indicate
# that they should be cleared.
$goBuildEnv['GOOS'] = $null
$goBuildEnv['GOARCH'] = $null
$goBuildEnv['GO111MODULE'] = $null
$goBuildEnv['GOROOT'] = $null
$procExe = Join-Path $toolchain 'bin' 'go.exe' -Resolve
$proc = Start-Process -FilePath $procExe -WorkingDirectory $repoRoot -Environment $goBuildEnv -ArgumentList 'build', '-o', $gocrossPath, "-ldflags=-X=tailscale.com/version.gitCommitStamp=$wantVer", 'tailscale.com/tool/gocross' -NoNewWindow -Wait -PassThru
if ($proc.ExitCode -ne 0) {
throw 'error building gocross'
}
}
} # bootstrapScriptBlock
Start-ChildScope -ScriptBlock $bootstrapScriptBlock
$repoRoot = Get-RepoRoot
$execEnv = Copy-Environment
# Start-Process's -Environment arg applies diffs, so instead of removing
# these variables from $execEnv, we must set them to $null to indicate
# that they should be cleared.
$execEnv['GOROOT'] = $null
$argList = Copy-ScriptArgs
if ($Env:TS_USE_GOCROSS -ne '1') {
$revFile = Join-Path $repoRoot 'go.toolchain.rev' -Resolve
switch -Wildcard -File $revFile {
"/*" { $toolchain = $_ }
default {
$rev = $_
$tsgo = Join-Path $Env:USERPROFILE '.cache' 'tsgo'
$toolchain = Join-Path $tsgo $rev -Resolve
}
}
$procExe = Join-Path $toolchain 'bin' 'go.exe' -Resolve
$proc = Start-Process -FilePath $procExe -WorkingDirectory $repoRoot -Environment $execEnv -ArgumentList $argList -NoNewWindow -Wait -PassThru
exit $proc.ExitCode
}
$procExe = Join-Path $repoRoot 'gocross.exe' -Resolve
$proc = Start-Process -FilePath $procExe -WorkingDirectory $repoRoot -Environment $execEnv -ArgumentList $argList -NoNewWindow -Wait -PassThru
exit $proc.ExitCode