Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge jdk8u342-b02 #1

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions common/autoconf/generated-configure.sh
Original file line number Diff line number Diff line change
@@ -4446,7 +4446,7 @@ apt_help() {
pulse)
PKGHANDLER_COMMAND="sudo apt-get install libpulse-dev" ;;
x11)
PKGHANDLER_COMMAND="sudo apt-get install libX11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev" ;;
PKGHANDLER_COMMAND="sudo apt-get install libx11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev" ;;
ccache)
PKGHANDLER_COMMAND="sudo apt-get install ccache" ;;
esac
@@ -4841,7 +4841,7 @@ TOOLCHAIN_DESCRIPTION_xlc="IBM XL C/C++"


#
# Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
@@ -4867,7 +4867,7 @@ TOOLCHAIN_DESCRIPTION_xlc="IBM XL C/C++"

################################################################################
# The order of these defines the priority by which we try to find them.
VALID_VS_VERSIONS="2010 2012 2013 2015 2017"
VALID_VS_VERSIONS="2010 2012 2013 2015 2017 2019"

VS_DESCRIPTION_2010="Microsoft Visual Studio 2010"
VS_VERSION_INTERNAL_2010=100
@@ -4925,6 +4925,20 @@ VS_SDK_INSTALLDIR_2017=
VS_VS_PLATFORM_NAME_2017="v141"
VS_SDK_PLATFORM_NAME_2017=

VS_DESCRIPTION_2019="Microsoft Visual Studio 2019"
VS_VERSION_INTERNAL_2019=141
VS_MSVCR_2019=vcruntime140.dll
VS_MSVCP_2019=msvcp140.dll
VS_ENVVAR_2019="VS160COMNTOOLS"
VS_USE_UCRT_2019="true"
VS_VS_INSTALLDIR_2019="Microsoft Visual Studio/2019"
VS_EDITIONS_2019="BuildTools Community Professional Enterprise"
VS_SDK_INSTALLDIR_2019=
VS_VS_PLATFORM_NAME_2019="v142"
VS_SDK_PLATFORM_NAME_2019=
VS_SUPPORTED_2019=false
VS_TOOLSET_SUPPORTED_2019=false

################################################################################


@@ -4966,7 +4980,7 @@ VS_SDK_PLATFORM_NAME_2017=
#CUSTOM_AUTOCONF_INCLUDE

# Do not change or remove the following line, it is needed for consistency checks:
DATE_WHEN_GENERATED=1645701114
DATE_WHEN_GENERATED=1652906026

###############################################################################
#
@@ -27446,7 +27460,7 @@ printf "%s\n" "$as_me: Valid Visual Studio versions: $VALID_VS_VERSIONS." >&6;}
VS_BASE="$unix_path"
fi

# In VS 2017, the default installation is in a subdir named after the edition.
# In VS 2017 and VS 2019, the default installation is in a subdir named after the edition.
# Find the first one present and use that.
if test "x$VS_EDITIONS" != x; then
for edition in $VS_EDITIONS; do
@@ -27503,7 +27517,7 @@ printf "%s\n" "$as_me: Warning: None of $VCVARSFILES were found, Visual Studio i
VS_BASE="$unix_path"
fi

# In VS 2017, the default installation is in a subdir named after the edition.
# In VS 2017 and VS 2019, the default installation is in a subdir named after the edition.
# Find the first one present and use that.
if test "x$VS_EDITIONS" != x; then
for edition in $VS_EDITIONS; do
@@ -27576,7 +27590,7 @@ printf "%s\n" "$as_me: directory within the Visual Studio installation" >&6;}
VS_BASE="$unix_path"
fi

# In VS 2017, the default installation is in a subdir named after the edition.
# In VS 2017 and VS 2019, the default installation is in a subdir named after the edition.
# Find the first one present and use that.
if test "x$VS_EDITIONS" != x; then
for edition in $VS_EDITIONS; do
@@ -27635,7 +27649,7 @@ printf "%s\n" "$as_me: Warning: None of $VCVARSFILES were found, Visual Studio i
VS_BASE="$unix_path"
fi

