summaryrefslogtreecommitdiff
path: root/testsuite/mk/boilerplate.mk
blob: 055c85659a3eb7bae7c152fdc83005ff400664c9 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# Eliminate use of the built-in implicit rules, and clear out the default list
# of suffixes for suffix rules. Speeds up make quite a bit. Both are needed
# for the shortest `make -d` output.
# Don't set --no-builtin-variables; some rules might stop working if you do
# (e.g. 'make clean' in testsuite/ currently relies on an implicit $RM).
MAKEFLAGS += --no-builtin-rules
.SUFFIXES:

default: all

HAVE_EVAL := NO
$(eval HAVE_EVAL := YES)

ifeq "$(HAVE_EVAL)" "NO"
$(error Your make does not support eval. You need GNU make >= 3.81)
endif

ifeq "$(abspath /)" ""
$(error Your make does not support abspath. You need GNU make >= 3.81)
endif

show:
	@echo '$(VALUE)="$($(VALUE))"'

define canonicalise
# $1 = path variable
$1_CYGPATH := $$(shell $(SHELL) -c "cygpath -m '$$($1)'" 2> /dev/null)
ifneq "$$($1_CYGPATH)" ""
# We use 'override' in case we are trying to update a value given on
# the commandline (e.g. TEST_HC)
override $1 := $$($1_CYGPATH)
endif
endef

define canonicaliseExecutable
# $1 = program path variable
ifneq "$$(shell test -x '$$($1).exe' && echo exists)" ""
# We use 'override' in case we are trying to update a value given on
# the commandline (e.g. TEST_HC)
override $1 := $$($1).exe
endif
$(call canonicalise,$1)
endef

ifeq "$(TEST_HC)" ""

# Note [Spaces in TEST_HC]
#
# Tests should be able to handle paths with spaces.
#
# One of the things ./validate (without --fast) does is check if binary
# distributions can succesfully be installed and used in paths containing
# spaces.
#
# It does so in the following way:
#    * create a binary distribution in 'bindistprep/'.
#    * install that binary distribution in 'bindisttest/install   dir/'
#    * run the testsuite with BINDIST=YES
#
# BINDIST=YES tells the testsuite driver to use
# 'bindisttest/install   dir/bin/ghc' instead of 'inplace/bin/ghc-stage2' as
# TEST_HC.
#
# Before, if a GHC developer forgot to quote TEST_HC in their Makefile when
# adding a new test, the test would fail with a puzzling "command not found:
# bindisttest/install" error (but only when validating).
#
# Therefore, we now:
#   * make sure 'bindisttest/install' does exist, and show a nice message when
#     it is executed.
#   * let the default value of TEST_HC also contain spaces
#     (i.e. 'inplace/test   spaces/ghc-stage2'), such that the test always
#     fails, also without BINDIST=YES, and again show a nice message when it
#     indeed does so, through 'inplace/test'.

# The `wildcard` function requires spaces to be escaped. Other gnu make
# functions can't seem to handle spaces at all (e.g. `abspath`).
STAGE1_TEST_SPACES := $(TOP)/../inplace/test\ \ \ spaces/ghc-stage1
STAGE1_NORMAL := $(TOP)/../inplace/bin/ghc-stage1

ifneq "$(wildcard $(STAGE1_TEST_SPACES) $(STAGE1_NORMAL))" ""
IMPLICIT_COMPILER = NO
IN_TREE_COMPILER = YES

ifneq "$(wildcard $(STAGE1_TEST_SPACES))" ""
# See Note [Spaces in TEST_HC].
STAGE1_GHC := $(abspath $(TOP)/../)/inplace/test   spaces/ghc-stage1
STAGE2_GHC := $(abspath $(TOP)/../)/inplace/test   spaces/ghc-stage2
STAGE3_GHC := $(abspath $(TOP)/../)/inplace/test   spaces/ghc-stage3
else
# Maybe we're on Windows (no symlink support), or in a bindist or sdist, which
# don't have the 'test   spaces' symlink.
STAGE1_GHC := $(abspath $(TOP)/../)/inplace/bin/ghc-stage1
STAGE2_GHC := $(abspath $(TOP)/../)/inplace/bin/ghc-stage2
STAGE3_GHC := $(abspath $(TOP)/../)/inplace/bin/ghc-stage3
endif

ifeq "$(BINDIST)" "YES"
# See Note [Spaces in TEST_HC].
TEST_HC := $(abspath $(TOP)/../)/bindisttest/install   dir/bin/ghc
else ifeq "$(stage)" "1"
TEST_HC := $(STAGE1_GHC)
else ifeq "$(stage)" "3"
TEST_HC := $(STAGE3_GHC)
else
# use stage2 by default
TEST_HC := $(STAGE2_GHC)
endif

else
IMPLICIT_COMPILER = YES
IN_TREE_COMPILER = NO
TEST_HC := $(shell which ghc)
endif

else # neq "$(TEST_HC)" ""

