blob: 0f58c30351fe3548adc5ce1bffd58890578d3170 [file] [log] [blame]
Lukacs Berki840acb12017-01-25 14:01:33 +00001#!/bin/bash
2#
3# Copyright 2016 The Bazel Authors. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Laszlo Csomor6ab4e162018-08-13 08:31:31 -070016
17# --- begin runfiles.bash initialization ---
18# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash).
19set -euo pipefail
20if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
21 if [[ -f "$0.runfiles_manifest" ]]; then
22 export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
23 elif [[ -f "$0.runfiles/MANIFEST" ]]; then
24 export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
25 elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
26 export RUNFILES_DIR="$0.runfiles"
27 fi
28fi
29if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
30 source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
31elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
32 source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
33 "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
34else
35 echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
36 exit 1
37fi
38# --- end runfiles.bash initialization ---
39
40source "$(rlocation "io_bazel/src/test/shell/integration_test_setup.sh")" \
Lukacs Berki840acb12017-01-25 14:01:33 +000041 || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
42
Laszlo Csomor6ab4e162018-08-13 08:31:31 -070043case "$(uname -s | tr [:upper:] [:lower:])" in
44msys*|mingw*|cygwin*)
45 declare -r is_windows=true
46 ;;
47*)
48 declare -r is_windows=false
49 ;;
50esac
Lukacs Berki840acb12017-01-25 14:01:33 +000051
Laszlo Csomor6ab4e162018-08-13 08:31:31 -070052if "$is_windows"; then
53 export MSYS_NO_PATHCONV=1
54 export MSYS2_ARG_CONV_EXCL="*"
55fi
Lukacs Berki840acb12017-01-25 14:01:33 +000056
Laszlo Csomorf57c3062019-01-16 05:06:00 -080057if ! type try_with_timeout >&/dev/null; then
58 # Bazel's testenv.sh defines try_with_timeout but the Google-internal version
59 # uses a different testenv.sh.
60 function try_with_timeout() { $* ; }
61fi
62
Lukacs Berki840acb12017-01-25 14:01:33 +000063#### TESTS #############################################################
64
65function test_no_rebuild_on_irrelevant_header_change() {
Laszlo Csomor6ab4e162018-08-13 08:31:31 -070066 local -r pkg=$FUNCNAME
67 mkdir -p $pkg
68 cat > $pkg/BUILD <<EOF
Lukacs Berki840acb12017-01-25 14:01:33 +000069cc_binary(name="a", srcs=["a.cc"], deps=["b"])
70cc_library(name="b", srcs=["b1.h", "b2.h"])
71EOF
72
Laszlo Csomor6ab4e162018-08-13 08:31:31 -070073 cat > $pkg/a.cc <<EOF
74#include "$pkg/b1.h"
Lukacs Berki840acb12017-01-25 14:01:33 +000075
76int main(void) {
77 return B_RETURN_VALUE;
78}
79EOF
80
Laszlo Csomor6ab4e162018-08-13 08:31:31 -070081 cat > $pkg/b1.h <<EOF
Lukacs Berki840acb12017-01-25 14:01:33 +000082#define B_RETURN_VALUE 31
83EOF
84
Laszlo Csomor6ab4e162018-08-13 08:31:31 -070085 cat > $pkg/b2.h <<EOF
Lukacs Berki840acb12017-01-25 14:01:33 +000086=== BANANA ===
87EOF
88
Laszlo Csomor6ab4e162018-08-13 08:31:31 -070089 bazel build //$pkg:a || fail "build failed"
90 echo "CHERRY" > $pkg/b2.h
91 bazel build //$pkg:a >& $TEST_log || fail "build failed"
92 expect_not_log "Compiling $pkg/a.cc"
Lukacs Berki840acb12017-01-25 14:01:33 +000093}
94
95function test_new_header_is_required() {
Laszlo Csomor6ab4e162018-08-13 08:31:31 -070096 local -r pkg=$FUNCNAME
97 mkdir -p $pkg
98 cat > $pkg/BUILD <<EOF
Lukacs Berki840acb12017-01-25 14:01:33 +000099cc_binary(name="a", srcs=["a.cc"], deps=[":b"])
100cc_library(name="b", srcs=["b1.h", "b2.h"])
101EOF
102
Laszlo Csomor6ab4e162018-08-13 08:31:31 -0700103 cat > $pkg/a.cc << EOF
104#include "$pkg/b1.h"
Lukacs Berki840acb12017-01-25 14:01:33 +0000105
106int main(void) {
107 return B1;
108}
109EOF
110
Laszlo Csomor6ab4e162018-08-13 08:31:31 -0700111 cat > $pkg/b1.h <<EOF
Lukacs Berki840acb12017-01-25 14:01:33 +0000112#define B1 3
113EOF
114
Laszlo Csomor6ab4e162018-08-13 08:31:31 -0700115 cat > $pkg/b2.h <<EOF
Lukacs Berki840acb12017-01-25 14:01:33 +0000116#define B2 4
117EOF
118
Laszlo Csomor6ab4e162018-08-13 08:31:31 -0700119 bazel build //$pkg:a || fail "build failed"
120 cat > $pkg/a.cc << EOF
121#include "$pkg/b1.h"
122#include "$pkg/b2.h"
Lukacs Berki840acb12017-01-25 14:01:33 +0000123
124int main(void) {
125 return B1 + B2;
126}
127EOF
128
Laszlo Csomor6ab4e162018-08-13 08:31:31 -0700129 bazel build //$pkg:a || fail "build failled"
Lukacs Berki840acb12017-01-25 14:01:33 +0000130}
131
George Gensureabc93322017-02-27 12:16:39 +0000132function test_no_recompile_on_shutdown() {
Laszlo Csomor6ab4e162018-08-13 08:31:31 -0700133 local -r pkg=$FUNCNAME
134 mkdir -p $pkg
135 cat > $pkg/BUILD <<EOF
George Gensureabc93322017-02-27 12:16:39 +0000136cc_binary(name="a", srcs=["a.cc"], deps=["b"])
137cc_library(name="b", includes=["."], hdrs=["b.h"])
138EOF
139
Laszlo Csomor6ab4e162018-08-13 08:31:31 -0700140 cat > $pkg/a.cc <<EOF
George Gensureabc93322017-02-27 12:16:39 +0000141#include "b.h"
142
143int main(void) {
144 return B_RETURN_VALUE;
145}
146EOF
147
Laszlo Csomor6ab4e162018-08-13 08:31:31 -0700148 cat > $pkg/b.h <<EOF
George Gensureabc93322017-02-27 12:16:39 +0000149#define B_RETURN_VALUE 31
150EOF
151
Laszlo Csomor6ab4e162018-08-13 08:31:31 -0700152 bazel build -s //$pkg:a >& $TEST_log || fail "build failed"
153 expect_log "Compiling $pkg/a.cc"
Laszlo Csomorf57c3062019-01-16 05:06:00 -0800154 try_with_timeout bazel shutdown || fail "shutdown failed"
Laszlo Csomor6ab4e162018-08-13 08:31:31 -0700155 bazel build -s //$pkg:a >& $TEST_log || fail "build failed"
156 expect_not_log "Compiling $pkg/a.cc"
George Gensureabc93322017-02-27 12:16:39 +0000157}
158
Lukacs Berki840acb12017-01-25 14:01:33 +0000159run_suite "Tests for Bazel's C++ rules"