blob: e07ad6cffa2f17aeac17c2d6ccf032ecab0a9c50 [file] [log] [blame]
Andrey Ponomarenkoab282102012-03-11 11:57:02 +04001#!/usr/bin/perl
2###########################################################################
3# Makefile for ABI Compliance Checker
Andrey Ponomarenko07aea072012-11-12 16:15:07 +04004# Install/remove the tool for GNU/Linux, FreeBSD and Mac OS X
Andrey Ponomarenkoab282102012-03-11 11:57:02 +04005#
Andrey Ponomarenko85043792012-05-14 16:48:07 +04006# Copyright (C) 2009-2010 The Linux Foundation
7# Copyright (C) 2009-2011 Institute for System Programming, RAS
8# Copyright (C) 2011-2012 Nokia Corporation and/or its subsidiary(-ies)
9# Copyright (C) 2011-2012 ROSA Laboratory
Andrey Ponomarenkoab282102012-03-11 11:57:02 +040010#
11# Written by Andrey Ponomarenko
12#
13# This program is free software: you can redistribute it and/or modify
14# it under the terms of the GNU General Public License or the GNU Lesser
15# General Public License as published by the Free Software Foundation.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# and the GNU Lesser General Public License along with this program.
24# If not, see <http://www.gnu.org/licenses/>.
25###########################################################################
26use Getopt::Long;
27Getopt::Long::Configure ("posix_default", "no_ignore_case");
28use File::Path qw(mkpath rmtree);
29use File::Copy qw(copy);
30use File::Basename qw(dirname);
31use Cwd qw(abs_path);
32use File::Find;
33use Config;
34use strict;
35
36my $TOOL_SNAME = "abi-compliance-checker";
37my $ARCHIVE_DIR = abs_path(dirname($0));
38
39my $HELP_MSG = "
40NAME:
41 Makefile for ABI Compliance Checker
42
43DESCRIPTION:
44 Install $TOOL_SNAME command and private modules.
45
46USAGE:
47 sudo perl $0 -install -prefix=/usr
48 sudo perl $0 -update -prefix=/usr
49 sudo perl $0 -remove -prefix=/usr
50
51OPTIONS:
52 -h|-help
53 Print this help.
54
55 --prefix=PREFIX
56 Install files in PREFIX [/usr/local].
57
58 -install
59 Command to install the tool.
60
61 -update
62 Command to update existing installation.
63
64 -remove
65 Command to remove the tool.
66
67EXTRA OPTIONS:
68 --destdir=DESTDIR
69 This option is for maintainers to build
70 RPM or DEB packages inside the build root.
71 The environment variable DESTDIR is also
72 supported.
73\n";
74
Andrey Ponomarenko07aea072012-11-12 16:15:07 +040075if(not @ARGV)
76{
Andrey Ponomarenkoab282102012-03-11 11:57:02 +040077 print $HELP_MSG;
78 exit(0);
79}
80
81my ($PREFIX, $DESTDIR, $Help, $Install, $Update, $Remove);
82
83GetOptions(
84 "h|help!" => \$Help,
85 "prefix=s" => \$PREFIX,
86 "destdir=s" => \$DESTDIR,
87 "install!" => \$Install,
88 "update!" => \$Update,
89 "remove!" => \$Remove
90) or exit(1);
91
92sub scenario()
93{
Andrey Ponomarenko07aea072012-11-12 16:15:07 +040094 if($Help)
95 {
Andrey Ponomarenkoab282102012-03-11 11:57:02 +040096 print $HELP_MSG;
97 exit(0);
98 }
Andrey Ponomarenko07aea072012-11-12 16:15:07 +040099 if($Config{"osname"}!~/linux|freebsd|openbsd|netbsd|macos|darwin|rhapsody/)
100 {
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400101 print STDERR "The tool is ready-to-use without the need to install.\n";
Andrey Ponomarenko07aea072012-11-12 16:15:07 +0400102 print STDERR "This Makefile is for GNU/Linux, FreeBSD and Mac OS X.\n";
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400103 exit(1);
104 }
Andrey Ponomarenko07aea072012-11-12 16:15:07 +0400105 if(not $Install and not $Update and not $Remove)
106 {
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400107 print STDERR "ERROR: command is not selected (-install, -update or -remove)\n";
108 exit(1);
109 }
110 if($PREFIX ne "/") {
111 $PREFIX=~s/[\/]+\Z//g;
112 }
113 if(not $PREFIX)
114 { # default prefix
115 $PREFIX = "/usr/local";
116 }
117 if(my $Var = $ENV{"DESTDIR"})
118 {
119 print "Using DESTDIR environment variable\n";
120 $DESTDIR = $Var;
121 }
122 if($DESTDIR)
123 {
124 if($DESTDIR ne "/") {
125 $DESTDIR=~s/[\/]+\Z//g;
126 }
Andrey Ponomarenko07aea072012-11-12 16:15:07 +0400127 if($DESTDIR!~/\A\//)
128 {
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400129 print STDERR "ERROR: destdir is not absolute path\n";
130 exit(1);
131 }
Andrey Ponomarenko07aea072012-11-12 16:15:07 +0400132 if(not -d $DESTDIR)
133 {
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400134 print STDERR "ERROR: you should create destdir directory first\n";
135 exit(1);
136 }
137 $PREFIX = $DESTDIR.$PREFIX;
Andrey Ponomarenko07aea072012-11-12 16:15:07 +0400138 if(not -d $PREFIX)
139 {
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400140 print STDERR "ERROR: you should create installation directory first (destdir + prefix):\n mkdir -p $PREFIX\n";
141 exit(1);
142 }
143 }
144 else
145 {
Andrey Ponomarenko07aea072012-11-12 16:15:07 +0400146 if($PREFIX!~/\A\//)
147 {
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400148 print STDERR "ERROR: prefix is not absolute path\n";
149 exit(1);
150 }
Andrey Ponomarenko07aea072012-11-12 16:15:07 +0400151 if(not -d $PREFIX)
152 {
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400153 print STDERR "ERROR: you should create prefix directory first\n";
154 exit(1);
155 }
156 }
157
158 print "INSTALL PREFIX: $PREFIX\n";
159
160 # paths
161 my $EXE_PATH = "$PREFIX/bin";
162 my $MODULES_PATH = "$PREFIX/share/$TOOL_SNAME";
163 my $REL_PATH = "../share/$TOOL_SNAME";
164
Andrey Ponomarenko07aea072012-11-12 16:15:07 +0400165 if(not -w $PREFIX)
166 {
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400167 print STDERR "ERROR: you should be root\n";
168 exit(1);
169 }
170 if($Remove or $Update)
171 {
172 # remove executable
173 print "-- Removing $EXE_PATH/$TOOL_SNAME\n";
174 unlink($EXE_PATH."/".$TOOL_SNAME);
175
176 # remove modules
177 print "-- Removing $MODULES_PATH\n";
178 rmtree($MODULES_PATH);
179 }
180 if($Install or $Update)
181 {
182 if(-e $EXE_PATH."/".$TOOL_SNAME or -e $MODULES_PATH)
183 { # check installed
184 if(not $Remove) {
185 print STDERR "ERROR: you should remove old version first (`sudo perl $0 -remove --prefix=$PREFIX`)\n";
186 exit(1);
187 }
188 }
189
190 # configure
191 my $Content = readFile($ARCHIVE_DIR."/".$TOOL_SNAME.".pl");
192 if($DESTDIR) { # relative path
193 $Content=~s/ACC_MODULES_INSTALL_PATH/$REL_PATH/;
194 }
195 else { # absolute path
196 $Content=~s/ACC_MODULES_INSTALL_PATH/$MODULES_PATH/;
197 }
198
199 # copy executable
200 print "-- Installing $EXE_PATH/$TOOL_SNAME\n";
201 mkpath($EXE_PATH);
202 writeFile($EXE_PATH."/".$TOOL_SNAME, $Content);
Andrey Ponomarenko989a50b2012-04-03 12:24:22 +0400203 chmod(0775, $EXE_PATH."/".$TOOL_SNAME);
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400204
205 # copy modules
206 if(-d $ARCHIVE_DIR."/modules")
207 {
208 print "-- Installing $MODULES_PATH\n";
209 mkpath($MODULES_PATH);
210 copyDir($ARCHIVE_DIR."/modules", $MODULES_PATH);
211 }
212
213 # check PATH
214 if($ENV{"PATH"}!~/(\A|:)\Q$EXE_PATH\E(\Z|:)/) {
215 print "WARNING: your PATH variable doesn't include \'$EXE_PATH\'\n";
216 }
217 }
218 exit(0);
219}
220
221sub copyDir($$)
222{
223 my ($From, $To) = @_;
224 my %Files;
225 find(\&wanted, $From);
226 sub wanted {
227 $Files{$File::Find::dir."/$_"} = 1 if($_ ne ".");
228 }
229 foreach my $Path (sort keys(%Files))
230 {
231 if($Config{"osname"}!~/win/ and $Path=~/Targets\/(windows|symbian)/) {
Andrey Ponomarenko07aea072012-11-12 16:15:07 +0400232 next;
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400233 }
234 my $Inst = $Path;
235 $Inst=~s/\A\Q$ARCHIVE_DIR\E/$To/;
236 if(-d $Path)
237 { # directories
238 mkpath($Inst);
239 }
240 else
241 { # files
242 mkpath(dirname($Inst));
243 copy($Path, $Inst);
244 }
245 }
246}
247
248sub readFile($)
249{
250 my $Path = $_[0];
251 return "" if(not $Path or not -f $Path);
252 open(FILE, $Path) || die ("can't open file \'$Path\': $!\n");
253 local $/ = undef;
254 my $Content = <FILE>;
255 close(FILE);
256 return $Content;
257}
258
259sub writeFile($$)
260{
261 my ($Path, $Content) = @_;
262 return if(not $Path);
263 open(FILE, ">".$Path) || die ("can't open file \'$Path\': $!\n");
264 print FILE $Content;
265 close(FILE);
266}
267
268scenario();