Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Some helper code to do avahi detection for distcc correctly
#!/usr/bin/env perl
# /etc/paludis/distcc-env.pl
use 5.012;
use warnings;
my $machine = $ENV{CHOST} // do { qx(gcc -dumpmachine) =~ s/\n//r };
die 'CHOST missing' unless $machine;
my %entries =
map { $_->{txt}{'org.freedesktop.Avahi.cookie'} // $_->{address} => $_ }
grep { $_->{txt}{cc_machine} eq $machine and $_->{ipver} eq 'IPv4' }
map { {parse_line($_)}; }
grep { /^=;/ }
qx(avahi-browse -cprl _distcc._tcp);
my @host_cpus = qx(lscpu -p) =~ /^\d/mg;
my $cpus = +@host_cpus;
my @hosts;
for ( values %entries ) {
$cpus += $_->{txt}{cpus},
push @hosts, "$_->{address}:$_->{port}";
}
if ( @ARGV and $ARGV[0] eq '--prioritise-self' ) {
unshift @hosts, 'localhost';
}
else {
push @hosts, 'localhost';
}
say sprintf('DISTCC_HOSTS="%s"', join(q{ }, @hosts));
say sprintf('MAKEOPTS="-j%d -l%d"', $cpus, 1 + @host_cpus);
say 'CCACHE_PREFIX="distcc"';
sub parse_line {
my $line = shift;
my %fields;
@fields{qw(type iface ipver name service domain hostname address port txt)}
= split(q{;}, $line);
$fields{txt} = {map { split q{=} } $fields{txt} =~ m/"(.*?)=(.*?)"/g};
return %fields;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment