1/*
2   Copyright The containerd Authors.
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17package platforms
18
19import (
20	"runtime"
21	"strings"
22)
23
24// isLinuxOS returns true if the operating system is Linux.
25//
26// The OS value should be normalized before calling this function.
27func isLinuxOS(os string) bool {
28	return os == "linux"
29}
30
31// These function are generated from https://golang.org/src/go/build/syslist.go.
32//
33// We use switch statements because they are slightly faster than map lookups
34// and use a little less memory.
35
36// isKnownOS returns true if we know about the operating system.
37//
38// The OS value should be normalized before calling this function.
39func isKnownOS(os string) bool {
40	switch os {
41	case "aix", "android", "darwin", "dragonfly", "freebsd", "hurd", "illumos", "js", "linux", "nacl", "netbsd", "openbsd", "plan9", "solaris", "windows", "zos":
42		return true
43	}
44	return false
45}
46
47// isArmArch returns true if the architecture is ARM.
48//
49// The arch value should be normalized before being passed to this function.
50func isArmArch(arch string) bool {
51	switch arch {
52	case "arm", "arm64":
53		return true
54	}
55	return false
56}
57
58// isKnownArch returns true if we know about the architecture.
59//
60// The arch value should be normalized before being passed to this function.
61func isKnownArch(arch string) bool {
62	switch arch {
63	case "386", "amd64", "amd64p32", "arm", "armbe", "arm64", "arm64be", "ppc64", "ppc64le", "mips", "mipsle", "mips64", "mips64le", "mips64p32", "mips64p32le", "ppc", "riscv", "riscv64", "s390", "s390x", "sparc", "sparc64", "wasm":
64		return true
65	}
66	return false
67}
68
69func normalizeOS(os string) string {
70	if os == "" {
71		return runtime.GOOS
72	}
73	os = strings.ToLower(os)
74
75	switch os {
76	case "macos":
77		os = "darwin"
78	}
79	return os
80}
81
82// normalizeArch normalizes the architecture.
83func normalizeArch(arch, variant string) (string, string) {
84	arch, variant = strings.ToLower(arch), strings.ToLower(variant)
85	switch arch {
86	case "i386":
87		arch = "386"
88		variant = ""
89	case "x86_64", "x86-64":
90		arch = "amd64"
91		variant = ""
92	case "aarch64", "arm64":
93		arch = "arm64"
94		switch variant {
95		case "8", "v8":
96			variant = ""
97		}
98	case "armhf":
99		arch = "arm"
100		variant = "v7"
101	case "armel":
102		arch = "arm"
103		variant = "v6"
104	case "arm":
105		switch variant {
106		case "", "7":
107			variant = "v7"
108		case "5", "6", "8":
109			variant = "v" + variant
110		}
111	}
112
113	return arch, variant
114}
115