git.iko.soy/typeable/pandoc
on fcda8d1 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 find $(BUILDDIR) -type f -name "$$f" \
139 -perm +a=x -exec ln -s -f {} . \; ; \
140 done
142.PHONY: build-doc
143cleanup_files+=README.html $(MANPAGES)
144build-doc: $(DOCS) $(MANPAGES)
146.PHONY: build-program
147build-program: build-exec build-doc
149.PHONY: build-lib-doc haddock
150build-lib-doc: html
151haddock: build-lib-doc
152cleanup_files+=html
153html/: configure
154 -rm -rf html
155 $(BUILDCMD) haddock && cp -r $(BUILDDIR)/doc/html .
157.PHONY: build-all
158build-all: build-program build-lib-doc
160# User documents installation.
161.PHONY: install-doc uninstall-doc
162man_all:=$(patsubst $(MANDIR)/%,%,$(MANPAGES))
163install-doc: build-doc
164 $(INSTALL) -d $(DOCPATH) && $(INSTALL_DATA) $(DOCS) $(DOCPATH)/
165 for f in $(man_all); do \
166 $(INSTALL) -d $(MANPATH)/$$(dirname $$f); \
167 $(INSTALL_DATA) $(MANDIR)/$$f $(MANPATH)/$$f; \
168 done
169uninstall-doc:
170 -for f in $(DOCS); do rm -f $(DOCPATH)/$$f; done
171 -for f in $(man_all); do rm -f $(MANPATH)/$$f; done
172 -rmdir $(DOCPATH)
174# Library documents installation.
175.PHONY: install-lib-doc uninstall-lib-doc
176install-lib-doc: build-lib-doc
177 $(INSTALL) -d $(LIBDOCPATH) && cp -R html $(LIBDOCPATH)/
178uninstall-lib-doc:
179 -rm -rf $(LIBDOCPATH)/html
180 -rmdir $(LIBDOCPATH)
182# Helper to install the given files $(1) into the path $(2).
183# It also has the ability to follow symlinks.
184install-executable-files = \
185 $(INSTALL) -d $(2); \
186 for f in $(1); do \
187 if [ -L $$f ]; then \
188 f=$$(readlink $$f); \
189 fi; \
190 $(INSTALL_PROGRAM) $$f $(2)/; \
191 done
193# Program only installation.
194.PHONY: install-exec uninstall-exec
195install-exec: build-exec
196 $(STRIP) $(EXECS)
197 $(call install-executable-files,$(PROGS),$(BINPATH))
198uninstall-exec:
199 -for f in $(notdir $(PROGS)); do rm -f $(BINPATH)/$$f; done ;
201# Program + user documents installation.
202.PHONY: install-program uninstall-program
203install-program: install-exec install-doc
204uninstall-program: uninstall-exec uninstall-doc
206.PHONY: install-all uninstall-all
207# Install libraries
208install-all: build-all install-doc install-lib-doc
209 # Install the library (+ main executable) and register it.
210 destdir=$(DESTDIR); \
211 # Older Cabal versions have no '--destdir' option.
212 if $(BUILDCMD) copy --help | grep -q '\-\-destdir'; then \
213 opt="--destdir=$${destdir-/}"; \
214 else \
215 opt="--copy-prefix=$${destdir}$(PREFIX)"; \
216 fi; \
217 $(BUILDCMD) copy $$opt; \
218 $(BUILDCMD) register
219 # Note that, we are in the position of having to install the wrappers
220 # separately, as Cabal installs the main exec along with the library.
221 $(call install-executable-files,$(WRAPPERS),$(BINPATH))
222uninstall-all: uninstall-program uninstall-lib-doc
223 -pkg_id="$(NAME)-$(VERSION)"; \
224 libdir=$$($(GHC_PKG) field $$pkg_id library-dirs 2>/dev/null | \
225 sed 's/^library-dirs: *//'); \
226 if [ -d "$$libdir" ]; then \
227 $(BUILDCMD) unregister; \
228 rm -rf $$libdir; \
229 rmdir $$(dirname $$libdir); \
230 else \
231 echo "*** Couldn't locate library files for pkgid: $$pkg_id. ***"; \
232 fi
234# Default installation recipe for a common deployment scenario.
235.PHONY: install uninstall
236install: install-program
237uninstall: uninstall-program
239# OSX packages: make osx-pkg-prep, then (as root) make osx-pkg
240.PHONY: osx-pkg osx-pkg-prep
241osx_dest:=osx-pkg-tmp
242osx_src:=osx
243doc_more:=COPYRIGHT.rtf $(osx_src)/Welcome.rtf
244osx_pkg_name:=$(RELNAME).pkg
245cleanup_files+=$(osx_dest) $(doc_more) $(osx_pkg_name)
246osx-pkg-prep: $(osx_dest)
247$(osx_dest)/: build-program $(doc_more)
248 -rm -rf $(osx_dest)
249 $(INSTALL) -d $(osx_dest)
250 DESTDIR=$(osx_dest)/Package_root $(MAKE) install-program
251 cp $(osx_src)/uninstall-pandoc $(osx_dest)/Package_root/usr/local/bin/
252 find $(osx_dest) -type f -regex ".*bin/.*" | xargs chmod +x
253 find $(osx_dest) -type f -regex ".*bin/$(notdir $(MAIN))" | xargs $(STRIP)
254 $(INSTALL) -d $(osx_dest)/Resources
255 ./$(MAIN) -s -S README -o $(osx_dest)/Resources/ReadMe.html
256 cp COPYRIGHT.rtf $(osx_dest)/Resources/License.rtf
257 sed -e 's#@PREFIX@#$(PREFIX)#g' $(osx_src)/Welcome.rtf > $(osx_dest)/Resources/Welcome.rtf
258 sed -e 's/@VERSION@/$(VERSION)/g' $(osx_src)/Info.plist > $(osx_dest)/Info.plist
259 cp $(osx_src)/Description.plist $(osx_dest)/
260osx-pkg: $(osx_pkg_name)
261$(osx_pkg_name)/: $(osx_dest)
262 if [ "`id -u`" != 0 ]; then \
263 echo "Root permissions needed to create OSX package!"; \
264 exit 1; \
265 fi
266 find $(osx_dest) | xargs chown root:wheel
267 PackageMaker -build -p $(osx_pkg_name) \
268 -f $(osx_dest)/Package_root \
269 -r $(osx_dest)/Resources \
270 -i $(osx_dest)/Info.plist \
271 -d $(osx_dest)/Description.plist
272 chgrp admin $(osx_pkg_name)
273 -rm -rf $(osx_dest)
275.PHONY: osx-dmg
276osx_dmg_name:=$(RELNAME).dmg
277cleanup_files+=$(osx_dmg_name)
278osx_udzo_name:=$(RELNAME).udzo.dmg
279osx_dmg_volume:="$(NAME) $(VERSION)"
280osx-dmg: $(osx_dmg_name)
281$(osx_dmg_name): $(osx_pkg_name)
282 -rm -f $(osx_dmg_name)
283 hdiutil create $(osx_dmg_name) -size 05m -fs HFS+ -volname $(osx_dmg_volume)
284 dev_handle=`hdid $(osx_dmg_name) | grep Apple_HFS | \
285 perl -e '\$$_=<>; /^\\/dev\\/(disk.)/; print \$$1'`; \
286 ditto $(osx_pkg_name) /Volumes/$(osx_dmg_volume)/$(osx_pkg_name); \
287 hdiutil detach $$dev_handle
288 hdiutil convert $(osx_dmg_name) -format UDZO -o $(osx_udzo_name)
289 -rm -f $(osx_dmg_name)
290 mv $(osx_udzo_name) $(osx_dmg_name)
293.PHONY: win-pkg
294win_pkg_name:=$(RELNAME).zip
295win_docs:=COPYING.txt COPYRIGHT.txt BUGS.txt README.txt README.html
296cleanup_files+=$(win_pkg_name) $(win_docs)
297win-pkg: $(win_pkg_name)
298$(win_pkg_name): $(THIS).exe $(win_docs)
299 zip -r $(win_pkg_name) $(THIS).exe $(win_docs)
301.PHONY: test test-markdown
302test: $(MAIN)
303 @cd $(TESTDIR) && perl runtests.pl -s $(PWD)/$(MAIN)
304compat:=$(PWD)/hsmarkdown
305markdown_test_dirs:=$(wildcard $(TESTDIR)/MarkdownTest_*)
306test-markdown: $(MAIN) $(compat)
307 @for suite in $(markdown_test_dirs); do \
308 ( \
309 suite_version=$$(echo $$suite | sed -e 's/.*_//');\
310 echo "-----------------------------------------";\
311 echo "Running Markdown test suite version $${suite_version}.";\
312 PATH=$(PWD):$$PATH; export PATH; cd $$suite && \
313 perl MarkdownTest.pl -s $(compat) -tidy ; \
314 ) \
315 done
317# Stolen and slightly improved from a GPLed Makefile. Credits to John Meacham.
318src_all:=$(shell find $(SRCDIR) -type f -name '*hs' | egrep -v '^\./(_darcs|lib|test)/')
319cleanup_files+=$(patsubst %,$(SRCDIR)/%,tags tags.sorted)
320tags: $(src_all)
321 cd $(SRCDIR) && hasktags -c $(src_all:$(SRCDIR)/%=%); \
322 LC_ALL=C sort tags >tags.sorted; mv tags.sorted tags
324.PHONY: tarball
325tarball_name:=$(RELNAME).tar.gz
326cleanup_files+=$(tarball_name)
327tarball: $(tarball_name)
328$(tarball_name):
329 svn export . $(RELNAME)
330 $(MAKE) -C $(RELNAME) templates
331 $(MAKE) -C $(RELNAME) wrappers
332 tar cvzf $(tarball_name) $(RELNAME)
333 -rm -rf $(RELNAME)
335.PHONY: deb
336deb_name:=$(shell grep ^Package debian/control | cut -d' ' -f2 | head -n 1)
337deb_version:=$(shell head -n 1 debian/changelog | cut -f2 -d' ' | tr -d '()')
338deb_arch:=i386
339deb_main:=$(deb_name)_$(deb_version)_$(deb_arch).deb
340deb: debian
341 @[ -x /usr/bin/fakeroot ] || { \
342 echo "*** Please install fakeroot package. ***"; \
343 exit 1; \
344 }
345 @[ -x /usr/bin/dpkg-buildpackage ] || { \
346 echo "*** Please install dpkg-dev package. ***"; \
347 exit 1; \
348 }
349 -mv $(BUILDVARS) $(BUILDVARS).old # backup settings
350 if [ -x /usr/bin/debuild ]; then \
351 debuild -uc -us -i.svn -I.svn -i_darcs -I_darcs --lintian-opts -i; \
352 else \
353 echo "*** Please install devscripts package. ***"; \
354 echo "*** Using dpkg-buildpackage for package building. ***"; \
355 dpkg-buildpackage -rfakeroot -uc -us -i.svn -I.svn -i_darcs -I_darcs; \
356 fi
357 -mv $(BUILDVARS).old $(BUILDVARS) # restore
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)