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 B<dh_link> is a debhelper program that creates symlinks in package build
22 B<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 B<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 B<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
57 Create any links specified by command line parameters in ALL packages
58 acted on, not just the first.
60 =item B<-X>I<item>, B<--exclude=>I<item>
62 Exclude symlinks that contain I<item> anywhere in their filename from
63 being corrected to comply with Debian policy.
65 =item I<source destination> ...
67 Create a file named I<destination> as a link to a file named I<source>. Do
68 this in the package build directory of the first package acted on.
69 (Or in all packages if B<-A> is specified.)
75 dh_link usr/share/man/man1/foo.1 usr/share/man/man1/bar.1
77 Make F<bar.1> be a symlink to F<foo.1>
79 dh_link var/lib/foo usr/lib/foo \
80 usr/share/man/man1/foo.1 usr/share/man/man1/bar.1
82 Make F</usr/lib/foo/> be a link to F</var/lib/foo/>, and F<bar.1> be a symlink to
87 # This expand_path expands all path "." and ".." components, but doesn't
88 # resolve symbolic links.
90 my $start = @_ ? shift : '.';
91 my @pathname = split(m:/+:,$start);
95 foreach $entry (@pathname) {
96 if ($entry eq '.' || $entry eq '') {
99 elsif ($entry eq '..') {
100 if ($#respath == -1) {
108 push @respath, $entry;
113 foreach $entry (@respath) {
114 $result .= '/' . $entry;
116 if (! defined $result) {
117 $result="/"; # special case
125 foreach my $package (@{$dh{DOPACKAGES}}) {
126 my $tmp=tmpdir($package);
127 my $file=pkgfile($package,"links");
131 @links=filearray($file);
134 # Make sure it has pairs of symlinks and destinations. If it
135 # doesn't, $#links will be _odd_ (not even, -- it's zero-based).
136 if (int($#links/2) eq $#links/2) {
137 error("$file lists a link without a destination.");
140 if (($package eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
144 # Same test as above, including arguments this time.
145 if (int($#links/2) eq $#links/2) {
146 error("parameters list a link without a destination.");
149 # v4 or later and only if there is a temp dir already
150 if (! compat(3) && -e $tmp) {
151 # Scan for existing links and add them to @links, so they
152 # are recreated policy conformant.
156 return if excludefile($_);
157 my $dir=$File::Find::dir;
159 my $target = readlink($_);
160 if ($target=~/^\//) {
161 push @links, $target;
164 push @links, "$dir/$target";
166 push @links, "$dir/$_";
174 my $src=expand_path(pop @links);
180 warning("skipping link from $src to self");
184 # Make sure the directory the link will be in exists.
185 my $basedir=dirname("$tmp/$dest");
187 doit("install","-d",$basedir);
190 # Policy says that if the link is all within one toplevel
191 # directory, it should be relative. If it's between
192 # top level directories, leave it absolute.
193 my @src_dirs=split(m:/+:,$src);
194 my @dest_dirs=split(m:/+:,$dest);
195 if (@src_dirs > 0 && $src_dirs[0] eq $dest_dirs[0]) {
196 # Figure out how much of a path $src and $dest
199 for ($x=0; $x < @src_dirs && $src_dirs[$x] eq $dest_dirs[$x]; $x++) {}
200 # Build up the new src.
202 for (1..$#dest_dirs - $x) {
205 for ($x .. $#src_dirs) {
206 $src.=$src_dirs[$_]."/";
208 if ($x > $#src_dirs && ! length $src) {
209 $src.="."; # special case
214 # Make sure it's properly absolute.
218 if (-d "$tmp/$dest" && ! -l "$tmp/$dest") {
219 error("link destination $tmp/$dest is a directory");
221 doit("rm", "-f", "$tmp/$dest");
222 doit("ln","-sf", $src, "$tmp/$dest");
230 This program is a part of debhelper.
234 Joey Hess <joeyh@debian.org>