git.iko.soy/typeable/pandoc
on 229b310 help sign in
typeable/pandoc/Makefile
391 lines 13 kB history raw
1# Makefile for Pandoc.
3#-------------------------------------------------------------------------------
4# Constant names and commands in source tree
5#-------------------------------------------------------------------------------
6CABAL := pandoc.cabal
7SRCDIR := src
8MANDIR := man
9TESTDIR := tests
10BUILDDIR := dist
11BUILDCONF := .setup-config
12BUILDCMD := runhaskell Setup.hs
13BUILDVARS := vars
14CONFIGURE := configure
16#-------------------------------------------------------------------------------
17# Cabal constants
18#-------------------------------------------------------------------------------
19NAME := $(shell sed -ne 's/^[Nn]ame:[[:space:]]*//p' $(CABAL))
20THIS := $(shell echo $(NAME) | tr A-Z a-z)
21VERSION := $(shell sed -ne 's/^version[[:space:]]*=[[:space:]]*"\([^"]*\)"/\1/p' $(SRCDIR)/Main.hs)
22RELNAME := $(THIS)-$(VERSION)
23EXECSBASE := $(shell sed -ne 's/^[Ee]xecutable:[[:space:]]*//p' $(CABAL))
25#-------------------------------------------------------------------------------
26# Install targets
27#-------------------------------------------------------------------------------
28WRAPPERS := html2markdown markdown2pdf hsmarkdown
29# Add .exe extensions if we're running Windows/Cygwin.
30EXTENSION := $(shell uname | tr '[:upper:]' '[:lower:]' | \
31 sed -ne 's/^cygwin.*$$/\.exe/p')
32EXECS := $(addsuffix $(EXTENSION),$(EXECSBASE))
33PROGS := $(EXECS) $(WRAPPERS)
34MAIN := $(firstword $(EXECS))
35DOCS := README.html README BUGS
36MANPAGES := $(patsubst %.md,%,$(wildcard $(MANDIR)/man?/*.?.md))
38#-------------------------------------------------------------------------------
39# Variables to setup through environment
40#-------------------------------------------------------------------------------
42# Specify default values.
43prefix := /usr/local
44destdir :=
45# Attempt to set variables from a previous make session.
46-include $(BUILDVARS)
47# Fallback to defaults but allow to get the values from environment.
48PREFIX ?= $(prefix)
49DESTDIR ?= $(destdir)
51#-------------------------------------------------------------------------------
52# Installation paths
53#-------------------------------------------------------------------------------
54DESTPATH := $(DESTDIR)$(PREFIX)
55BINPATH := $(DESTPATH)/bin
56DATAPATH := $(DESTPATH)/share
57LIBPATH := $(DESTPATH)/$(NAME)-$(VERSION)
58DOCPATH := $(DATAPATH)/doc/$(THIS)
59LIBDOCPATH := $(DATAPATH)/doc/$(THIS)-doc
60MANPATH := $(DATAPATH)/man
61PKGPATH := $(DATAPATH)/$(THIS)
63#-------------------------------------------------------------------------------
64# Generic Makefile variables
65#-------------------------------------------------------------------------------
66INSTALL := install -c
67INSTALL_PROGRAM := $(INSTALL) -m 755
68INSTALL_DATA := $(INSTALL) -m 644
69STRIP := strip
70GHC := ghc
71GHC_PKG := ghc-pkg
73#-------------------------------------------------------------------------------
74# Recipes
75#-------------------------------------------------------------------------------
77# Default target.
78.PHONY: all
79all: build-program
81# Document process rules.
82%.html: % $(MAIN)
83 ./$(MAIN) -s -S --toc $< >$@ || rm -f $@
84%.tex: % $(MAIN)
85 ./$(MAIN) -s -w latex $< >$@ || rm -f $@
86%.rtf: % $(MAIN)
87 ./$(MAIN) -s -w rtf $< >$@ || rm -f $@
88%.pdf: % $(MAIN) markdown2pdf
89 sh ./markdown2pdf $< || rm -f $@
90%.txt: %
91 perl -p -e 's/\n/\r\n/' $< > $@ || rm -f $@ # convert to DOS line endings
92%.1: %.1.md $(MAIN)
93 ./$(MAIN) -s -S -w man $< >$@ || rm -f $@
95.PHONY: templates
96templates: $(SRCDIR)/templates
97 $(MAKE) -C $(SRCDIR)/templates
99define generate-shell-script
100echo "Generating $@..."; \
101awk ' \
102 /^[ \t]*###+ / { \
103 lead = $$0; sub(/[^ \t].*$$/, "", lead); \
104 t = "$(dir $<)/"$$2; \
105 while (getline line < t > 0) \
106 print lead line; \
107 next; \
108 } \
109 { print } \
110' <$< >$@
111chmod +x $@
112endef
114.PHONY: wrappers
115wrappers: $(WRAPPERS)
116cleanup_files+=$(WRAPPERS)
117$(WRAPPERS): %: $(SRCDIR)/wrappers/%.in $(SRCDIR)/wrappers/*.sh
118 @$(generate-shell-script)
120.PHONY: configure
121cleanup_files+=$(BUILDDIR) $(BUILDCONF) $(BUILDVARS)
122configure: $(BUILDCONF) templates
123$(BUILDCONF): $(CABAL)
124 $(BUILDCMD) configure --prefix=$(PREFIX)
125 # Make configuration time settings persistent (definitely a hack).
126 @echo "PREFIX?=$(PREFIX)" >$(BUILDVARS)
127 @echo "DESTDIR?=$(DESTDIR)" >>$(BUILDVARS)
129.PHONY: build
130build: configure
131 $(BUILDCMD) build
133.PHONY: build-exec
134build-exec: $(PROGS)
135cleanup_files+=$(EXECS)
136$(EXECS): build
137 for f in $@; do \
138 [ -f $$f ] || { \
139 find $(BUILDDIR) -type f -name "$$f" \
140 -perm +a=x -exec ln -s {} . \; ; \
141 } \
142 done
144.PHONY: build-doc
145cleanup_files+=README.html $(MANPAGES)
146build-doc: $(DOCS) $(MANPAGES)
148.PHONY: build-program
149build-program: build-exec build-doc
151.PHONY: build-lib-doc haddock
152build-lib-doc: html
153haddock: build-lib-doc
154cleanup_files+=html
155html/: configure
156 -rm -rf html
157 $(BUILDCMD) haddock && mv $(BUILDDIR)/doc/html .
159.PHONY: build-all
160build-all: build-program build-lib-doc
162# User documents installation.
163.PHONY: install-doc uninstall-doc
164man_all:=$(patsubst $(MANDIR)/%,%,$(MANPAGES))
165install-doc: build-doc
166 $(INSTALL) -d $(DOCPATH) && $(INSTALL_DATA) $(DOCS) $(DOCPATH)/
167 for f in $(man_all); do \
168 $(INSTALL) -d $(MANPATH)/$$(dirname $$f); \
169 $(INSTALL_DATA) $(MANDIR)/$$f $(MANPATH)/$$f; \
170 done
171uninstall-doc:
172 -for f in $(DOCS); do rm -f $(DOCPATH)/$$f; done
173 -for f in $(man_all); do rm -f $(MANPATH)/$$f; done
174 -rmdir $(DOCPATH)
176# Library documents installation.
177.PHONY: install-lib-doc uninstall-lib-doc
178install-lib-doc: build-lib-doc
179 $(INSTALL) -d $(LIBDOCPATH) && cp -R html $(LIBDOCPATH)/
180uninstall-lib-doc:
181 -rm -rf $(LIBDOCPATH)/html
182 -rmdir $(LIBDOCPATH)
184# Helper to install the given files $(1) into the path $(2).
185# It also has the ability to follow symlinks.
186install-executable-files = \
187 $(INSTALL) -d $(2); \
188 for f in $(1); do \
189 if [ -L $$f ]; then \
190 f=$$(readlink $$f); \
191 fi; \
192 $(INSTALL_PROGRAM) $$f $(2)/; \
193 done
195# Program only installation.
196.PHONY: install-exec uninstall-exec
197install-exec: build-exec
198 $(STRIP) $(EXECS)
199 $(call install-executable-files,$(PROGS),$(BINPATH))
200uninstall-exec:
201 -for f in $(notdir $(PROGS)); do rm -f $(BINPATH)/$$f; done ;
203# Program + user documents installation.
204.PHONY: install-program uninstall-program
205install-program: install-exec install-doc
206uninstall-program: uninstall-exec uninstall-doc
208# Install everything.
209.PHONY: install-all uninstall-all
210install-all: build-exec install-doc install-lib-doc
211 # Install the library (+ main executable) and register it.
212 destdir=$(DESTDIR); \
213 # Older Cabal versions have no '--destdir' option.
214 if $(BUILDCMD) copy --help | grep -q '\-\-destdir'; then \
215 opt="--destdir=$${destdir-/}"; \
216 else \
217 opt="--copy-prefix=$${destdir}$(PREFIX)"; \
218 fi; \
219 $(BUILDCMD) copy $$opt; \
220 $(BUILDCMD) register
221 # Note that, we are in the position of having to install the wrappers
222 # separately, as Cabal installs the main exec along with the library.
223 $(call install-executable-files,$(WRAPPERS),$(BINPATH))
224uninstall-all: uninstall-exec uninstall-doc uninstall-lib-doc
225 -pkg_id="$(NAME)-$(VERSION)"; \
226 libdir=$$($(GHC_PKG) field $$pkg_id library-dirs 2>/dev/null | \
227 sed 's/^library-dirs: *//'); \
228 if [ -d "$$libdir" ]; then \
229 $(BUILDCMD) unregister; \
230 rm -rf $$libdir; \
231 rmdir $$(dirname $$libdir); \
232 else \
233 echo "*** Couldn't locate library files for pkgid: $$pkg_id. ***"; \
234 fi
236# Default installation recipe for a common deployment scenario.
237.PHONY: install uninstall
238install: install-program
239uninstall: uninstall-program
241# OSX packages: make osx-pkg-prep, then (as root) make osx-pkg
242.PHONY: osx-pkg osx-pkg-prep
243osx_dest:=osx-pkg-tmp
244osx_src:=osx
245doc_more:=README.rtf COPYRIGHT.rtf $(osx_src)/Welcome.rtf
246osx_pkg_name:=$(RELNAME).pkg
247cleanup_files+=$(osx_dest) $(doc_more) $(osx_pkg_name)
248osx-pkg-prep: $(osx_dest)
249$(osx_dest)/: build-program $(doc_more)
250 -rm -rf $(osx_dest)
251 $(INSTALL) -d $(osx_dest)
252 DESTDIR=$(osx_dest)/Package_root $(MAKE) install-program
253 cp $(osx_src)/uninstall-pandoc $(osx_dest)/Package_root/usr/local/bin/
254 find $(osx_dest) -type f -regex ".*bin/.*" | xargs chmod +x
255 find $(osx_dest) -type f -regex ".*bin/$(notdir $(MAIN))" | xargs $(STRIP)
256 $(INSTALL) -d $(osx_dest)/Resources
257 cp README.rtf $(osx_dest)/Resources/ReadMe.rtf
258 cp COPYRIGHT.rtf $(osx_dest)/Resources/License.rtf
259 sed -e 's#@PREFIX@#$(PREFIX)#g' $(osx_src)/Welcome.rtf > $(osx_dest)/Resources/Welcome.rtf
260 sed -e 's/@VERSION@/$(VERSION)/g' $(osx_src)/Info.plist > $(osx_dest)/Info.plist
261 cp $(osx_src)/Description.plist $(osx_dest)/
262osx-pkg: $(osx_pkg_name)
263$(osx_pkg_name)/: $(osx_dest)
264 if [ "`id -u`" != 0 ]; then \
265 echo "Root permissions needed to create OSX package!"; \
266 exit 1; \
267 fi
268 find $(osx_dest) | xargs chown root:wheel
269 PackageMaker -build -p $(osx_pkg_name) \
270 -f $(osx_dest)/Package_root \
271 -r $(osx_dest)/Resources \
272 -i $(osx_dest)/Info.plist \
273 -d $(osx_dest)/Description.plist
274 chgrp admin $(osx_pkg_name)
275 -rm -rf $(osx_dest)
277.PHONY: osx-dmg
278osx_dmg_name:=$(RELNAME).dmg
279cleanup_files+=$(osx_dmg_name)
280osx_udzo_name:=$(RELNAME).udzo.dmg
281osx_dmg_volume:="$(NAME) $(VERSION)"
282osx-dmg: $(osx_dmg_name)
283$(osx_dmg_name): $(osx_pkg_name)
284 -rm -f $(osx_dmg_name)
285 hdiutil create $(osx_dmg_name) -size 05m -fs HFS+ -volname $(osx_dmg_volume)
286 dev_handle=`hdid $(osx_dmg_name) | grep Apple_HFS | \
287 perl -e '\$$_=<>; /^\\/dev\\/(disk.)/; print \$$1'`; \
288 ditto $(osx_pkg_name) /Volumes/$(osx_dmg_volume)/$(osx_pkg_name); \
289 hdiutil detach $$dev_handle
290 hdiutil convert $(osx_dmg_name) -format UDZO -o $(osx_udzo_name)
291 -rm -f $(osx_dmg_name)
292 mv $(osx_udzo_name) $(osx_dmg_name)
295.PHONY: win-pkg
296win_pkg_name:=$(RELNAME).zip
297win_docs:=COPYING.txt COPYRIGHT.txt BUGS.txt README.txt README.html
298cleanup_files+=$(win_pkg_name) $(win_docs)
299win-pkg: $(win_pkg_name)
300$(win_pkg_name): $(THIS).exe $(win_docs)
301 zip -r $(win_pkg_name) $(THIS).exe $(win_docs)
303.PHONY: test test-markdown
304test: $(MAIN)
305 @cd $(TESTDIR) && perl runtests.pl -s $(PWD)/$(MAIN)
306compat:=$(PWD)/hsmarkdown
307markdown_test_dirs:=$(wildcard $(TESTDIR)/MarkdownTest_*)
308test-markdown: $(MAIN) $(compat)
309 @for suite in $(markdown_test_dirs); do \
310 ( \
311 suite_version=$$(echo $$suite | sed -e 's/.*_//');\
312 echo "-----------------------------------------";\
313 echo "Running Markdown test suite version $${suite_version}.";\
314 PATH=$(PWD):$$PATH; export PATH; cd $$suite && \
315 perl MarkdownTest.pl -s $(compat) -tidy ; \
316 ) \
317 done
319# Stolen and slightly improved from a GPLed Makefile. Credits to John Meacham.
320src_all:=$(shell find $(SRCDIR) -type f -name '*hs' | egrep -v '^\./(_darcs|lib|test)/')
321cleanup_files+=$(patsubst %,$(SRCDIR)/%,tags tags.sorted)
322tags: $(src_all)
323 cd $(SRCDIR) && hasktags -c $(src_all:$(SRCDIR)/%=%); \
324 LC_ALL=C sort tags >tags.sorted; mv tags.sorted tags
326.PHONY: tarball
327tarball_name:=$(RELNAME).tar.gz
328cleanup_files+=$(tarball_name)
329tarball: $(tarball_name)
330$(tarball_name):
331 svn export . $(RELNAME)
332 $(MAKE) -C $(RELNAME) templates
333 $(MAKE) -C $(RELNAME) wrappers
334 tar cvzf $(tarball_name) $(RELNAME)
335 -rm -rf $(fullname)
337.PHONY: deb
338deb_name:=$(shell grep ^Package debian/control | cut -d' ' -f2 | head -n 1)
339deb_version:=$(shell head -n 1 debian/changelog | cut -f2 -d' ' | tr -d '()')
340deb_arch:=i386
341deb_main:=$(deb_name)_$(deb_version)_$(deb_arch).deb
342deb: debian
343 @[ -x /usr/bin/fakeroot ] || { \
344 echo "*** Please install fakeroot package. ***"; \
345 exit 1; \
346 }
347 @[ -x /usr/bin/dpkg-buildpackage ] || { \
348 echo "*** Please install dpkg-dev package. ***"; \
349 exit 1; \
350 }
351 if [ -x /usr/bin/debuild ]; then \
352 debuild -uc -us -i.svn -I.svn -i_darcs -I_darcs --lintian-opts -i; \
353 else \
354 echo "*** Please install devscripts package. ***"; \
355 echo "*** Using dpkg-buildpackage for package building. ***"; \
356 dpkg-buildpackage -rfakeroot -uc -us -i.svn -I.svn -i_darcs -I_darcs; \
357 fi
359.PHONY: website
360web_src:=web
361web_dest:=web/pandoc
362make_page:=./$(MAIN) -s -S -B $(web_src)/header.html \
363 -A $(web_src)/footer.html \
364 -H $(web_src)/css
365cleanup_files+=$(web_dest)
366website: $(MAIN) html
367 -rm -rf $(web_dest)
368 ( \
369 mkdir $(web_dest); \
370 cp -r html $(web_dest)/doc; \
371 cp $(web_src)/* $(web_dest)/; \
372 sed -e 's#@PREFIX@#$(PREFIX)#g' $(osx_src)/Welcome > \
373 $(web_dest)/osx-notes.txt; \
374 cp changelog $(web_dest)/ ; \
375 cp README $(web_dest)/ ; \
376 cp INSTALL $(web_dest)/ ; \
377 cp $(MANDIR)/man1/pandoc.1.md $(web_dest)/ ; \
378 cp $(MANDIR)/man1/*.1 $(web_dest)/ ; \
379 PANDOC_PATH=$(shell pwd) make -C $(web_dest) ; \
380 ) || { rm -rf $(web_dest); exit 1; }
382.PHONY: distclean clean
383distclean: clean
384 if [ -d debian ]; then \
385 chmod +x debian/rules; fakeroot debian/rules clean; \
386 fi
388clean:
389 make -C $(SRCDIR)/templates clean
390 -if [ -f $(BUILDCONF) ]; then $(BUILDCMD) clean; fi
391 -rm -rf $(cleanup_files)