1SUITE_basedir_SETUP() {
2    unset CCACHE_NODIRECT
3
4    mkdir -p dir1/src dir1/include
5    cat <<EOF >dir1/src/test.c
6#include <stdarg.h>
7#include <test.h>
8EOF
9    cat <<EOF >dir1/include/test.h
10int test;
11EOF
12    cp -r dir1 dir2
13    backdate dir1/include/test.h dir2/include/test.h
14}
15
16SUITE_basedir() {
17    # -------------------------------------------------------------------------
18    TEST "Enabled CCACHE_BASEDIR"
19
20    cd dir1
21    CCACHE_BASEDIR="`pwd`" $CCACHE_COMPILE -I`pwd`/include -c src/test.c
22    expect_stat 'cache hit (direct)' 0
23    expect_stat 'cache hit (preprocessed)' 0
24    expect_stat 'cache miss' 1
25
26    cd ../dir2
27    CCACHE_BASEDIR="`pwd`" $CCACHE_COMPILE -I`pwd`/include -c src/test.c
28    expect_stat 'cache hit (direct)' 1
29    expect_stat 'cache hit (preprocessed)' 0
30    expect_stat 'cache miss' 1
31
32    # -------------------------------------------------------------------------
33    TEST "Disabled (default) CCACHE_BASEDIR"
34
35    cd dir1
36    CCACHE_BASEDIR="`pwd`" $CCACHE_COMPILE -I`pwd`/include -c src/test.c
37    expect_stat 'cache hit (direct)' 0
38    expect_stat 'cache hit (preprocessed)' 0
39    expect_stat 'cache miss' 1
40
41    # CCACHE_BASEDIR="" is the default:
42    $CCACHE_COMPILE -I`pwd`/include -c src/test.c
43    expect_stat 'cache hit (direct)' 0
44    expect_stat 'cache hit (preprocessed)' 0
45    expect_stat 'cache miss' 2
46
47    # -------------------------------------------------------------------------
48    TEST "Path normalization"
49
50    cd dir1
51    CCACHE_BASEDIR="`pwd`" $CCACHE_COMPILE -I`pwd`/include -c src/test.c
52    expect_stat 'cache hit (direct)' 0
53    expect_stat 'cache hit (preprocessed)' 0
54    expect_stat 'cache miss' 1
55
56    mkdir subdir
57    ln -s `pwd`/include subdir/symlink
58
59    # Rewriting triggered by CCACHE_BASEDIR should handle paths with multiple
60    # slashes, redundant "/." parts and "foo/.." parts correctly. Note that the
61    # ".." part of the path is resolved after the symlink has been resolved.
62    CCACHE_BASEDIR=`pwd` $CCACHE_COMPILE -I`pwd`//./subdir/symlink/../include -c `pwd`/src/test.c
63    expect_stat 'cache hit (direct)' 1
64    expect_stat 'cache hit (preprocessed)' 0
65    expect_stat 'cache miss' 1
66
67    # -------------------------------------------------------------------------
68    TEST "Rewriting in stderr"
69
70    cat <<EOF >stderr.h
71int stderr(void)
72{
73  // Trigger warning by having no return statement.
74}
75EOF
76    backdate stderr.h
77    cat <<EOF >stderr.c
78#include <stderr.h>
79EOF
80
81    CCACHE_BASEDIR=`pwd` $CCACHE_COMPILE -Wall -W -I`pwd` -c `pwd`/stderr.c -o `pwd`/stderr.o 2>stderr.txt
82    expect_stat 'cache hit (direct)' 0
83    expect_stat 'cache hit (preprocessed)' 0
84    expect_stat 'cache miss' 1
85    if grep `pwd` stderr.txt >/dev/null 2>&1; then
86        test_failed "Base dir (`pwd`) found in stderr:\n`cat stderr.txt`"
87    fi
88
89    CCACHE_BASEDIR=`pwd` $CCACHE_COMPILE -Wall -W -I`pwd` -c `pwd`/stderr.c -o `pwd`/stderr.o 2>stderr.txt
90    expect_stat 'cache hit (direct)' 1
91    expect_stat 'cache hit (preprocessed)' 0
92    expect_stat 'cache miss' 1
93    if grep `pwd` stderr.txt >/dev/null 2>&1; then
94        test_failed "Base dir (`pwd`) found in stderr:\n`cat stderr.txt`"
95    fi
96
97    # -------------------------------------------------------------------------
98    TEST "-MF/-MQ/-MT with absolute paths"
99
100    for option in MF "MF " MQ "MQ " MT "MT "; do
101        clear_cache
102        cd dir1
103        CCACHE_BASEDIR="`pwd`" $CCACHE_COMPILE -I`pwd`/include -MD -${option}`pwd`/test.d -c src/test.c
104        expect_stat 'cache hit (direct)' 0
105        expect_stat 'cache hit (preprocessed)' 0
106        expect_stat 'cache miss' 1
107        cd ..
108
109        cd dir2
110        CCACHE_BASEDIR="`pwd`" $CCACHE_COMPILE -I`pwd`/include -MD -${option}`pwd`/test.d -c src/test.c
111        expect_stat 'cache hit (direct)' 1
112        expect_stat 'cache hit (preprocessed)' 0
113        expect_stat 'cache miss' 1
114        cd ..
115    done
116
117    # -------------------------------------------------------------------------
118    # When BASEDIR is set to /, check that -MF, -MQ and -MT arguments with
119    # absolute paths are rewritten to relative and that the dependency file
120    # only contains relative paths.
121    TEST "-MF/-MQ/-MT with absolute paths and BASEDIR set to /"
122
123    for option in MF "MF " MQ "MQ " MT "MT "; do
124        clear_cache
125        cd dir1
126        CCACHE_BASEDIR="/" $CCACHE_COMPILE -I`pwd`/include -MD -${option}`pwd`/test.d -c src/test.c
127        expect_stat 'cache hit (direct)' 0
128        expect_stat 'cache hit (preprocessed)' 0
129        expect_stat 'cache miss' 1
130        # Check that there is no absolute path in the dependency file:
131        while read line; do
132            for file in $line; do
133                case $file in /*)
134                    test_failed "Absolute file path '$file' found in dependency file '`pwd`/test.d'"
135                esac
136            done
137        done <test.d
138        cd ..
139
140        cd dir2
141        CCACHE_BASEDIR="/" $CCACHE_COMPILE -I`pwd`/include -MD -${option}`pwd`/test.d -c src/test.c
142        expect_stat 'cache hit (direct)' 1
143        expect_stat 'cache hit (preprocessed)' 0
144        expect_stat 'cache miss' 1
145        cd ..
146    done
147}
148