]>
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
};
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();
55 my @packages = $control->get_packages();
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
};
65 if (!$this->{libpkg
} && !$this->{binpkg
}) {
66 error
("Could not find any Cargo lib or bin packages to build.");
69 my $parallel = $this->get_parallel();
70 $this->{j
} = $parallel > 0 ?
["-j$parallel"] : [];
72 $this->SUPER::pre_building_step
($step);
77 opendir(my $dirhandle, $this->get_sourcedir());
78 my @sources = grep { $_ ne '.' && $_ ne '..' && $_ ne '.git' && $_ ne 'debian' } readdir($dirhandle);
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");
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);
104 doit
("ln", "-st", "$registry", @crates);
106 # Handle the case of building the package with the same version of the
108 if (-l
"$registry/$crate") {
109 unlink("$registry/$crate");
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");
116 open(CONFIG
, ">" . $this->{cargo_home
} . "/config");
119 replace-with = "dh-cargo-registry"
121 [source.dh-cargo-registry]
122 directory = "$registry"
125 $ENV{'CARGO_HOME'} = $this->{cargo_home
};
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");
135 doit
("rm", "-rf", $this->{cargo_home
}, $this->{cargo_registry
});