about summary refs log tree commit diff
path: root/scripts/resolve-system-dependencies.pl.in
blob: 412f07274c12e69ab557e9662d0f34ae3d89807b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#! @perl@ -w @perlFlags@

use utf8;
use strict;
use warnings;
use Cwd qw(realpath);
use File::Basename qw(dirname);
use File::Path qw(make_path);
use File::Spec::Functions qw(catfile);
use List::Util qw(reduce);
use IPC::Open3;
use Nix::Config;
use Nix::Store qw(derivationFromPath);
use POSIX qw(uname);
use Storable qw(lock_retrieve lock_store);

my ($sysname, undef, $version, undef, $machine) = uname;
$sysname =~ /Darwin/ or die "This tool is only meant to be used on Darwin systems.";

my $cache = "$Nix::Config::stateDir/dependency-maps/$machine-$sysname-$version.map";

make_path dirname($cache);

our $DEPS;
eval {
  $DEPS = lock_retrieve($cache);
} or do {
  lock_store {}, $cache;
  $DEPS = {};
};

sub mkset(@) {
  my %set;
  @set{@_} = ();
  \%set
}

sub union($$) {
  my ($set1, $set2) = @_;
  my $new = {};
  foreach my $key (keys %$set1) {
    $new->{$key} = $set1->{$key};
  }
  foreach my $key (keys %$set2) {
    $new->{$key} = $set2->{$key};
  }
  $new
}

sub cache_filepath($) {
  my $fp = shift;
  $fp =~ s/-/--/g;
  $fp =~ s/\//-/g;
  $fp =~ s/^-//g;
  catfile $cache, $fp
}

sub resolve_tree {
  sub resolve_tree_inner {
    my ($lib, $TREE) = @_;
    return if (defined $TREE->{$lib});
    $TREE->{$lib} = mkset(@{cache_get($lib)});
    foreach my $dep (keys %{$TREE->{$lib}}) {
      resolve_tree_inner($dep, $TREE);
    }
    values %$TREE
  }

  reduce { union($a, $b) } {}, resolve_tree_inner(@_)
}

sub cache_get {
  my $key = shift;
  if (defined $DEPS->{$key}) {
    $DEPS->{$key}
  } else {
    cache_insert($key);
    cache_get($key)
  }
}

sub cache_insert($) {
  my $key = shift;
  print STDERR "Finding dependencies for $key...\n";
  my @deps = find_deps($key);
  $DEPS->{$key} = \@deps;
}

sub find_deps($) {
  my $lib = shift;
  my($chld_in, $chld_out, $chld_err);
  my $pid = open3($chld_in, $chld_out, $chld_err, "@otool@", "-L", "-arch", "x86_64", $lib);
  waitpid($pid, 0);
  my $line = readline $chld_out;
  if($? == 0 and $line !~ /not an object file/) {
    my @libs;
    while(<$chld_out>) {
      my $dep = (split /\s+/)[1];
      push @libs, $dep unless $dep eq $lib or $dep =~ /\@rpath/;
    }
    @libs
  } elsif (-l $lib) {
    (realpath($lib))
  } else {
    ()
  }
}

if (defined $ARGV[0]) {
  my $deps = derivationFromPath($ARGV[0])->{"env"}->{"__impureHostDeps"};
  if (defined $deps) {
    my @files = split(/\s+/, $deps);
    my $depcache = {};
    my $depset = reduce { union($a, $b) } (map { resolve_tree($_, $depcache) } @files);
    print "extra-chroot-dirs\n";
    print join("\n", keys %$depset);
    print "\n\n";
  }
  lock_store($DEPS, $cache);
} else {
  print STDERR "Usage: $0 path/to/derivation.drv\n";
  exit 1
}