123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278(*---------------------------------------------------------------------------
Copyright (c) 2020 The b0 programmers. All rights reserved.
Distributed under the ISC license, see terms at the end of the file.
---------------------------------------------------------------------------*)openB0_stdopenB0_std.Fut.SyntaxopenB00letread_spawn_stdoutmfilecmd=Memo.spawnm~writes:[file]~stdout:(`Filefile)cmd;Fut.mapString.trim(Memo.readmfile)letrecfind_first_existing_file=function|[]->None|f::fs->matchOs.File.existsf|>Log.if_error~use:falsewith|true->Somef|false->find_first_existing_filefs(* A few tools used by this module *)moduleTool=structletbrew=Tool.by_name"brew"letcmd_exe=Tool.by_name"cmd.exe"(* windows *)letgetprop=Tool.by_name"getprop"(* android *)letport=Tool.by_name"port"letsw_vers=Tool.by_name"sw_vers"(* macos *)letuname=Tool.by_name"uname"end(* Store keys for os-release and uname, not exposed but used below *)letos_release=(* Access https://www.freedesktop.org/software/systemd/man/os-release.html *)letunquotes=letquotedslen=s.[0]='"'&&s.[len-1]='"'inmatchString.lengthswith|0|1->s|lenwhenquotedslen->String.subranges~first:1~last:(len-2)|len->sinletparse_lineilineacc=matchString.length(String.trimline)with|0->acc|_whenline.[0]='#'->acc|_->matchString.cut_left~sep:"="linewith|None->B00_lines.erri"Cannot find %a char in %S"Fmt.(codechar)'='line|Some(k,v)->String.Map.add(String.trimk)(unquote(String.trimv))accinletdetsm=letfiles=[Fpath.v"/etc/os-release";Fpath.v"/usr/lib/os-release"]inmatchfind_first_existing_filefileswith|None->Fut.returnString.Map.empty|Somefile->Memo.file_readymfile;let*s=Memo.readmfileinletmap=B00_lines.folds~fileparse_lineString.Map.emptyinletmap=Memo.notify_if_errorm`Warn~use:String.Map.emptymapinFut.returnmapinStore.key~mark:"B00_os.os_release_file"detletuname=(* gets system name, release version, machine arch *)letmark="b00_os.uname"inletdetsm=let*tool=Memo.tool_optmTool.unameinmatchtoolwith|None->Fut.returnNone|Someuname->letfile=Fpath.(Store.dirs/mark)inletuname=unameCmd.(atom"-s"%"-r"%"-m")inlet*s=read_spawn_stdoutmfileunameinmatchString.split_on_char' 'swith|[sys;rel;mach]->Fut.return(Some(sys,rel,mach))|_->Memo.notifym`Warn"Could not parse %a output %S."Fmt.(codestring)"uname"s;Fut.returnNoneinStore.key~markdet(* name *)letname=letof_unamem=function|None->letenv=Env.env(Memo.envm)inifString.Map.mem"COMSPEC"env||String.Map.mem"ComSpec"envthen"windows"else"unknown"|Some(s,_,_)->matchString.Ascii.lowercaseswith|"darwin"->"macos"|s->sinletdetsm=Fut.map(of_unamem)(Store.getsuname)inStore.key~mark:"b00_os.name"det(* version *)letfreebsd_versionsmfile=letfile=Fpath.(Store.dirs/file)inletuname=(Memo.toolmTool.uname)(Cmd.atom"-U")inread_spawn_stdoutmfileunamelettry_android_versionsmfile=let*getprop=Memo.tool_optmTool.getpropinmatchgetpropwith|None->Fut.returnNone|Somegetprop->letfile=Fpath.(Store.dirs/file)inletgetprop=getprop(Cmd.atom"ro.build.version.release")inFut.mapOption.some(read_spawn_stdoutmfilegetprop)letlinux_versionsmfile=Fut.bind(try_android_versionsmfile)@@function|Somev->Fut.returnv|None->Fut.bind(Store.getsos_release)@@functioninfo->matchString.Map.find_opt"VERSION_ID"infowith|None->Fut.return"unknown"|Somev->Fut.returnvletmacos_versionsmfile=letfile=Fpath.(Store.dirs/file)inletsw_vers=(Memo.toolmTool.sw_vers)(Cmd.atom"-productVersion")inread_spawn_stdoutmfilesw_versletwindows_versionsmfile=letfile=Fpath.(Store.dirs/file)inletcmd_exe=(Memo.toolmTool.cmd_exe)Cmd.(atom"/c"%"ver")inlet*s=read_spawn_stdoutmfilecmd_exein(* Format is 'Product [Version XXXX]', we parse the XXX *)leterrms=Memo.notifym`Warn"@[<v>Could not parse %a output %S."Fmt.(codestring)"ver"s;Fut.return"unknown"inletlast=String.lengths-2iniflast<0thenerrmselsematchString.rindex_opts' 'with|None->errms|Somei->Fut.return(String.subrange~first:(i+1)~lasts)letversion=letmark="b00_os.version"inletdetsm=Fut.bind(Store.getsname)@@function|"freebsd"->freebsd_versionsmmark|"linux"->linux_versionsmmark|"macos"->macos_versionsmmark|"windows"->windows_versionsmmark|_->Fut.bind(Store.getsuname)@@function|Some(_,r,_)->Fut.return(String.trimr)|None->Fut.return"unknown"inStore.key~markdet(* distribution *)letlinux_distributionsm=let*info=Store.getsos_releaseinmatchString.Map.find_opt"ID"infowith|None->Fut.return"unknown"|Somev->Fut.returnvletmacos_distributionsm=let*brew=Memo.tool_optmTool.brewinmatchbrewwith|Some_->Fut.return"homebrew"|None->let*port=Memo.tool_optmTool.portinmatchportwith|Some_->Fut.return"macports"|None->Fut.return"macos"letdistribution=letdetsm=Fut.bind(Store.getsname)@@function|"linux"->linux_distributionsm|"macos"->macos_distributionsm|n->Fut.returnninStore.key~mark:"b00_os.distribution"det(* family *)letlinux_familysm=let*info=Store.getsos_releaseinmatchString.Map.find_opt"ID_LIKE"infowith|None->Store.getsdistribution|Somev->Fut.return@@matchString.cut_left~sep:" "vwithNone->v|Some(f,_)->fletfamily=letdetsm=Fut.bind(Store.getsname)@@function|"linux"->linux_familysm|"freebsd"|"openbsd"|"netbsd"|"dragonfly"->Fut.return"bsd"|"windows"|"cygwin"->Fut.return"windows"|n->Fut.returnninStore.key~mark:"b00_os.family"det(* Executable file extension *)letexe_ext=letext_of_name=function"windows"->".exe"|_->""inletdetsm=Fut.mapext_of_name(Store.getsname)inStore.key~mark:"b00_os.exe_ext"det(* Machine architecture *)letarch=letreta=ifa=""then"unknown"elseString.Ascii.lowercaseainletdetsm=Fut.bind(Store.getsuname)@@function|Some(_,_,a)->Fut.return(reta)|None->letenv=Env.env(Memo.envm)inmatchString.Map.find"PROCESSOR_ARCHITECTURE"envwith|exceptionNot_found->Fut.return"unknown"|"x86"asa->beginmatchString.Map.find"PROCESSOR_ARCHITEW6432"envwith|exceptionNot_found->Fut.returna|a->Fut.return(reta)end|a->Fut.return(reta)inStore.key~mark:"b00_os.arch"detletarch_normalized=letnormalize=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"|awhenString.(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->ainletdetsm=Fut.mapnormalize(Store.getsarch)inStore.key~mark:"b00_os.arch_normalized"detletarch_bits=letdefault=64inletdetsm=Fut.bind(Store.getsarch_normalized)@@function|"x86_64"|"ppc64"|"ia64"|"arm64"|"sparc64"|"mips64"->Fut.return64|"x86_32"|"ppc32"|"arm32"->Fut.return32|a->Memo.notifym`Warn"Unknown word size for arch %a. Using %d bits."Fmt.(codestring)adefault;Fut.returndefaultinStore.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.
---------------------------------------------------------------------------*)