source: trunk/debathena/debathena/config-package-dev/divert.mk @ 25061

Revision 25061, 6.0 KB checked in by andersk, 13 years ago (diff)
RevLine 
[22854]1# -*- mode: makefile; coding: utf-8 -*-
2# Copyright © 2007-2008 Anders Kaseorg <andersk@mit.edu> and
3#                       Tim Abbott <tabbott@mit.edu>
4#
5# This program is free software; you can redistribute it and/or
6# modify it under the terms of the GNU General Public License as
7# published by the Free Software Foundation; either version 2, or (at
8# your option) any later version.
9#
10# This program is distributed in the hope that it will be useful, but
11# WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13# General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18# 02111-1307 USA.
19
[23076]20# Don't include divert.mk in your rules files directly; instead use
21# config-package.mk.
22
23# divert.mk handles the low-level diversion logic.  It includes
24# divert.sh.in in the postinst and prerm scripts, and adds
25
[22854]26ifndef _cdbs_rules_divert
27_cdbs_rules_divert = 1
28
[22961]29include /usr/share/cdbs/1/rules/debhelper.mk
30
[22923]31CDBS_BUILD_DEPENDS := $(CDBS_BUILD_DEPENDS), config-package-dev (>= 4.5~)
[22854]32
[23076]33# divert.sh.in is included in the postinst/prerm scripts of packages
34# installing diversions using config-package-dev.
[22854]35DEB_DIVERT_SCRIPT = /usr/share/config-package-dev/divert.sh.in
[23076]36# script used to encode the path of a file uniquely in a valid virtual
37# package name.
38DEB_DIVERT_ENCODER = /usr/share/config-package-dev/encode
[22854]39
40DEB_DIVERT_PACKAGES += $(foreach package,$(DEB_ALL_PACKAGES), \
[22923]41    $(if $(DEB_TRANSFORM_FILES_$(package)),$(package), \
[22918]42    $(if $(DEB_REMOVE_FILES_$(package)),$(package), \
43    $(if $(DEB_UNREMOVE_FILES_$(package)),$(package), \
44    $(if $(DEB_UNDIVERT_FILES_$(package)),$(package), \
45    $(if $(DEB_DIVERT_FILES_$(package)),$(package)))))))
[22854]46
47ifeq ($(DEB_DIVERT_EXTENSION),)
48DEB_DIVERT_EXTENSION = .divert
49endif
50
[23076]51# Replace only the last instance of DEB_DIVERT_EXTENSION in the
52# filename, to make it possible to divert /path/foo.divert to
53# foo.divert.divert-orig
[22918]54divert_files_replace_name = $(shell echo $(1) | perl -pe 's/(.*)\Q$(DEB_DIVERT_EXTENSION)\E/$$1$(2)/')
55
[24906]56# Transform a full path into the path it should be diverted to if it's
57# removed
58remove_files_name = /usr/share/$(cdbs_curpkg)/$(shell $(DEB_DIVERT_ENCODER) $(1))
59
[25061]60dh_compat_5 := $(shell if [ '$(DH_COMPAT)' -ge 5 ]; then echo y; fi)
61
62reverse = $(foreach n,$(shell seq $(words $(1)) -1 1),$(word $(n),$(1)))
63reverse_dh_compat_5 = $(if $(dh_compat_5),$(call reverse,$(1)),$(1))
64
[22854]65debian-divert/%: package = $(subst debian-divert/,,$@)
[22923]66debian-divert/%: divert_files = $(DEB_DIVERT_FILES_$(package)) $(DEB_TRANSFORM_FILES_$(package))
[22918]67debian-divert/%: divert_remove_files = $(DEB_REMOVE_FILES_$(package))
68debian-divert/%: divert_undivert_files = $(DEB_UNDIVERT_FILES_$(package))
69debian-divert/%: divert_unremove_files = $(DEB_UNREMOVE_FILES_$(package))
70debian-divert/%: divert_files_all = $(strip $(divert_files) $(divert_remove_files) $(divert_undivert_files) $(divert_unremove_files))
71debian-divert/%: divert_files_thispkg = $(strip $(divert_files) $(divert_remove_files))
[22854]72$(patsubst %,debian-divert/%,$(DEB_DIVERT_PACKAGES)) :: debian-divert/%:
[23076]73#   Writing shell scripts in makefiles sucks.  Remember to $$ shell
74#   variables and include \ at the end of each line.
75# Add code to postinst to add/remove diversions as appropriate
[23197]76        set -e; \
77        { \
[22854]78            sed 's/#PACKAGE#/$(cdbs_curpkg)/g; s/#DEB_DIVERT_EXTENSION#/$(DEB_DIVERT_EXTENSION)/g' $(DEB_DIVERT_SCRIPT); \
[22918]79            $(if $(divert_files_all), \
[22854]80                echo 'if [ "$$1" = "configure" ]; then'; \
[22918]81                $(foreach file,$(divert_undivert_files), \
[24906]82                    echo "    check_undivert_unlink $(call divert_files_replace_name,$(file), )"; )\
[22918]83                $(foreach file,$(divert_unremove_files), \
[24906]84                    echo "    check_undivert_unremove $(file) $(call remove_files_name,$(file))"; )\
[22854]85                $(foreach file,$(divert_files), \
[22918]86                    echo "    divert_link $(call divert_files_replace_name,$(file), )";) \
87                $(foreach file,$(divert_remove_files), \
[23196]88                    mkdir -p debian/$(cdbs_curpkg)/usr/share/$(cdbs_curpkg); \
[24906]89                    echo "    divert_remove $(file) $(call remove_files_name,$(file))";) \
[22854]90                echo 'fi'; \
91            ) \
[23197]92        } >> $(CURDIR)/debian/$(cdbs_curpkg).postinst.debhelper
[23076]93# Add code to prerm script to undo diversions when package is removed.
[23197]94        set -e; \
95        { \
[25061]96            $(if $(dh_compat_5),, \
97                if [ -e $(CURDIR)/debian/$(cdbs_curpkg).prerm.debhelper ]; then \
98                    cat $(CURDIR)/debian/$(cdbs_curpkg).prerm.debhelper; \
99                fi;) \
[22854]100            sed 's/#PACKAGE#/$(cdbs_curpkg)/g; s/#DEB_DIVERT_EXTENSION#/$(DEB_DIVERT_EXTENSION)/g' $(DEB_DIVERT_SCRIPT); \
[22918]101            $(if $(divert_files_thispkg), \
[22854]102                echo 'if [ "$$1" = "remove" ]; then'; \
[25061]103                $(foreach file,$(call reverse_dh_compat_5,$(divert_files)), \
[22918]104                    echo "    undivert_unlink $(call divert_files_replace_name,$(file), )";) \
[25061]105                $(foreach file,$(call reverse_dh_compat_5,$(divert_remove_files)), \
[22918]106                    echo "    undivert_unremove $(file) $(cdbs_curpkg)";) \
[22854]107                echo 'fi'; \
108            ) \
[25061]109            $(if $(dh_compat_5), \
110                if [ -e $(CURDIR)/debian/$(cdbs_curpkg).prerm.debhelper ]; then \
111                    cat $(CURDIR)/debian/$(cdbs_curpkg).prerm.debhelper; \
112                fi;) \
113        } >> $(CURDIR)/debian/$(cdbs_curpkg).prerm.debhelper.new
114        mv $(CURDIR)/debian/$(cdbs_curpkg).prerm.debhelper.new \
115            $(CURDIR)/debian/$(cdbs_curpkg).prerm.debhelper
[23076]116# Add an encoding of the names of the diverted files to the Provides:
117# and Conflicts: lists.  This prevents two packages diverting the same
118# file from being installed simultaneously (it cannot work, and this
119# produces a much less ugly error).  Requires in debian/control:
120#   Provides: $(diverted-files)
121#   Conflicts: $(diverted-files)
[23197]122        set -e; \
123        { \
[22854]124            echo -n "diverted-files="; \
[22918]125            $(foreach file,$(divert_files_thispkg),\
[23076]126                echo -n "diverts-"; \
[22918]127                ${DEB_DIVERT_ENCODER} "$(call divert_files_replace_name,$(file))"; \
[22854]128                echo -n ", ";) \
[23197]129            echo; \
130        } >> $(CURDIR)/debian/$(cdbs_curpkg).substvars
[22854]131
[22961]132$(patsubst %,binary-post-install/%,$(DEB_DIVERT_PACKAGES)) :: binary-post-install/%: debian-divert/%
[22854]133
134endif
Note: See TracBrowser for help on using the repository browser.