]>
git.proxmox.com Git - dh-cargo.git/blob - cargo.pm
1 # debhelper buildsystem for Rust crates using Cargo
3 # Josh Triplett <josh@joshtriplett.org>
5 package Debian
::Debhelper
::Buildsystem
::cargo
;
10 use Debian
::Debhelper
::Dh_Lib
;
11 use Dpkg
::Changelog
::Debian
;
12 use Dpkg
::Control
::Info
;
14 use base
'Debian::Debhelper::Buildsystem';
20 sub check_auto_buildable
{
22 if (-f
$this->get_sourcepath("Cargo.toml")) {
30 my $this = $class->SUPER::new
(@_);
31 $this->enforce_in_source_building();
35 sub pre_building_step
{
39 $this->{cargo_home
} = Cwd
::abs_path
($this->get_sourcepath("debian/cargo_home"));
40 $this->{cargo_registry
} = Cwd
::abs_path
($this->get_sourcepath("debian/cargo_registry"));
42 my $control = Dpkg
::Control
::Info-
>new();
44 my $source = $control->get_source();
45 my $crate = $source->{'X-Cargo-Crate'};
47 $crate = $source->{Source
};
49 $crate =~ s/-[0-9]+(\.[0-9]+)*$//;
51 $this->{crate
} = $crate;
52 my $changelog = Dpkg
::Changelog
::Debian-
>new(range
=> { count
=> 1 });
53 $changelog->load($this->get_sourcepath("debian/changelog"));
54 $this->{version
} = Dpkg
::Version-
>new(@{$changelog}[0]->get_version())->version();
56 my @packages = $control->get_packages();
59 $this->{featurepkg
} = [];
60 foreach my $package (@packages) {
61 if ($package->{Package
} =~ /^librust-.*-dev$/ && $package->{Architecture
} eq 'all') {
62 if ($package->{Package
} =~ /\+/) {
63 push(@{$this->{featurepkg
}}, $package->{Package
});
66 if ($this->{libpkg
}) {
67 error
("Multiple Cargo lib packages found: " . $this->{libpkg
} . " and " . $package->{Package
});
69 $this->{libpkg
} = $package->{Package
};
70 } elsif ($package->{Architecture
} ne 'all') {
71 $this->{binpkg
} = $package->{Package
};
74 if (!$this->{libpkg
} && !$this->{binpkg
}) {
75 error
("Could not find any Cargo lib or bin packages to build.");
77 if (@{$this->{featurepkg
}} && !$this->{libpkg
}) {
78 error
("Found feature packages but no lib package.");
81 my $parallel = $this->get_parallel();
82 $this->{j
} = $parallel > 0 ?
["-j$parallel"] : [];
84 $this->SUPER::pre_building_step
($step);
89 opendir(my $dirhandle, $this->get_sourcedir());
90 my @sources = grep { $_ ne '.' && $_ ne '..' && $_ ne '.git' && $_ ne 'debian' } readdir($dirhandle);
101 my $crate = $this->{crate
} . '-' . $this->{version
};
102 if ($this->{libpkg
}) {
103 my $target = $this->get_sourcepath("debian/" . $this->{libpkg
} . "/usr/share/cargo/registry/$crate");
104 my @sources = $this->get_sources();
105 doit
("mkdir", "-p", $target);
106 doit
("cp", "-at", $target, @sources);
107 doit
("cp", $this->get_sourcepath("debian/cargo-checksum.json"), "$target/.cargo-checksum.json");
109 foreach my $pkg (@{$this->{featurepkg
}}) {
110 my $target = $this->get_sourcepath("debian/$pkg/usr/share/doc");
111 doit
("mkdir", "-p", $target);
112 doit
("ln", "-s", $this->{libpkg
}, "$target/$pkg");
114 if ($this->{binpkg
}) {
115 my $registry = $this->{cargo_registry
};
116 doit
("mkdir", "-p", $this->{cargo_home
}, $registry);
117 opendir(my $dirhandle, '/usr/share/cargo/registry');
118 my @crates = map { "/usr/share/cargo/registry/$_" } grep { $_ ne '.' && $_ ne '..' } readdir($dirhandle);
119 closedir($dirhandle);
121 doit
("ln", "-st", "$registry", @crates);
123 # Handle the case of building the package with the same version of the
125 if (-l
"$registry/$crate") {
126 unlink("$registry/$crate");
128 mkdir("$registry/$crate");
129 my @sources = $this->get_sources();
130 doit
("cp", "-at", "$registry/$crate", @sources);
131 doit
("cp", $this->get_sourcepath("debian/cargo-checksum.json"), "$registry/$crate/.cargo-checksum.json");
133 open(CONFIG
, ">" . $this->{cargo_home
} . "/config");
136 replace-with = "dh-cargo-registry"
138 [source.dh-cargo-registry]
139 directory = "$registry"
142 $ENV{'CARGO_HOME'} = $this->{cargo_home
};
144 my $target = $this->get_sourcepath("debian/" . $this->{binpkg
} . "/usr");
145 doit
("cargo", "install", $this->{crate
}, "--vers", $this->{version
}, "--root", $target, @{$this->{j
}});
146 doit
("rm", "$target/.crates.toml");
152 doit
("rm", "-rf", $this->{cargo_home
}, $this->{cargo_registry
});