ifeq "$(TEST_HC)" "ghc"
IMPLICIT_COMPILER = YES
else
IMPLICIT_COMPILER = NO
endif
IN_TREE_COMPILER = NO
# We want to support both "ghc" and "/usr/bin/ghc" as values of TEST_HC
# passed in by the user, but
#     which ghc          == /usr/bin/ghc
#     which /usr/bin/ghc == /usr/bin/ghc
# so on unix-like platforms we can just always 'which' it.
# However, on cygwin, we can't just use which:
#     $ which c:/ghc/ghc-7.4.1/bin/ghc.exe
#     which: no ghc.exe in (./c:/ghc/ghc-7.4.1/bin)
# so we start off by using realpath, and if that succeeds then we use
# that value. Otherwise we fall back on 'which'.
#
# Note also that we need to use 'override' in order to override a
# value given on the commandline.
TEST_HC_REALPATH := $(realpath $(TEST_HC))
ifeq "$(TEST_HC_REALPATH)" ""
override TEST_HC := $(shell which '$(TEST_HC)')
else
override TEST_HC := $(TEST_HC_REALPATH)
endif
endif # "$(TEST_HC)" ""

# We can't use $(dir ...) here as TEST_HC might be in a path
# containing spaces
BIN_ROOT = $(shell dirname '$(TEST_HC)')

ifeq "$(IMPLICIT_COMPILER)" "YES"
find_tool = $(shell which $(1))
else
find_tool = $(BIN_ROOT)/$(1)
endif

ifeq "$(GHC_PKG)" ""
GHC_PKG := $(call find_tool,ghc-pkg)
endif

ifeq "$(RUNGHC)" ""
RUNGHC := $(call find_tool,runghc)
endif

ifeq "$(HADDOCK)" ""
HADDOCK := $(call find_tool,haddock)
endif

ifeq "$(HSC2HS)" ""
HSC2HS := $(call find_tool,hsc2hs)
endif

ifeq "$(HP2PS_ABS)" ""
HP2PS_ABS := $(call find_tool,hp2ps)
endif

ifeq "$(HPC)" ""
HPC := $(call find_tool,hpc)
endif

$(eval $(call canonicaliseExecutable,TEST_HC))
ifeq "$(shell test -x '$(TEST_HC)' && echo exists)" ""
$(error Cannot find ghc: $(TEST_HC))
endif

$(eval $(call canonicaliseExecutable,GHC_PKG))
ifeq "$(shell test -x '$(GHC_PKG)' && echo exists)" ""
$(error Cannot find ghc-pkg: $(GHC_PKG))
endif

$(eval $(call canonicaliseExecutable,HADDOCK))
ifeq "$(shell test -x '$(HADDOCK)' && echo exists)" ""
# haddock is optional. Use 'override' to override canonicalise's override...
override HADDOCK :=
endif

$(eval $(call canonicaliseExecutable,HSC2HS))
ifeq "$(shell test -x '$(HSC2HS)' && echo exists)" ""
$(error Cannot find hsc2hs: $(HSC2HS))
endif

$(eval $(call canonicaliseExecutable,HP2PS_ABS))
ifeq "$(shell test -x '$(HP2PS_ABS)' && echo exists)" ""
$(error Cannot find hp2ps: $(HP2PS_ABS))
endif

$(eval $(call canonicaliseExecutable,HPC))
ifeq "$(shell test -x '$(HPC)' && echo exists)" ""
$(error Cannot find hpc: $(HPC))
endif

# Be careful when using this. On Windows it ends up looking like
# c:/foo/bar which confuses make, as make thinks that the : is Makefile
# syntax
TOP_ABS := $(abspath $(TOP))
$(eval $(call canonicalise,TOP_ABS))

GS = gs
CP = cp
RM = rm -f
PYTHON = python
ifeq "$(shell $(SHELL) -c 'python2 -c 0' 2> /dev/null && echo exists)" "exists"
PYTHON = python2
endif

CHECK_API_ANNOTATIONS := $(abspath $(TOP)/../inplace/bin/check-api-annotations)

# -----------------------------------------------------------------------------
# configuration of TEST_HC

# ghc-config.hs is a short Haskell program that runs ghc --info, parses
# the results, and emits a little .mk file with make bindings for the values.
# This way we cache the results for different values of $(TEST_HC)

$(TOP)/mk/ghc-config : $(TOP)/mk/ghc-config.hs
	"$(TEST_HC)" --make -o $@ $<

empty=
space=$(empty) $(empty)
ghc-config-mk = $(TOP)/mk/ghcconfig$(subst $(space),_,$(subst :,_,$(subst /,_,$(subst \,_,$(TEST_HC))))).mk

$(ghc-config-mk) : $(TOP)/mk/ghc-config
	$(TOP)/mk/ghc-config "$(TEST_HC)" >"$@"; if [ $$? != 0 ]; then $(RM) "$@"; exit 1; fi
# If the ghc-config fails, remove $@, and fail

# Note: $(CLEANING) is not defined in the testsuite.
ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
include $(ghc-config-mk)
endif

ifeq "$(GhcDynamic)" "YES"
ghcThWayFlags     = -dynamic
ghciWayFlags      = -dynamic
ghcPluginWayFlags = -dynamic
else
ghcThWayFlags     = -static
ghciWayFlags      = -static
ghcPluginWayFlags = -static
endif

# -----------------------------------------------------------------------------

ifeq "$(HostOS)" "mingw32"
WINDOWS = YES
else
WINDOWS = NO
endif
ifeq "$(HostOS)" "darwin"
DARWIN = YES
else
DARWIN = NO
endif