File 4702-Setup-VC-env-in-otp_build.patch of Package erlang

From 73d3d02d887408e99489a9df3877dd409021cc82 Mon Sep 17 00:00:00 2001
From: Dan Gudmundsson <dgud@erlang.org>
Date: Mon, 11 Nov 2019 15:43:49 +0100
Subject: [PATCH 2/6] Setup VC env in otp_build

If there is no cl.exe in path try to setup the paths and the
needed env variables.

Use vcvarsall.bat from VC so we get the correct path and libraries.
Search some default installations paths
---
 erts/etc/win32/wsl_tools/SetupWSLcross.bat | 60 ++++++++++++++++++++++++++++++
 otp_build                                  | 27 +++++++++++++-
 2 files changed, 85 insertions(+), 2 deletions(-)
 create mode 100644 erts/etc/win32/wsl_tools/SetupWSLcross.bat

diff --git a/erts/etc/win32/wsl_tools/SetupWSLcross.bat b/erts/etc/win32/wsl_tools/SetupWSLcross.bat
new file mode 100644
index 0000000000..d29400f133
--- /dev/null
+++ b/erts/etc/win32/wsl_tools/SetupWSLcross.bat
@@ -0,0 +1,60 @@
+@echo off
+rem Setup MCL and echo the environment
+rem Usage: eval `cmd.exe /c SetupWSLcross.bat x64`
+
+IF "%~1"=="x86" GOTO search
+IF "%~1"=="x64" GOTO search
+
+GOTO badarg
+
+:search
+IF EXIST "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat". (
+   call "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" %~1 > nul
+   goto continue
+)
+
+IF EXIST "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat". (
+   call "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" %~1 > nul
+   goto continue
+)
+
+IF EXIST "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvarsall.bat". (
+   call "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" %~1 > nul
+   goto continue
+)
+
+IF EXIST "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat". (
+   call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" %~1 > nul
+   goto continue
+)
+
+GOTO no_vcvars
+
+:continue
+
+FOR /F "delims==" %%F IN ('where cl.exe') DO SET _cl_exec_=%%F
+FOR %%F IN ("%_cl_exec_%") DO SET CL_PATH=%%~dpF
+
+FOR /F "delims==" %%F IN ('where rc.exe') DO SET _rc_exec_=%%F
+FOR %%F IN ("%_rc_exec_%") DO SET RC_PATH=%%~dpF
+
+rem Order is important for some unknown reason
+set WSLENV=VCToolsRedistDir/up:CL_PATH/up:RC_PATH/up:LIBPATH/ul:LIB/ul:INCLUDE/ul
+wsl.exe echo INCLUDE=\"$INCLUDE\";
+wsl.exe echo LIB=\"$LIB\";
+wsl.exe echo LIBPATH=\"$LIBPATH\";
+wsl.exe echo VCToolsRedistDir=\"$VCToolsRedistDir\";
+wsl.exe echo PATH=\"$CL_PATH\":\"$RC_PATH\":'$PATH';
+wsl.exe echo WSLENV='$WSLENV:LIBPATH/l:LIB/l:INCLUDE/l';
+rem wsl.exe echo export 'INCLUDE LIB LIBPATH VCToolsRedistDir WSLENV PATH';
+wsl.exe echo "# Eval this file eval \`cmd.exe /c SetupWSLcross.bat\`"
+
+exit
+
+:badarg
+echo "Bad TARGET or not specified: %~1 expected x86 or x64"
+exit
+
+:no_vcvars
+echo "Error: SetupWSLcross.bat: Could not find vcvarsall.bat"
+exit
diff --git a/otp_build b/otp_build
index ed00a3ed7e..8d9854614e 100755
--- a/otp_build
+++ b/otp_build
@@ -777,6 +777,13 @@ echo_env_wsl ()
 
     WIN32_WRAPPER_PATH="$ERL_TOP/erts/etc/win32/wsl_tools/vc:$ERL_TOP/erts/etc/win32/wsl_tools"
 
+    if [ ! -n "`lookup_prog_in_path cl`" ]; then
+        if [ X"$X64" = X"true" ]; then
+            setup_win32_cl_env "x64" `wslpath -a -m erts/etc/win32/wsl_tools/SetupWSLcross.bat`
+        else
+            setup_win32_cl_env "x86" `wslpath -a -m erts/etc/win32/wsl_tools/SetupWSLcross.bat`
+        fi
+    fi
     echo_setenv OVERRIDE_TARGET win32 ';'
     if [ X"$X64" = X"true" ]; then
         echo_setenv CONFIG_SUBTYPE win64 ';'
@@ -802,6 +809,17 @@ echo_env_wsl ()
     echo_envinfo
 }
 
+
+setup_win32_cl_env ()
+{
+    eval `cmd.exe /c $2 $1`
+    echo_setenv INCLUDE "$INCLUDE" ';'
+    echo_setenv LIB "$LIB" ';'
+    echo_setenv LIBPATH "$LIBPATH" ';'
+    echo_setenv VCToolsRedistDir "$VCToolsRedistDir" ';'
+    echo_setenv WSLENV "$WSLENV" ';'
+}
+
 lookup_prog_in_path ()
 {
     PROG=$1
@@ -1092,8 +1110,13 @@ case $TARGET in
 		'the command'
 	    exit 1
 	fi;;
-	*)
-	;;
+    *)
+        if [ -x /bin/wslpath -a X"$OVERRIDE_TARGET" = X"" \
+                -a X"$1" != X"env_win32" -a X"$1" != X"env_msys32" -a X"$1" != X"env_msys64" ]; then
+            echo "Building linux binary; if you intended to cross build for win32 use"  >&2
+            echo '   eval `./otp_build env_win32`\n' >&2
+        fi
+        ;;
 esac
 
 if [ ! -z "$OVERRIDE_TARGET" ]; then
-- 
2.16.4

openSUSE Build Service is sponsored by