blob: d6cd4fecd14a1bfd47719fe9969022aafc0e0030 [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)
Andrey Ponomarenko8f4b9812013-02-07 19:11:42 +04009# Copyright (C) 2011-2013 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);
Andrey Ponomarenko74b33ee2012-12-14 15:24:09 +040029use File::Spec qw(catfile file_name_is_absolute);
Andrey Ponomarenkoab282102012-03-11 11:57:02 +040030use File::Copy qw(copy);
31use File::Basename qw(dirname);
32use Cwd qw(abs_path);
33use File::Find;
34use Config;
35use strict;
36
37my $TOOL_SNAME = "abi-compliance-checker";
38my $ARCHIVE_DIR = abs_path(dirname($0));
39
40my $HELP_MSG = "
41NAME:
42 Makefile for ABI Compliance Checker
43
44DESCRIPTION:
45 Install $TOOL_SNAME command and private modules.
46
47USAGE:
48 sudo perl $0 -install -prefix=/usr
49 sudo perl $0 -update -prefix=/usr
50 sudo perl $0 -remove -prefix=/usr
51
52OPTIONS:
53 -h|-help
54 Print this help.
55
56 --prefix=PREFIX
57 Install files in PREFIX [/usr/local].
58
59 -install
60 Command to install the tool.
61
62 -update
63 Command to update existing installation.
64
65 -remove
66 Command to remove the tool.
67
68EXTRA OPTIONS:
69 --destdir=DESTDIR
70 This option is for maintainers to build
71 RPM or DEB packages inside the build root.
72 The environment variable DESTDIR is also
73 supported.
74\n";
75
Andrey Ponomarenko07aea072012-11-12 16:15:07 +040076if(not @ARGV)
77{
Andrey Ponomarenkoab282102012-03-11 11:57:02 +040078 print $HELP_MSG;
79 exit(0);
80}
81
82my ($PREFIX, $DESTDIR, $Help, $Install, $Update, $Remove);
83
84GetOptions(
85 "h|help!" => \$Help,
86 "prefix=s" => \$PREFIX,
87 "destdir=s" => \$DESTDIR,
88 "install!" => \$Install,
89 "update!" => \$Update,
90 "remove!" => \$Remove
91) or exit(1);
92
93sub scenario()
94{
Andrey Ponomarenko07aea072012-11-12 16:15:07 +040095 if($Help)
96 {
Andrey Ponomarenkoab282102012-03-11 11:57:02 +040097 print $HELP_MSG;
98 exit(0);
99 }
Andrey Ponomarenko07aea072012-11-12 16:15:07 +0400100 if(not $Install and not $Update and not $Remove)
101 {
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400102 print STDERR "ERROR: command is not selected (-install, -update or -remove)\n";
103 exit(1);
104 }
105 if($PREFIX ne "/") {
106 $PREFIX=~s/[\/]+\Z//g;
107 }
108 if(not $PREFIX)
109 { # default prefix
Andrey Ponomarenko74b33ee2012-12-14 15:24:09 +0400110 if($Config{"osname"}!~/win/i) {
111 $PREFIX = "/usr/local";
112 }
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400113 }
114 if(my $Var = $ENV{"DESTDIR"})
115 {
116 print "Using DESTDIR environment variable\n";
117 $DESTDIR = $Var;
118 }
119 if($DESTDIR)
120 {
121 if($DESTDIR ne "/") {
122 $DESTDIR=~s/[\/]+\Z//g;
123 }
Andrey Ponomarenko74b33ee2012-12-14 15:24:09 +0400124 if(not isAbs($DESTDIR))
Andrey Ponomarenko07aea072012-11-12 16:15:07 +0400125 {
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400126 print STDERR "ERROR: destdir is not absolute path\n";
127 exit(1);
128 }
Andrey Ponomarenko07aea072012-11-12 16:15:07 +0400129 if(not -d $DESTDIR)
130 {
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400131 print STDERR "ERROR: you should create destdir directory first\n";
132 exit(1);
133 }
134 $PREFIX = $DESTDIR.$PREFIX;
Andrey Ponomarenko07aea072012-11-12 16:15:07 +0400135 if(not -d $PREFIX)
136 {
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400137 print STDERR "ERROR: you should create installation directory first (destdir + prefix):\n mkdir -p $PREFIX\n";
138 exit(1);
139 }
140 }
141 else
142 {
Andrey Ponomarenko74b33ee2012-12-14 15:24:09 +0400143 if(not isAbs($PREFIX))
Andrey Ponomarenko07aea072012-11-12 16:15:07 +0400144 {
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400145 print STDERR "ERROR: prefix is not absolute path\n";
146 exit(1);
147 }
Andrey Ponomarenko07aea072012-11-12 16:15:07 +0400148 if(not -d $PREFIX)
149 {
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400150 print STDERR "ERROR: you should create prefix directory first\n";
151 exit(1);
152 }
153 }
154
155 print "INSTALL PREFIX: $PREFIX\n";
156
157 # paths
Andrey Ponomarenko74b33ee2012-12-14 15:24:09 +0400158 my $EXE_PATH = catFile($PREFIX, "bin");
159 my $MODULES_PATH = catFile($PREFIX, "share", $TOOL_SNAME);
160 my $REL_PATH = catFile("..", "share", $TOOL_SNAME);
161 my $TOOL_PATH = catFile($EXE_PATH, $TOOL_SNAME);
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400162
Andrey Ponomarenko07aea072012-11-12 16:15:07 +0400163 if(not -w $PREFIX)
164 {
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400165 print STDERR "ERROR: you should be root\n";
166 exit(1);
167 }
168 if($Remove or $Update)
169 {
Andrey Ponomarenko74b33ee2012-12-14 15:24:09 +0400170 if(-e $EXE_PATH."/".$TOOL_SNAME)
171 { # remove executable
172 print "-- Removing $TOOL_PATH\n";
173 unlink($EXE_PATH."/".$TOOL_SNAME);
174 }
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400175
Andrey Ponomarenko570ece52012-11-30 16:36:44 +0400176 if(-d $MODULES_PATH)
177 { # remove modules
178 print "-- Removing $MODULES_PATH\n";
179 rmtree($MODULES_PATH);
180 }
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400181 }
182 if($Install or $Update)
183 {
184 if(-e $EXE_PATH."/".$TOOL_SNAME or -e $MODULES_PATH)
185 { # check installed
Andrey Ponomarenko27681702012-11-12 16:33:39 +0400186 if(not $Remove)
187 {
Andrey Ponomarenko74b33ee2012-12-14 15:24:09 +0400188 print STDERR "ERROR: you should remove old version first (`perl $0 -remove --prefix=$PREFIX`)\n";
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400189 exit(1);
190 }
191 }
192
193 # configure
194 my $Content = readFile($ARCHIVE_DIR."/".$TOOL_SNAME.".pl");
195 if($DESTDIR) { # relative path
Andrey Ponomarenko74b33ee2012-12-14 15:24:09 +0400196 $Content=~s/MODULES_INSTALL_PATH/$REL_PATH/;
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400197 }
198 else { # absolute path
Andrey Ponomarenko74b33ee2012-12-14 15:24:09 +0400199 $Content=~s/MODULES_INSTALL_PATH/$MODULES_PATH/;
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400200 }
201
202 # copy executable
Andrey Ponomarenko74b33ee2012-12-14 15:24:09 +0400203 print "-- Installing $TOOL_PATH\n";
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400204 mkpath($EXE_PATH);
205 writeFile($EXE_PATH."/".$TOOL_SNAME, $Content);
Andrey Ponomarenko989a50b2012-04-03 12:24:22 +0400206 chmod(0775, $EXE_PATH."/".$TOOL_SNAME);
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400207
Andrey Ponomarenko74b33ee2012-12-14 15:24:09 +0400208 if($Config{"osname"}=~/win/i) {
209 writeFile($EXE_PATH."/".$TOOL_SNAME.".cmd", "\@perl \"$TOOL_PATH\" \%*");
210 }
211
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400212 # copy modules
213 if(-d $ARCHIVE_DIR."/modules")
214 {
215 print "-- Installing $MODULES_PATH\n";
216 mkpath($MODULES_PATH);
217 copyDir($ARCHIVE_DIR."/modules", $MODULES_PATH);
218 }
219
220 # check PATH
Andrey Ponomarenko8f4b9812013-02-07 19:11:42 +0400221 my $Warn = "WARNING: your PATH variable doesn't include \'$EXE_PATH\'\n";
222
223 if($Config{"osname"}=~/win/i)
224 {
225 if($ENV{"PATH"}!~/(\A|[:;])\Q$EXE_PATH\E[\/\\]?(\Z|[:;])/i) {
226 print $Warn;
227 }
228 }
229 else
230 {
231 if($ENV{"PATH"}!~/(\A|[:;])\Q$EXE_PATH\E[\/\\]?(\Z|[:;])/) {
232 print $Warn;
233 }
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400234 }
235 }
236 exit(0);
237}
238
Andrey Ponomarenko74b33ee2012-12-14 15:24:09 +0400239sub catFile(@) {
240 return File::Spec->catfile(@_);
241}
242
243sub isAbs($) {
244 return File::Spec->file_name_is_absolute($_[0]);
245}
246
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400247sub copyDir($$)
248{
249 my ($From, $To) = @_;
250 my %Files;
251 find(\&wanted, $From);
252 sub wanted {
253 $Files{$File::Find::dir."/$_"} = 1 if($_ ne ".");
254 }
255 foreach my $Path (sort keys(%Files))
256 {
257 if($Config{"osname"}!~/win/ and $Path=~/Targets\/(windows|symbian)/) {
Andrey Ponomarenko07aea072012-11-12 16:15:07 +0400258 next;
Andrey Ponomarenkoab282102012-03-11 11:57:02 +0400259 }
260 my $Inst = $Path;
261 $Inst=~s/\A\Q$ARCHIVE_DIR\E/$To/;
262 if(-d $Path)
263 { # directories
264 mkpath($Inst);
265 }
266 else
267 { # files
268 mkpath(dirname($Inst));
269 copy($Path, $Inst);
270 }
271 }
272}
273
274sub readFile($)
275{
276 my $Path = $_[0];
277 return "" if(not $Path or not -f $Path);
278 open(FILE, $Path) || die ("can't open file \'$Path\': $!\n");
279 local $/ = undef;
280 my $Content = <FILE>;
281 close(FILE);
282 return $Content;
283}
284
285sub writeFile($$)
286{
287 my ($Path, $Content) = @_;
288 return if(not $Path);
289 open(FILE, ">".$Path) || die ("can't open file \'$Path\': $!\n");
290 print FILE $Content;
291 close(FILE);
292}
293
294scenario();