boilerplate.mk 4.6 KB
Newer Older
1

2 3
default: all

Ian Lynagh's avatar
Ian Lynagh committed
4 5 6 7
HAVE_EVAL := NO
$(eval HAVE_EVAL := YES)

ifeq "$(HAVE_EVAL)" "NO"
8 9 10 11 12
$(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)
Ian Lynagh's avatar
Ian Lynagh committed
13 14
endif

Ian Lynagh's avatar
Ian Lynagh committed
15 16 17
show:
	@echo '$(VALUE)="$($(VALUE))"'

18
define canonicalise
19
# $1 = path variable
Ian Lynagh's avatar
Ian Lynagh committed
20
$1_CYGPATH := $$(shell $(SHELL) -c "cygpath -m '$$($1)'" 2> /dev/null)
21
ifneq "$$($1_CYGPATH)" ""
Ian Lynagh's avatar
Ian Lynagh committed
22 23 24
# We use 'override' in case we are trying to update a value given on
# the commandline (e.g. TEST_HC)
override $1 := $$($1_CYGPATH)
25 26 27
endif
endef

28 29
define canonicaliseExecutable
# $1 = program path variable
30
ifneq "$$(shell test -x '$$($1).exe' && echo exists)" ""
Ian Lynagh's avatar
Ian Lynagh committed
31 32 33
# We use 'override' in case we are trying to update a value given on
# the commandline (e.g. TEST_HC)
override $1 := $$($1).exe
34 35 36 37
endif
$(call canonicalise,$1)
endef

38 39
ifeq "$(TEST_HC)" ""

40 41 42
STAGE1_GHC := $(abspath $(TOP)/../inplace/bin/ghc-stage1)
STAGE2_GHC := $(abspath $(TOP)/../inplace/bin/ghc-stage2)
STAGE3_GHC := $(abspath $(TOP)/../inplace/bin/ghc-stage3)
43

44
ifneq "$(wildcard $(STAGE1_GHC) $(STAGE1_GHC).exe)" ""
45

46
IN_TREE_COMPILER = YES
47
ifeq "$(BINDIST)" "YES"
48
TEST_HC := $(abspath $(TOP)/../)/bindisttest/install   dir/bin/ghc
49 50 51
else ifeq "$(stage)" "1"
TEST_HC := $(STAGE1_GHC)
else ifeq "$(stage)" "3"
52
TEST_HC := $(STAGE3_GHC)
53 54
else
# use stage2 by default
55
TEST_HC := $(STAGE2_GHC)
56 57
endif

58
else
59
IN_TREE_COMPILER = NO
60 61 62
TEST_HC := $(shell which ghc)
endif

63 64
else
IN_TREE_COMPILER = NO
Ian Lynagh's avatar
Ian Lynagh committed
65 66 67 68
# 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
Ian Lynagh's avatar
Ian Lynagh committed
69 70 71 72 73 74 75 76 77 78 79
# 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)" ""
Ian Lynagh's avatar
Ian Lynagh committed
80
override TEST_HC := $(shell which '$(TEST_HC)')
Ian Lynagh's avatar
Ian Lynagh committed
81 82 83
else
override TEST_HC := $(TEST_HC_REALPATH)
endif
84
endif
85

86 87 88
# We can't use $(dir ...) here as TEST_HC might be in a path
# containing spaces
BIN_ROOT = $(shell dirname '$(TEST_HC)')
Ian Lynagh's avatar
Ian Lynagh committed
89

90
ifeq "$(GHC_PKG)" ""
Ian Lynagh's avatar
Ian Lynagh committed
91
GHC_PKG := $(BIN_ROOT)/ghc-pkg
92
endif
93

94
ifeq "$(HSC2HS)" ""
Ian Lynagh's avatar
Ian Lynagh committed
95
HSC2HS := $(BIN_ROOT)/hsc2hs
96 97
endif

98
ifeq "$(HP2PS_ABS)" ""
Ian Lynagh's avatar
Ian Lynagh committed
99
HP2PS_ABS := $(BIN_ROOT)/hp2ps
100 101
endif

102
ifeq "$(HPC)" ""
Ian Lynagh's avatar
Ian Lynagh committed
103
HPC := $(BIN_ROOT)/hpc
104 105
endif

Ian Lynagh's avatar
Ian Lynagh committed
106
$(eval $(call canonicaliseExecutable,TEST_HC))
107
ifeq "$(shell test -x '$(TEST_HC)' && echo exists)" ""
Ian Lynagh's avatar
Ian Lynagh committed
108
$(error Cannot find ghc: $(TEST_HC))
109
endif
110

111
$(eval $(call canonicaliseExecutable,GHC_PKG))
112
ifeq "$(shell test -x '$(GHC_PKG)' && echo exists)" ""
Ian Lynagh's avatar
Ian Lynagh committed
113
$(error Cannot find ghc-pkg: $(GHC_PKG))
114
endif
115

116
$(eval $(call canonicaliseExecutable,HSC2HS))
117
ifeq "$(shell test -x '$(HSC2HS)' && echo exists)" ""
Ian Lynagh's avatar
Ian Lynagh committed
118
$(error Cannot find hsc2hs: $(HSC2HS))
119 120
endif

121
$(eval $(call canonicaliseExecutable,HP2PS_ABS))
122
ifeq "$(shell test -x '$(HP2PS_ABS)' && echo exists)" ""
Ian Lynagh's avatar
Ian Lynagh committed
123
$(error Cannot find hp2ps: $(HP2PS_ABS))
124 125
endif

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

131 132 133
# 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
134 135 136
TOP_ABS := $(abspath $(TOP))
$(eval $(call canonicalise,TOP_ABS))

137 138 139
GS = gs
CP = cp
RM = rm -f
140
PYTHON = python
141 142 143
ifeq "$(shell $(SHELL) -c 'python2 -c 0' 2> /dev/null && echo exists)" "exists"
PYTHON = python2
endif
144

145 146 147 148 149 150 151
# -----------------------------------------------------------------------------
# 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)

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

Simon Marlow's avatar
Simon Marlow committed
155 156
empty=
space=$(empty) $(empty)
157
ghc-config-mk = $(TOP)/mk/ghcconfig$(subst $(space),_,$(subst :,_,$(subst /,_,$(subst \,_,$(TEST_HC))))).mk
158

159 160 161
$(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
162

163
ifeq "$(findstring clean,$(MAKECMDGOALS))" ""
164
include $(ghc-config-mk)
165
endif
166 167 168

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

169 170 171 172 173
ifeq "$(HostOS)" "mingw32"
WINDOWS = YES
else
WINDOWS = NO
endif
Ian Lynagh's avatar
Ian Lynagh committed
174 175 176 177 178
ifeq "$(HostOS)" "darwin"
DARWIN = YES
else
DARWIN = NO
endif
179