123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352(*
* Copyright (c) 2020 - 2021 Tarides - Antonin Décimo <antonin@tarides.com>
*
* Permission to use, copy, modify, and 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.
*
*)openDockerfileopenPrintfletrun_cmdfmt=ksprintf(run"cmd /S /C %s")fmtletrun_powershell?(escape=Fun.id)fmt=ksprintf(funs->run{|powershell -Command "%s"|}(escapes))fmtletrun_vc~archfmt=letarch=matcharchwith|`I386->"x86"|`X86_64->"amd64"|`Aarch64|`Aarch32|`Ppc64le|`S390x|`Riscv64->invalid_arg"Unsupported architecture"inksprintf(run{|cd C:\BuildTools\VC\Auxiliary\Build && vcvarsall.bat %s && %s|}arch)fmtletrun_ocaml_envargsfmt=ksprintf(run{|ocaml-env exec %s -- %s|}(String.concat" "args))fmtletcleanupt=t@@run_powershell{|Remove-Item 'C:\TEMP' -Recurse|}|>crunch(* VC redist is needed to run WinGet. *)letinstall_vc_redist?(vs_version="16")()=add~src:["https://aka.ms/vs/"^vs_version^"/release/vc_redist.x64.exe"]~dst:{|C:\TEMP\|}()@@run{|C:\TEMP\vc_redist.x64.exe /install /passive /norestart /log C:\TEMP\vc_redist.log|}|>cleanupletinstall_visual_studio_build_tools?(vs_version="16")components=letinstall=letfmt=format_of_string{|C:\TEMP\Install.cmd C:\TEMP\vs_buildtools.exe --quiet --wait --norestart --nocache `
--installPath C:\BuildTools --channelUri C:\TEMP\VisualStudio.chman `
--installChannelUri C:\TEMP\VisualStudio.chman%s|}inrunfmt(List.fold_left(funacccomponent->acc^" `\n --add "^component)""components)in(* https://docs.microsoft.com/en-us/visualstudio/install/advanced-build-tools-container?view=vs-2019#install-script *)add~src:["https://raw.githubusercontent.com/avsm/ocaml-dockerfile/master/src-opam/Install.cmd";]~dst:{|C:\TEMP\|}()@@add~src:["https://aka.ms/vscollect.exe"]~dst:{|C:\TEMP\collect.exe|}()@@add~src:["https://aka.ms/vs/"^vs_version^"/release/channel"]~dst:{|C:\TEMP\VisualStudio.chman|}()@@add~src:["https://aka.ms/vs/"^vs_version^"/release/vs_buildtools.exe"]~dst:{|C:\TEMP\vs_buildtools.exe|}()@@installletheader~alias?win10_revision?(version=(Distro.win10_latest_image:Distro.win10_release:>Distro.win_all))()=letimg,tag=Distro.win10_base_tag?win10_revision`Windowsversioninparser_directive(`Escape'`')@@from~alias~tagimg@@user"ContainerAdministrator"letsanitize_reg_path()=run{|for /f "tokens=1,2,*" %%a in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /V Path ^| findstr /r "\\$"') do `
for /f "delims=" %%l in ('cmd /v:on /c "set v=%%c&& echo !v:~0,-1!"') do `
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /V Path /t REG_EXPAND_SZ /f /d "%%l"|}letprepend_pathpaths=letpaths=String.concat";"pathsinrun{|for /f "tokens=1,2,*" %%a in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /V Path ^| findstr /r "^[^H]"') do `
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /V Path /t REG_EXPAND_SZ /f /d "%s;%%c"|}pathsletremove_system_attribute?(recurse=true)path=run_powershell{|Foreach($file in Get-ChildItem -Path '%s' %s -force) { `
If (((Get-ItemProperty -Path $file.fullname).attributes -band [io.fileattributes]::System)) { `
Set-ItemProperty -Path $file.fullname -Name attributes -Value ((Get-ItemProperty $file.fullname).attributes -BXOR [io.fileattributes]::System) `
} `
} #end Foreach|}path(ifrecursethen"-Recurse"else"")letocaml_for_windows_package_exn~switch~port~arch=letvariant=letbitness=ifOcaml_version.arch_is_32bitarchthen"32"else"64"inmatcharchwith|`X86_64|`I386->(matchportwith`Mingw->"mingw"|`Msvc->"msvc")^bitness|_->invalid_arg"Unsupported architecture"inlet_,pkgver=Ocaml_version.Opam.V2.packageswitchin("ocaml-variants",pkgver^"+"^variant)letgit_init~name~email~opam_repository=String.concat" && "[sprintf"git config --global user.email '%s'"email;sprintf"git config --global user.name '%s'"name;"git config --system core.longpaths true";sprintf"git config --global --add safe.directory %s"opam_repository;]moduleCygwin=structtypecyg={root:string;site:string;args:stringlist}letcygcache={|C:\TEMP\cache|}letdefault={root={|C:\cygwin64|};site="https://mirrors.kernel.org/sourceware/cygwin/";args=["--quiet-mode";"--no-shortcuts";"--no-startmenu";"--no-desktop";"--only-site";"--local-package-dir";cygcache;];}letrun_sh?(cyg=default)fmt=ksprintf(run{|%s\bin\bash.exe --login -c "%s"|}cyg.root)fmtletrun_sh_ocaml_env?(cyg=default)argsfmt=ksprintf(run_sh~cyg"ocaml-env exec %s -- %s"(String.concat" "args))fmtletinstall_cygsympathy_from_sourcecyg=run{|mkdir %s\lib\cygsympathy && mkdir %s\etc\postinstall|}cyg.rootcyg.root@@add~src:["https://raw.githubusercontent.com/metastack/cygsympathy/master/cygsympathy.cmd";]~dst:(cyg.root^{|\lib\cygsympathy\|})()@@add~src:["https://raw.githubusercontent.com/metastack/cygsympathy/master/cygsympathy.sh";]~dst:(cyg.root^{|\lib\cygsympathy\cygsympathy|})()(* Beware: CygSymPathy must be executed last, or it may miss files
installed by other post-install scripts. Use a name that is
greater than every other script in the lexicographic order. *)@@run{|mklink %s\etc\postinstall\zp_zcygsympathy.sh %s\lib\cygsympathy\cygsympathy|}cyg.rootcyg.rootletinstall_msvs_tools_from_source?(version="0.4.1")cyg=add~src:["https://github.com/metastack/msvs-tools/archive/"^version^".tar.gz";]~dst:{|C:\TEMP\msvs-tools.tar.gz|}()@@run_sh~cyg{|cd /tmp && tar -xf /cygdrive/c/TEMP/msvs-tools.tar.gz && cp msvs-tools-%s/msvs-detect msvs-tools-%s/msvs-promote-path /bin|}versionversionletcygsetup?(cyg=default)?(upgrade=false)fmt=ksprintf(run{|%s\setup-x86_64.exe %s --root %s --site %s --symlink-type=native %s%s|}cyg.root(String.concat" "cyg.args)cyg.rootcyg.site(ifupgradethen" --upgrade-also"else""))fmtletinstall?(cyg=default)pkgs=cygsetup~cyg"--packages %s"(pkgs|>List.sort_uniqString.compare|>String.concat",")|>cleanupletupdate?(cyg=default)()=cygsetup~cyg~upgrade:true""|>cleanupletsetup_env~cyg=env[("CYGWIN","winsymlinks:native")]@@prepend_path(List.map((^)cyg.root)[{|\bin|}])letinstall_from_release?(cyg=default)?(extra=[])()=setup_env~cyg@@add~src:["https://www.cygwin.com/setup-x86_64.exe"]~dst:(cyg.root^{|\setup-x86_64.exe|})()@@install_cygsympathy_from_sourcecyg@@install~cygextra@@install_msvs_tools_from_sourcecyg@@run{|awk -i inplace "/(^#)|(^$)/{print;next}{$4=""noacl,""$4; print}" %s\etc\fstab|}cyg.root@@remove_system_attribute(cyg.root^{|\dev|})letsetup?(cyg=default)?from()=(matchfromwith|Somefrom->copy~from~src:[default.root]~dst:default.root()@@setup_env~cyg|>cleanup|None->empty)@@workdir{|%s\home\opam|}cyg.rootletcygwin_packages?(extra=[])?(flexdll_version="0.39-1")()=(* 2021-03-19: flexdll 0.39 is required, but is in Cygwin testing *)"make"::"diffutils"::"ocaml"::"gcc-core"::"git"::"patch"::"m4"::"cygport"::("flexdll="^flexdll_version)::extraletmingw_packages?(extra=[])()="make"::"diffutils"::"mingw64-x86_64-gcc-core"::extraletmsvc_packages?(extra=[])()="make"::"diffutils"::extraletocaml_for_windows_packages?cyg?(extra=[])?(version="0.0.0.2")()=letpackages="make"::"diffutils"::"mingw64-x86_64-gcc-g++"::"vim"::"git"::"curl"::"rsync"::"unzip"::"patch"::"m4"::extrainlett=add~src:["https://github.com/fdopen/opam-repository-mingw/releases/download/"^version^"/opam64.tar.xz";]~dst:{|C:\TEMP\|}()@@run_sh?cyg{|cd /tmp && tar -xf /cygdrive/c/TEMP/opam64.tar.xz && ./opam64/install.sh --prefix=/usr && rm -rf opam64 opam64.tar.xz|}in(packages,t)moduleGit=structletinit?(cyg=default)?(name="Docker")?(email="docker@example.com")()=env[("HOME",cyg.root^{|\home\opam|})]@@run_sh~cyg"%s"(git_init~email~name~opam_repository:"/home/opam/opam-repository")endendmoduleWinget=structletis_supportedversion=not(List.memversion[`V1507;`Ltsc2015;`V1511;`V1607;`Ltsc2016;`V1703;`V1709;`V1803;])letfooterpath=run{|mkdir "C:\Program Files\winget-cli"|}@@run{|move "C:\TEMP\winget-cli\%s\winget.exe" "C:\Program Files\winget-cli\"|}path@@run{|move "C:\TEMP\winget-cli\%s\WindowsPackageManager.dll" "C:\Program Files\winget-cli\"|}path@@run{|move "C:\TEMP\winget-cli\%s\resources.pri" "C:\Program Files\winget-cli\"|}path|>crunchletinstall_from_release?winget_version()=letfile="Microsoft.DesktopAppInstaller_8wekyb3d8bbwe."inletsrc=letsrc="https://github.com/microsoft/winget-cli/releases/"inmatchwinget_versionwith|None->src^"latest/download/"^file^"msixbundle"|Somever->src^"download/"^ver^"/"^file^"msixbundle"inletdst={|C:\TEMP\|}^file^"zip"inadd~src:[src]~dst()@@run_powershell{|Expand-Archive -LiteralPath %s -DestinationPath C:\TEMP\winget-cli -Force|}dst@@run{|ren C:\TEMP\winget-cli\AppInstaller_x64.msix AppInstaller_x64.zip|}@@run_powershell{|Expand-Archive -LiteralPath C:\TEMP\winget-cli\AppInstaller_x64.zip -DestinationPath C:\TEMP\winget-cli\ -Force|}@@footer""letsetup?from()=letescapes=String.(concat{|""""|}(split_on_char'"'s))in(matchfromwith|Somefrom->copy~from~src:[{|C:\Program Files\winget-cli|}]~dst:{|C:\Program Files\winget-cli|}()|None->empty)@@prepend_path[{|C:\Program Files\winget-cli|}]@@run_powershell~escape{|$path=(Join-Path $env:LOCALAPPDATA 'Packages\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\LocalState'); New-Item $path -ItemType Directory -Force; '{ "$schema": "https://aka.ms/winget-settings.schema.json", "telemetry": { "disable": "true" } }' | Out-File -encoding ASCII (Join-Path $path 'settings.json')|}letinstallpkgs=List.fold_left(funaccpkg->acc@@run"winget install --exact --accept-source-agreements \
--accept-package-agreements %s"pkg)emptypkgsletdev_packages?version?extra()=matchversionwith(* 2021-04-01: Installing git fails with exit-code 2316632065. *)|Some`V1809->maybeinstallextra|_->install["Git.Git"]@@maybeinstallextramoduleGit=structletinit?(name="Docker")?(email="docker@example.com")()=run"%s"(git_init~email~name~opam_repository:"C:/cygwin64/home/opam/opam-repository")endend