package b0
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
Software construction and deployment kit
Install
dune-project
Dependency
Authors
Maintainers
Sources
b0-0.0.5.tbz
sha512=00a6868b4dfa34565d0141b335622a81a0e8d5b9e3c6dfad025dabfa3df2db2a1302b492953bbbce30c3a4406c324fcec25250a00b38f6d18a69e15605e3b07e
doc/src/b0_b00_kit/b00_os.ml.html
Source file b00_os.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278(*--------------------------------------------------------------------------- Copyright (c) 2020 The b0 programmers. All rights reserved. Distributed under the ISC license, see terms at the end of the file. ---------------------------------------------------------------------------*) open B0_std open B0_std.Fut.Syntax open B00 let read_spawn_stdout m file cmd = Memo.spawn m ~writes:[file] ~stdout:(`File file) cmd; Fut.map String.trim (Memo.read m file) let rec find_first_existing_file = function | [] -> None | f :: fs -> match Os.File.exists f |> Log.if_error ~use:false with | true -> Some f | false -> find_first_existing_file fs (* A few tools used by this module *) module Tool = struct let brew = Tool.by_name "brew" let cmd_exe = Tool.by_name "cmd.exe" (* windows *) let getprop = Tool.by_name "getprop" (* android *) let port = Tool.by_name "port" let sw_vers = Tool.by_name "sw_vers" (* macos *) let uname = Tool.by_name "uname" end (* Store keys for os-release and uname, not exposed but used below *) let os_release = (* Access https://www.freedesktop.org/software/systemd/man/os-release.html *) let unquote s = let quoted s len = s.[0] = '"' && s.[len - 1] = '"' in match String.length s with | 0 | 1 -> s | len when quoted s len -> String.subrange s ~first:1 ~last:(len - 2) | len -> s in let parse_line i line acc = match String.length (String.trim line) with | 0 -> acc | _ when line.[0] = '#' -> acc | _ -> match String.cut_left ~sep:"=" line with | None -> B00_lines.err i "Cannot find %a char in %S" Fmt.(code char) '=' line | Some (k, v) -> String.Map.add (String.trim k) (unquote (String.trim v)) acc in let det s m = let files = [Fpath.v "/etc/os-release"; Fpath.v "/usr/lib/os-release"] in match find_first_existing_file files with | None -> Fut.return String.Map.empty | Some file -> Memo.file_ready m file; let* s = Memo.read m file in let map = B00_lines.fold s ~file parse_line String.Map.empty in let map = Memo.notify_if_error m `Warn ~use:String.Map.empty map in Fut.return map in Store.key ~mark:"B00_os.os_release_file" det let uname = (* gets system name, release version, machine arch *) let mark = "b00_os.uname" in let det s m = let* tool = Memo.tool_opt m Tool.uname in match tool with | None -> Fut.return None | Some uname -> let file = Fpath.(Store.dir s / mark) in let uname = uname Cmd.(atom "-s" % "-r" % "-m") in let* s = read_spawn_stdout m file uname in match String.split_on_char ' ' s with | [sys; rel; mach] -> Fut.return (Some (sys, rel, mach)) | _ -> Memo.notify m `Warn "Could not parse %a output %S." Fmt.(code string) "uname" s; Fut.return None in Store.key ~mark det (* name *) let name = let of_uname m = function | None -> let env = Env.env (Memo.env m) in if String.Map.mem "COMSPEC" env || String.Map.mem "ComSpec" env then "windows" else "unknown" | Some (s, _, _) -> match String.Ascii.lowercase s with | "darwin" -> "macos" | s -> s in let det s m = Fut.map (of_uname m) (Store.get s uname) in Store.key ~mark:"b00_os.name" det (* version *) let freebsd_version s m file = let file = Fpath.(Store.dir s / file) in let uname = (Memo.tool m Tool.uname) (Cmd.atom "-U") in read_spawn_stdout m file uname let try_android_version s m file = let* getprop = Memo.tool_opt m Tool.getprop in match getprop with | None -> Fut.return None | Some getprop -> let file = Fpath.(Store.dir s / file) in let getprop = getprop (Cmd.atom "ro.build.version.release") in Fut.map Option.some (read_spawn_stdout m file getprop) let linux_version s m file = Fut.bind (try_android_version s m file) @@ function | Some v -> Fut.return v | None -> Fut.bind (Store.get s os_release) @@ function info -> match String.Map.find_opt "VERSION_ID" info with | None -> Fut.return "unknown" | Some v -> Fut.return v let macos_version s m file = let file = Fpath.(Store.dir s / file) in let sw_vers = (Memo.tool m Tool.sw_vers) (Cmd.atom "-productVersion") in read_spawn_stdout m file sw_vers let windows_version s m file = let file = Fpath.(Store.dir s / file) in let cmd_exe = (Memo.tool m Tool.cmd_exe) Cmd.(atom "/c" % "ver") in let* s = read_spawn_stdout m file cmd_exe in (* Format is 'Product [Version XXXX]', we parse the XXX *) let err m s = Memo.notify m `Warn "@[<v>Could not parse %a output %S." Fmt.(code string) "ver" s; Fut.return "unknown" in let last = String.length s - 2 in if last < 0 then err m s else match String.rindex_opt s ' ' with | None -> err m s | Some i -> Fut.return (String.subrange ~first:(i + 1) ~last s) let version = let mark = "b00_os.version" in let det s m = Fut.bind (Store.get s name) @@ function | "freebsd" -> freebsd_version s m mark | "linux" -> linux_version s m mark | "macos" -> macos_version s m mark | "windows" -> windows_version s m mark | _ -> Fut.bind (Store.get s uname) @@ function | Some (_, r, _) -> Fut.return (String.trim r) | None -> Fut.return "unknown" in Store.key ~mark det (* distribution *) let linux_distribution s m = let* info = Store.get s os_release in match String.Map.find_opt "ID" info with | None -> Fut.return "unknown" | Some v -> Fut.return v let macos_distribution s m = let* brew = Memo.tool_opt m Tool.brew in match brew with | Some _ -> Fut.return "homebrew" | None -> let* port = Memo.tool_opt m Tool.port in match port with | Some _ -> Fut.return "macports" | None -> Fut.return "macos" let distribution = let det s m = Fut.bind (Store.get s name) @@ function | "linux" -> linux_distribution s m | "macos" -> macos_distribution s m | n -> Fut.return n in Store.key ~mark:"b00_os.distribution" det (* family *) let linux_family s m = let* info = Store.get s os_release in match String.Map.find_opt "ID_LIKE" info with | None -> Store.get s distribution | Some v -> Fut.return @@ match String.cut_left ~sep:" " v with None -> v | Some (f, _) -> f let family = let det s m = Fut.bind (Store.get s name) @@ function | "linux" -> linux_family s m | "freebsd" | "openbsd" | "netbsd" | "dragonfly" -> Fut.return "bsd" | "windows" | "cygwin" -> Fut.return "windows" | n -> Fut.return n in Store.key ~mark:"b00_os.family" det (* Executable file extension *) let exe_ext = let ext_of_name = function "windows" -> ".exe" | _ -> "" in let det s m = Fut.map ext_of_name (Store.get s name) in Store.key ~mark:"b00_os.exe_ext" det (* Machine architecture *) let arch = let ret a = if a = "" then "unknown" else String.Ascii.lowercase a in let det s m = Fut.bind (Store.get s uname) @@ function | Some (_, _, a) -> Fut.return (ret a) | None -> let env = Env.env (Memo.env m) in match String.Map.find "PROCESSOR_ARCHITECTURE" env with | exception Not_found -> Fut.return "unknown" | "x86" as a -> begin match String.Map.find "PROCESSOR_ARCHITEW6432" env with | exception Not_found -> Fut.return a | a -> Fut.return (ret a) end | a -> Fut.return (ret a) in Store.key ~mark:"b00_os.arch" det let arch_normalized = let normalize = function | "x86" | "i386" | "i586" | "i686" -> "x86_32" | "x86_64" | "amd64" -> "x86_64" | "powerpc" | "ppc" | "ppcle" -> "ppc32" | "ppc64" | "ppc64le" -> "ppc64" | "aarch64_be" | "aarch64" -> "arm64" | "armv8b" | "armv8l" -> "arm32" | a when String.(starts_with ~prefix:"armv5" a || starts_with ~prefix:"armv6" a || starts_with ~prefix:"earmv6" a || starts_with ~prefix:"armv7" a || starts_with ~prefix:"earmv7" a) -> "arm32" | a -> a in let det s m = Fut.map normalize (Store.get s arch) in Store.key ~mark:"b00_os.arch_normalized" det let arch_bits = let default = 64 in let det s m = Fut.bind (Store.get s arch_normalized) @@ function | "x86_64" | "ppc64" | "ia64" | "arm64" | "sparc64" | "mips64"-> Fut.return 64 | "x86_32" | "ppc32" | "arm32" -> Fut.return 32 | a -> Memo.notify m `Warn "Unknown word size for arch %a. Using %d bits." Fmt.(code string) a default; Fut.return default in Store.key ~mark:"b00_os.arch_bits" det (*--------------------------------------------------------------------------- Copyright (c) 2020 The b0 programmers Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ---------------------------------------------------------------------------*)
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>