summaryrefslogtreecommitdiff
path: root/tools/apbuild/Apbuild/Utils.pm
blob: a5089299b59b2a60301dd05b2cb3210463af5b5f (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package Apbuild::Utils;

use strict;
use warnings;
use Exporter;
use base qw(Exporter);
use IO::Handle;
use IPC::Open2;
use POSIX;
use Cwd qw(abs_path);


our @EXPORT = qw(debug error checkCommand empty homeDir searchLib searchStaticLib soname run parseDataFile writeDataFile);
our $debugOpened = 0;


##
# debug(message)
#
# If the environment variable $APBUILD_DEBUG is set to 1,
# then print a debugging message to /dev/tty (not stdout or stderr).
sub debug {
	return if (empty($ENV{APBUILD_DEBUG}) || !$ENV{APBUILD_DEBUG});

	if (!$debugOpened) {
		if (open DEBUG, '>/dev/tty') {
			$debugOpened = 1;
		} else {
			return;
		}
	}

	my @args = split /\n/, "@_";
	foreach (@args) {
		$_ = '# ' . $_;
		$_ .= "\n";
	}

	print DEBUG "\033[1;33m";
	print DEBUG join '', @args;
	print DEBUG "\033[0m";
	DEBUG->flush;
}


##
# error(message)
#
# Print an error message to stderr. It will be displayed in red.
sub error {
	print STDERR "\033[1;31m";
	print STDERR $_[0];
	print STDERR "\033[0m";
	STDERR->flush;
}


##
# checkCommand(file)
# file: an command's filename.
# Returns: the full path to $file, or undef if $file is not a valid command.
#
# Checks whether $file is an executable which is in $PATH or the working directory.
#
# Example:
# checkCommand('gcc');  # Returns "/usr/bin/gcc"
sub checkCommand {
	my ($file, $file2) = split / /, $_[0];
	$file = $file2 if ($file =~ /ccache/);

	return abs_path($file) if (-x $file);
	foreach my $dir (split /:+/, $ENV{PATH}) {
		if (-x "$dir/$file") {
			return "$dir/$file";
		}
	}
	return undef;
}

##
# empty(str)
#
# Checks whether $str is undefined or empty.
sub empty {
	return !defined($_[0]) || $_[0] eq '';
}

##
# homeDir()
#
# Returns the user's home folder.
sub homeDir {
	if (!$ENV{HOME}) {
		my $user = getpwuid(POSIX::getuid());
		$ENV{HOME} = (getpwnam($user))[7];
	}
	return $ENV{HOME};
}

##
# searchLib(basename, [extra_paths])
# basename: the base name of the library.
# extra_paths: a reference to an array, which contains extra folders in which to look for the library.
# Returns: the absolute path to the library, or undef if not found.
#
# Get the absolute path of a (static or shared) library.
#
# Example:
# searchLib("libfoo.so.1"); # Returns "/usr/lib/libfoo.so.1"
sub searchLib {
	my ($basename, $extra_paths) = @_;

	if ($extra_paths) {
		foreach my $path (reverse(@{$extra_paths})) {
			return "$path/$basename" if (-f "$path/$basename");
		}
	}
	foreach my $path ('/usr/local/lib', '/lib', '/usr/lib') {
		return "$path/$basename" if (-f "$path/$basename");
	}
	return undef;
}

##
# soname(lib)
# lib: a filename to a shared library.
# Returns: the soname.
#
# Get the soname of the specified shared library by reading
# the SONAME section of the shared library file.
sub soname {
	my ($lib) = @_;
	my ($r, $w);

	if (open2($r, $w, 'objdump', '-p', $lib)) {
		close $w;
		my @lines = <$r>;
		close $r;

		my ($soname) = grep {/SONAME/} @lines;
		$soname =~ s/.*?SONAME[ \t]+//;
		$soname =~ s/\n//gs;
		return $soname;

	} else {
		my ($soname) = $lib =~ /.*\/lib(.+)\.so/;
		return $soname;
	}
}

##
# run(args...)
# Returns: the command's exit code.
#
# Run a command with system().
sub run {
	# split the first item in @_ into "words".  The `printf ...`
	# takes care of respecting ' and " quotes so we don't split a
	# quoted string that contains whitespace.  If $cmd itself
	# contains \n, this will still go wrong.
	my $cmd    = shift @_;
	my @words  = `printf '%s\n' $cmd`;
	chomp @words;
	my $status = system(@words, @_);
	return 127 if ($status == -1);
	return $status / 256 if ($status != 0);
	return 0;
}

sub parseDataFile {
	my ($file, $r_hash) = @_;

	%{$r_hash} = ();
	return if (!open FILE, "< $file");
	foreach (<FILE>) {
		next if (/^#/);
		s/[\r\n]//g;
		next if (length($_) == 0);

		my ($key, $value) = split /	/, $_, 2;
		$r_hash->{$key} = $value;
	}
	close FILE;
}

sub writeDataFile {
	my ($file, $r_hash) = @_;
	return if (!open FILE, "> $file");
	foreach my $key (sort(keys %{$r_hash})) {
		print FILE "$key	$r_hash->{$key}\n";
	}
	close FILE;
}

1;