all: Add build constants for runtime.GOOS comparisons (#8442)

all: Add package runtimeos for runtime.GOOS comparisons

I grew tired of hand written string comparisons. This adds generated
constants for the GOOS values, and predefined Is$OS constants that can
be iffed on. In a couple of places I rewrote trivial switch:es to if:s,
and added Illumos where we checked for Solaris (because they are
effectively the same, and if we're going to target one of them that
would be Illumos...).
This commit is contained in:
Jakob Borg
2022-07-28 19:36:39 +02:00
committed by GitHub
parent f13d65262a
commit a3c724f2c3
51 changed files with 282 additions and 192 deletions
+38
View File
@@ -0,0 +1,38 @@
// Code generated by runtimeos.sh. DO NOT EDIT.
package build
import "runtime"
const (
AIX = "aix"
Android = "android"
Darwin = "darwin"
Dragonfly = "dragonfly"
FreeBSD = "freebsd"
Illumos = "illumos"
IOS = "ios"
JS = "js"
Linux = "linux"
NetBSD = "netbsd"
OpenBSD = "openbsd"
Plan9 = "plan9"
Solaris = "solaris"
Windows = "windows"
)
const (
IsAIX = runtime.GOOS == AIX
IsAndroid = runtime.GOOS == Android
IsDarwin = runtime.GOOS == Darwin
IsDragonfly = runtime.GOOS == Dragonfly
IsFreeBSD = runtime.GOOS == FreeBSD
IsIllumos = runtime.GOOS == Illumos
IsIOS = runtime.GOOS == IOS
IsJS = runtime.GOOS == JS
IsLinux = runtime.GOOS == Linux
IsNetBSD = runtime.GOOS == NetBSD
IsOpenBSD = runtime.GOOS == OpenBSD
IsPlan9 = runtime.GOOS == Plan9
IsSolaris = runtime.GOOS == Solaris
IsWindows = runtime.GOOS == Windows
)
+43
View File
@@ -0,0 +1,43 @@
# Copyright (C) 2022 The Syncthing Authors.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.
#!/bin/sh
# List known operating system/architecture combos, removing the
# architecture, and making constants of them.
osname() {
echo "$1" | awk '{print toupper(substr($1, 1, 1)) substr($1, 2)}' \
| sed s/aix/AIX/i \
| sed s/bsd/BSD/i \
| sed s/ios/IOS/i \
| sed s/js/JS/i
}
osconstants() {
echo "// Code generated by runtimeos.sh. DO NOT EDIT."
echo "package build"
echo "import \"runtime\""
echo "const ("
for OS in $(go tool dist list | sed 's|/.*||' | sort | uniq) ; do
name=$(osname "$OS")
echo "$name = \"$OS\""
done
echo ")"
echo
echo "const ("
for OS in $(go tool dist list | sed 's|/.*||' | sort | uniq) ; do
name=$(osname "$OS")
echo "Is$name = runtime.GOOS == $name"
done
echo ")"
}
osconstants > runtimeos.gen.go
gofmt -w -s runtimeos.gen.go