aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: 657a0c4ffa4d9a6b9624d10638767081243c95af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# Makefile for Pandoc.

#-------------------------------------------------------------------------------
# Constant names and commands in source tree
#-------------------------------------------------------------------------------
CABAL     := Pandoc.cabal
SRCDIR    := src
MANDIR    := man
TESTDIR   := tests
BUILDDIR  := dist
BUILDCONF := .setup-config
BUILDCMD  := runhaskell Setup.hs
BUILDVARS := vars
CONFIGURE := configure

#-------------------------------------------------------------------------------
# Cabal constants
#-------------------------------------------------------------------------------
NAME      := $(shell sed -ne 's/^[Nn]ame:[[:space:]]*//p' $(CABAL))
THIS      := $(shell echo $(NAME) | tr A-Z a-z)
VERSION   := $(shell sed -ne 's/^version[[:space:]]*=[[:space:]]*"\([^"]*\)"/\1/p' $(SRCDIR)/Main.hs)
RELNAME   := $(THIS)-$(VERSION)
EXECSBASE := $(shell sed -ne 's/^[Ee]xecutable:[[:space:]]*//p' $(CABAL))

#-------------------------------------------------------------------------------
# Install targets
#-------------------------------------------------------------------------------
WRAPPERS  := html2markdown markdown2pdf hsmarkdown
# Add .exe extensions if we're running Windows/Cygwin.
EXTENSION := $(shell uname | tr '[:upper:]' '[:lower:]' | \
               sed -ne 's/^cygwin.*$$/\.exe/p')
EXECS     := $(addsuffix $(EXTENSION),$(EXECSBASE))
PROGS     := $(EXECS) $(WRAPPERS) 
MAIN      := $(firstword $(EXECS))
DOCS      := README.html README BUGS 

#-------------------------------------------------------------------------------
# Variables to setup through environment
#-------------------------------------------------------------------------------

# Specify default values.
prefix    := /usr/local
destdir   := 
# Attempt to set variables from a previous make session.
-include $(BUILDVARS)
# Fallback to defaults but allow to get the values from environment.
PREFIX    ?= $(prefix)
DESTDIR   ?= $(destdir)

#-------------------------------------------------------------------------------
# Installation paths
#-------------------------------------------------------------------------------
DESTPATH    := $(DESTDIR)$(PREFIX)
BINPATH     := $(DESTPATH)/bin
DATAPATH    := $(DESTPATH)/share
LIBPATH     := $(DESTPATH)/$(NAME)-$(VERSION)
DOCPATH     := $(DATAPATH)/doc/$(THIS)
LIBDOCPATH  := $(DATAPATH)/doc/$(THIS)-doc
MANPATH     := $(DATAPATH)/man
PKGPATH     := $(DATAPATH)/$(THIS)

#-------------------------------------------------------------------------------
# Generic Makefile variables
#-------------------------------------------------------------------------------
INSTALL         := install -c
INSTALL_PROGRAM := $(INSTALL) -m 755
INSTALL_DATA    := $(INSTALL) -m 644
STRIP           := strip
GHC             := ghc
GHC_PKG         := ghc-pkg

#-------------------------------------------------------------------------------
# Recipes
#-------------------------------------------------------------------------------

# Default target.
.PHONY: all
all: build-program

# Document process rules.
%.html: % $(MAIN)
	./$(MAIN) -s $< >$@ || rm -f $@
%.tex: % $(MAIN)
	./$(MAIN) -s -w latex $< >$@ || rm -f $@
%.rtf: % $(MAIN)
	./$(MAIN) -s -w rtf $< >$@ || rm -f $@
%.pdf: % $(MAIN) markdown2pdf
	sh ./markdown2pdf $< || rm -f $@
%.txt: %
	perl -p -e 's/\n/\r\n/' $< > $@ || rm -f $@ # convert to DOS line endings

.PHONY: templates
templates: $(SRCDIR)/templates
	$(MAKE) -C $(SRCDIR)/templates

define generate-shell-script
echo "Generating $@...";                                 \
awk '                                                    \
	/^[ \t]*###+ / {                                 \
                lead = $$0; sub(/[^ \t].*$$/, "", lead); \
		t = "$(dir $<)/"$$2;                     \
		while (getline line < t > 0)             \
			print lead line;                 \
		next;                                    \
	}                                                \
	{ print }                                        \
' <$< >$@
chmod +x $@
endef

