]> git.proxmox.com Git - dh-cargo.git/blob - cargo.pm
Rewrite to avoid invoking cargo directly on .crate contents
[dh-cargo.git] / cargo.pm
1 # debhelper buildsystem for Rust crates using Cargo
2 #
3 # Josh Triplett <josh@joshtriplett.org>
4
5 package Debian::Debhelper::Buildsystem::cargo;
6
7 use strict;
8 use warnings;
9 use Cwd;
10 use Debian::Debhelper::Dh_Lib;
11 use Dpkg::Changelog::Debian;
12 use Dpkg::Control::Info;
13 use Dpkg::Version;
14 use base 'Debian::Debhelper::Buildsystem';
15
16 sub DESCRIPTION {
17 "Rust Cargo"
18 }
19
20 sub check_auto_buildable {
21 my $this = shift;
22 if (-f $this->get_sourcepath("Cargo.toml")) {
23 return 1;
24 }
25 return 0;
26 }
27
28 sub new {
29 my $class = shift;
30 my $this = $class->SUPER::new(@_);
31 $this->enforce_in_source_building();
32 return $this;
33 }
34
35 sub pre_building_step {
36 my $this = shift;
37 my $step = shift;
38
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"));
41
42 my $control = Dpkg::Control::Info->new();
43
44 my $source = $control->get_source();
45 my $crate = $source->{'X-Cargo-Crate'};
46 if (!$crate) {
47 $crate = $source->{Source};
48 $crate =~ s/^rust-//;
49 }
50 $this->{crate} = $crate;
51 my $changelog = Dpkg::Changelog::Debian->new(range => { count => 1 });
52 $changelog->load($this->get_sourcepath("debian/changelog"));
53 $this->{version} = Dpkg::Version->new(@{$changelog}[0]->get_version())->version();
54
55 my @packages = $control->get_packages();
56 $this->{libpkg} = 0;
57 $this->{binpkg} = 0;
58 foreach my $package (@packages) {
59 if ($package->{Package} =~ /^librust-.*-dev$/ && $package->{Architecture} eq 'all') {
60 $this->{libpkg} = $package->{Package};
61 } elsif ($package->{Architecture} ne 'all') {
62 $this->{binpkg} = $package->{Package};
63 }
64 }
65 if (!$this->{libpkg} && !$this->{binpkg}) {
66 error("Could not find any Cargo lib or bin packages to build.");
67 }
68
69 my $parallel = $this->get_parallel();
70 $this->{j} = $parallel > 0 ? ["-j$parallel"] : [];
71
72 $this->SUPER::pre_building_step($step);
73 }
74
75 sub get_sources {
76 my $this=shift;
77 opendir(my $dirhandle, $this->get_sourcedir());
78 my @sources = grep { $_ ne '.' && $_ ne '..' && $_ ne '.git' && $_ ne 'debian' } readdir($dirhandle);
79 closedir($dirhandle);
80 @sources
81 }
82
83 sub configure {
84 my $this=shift;
85 }
86
87 sub install {
88 my $this=shift;
89 my $crate = $this->{crate} . '-' . $this->{version};
90 if ($this->{libpkg}) {
91 my $target = $this->get_sourcepath("debian/" . $this->{libpkg} . "/usr/share/cargo/registry/$crate");
92 my @sources = $this->get_sources();
93 doit("mkdir", "-p", $target);
94 doit("cp", "-at", $target, @sources);
95 doit("cp", $this->get_sourcepath("debian/cargo-checksum.json"), "$target/.cargo-checksum.json");
96 }
97 if ($this->{binpkg}) {
98 my $registry = $this->{cargo_registry};
99 doit("mkdir", "-p", $this->{cargo_home}, $registry);
100 opendir(my $dirhandle, '/usr/share/cargo/registry');
101 my @crates = map { "/usr/share/cargo/registry/$_" } grep { $_ ne '.' && $_ ne '..' } readdir($dirhandle);
102 closedir($dirhandle);
103 if (@crates) {
104 doit("ln", "-st", "$registry", @crates);
105 }
106 # Handle the case of building the package with the same version of the
107 # package installed.
108 if (-l "$registry/$crate") {
109 unlink("$registry/$crate");
110 }
111 mkdir("$registry/$crate");
112 my @sources = $this->get_sources();
113 doit("cp", "-at", "$registry/$crate", @sources);
114 doit("cp", $this->get_sourcepath("debian/cargo-checksum.json"), "$registry/$crate/.cargo-checksum.json");
115
116 open(CONFIG, ">" . $this->{cargo_home} . "/config");
117 print(CONFIG qq{
118 [source.crates-io]
119 replace-with = "dh-cargo-registry"
120
121 [source.dh-cargo-registry]
122 directory = "$registry"
123 });
124 close(CONFIG);
125 $ENV{'CARGO_HOME'} = $this->{cargo_home};
126
127 my $target = $this->get_sourcepath("debian/" . $this->{binpkg} . "/usr");
128 doit("cargo", "install", $this->{crate}, "--vers", $this->{version}, "--root", $target, @{$this->{j}});
129 doit("rm", "$target/.crates.toml");
130 }
131 }
132
133 sub clean {
134 my $this=shift;
135 doit("rm", "-rf", $this->{cargo_home}, $this->{cargo_registry});
136 }
137
138 1