Mac OS X deployment target: piping opts through Makefiles
[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 # Beyond stage 1, honour any Mac OS X depolyment target options.  If we use 
9 # these options in stage 1 we get a linker error if the bootstrap compiler is
10 #  for a more recent OS version
11 ifeq "$(stage)" "2"
12 SRC_CC_OPTS += $(MACOSX_DEPLOYMENT_CC_OPTS)
13 SRC_HC_OPTS += $(addprefix -optc, $(MACOSX_DEPLOYMENT_CC_OPTS))
14 SRC_LD_OPTS += $(addprefix -optl, $(MACOSX_DEPLOYMENT_LD_OPTS))
15 endif
16
17 # This causes libghccompat.a to be used:
18 include $(GHC_COMPAT_DIR)/compat.mk
19
20 # This is required because libghccompat.a must be built with
21 # $(GhcHcOpts) because it is linked to the compiler, and hence
22 # we must also build with $(GhcHcOpts) here:
23 SRC_HC_OPTS += $(GhcHcOpts) $(GhcStage1HcOpts)
24
25 # We have two version: the inplace version compiled by the bootstrap compiler
26 #   and the install version compiled by the stage 1 compiler
27 ifeq "$(stage)" "2"
28 HS_PROG           = hsc2hs.bin
29 else
30 HS_PROG           = hsc2hs-inplace.bin
31 endif
32
33 ifeq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
34 HS_PROG           = hsc2hs$(exeext)
35 endif
36 ifeq "$(HOSTPLATFORM)" "i386-unknown-cygwinw32"
37 HS_PROG           = hsc2hs$(exeext)
38 endif
39
40 # Note: Somehow we should pass $(exeext) here, but the history of changes used
41 # for calling the C preprocessor via GHC has changed a few times, making a
42 # clean solution impossible. So we revert to a hack in Main.hs...
43 SRC_HC_OPTS      += -Wall
44
45 # -----------------------------------------------------------------------------
46 # hsc2hs script
47
48 INPLACE_HS=hsc2hs-inplace.hs
49 INPLACE_PROG=hsc2hs-inplace
50 EXCLUDED_SRCS+=$(INPLACE_HS)
51
52 ifeq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
53 extra_flags=$(addprefix --cflag=,$(filter-out -O,$(SRC_CC_OPTS)))
54 else
55 extra_flags=
56 endif
57
58 nothing=
59 space=$(nothing) $(nothing)
60 # Given
61 #   foo bar
62 # make
63 #   :\"-Ifoo\":\"-Ibar\"
64 GMP_INCLUDE_DIRS_STRINGS = $(subst $(space),,$(foreach d,$(GMP_INCLUDE_DIRS),:\"-I$(d)\"))
65
66 $(INPLACE_HS): Makefile $(FPTOOLS_TOP)/mk/config.mk
67         echo "import System.Cmd; import System.Environment; import System.Exit" > $@
68         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" >> $@
69
70 $(INPLACE_PROG): $(INPLACE_HS)
71         $(HC) --make $< -o $@
72
73 all :: $(INPLACE_PROG)
74
75 CLEAN_FILES += $(INPLACE_HS) $(INPLACE_PROG)
76
77 ifeq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
78 INSTALL_PROGS    += $(HS_PROG)
79 else
80 INSTALL_LIBEXECS += $(HS_PROG)
81 LINK = hsc2hs-ghc
82 LINK_TARGET = $(LINK)-$(ProjectVersion)
83 INSTALLED_SCRIPT=$(DESTDIR)$(bindir)/$(LINK_TARGET)
84 install::
85         $(RM) -f $(INSTALLED_SCRIPT)
86         echo "#!$(SHELL)"                  >> $(INSTALLED_SCRIPT)
87         echo "HSC2HS_BINDIR=$(libexecdir)" >> $(INSTALLED_SCRIPT)
88         echo "HSC2HS_DIR=$(libdir)"        >> $(INSTALLED_SCRIPT)
89         echo "HS_PROG=$(HS_PROG)"          >> $(INSTALLED_SCRIPT)
90         echo "HSC2HS_EXTRA="               >> $(INSTALLED_SCRIPT)
91         cat hsc2hs.sh                      >> $(INSTALLED_SCRIPT)
92         $(EXECUTABLE_FILE) $(INSTALLED_SCRIPT)
93
94 ifneq "$(NO_INSTALL_HSC2HS)" "YES"
95 install::
96         $(CP) $(INSTALLED_SCRIPT) $(DESTDIR)/$(bindir)/hsc2hs
97 endif
98 endif
99
100 # -----------------------------------------------------------------------------
101
102 override datadir=$(libdir)
103 INSTALL_DATAS += template-hsc.h
104
105 binary-dist:
106         $(INSTALL_DIR)                      $(BIN_DIST_DIR)/utils/hsc2hs
107         $(INSTALL_DATA)    Makefile         $(BIN_DIST_DIR)/utils/hsc2hs/
108         $(INSTALL_DATA)    hsc2hs.sh        $(BIN_DIST_DIR)/utils/hsc2hs/
109         $(INSTALL_DATA)    $(INSTALL_DATAS) $(BIN_DIST_DIR)/utils/hsc2hs/
110         $(INSTALL_PROGRAM) $(HS_PROG)       $(BIN_DIST_DIR)/utils/hsc2hs/
111
112 include $(TOP)/mk/target.mk