.PHONY: wrappers
wrappers: $(WRAPPERS)
cleanup_files+=$(WRAPPERS)
$(WRAPPERS): %: $(SRCDIR)/wrappers/%.in $(SRCDIR)/wrappers/*.sh
	@$(generate-shell-script)

.PHONY: configure
cleanup_files+=$(BUILDDIR) $(BUILDCONF) $(BUILDVARS)
configure: $(BUILDCONF) templates
$(BUILDCONF): $(CABAL)
	$(BUILDCMD) configure --prefix=$(PREFIX)
	# Make configuration time settings persistent (definitely a hack).
	@echo "PREFIX?=$(PREFIX)" >$(BUILDVARS)
	@echo "DESTDIR?=$(DESTDIR)" >>$(BUILDVARS)

.PHONY: build
build: configure
	$(BUILDCMD) build

.PHONY: build-exec
build-exec: $(PROGS)
cleanup_files+=$(EXECS)
$(EXECS): build
	for f in $@; do \
		[ -f $$f ] || { \
			find $(BUILDDIR) -type f -name "$$f" \
							 -perm +a=x -exec ln -s {} . \; ; \
		} \
	done

.PHONY: build-doc
cleanup_files+=README.html 
build-doc: $(DOCS)

.PHONY: build-program
build-program: build-exec build-doc

.PHONY: build-lib-doc haddock
build-lib-doc: html
haddock: build-lib-doc
cleanup_files+=html
html/: configure
	-rm -rf html
	$(BUILDCMD) haddock && mv $(BUILDDIR)/doc/html .

.PHONY: build-all
build-all: build-program build-lib-doc

# User documents installation.
.PHONY: install-doc uninstall-doc
man_all:=$(patsubst $(MANDIR)/%,%,$(wildcard $(MANDIR)/man?/*.1))
install-doc: build-doc
	$(INSTALL) -d $(DOCPATH) && $(INSTALL_DATA) $(DOCS) $(DOCPATH)/
	for f in $(man_all); do \
		$(INSTALL) -d $(MANPATH)/$$(dirname $$f); \
		$(INSTALL_DATA) $(MANDIR)/$$f $(MANPATH)/$$f; \
	done
uninstall-doc:
	-for f in $(DOCS); do rm -f $(DOCPATH)/$$f; done
	-for f in $(man_all); do rm -f $(MANPATH)/$$f; done
	-rmdir $(DOCPATH)

# Library documents installation.
.PHONY: install-lib-doc uninstall-lib-doc
install-lib-doc: build-lib-doc
	$(INSTALL) -d $(LIBDOCPATH) && cp -R html $(LIBDOCPATH)/
uninstall-lib-doc:
	-rm -rf $(LIBDOCPATH)/html
	-rmdir $(LIBDOCPATH)

# Helper to install the given files $(1) into the path $(2).
# It also has the ability to follow symlinks.
install-executable-files = \
	$(INSTALL) -d $(2); \
	for f in $(1); do \
		if [ -L $$f ]; then \
			f=$$(readlink $$f); \
		fi; \
		$(INSTALL_PROGRAM) $$f $(2)/; \
	done

# Program only installation.
.PHONY: install-exec uninstall-exec
install-exec: build-exec
	$(STRIP) $(EXECS)
	$(call install-executable-files,$(PROGS),$(BINPATH))
uninstall-exec:
	-for f in $(notdir $(PROGS)); do rm -f $(BINPATH)/$$f; done ;

# Program + user documents installation.
.PHONY: install-program uninstall-program
install-program: install-exec install-doc
uninstall-program: uninstall-exec uninstall-doc

# Install everything.
.PHONY: install-all uninstall-all
install-all: build-exec install-doc install-lib-doc
	# Install the library (+ main executable) and register it.
	destdir=$(DESTDIR); \
	# Older Cabal versions have no '--destdir' option.
	if $(BUILDCMD) copy --help | grep -q '\-\-destdir'; then \
		opt="--destdir=$${destdir-/}"; \
	else \
		opt="--copy-prefix=$${destdir}$(PREFIX)"; \
	fi; \
	$(BUILDCMD) copy $$opt; \
	$(BUILDCMD) register
	# Note that, we are in the position of having to install the wrappers
	# separately, as Cabal installs the main exec along with the library.
	$(call install-executable-files,$(WRAPPERS),$(BINPATH))
uninstall-all: uninstall-exec uninstall-doc uninstall-lib-doc
	-pkg_id="$(NAME)-$(VERSION)"; \
	libdir=$$($(GHC_PKG) field $$pkg_id library-dirs 2>/dev/null | \
		  sed 's/^library-dirs: *//'); \
	if [ -d "$$libdir" ]; then \
		$(BUILDCMD) unregister; \
		rm -rf $$libdir; \
		rmdir $$(dirname $$libdir); \
	else \
		echo "*** Couldn't locate library files for pkgid: $$pkg_id. ***"; \
	fi