# In VS 2017, the default installation is in a subdir named after the edition.
# In VS 2017 and VS 2019, the default installation is in a subdir named after the edition.
# Find the first one present and use that.
if test "x$VS_EDITIONS" != x; then
for edition in $VS_EDITIONS; do
@@ -27696,7 +27710,7 @@ printf "%s\n" "$as_me: Warning: None of $VCVARSFILES were found, Visual Studio i
VS_BASE="$unix_path"
fi

# In VS 2017, the default installation is in a subdir named after the edition.
# In VS 2017 and VS 2019, the default installation is in a subdir named after the edition.
# Find the first one present and use that.
if test "x$VS_EDITIONS" != x; then
for edition in $VS_EDITIONS; do
@@ -27754,7 +27768,7 @@ printf "%s\n" "$as_me: Warning: None of $VCVARSFILES were found, Visual Studio i
VS_BASE="$unix_path"
fi

# In VS 2017, the default installation is in a subdir named after the edition.
# In VS 2017 and VS 2019, the default installation is in a subdir named after the edition.
# Find the first one present and use that.
if test "x$VS_EDITIONS" != x; then
for edition in $VS_EDITIONS; do
@@ -27811,7 +27825,7 @@ printf "%s\n" "$as_me: Warning: None of $VCVARSFILES were found, Visual Studio i
VS_BASE="$unix_path"
fi

# In VS 2017, the default installation is in a subdir named after the edition.
# In VS 2017 and VS 2019, the default installation is in a subdir named after the edition.
# Find the first one present and use that.
if test "x$VS_EDITIONS" != x; then
for edition in $VS_EDITIONS; do
@@ -52254,7 +52268,7 @@ printf "%s\n" "$as_me: The path of CYGWIN_VC_INSTALL_DIR, which resolves as \"$p
POSSIBLE_MSVC_DLL="$CYGWIN_VC_INSTALL_DIR/redist/x86/Microsoft.VC${VS_VERSION_INTERNAL}.CRT/$DLL_NAME"
fi
else
# Probe: Using well-known location from VS 2017
# Probe: Using well-known location from VS 2017 and VS 2019
if test "x$OPENJDK_TARGET_CPU_BITS" = x64; then
POSSIBLE_MSVC_DLL="`ls $CYGWIN_VC_INSTALL_DIR/Redist/MSVC/*/x64/Microsoft.VC${VS_VERSION_INTERNAL}.CRT/$DLL_NAME`"
else
@@ -53658,7 +53672,7 @@ printf "%s\n" "$as_me: The path of CYGWIN_VC_INSTALL_DIR, which resolves as \"$p
POSSIBLE_MSVC_DLL="$CYGWIN_VC_INSTALL_DIR/redist/x86/Microsoft.VC${VS_VERSION_INTERNAL}.CRT/$DLL_NAME"
fi
else
# Probe: Using well-known location from VS 2017
# Probe: Using well-known location from VS 2017 and VS 2019
if test "x$OPENJDK_TARGET_CPU_BITS" = x64; then
POSSIBLE_MSVC_DLL="`ls $CYGWIN_VC_INSTALL_DIR/Redist/MSVC/*/x64/Microsoft.VC${VS_VERSION_INTERNAL}.CRT/$DLL_NAME`"
else
2 changes: 1 addition & 1 deletion common/autoconf/help.m4
Original file line number Diff line number Diff line change
@@ -112,7 +112,7 @@ apt_help() {
pulse)
PKGHANDLER_COMMAND="sudo apt-get install libpulse-dev" ;;
x11)
PKGHANDLER_COMMAND="sudo apt-get install libX11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev" ;;
PKGHANDLER_COMMAND="sudo apt-get install libx11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev" ;;
ccache)
PKGHANDLER_COMMAND="sudo apt-get install ccache" ;;
esac
22 changes: 18 additions & 4 deletions common/autoconf/toolchain_windows.m4
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,7 @@

################################################################################
# The order of these defines the priority by which we try to find them.
VALID_VS_VERSIONS="2010 2012 2013 2015 2017"
VALID_VS_VERSIONS="2010 2012 2013 2015 2017 2019"

VS_DESCRIPTION_2010="Microsoft Visual Studio 2010"
VS_VERSION_INTERNAL_2010=100
@@ -83,6 +83,20 @@ VS_SDK_INSTALLDIR_2017=
VS_VS_PLATFORM_NAME_2017="v141"
VS_SDK_PLATFORM_NAME_2017=

