diff options
author | Laszlo Csomor <laszlocsomor@google.com> | 2017-07-04 05:45:40 -0400 |
---|---|---|
committer | John Cater <jcater@google.com> | 2017-07-05 10:57:51 -0400 |
commit | 9409109c3016c51403ed4387ecda6c99459f1a8d (patch) | |
tree | a4df58dec6aa7990bf7280785b63abfe9840da03 /src/main/native/windows | |
parent | 6c446ac82a23bd803a4881ac84e427325db15e69 (diff) |
build_windows_jni.sh: move file to subdirectory
The script more logically belongs in
src/main/native/windows than in src/main/native.
Also move the //src/main/native:windows_jni rule
into //src/main/native/windows:windows_jni, so the
logic of building the JNI library is fully
contained in that package.
Change-Id: I96e19003932cc0ddc5af3471b0b31a1aec09b8fa
PiperOrigin-RevId: 160876594
Diffstat (limited to 'src/main/native/windows')
-rw-r--r-- | src/main/native/windows/BUILD | 28 | ||||
-rwxr-xr-x | src/main/native/windows/build_windows_jni.sh | 100 |
2 files changed, 117 insertions, 11 deletions
diff --git a/src/main/native/windows/BUILD b/src/main/native/windows/BUILD index c7dead2469..926643c878 100644 --- a/src/main/native/windows/BUILD +++ b/src/main/native/windows/BUILD @@ -6,8 +6,8 @@ filegroup( visibility = ["//src/main/native:__pkg__"], ) -# TODO(xingao): verify that this filegroup contains exactly what it has to wrt. -# where it is used (//src/main/native:embedded_tools). +# TODO(xingao): verify that this filegroup contains exactly what it has to, with +# regard to where it is used (//src/main/native:embedded_tools). # Context: https://github.com/bazelbuild/bazel/commit/33d05f6b551cf2fdb257cb536a5e864d095144a1 filegroup( name = "embedded_tools", @@ -15,15 +15,6 @@ filegroup( visibility = ["//src/main/native:__pkg__"], ) -filegroup( - name = "raw-sources", - srcs = glob([ - "*.cc", - "*.h", - ]), - visibility = ["//src/main/native:__pkg__"], -) - cc_library( name = "lib-file", srcs = ["file.cc"], @@ -41,3 +32,18 @@ cc_library( srcs = ["util.cc"], hdrs = ["util.h"], ) + +genrule( + name = "windows_jni", + srcs = glob([ + "*.cc", + "*.h", + ]), + outs = ["windows_jni.dll"], + cmd = "$(location build_windows_jni.sh) $@ $(SRCS)", + tools = ["build_windows_jni.sh"], + visibility = [ + "//src:__pkg__", + "//src/test/java/com/google/devtools/build/lib:__subpackages__", + ], +) diff --git a/src/main/native/windows/build_windows_jni.sh b/src/main/native/windows/build_windows_jni.sh new file mode 100755 index 0000000000..a66267814a --- /dev/null +++ b/src/main/native/windows/build_windows_jni.sh @@ -0,0 +1,100 @@ +#!/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 + +# 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" + +# 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 + +# 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" + +# 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 + +# 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. +cat > "${VSTEMP}/windows_jni.bat" <<EOF +@echo OFF +@call "${VSVARS}" amd64 +@cd $(cygpath -a -w "${PWD}") +@set TMP=$(cygpath -a -w "${VSTEMP}") +@CL /O2 /EHsc /LD /Fe:"$(cygpath -a -w ${DLL})" /I "${JAVAINCLUDES}" /I "${JAVAINCLUDES}/win32" /I . ${WINDOWS_SOURCES[*]} +EOF + +# Invoke the file and hopefully generate the .DLL . +chmod +x "${VSTEMP}/windows_jni.bat" +exec "${VSTEMP}/windows_jni.bat" |