# Default installation recipe for a common deployment scenario.
.PHONY: install uninstall
install: install-program
uninstall: uninstall-program

# OSX packages:  make osx-pkg-prep, then (as root) make osx-pkg
.PHONY: osx-pkg osx-pkg-prep
osx_dest:=osx-pkg-tmp
osx_src:=osx
doc_more:=README.rtf COPYRIGHT.rtf $(osx_src)/Welcome.rtf
osx_pkg_name:=$(RELNAME).pkg
cleanup_files+=$(osx_dest) $(doc_more) $(osx_pkg_name)
osx-pkg-prep: $(osx_dest)
$(osx_dest)/: build-program $(doc_more)
	-rm -rf $(osx_dest)
	$(INSTALL) -d $(osx_dest)
	DESTDIR=$(osx_dest)/Package_root $(MAKE) install-program
	cp $(osx_src)/uninstall-pandoc $(osx_dest)/Package_root/usr/local/bin/
	find $(osx_dest) -type f -regex ".*bin/.*" | xargs chmod +x
	find $(osx_dest) -type f -regex ".*bin/$(notdir $(MAIN))" | xargs $(STRIP)
	$(INSTALL) -d $(osx_dest)/Resources
	cp README.rtf $(osx_dest)/Resources/ReadMe.rtf
	cp COPYRIGHT.rtf $(osx_dest)/Resources/License.rtf
	sed -e 's#@PREFIX@#$(PREFIX)#g' $(osx_src)/Welcome.rtf > $(osx_dest)/Resources/Welcome.rtf
	sed -e 's/@VERSION@/$(VERSION)/g' $(osx_src)/Info.plist > $(osx_dest)/Info.plist
	cp $(osx_src)/Description.plist $(osx_dest)/
osx-pkg: $(osx_pkg_name)
$(osx_pkg_name)/: $(osx_dest)
	if [ "`id -u`" != 0 ]; then \
		echo "Root permissions needed to create OSX package!"; \
		exit 1; \
	fi
	find $(osx_dest) | xargs chown root:wheel
	PackageMaker -build -p $(osx_pkg_name) \
	                    -f $(osx_dest)/Package_root \
	                    -r $(osx_dest)/Resources \
	                    -i $(osx_dest)/Info.plist \
	                    -d $(osx_dest)/Description.plist
	chgrp admin $(osx_pkg_name)
	-rm -rf $(osx_dest)

.PHONY: osx-dmg
osx_dmg_name:=$(RELNAME).dmg
cleanup_files+=$(osx_dmg_name)
osx_udzo_name:=$(RELNAME).udzo.dmg
osx_dmg_volume:="$(NAME) $(VERSION)"
osx-dmg: $(osx_dmg_name)
$(osx_dmg_name): $(osx_pkg_name)
	-rm -f $(osx_dmg_name)
	hdiutil create $(osx_dmg_name) -size 05m -fs HFS+ -volname $(osx_dmg_volume)
	dev_handle=`hdid $(osx_dmg_name) | grep Apple_HFS | \
		    perl -e '\$$_=<>; /^\\/dev\\/(disk.)/; print \$$1'`; \
	ditto $(osx_pkg_name) /Volumes/$(osx_dmg_volume)/$(osx_pkg_name); \
	hdiutil detach $$dev_handle
	hdiutil convert $(osx_dmg_name) -format UDZO -o $(osx_udzo_name)
	-rm -f $(osx_dmg_name)
	mv $(osx_udzo_name) $(osx_dmg_name)


.PHONY: win-pkg
win_pkg_name:=$(RELNAME).zip
win_docs:=COPYING.txt COPYRIGHT.txt BUGS.txt README.txt README.html
cleanup_files+=$(win_pkg_name) $(win_docs)
win-pkg: $(win_pkg_name)
$(win_pkg_name): $(THIS).exe  $(win_docs)
	zip -r $(win_pkg_name) $(THIS).exe $(win_docs)

.PHONY: test test-markdown
test: $(MAIN)
	@cd $(TESTDIR) && perl runtests.pl -s $(PWD)/$(MAIN)
compat:=$(PWD)/hsmarkdown
markdown_test_dirs:=$(wildcard $(TESTDIR)/MarkdownTest_*)
test-markdown: $(MAIN) $(compat)
	@for suite in $(markdown_test_dirs); do \
		( \
			suite_version=$$(echo $$suite | sed -e 's/.*_//');\
			echo "-----------------------------------------";\
			echo "Running Markdown test suite version $${suite_version}.";\
			PATH=$(PWD):$$PATH; export PATH; cd $$suite && \
			perl MarkdownTest.pl -s $(compat) -tidy ; \
		) \
	done