VS_DESCRIPTION_2019="Microsoft Visual Studio 2019"
VS_VERSION_INTERNAL_2019=141
VS_MSVCR_2019=vcruntime140.dll
VS_MSVCP_2019=msvcp140.dll
VS_ENVVAR_2019="VS160COMNTOOLS"
VS_USE_UCRT_2019="true"
VS_VS_INSTALLDIR_2019="Microsoft Visual Studio/2019"
VS_EDITIONS_2019="BuildTools Community Professional Enterprise"
VS_SDK_INSTALLDIR_2019=
VS_VS_PLATFORM_NAME_2019="v142"
VS_SDK_PLATFORM_NAME_2019=
VS_SUPPORTED_2019=false
VS_TOOLSET_SUPPORTED_2019=false

################################################################################

AC_DEFUN([TOOLCHAIN_CHECK_POSSIBLE_VISUAL_STUDIO_ROOT],
@@ -93,7 +107,7 @@ AC_DEFUN([TOOLCHAIN_CHECK_POSSIBLE_VISUAL_STUDIO_ROOT],
METHOD="$3"

BASIC_WINDOWS_REWRITE_AS_UNIX_PATH(VS_BASE)
# In VS 2017, the default installation is in a subdir named after the edition.
# In VS 2017 and VS 2019, the default installation is in a subdir named after the edition.
# Find the first one present and use that.
if test "x$VS_EDITIONS" != x; then
for edition in $VS_EDITIONS; do
@@ -520,7 +534,7 @@ AC_DEFUN([TOOLCHAIN_SETUP_MSVC_DLL],
POSSIBLE_MSVC_DLL="$CYGWIN_VC_INSTALL_DIR/redist/x86/Microsoft.VC${VS_VERSION_INTERNAL}.CRT/$DLL_NAME"
fi
else
# Probe: Using well-known location from VS 2017
# Probe: Using well-known location from VS 2017 and VS 2019
if test "x$OPENJDK_TARGET_CPU_BITS" = x64; then
POSSIBLE_MSVC_DLL="`ls $CYGWIN_VC_INSTALL_DIR/Redist/MSVC/*/x64/Microsoft.VC${VS_VERSION_INTERNAL}.CRT/$DLL_NAME`"
else
27 changes: 17 additions & 10 deletions hotspot/src/os/aix/vm/attachListener_aix.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright 2012, 2013 SAP AG. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
@@ -65,10 +65,10 @@ class AixAttachListener: AllStatic {
static char _path[UNIX_PATH_MAX];
static bool _has_path;
// Shutdown marker to prevent accept blocking during clean-up.
static bool _shutdown;
static volatile bool _shutdown;

// the file descriptor for the listening socket
static int _listener;
static volatile int _listener;

static bool _atexit_registered;

@@ -131,10 +131,10 @@ class AixAttachOperation: public AttachOperation {
// statics
char AixAttachListener::_path[UNIX_PATH_MAX];
bool AixAttachListener::_has_path;
int AixAttachListener::_listener = -1;
volatile int AixAttachListener::_listener = -1;
bool AixAttachListener::_atexit_registered = false;
// Shutdown marker to prevent accept blocking during clean-up
bool AixAttachListener::_shutdown = false;
volatile bool AixAttachListener::_shutdown = false;

// Supporting class to help split a buffer into individual components
class ArgumentIterator : public StackObj {
@@ -179,7 +179,6 @@ extern "C" {
AixAttachListener::set_shutdown(true);
int s = AixAttachListener::listener();
if (s != -1) {
AixAttachListener::set_listener(-1);
::shutdown(s, 2);
}
if (AixAttachListener::has_path()) {
@@ -363,10 +362,14 @@ AixAttachOperation* AixAttachListener::dequeue() {
// We must prevent accept blocking on the socket if it has been shut down.
// Therefore we allow interrups and check whether we have been shut down already.
if (AixAttachListener::is_shutdown()) {
::close(listener());
set_listener(-1);
return NULL;
}
s=::accept(listener(), &addr, &len);
s = ::accept(listener(), &addr, &len);
if (s == -1) {
::close(listener());
set_listener(-1);
return NULL; // log a warning?
}

@@ -529,9 +532,13 @@ bool AttachListener::check_socket_file() {
listener_cleanup();

// wait to terminate current attach listener instance...
while (AttachListener::transit_state(AL_INITIALIZING,
AL_NOT_INITIALIZED) != AL_NOT_INITIALIZED) {
os::yield();
{
// avoid deadlock if AttachListener thread is blocked at safepoint
ThreadBlockInVM tbivm(JavaThread::current());
while (AttachListener::transit_state(AL_INITIALIZING,
AL_NOT_INITIALIZED) != AL_NOT_INITIALIZED) {
os::yield();
}
}
return is_init_trigger();
}
18 changes: 10 additions & 8 deletions hotspot/src/os/bsd/vm/attachListener_bsd.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -65,7 +65,7 @@ class BsdAttachListener: AllStatic {
static bool _has_path;

// the file descriptor for the listening socket
static int _listener;
static volatile int _listener;

static bool _atexit_registered;

@@ -125,7 +125,7 @@ class BsdAttachOperation: public AttachOperation {
// statics
char BsdAttachListener::_path[UNIX_PATH_MAX];
bool BsdAttachListener::_has_path;
int BsdAttachListener::_listener = -1;
volatile int BsdAttachListener::_listener = -1;
bool BsdAttachListener::_atexit_registered = false;

// Supporting class to help split a buffer into individual components
@@ -494,11 +494,13 @@ bool AttachListener::check_socket_file() {
listener_cleanup();

// wait to terminate current attach listener instance...

while (AttachListener::transit_state(AL_INITIALIZING,

AL_NOT_INITIALIZED) != AL_NOT_INITIALIZED) {
os::yield();
{
// avoid deadlock if AttachListener thread is blocked at safepoint
ThreadBlockInVM tbivm(JavaThread::current());
while (AttachListener::transit_state(AL_INITIALIZING,
AL_NOT_INITIALIZED) != AL_NOT_INITIALIZED) {
os::yield();
}
}
return is_init_trigger();
}
16 changes: 10 additions & 6 deletions hotspot/src/os/linux/vm/attachListener_linux.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -65,7 +65,7 @@ class LinuxAttachListener: AllStatic {
static bool _has_path;

// the file descriptor for the listening socket
static int _listener;
static volatile int _listener;

static bool _atexit_registered;

@@ -125,7 +125,7 @@ class LinuxAttachOperation: public AttachOperation {
// statics
char LinuxAttachListener::_path[UNIX_PATH_MAX];
bool LinuxAttachListener::_has_path;
int LinuxAttachListener::_listener = -1;
volatile int LinuxAttachListener::_listener = -1;
bool LinuxAttachListener::_atexit_registered = false;

// Supporting class to help split a buffer into individual components
@@ -489,9 +489,13 @@ bool AttachListener::check_socket_file() {
listener_cleanup();

// wait to terminate current attach listener instance...
while (AttachListener::transit_state(AL_INITIALIZING,
AL_NOT_INITIALIZED) != AL_NOT_INITIALIZED) {
os::yield();
{
// avoid deadlock if AttachListener thread is blocked at safepoint
ThreadBlockInVM tbivm(JavaThread::current());
while (AttachListener::transit_state(AL_INITIALIZING,
AL_NOT_INITIALIZED) != AL_NOT_INITIALIZED) {
os::yield();
}
}
return is_init_trigger();
}
Original file line number Diff line number Diff line change
@@ -28,31 +28,43 @@
* @run main RemovingUnixDomainSocketTest
*/

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;

import jdk.test.lib.Utils;
import jdk.test.lib.apps.LingeredApp;
import jdk.test.lib.JDKToolLauncher;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;

public class RemovingUnixDomainSocketTest {

// timeout (in seconds)
private static final long timeout = Utils.adjustTimeout(60);

private static void runJCmd(long pid) throws InterruptedException, IOException {
JDKToolLauncher jcmd = JDKToolLauncher.createUsingTestJDK("jcmd");
jcmd.addToolArg(Long.toString(pid));
jcmd.addToolArg("VM.version");

ProcessBuilder pb = new ProcessBuilder(jcmd.getCommand());
Process jcmdProc = pb.start();
Process jcmdProc = ProcessTools.startProcess("jcmd", new ProcessBuilder(jcmd.getCommand()));

OutputAnalyzer out = new OutputAnalyzer(jcmdProc);

jcmdProc.waitFor();
if (!jcmdProc.waitFor(timeout, TimeUnit.SECONDS)) {
log("jcmd is still running after " + timeout + " seconds, terminating...");
jcmdProc.destroy();
jcmdProc.waitFor();
}

System.out.println(out.getStdout());
System.err.println(out.getStderr());
log("jcmd stdout: [" + out.getStdout() + "];\n" +
"jcmd stderr: [" + out.getStderr() + "]\n" +
"jcmd exitValue = " + out.getExitValue());

out.stderrShouldBeEmpty();
out.shouldHaveExitValue(0)
.stderrShouldBeEmptyIgnoreVMWarnings();
}

public static void main(String... args) throws Exception {
@@ -64,10 +76,10 @@ public static void main(String... args) throws Exception {
runJCmd(app.getPid());

// Remove unix domain socket file
var sockFile = Path.of(System.getProperty("java.io.tmpdir"),
File sockFile = Path.of(System.getProperty("java.io.tmpdir"),
".java_pid" + app.getPid())
.toFile();
System.out.println("Remove " + sockFile.toString());
log("Remove " + sockFile.toString());
sockFile.delete();

// Access to Attach Listener again
@@ -77,4 +89,7 @@ public static void main(String... args) throws Exception {
}
}

static void log(Object s) {
System.out.println(String.valueOf(s));
}
}
9 changes: 8 additions & 1 deletion jdk/test/lib/jdk/test/lib/apps/LingeredApp.java
Original file line number Diff line number Diff line change
@@ -40,8 +40,11 @@
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.UUID;

import jdk.test.lib.Utils;
import jdk.test.lib.process.OutputBuffer;
import jdk.test.lib.process.ProcessTools;
import jdk.test.lib.process.StreamPumper;
@@ -230,7 +233,11 @@ public void deleteLock() throws IOException {
public void waitAppTerminate() {
// This code is modeled after tail end of ProcessTools.getOutput().
try {
appProcess.waitFor();
// If the app hangs, we don't want to wait for the to test timeout.
if (!appProcess.waitFor(Utils.adjustTimeout(appWaitTime), TimeUnit.SECONDS)) {
appProcess.destroy();
appProcess.waitFor();
}
outPumperThread.join();
errPumperThread.join();
} catch (InterruptedException e) {
209 changes: 209 additions & 0 deletions make/devkit/createWindowsDevkit2019.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
#!/bin/bash
#
# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation. Oracle designates this
# particular file as subject to the "Classpath" exception as provided
# by Oracle in the LICENSE file that accompanied this code.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#

# This script copies parts of a Visual Studio installation into a devkit
# suitable for building OpenJDK and OracleJDK. Needs to run in Cygwin or WSL.
# erik.joelsson@oracle.com

VS_VERSION="2019"
VS_VERSION_NUM_NODOT="160"
VS_DLL_VERSION="140"
SDK_VERSION="10"
SDK_FULL_VERSION="10.0.17763.0"
MSVC_DIR="Microsoft.VC141.CRT"
MSVC_FULL_VERSION="14.12.27508"
REDIST_FULL_VERSION="14.20.27508"

SCRIPT_DIR="$(cd "$(dirname $0)" > /dev/null && pwd)"
BUILD_DIR="${SCRIPT_DIR}/../../build/devkit"

################################################################################
# Prepare settings

UNAME_SYSTEM=`uname -s`
UNAME_RELEASE=`uname -r`

# Detect cygwin or WSL
IS_CYGWIN=`echo $UNAME_SYSTEM | grep -i CYGWIN`
IS_WSL=`echo $UNAME_RELEASE | grep Microsoft`
if test "x$IS_CYGWIN" != "x"; then
BUILD_ENV="cygwin"
elif test "x$IS_WSL" != "x"; then
BUILD_ENV="wsl"
else
echo "Unknown environment; only Cygwin and WSL are supported."
exit 1
fi

if test "x$BUILD_ENV" = "xcygwin"; then
WINDOWS_PATH_TO_UNIX_PATH="cygpath -u"
elif test "x$BUILD_ENV" = "xwsl"; then
WINDOWS_PATH_TO_UNIX_PATH="wslpath -u"
fi

# Work around the insanely named ProgramFiles(x86) env variable
PROGRAMFILES_X86="$($WINDOWS_PATH_TO_UNIX_PATH "$(cmd.exe /c set | sed -n 's/^ProgramFiles(x86)=//p' | tr -d '\r')")"

# Find Visual Studio installation dir
VSNNNCOMNTOOLS=`cmd.exe /c echo %VS${VS_VERSION_NUM_NODOT}COMNTOOLS% | tr -d '\r'`
if [ -d "$VSNNNCOMNTOOLS" ]; then
VS_INSTALL_DIR="$($WINDOWS_PATH_TO_UNIX_PATH "$VSNNNCOMNTOOLS/../..")"
else
VS_INSTALL_DIR="${PROGRAMFILES_X86}/Microsoft Visual Studio/2019"
VS_INSTALL_DIR="$(ls -d "${VS_INSTALL_DIR}/"{Community,Professional,Enterprise} 2>/dev/null | head -n1)"
fi
echo "VS_INSTALL_DIR: $VS_INSTALL_DIR"

# Extract semantic version
POTENTIAL_INI_FILES="Common7/IDE/wdexpress.isolation.ini Common7/IDE/devenv.isolation.ini"
for f in $POTENTIAL_INI_FILES; do
if [ -f "$VS_INSTALL_DIR/$f" ]; then
VS_VERSION_SP="$(grep ^SemanticVersion= "$VS_INSTALL_DIR/$f")"
# Remove SemnaticVersion=
VS_VERSION_SP="${VS_VERSION_SP#*=}"
# Remove suffix of too detailed numbering starting with +
VS_VERSION_SP="${VS_VERSION_SP%+*}"
break
fi
done
if [ -z "$VS_VERSION_SP" ]; then
echo "Failed to find SP version"
exit 1
fi
echo "Found Version SP: $VS_VERSION_SP"

# Setup output dirs
DEVKIT_ROOT="${BUILD_DIR}/VS${VS_VERSION}-${VS_VERSION_SP}-devkit"
DEVKIT_BUNDLE="${DEVKIT_ROOT}.tar.gz"

echo "Creating devkit in $DEVKIT_ROOT"

MSVCR_DLL=${MSVC_DIR}/vcruntime${VS_DLL_VERSION}.dll
MSVCP_DLL=${MSVC_DIR}/msvcp${VS_DLL_VERSION}.dll

################################################################################
# Copy Visual Studio files

TOOLS_VERSION="$(ls "$VS_INSTALL_DIR/VC/Tools/MSVC" | sort -r -n | head -n1)"
echo "Found Tools version: $TOOLS_VERSION"
VC_SUBDIR="VC/Tools/MSVC/$TOOLS_VERSION"
REDIST_VERSION="$(ls "$VS_INSTALL_DIR/VC/Redist/MSVC" | sort -r -n | head -n1)"
echo "Found Redist version: $REDIST_VERSION"
REDIST_SUBDIR="VC/Redist/MSVC/$REDIST_VERSION"
echo "Copying VC..."
rm -rf $DEVKIT_ROOT/VC
mkdir -p $DEVKIT_ROOT/VC/bin
cp -r "$VS_INSTALL_DIR/${VC_SUBDIR}/bin/Hostx64/x64" $DEVKIT_ROOT/VC/bin/
cp -r "$VS_INSTALL_DIR/${VC_SUBDIR}/bin/Hostx86/x86" $DEVKIT_ROOT/VC/bin/
mkdir -p $DEVKIT_ROOT/VC/lib
cp -r "$VS_INSTALL_DIR/${VC_SUBDIR}/lib/x64" $DEVKIT_ROOT/VC/lib/
cp -r "$VS_INSTALL_DIR/${VC_SUBDIR}/lib/x86" $DEVKIT_ROOT/VC/lib/
cp -r "$VS_INSTALL_DIR/${VC_SUBDIR}/include" $DEVKIT_ROOT/VC/
mkdir -p $DEVKIT_ROOT/VC/atlmfc/lib
cp -r "$VS_INSTALL_DIR/${VC_SUBDIR}/atlmfc/lib/x64" $DEVKIT_ROOT/VC/atlmfc/lib/
cp -r "$VS_INSTALL_DIR/${VC_SUBDIR}/atlmfc/lib/x86" $DEVKIT_ROOT/VC/atlmfc/lib/
cp -r "$VS_INSTALL_DIR/${VC_SUBDIR}/atlmfc/include" $DEVKIT_ROOT/VC/atlmfc/
mkdir -p $DEVKIT_ROOT/VC/Auxiliary
cp -r "$VS_INSTALL_DIR/VC/Auxiliary/Build" $DEVKIT_ROOT/VC/Auxiliary/
mkdir -p $DEVKIT_ROOT/VC/redist
cp -r "$VS_INSTALL_DIR/$REDIST_SUBDIR/x64" $DEVKIT_ROOT/VC/redist/
cp -r "$VS_INSTALL_DIR/$REDIST_SUBDIR/x86" $DEVKIT_ROOT/VC/redist/

# The redist runtime libs are needed to run the compiler but may not be
# installed on the machine where the devkit will be used.
cp $DEVKIT_ROOT/VC/redist/x86/$MSVCR_DLL $DEVKIT_ROOT/VC/bin/x86
cp $DEVKIT_ROOT/VC/redist/x86/$MSVCP_DLL $DEVKIT_ROOT/VC/bin/x86
cp $DEVKIT_ROOT/VC/redist/x64/$MSVCR_DLL $DEVKIT_ROOT/VC/bin/x64
cp $DEVKIT_ROOT/VC/redist/x64/$MSVCP_DLL $DEVKIT_ROOT/VC/bin/x64

################################################################################
# Copy SDK files

SDK_INSTALL_DIR="$PROGRAMFILES_X86/Windows Kits/$SDK_VERSION"
echo "SDK_INSTALL_DIR: $SDK_INSTALL_DIR"

SDK_FULL_VERSION="$(ls "$SDK_INSTALL_DIR/bin" | sort -r -n | head -n1)"
echo "Found SDK version: $SDK_FULL_VERSION"
UCRT_VERSION="$(ls "$SDK_INSTALL_DIR/Redist" | grep $SDK_VERSION | sort -r -n | head -n1)"
echo "Found UCRT version: $UCRT_VERSION"
echo "Copying SDK..."
rm -rf $DEVKIT_ROOT/$SDK_VERSION
mkdir -p $DEVKIT_ROOT/$SDK_VERSION/bin
cp -r "$SDK_INSTALL_DIR/bin/$SDK_FULL_VERSION/x64" $DEVKIT_ROOT/$SDK_VERSION/bin/
cp -r "$SDK_INSTALL_DIR/bin/$SDK_FULL_VERSION/x86" $DEVKIT_ROOT/$SDK_VERSION/bin/
mkdir -p $DEVKIT_ROOT/$SDK_VERSION/lib
cp -r "$SDK_INSTALL_DIR/lib/$SDK_FULL_VERSION/um/x64" $DEVKIT_ROOT/$SDK_VERSION/lib/
cp -r "$SDK_INSTALL_DIR/lib/$SDK_FULL_VERSION/um/x86" $DEVKIT_ROOT/$SDK_VERSION/lib/
cp -r "$SDK_INSTALL_DIR/lib/$SDK_FULL_VERSION/ucrt/x64" $DEVKIT_ROOT/$SDK_VERSION/lib/
cp -r "$SDK_INSTALL_DIR/lib/$SDK_FULL_VERSION/ucrt/x86" $DEVKIT_ROOT/$SDK_VERSION/lib/
mkdir -p $DEVKIT_ROOT/$SDK_VERSION/Redist
cp -r "$SDK_INSTALL_DIR/Redist/$UCRT_VERSION/ucrt" $DEVKIT_ROOT/$SDK_VERSION/Redist/
mkdir -p $DEVKIT_ROOT/$SDK_VERSION/include
cp -r "$SDK_INSTALL_DIR/include/$SDK_FULL_VERSION/"* $DEVKIT_ROOT/$SDK_VERSION/include/

################################################################################
# Generate devkit.info

echo-info() {
echo "$1" >> $DEVKIT_ROOT/devkit.info
}

echo "Generating devkit.info..."
rm -f $DEVKIT_ROOT/devkit.info
echo-info "# This file describes to configure how to interpret the contents of this devkit"
echo-info "DEVKIT_NAME=\"Microsoft Visual Studio $VS_VERSION $VS_VERSION_SP (devkit)\""
echo-info "DEVKIT_VS_VERSION=\"$VS_VERSION\""
echo-info ""
echo-info "DEVKIT_TOOLCHAIN_PATH_x86=\"\$DEVKIT_ROOT/VC/bin/x86:\$DEVKIT_ROOT/$SDK_VERSION/bin/x86\""
echo-info "DEVKIT_VS_INCLUDE_x86=\"\$DEVKIT_ROOT/VC/include;\$DEVKIT_ROOT/VC/atlmfc/include;\$DEVKIT_ROOT/$SDK_VERSION/include/shared;\$DEVKIT_ROOT/$SDK_VERSION/include/ucrt;\$DEVKIT_ROOT/$SDK_VERSION/include/um;\$DEVKIT_ROOT/$SDK_VERSION/include/winrt\""
echo-info "DEVKIT_VS_LIB_x86=\"\$DEVKIT_ROOT/VC/lib/x86;\$DEVKIT_ROOT/VC/atlmfc/lib/x86;\$DEVKIT_ROOT/$SDK_VERSION/lib/x86\""
echo-info "DEVKIT_MSVCR_DLL_x86=\"\$DEVKIT_ROOT/VC/redist/x86/$MSVCR_DLL\""
echo-info "DEVKIT_MSVCP_DLL_x86=\"\$DEVKIT_ROOT/VC/redist/x86/$MSVCP_DLL\""
echo-info "DEVKIT_UCRT_DLL_DIR_x86=\"\$DEVKIT_ROOT/10/Redist/ucrt/DLLs/x86\""
echo-info ""
echo-info "DEVKIT_TOOLCHAIN_PATH_x86_64=\"\$DEVKIT_ROOT/VC/bin/x64:\$DEVKIT_ROOT/$SDK_VERSION/bin/x64:\$DEVKIT_ROOT/$SDK_VERSION/bin/x86\""
echo-info "DEVKIT_VS_INCLUDE_x86_64=\"\$DEVKIT_ROOT/VC/include;\$DEVKIT_ROOT/VC/atlmfc/include;\$DEVKIT_ROOT/$SDK_VERSION/include/shared;\$DEVKIT_ROOT/$SDK_VERSION/include/ucrt;\$DEVKIT_ROOT/$SDK_VERSION/include/um;\$DEVKIT_ROOT/$SDK_VERSION/include/winrt\""
echo-info "DEVKIT_VS_LIB_x86_64=\"\$DEVKIT_ROOT/VC/lib/x64;\$DEVKIT_ROOT/VC/atlmfc/lib/x64;\$DEVKIT_ROOT/$SDK_VERSION/lib/x64\""
echo-info "DEVKIT_MSVCR_DLL_x86_64=\"\$DEVKIT_ROOT/VC/redist/x64/$MSVCR_DLL\""
echo-info "DEVKIT_MSVCP_DLL_x86_64=\"\$DEVKIT_ROOT/VC/redist/x64/$MSVCP_DLL\""
echo-info "DEVKIT_UCRT_DLL_DIR_x86_64=\"\$DEVKIT_ROOT/10/Redist/ucrt/DLLs/x64\""
echo-info ""
echo-info "DEVKIT_TOOLS_VERSION=\"$TOOLS_VERSION\""
echo-info "DEVKIT_REDIST_VERSION=\"$REDIST_VERSION\""
echo-info "DEVKIT_SDK_VERSION=\"$SDK_FULL_VERSION\""
echo-info "DEVKIT_UCRT_VERSION=\"$UCRT_VERSION\""

################################################################################
# Copy this script

echo "Copying this script..."
cp $0 $DEVKIT_ROOT/

################################################################################
# Create bundle

echo "Creating bundle: $DEVKIT_BUNDLE"
(cd "$DEVKIT_ROOT" && tar zcf "$DEVKIT_BUNDLE" .)