@@ -16,10 +16,9 @@ CONFIGURE := configure
1616#-------------------------------------------------------------------------------
1717# Cabal constants
1818#-------------------------------------------------------------------------------
19−NAME := $(shell sed -ne 's/^[Nn]ame:[[:space:]]*//p' $(CABAL))
20−THIS := $(shell echo $(NAME) | tr A-Z a-z)
19+PKG := $(shell sed -ne 's/^[Nn]ame:[[:space:]]*//p' $(CABAL) | tr A-Z a-z)
2120VERSION := $(shell sed -ne 's/^[Vv]ersion:[[:space:]]*//p' $(CABAL))
22−RELNAME := $(THIS)-$(VERSION)
21+PKGID := $(PKG)-$(VERSION)
2322EXECSBASE := $(shell sed -ne 's/^[Ee]xecutable:[[:space:]]*//p' $(CABAL))
2423
2524#-------------------------------------------------------------------------------
@@ -30,9 +29,9 @@ WRAPPERS := html2markdown markdown2pdf hsmarkdown
3029EXTENSION := $(shell uname | tr '[:upper:]' '[:lower:]' | \
3130 sed -ne 's/^cygwin.*$$/\.exe/p')
3231EXECS := $(addsuffix $(EXTENSION),$(EXECSBASE))
33−PROGS := $(EXECS) $(WRAPPERS)
32+PROGS := $(EXECS) $(WRAPPERS)
3433MAIN := $(firstword $(EXECS))
35−DOCS := README.html README BUGS
34+DOCS := README.html README BUGS
3635MANPAGES := $(patsubst %.md,%,$(wildcard $(MANDIR)/man?/*.?.md))
3736
3837#-------------------------------------------------------------------------------
@@ -41,7 +40,7 @@ MANPAGES := $(patsubst %.md,%,$(wildcard $(MANDIR)/man?/*.?.md))
4140
4241# Specify default values.
4342prefix := /usr/local
44−destdir :=
43+destdir :=
4544# Attempt to set variables from a previous make session.
4645-include $(BUILDVARS)
4746# Fallback to defaults but allow to get the values from environment.
@@ -54,11 +53,9 @@ DESTDIR ?= $(destdir)
5453DESTPATH := $(DESTDIR)$(PREFIX)
5554BINPATH := $(DESTPATH)/bin
5655DATAPATH := $(DESTPATH)/share
57−LIBPATH := $(DESTPATH)/$(NAME)-$(VERSION)
58−DOCPATH := $(DATAPATH)/doc/$(THIS)
59−LIBDOCPATH := $(DATAPATH)/doc/$(THIS)-doc
6056MANPATH := $(DATAPATH)/man
61−PKGPATH := $(DATAPATH)/$(THIS)
57+PKGDATAPATH := $(DATAPATH)/$(PKGID)
58+PKGDOCPATH := $(PKGDATAPATH)/doc
6259
6360#-------------------------------------------------------------------------------
6461# Generic Makefile variables
@@ -97,7 +94,7 @@ templates: $(SRCDIR)/templates
9794 $(MAKE) -C $(SRCDIR)/templates
9895
9996define generate-shell-script
100−echo "Generating $@..."; \
97+echo >&2 "Generating $@..."; \
10198awk ' \
10299 /^[ \t]*###+ / { \
103100 lead = $$0; sub(/[^ \t].*$$/, "", lead); \
@@ -136,8 +133,8 @@ build-exec: $(PROGS)
136133cleanup_files+=$(EXECS)
137134$(EXECS): build
138135 for f in $@; do \
139− find $(BUILDDIR) -type f -name "$$f" \
140− -perm +a=x -exec ln -s -f {} . \; ; \
136+ find $(BUILDDIR) -type f -name "$$f" -perm +a=x \
137+ -exec ln -s -f {} . \; ; \
141138 done
142139
143140.PHONY: build-doc
@@ -162,42 +159,29 @@ build-all: build-program build-lib-doc
162159.PHONY: install-doc uninstall-doc
163160man_all:=$(patsubst $(MANDIR)/%,%,$(MANPAGES))
164161install-doc: build-doc
165− $(INSTALL) -d $(DOCPATH) && $(INSTALL_DATA) $(DOCS) $(DOCPATH)/
162+ $(INSTALL) -d $(PKGDOCPATH) && $(INSTALL_DATA) $(DOCS) $(PKGDOCPATH)/
166163 for f in $(man_all); do \
167164 $(INSTALL) -d $(MANPATH)/$$(dirname $$f); \
168165 $(INSTALL_DATA) $(MANDIR)/$$f $(MANPATH)/$$f; \
169166 done
170167uninstall-doc:
171− -for f in $(DOCS); do rm -f $(DOCPATH)/$$f; done
168+ -for f in $(DOCS); do rm -f $(PKGDOCPATH)/$$f; done
172169 -for f in $(man_all); do rm -f $(MANPATH)/$$f; done
173− -rmdir $(DOCPATH)
174−
175−# Library documents installation.
176−.PHONY: install-lib-doc uninstall-lib-doc
177−install-lib-doc: build-lib-doc
178− $(INSTALL) -d $(LIBDOCPATH) && cp -R html $(LIBDOCPATH)/
179−uninstall-lib-doc:
180− -rm -rf $(LIBDOCPATH)/html
181− -rmdir $(LIBDOCPATH)
182−
183−# Helper to install the given files $(1) into the path $(2).
184−# It also has the ability to follow symlinks.
185−install-executable-files = \
186− $(INSTALL) -d $(2); \
187− for f in $(1); do \
188− if [ -L $$f ]; then \
189− f=$$(readlink $$f); \
190− fi; \
191− $(INSTALL_PROGRAM) $$f $(2)/; \
192− done
170+ rmdir $(PKGDOCPATH) $(PKGDATAPATH) 2>/dev/null ||:
193171
194172# Program only installation.
195173.PHONY: install-exec uninstall-exec
196174install-exec: build-exec
197175 $(STRIP) $(EXECS)
198− $(call install-executable-files,$(PROGS),$(BINPATH))
176+ $(INSTALL) -d $(BINPATH); \
177+ for f in $(PROGS); do \
178+ if [ -L $$f ]; then \
179+ f=$$(readlink $$f); \
180+ fi; \
181+ $(INSTALL_PROGRAM) $$f $(BINPATH)/; \
182+ done
199183uninstall-exec:
200− -for f in $(notdir $(PROGS)); do rm -f $(BINPATH)/$$f; done ;
184+ -for f in $(notdir $(PROGS)); do rm -f $(BINPATH)/$$f; done
201185
202186# Program + user documents installation.
203187.PHONY: install-program uninstall-program
@@ -205,9 +189,8 @@ install-program: install-exec install-doc
205189uninstall-program: uninstall-exec uninstall-doc
206190
207191.PHONY: install-all uninstall-all
208−# Install libraries
209−install-all: build-all install-doc install-lib-doc
210− # Install the library (+ main executable) and register it.
192+# Full installation through Cabal: main + wrappers + user docs + lib + lib docs
193+install-all: install-program
211194 destdir=$(DESTDIR); \
212195 # Older Cabal versions have no '--destdir' option.
213196 if $(BUILDCMD) copy --help | grep -q '\-\-destdir'; then \
@@ -215,22 +198,24 @@ install-all: build-all install-doc install-lib-doc
215198 else \
216199 opt="--copy-prefix=$${destdir}$(PREFIX)"; \
217200 fi; \
218− $(BUILDCMD) copy $$opt; \
219− $(BUILDCMD) register
220− # Note that, we are in the position of having to install the wrappers
221− # separately, as Cabal installs the main exec along with the library.
222− $(call install-executable-files,$(WRAPPERS),$(BINPATH))
223−uninstall-all: uninstall-program uninstall-lib-doc
224− -pkg_id="$(NAME)-$(VERSION)"; \
225− libdir=$$($(GHC_PKG) field $$pkg_id library-dirs 2>/dev/null | \
201+ $(BUILDCMD) copy $$opt; $(BUILDCMD) register
202+# Cabal lacks an 'uninstall' command. We have to remove some cruft manually.
203+uninstall-all: uninstall-program
204+ @libdir=$$($(GHC_PKG) field $(PKGID) library-dirs 2>/dev/null | \
226205 sed 's/^library-dirs: *//'); \
227− if [ -d "$$libdir" ]; then \
228− $(BUILDCMD) unregister; \
229− rm -rf $$libdir; \
230− rmdir $$(dirname $$libdir); \
206+ htmldir=$$($(GHC_PKG) field $(PKGID) haddock-html 2>/dev/null | \
207+ sed 's/^haddock-html: *//'); \
208+ if [ -d $$libdir ]; then \
209+ $(BUILDCMD) unregister ||:; \
231210 else \
232− echo "*** Couldn't locate library files for pkgid: $$pkg_id. ***"; \
233− fi
211+ echo >&2 "*** Couldn't locate library for pkgid: $(PKGID). ***"; \
212+ fi; \
213+ for d in $$libdir $$htmldir; do \
214+ [ -d $$d ] && { \
215+ rm -rf $$d; rmdir $$(dirname $$d) 2>/dev/null ||:; \
216+ } \
217+ done; \
218+ rmdir $(PKGDOCPATH) $(PKGDATAPATH) 2>/dev/null ||:
234219
235220# Default installation recipe for a common deployment scenario.
236221.PHONY: install uninstall
@@ -242,8 +227,8 @@ uninstall: uninstall-program
242227freebsd_dest:=freebsd
243228freebsd_makefile:=$(freebsd_dest)/Makefile
244229freebsd_template:=$(freebsd_makefile).in
245−cleanup_files+=$(freebsd_makefile)
246−freebsd : $(freebsd_makefile)
230+cleanup_files+=$(freebsd_makefile)
231+freebsd : $(freebsd_makefile)
247232$(freebsd_makefile) : $(freebsd_template)
248233 sed -e 's/@VERSION@/$(VERSION)/' $< > $@
249234
@@ -257,15 +242,15 @@ macport : $(portfile)
257242$(portfile) : $(portfile_template) tarball
258243 sed -e 's/@VERSION@/$(VERSION)/' $(portfile_template) | \
259244 sed -e 's/@TARBALLMD5SUM@/$(word 2, $(shell openssl md5 $(tarball)))/' > \
260− $(portfile)
245+ $(portfile)
261246
262247.PHONY: win-pkg
263−win_pkg_name:=$(RELNAME).zip
248+win_pkg_name:=$(PKGID).zip
264249win_docs:=COPYING.txt COPYRIGHT.txt BUGS.txt README.txt README.html
265250cleanup_files+=$(win_pkg_name) $(win_docs)
266251win-pkg: $(win_pkg_name)
267−$(win_pkg_name): $(THIS).exe $(win_docs)
268− zip -r $(win_pkg_name) $(THIS).exe $(win_docs)
252+$(win_pkg_name): $(PKG).exe $(win_docs)
253+ zip -r $(win_pkg_name) $(PKG).exe $(win_docs)
269254
270255.PHONY: test test-markdown
271256test: $(MAIN)
@@ -276,8 +261,8 @@ test-markdown: $(MAIN) $(compat)
276261 @for suite in $(markdown_test_dirs); do \
277262 ( \
278263 suite_version=$$(echo $$suite | sed -e 's/.*_//');\
279− echo "-----------------------------------------";\
280− echo "Running Markdown test suite version $${suite_version}.";\
264+ echo >&2 "-----------------------------------------";\
265+ echo >&2 "Running Markdown test suite version $${suite_version}.";\
281266 PATH=$(PWD):$$PATH; export PATH; cd $$suite && \
282267 perl MarkdownTest.pl -s $(compat) -tidy ; \
283268 ) \
@@ -291,15 +276,15 @@ tags: $(src_all)
291276 LC_ALL=C sort tags >tags.sorted; mv tags.sorted tags
292277
293278.PHONY: tarball
294−tarball:=$(RELNAME).tar.gz
279+tarball:=$(PKGID).tar.gz
295280cleanup_files+=$(tarball)
296281tarball: $(tarball)
297282$(tarball):
298− svn export . $(RELNAME)
299− $(MAKE) -C $(RELNAME) templates
300− $(MAKE) -C $(RELNAME) wrappers
301− tar cvzf $(tarball) $(RELNAME)
302− -rm -rf $(RELNAME)
283+ svn export . $(PKGID)
284+ $(MAKE) -C $(PKGID) templates
285+ $(MAKE) -C $(PKGID) wrappers
286+ tar cvzf $(tarball) $(PKGID)
287+ -rm -rf $(PKGID)
303288
304289.PHONY: deb
305290deb_name:=$(shell grep ^Package debian/control | cut -d' ' -f2 | head -n 1)
@@ -308,34 +293,33 @@ deb_arch:=i386
308293deb_main:=$(deb_name)_$(deb_version)_$(deb_arch).deb
309294deb: debian
310295 @[ -x /usr/bin/fakeroot ] || { \
311− echo "*** Please install fakeroot package. ***"; \
296+ echo >&2 "*** Please install fakeroot package. ***"; \
312297 exit 1; \
313298 }
314299 @[ -x /usr/bin/dpkg-buildpackage ] || { \
315− echo "*** Please install dpkg-dev package. ***"; \
300+ echo >&2 "*** Please install dpkg-dev package. ***"; \
316301 exit 1; \
317302 }
318− -mv $(BUILDVARS) $(BUILDVARS).old # backup settings
303+ mv $(BUILDVARS) $(BUILDVARS).old 2>/dev/null ||: # backup settings
319304 if [ -x /usr/bin/debuild ]; then \
320305 debuild -uc -us -i.svn -I.svn -i_darcs -I_darcs --lintian-opts -i; \
321306 else \
322− echo "*** Please install devscripts package. ***"; \
323− echo "*** Using dpkg-buildpackage for package building. ***"; \
307+ echo >&2 "*** Please install devscripts package. ***"; \
308+ echo >&2 "*** Using dpkg-buildpackage for package building. ***"; \
324309 dpkg-buildpackage -rfakeroot -uc -us -i.svn -I.svn -i_darcs -I_darcs; \
325310 fi
326− -mv $(BUILDVARS).old $(BUILDVARS) # restore
311+ mv $(BUILDVARS).old $(BUILDVARS) 2>/dev/null ||: # restore
327312
328313.PHONY: website
329314web_src:=web
330315web_dest:=pandoc-website
331316make_page:=./$(MAIN) -s -S -B $(web_src)/header.html \
332317 -A $(web_src)/footer.html \
333− -H $(web_src)/css
318+ -H $(web_src)/css
334319cleanup_files+=$(web_dest)
335320$(web_dest) : html $(wildcard $(web_src)/*) changelog \
336321 INSTALL $(MANPAGES) $(MANDIR)/man1/pandoc.1.md README
337− -rm -rf $(web_dest)
338− ( \
322+ rm -rf $(web_dest) && { \
339323 mkdir $(web_dest); \
340324 cp -r html $(web_dest)/doc; \
341325 cp $(web_src)/* $(web_dest)/; \
@@ -346,9 +330,9 @@ $(web_dest) : html $(wildcard $(web_src)/*) changelog \
346330 cp INSTALL $(web_dest)/ ; \
347331 cp $(MANDIR)/man1/pandoc.1.md $(web_dest)/ ; \
348332 cp $(MANDIR)/man1/*.1 $(web_dest)/ ; \
349− ) || { rm -rf $(web_dest); exit 1; }
333+ } || { rm -rf $(web_dest); exit 1; }
350334website: $(MAIN) $(web_dest)
351− PANDOC_PATH=$(shell pwd) make -C $(web_dest)
335+ PANDOC_PATH=$(shell pwd) make -C $(web_dest)
352336
353337.PHONY: distclean clean
354338distclean: clean