#!/usr/bin/env bash
# MSVC 2017
# MSVC_BUILD_TOOLS="C:\\Program^ Files^ ^(x86^)\\Microsoft^ Visual^ Studio\\2017\\BuildTools\\VC\\Auxiliary\\Build\\vcvarsall.bat"
# MSVC 2015
MSVC_BUILD_TOOLS="C:\\Program^ Files^ ^(x86^)\\Microsoft^ Visual^ C++^ Build^ Tools\\vcbuildtools.bat"
set -euo pipefail
# Catching the result of the program does not play nice with the progress output. Otherwise we could use this:
# linker_command=$(bin/crystal build --cross-compile --target x86_64-unknown-windows-msvc "${@}")
# name=$(echo $linker_command | sed -E 's/.*\/Fe([^ "]+).*/\1/')
# Cross-compile Crystal program
bin/crystal build --progress --cross-compile --target x86_64-unknown-windows-msvc "${@}"
name=$(basename "${1%.*}")
linker_command="cl ${name}.o /Fe${name} pcre.lib gc.lib libcmt.lib"
# user32.lib should not be required but libgc expects it
linker_command="${linker_command} user32.lib"
echo
# Run the msvc build tools to link final executable through Windows interoperability
cmd.exe /S /c "${MSVC_BUILD_TOOLS} amd64 && cd /D %CD% && ${linker_command}"
echo
echo "./${name}.exe"
# Run the compiled executable through Windows interoperability
./${name}.exe