chiark / gitweb /
Merge branch 'master' into refs/top-bases/debian/locations
[topgit.git] / debian / tg2quilt.mk
1 #
2 # tg2quilt.mk - TopGit-to-quilt functionality for debian/rules files
3 #
4 # This make(1) snippet facilitates the conversion of TopGit branches to
5 # a quilt series.
6 #
7 # It is intended to be included from debian/rules files of TopGit-using
8 # packages after including the quilt rules, like so:
9 #
10 #   include /usr/share/quilt/quilt.make
11 #   -include /usr/share/topgit/tg2quilt.mk
12 #
13 # The leading dash is necessary for make not to die when the file is not
14 # installed. TopGit is not a build dependency (and does not need to be), and
15 # if the package is not installed, debian/rules can still be used normally.
16 #
17 # The snippet exports the following targets. These targets only perform the
18 # describe behaviour when invoked from a TopGit repository (`tg summary -t`
19 # returns a non-empty set); used outside, they simply output informational
20 # messages but do not cause errors.
21 #
22 #       tg-export: exports the TopGit patches into a quilt series under
23 #                  debian/patches.
24 #                    You may set TG_BRANCHES to a comma- or -space-separated
25 #                  subset of branches (but not comma-and-space-separated) to
26 #                  export (before including the file).
27 #                    If debian/patches/series already exists, the target
28 #                  will take note, blather a bit, and get out of the way.
29 #
30 #        tg-clean: cleans the source tree, just like the debian/rules clean
31 #                  target, and invokes tg-rmdir
32 #
33 #        tg-rmdir: tries to remove debian/patches, but only if there are no
34 #                  non-TopGit files under the directory.
35 #                    The heuristic is to find files that do not contain a line
36 #                  matchines /^tg:/, minus the series file. If any such files
37 #                  are found, an error occurs. Otherwise, the directory is
38 #                  removed. This means that edits to the series file are
39 #                  likely to vanish.
40 #
41 #  tg-cleanexport: recreates the debian/patches directory from scratch, using
42 #                  tg-rmdir and tg-export.
43 #
44 #   tg-forceclean: cleans the source tree, just like the debian/rules clean
45 #                  target, and forcefully removes the debian/patches
46 #                  directory in doing so. Yes, *force*-fully. WHAM!
47 #
48 # This file also hooks into the standard debian/rules and quilt targets such
49 # that tg-export is automatically invoked before quilt gets a chance to patch
50 # or unpatch the tree.
51 #
52 # The QUILT_PATCH_DIR variable can be set before including the file to override
53 # the default debian/patches location.
54 #
55 # More information, particularly for people working on TopGit-using packages,
56 # can be found in /usr/share/doc/topgit/HOWTO-tg2quilt.gz .
57 #
58 # Copyright © 2008 martin f. krafft <madduck@debian.org> Released under terms
59 # of the the Artistic Licence 2.0.
60 #
61
62 ifeq ($(shell tg summary -t),)
63   # This is not a TopGit branch, so just blubber a bit.
64
65   tg-export tg-clean tg-forceclean tg-rmdir tg-cleanexport:
66         @echo "E: The $@ target only works from a TopGit repository." >&2
67 else
68
69 # We are in a TopGit branch, so let the fun begin.
70
71 ifdef PATCHES_DIR
72         DUMMY := $(warning W: The $$PATCHES_DIR variable is deprecated, please use $$QUILT_PATCH_DIR instead.)
73         DUMMY := $(warning W: Sleeping for 10 seconds so you can read this!)
74         DUMMY := $(shell sleep 10)
75         QUILT_PATCH_DIR := $(PATCHES_DIR)
76 endif
77
78 QUILT_PATCH_DIR ?= debian/patches
79 QUILT_STAMPFN ?= patch
80
81 # Hook tg-export into quilt's make(1) snippet such that it gets executed
82 # before quilt patches or unpatches.
83 $(QUILT_STAMPFN): tg-export
84 unpatch: __tg-temp-export
85 __tg-temp-export:
86         @echo "Exporting TopGit branches to series so that quilt can clean up..." >&2
87         $(MAKE) --no-print-directory -f debian/rules tg-export
88 .PHONY: __tg-temp-export
89
90 # Set some tg-export-specific variables, e.g. default TG_BRANCHES to all
91 # TopGit branches
92 tg-export: TG_BRANCHES ?= $(shell tg summary -t)
93         # see make manual for this trick:
94 tg-export: __TG_COMMA := ,
95 tg-export: __TG_EMPTY :=
96 tg-export: __TG_SPACE := $(__TG_EMPTY) $(__TG_EMPTY)
97
98 ifeq ($(wildcard $(QUILT_PATCH_DIR)/series),)
99   # The series file does not exist, so we proceed normally
100
101   # tg-export will not work if the target dir already exists, so try to remove
102   # it by calling tg-rmdir
103   tg-export: tg-rmdir
104         tg export -b $(subst $(__TG_SPACE),$(__TG_COMMA),$(TG_BRANCHES)) --quilt $(QUILT_PATCH_DIR)
105 else
106   # The series file already exists, so assume there is nothing to do.
107   tg-export:
108         @echo "I: TopGit patch series already exists, nothing to do." >&2
109         @echo "I: (invoke the \`tg-clean\` target to remove the series.)" >&2
110         @echo "I: (you can ignore this message during a \`tg-clean\` run.)" >&2
111 endif
112
113 ifeq ($(wildcard $(QUILT_PATCH_DIR)),)
114   # No patch directory, so nothing to do:
115   tg-rmdir:
116         @true
117
118 else
119   # There is a patch directory, let's try to clean it out:
120   tg-rmdir: __TG_FILES := $(shell find $(QUILT_PATCH_DIR) -type f -a -not -path \*/series \
121                                     | xargs grep -l '^tg:')
122   tg-rmdir:
123         # remove all files whose contents matches /^tg:/
124         test -n "$(__TG_FILES)" && rm $(__TG_FILES) || :
125         # remove the series file
126         test -f $(QUILT_PATCH_DIR)/series && rm $(QUILT_PATCH_DIR)/series || :
127         # try to remove directories
128         find $(QUILT_PATCH_DIR) -depth -type d -empty -execdir rmdir {} +
129         # fail if the directory could not be removed and still exists
130         @test ! -d $(QUILT_PATCH_DIR) || { \
131           echo "E: $(QUILT_PATCH_DIR) contains non-TopGit-generated files:" >&2; \
132           find $(QUILT_PATCH_DIR) -type f -printf 'E:   %P\n' >&2; \
133           false; \
134         }
135 endif
136
137 # Make sure that we try to clean up the patches directory last
138 tg-clean: clean
139         $(MAKE) --no-print-directory -f debian/rules tg-rmdir
140
141 tg-forceclean: clean
142         test -d $(QUILT_PATCH_DIR) && rm -r $(QUILT_PATCH_DIR) || :
143
144 tg-cleanexport: tg-rmdir
145         $(MAKE) --no-print-directory -f debian/rules tg-export
146
147 endif
148
149 .PHONY: tg-clean tg-export tg-forceclean tg-rmdir
150
151 # vim:ft=make:ts=8:noet
152 # -*- Makefile -*-, you silly Emacs! (shamelessly stolen from quilt)