Merge branch 'master' of darcs.haskell.org:/srv/darcs//ghc
[ghc.git] / rules / shell-wrapper.mk
1 # -----------------------------------------------------------------------------
2 #
3 # (c) 2009 The University of Glasgow
4 #
5 # This file is part of the GHC build system.
6 #
7 # To understand how the build system works and how to modify it, see
8 # http://hackage.haskell.org/trac/ghc/wiki/Building/Architecture
9 # http://hackage.haskell.org/trac/ghc/wiki/Building/Modifying
10 #
11 # -----------------------------------------------------------------------------
12
13 define shell-wrapper
14 $(call trace, shell-wrapper($1,$2))
15 $(call profStart, shell-wrapper($1,$2))
16 # $1 = dir
17 # $2 = distdir
18
19 ifeq "$$($1_$2_SHELL_WRAPPER_NAME)" ""
20 $1_$2_SHELL_WRAPPER_NAME = $1/$$($1_$2_PROG).wrapper
21 endif
22
23 ifeq "$$($1_$2_WANT_INPLACE_WRAPPER)" "YES"
24
25 ifeq "$$($1_$2_TOPDIR)" "YES"
26 INPLACE_WRAPPER = $$(INPLACE_LIB)/$$($1_$2_PROG)
27 else
28 INPLACE_WRAPPER = $$(INPLACE_BIN)/$$($1_$2_PROG)
29 endif
30
31 all_$1_$2 : $$(INPLACE_WRAPPER)
32
33 $$(INPLACE_BIN)/$$($1_$2_PROG): WRAPPER=$$@
34 ifeq "$$($1_$2_SHELL_WRAPPER)" "YES"
35 $$(INPLACE_WRAPPER): $$($1_$2_SHELL_WRAPPER_NAME)
36 endif
37 $$(INPLACE_WRAPPER): $$($1_$2_INPLACE)
38 $$(call removeFiles, $$@)
39 echo '#!$$(SHELL)' >> $$@
40 echo 'executablename="$$(TOP)/$$<"' >> $$@
41 echo 'datadir="$$(TOP)/$$(INPLACE_LIB)"' >> $$@
42 echo 'bindir="$$(TOP)/$$(INPLACE_BIN)"' >> $$@
43 echo 'topdir="$$(TOP)/$$(INPLACE_TOPDIR)"' >> $$@
44 echo 'pgmgcc="$$(WhatGccIsCalled)"' >> $$@
45 $$($1_$2_SHELL_WRAPPER_EXTRA)
46 $$($1_$2_INPLACE_SHELL_WRAPPER_EXTRA)
47 ifeq "$$(DYNAMIC_BY_DEFAULT)" "YES"
48 echo 'export LD_LIBRARY_PATH="$$($1_$2_DEP_LIB_DIRS_SEARCHPATH)"' >> $$@
49 endif
50 ifeq "$$($1_$2_SHELL_WRAPPER)" "YES"
51 cat $$($1_$2_SHELL_WRAPPER_NAME) >> $$@
52 else
53 echo 'exec "$$$$executablename" $$$${1+"$$$$@"}' >> $$@
54 endif
55 $$(EXECUTABLE_FILE) $$@
56
57 endif
58
59 ifeq "$$($1_$2_WANT_INSTALLED_WRAPPER)" "YES"
60
61 ifeq "$$($1_$2_INSTALL_SHELL_WRAPPER_NAME)" ""
62 $1_$2_INSTALL_SHELL_WRAPPER_NAME = $$($1_$2_PROG)
63 endif
64
65 # Install the binary in $(libexecdir), and install a shell wrapper in $(bindir)
66 INSTALL_LIBEXECS += $1/$2/build/tmp/$$($1_$2_PROG)
67 BINDIST_WRAPPERS += $$($1_$2_SHELL_WRAPPER_NAME)
68
69 install: install_$1_$2_wrapper
70
71 .PHONY: install_$1_$2_wrapper
72 install_$1_$2_wrapper: WRAPPER=$$(DESTDIR)$$(bindir)/$(CrossCompilePrefix)$$($1_$2_INSTALL_SHELL_WRAPPER_NAME)
73 install_$1_$2_wrapper:
74 $$(call INSTALL_DIR,"$$(DESTDIR)$$(bindir)")
75 $$(call removeFiles, "$$(WRAPPER)")
76 $$(CREATE_SCRIPT) "$$(WRAPPER)"
77 echo '#!$$(SHELL)' >> "$$(WRAPPER)"
78 echo 'exedir="$$(ghclibexecdir)/bin"' >> "$$(WRAPPER)"
79 echo 'exeprog="$$($1_$2_PROG)"' >> "$$(WRAPPER)"
80 echo 'executablename="$$$$exedir/$$$$exeprog"' >> "$$(WRAPPER)"
81 echo 'datadir="$$(datadir)"' >> "$$(WRAPPER)"
82 echo 'bindir="$$(bindir)"' >> "$$(WRAPPER)"
83 echo 'topdir="$$(topdir)"' >> "$$(WRAPPER)"
84 $$($1_$2_SHELL_WRAPPER_EXTRA)
85 $$($1_$2_INSTALL_SHELL_WRAPPER_EXTRA)
86 cat $$($1_$2_SHELL_WRAPPER_NAME) >> "$$(WRAPPER)"
87 $$(EXECUTABLE_FILE) "$$(WRAPPER)"
88
89 endif
90
91 $(call profEnd, shell-wrapper($1,$2))
92 endef