File 4705-Make-build-installer-work-from-WSL.patch of Package erlang
From 5a06938c904479e5dcb5035e67fc8f8bc01faf35 Mon Sep 17 00:00:00 2001
From: Dan Gudmundsson <dgud@erlang.org>
Date: Thu, 7 Nov 2019 12:33:42 +0100
Subject: [PATCH 5/6] Make build installer work from WSL
Support MSVC compiler and redist from 'Visual Studio 2019'.
Also add support for NSIS 3.0.
---
 erts/etc/win32/nsis/Makefile              | 26 +++++++++-------
 erts/etc/win32/nsis/dll_version_helper.sh | 19 ++++++++----
 erts/etc/win32/nsis/find_redist.sh        | 50 ++++++++++++++-----------------
 3 files changed, 50 insertions(+), 45 deletions(-)
diff --git a/erts/etc/win32/nsis/Makefile b/erts/etc/win32/nsis/Makefile
index c541d2b293..4c5e5325b3 100644
--- a/erts/etc/win32/nsis/Makefile
+++ b/erts/etc/win32/nsis/Makefile
@@ -23,7 +23,7 @@ include $(ERL_TOP)/make/$(TARGET)/otp.mk
 include $(ERL_TOP)/erts/vsn.mk
 
 VERSION_HEADER = erlang.nsh
-MAKENSIS = makensis
+MAKENSIS = makensis.exe
 MAKENSISFLAGS = /V2
 CUSTOM_MODERN=custom_modern.exe
 
@@ -53,19 +53,23 @@ WTARGET_DIR=$(shell (w32_path.sh -d "$(TARGET_DIR)"))
 
 ifeq ($(CONFIG_SUBTYPE),win64)
   WINTYPE=win64
+  REDIST_TARGET=vcredist_x64.exe
 else
   WINTYPE=win32
+  REDIST_TARGET=vcredist_x86.exe
 endif
 
-REDIST_FILE=$(shell (sh ./find_redist.sh || echo ""))
-NICEREDISTFILE=$(shell (w32_path.sh -d "$(REDIST_FILE)" 2>/dev/null || echo ""))
-
-REDIST_TARGET=$(shell (sh ./find_redist.sh -n || echo ""))
-REDIST_DLL_VERSION=$(shell (sh ./dll_version_helper.sh $(NICEREDISTFILE) || echo ""))
-REDIST_DLL_NAME=$(shell (sh ./dll_version_helper.sh -n $(NICEREDISTFILE) || echo ""))
+REDIST_FILE=$(shell (sh ./find_redist.sh $(WINTYPE) || echo ""))
+NICEREDISTFILE=$(shell (w32_path.sh -m "$(REDIST_FILE)" 2>/dev/null || echo "NOTFOUND"))
+# $(info $$NICEREDISTFILE = [${NICEREDISTFILE}])
+REDIST_DLL_VERSION=$(shell (sh ./dll_version_helper.sh "$(NICEREDISTFILE)" || echo ""))
+REDIST_DLL_NAME=$(shell (sh ./dll_version_helper.sh -n "$(NICEREDISTFILE)" || echo ""))
+# $(info $$REDIST_DLL_VERSION = [${REDIST_DLL_VERSION}])
+# $(info $$REDIST_DLL_NAME = [${REDIST_DLL_NAME}])
+# $(info $$REDIST_FILE = [${REDIST_FILE}])
 
 release_spec:
-	@NSIS_VER=`makensis /hdrinfo | head -1 | awk '{print $$2}'`; \
+	@NSIS_VER=`makensis.exe -version`; \
 	case $$NSIS_VER in \
 	v2.0b*) \
 	  echo '!define MUI_VERSION "$(SYSTEM_VSN)"' > $(VERSION_HEADER);\
@@ -77,7 +81,7 @@ release_spec:
 		echo '!define HAVE_DOCS 1' >> $(VERSION_HEADER); \
 	  fi;\
 	  $(MAKENSIS) erlang.nsi;;\
-	v2.*) \
+	v2.* | v3.*) \
 	  echo '!define OTP_VERSION "$(SYSTEM_VSN)"' > $(VERSION_HEADER);\
 	  echo '!define ERTS_VERSION "$(VSN)"' >> $(VERSION_HEADER);\
 	  echo '!define TESTROOT "$(WTESTROOT)"' >> $(VERSION_HEADER);\
@@ -89,7 +93,7 @@ release_spec:
 	  fi;\
 	  if [ '!' -z "$(REDIST_FILE)" -a '!' -z "$(REDIST_DLL_VERSION)" ];\
 	  then \
-		cp $(REDIST_FILE) "$(RELEASE_PATH)/$(REDIST_TARGET)";\
+		cp "$(REDIST_FILE)" "$(RELEASE_PATH)/$(REDIST_TARGET)";\
 		echo '!define HAVE_REDIST_FILE 1'  >> $(VERSION_HEADER); \
 		echo '!define REDIST_DLL_VERSION "$(REDIST_DLL_VERSION)"' >> $(VERSION_HEADER);\
 		echo '!define REDIST_DLL_NAME "$(REDIST_DLL_NAME)"' >> $(VERSION_HEADER);\
@@ -102,7 +106,7 @@ release_spec:
 	  echo "Running $(MAKENSIS) $(MAKENSISFLAGS) erlang20.nsi";\
 	  $(MAKENSIS) $(MAKENSISFLAGS) erlang20.nsi;;\
 	*) \
-	  echo 'Unsupported NSIS version';;\
+	  echo "Unsupported NSIS version: $$NSIS_VER";;\
 	esac	
 
 release_docs release_docs_spec docs:
