| 1 | // Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module pref |
| 5 | |
| 6 | import os |
| 7 | |
| 8 | pub enum OS { |
| 9 | _auto // Reserved so .macos cannot be misunderstood as auto |
| 10 | ios |
| 11 | macos |
| 12 | linux |
| 13 | windows |
| 14 | freebsd |
| 15 | openbsd |
| 16 | netbsd |
| 17 | dragonfly |
| 18 | js_node |
| 19 | js_browser |
| 20 | js_freestanding |
| 21 | android |
| 22 | termux // like android, but compiling/running natively on the devices |
| 23 | solaris |
| 24 | qnx |
| 25 | serenity |
| 26 | plan9 |
| 27 | vinix |
| 28 | haiku |
| 29 | wasm32 |
| 30 | wasm32_emscripten |
| 31 | wasm32_wasi |
| 32 | browser // -b wasm -os browser |
| 33 | wasi // -b wasm -os wasi |
| 34 | raw |
| 35 | all |
| 36 | } |
| 37 | |
| 38 | // Helper function to convert string names to OS enum |
| 39 | pub fn os_from_string(os_str string) !OS { |
| 40 | lcased_os_str := os_str.to_lower_ascii() |
| 41 | match lcased_os_str { |
| 42 | '' { |
| 43 | return ._auto |
| 44 | } |
| 45 | 'linux' { |
| 46 | return .linux |
| 47 | } |
| 48 | 'nix' { |
| 49 | return .linux |
| 50 | } |
| 51 | 'windows' { |
| 52 | return .windows |
| 53 | } |
| 54 | 'ios' { |
| 55 | return .ios |
| 56 | } |
| 57 | 'macos' { |
| 58 | return .macos |
| 59 | } |
| 60 | 'mac' { |
| 61 | return .macos |
| 62 | } |
| 63 | 'darwin' { |
| 64 | return .macos |
| 65 | } |
| 66 | 'freebsd' { |
| 67 | return .freebsd |
| 68 | } |
| 69 | 'openbsd' { |
| 70 | return .openbsd |
| 71 | } |
| 72 | 'netbsd' { |
| 73 | return .netbsd |
| 74 | } |
| 75 | 'dragonfly' { |
| 76 | return .dragonfly |
| 77 | } |
| 78 | 'js', 'js_node' { |
| 79 | return .js_node |
| 80 | } |
| 81 | 'js_freestanding' { |
| 82 | return .js_freestanding |
| 83 | } |
| 84 | 'js_browser' { |
| 85 | return .js_browser |
| 86 | } |
| 87 | 'solaris' { |
| 88 | return .solaris |
| 89 | } |
| 90 | 'serenity' { |
| 91 | return .serenity |
| 92 | } |
| 93 | 'qnx' { |
| 94 | return .qnx |
| 95 | } |
| 96 | 'plan9' { |
| 97 | return .plan9 |
| 98 | } |
| 99 | 'vinix' { |
| 100 | return .vinix |
| 101 | } |
| 102 | 'android' { |
| 103 | return .android |
| 104 | } |
| 105 | 'termux' { |
| 106 | return .termux |
| 107 | } |
| 108 | 'haiku' { |
| 109 | return .haiku |
| 110 | } |
| 111 | 'raw' { |
| 112 | return .raw |
| 113 | } |
| 114 | // WASM options: |
| 115 | 'wasm32' { |
| 116 | return .wasm32 |
| 117 | } |
| 118 | 'wasm32_wasi' { |
| 119 | return .wasm32_wasi |
| 120 | } |
| 121 | 'wasm32_emscripten' { |
| 122 | return .wasm32_emscripten |
| 123 | } |
| 124 | // Native WASM options: |
| 125 | 'browser' { |
| 126 | return .browser |
| 127 | } |
| 128 | 'wasi' { |
| 129 | return .wasi |
| 130 | } |
| 131 | else { |
| 132 | return error('bad OS ${os_str}') |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // lower returns the name that could be used with `-os osname`, for each OS enum value |
| 138 | // NOTE: it is important to not change the names here, they should match 1:1, since they |
| 139 | // are used as part of the cache keys, when -usecache is passed. |
| 140 | pub fn (o OS) lower() string { |
| 141 | return match o { |
| 142 | ._auto { '' } |
| 143 | .linux { 'linux' } |
| 144 | .windows { 'windows' } |
| 145 | .macos { 'macos' } |
| 146 | .ios { 'ios' } |
| 147 | .freebsd { 'freebsd' } |
| 148 | .openbsd { 'openbsd' } |
| 149 | .netbsd { 'netbsd' } |
| 150 | .dragonfly { 'dragonfly' } |
| 151 | .js_node { 'js' } |
| 152 | .js_freestanding { 'js_freestanding' } |
| 153 | .js_browser { 'js_browser' } |
| 154 | .solaris { 'solaris' } |
| 155 | .serenity { 'serenity' } |
| 156 | .qnx { 'qnx' } |
| 157 | .plan9 { 'plan9' } |
| 158 | .vinix { 'vinix' } |
| 159 | .android { 'android' } |
| 160 | .termux { 'termux' } |
| 161 | .haiku { 'haiku' } |
| 162 | .raw { 'raw' } |
| 163 | .wasm32 { 'wasm32' } |
| 164 | .wasm32_wasi { 'wasm32_wasi' } |
| 165 | .wasm32_emscripten { 'wasm32_emscripten' } |
| 166 | .browser { 'browser' } |
| 167 | .wasi { 'wasi' } |
| 168 | .all { 'all' } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | pub fn (o OS) str() string { |
| 173 | // TODO: check more thoroughly, why this method needs to exist at all, |
| 174 | // and why should it override the default autogenerated .str() method, |
| 175 | // instead of being named something like .label() ... |
| 176 | // It seems to serve only display purposes on the surface, but it is used |
| 177 | // internally by the compiler for comptime comparisons, which seems very |
| 178 | // error prone. It bugged the interpretation of `$if wasm32_emscripten {` for example. |
| 179 | match o { |
| 180 | ._auto { return 'RESERVED: AUTO' } |
| 181 | .ios { return 'iOS' } |
| 182 | .macos { return 'MacOS' } |
| 183 | .linux { return 'Linux' } |
| 184 | .windows { return 'Windows' } |
| 185 | .freebsd { return 'FreeBSD' } |
| 186 | .openbsd { return 'OpenBSD' } |
| 187 | .netbsd { return 'NetBSD' } |
| 188 | .dragonfly { return 'Dragonfly' } |
| 189 | .js_node { return 'NodeJS' } |
| 190 | .js_freestanding { return 'JavaScript' } |
| 191 | .js_browser { return 'JavaScript(Browser)' } |
| 192 | .android { return 'Android' } |
| 193 | .termux { return 'Termux' } |
| 194 | .solaris { return 'Solaris' } |
| 195 | .qnx { return 'QNX' } |
| 196 | .serenity { return 'SerenityOS' } |
| 197 | .plan9 { return 'Plan9' } |
| 198 | .vinix { return 'Vinix' } |
| 199 | .haiku { return 'Haiku' } |
| 200 | .wasm32 { return 'WebAssembly' } |
| 201 | .wasm32_emscripten { return 'WebAssembly(Emscripten)' } |
| 202 | .wasm32_wasi { return 'WebAssembly(WASI)' } |
| 203 | .browser { return 'browser' } |
| 204 | .wasi { return 'wasi' } |
| 205 | .raw { return 'Raw' } |
| 206 | .all { return 'all' } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | pub fn get_host_os() OS { |
| 211 | if os.getenv('TERMUX_VERSION') != '' { |
| 212 | return .termux |
| 213 | } |
| 214 | $if android { |
| 215 | return .android |
| 216 | } |
| 217 | $if emscripten ? { |
| 218 | return .wasm32_emscripten |
| 219 | } |
| 220 | $if wasm32_emscripten { |
| 221 | return .wasm32_emscripten |
| 222 | } |
| 223 | $if linux { |
| 224 | return .linux |
| 225 | } |
| 226 | $if ios { |
| 227 | return .ios |
| 228 | } |
| 229 | $if macos { |
| 230 | return .macos |
| 231 | } |
| 232 | $if windows { |
| 233 | return .windows |
| 234 | } |
| 235 | $if freebsd { |
| 236 | return .freebsd |
| 237 | } |
| 238 | $if openbsd { |
| 239 | return .openbsd |
| 240 | } |
| 241 | $if netbsd { |
| 242 | return .netbsd |
| 243 | } |
| 244 | $if dragonfly { |
| 245 | return .dragonfly |
| 246 | } |
| 247 | $if serenity { |
| 248 | return .serenity |
| 249 | } |
| 250 | //$if plan9 { |
| 251 | // return .plan9 |
| 252 | //} |
| 253 | $if vinix { |
| 254 | return .vinix |
| 255 | } |
| 256 | $if solaris { |
| 257 | return .solaris |
| 258 | } |
| 259 | $if haiku { |
| 260 | return .haiku |
| 261 | } |
| 262 | $if js_node { |
| 263 | return .js_node |
| 264 | } |
| 265 | $if js_freestanding { |
| 266 | return .js_freestanding |
| 267 | } |
| 268 | $if js_browser { |
| 269 | return .js_browser |
| 270 | } |
| 271 | $if js { |
| 272 | return .js_node |
| 273 | } |
| 274 | return ._auto |
| 275 | } |
| 276 | |