All installed Haskell prgms have an inplace and an installed version
[ghc-hetmet.git] / utils / hsc2hs / Makefile
1 # -----------------------------------------------------------------------------
2 # To compile with nhc98 on unix:
3 #     nhc98 -cpp -package base -o hsc2hs-bin Main.hs
4
5 TOP=../..
6 include $(TOP)/mk/boilerplate.mk
7
8 # This causes libghccompat.a to be used:
9 include $(GHC_COMPAT_DIR)/compat.mk
10
11 # This is required because libghccompat.a must be built with
12 # $(GhcHcOpts) because it is linked to the compiler, and hence
13 # we must also build with $(GhcHcOpts) here:
14 SRC_HC_OPTS += $(GhcHcOpts) $(GhcStage1HcOpts)
15
16 # We have two version: the inplace version compiled by the bootstrap compiler
17 #   and the install version compiled by the stage 1 compiler
18 ifeq "$(stage)" "2"
19 HS_PROG           = hsc2hs.bin
20 else
21 HS_PROG           = hsc2hs-inplace.bin
22 endif
23
24 ifeq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
25 HS_PROG           = hsc2hs$(exeext)
26 endif
27 ifeq "$(HOSTPLATFORM)" "i386-unknown-cygwinw32"
28 HS_PROG           = hsc2hs$(exeext)
29 endif
30
31 # Note: Somehow we should pass $(exeext) here, but the history of changes used
32 # for calling the C preprocessor via GHC has changed a few times, making a
33 # clean solution impossible. So we revert to a hack in Main.hs...
34 SRC_HC_OPTS      += -Wall
35
36 # -----------------------------------------------------------------------------
37 # hsc2hs script
38
39 INPLACE_HS=hsc2hs-inplace.hs
40 INPLACE_PROG=hsc2hs-inplace
41 EXCLUDED_SRCS+=$(INPLACE_HS)
42
43 ifeq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
44 extra_flags=$(addprefix --cflag=,$(filter-out -O,$(SRC_CC_OPTS)))
45 else
46 extra_flags=
47 endif
48
49 nothing=
50 space=$(nothing) $(nothing)
51 # Given
52 #   foo bar
53 # make
54 #   :\"-Ifoo\":\"-Ibar\"
55 GMP_INCLUDE_DIRS_STRINGS = $(subst $(space),,$(foreach d,$(GMP_INCLUDE_DIRS),:\"-I$(d)\"))
56
57 $(INPLACE_HS): Makefile $(FPTOOLS_TOP)/mk/config.mk
58         echo "import System.Cmd; import System.Environment; import System.Exit" > $@
59         echo "main = do args <- getArgs; rawSystem \"$(FPTOOLS_TOP_ABS)/$(GHC_HSC2HS_DIR_REL)/$(HS_PROG)\" (\"--template=$(FPTOOLS_TOP_ABS)/$(GHC_HSC2HS_DIR_REL)/template-hsc.h\":\"--cc=$(CC)\":\"--ld=$(CC)\"$(patsubst %,:\"%\",$(extra_flags)):\"--cflag=-D__GLASGOW_HASKELL__=$(ProjectVersionInt)\":\"-I$(FPTOOLS_TOP_ABS)/$(GHC_INCLUDE_DIR_REL)\":\"-I$(FPTOOLS_TOP_ABS)/gmp/gmpbuild\"$(GMP_INCLUDE_DIRS_STRINGS):args) >>= exitWith" >> $@
60
61 $(INPLACE_PROG): $(INPLACE_HS)
62         $(HC) --make $< -o $@
63
64 all :: $(INPLACE_PROG)
65
66 CLEAN_FILES += $(INPLACE_HS) $(INPLACE_PROG)
67
68 ifeq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
69 INSTALL_PROGS    += $(HS_PROG)
70 else
71 INSTALL_LIBEXECS += $(HS_PROG)
72 LINK = hsc2hs-ghc
73 LINK_TARGET = $(LINK)-$(ProjectVersion)
74 INSTALLED_SCRIPT=$(DESTDIR)$(bindir)/$(LINK_TARGET)
75 install::
76         $(RM) -f $(INSTALLED_SCRIPT)
77         echo "#!$(SHELL)"                  >> $(INSTALLED_SCRIPT)
78         echo "HSC2HS_BINDIR=$(libexecdir)" >> $(INSTALLED_SCRIPT)
79         echo "HSC2HS_DIR=$(libdir)"        >> $(INSTALLED_SCRIPT)
80         echo "HS_PROG=$(HS_PROG)"          >> $(INSTALLED_SCRIPT)
81         echo "HSC2HS_EXTRA="               >> $(INSTALLED_SCRIPT)
82         cat hsc2hs.sh                      >> $(INSTALLED_SCRIPT)
83         $(EXECUTABLE_FILE) $(INSTALLED_SCRIPT)
84
85 ifneq "$(NO_INSTALL_HSC2HS)" "YES"
86 install::
87         $(CP) $(INSTALLED_SCRIPT) $(DESTDIR)/$(bindir)/hsc2hs
88 endif
89 endif
90
91 # -----------------------------------------------------------------------------
92
93 override datadir=$(libdir)
94 INSTALL_DATAS += template-hsc.h
95
96 binary-dist:
97         $(INSTALL_DIR)                      $(BIN_DIST_DIR)/utils/hsc2hs
98         $(INSTALL_DATA)    Makefile         $(BIN_DIST_DIR)/utils/hsc2hs/
99         $(INSTALL_DATA)    hsc2hs.sh        $(BIN_DIST_DIR)/utils/hsc2hs/
100         $(INSTALL_DATA)    $(INSTALL_DATAS) $(BIN_DIST_DIR)/utils/hsc2hs/
101         $(INSTALL_PROGRAM) $(HS_PROG)       $(BIN_DIST_DIR)/utils/hsc2hs/
102
103 include $(TOP)/mk/target.mk