diff --git a/erts/etc/win32/nsis/dll_version_helper.sh b/erts/etc/win32/nsis/dll_version_helper.sh
index 9eafb6ce0e..1b295aa89a 100755
--- a/erts/etc/win32/nsis/dll_version_helper.sh
+++ b/erts/etc/win32/nsis/dll_version_helper.sh
@@ -44,12 +44,16 @@ int main(void)
 }
 
 EOF
-cl -MD hello.c  > /dev/null 2>&1
+cl.exe -MD hello.c  > /dev/null 2>&1
 if [ '!' -f hello.exe.manifest ]; then
     # Gah - VC 2010 changes the way it handles DLL's and manifests... Again...
     # need another way of getting the version
     DLLNAME=`dumpbin.exe -imports hello.exe | egrep MSVCR.*dll`
     DLLNAME=`echo $DLLNAME`
+    if [ -z "$DLLNAME" ]; then
+        DLLNAME=`dumpbin.exe -imports hello.exe | egrep VCRUNTIME.*dll`
+	DLLNAME=`echo $DLLNAME`
+    fi
     if [ '!' -z "$1" ]; then
 	FILETOLOOKIN=$1
     else
@@ -92,22 +96,25 @@ int main(void)
   for(i=0; i < n; ++i) {
     sprintf(buff,"\\\\StringFileInfo\\\\%04x%04x\\\\FileVersion",
 	    translate[i*2],translate[i*2+1]);
-    if (VerQueryValue(versinfo,buff,&vs_verinfo,&vs_ver_size)) {
-      printf("%s\n",(char *) vs_verinfo);
-      return 0;
+    if (VerQueryValue(versinfo,buff,&vs_verinfo,&vs_ver_size) && vs_ver_size > 2) {
+        if(vs_verinfo[1] == 0) // Wide char (depends on compiler version!!)
+            printf("%S\n",(unsigned short *) vs_verinfo);
+        else 
+            printf("%s\n",(char *) vs_verinfo);
+        return 0;
     }
   } 
   fprintf(stderr,"Failed to find file version of %s\n",REQ_MODULE);
   return 0;
 }
 EOF
-    cl -MD helper.c version.lib > /dev/null 2>&1
+    cl.exe -MD helper.c version.lib > /dev/null 2>&1
     if [ '!' -f helper.exe ]; then
 	echo "Failed to build helper program." >&2
 	exit 1
     fi
     NAME=$DLLNAME
-    VERSION=`./helper`
+    VERSION=`./helper.exe`
 else
     VERSION=`grep '<assemblyIdentity' hello.exe.manifest | sed 's,.*version=.\([0-9\.]*\).*,\1,g' | grep -v '<'`
     NAME=`grep '<assemblyIdentity' hello.exe.manifest | sed 's,.*name=.[A-Za-z\.]*\([0-9]*\).*,msvcr\1.dll,g' | grep -v '<'`
diff --git a/erts/etc/win32/nsis/find_redist.sh b/erts/etc/win32/nsis/find_redist.sh
index c070ad469a..deb5480e1a 100755
--- a/erts/etc/win32/nsis/find_redist.sh
+++ b/erts/etc/win32/nsis/find_redist.sh
@@ -26,17 +26,9 @@ lookup_prog_in_path ()
     save_ifs=$IFS
     IFS=:
     for p in $PATH; do
-	# In cygwin the programs are not always executable and have .exe suffix...
-	if [ "X$TARGET" = "Xwin32" ]; then
-	    if [ -f $p/$PROG.exe ]; then
-		echo $p/$PROG
-		break;
-	    fi
-	else
-	    if [ -x $p/$PROG ]; then
-		echo $p/$PROG
-		break;
-	    fi
+	if [ -f $p/$PROG.exe ]; then
+	    echo $p/$PROG
+	    break;
 	fi
     done
     IFS=$save_ifs
@@ -89,36 +81,38 @@ add_path_element()
     echo "$PA"
 }
 
-
-CLPATH=`lookup_prog_in_path cl`
-
-if [ -z "$CLPATH" ]; then 
-    echo "Can not locate cl.exe and vcredist_x86/x64.exe - OK if using mingw" >&2
-    exit 1
-fi
-
 # Look to see if it's 64bit
-XX=`remove_path_element cl "$CLPATH"`
-YY=`remove_path_element amd64 "$XX"`
-if [ "$YY" != "$XX" ]; then
+if [ "$1" = "win64" ]; then
     AMD64DIR=true
     VCREDIST=vcredist_x64
     COMPONENTS="cl amd64 bin vc"
-else
+elif [ "$1" = "win32" ]; then
     AMD64DIR=false
     VCREDIST=vcredist_x86
     COMPONENTS="cl bin vc"
+else
+    echo "TARGET argument should win32 or win64"
+    exit 2
 fi
 
-if [ X"$1" = X"-n" ]; then
-    echo $VCREDIST.exe
-    exit 0
+if [ x"$VCToolsRedistDir" != x"" ]; then
+    File="$VCToolsRedistDir/$VCREDIST.exe"
+    if [ -r "$File" ]; then
+	echo "$File"
+	exit 0
+    fi
+fi
+
+CLPATH=`lookup_prog_in_path cl`
+if [ -z "$CLPATH" ]; then 
+    echo "Can not locate cl.exe and vcredist_x86/x64.exe - OK if using mingw" >&2
+    exit 1
 fi
 
-# echo $CLPATH
+echo $CLPATH
 BPATH=$CLPATH
 for x in $COMPONENTS; do
-    # echo $x
+    #echo $x
     NBPATH=`remove_path_element $x "$BPATH"`
     if [ "$NBPATH" = "$BPATH" ]; then
 	echo "Failed to locate $VCREDIST.exe because cl.exe was in an unexpected location" >&2
-- 
2.16.4