Andrey Ponomarenko | ab28210 | 2012-03-11 11:57:02 +0400 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | ########################################################################### |
| 3 | # Makefile for ABI Compliance Checker |
| 4 | # Install/remove the tool for GNU/Linux and FreeBSD |
| 5 | # |
Andrey Ponomarenko | 8504379 | 2012-05-14 16:48:07 +0400 | [diff] [blame^] | 6 | # 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 Ponomarenko | ab28210 | 2012-03-11 11:57:02 +0400 | [diff] [blame] | 10 | # |
| 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 | ########################################################################### |
| 26 | use Getopt::Long; |
| 27 | Getopt::Long::Configure ("posix_default", "no_ignore_case"); |
| 28 | use File::Path qw(mkpath rmtree); |
| 29 | use File::Copy qw(copy); |
| 30 | use File::Basename qw(dirname); |
| 31 | use Cwd qw(abs_path); |
| 32 | use File::Find; |
| 33 | use Config; |
| 34 | use strict; |
| 35 | |
| 36 | my $TOOL_SNAME = "abi-compliance-checker"; |
| 37 | my $ARCHIVE_DIR = abs_path(dirname($0)); |
| 38 | |
| 39 | my $HELP_MSG = " |
| 40 | NAME: |
| 41 | Makefile for ABI Compliance Checker |
| 42 | |
| 43 | DESCRIPTION: |
| 44 | Install $TOOL_SNAME command and private modules. |
| 45 | |
| 46 | USAGE: |
| 47 | sudo perl $0 -install -prefix=/usr |
| 48 | sudo perl $0 -update -prefix=/usr |
| 49 | sudo perl $0 -remove -prefix=/usr |
| 50 | |
| 51 | OPTIONS: |
| 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 | |
| 67 | EXTRA 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 | |
| 75 | if(not @ARGV) { |
| 76 | print $HELP_MSG; |
| 77 | exit(0); |
| 78 | } |
| 79 | |
| 80 | my ($PREFIX, $DESTDIR, $Help, $Install, $Update, $Remove); |
| 81 | |
| 82 | GetOptions( |
| 83 | "h|help!" => \$Help, |
| 84 | "prefix=s" => \$PREFIX, |
| 85 | "destdir=s" => \$DESTDIR, |
| 86 | "install!" => \$Install, |
| 87 | "update!" => \$Update, |
| 88 | "remove!" => \$Remove |
| 89 | ) or exit(1); |
| 90 | |
| 91 | sub scenario() |
| 92 | { |
| 93 | if($Help) { |
| 94 | print $HELP_MSG; |
| 95 | exit(0); |
| 96 | } |
| 97 | if($Config{"osname"}!~/linux|freebsd|openbsd|netbsd/) |
| 98 | { # MS Windows, Mac OS X |
| 99 | print STDERR "The tool is ready-to-use without the need to install.\n"; |
| 100 | print STDERR "This Makefile is for GNU/Linux and BSD systems.\n"; |
| 101 | exit(1); |
| 102 | } |
| 103 | if(not $Install and not $Update and not $Remove) { |
| 104 | print STDERR "ERROR: command is not selected (-install, -update or -remove)\n"; |
| 105 | exit(1); |
| 106 | } |
| 107 | if($PREFIX ne "/") { |
| 108 | $PREFIX=~s/[\/]+\Z//g; |
| 109 | } |
| 110 | if(not $PREFIX) |
| 111 | { # default prefix |
| 112 | $PREFIX = "/usr/local"; |
| 113 | } |
| 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 | } |
| 124 | if($DESTDIR!~/\A\//) { |
| 125 | print STDERR "ERROR: destdir is not absolute path\n"; |
| 126 | exit(1); |
| 127 | } |
| 128 | if(not -d $DESTDIR) { |
| 129 | print STDERR "ERROR: you should create destdir directory first\n"; |
| 130 | exit(1); |
| 131 | } |
| 132 | $PREFIX = $DESTDIR.$PREFIX; |
| 133 | if(not -d $PREFIX) { |
| 134 | print STDERR "ERROR: you should create installation directory first (destdir + prefix):\n mkdir -p $PREFIX\n"; |
| 135 | exit(1); |
| 136 | } |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | if($PREFIX!~/\A\//) { |
| 141 | print STDERR "ERROR: prefix is not absolute path\n"; |
| 142 | exit(1); |
| 143 | } |
| 144 | if(not -d $PREFIX) { |
| 145 | print STDERR "ERROR: you should create prefix directory first\n"; |
| 146 | exit(1); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | print "INSTALL PREFIX: $PREFIX\n"; |
| 151 | |
| 152 | # paths |
| 153 | my $EXE_PATH = "$PREFIX/bin"; |
| 154 | my $MODULES_PATH = "$PREFIX/share/$TOOL_SNAME"; |
| 155 | my $REL_PATH = "../share/$TOOL_SNAME"; |
| 156 | |
| 157 | if(not -w $PREFIX) { |
| 158 | print STDERR "ERROR: you should be root\n"; |
| 159 | exit(1); |
| 160 | } |
| 161 | if($Remove or $Update) |
| 162 | { |
| 163 | # remove executable |
| 164 | print "-- Removing $EXE_PATH/$TOOL_SNAME\n"; |
| 165 | unlink($EXE_PATH."/".$TOOL_SNAME); |
| 166 | |
| 167 | # remove modules |
| 168 | print "-- Removing $MODULES_PATH\n"; |
| 169 | rmtree($MODULES_PATH); |
| 170 | } |
| 171 | if($Install or $Update) |
| 172 | { |
| 173 | if(-e $EXE_PATH."/".$TOOL_SNAME or -e $MODULES_PATH) |
| 174 | { # check installed |
| 175 | if(not $Remove) { |
| 176 | print STDERR "ERROR: you should remove old version first (`sudo perl $0 -remove --prefix=$PREFIX`)\n"; |
| 177 | exit(1); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | # configure |
| 182 | my $Content = readFile($ARCHIVE_DIR."/".$TOOL_SNAME.".pl"); |
| 183 | if($DESTDIR) { # relative path |
| 184 | $Content=~s/ACC_MODULES_INSTALL_PATH/$REL_PATH/; |
| 185 | } |
| 186 | else { # absolute path |
| 187 | $Content=~s/ACC_MODULES_INSTALL_PATH/$MODULES_PATH/; |
| 188 | } |
| 189 | |
| 190 | # copy executable |
| 191 | print "-- Installing $EXE_PATH/$TOOL_SNAME\n"; |
| 192 | mkpath($EXE_PATH); |
| 193 | writeFile($EXE_PATH."/".$TOOL_SNAME, $Content); |
Andrey Ponomarenko | 989a50b | 2012-04-03 12:24:22 +0400 | [diff] [blame] | 194 | chmod(0775, $EXE_PATH."/".$TOOL_SNAME); |
Andrey Ponomarenko | ab28210 | 2012-03-11 11:57:02 +0400 | [diff] [blame] | 195 | |
| 196 | # copy modules |
| 197 | if(-d $ARCHIVE_DIR."/modules") |
| 198 | { |
| 199 | print "-- Installing $MODULES_PATH\n"; |
| 200 | mkpath($MODULES_PATH); |
| 201 | copyDir($ARCHIVE_DIR."/modules", $MODULES_PATH); |
| 202 | } |
| 203 | |
| 204 | # check PATH |
| 205 | if($ENV{"PATH"}!~/(\A|:)\Q$EXE_PATH\E(\Z|:)/) { |
| 206 | print "WARNING: your PATH variable doesn't include \'$EXE_PATH\'\n"; |
| 207 | } |
| 208 | } |
| 209 | exit(0); |
| 210 | } |
| 211 | |
| 212 | sub copyDir($$) |
| 213 | { |
| 214 | my ($From, $To) = @_; |
| 215 | my %Files; |
| 216 | find(\&wanted, $From); |
| 217 | sub wanted { |
| 218 | $Files{$File::Find::dir."/$_"} = 1 if($_ ne "."); |
| 219 | } |
| 220 | foreach my $Path (sort keys(%Files)) |
| 221 | { |
| 222 | if($Config{"osname"}!~/win/ and $Path=~/Targets\/(windows|symbian)/) { |
| 223 | next; |
| 224 | } |
| 225 | my $Inst = $Path; |
| 226 | $Inst=~s/\A\Q$ARCHIVE_DIR\E/$To/; |
| 227 | if(-d $Path) |
| 228 | { # directories |
| 229 | mkpath($Inst); |
| 230 | } |
| 231 | else |
| 232 | { # files |
| 233 | mkpath(dirname($Inst)); |
| 234 | copy($Path, $Inst); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | sub readFile($) |
| 240 | { |
| 241 | my $Path = $_[0]; |
| 242 | return "" if(not $Path or not -f $Path); |
| 243 | open(FILE, $Path) || die ("can't open file \'$Path\': $!\n"); |
| 244 | local $/ = undef; |
| 245 | my $Content = <FILE>; |
| 246 | close(FILE); |
| 247 | return $Content; |
| 248 | } |
| 249 | |
| 250 | sub writeFile($$) |
| 251 | { |
| 252 | my ($Path, $Content) = @_; |
| 253 | return if(not $Path); |
| 254 | open(FILE, ">".$Path) || die ("can't open file \'$Path\': $!\n"); |
| 255 | print FILE $Content; |
| 256 | close(FILE); |
| 257 | } |
| 258 | |
| 259 | scenario(); |