1#!/bin/bash
2#
3# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
4# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5#
6# This code is free software; you can redistribute it and/or modify it
7# under the terms of the GNU General Public License version 2 only, as
8# published by the Free Software Foundation.  Oracle designates this
9# particular file as subject to the "Classpath" exception as provided
10# by Oracle in the LICENSE file that accompanied this code.
11#
12# This code is distributed in the hope that it will be useful, but WITHOUT
13# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15# version 2 for more details (a copy is included in the LICENSE file that
16# accompanied this code).
17#
18# You should have received a copy of the GNU General Public License version
19# 2 along with this work; if not, write to the Free Software Foundation,
20# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21#
22# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23# or visit www.oracle.com if you need additional information or have any
24# questions.
25#
26
27# This script copies parts of a Visual Studio installation into a devkit
28# suitable for building OpenJDK and OracleJDK. Needs to run in Cygwin or WSL.
29# erik.joelsson@oracle.com
30
31VS_VERSION="2019"
32VS_VERSION_NUM_NODOT="160"
33VS_DLL_VERSION="140"
34SDK_VERSION="10"
35SDK_FULL_VERSION="10.0.17763.0"
36MSVC_DIR="Microsoft.VC141.CRT"
37MSVC_FULL_VERSION="14.12.27508"
38REDIST_FULL_VERSION="14.20.27508"
39
40SCRIPT_DIR="$(cd "$(dirname $0)" > /dev/null && pwd)"
41BUILD_DIR="${SCRIPT_DIR}/../../build/devkit"
42
43################################################################################
44# Prepare settings
45
46UNAME_SYSTEM=`uname -s`
47UNAME_RELEASE=`uname -r`
48
49# Detect cygwin or WSL
50IS_CYGWIN=`echo $UNAME_SYSTEM | grep -i CYGWIN`
51IS_WSL=`echo $UNAME_RELEASE | grep Microsoft`
52if test "x$IS_CYGWIN" != "x"; then
53    BUILD_ENV="cygwin"
54elif test "x$IS_WSL" != "x"; then
55    BUILD_ENV="wsl"
56else
57    echo "Unknown environment; only Cygwin and WSL are supported."
58    exit 1
59fi
60
61if test "x$BUILD_ENV" = "xcygwin"; then
62    WINDOWS_PATH_TO_UNIX_PATH="cygpath -u"
63elif test "x$BUILD_ENV" = "xwsl"; then
64    WINDOWS_PATH_TO_UNIX_PATH="wslpath -u"
65fi
66
67# Work around the insanely named ProgramFiles(x86) env variable
68PROGRAMFILES_X86="$($WINDOWS_PATH_TO_UNIX_PATH "$(cmd.exe /c set | sed -n 's/^ProgramFiles(x86)=//p' | tr -d '\r')")"
69
70# Find Visual Studio installation dir
71VSNNNCOMNTOOLS=`cmd.exe /c echo %VS${VS_VERSION_NUM_NODOT}COMNTOOLS% | tr -d '\r'`
72if [ -d "$VSNNNCOMNTOOLS" ]; then
73    VS_INSTALL_DIR="$($WINDOWS_PATH_TO_UNIX_PATH "$VSNNNCOMNTOOLS/../..")"
74else
75    VS_INSTALL_DIR="${PROGRAMFILES_X86}/Microsoft Visual Studio/2019"
76    VS_INSTALL_DIR="$(ls -d "${VS_INSTALL_DIR}/"{Community,Professional,Enterprise} 2>/dev/null | head -n1)"
77fi
78echo "VS_INSTALL_DIR: $VS_INSTALL_DIR"
79
80# Extract semantic version
81POTENTIAL_INI_FILES="Common7/IDE/wdexpress.isolation.ini Common7/IDE/devenv.isolation.ini"
82for f in $POTENTIAL_INI_FILES; do
83    if [ -f "$VS_INSTALL_DIR/$f" ]; then
84        VS_VERSION_SP="$(grep ^SemanticVersion= "$VS_INSTALL_DIR/$f")"
85        # Remove SemnaticVersion=
86        VS_VERSION_SP="${VS_VERSION_SP#*=}"
87        # Remove suffix of too detailed numbering starting with +
88        VS_VERSION_SP="${VS_VERSION_SP%+*}"
89        break
90    fi
91done
92if [ -z "$VS_VERSION_SP" ]; then
93    echo "Failed to find SP version"
94    exit 1
95fi
96echo "Found Version SP: $VS_VERSION_SP"
97
98# Setup output dirs
99DEVKIT_ROOT="${BUILD_DIR}/VS${VS_VERSION}-${VS_VERSION_SP}-devkit"
100DEVKIT_BUNDLE="${DEVKIT_ROOT}.tar.gz"
101
102echo "Creating devkit in $DEVKIT_ROOT"
103
104MSVCR_DLL=${MSVC_DIR}/vcruntime${VS_DLL_VERSION}.dll
105MSVCP_DLL=${MSVC_DIR}/msvcp${VS_DLL_VERSION}.dll
106
107################################################################################
108# Copy Visual Studio files
109
110TOOLS_VERSION="$(ls "$VS_INSTALL_DIR/VC/Tools/MSVC" | sort -r -n | head -n1)"
111echo "Found Tools version: $TOOLS_VERSION"
112VC_SUBDIR="VC/Tools/MSVC/$TOOLS_VERSION"
113REDIST_VERSION="$(ls "$VS_INSTALL_DIR/VC/Redist/MSVC" | sort -r -n | head -n1)"
114echo "Found Redist version: $REDIST_VERSION"
115REDIST_SUBDIR="VC/Redist/MSVC/$REDIST_VERSION"
116echo "Copying VC..."
117rm -rf $DEVKIT_ROOT/VC
118mkdir -p $DEVKIT_ROOT/VC/bin
119cp -r "$VS_INSTALL_DIR/${VC_SUBDIR}/bin/Hostx64/x64" $DEVKIT_ROOT/VC/bin/
120cp -r "$VS_INSTALL_DIR/${VC_SUBDIR}/bin/Hostx86/x86" $DEVKIT_ROOT/VC/bin/
121mkdir -p $DEVKIT_ROOT/VC/lib
122cp -r "$VS_INSTALL_DIR/${VC_SUBDIR}/lib/x64" $DEVKIT_ROOT/VC/lib/
123cp -r "$VS_INSTALL_DIR/${VC_SUBDIR}/lib/x86" $DEVKIT_ROOT/VC/lib/
124cp -r "$VS_INSTALL_DIR/${VC_SUBDIR}/include" $DEVKIT_ROOT/VC/
125mkdir -p $DEVKIT_ROOT/VC/atlmfc/lib
126cp -r "$VS_INSTALL_DIR/${VC_SUBDIR}/atlmfc/lib/x64" $DEVKIT_ROOT/VC/atlmfc/lib/
127cp -r "$VS_INSTALL_DIR/${VC_SUBDIR}/atlmfc/lib/x86" $DEVKIT_ROOT/VC/atlmfc/lib/
128cp -r "$VS_INSTALL_DIR/${VC_SUBDIR}/atlmfc/include" $DEVKIT_ROOT/VC/atlmfc/
129mkdir -p $DEVKIT_ROOT/VC/Auxiliary
130cp -r "$VS_INSTALL_DIR/VC/Auxiliary/Build" $DEVKIT_ROOT/VC/Auxiliary/
131mkdir -p $DEVKIT_ROOT/VC/redist
132cp -r "$VS_INSTALL_DIR/$REDIST_SUBDIR/x64" $DEVKIT_ROOT/VC/redist/
133cp -r "$VS_INSTALL_DIR/$REDIST_SUBDIR/x86" $DEVKIT_ROOT/VC/redist/
134
135# The redist runtime libs are needed to run the compiler but may not be
136# installed on the machine where the devkit will be used.
137cp $DEVKIT_ROOT/VC/redist/x86/$MSVCR_DLL $DEVKIT_ROOT/VC/bin/x86
138cp $DEVKIT_ROOT/VC/redist/x86/$MSVCP_DLL $DEVKIT_ROOT/VC/bin/x86
139cp $DEVKIT_ROOT/VC/redist/x64/$MSVCR_DLL $DEVKIT_ROOT/VC/bin/x64
140cp $DEVKIT_ROOT/VC/redist/x64/$MSVCP_DLL $DEVKIT_ROOT/VC/bin/x64
141
142################################################################################
143# Copy SDK files
144
145SDK_INSTALL_DIR="$PROGRAMFILES_X86/Windows Kits/$SDK_VERSION"
146echo "SDK_INSTALL_DIR: $SDK_INSTALL_DIR"
147
148SDK_FULL_VERSION="$(ls "$SDK_INSTALL_DIR/bin" | sort -r -n | head -n1)"
149echo "Found SDK version: $SDK_FULL_VERSION"
150UCRT_VERSION="$(ls "$SDK_INSTALL_DIR/Redist" | grep $SDK_VERSION | sort -r -n | head -n1)"
151echo "Found UCRT version: $UCRT_VERSION"
152echo "Copying SDK..."
153rm -rf $DEVKIT_ROOT/$SDK_VERSION
154mkdir -p $DEVKIT_ROOT/$SDK_VERSION/bin
155cp -r "$SDK_INSTALL_DIR/bin/$SDK_FULL_VERSION/x64" $DEVKIT_ROOT/$SDK_VERSION/bin/
156cp -r "$SDK_INSTALL_DIR/bin/$SDK_FULL_VERSION/x86" $DEVKIT_ROOT/$SDK_VERSION/bin/
157mkdir -p $DEVKIT_ROOT/$SDK_VERSION/lib
158cp -r "$SDK_INSTALL_DIR/lib/$SDK_FULL_VERSION/um/x64" $DEVKIT_ROOT/$SDK_VERSION/lib/
159cp -r "$SDK_INSTALL_DIR/lib/$SDK_FULL_VERSION/um/x86" $DEVKIT_ROOT/$SDK_VERSION/lib/
160cp -r "$SDK_INSTALL_DIR/lib/$SDK_FULL_VERSION/ucrt/x64" $DEVKIT_ROOT/$SDK_VERSION/lib/
161cp -r "$SDK_INSTALL_DIR/lib/$SDK_FULL_VERSION/ucrt/x86" $DEVKIT_ROOT/$SDK_VERSION/lib/
162mkdir -p $DEVKIT_ROOT/$SDK_VERSION/Redist
163cp -r "$SDK_INSTALL_DIR/Redist/$UCRT_VERSION/ucrt" $DEVKIT_ROOT/$SDK_VERSION/Redist/
164mkdir -p $DEVKIT_ROOT/$SDK_VERSION/include
165cp -r "$SDK_INSTALL_DIR/include/$SDK_FULL_VERSION/"* $DEVKIT_ROOT/$SDK_VERSION/include/
166
167################################################################################
168# Generate devkit.info
169
170echo-info() {
171    echo "$1" >> $DEVKIT_ROOT/devkit.info
172}
173
174echo "Generating devkit.info..."
175rm -f $DEVKIT_ROOT/devkit.info
176echo-info "# This file describes to configure how to interpret the contents of this devkit"
177echo-info "DEVKIT_NAME=\"Microsoft Visual Studio $VS_VERSION $VS_VERSION_SP (devkit)\""
178echo-info "DEVKIT_VS_VERSION=\"$VS_VERSION\""
179echo-info ""
180echo-info "DEVKIT_TOOLCHAIN_PATH_x86=\"\$DEVKIT_ROOT/VC/bin/x86:\$DEVKIT_ROOT/$SDK_VERSION/bin/x86\""
181echo-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\""
182echo-info "DEVKIT_VS_LIB_x86=\"\$DEVKIT_ROOT/VC/lib/x86;\$DEVKIT_ROOT/VC/atlmfc/lib/x86;\$DEVKIT_ROOT/$SDK_VERSION/lib/x86\""
183echo-info "DEVKIT_MSVCR_DLL_x86=\"\$DEVKIT_ROOT/VC/redist/x86/$MSVCR_DLL\""
184echo-info "DEVKIT_MSVCP_DLL_x86=\"\$DEVKIT_ROOT/VC/redist/x86/$MSVCP_DLL\""
185echo-info "DEVKIT_UCRT_DLL_DIR_x86=\"\$DEVKIT_ROOT/10/Redist/ucrt/DLLs/x86\""
186echo-info ""
187echo-info "DEVKIT_TOOLCHAIN_PATH_x86_64=\"\$DEVKIT_ROOT/VC/bin/x64:\$DEVKIT_ROOT/$SDK_VERSION/bin/x64:\$DEVKIT_ROOT/$SDK_VERSION/bin/x86\""
188echo-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\""
189echo-info "DEVKIT_VS_LIB_x86_64=\"\$DEVKIT_ROOT/VC/lib/x64;\$DEVKIT_ROOT/VC/atlmfc/lib/x64;\$DEVKIT_ROOT/$SDK_VERSION/lib/x64\""
190echo-info "DEVKIT_MSVCR_DLL_x86_64=\"\$DEVKIT_ROOT/VC/redist/x64/$MSVCR_DLL\""
191echo-info "DEVKIT_MSVCP_DLL_x86_64=\"\$DEVKIT_ROOT/VC/redist/x64/$MSVCP_DLL\""
192echo-info "DEVKIT_UCRT_DLL_DIR_x86_64=\"\$DEVKIT_ROOT/10/Redist/ucrt/DLLs/x64\""
193echo-info ""
194echo-info "DEVKIT_TOOLS_VERSION=\"$TOOLS_VERSION\""
195echo-info "DEVKIT_REDIST_VERSION=\"$REDIST_VERSION\""
196echo-info "DEVKIT_SDK_VERSION=\"$SDK_FULL_VERSION\""
197echo-info "DEVKIT_UCRT_VERSION=\"$UCRT_VERSION\""
198
199################################################################################
200# Copy this script
201
202echo "Copying this script..."
203cp $0 $DEVKIT_ROOT/
204
205################################################################################
206# Create bundle
207
208echo "Creating bundle: $DEVKIT_BUNDLE"
209(cd "$DEVKIT_ROOT" && tar zcf "$DEVKIT_BUNDLE" .)
210