5 dh_link - create symlinks in package build directories
11 use Debian::Debhelper::Dh_Lib;
15 B<dh_link> [S<I<debhelper options>>] [B<-A>] [B<-X>I<item>] [S<I<source destination ...>>]
19 dh_link is a debhelper program that creates symlinks in package build
22 dh_link accepts a list of pairs of source and destination files. The source
23 files are the already existing files that will be symlinked from. The
24 destination files are the symlinks that will be created. There B<must> be
25 an equal number of source and destination files specified.
27 Be sure you B<do> specify the full filename to both the source and
28 destination files (unlike you would do if you were using something like
31 dh_link will generate symlinks that comply with debian policy - absolute
32 when policy says they should be absolute, and relative links with as short
33 a path as possible. It will also create any subdirectories it needs to to put
36 dh_link also scans the package build tree for existing symlinks which do not
37 conform to debian policy, and corrects them (v4 or later).
43 =item debian/I<package>.links
45 Lists pairs of source and destination files to be symlinked. Each pair
46 should be put on its own line, with the source and destination separated by
55 Create any links specified by command line parameters in ALL packages
56 acted on, not just the first.
58 =item B<-Xitem>, B<--exclude=item>
60 Do not correct symlinks that contain "item" anywhere in their filename from
61 being corrected to comply with debian policy.
63 =item I<source destination ...>
65 Create a file named "destination" as a link to a file named "source". Do
66 this in the package build directory of the first package acted on.
67 (Or in all packages if -A is specified.)
73 dh_link usr/share/man/man1/foo.1 usr/share/man/man1/bar.1
75 Make bar.1 be a symlink to foo.1
77 dh_link var/lib/foo usr/lib/foo \
78 usr/share/man/man1/foo.1 usr/share/man/man1/bar.1
80 Make /usr/lib/foo/ be a link to /var/lib/foo/, and bar.1 be a symlink to
85 # This expand_path expands all path "." and ".." components, but doesn't
86 # resolve symbolic links.
88 my $start = @_ ? shift : '.';
89 my @pathname = split(m:/+:,$start);
93 foreach $entry (@pathname) {
94 if ($entry eq '.' || $entry eq '') {
97 elsif ($entry eq '..') {
98 if ($#respath == -1) {
106 push @respath, $entry;
111 foreach $entry (@respath) {
112 $result .= '/' . $entry;
114 if (! defined $result) {
115 $result="/"; # special case
123 foreach my $package (@{$dh{DOPACKAGES}}) {
124 my $tmp=tmpdir($package);
125 my $file=pkgfile($package,"links");
129 @links=filearray($file);
132 # Make sure it has pairs of symlinks and destinations. If it
133 # doesn't, $#links will be _odd_ (not even, -- it's zero-based).
134 if (int($#links/2) eq $#links/2) {
135 error("$file lists a link without a destination.");
138 if (($package eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
142 # Same test as above, including arguments this time.
143 if (int($#links/2) eq $#links/2) {
144 error("parameters list a link without a destination.");
147 # v4 or later and only if there is a temp dir already
148 if (! compat(3) && -e $tmp) {
149 # Scan for existing links and add them to @links, so they
150 # are recreated policy conformant.
154 return if excludefile($_);
155 my $dir=$File::Find::dir;
157 my $target = readlink($_);
158 if ($target=~/^\//) {
159 push @links, $target;
162 push @links, "$dir/$target";
164 push @links, "$dir/$_";
172 my $src=expand_path(pop @links);
178 warning("skipping link from $src to self");
182 # Make sure the directory the link will be in exists.
183 my $basedir=dirname("$tmp/$dest");
185 doit("install","-d",$basedir);
188 # Policy says that if the link is all within one toplevel
189 # directory, it should be relative. If it's between
190 # top level directories, leave it absolute.
191 my @src_dirs=split(m:/+:,$src);
192 my @dest_dirs=split(m:/+:,$dest);
193 if (@src_dirs > 0 && $src_dirs[0] eq $dest_dirs[0]) {
194 # Figure out how much of a path $src and $dest
197 for ($x=0; $x < @src_dirs && $src_dirs[$x] eq $dest_dirs[$x]; $x++) {}
198 # Build up the new src.
200 for (1..$#dest_dirs - $x) {
203 for ($x .. $#src_dirs) {
204 $src.=$src_dirs[$_]."/";
206 if ($x > $#src_dirs && ! length $src) {
207 $src.="."; # special case
212 # Make sure it's properly absolute.
216 if (-d "$tmp/$dest" && ! -l "$tmp/$dest") {
217 error("link destination $tmp/$dest is a directory");
219 doit("rm", "-f", "$tmp/$dest");
220 doit("ln","-sf", $src, "$tmp/$dest");
228 This program is a part of debhelper.
232 Joey Hess <joeyh@debian.org>