# Stolen and slightly improved from a GPLed Makefile.  Credits to John Meacham.
src_all:=$(shell find $(SRCDIR) -type f -name '*hs' | egrep -v '^\./(_darcs|lib|test)/')
cleanup_files+=$(patsubst %,$(SRCDIR)/%,tags tags.sorted)
tags: $(src_all)
	cd $(SRCDIR) && hasktags -c $(src_all:$(SRCDIR)/%=%); \
	LC_ALL=C sort tags >tags.sorted; mv tags.sorted tags

.PHONY: tarball
tarball_name:=$(RELNAME).tar.gz
cleanup_files+=$(tarball_name)
tarball: $(tarball_name)
$(tarball_name):
	svn export . $(RELNAME)
	$(MAKE) -C $(RELNAME) templates
	$(MAKE) -C $(RELNAME) wrappers
	tar cvzf $(tarball_name) $(RELNAME)
	-rm -rf $(fullname)

.PHONY: deb
deb_name:=$(shell grep ^Package debian/control | cut -d' ' -f2 | head -n 1)
deb_version:=$(shell head -n 1 debian/changelog | cut -f2 -d' ' | tr -d '()')
deb_arch:=i386
deb_main:=$(deb_name)_$(deb_version)_$(deb_arch).deb
deb: debian
	@[ -x /usr/bin/fakeroot ] || { \
		echo "*** Please install fakeroot package. ***"; \
		exit 1; \
	}
	@[ -x /usr/bin/dpkg-buildpackage ] || { \
		echo "*** Please install dpkg-dev package. ***"; \
		exit 1; \
	}
	if [ -x /usr/bin/debuild ]; then \
		debuild -uc -us -i.svn -I.svn -i_darcs -I_darcs --lintian-opts -i; \
	else \
		echo "*** Please install devscripts package. ***"; \
		echo "*** Using dpkg-buildpackage for package building. ***"; \
		dpkg-buildpackage -rfakeroot -uc -us -i.svn -I.svn -i_darcs -I_darcs; \
	fi

.PHONY: website
web_src:=web
web_dest:=web/pandoc
make_page:=./$(MAIN) -s -S -B $(web_src)/header.html \
                        -A $(web_src)/footer.html \
	                -H $(web_src)/css 
cleanup_files+=$(web_dest)
website: $(MAIN) html
	-rm -rf $(web_dest)
	( \
		mkdir $(web_dest); \
		cp -r html $(web_dest)/doc; \
		cp $(web_src)/*.css $(web_dest)/; \
		cp $(web_src)/css $(web_dest)/; \
		sed -e 's#@PREFIX@#$(PREFIX)#g' $(osx_src)/Welcome > \
			$(web_dest)/osx-notes.txt; \
		sed -e '/^ --.*/d' -e 's#^#    #g' changelog | \
			sed -e 's/    \(pandoc (.*\)/## \1/g' > \
			$(web_dest)/changelog.txt; \
		cp README $(web_dest)/ ; \
		cp INSTALL $(web_dest)/ ; \
		sed -e 's/@TARBALL_NAME@/$(tarball_name)/g' $(web_src)/index.txt | \
			sed -e 's/@OSX_DMG_NAME@/$(osx_dmg_name)/g' | \
			sed -e 's/@WINDOWS_PKG_NAME@/$(win_pkg_name)/g' | \
			sed -e 's/@VERSION@/$(VERSION)/g' > $(web_dest)/index.txt; \
		cp $(web_src)/features.txt $(web_dest)/ ; \
		cp $(web_src)/myheader.tex $(web_dest)/ ; \
		cp $(web_src)/S5DEMO $(web_dest)/ ; \
		cp $(web_src)/header.html $(web_dest)/ ; \
		cp $(web_src)/footer.html $(web_dest)/ ; \
		cp $(web_src)/mkdemos.pl $(web_dest)/ ; \
		cp $(web_src)/demos $(web_dest)/ ; \
		cp $(web_src)/docbook.css $(web_dest)/ ; \
		cp $(web_src)/config.xsl $(web_dest)/ ; \
		cp $(web_src)/Makefile $(web_dest)/ ; \
		PANDOC_PATH=$(shell pwd) make -C $(web_dest) ; \
	) || { rm -rf $(web_dest); exit 1; }

.PHONY: distclean clean
distclean: clean
	if [ -d debian ]; then \
		chmod +x debian/rules; fakeroot debian/rules clean; \
	fi

clean:
	make -C $(SRCDIR)/templates clean
	-if [ -f $(BUILDCONF) ]; then $(BUILDCMD) clean; fi
	-rm -rf $(cleanup_files)