aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/native/windows/build_windows_jni.sh
blob: a15da4a74cc8ff6cf6b258b021097fdc5e6f652b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash -eu

# Copyright 2016 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# It's not a good idea to link an MSYS dynamic library into a native Windows
# JVM, so we need to build it with Visual Studio. However, Bazel doesn't
# support multiple compilers in the same build yet, so we need to hack around
# this limitation using a genrule.

DLL="$1"
shift 1

function fail() {
  echo >&2 "ERROR: $*"
  exit 1
}

# Ensure the PATH is set up correctly.
if ! which which >&/dev/null ; then
  PATH="/bin:/usr/bin:$PATH"
  which which >&/dev/null \
      || fail "System PATH is not set up correctly, cannot run GNU bintools"
fi

# Create a temp directory. It will used for the batch file we generate soon and
# as the temp directory for CL.EXE .
VSTEMP=$(mktemp -d)
trap "rm -fr \"$VSTEMP\"" EXIT
VSVARS=""

# Visual Studio or Visual C++ Build Tools might not be installed at default
# location. Check BAZEL_VS and BAZEL_VC first.
if [ -n "${BAZEL_VC+set}" ]; then
  VSVARS="${BAZEL_VC}/VCVARSALL.BAT"
  # Check if BAZEL_VC points to Visual C++ Build Tools 2017
  if [ ! -f "${VSVARS}" ]; then
    VSVARS="${BAZEL_VC}/Auxiliary/Build/VCVARSALL.BAT"
  fi
else
  # Find Visual Studio. We don't have any regular environment variables
  # available so this is the best we can do.
  if [ -z "${BAZEL_VS+set}" ]; then
    VSVERSION="$(ls "C:/Program Files (x86)" \
        | grep -E "Microsoft Visual Studio [0-9]+" \
        | sort --version-sort \
        | tail -n 1)"
    [[ -n "$VSVERSION" ]] || fail "Visual Studio not found"
    BAZEL_VS="C:/Program Files (x86)/$VSVERSION"
  fi
  VSVARS="${BAZEL_VS}/VC/VCVARSALL.BAT"
fi

# Check if Visual Studio 2017 is installed. Look for it at the default
# locations.
if [ ! -f "${VSVARS}" ]; then
  VSVARS="C:/Program Files (x86)/Microsoft Visual Studio/2017/"
  VSEDITION="BuildTools"
  if [ -d "${VSVARS}Enterprise" ]; then
    VSEDITION="Enterprise"
  elif [ -d "${VSVARS}Professional" ]; then
    VSEDITION="Professional"
  elif [ -d "${VSVARS}Community" ]; then
    VSEDITION="Community"
  fi
  VSVARS+="$VSEDITION/VC/Auxiliary/Build/VCVARSALL.BAT"
fi

if [ ! -f "${VSVARS}" ]; then
  fail "VCVARSALL.bat not found, check your Visual Studio installation"
fi

JAVAINCLUDES=""
if [ -n "${JAVA_HOME+set}" ]; then
  JAVAINCLUDES="$JAVA_HOME/include"
else
  # Find Java. $(JAVA) in the BUILD file points to external/local_jdk/...,
  # which is not very useful for anything not MSYS-based.
  JAVA=$(ls "C:/Program Files/java" | grep -E "^jdk" | sort | tail -n 1)
  [[ -n "$JAVA" ]] || fail "JDK not found"
  JAVAINCLUDES="C:/Program Files/java/$JAVA/include"
fi

# Convert all compilation units to Windows paths.
WINDOWS_SOURCES=()
for i in $*; do
  if [[ "$i" =~ ^.*\.cc$ ]]; then
    WINDOWS_SOURCES+=("\"$(cygpath -a -w $i)\"")
  fi
done

# Copy jni headers to src/main/native folder
# Mimic genrule //src/main/native:copy_link_jni_md_header and //src/main/native:copy_link_jni_header
JNI_HEADERS_DIR="${VSTEMP}/src/main/native"
mkdir -p "$JNI_HEADERS_DIR"
cp -f "$JAVAINCLUDES/jni.h" "$JNI_HEADERS_DIR/"
cp -f "$JAVAINCLUDES/win32/jni_md.h" "$JNI_HEADERS_DIR/"

# CL.EXE needs a bunch of environment variables whose official location is a
# batch file. We can't make that have an effect on a bash instance, so
# generate a batch file that invokes it.
# As for `abs_pwd` and `pwd_drive`: in cmd.exe, it's not enough to `cd` into a
# directory. You must also change to its drive to truly set the cwd to that
# directory. See https://github.com/bazelbuild/bazel/issues/3906
abs_pwd="$(cygpath -a -w "${PWD}")"
pwd_drive="$(echo "$abs_pwd" | head -c2)"
cat > "${VSTEMP}/windows_jni.bat" <<EOF
@echo OFF
@call "${VSVARS}" amd64
@$pwd_drive
@cd "$abs_pwd"
@set TMP=$(cygpath -a -w "${VSTEMP}")
@CL /O2 /EHsc /LD /Fe:"$(cygpath -a -w ${DLL})" /I "%TMP%" /I . ${WINDOWS_SOURCES[*]}
EOF

# Invoke the file and hopefully generate the .DLL .
chmod +x "${VSTEMP}/windows_jni.bat"
exec "${VSTEMP}/windows_jni.bat"