git.iko.soy/typeable/pandoc
on 7fe2018 help sign in
typeable/pandoc/Makefile
389 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).in)
20VERSION := $(shell sed -ne 's/^version[[:space:]]*=[[:space:]]*"\([^"]*\)"/\1/p' $(SRCDIR)/Main.hs)
21EXECSBASE := $(shell sed -ne 's/^[Ee]xecutable:[[:space:]]*//p' $(CABAL).in)
23#-------------------------------------------------------------------------------
24# Install targets
25#-------------------------------------------------------------------------------
26WRAPPERS := web2markdown markdown2pdf
27SYMLINKS := markdown2html markdown2latex markdown2s5 markdown2rst \
28 markdown2rtf html2markdown latex2markdown rst2markdown
29PROGS := $(EXECS) $(WRAPPERS)
30# Add .exe extensions if we're running Windows/Cygwin.
31EXTENSION := $(shell uname | tr '[:upper:]' '[:lower:]' | \
32 sed -ne 's/^cygwin.*$$/\.exe/p')
33EXECS := $(addsuffix $(EXTENSION),$(EXECSBASE))
34# First entry in Cabal's executable stanza is the main executable.
35MAIN := $(firstword $(EXECS))
36DOCS := README.html README BUGS
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#-------------------------------------------------------------------------------
54THIS := $(shell echo $(NAME) | tr A-Z a-z)
55DESTPATH := $(DESTDIR)$(PREFIX)
56BINPATH := $(DESTPATH)/bin
57DATAPATH := $(DESTPATH)/share
58LIBPATH := $(DESTPATH)/$(NAME)-$(VERSION)
59DOCPATH := $(DATAPATH)/doc/$(THIS)
60LIBDOCPATH := $(DATAPATH)/doc/$(THIS)-doc
61MANPATH := $(DATAPATH)/man
62PKGPATH := $(DATAPATH)/$(THIS)
64#-------------------------------------------------------------------------------
65# Generic Makefile variables
66#-------------------------------------------------------------------------------
67INSTALL := install -c
68INSTALL_PROGRAM := $(INSTALL) -m 755
69INSTALL_DATA := $(INSTALL) -m 644
70STRIP := strip
71GHC := ghc
72GHC_PKG := ghc-pkg
74#-------------------------------------------------------------------------------
75# Recipes
76#-------------------------------------------------------------------------------
78# Default target.
79.PHONY: all
80all: build-program
82# Document process rules.
83%.html: % $(MAIN)
84 ./$(MAIN) -s $< >$@ || 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 $@
92.PHONY: templates
93templates: $(SRCDIR)/templates
94 $(MAKE) -C $(SRCDIR)/templates
96.PHONY: symlinks
97cleanup_files+=$(SYMLINKS)
98symlinks: $(SYMLINKS)
99$(SYMLINKS): $(MAIN)
100 ln -sf ./$(MAIN) $@
102define generate-shell-script
103echo "Generating $@..."; \
104awk ' \
105 /^[ \t]*###+ / { \
106 lead = $$0; sub(/[^ \t].*$$/, "", lead); \
107 t = "$(dir $<)/"$$2; \
108 while (getline line < t > 0) \
109 print lead line; \
110 next; \
111 } \
112 { print } \
113' <$< >$@
114endef
116.PHONY: wrappers
117wrappers: $(WRAPPERS)
118wrapper_deps := $(wildcard $(SRCDIR)/wrappers/*.sh)
119cleanup_files+=$(WRAPPERS)
120%: $(SRCDIR)/wrappers/%.in $(wrapper_deps)
121 @$(generate-shell-script)
123cleanup_files+=$(CABAL)
124$(CABAL): cabalize $(CABAL).in $(SRCDIR)/Main.hs
125 ./cabalize <$(CABAL).in >$(CABAL)
127.PHONY: configure
128cleanup_files+=$(BUILDDIR) $(BUILDCONF) $(BUILDVARS)
129configure: $(BUILDCONF) templates
130$(BUILDCONF): $(CABAL)
131 $(BUILDCMD) configure --prefix=$(PREFIX)
132 # Make configuration time settings persistent (definitely a hack).
133 @echo "PREFIX?=$(PREFIX)" >$(BUILDVARS)
134 @echo "DESTDIR?=$(DESTDIR)" >>$(BUILDVARS)
136.PHONY: build
137build: configure
138 $(BUILDCMD) build
140.PHONY: build-exec
141build-exec: $(PROGS) $(SYMLINKS)
142cleanup_files+=$(EXECS)
143$(EXECS): build
144 for f in $@; do \
145 [ -f $$f ] || { \
146 find $(BUILDDIR) -type f -name "$$f" \
147 -perm +a=x -exec ln -s {} . \; ; \
148 } \
149 done
151.PHONY: build-doc
152cleanup_files+=README.html
153build-doc: $(DOCS)
155.PHONY: build-program
156build-program: build-exec build-doc
158.PHONY: build-lib-doc haddock
159build-lib-doc: html
160haddock: build-lib-doc
161cleanup_files+=html
162html/: configure
163 -rm -rf html
164 $(BUILDCMD) haddock && mv $(BUILDDIR)/doc/html .
166.PHONY: build-all
167build-all: build-program build-lib-doc
169# User documents installation.
170.PHONY: install-doc uninstall-doc
171man_all:=$(patsubst $(MANDIR)/%,%,$(wildcard $(MANDIR)/man?/*.1))
172install-doc: build-doc
173 $(INSTALL) -d $(DOCPATH) && $(INSTALL_DATA) $(DOCS) $(DOCPATH)/
174 for f in $(man_all); do \
175 $(INSTALL) -d $(MANPATH)/$$(dirname $$f); \
176 $(INSTALL_DATA) $(MANDIR)/$$f $(MANPATH)/$$f; \
177 done
178uninstall-doc:
179 -for f in $(DOCS); do rm -f $(DOCPATH)/$$f; done
180 -for f in $(man_all); do rm -f $(MANPATH)/$$f; done
181 -rmdir $(DOCPATH)
183# Library documents installation.
184.PHONY: install-lib-doc uninstall-lib-doc
185install-lib-doc: build-lib-doc
186 $(INSTALL) -d $(LIBDOCPATH) && cp -a html $(LIBDOCPATH)/
187uninstall-lib-doc:
188 -rm -rf $(LIBDOCPATH)/html
189 -rmdir $(LIBDOCPATH)
191# Program only installation.
192.PHONY: install-exec uninstall-exec
193install-exec: build-exec
194 $(INSTALL) -d $(BINPATH); \
195 for f in $(PROGS); do \
196 if [ -L $$f ]; then \
197 f=$$(readlink $$f); \
198 fi; \
199 $(INSTALL_PROGRAM) $$f $(BINPATH)/; \
200 done
201 cd $(BINPATH); for f in $(SYMLINKS); do ln -sf $(MAIN) $$f; done
202uninstall-exec:
203 -for f in $(notdir $(PROGS) $(SYMLINKS)); do rm -f $(BINPATH)/$$f; done ;
205# Program + user documents installation.
206.PHONY: install-program uninstall-program
207install-program: install-exec install-doc
208uninstall-program: uninstall-exec uninstall-doc
210# Install everything.
211.PHONY: install-all uninstall-all
212install-all: install-doc install-lib-doc
213 destdir=$(DESTDIR); \
214 # Older Cabal versions have no '--destdir' option.
215 if $(BUILDCMD) copy --help | grep -q '\-\-destdir'; then \
216 opt="--destdir=$${destdir-/}"; \
217 else \
218 opt="--copy-prefix=$${destdir}$(PREFIX)"; \
219 fi; \
220 $(BUILDCMD) copy $$opt; \
221 $(BUILDCMD) register
222uninstall-all: uninstall-exec uninstall-doc 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
239osx_dest:=osx-pkg-tmp
240osx_src:=osx
241doc_more:=README.rtf COPYRIGHT.rtf $(osx_src)/Welcome.rtf
242osx_pkg_name:=$(NAME)_$(VERSION).pkg
243cleanup_files+=$(osx_dest) $(doc_more) $(osx_pkg_name)
244osx-pkg-prep: build-program $(osx_dest)
245$(osx_dest)/: $(doc_more)
246 -rm -rf $(osx_dest)
247 $(INSTALL) -d $(osx_dest)
248 DESTDIR=$(osx_dest)/Package_root $(MAKE) install-program
249 cp $(osx_src)/uninstall-pandoc $(osx_dest)/Package_root/usr/local/bin/
250 find $(osx_dest) -type f -regex ".*bin/.*" | xargs chmod +x
251 find $(osx_dest) -type f -regex ".*bin/$(notdir $(MAIN))" | xargs $(STRIP)
252 $(INSTALL) -d $(osx_dest)/Resources
253 cp README.rtf $(osx_dest)/Resources/ReadMe.rtf
254 cp COPYRIGHT.rtf $(osx_dest)/Resources/License.rtf
255 sed -e 's#@PREFIX@#$(PREFIX)#g' $(osx_src)/Welcome.rtf > $(osx_dest)/Resources/Welcome.rtf
256 sed -e 's/@VERSION@/$(VERSION)/g' $(osx_src)/Info.plist > $(osx_dest)/Info.plist
257 cp $(osx_src)/Description.plist $(osx_dest)/
258osx-pkg: osx-pkg-prep
259 if [ "`id -u`" != 0 ]; then \
260 echo "Root permissions needed to create OSX package!"; \
261 exit 1; \
262 fi
263 find $(osx_dest) | xargs chown root:wheel
264 PackageMaker -build -p $(osx_pkg_name) \
265 -f $(osx_dest)/Package_root \
266 -r $(osx_dest)/Resources \
267 -i $(osx_dest)/Info.plist \
268 -d $(osx_dest)/Description.plist
269 chgrp admin $(osx_pkg_name)
270 -rm -rf $(osx_dest)
272.PHONY: osx-dmg
273osx_dmg_name:=$(NAME).dmg
274osx_udzo_name:=$(NAME).udzo.dmg
275osx_dmg_volume:="$(NAME) $(VERSION)"
276osx-dmg: ../$(osx_dmg_name)
277../$(osx_dmg_name): $(osx_pkg_name)
278 -rm -f $(osx_dmg_name)
279 hdiutil create $(osx_dmg_name) -size 05m -fs HFS+ -volname $(osx_dmg_volume)
280 dev_handle=`hdid $(osx_dmg_name) | grep Apple_HFS | \
281 perl -e '\$$_=<>; /^\\/dev\\/(disk.)/; print \$$1'`; \
282 ditto $(osx_pkg_name) /Volumes/$(osx_dmg_volume)/$(osx_pkg_name); \
283 hdiutil detach $$dev_handle
284 hdiutil convert $(osx_dmg_name) -format UDZO -o $(osx_udzo_name)
285 -rm -f $(osx_dmg_name)
286 mv $(osx_udzo_name) ../$(osx_dmg_name)
288.PHONY: test test-markdown
289test: $(MAIN)
290 @cd $(TESTDIR) && perl runtests.pl -s $(PWD)/$(MAIN)
291test-markdown: $(MAIN)
292 @cd $(TESTDIR)/MarkdownTest_1.0.3 && perl MarkdownTest.pl -s $(PWD)/$(MAIN) -tidy
294# Stolen and slightly improved from a GPLed Makefile. Credits to John Meacham.
295src_all:=$(shell find $(SRCDIR) -type f -name '*hs' | egrep -v '^\./(_darcs|lib|test)/')
296cleanup_files+=$(patsubst %,$(SRCDIR)/%,tags tags.sorted)
297tags: $(src_all)
298 cd $(SRCDIR) && hasktags -c $(src_all:$(SRCDIR)/%=%); \
299 LC_ALL=C sort tags >tags.sorted; mv tags.sorted tags
301.PHONY: tarball
302fullname:=$(THIS)-$(VERSION)
303tarball_name:=$(fullname).tar.gz
304cleanup_files+=$(tarball_name)
305tarball: $(tarball_name)
306$(tarball_name):
307 svn export . $(fullname)
308 tar cvzf $(tarball_name) $(fullname)
309 -rm -rf $(fullname)
311.PHONY: deb
312deb_name:=$(shell grep ^Package debian/control | cut -d' ' -f2 | head -n 1)
313deb_version:=$(shell head -n 1 debian/changelog | cut -f2 -d' ' | tr -d '()')
314deb_arch:=i386
315deb_main:=$(deb_name)_$(deb_version)_$(deb_arch).deb
316deb: debian
317 @[ -x /usr/bin/fakeroot ] || { \
318 echo "*** Please install fakeroot package. ***"; \
319 exit 1; \
320 }
321 @[ -x /usr/bin/dpkg-buildpackage ] || { \
322 echo "*** Please install dpkg-dev package. ***"; \
323 exit 1; \
324 }
325 if [ -x /usr/bin/debuild ]; then \
326 debuild -uc -us -i.svn -I.svn -i_darcs -I_darcs --lintian-opts -i; \
327 else \
328 echo "*** Please install devscripts package. ***"; \
329 echo "*** Using dpkg-buildpackage for package building. ***"; \
330 dpkg-buildpackage -rfakeroot -uc -us -i.svn -I.svn -i_darcs -I_darcs; \
331 fi
333.PHONY: website
334web_src:=web
335web_dest:=web/pandoc
336make_page:=./$(MAIN) -s -B $(web_src)/header.html \
337 -A $(web_src)/footer.html \
338 -H $(web_src)/css
339win_main:=$(basename $(MAIN)).exe
340cleanup_files+=$(web_dest)
341website: $(web_dest)
342$(web_dest)/: $(MAIN) html $(tarball_name)
343 @[ -f ../$(osx_dmg_name) ] || { \
344 echo "*** Missing ../$(osx_dmg_name). ***"; \
345 exit 1; \
346 }
347 @[ -f ../$(deb_main) ] || { \
348 echo "*** Missing ../$(deb_main). ***"; \
349 exit 1; \
350 }
351 @[ -f $(win_main) ] || { \
352 echo "*** Missing $(win_main). ***"; \
353 exit 1; \
354 }
355 -rm -rf $(web_dest)
356 ( \
357 mkdir $(web_dest); \
358 cp -r html $(web_dest)/doc; \
359 cp ../$(osx_dmg_name) $(web_dest)/; \
360 cp ../$(deb_main) $(web_dest)/; \
361 cp $(win_main) $(web_dest)/; \
362 cp $(tarball_name) $(web_dest)/; \
363 cp $(web_src)/*.css $(web_dest)/; \
364 sed -e 's#@PREFIX@#$(PREFIX)#g' $(osx_src)/Welcome | \
365 $(make_page) > $(web_dest)/osx-notes.html; \
366 sed -e '/^ --.*/d' changelog | \
367 sed -e 's#^# #g' | \
368 sed -e 's/ \(pandoc (.*\)/## \1/g' | \
369 $(make_page) > $(web_dest)/history.html; \
370 $(make_page) README > $(web_dest)/README.html; \
371 $(make_page) INSTALL > $(web_dest)/INSTALL.html; \
372 sed -e 's/@TARBALL_NAME@/$(tarball_name)/g' $(web_src)/index.txt | \
373 sed -e 's/@DEB_NAME@/$(deb_main)/g' | \
374 sed -e 's/@OSX_DMG_NAME@/$(osx_dmg_name)/g' | \
375 sed -e 's/@WINDOWS_EXE_NAME@/$(win_main)/g' | \
376 sed -e 's/@VERSION@/$(VERSION)/g' | \
377 $(make_page) > $(web_dest)/index.html; \
378 ) || { rm -rf $(web_dest); exit 1; }
380.PHONY: distclean clean
381distclean: clean
382 if [ -d debian ]; then \
383 chmod +x debian/rules; fakeroot debian/rules clean; \
384 fi
386clean:
387 make -C $(SRCDIR)/templates clean
388 -if [ -f $(BUILDCONF) ]; then $(BUILDCMD) clean; fi
389 -rm -rf $(cleanup_files)