git.iko.soy/typeable/pandoc
on 5fb67ca help sign in
typeable/pandoc/Makefile
377 lines 13 kB history raw
1# Makefile for Pandoc.
3#-------------------------------------------------------------------------------
4# Constant names and commands in source tree
5#-------------------------------------------------------------------------------
6CABAL := pandoc.cabal
7SRCDIR := .
8MANDIR := man
9TESTDIR := tests
10BUILDDIR := dist
11BUILDCONF := $(BUILDDIR)/setup-config
12BUILDVARS := vars
13CONFIGURE := configure
14ODTSTYLES := odt-styles
16#-------------------------------------------------------------------------------
17# Cabal constants
18#-------------------------------------------------------------------------------
19PKG := $(shell sed -ne 's/^[Nn]ame:[[:space:]]*//p' $(CABAL) | tr A-Z a-z)
20VERSION := $(shell sed -ne 's/^[Vv]ersion:[[:space:]]*//p' $(CABAL))
21PKGID := $(PKG)-$(VERSION)
22EXECSBASE := $(shell sed -ne 's/^[Ee]xecutable:\{0,1\}[[:space:]]*//p' $(CABAL))
24#-------------------------------------------------------------------------------
25# Install targets
26#-------------------------------------------------------------------------------
27WRAPPERS := html2markdown markdown2pdf hsmarkdown markdown2odt
28# Add .exe extensions if we're running Windows/Cygwin.
29EXTENSION := $(shell uname | tr '[:upper:]' '[:lower:]' | \
30 sed -ne 's/^cygwin.*$$/\.exe/p')
31BUILDCMD := $(addsuffix $(EXTENSION), ./setup)
32EXECS := $(addsuffix $(EXTENSION),$(EXECSBASE))
33PROGS := $(EXECS) $(WRAPPERS)
34MAIN := $(firstword $(EXECS))
35DOCS := README.html README BUGS
36MANPAGES := $(patsubst %.md,%,$(wildcard $(MANDIR)/man?/*.?.md))
37ODTREF := $(ODTSTYLES)/reference.odt
39#-------------------------------------------------------------------------------
40# Variables to setup through environment
41#-------------------------------------------------------------------------------
43# Specify default values.
44prefix := /usr/local
45destdir :=
46# Attempt to set variables from a previous make session.
47-include $(BUILDVARS)
48# Fallback to defaults but allow to get the values from environment.
49PREFIX ?= $(prefix)
50DESTDIR ?= $(destdir)
51DATADIR ?= $(PKGID)
52DOCDIR ?= $(PKGID)/doc
54#-------------------------------------------------------------------------------
55# Installation paths
56#-------------------------------------------------------------------------------
57DESTPATH := $(DESTDIR)$(PREFIX)
58BINPATH := $(DESTPATH)/bin
59DATAPATH := $(DESTPATH)/share
60MANPATH := $(DATAPATH)/man
61PKGDATAPATH := $(DATAPATH)/$(DATADIR)
62PKGDOCPATH := $(DATAPATH)/$(DOCDIR)
64#-------------------------------------------------------------------------------
65# Generic Makefile variables
66#-------------------------------------------------------------------------------
67INSTALL := install -c
68INSTALL_PROGRAM := $(INSTALL) -m 755
69INSTALL_DATA := $(INSTALL) -m 644
70STRIP := strip
71GHC ?= ghc
72GHC_VERSION := $(shell $(GHC) --version | sed -e 's/[^0-9]*//')
74#-------------------------------------------------------------------------------
75# Recipes
76#-------------------------------------------------------------------------------
78# Default target.
79.PHONY: all
80all: build-program
82# Document process rules.
83%.html: % $(MAIN)
84 ./$(MAIN) -s -S --toc $< >$@ || rm -f $@
85%.tex: % $(MAIN)
86 ./$(MAIN) -s -w latex $< >$@ || rm -f $@
87%.rtf: % $(MAIN)
88 ./$(MAIN) -s -w rtf $< >$@ || rm -f $@
89%.pdf: % $(MAIN) markdown2pdf
90 sh ./markdown2pdf $< || rm -f $@
91%.txt: %
92 perl -p -e 's/\n/\r\n/' $< > $@ || rm -f $@ # convert to DOS line endings
93%.1: %.1.md $(MAIN)
94 ./$(MAIN) -s -S -w man $< >$@ || rm -f $@
96define generate-shell-script
97echo >&2 "Generating $@..."; \
98awk ' \
99 /^[ \t]*###+ / { \
100 lead = $$0; sub(/[^ \t].*$$/, "", lead); \
101 t = "$(dir $<)/"$$2; \
102 while (getline line < t > 0) \
103 print lead line; \
104 next; \
105 } \
106 { print } \
107' <$< >$@
108chmod +x $@
109endef
111cleanup_files+=$(ODTREF)
112$(ODTREF): $(addprefix $(ODTSTYLES)/, layout-cache meta.xml styles.xml content.xml mimetype \
113 settings.xml Configurations2 Thumbnails META-INF)
114 cd $(ODTSTYLES) ; \
115 zip -9 -r $(notdir $@) * -x $(notdir $@)
117ODTREFSH=$(SRCDIR)/wrappers/odtref.sh
118cleanup_files+=$(ODTREFSH)
119$(ODTREFSH): $(ODTREF)
120 echo "REFERENCEODT='$(PKGDATAPATH)/$(notdir $(ODTREF))'" > $@
122.PHONY: wrappers
123wrappers: $(WRAPPERS)
124cleanup_files+=$(WRAPPERS)
125$(WRAPPERS): %: $(SRCDIR)/wrappers/%.in $(SRCDIR)/wrappers/*.sh $(ODTREFSH)
126 @$(generate-shell-script)
128CABAL_BACKUP=$(CABAL).orig
129$(CABAL_BACKUP):
130 cp $(CABAL) $(CABAL_BACKUP) ; \
131 if echo $(GHC_VERSION) | grep -q '^6.6'; then \
132 cp $(CABAL).ghc66 $(CABAL); \
133 fi
135.PHONY: configure
136cleanup_files+=Setup.hi Setup.o $(BUILDCMD) $(BUILDVARS)
137ifdef GHC_PKG
138 hc_pkg = --with-hc-pkg=$(GHC_PKG)
139else
140 hc_pkg =
141endif
142templates=$(wildcard templates/*.* templates/headers/*.* templates/ui/default/*.*)
143configure: $(BUILDCONF)
144$(BUILDCMD): Setup.hs
145 $(GHC) -package Cabal Setup.hs -o $(BUILDCMD)
146$(BUILDCONF): $(CABAL) $(CABAL_BACKUP) $(BUILDCMD) $(templates)
147 $(BUILDCMD) configure --prefix=$(PREFIX) --with-compiler=$(GHC) $(hc_pkg) $(CABALOPTS)
148 @# Make configuration time settings persistent (definitely a hack).
149 @echo "PREFIX?=$(PREFIX)" >$(BUILDVARS)
150 @echo "DESTDIR?=$(DESTDIR)" >>$(BUILDVARS)
152.PHONY: build
153build: configure
154 $(BUILDCMD) build
156.PHONY: build-exec
157build-exec: $(PROGS)
158cleanup_files+=$(EXECS)
159$(EXECS): build
160 for f in $@; do \
161 find $(BUILDDIR) -type f -name "$$f" -perm +a=x \
162 -exec ln -s -f {} . \; ; \
163 done
165.PHONY: build-doc
166cleanup_files+=README.html $(MANPAGES)
167build-doc: $(DOCS) $(MANPAGES)
169.PHONY: build-program
170build-program: build-exec build-doc
172.PHONY: build-lib-doc haddock
173build-lib-doc: html
174haddock: build-lib-doc
175cleanup_files+=html
176html/: configure
177 -rm -rf html
178 $(BUILDCMD) haddock && cp -r $(BUILDDIR)/doc/html .
180.PHONY: build-all
181build-all: build-program build-lib-doc
183# User documents installation.
184.PHONY: install-doc uninstall-doc
185man_all:=$(patsubst $(MANDIR)/%,%,$(MANPAGES))
186install-doc: build-doc
187 $(INSTALL) -d $(PKGDOCPATH) && $(INSTALL_DATA) $(DOCS) $(PKGDOCPATH)/
188 for f in $(man_all); do \
189 $(INSTALL) -d $(MANPATH)/$$(dirname $$f); \
190 $(INSTALL_DATA) $(MANDIR)/$$f $(MANPATH)/$$f; \
191 done
192uninstall-doc:
193 -for f in $(DOCS); do rm -f $(PKGDOCPATH)/$$f; done
194 -for f in $(man_all); do rm -f $(MANPATH)/$$f; done
195 rmdir $(PKGDOCPATH) 2>/dev/null ||:
197# Data file installation.
198.PHONY: install-data uninstall-data
199install-data: $(ODTREF)
200 $(INSTALL) -d $(PKGDATAPATH) && $(INSTALL_DATA) $(ODTREF) $(PKGDATAPATH)/
201uninstall-data:
202 -rm -f $(PKGDATAPATH)/$(notdir $(ODTREF))
203 rmdir $(PKGDATAPATH) 2>/dev/null ||:
205# Program only installation.
206.PHONY: install-exec uninstall-exec
207install-exec: build-exec
208 $(STRIP) $(EXECS)
209 $(INSTALL) -d $(BINPATH); \
210 for f in $(PROGS); do \
211 if [ -L $$f ]; then \
212 f=$$(readlink $$f); \
213 fi; \
214 $(INSTALL_PROGRAM) $$f $(BINPATH)/; \
215 done
216uninstall-exec:
217 -for f in $(notdir $(PROGS)); do rm -f $(BINPATH)/$$f; done
219# Program + user documents installation.
220.PHONY: install-program uninstall-program
221install-program: install-exec install-data install-doc
222uninstall-program: uninstall-exec uninstall-doc uninstall-data
224.PHONY: install-all uninstall-all
225# Full installation through Cabal: main + wrappers + user docs + lib + lib docs
226install-all: build-all install-program
227 destdir=$(DESTDIR); \
228 # Older Cabal versions have no '--destdir' option.
229 if $(BUILDCMD) copy --help | grep -q '\-\-destdir'; then \
230 opt="--destdir=$${destdir-/}"; \
231 else \
232 opt="--copy-prefix=$${destdir}$(PREFIX)"; \
233 fi; \
234 $(BUILDCMD) copy $$opt; $(BUILDCMD) register
235# Cabal lacks an 'uninstall' command. We have to remove some cruft manually.
236uninstall-all: uninstall-program
237 @libdir=$$($(GHC_PKG) field $(PKGID) library-dirs 2>/dev/null | \
238 sed 's/^library-dirs: *//'); \
239 htmldir=$$($(GHC_PKG) field $(PKGID) haddock-html 2>/dev/null | \
240 sed 's/^haddock-html: *//'); \
241 if [ -d $$libdir ]; then \
242 $(BUILDCMD) unregister ||:; \
243 else \
244 echo >&2 "*** Couldn't locate library for pkgid: $(PKGID). ***"; \
245 fi; \
246 for d in $$libdir $$htmldir; do \
247 [ -d $$d ] && { \
248 rm -rf $$d; rmdir $$(dirname $$d) 2>/dev/null ||:; \
249 } \
250 done; \
251 rmdir $(PKGDOCPATH) $(PKGDATAPATH) 2>/dev/null ||:
253# Default installation recipe for a common deployment scenario.
254.PHONY: install uninstall
255install: install-program
256uninstall: uninstall-program
258# FreeBSD port
259.PHONY: freebsd
260freebsd_dest:=freebsd
261freebsd_makefile:=$(freebsd_dest)/Makefile
262freebsd_template:=$(freebsd_makefile).in
263cleanup_files+=$(freebsd_makefile)
264freebsd : $(freebsd_makefile)
265$(freebsd_makefile) : $(freebsd_template)
266 sed -e 's/@VERSION@/$(VERSION)/' $< > $@
268# MacPort
269.PHONY: macport
270macport_dest:=macports
271portfile:=$(macport_dest)/Portfile
272portfile_template:=$(portfile).in
273cleanup_files+=$(portfile)
274macport : $(portfile)
275$(portfile) : $(portfile_template) tarball
276 sed -e 's/@VERSION@/$(VERSION)/' $(portfile_template) | \
277 sed -e 's/@TARBALLMD5SUM@/$(word 2, $(shell openssl md5 $(tarball)))/' > \
278 $(portfile)
280.PHONY: win-pkg
281win_pkg_name:=$(PKGID).zip
282win_docs:=COPYING.txt COPYRIGHT.txt BUGS.txt README.txt README.html
283cleanup_files+=$(win_pkg_name) $(win_docs)
284win-pkg: $(win_pkg_name)
285$(win_pkg_name): $(PKG).exe $(win_docs)
286 zip -r $(win_pkg_name) $(PKG).exe $(win_docs)
288.PHONY: test test-markdown
289test: $(MAIN)
290 @cd $(TESTDIR) && perl runtests.pl -s $(PWD)/$(MAIN)
291compat:=$(PWD)/hsmarkdown
292markdown_test_dirs:=$(wildcard $(TESTDIR)/MarkdownTest_*)
293test-markdown: $(MAIN) $(compat)
294 @for suite in $(markdown_test_dirs); do \
295 ( \
296 suite_version=$$(echo $$suite | sed -e 's/.*_//');\
297 echo >&2 "-----------------------------------------";\
298 echo >&2 "Running Markdown test suite version $${suite_version}.";\
299 PATH=$(PWD):$$PATH; export PATH; cd $$suite && \
300 perl MarkdownTest.pl -s $(compat) -tidy ; \
301 ) \
302 done
304# Stolen and slightly improved from a GPLed Makefile. Credits to John Meacham.
305src_all:=$(shell find $(SRCDIR) -type f -name '*hs' | egrep -v '^\./(_darcs|lib|test)/')
306cleanup_files+=$(patsubst %,$(SRCDIR)/%,tags tags.sorted)
307tags: $(src_all)
308 cd $(SRCDIR) && hasktags -c $(src_all:$(SRCDIR)/%=%); \
309 LC_ALL=C sort tags >tags.sorted; mv tags.sorted tags
311.PHONY: tarball
312tarball:=$(PKGID).tar.gz
313cleanup_files+=$(tarball)
314tarball: $(tarball)
315$(tarball):
316 svn export . $(PKGID)
317 $(MAKE) -C $(PKGID) wrappers
318 tar cvzf $(tarball) $(PKGID)
319 -rm -rf $(PKGID)
321.PHONY: deb
322deb_name:=$(shell grep ^Package debian/control | cut -d' ' -f2 | head -n 1)
323deb_version:=$(shell head -n 1 debian/changelog | cut -f2 -d' ' | tr -d '()')
324deb_arch:=i386
325deb_main:=$(deb_name)_$(deb_version)_$(deb_arch).deb
326deb: debian
327 @[ -x /usr/bin/fakeroot ] || { \
328 echo >&2 "*** Please install fakeroot package. ***"; \
329 exit 1; \
330 }
331 @[ -x /usr/bin/dpkg-buildpackage ] || { \
332 echo >&2 "*** Please install dpkg-dev package. ***"; \
333 exit 1; \
334 }
335 mv $(BUILDVARS) $(BUILDVARS).old 2>/dev/null ||: # backup settings
336 if [ -x /usr/bin/debuild ]; then \
337 debuild -uc -us -i.svn -I.svn -i_darcs -I_darcs --lintian-opts -i; \
338 else \
339 echo >&2 "*** Please install devscripts package. ***"; \
340 echo >&2 "*** Using dpkg-buildpackage for package building. ***"; \
341 dpkg-buildpackage -rfakeroot -uc -us -i.svn -I.svn -i_darcs -I_darcs; \
342 fi
343 mv $(BUILDVARS).old $(BUILDVARS) 2>/dev/null ||: # restore
345.PHONY: website
346web_src:=web
347web_dest:=pandoc-website
348make_page:=./$(MAIN) -s -S -B $(web_src)/header.html \
349 -A $(web_src)/footer.html \
350 -H $(web_src)/css
351cleanup_files+=$(web_dest)
352$(web_dest) : html $(wildcard $(web_src)/*) debian/changelog \
353 INSTALL $(MANPAGES) $(MANDIR)/man1/pandoc.1.md README
354 rm -rf $(web_dest) && { \
355 mkdir $(web_dest); \
356 cp -r html $(web_dest)/doc; \
357 cp $(web_src)/* $(web_dest)/; \
358 sed -e 's#@VERSION@#$(VERSION)#g' $(web_src)/index.txt.in > \
359 $(web_dest)/index.txt; \
360 cp debian/changelog $(web_dest)/changelog.txt ; \
361 cp README $(web_dest)/ ; \
362 cp INSTALL $(web_dest)/ ; \
363 cp $(MANDIR)/man1/pandoc.1.md $(web_dest)/ ; \
364 cp $(MANPAGES) $(web_dest)/ ; \
365 } || { rm -rf $(web_dest); exit 1; }
366website: $(MAIN) $(web_dest)
367 PANDOC_PATH=$(shell pwd) make -C $(web_dest)
369.PHONY: distclean clean
370distclean: clean
371 if [ -d debian ]; then \
372 chmod +x debian/rules; fakeroot debian/rules clean; \
373 fi
375clean:
376 -if [ -f $(BUILDCONF) ]; then $(BUILDCMD) clean; fi
377 -rm -rf $(cleanup_files)