make inplace scripts less sensitive to /bin/sh quoting by avoiding \
[ghc-hetmet.git] / utils / ghc-pkg / Makefile
index 6a4dbcf..47b4ebe 100644 (file)
@@ -1,9 +1,6 @@
 TOP=../..
 include $(TOP)/mk/boilerplate.mk
 
-# hack for ghci-inplace script, see below
-INSTALLING=1
-
 # -----------------------------------------------------------------------------
 # ghc-pkg.bin
 
@@ -51,62 +48,45 @@ Version.hs : Makefile $(TOP)/mk/config.mk
 DIST_CLEAN_FILES += $(VERSION_HS)
 
 # -----------------------------------------------------------------------------
-# ghc-pkg script
+# ghc-pkg and ghc-pkg-inplace scripts
 
-ifeq "$(INSTALLING)" "1"
-ifeq "$(BIN_DIST)"   "1"
-GHCPKGBIN=$$\"\"libexecdir/$(HS_PROG)
-PKGCONF=$$\"\"libdir/package.conf
-else
-GHCPKGBIN=$(libexecdir)/$(HS_PROG)
-PKGCONF=$(libdir)/package.conf
-endif # BIN_DIST
-else
-GHCPKGBIN=$(FPTOOLS_TOP_ABS)/utils/ghc-pkg/$(HS_PROG)
-PKGCONF=$(FPTOOLS_TOP_ABS_PLATFORM)/driver/package.conf.inplace
-endif
+# ghc-pkg-inplace used to be either a /bin/sh script, or a .bat script
+# on Windows.  It is now a real binary, compiled from a tiny .hs
+# file.  The problem with using scripts here was that the .bat script
+# cannot be executed by /bin/sh on MSYS (it can on Cygwin), but the
+# /bin/sh script cannot be executed by Cabal.  So we would have needed
+# both.  A single binary is therefore simpler.
 
-ifneq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
-INSTALLED_SCRIPT_PROG  = ghc-pkg-$(ProjectVersion)
-endif
-INPLACE_SCRIPT_PROG    = ghc-pkg-inplace
+INPLACE_HS=ghc-pkg-inplace.hs
+INPLACE_PROG=ghc-pkg-inplace
+EXCLUDED_SRCS+=$(INPLACE_HS)
 
-SCRIPT_OBJS      = ghc-pkg.sh
-INTERP           = $(SHELL)
-SCRIPT_SUBST_VARS = GHCPKGBIN PKGCONFOPT
-ifneq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
-INSTALL_SCRIPTS  += $(SCRIPT_PROG)
-endif
-PKGCONFOPT       = --global-conf $(PKGCONF)
+# FPTOOLS_TOP_ABS platform uses backslashes, at least on Cygwin, but that
+# will go wrong when we use it in a Haskell string below.
+TOP_ABS=$(subst \\,/,$(FPTOOLS_TOP_ABS_PLATFORM))
 
-ifeq "$(INSTALLING)" "1"
-SCRIPT_PROG    =  $(INSTALLED_SCRIPT_PROG)
-ifneq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
-LINK           =  ghc-pkg
-endif
-else
-SCRIPT_PROG    =  $(INPLACE_SCRIPT_PROG)
-endif
+$(INPLACE_HS): Makefile $(FPTOOLS_TOP)/mk/config.mk
+       echo "import System.Cmd; import System.Environment; import System.Exit" > $@
+       echo "main = do args <- getArgs; rawSystem \"$(TOP_ABS)/$(GHC_PKG_DIR_REL)/$(HS_PROG)\" (\"--global-conf\":\"$(TOP_ABS)/driver/package.conf.inplace\":args) >>= exitWith" >> $@
 
-# -----------------------------------------------------------------------------
-# don't recurse on 'make install'
-#
-ifeq "$(INSTALLING)" "1"
-all :: $(HS_PROG)
-       $(MAKE) INSTALLING=0 BIN_DIST=0 $(MFLAGS) $@
-clean distclean maintainer-clean ::
-       $(MAKE) INSTALLING=0 BIN_DIST=0 $(MFLAGS) $@
-endif
+$(INPLACE_PROG): $(INPLACE_HS)
+       $(GHC) --make $< -o $@
 
-ifeq "$(INSTALLING)$(HOSTPLATFORM)" "0i386-unknown-mingw32"
-all :: $(INPLACE_SCRIPT_PROG).bat
+all :: $(INPLACE_PROG)
 
-# See "MSys Note 3" in compiler/Makefile
-$(INPLACE_SCRIPT_PROG).bat :
-       echo '@call $(subst /,\,$(FPTOOLS_TOP_ABS_PLATFORM)/utils/ghc-pkg/$(HS_PROG)) --global-conf $(PKGCONF) %*' >> $@
-       chmod 755 $@
+CLEAN_FILES += $(INPLACE_HS) $(INPLACE_PROG)
 
-CLEAN_FILES += $(INPLACE_SCRIPT_PROG).bat
+ifneq "$(HOSTPLATFORM)" "i386-unknown-mingw32"
+LINK = ghc-pkg
+LINK_TARGET = $(LINK)-$(ProjectVersion)
+INSTALLED_SCRIPT=$(bindir)/$(LINK_TARGET)
+install::
+       $(RM) -f $(INSTALLED_SCRIPT)
+       echo "#!$(SHELL)"                                           >> $(INSTALLED_SCRIPT)
+       echo "GHCPKGBIN=$(libexecdir)/$(HS_PROG)"                   >> $(INSTALLED_SCRIPT)
+       echo "PKGCONF=$(libdir)/package.conf"                       >> $(INSTALLED_SCRIPT)
+       echo 'exec $$GHCPKGBIN --global-conf $$PKGCONF $${1+"$$@"}' >> $(INSTALLED_SCRIPT)
+       $(EXECUTABLE_FILE) $(INSTALLED_SCRIPT)
 endif
 
 # ghc-pkg is needed to boot in rts/ and library dirs
@@ -117,4 +97,9 @@ boot :: depend
        $(MAKE) all
 endif
 
+binary-dist:
+       $(INSTALL_DIR)                $(BIN_DIST_DIR)/utils/ghc-pkg
+       $(INSTALL_DATA)    Makefile   $(BIN_DIST_DIR)/utils/ghc-pkg/
+       $(INSTALL_PROGRAM) $(HS_PROG) $(BIN_DIST_DIR)/utils/ghc-pkg/
+
 include $(TOP)/mk/target.mk