git.iko.soy/typeable/pandoc
on fbb0482 help sign in
typeable/pandoc/Makefile
362 lines 12 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 := ./setup
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/^[Vv]ersion:[[:space:]]*//p' $(CABAL))
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+=Setup.hi Setup.o $(BUILDCMD) $(BUILDVARS)
122configure: $(BUILDCONF) templates
123$(BUILDCONF): $(CABAL)
124 ghc -package Cabal Setup.hs -o $(BUILDCMD)
125 $(BUILDCMD) configure --prefix=$(PREFIX)
126 # Make configuration time settings persistent (definitely a hack).
127 @echo "PREFIX?=$(PREFIX)" >$(BUILDVARS)
128 @echo "DESTDIR?=$(DESTDIR)" >>$(BUILDVARS)
130.PHONY: build
131build: configure
132 $(BUILDCMD) build
134.PHONY: build-exec
135build-exec: $(PROGS)
136cleanup_files+=$(EXECS)
137$(EXECS): build
138 for f in $@; do \
139 find $(BUILDDIR) -type f -name "$$f" \
140 -perm +a=x -exec ln -s -f {} . \; ; \
141 done
143.PHONY: build-doc
144cleanup_files+=README.html $(MANPAGES)
145build-doc: $(DOCS) $(MANPAGES)
147.PHONY: build-program
148build-program: build-exec build-doc
150.PHONY: build-lib-doc haddock
151build-lib-doc: html
152haddock: build-lib-doc
153cleanup_files+=html
154html/: configure
155 -rm -rf html
156 $(BUILDCMD) haddock && cp -r $(BUILDDIR)/doc/html .
158.PHONY: build-all
159build-all: build-program build-lib-doc
161# User documents installation.
162.PHONY: install-doc uninstall-doc
163man_all:=$(patsubst $(MANDIR)/%,%,$(MANPAGES))
164install-doc: build-doc
165 $(INSTALL) -d $(DOCPATH) && $(INSTALL_DATA) $(DOCS) $(DOCPATH)/
166 for f in $(man_all); do \
167 $(INSTALL) -d $(MANPATH)/$$(dirname $$f); \
168 $(INSTALL_DATA) $(MANDIR)/$$f $(MANPATH)/$$f; \
169 done
170uninstall-doc:
171 -for f in $(DOCS); do rm -f $(DOCPATH)/$$f; done
172 -for f in $(man_all); do rm -f $(MANPATH)/$$f; done
173 -rmdir $(DOCPATH)
175# Library documents installation.
176.PHONY: install-lib-doc uninstall-lib-doc
177install-lib-doc: build-lib-doc
178 $(INSTALL) -d $(LIBDOCPATH) && cp -R html $(LIBDOCPATH)/
179uninstall-lib-doc:
180 -rm -rf $(LIBDOCPATH)/html
181 -rmdir $(LIBDOCPATH)
183# Helper to install the given files $(1) into the path $(2).
184# It also has the ability to follow symlinks.
185install-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
194# Program only installation.
195.PHONY: install-exec uninstall-exec
196install-exec: build-exec
197 $(STRIP) $(EXECS)
198 $(call install-executable-files,$(PROGS),$(BINPATH))
199uninstall-exec:
200 -for f in $(notdir $(PROGS)); do rm -f $(BINPATH)/$$f; done ;
202# Program + user documents installation.
203.PHONY: install-program uninstall-program
204install-program: install-exec install-doc
205uninstall-program: uninstall-exec uninstall-doc
207.PHONY: install-all uninstall-all
208# Install libraries
209install-all: build-all install-doc install-lib-doc
210 # Install the library (+ main executable) and register it.
211 destdir=$(DESTDIR); \
212 # Older Cabal versions have no '--destdir' option.
213 if $(BUILDCMD) copy --help | grep -q '\-\-destdir'; then \
214 opt="--destdir=$${destdir-/}"; \
215 else \
216 opt="--copy-prefix=$${destdir}$(PREFIX)"; \
217 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))
223uninstall-all: uninstall-program uninstall-lib-doc
224 -pkg_id="$(NAME)-$(VERSION)"; \
225 libdir=$$($(GHC_PKG) field $$pkg_id library-dirs 2>/dev/null | \
226 sed 's/^library-dirs: *//'); \
227 if [ -d "$$libdir" ]; then \
228 $(BUILDCMD) unregister; \
229 rm -rf $$libdir; \
230 rmdir $$(dirname $$libdir); \
231 else \
232 echo "*** Couldn't locate library files for pkgid: $$pkg_id. ***"; \
233 fi
235# Default installation recipe for a common deployment scenario.
236.PHONY: install uninstall
237install: install-program
238uninstall: uninstall-program
240# FreeBSD port
241.PHONY: freebsd
242freebsd_dest:=freebsd
243freebsd_makefile:=$(freebsd_dest)/Makefile
244freebsd_template:=$(freebsd_makefile).in
245cleanup_files+=$(freebsd_makefile)
246freebsd : $(freebsd_makefile)
247$(freebsd_makefile) : $(freebsd_template)
248 sed -e 's/@VERSION@/$(VERSION)/' $< > $@
250# MacPort
251.PHONY: macport
252macport_dest:=macports
253portfile:=$(macport_dest)/Portfile
254portfile_template:=$(portfile).in
255cleanup_files+=$(portfile)
256macport : $(portfile)
257$(portfile) : $(portfile_template) tarball
258 sed -e 's/@VERSION@/$(VERSION)/' $(portfile_template) | \
259 sed -e 's/@TARBALLMD5SUM@/$(word 2, $(shell openssl md5 $(tarball)))/' > \
260 $(portfile)
262.PHONY: win-pkg
263win_pkg_name:=$(RELNAME).zip
264win_docs:=COPYING.txt COPYRIGHT.txt BUGS.txt README.txt README.html
265cleanup_files+=$(win_pkg_name) $(win_docs)
266win-pkg: $(win_pkg_name)
267$(win_pkg_name): $(THIS).exe $(win_docs)
268 zip -r $(win_pkg_name) $(THIS).exe $(win_docs)
270.PHONY: test test-markdown
271test: $(MAIN)
272 @cd $(TESTDIR) && perl runtests.pl -s $(PWD)/$(MAIN)
273compat:=$(PWD)/hsmarkdown
274markdown_test_dirs:=$(wildcard $(TESTDIR)/MarkdownTest_*)
275test-markdown: $(MAIN) $(compat)
276 @for suite in $(markdown_test_dirs); do \
277 ( \
278 suite_version=$$(echo $$suite | sed -e 's/.*_//');\
279 echo "-----------------------------------------";\
280 echo "Running Markdown test suite version $${suite_version}.";\
281 PATH=$(PWD):$$PATH; export PATH; cd $$suite && \
282 perl MarkdownTest.pl -s $(compat) -tidy ; \
283 ) \
284 done
286# Stolen and slightly improved from a GPLed Makefile. Credits to John Meacham.
287src_all:=$(shell find $(SRCDIR) -type f -name '*hs' | egrep -v '^\./(_darcs|lib|test)/')
288cleanup_files+=$(patsubst %,$(SRCDIR)/%,tags tags.sorted)
289tags: $(src_all)
290 cd $(SRCDIR) && hasktags -c $(src_all:$(SRCDIR)/%=%); \
291 LC_ALL=C sort tags >tags.sorted; mv tags.sorted tags
293.PHONY: tarball
294tarball:=$(RELNAME).tar.gz
295cleanup_files+=$(tarball)
296tarball: $(tarball)
297$(tarball):
298 svn export . $(RELNAME)
299 $(MAKE) -C $(RELNAME) templates
300 $(MAKE) -C $(RELNAME) wrappers
301 tar cvzf $(tarball) $(RELNAME)
302 -rm -rf $(RELNAME)
304.PHONY: deb
305deb_name:=$(shell grep ^Package debian/control | cut -d' ' -f2 | head -n 1)
306deb_version:=$(shell head -n 1 debian/changelog | cut -f2 -d' ' | tr -d '()')
307deb_arch:=i386
308deb_main:=$(deb_name)_$(deb_version)_$(deb_arch).deb
309deb: debian
310 @[ -x /usr/bin/fakeroot ] || { \
311 echo "*** Please install fakeroot package. ***"; \
312 exit 1; \
313 }
314 @[ -x /usr/bin/dpkg-buildpackage ] || { \
315 echo "*** Please install dpkg-dev package. ***"; \
316 exit 1; \
317 }
318 -mv $(BUILDVARS) $(BUILDVARS).old # backup settings
319 if [ -x /usr/bin/debuild ]; then \
320 debuild -uc -us -i.svn -I.svn -i_darcs -I_darcs --lintian-opts -i; \
321 else \
322 echo "*** Please install devscripts package. ***"; \
323 echo "*** Using dpkg-buildpackage for package building. ***"; \
324 dpkg-buildpackage -rfakeroot -uc -us -i.svn -I.svn -i_darcs -I_darcs; \
325 fi
326 -mv $(BUILDVARS).old $(BUILDVARS) # restore
328.PHONY: website
329web_src:=web
330web_dest:=pandoc-website
331make_page:=./$(MAIN) -s -S -B $(web_src)/header.html \
332 -A $(web_src)/footer.html \
333 -H $(web_src)/css
334cleanup_files+=$(web_dest)
335$(web_dest) : html $(wildcard $(web_src)/*) changelog \
336 INSTALL $(MANPAGES) $(MANDIR)/man1/pandoc.1.md README
337 -rm -rf $(web_dest)
338 ( \
339 mkdir $(web_dest); \
340 cp -r html $(web_dest)/doc; \
341 cp $(web_src)/* $(web_dest)/; \
342 sed -e 's#@VERSION@#$(VERSION)#g' $(web_src)/index.txt.in > \
343 $(web_dest)/index.txt; \
344 cp changelog $(web_dest)/ ; \
345 cp README $(web_dest)/ ; \
346 cp INSTALL $(web_dest)/ ; \
347 cp $(MANDIR)/man1/pandoc.1.md $(web_dest)/ ; \
348 cp $(MANDIR)/man1/*.1 $(web_dest)/ ; \
349 ) || { rm -rf $(web_dest); exit 1; }
350website: $(MAIN) $(web_dest)
351 PANDOC_PATH=$(shell pwd) make -C $(web_dest)
353.PHONY: distclean clean
354distclean: clean
355 if [ -d debian ]; then \
356 chmod +x debian/rules; fakeroot debian/rules clean; \
357 fi
359clean:
360 make -C $(SRCDIR)/templates clean
361 -if [ -f $(BUILDCONF) ]; then $(BUILDCMD) clean; fi
362 -rm -rf $(cleanup_files)