Andrey Ponomarenko | ab28210 | 2012-03-11 11:57:02 +0400 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | ########################################################################### |
| 3 | # Makefile for ABI Compliance Checker |
Andrey Ponomarenko | 07aea07 | 2012-11-12 16:15:07 +0400 | [diff] [blame^] | 4 | # Install/remove the tool for GNU/Linux, FreeBSD and Mac OS X |
Andrey Ponomarenko | ab28210 | 2012-03-11 11:57:02 +0400 | [diff] [blame] | 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 | |
Andrey Ponomarenko | 07aea07 | 2012-11-12 16:15:07 +0400 | [diff] [blame^] | 75 | if(not @ARGV) |
| 76 | { |
Andrey Ponomarenko | ab28210 | 2012-03-11 11:57:02 +0400 | [diff] [blame] | 77 | print $HELP_MSG; |
| 78 | exit(0); |
| 79 | } |
| 80 | |
| 81 | my ($PREFIX, $DESTDIR, $Help, $Install, $Update, $Remove); |
| 82 | |
| 83 | GetOptions( |
| 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 | |
| 92 | sub scenario() |
| 93 | { |
Andrey Ponomarenko | 07aea07 | 2012-11-12 16:15:07 +0400 | [diff] [blame^] | 94 | if($Help) |
| 95 | { |
Andrey Ponomarenko | ab28210 | 2012-03-11 11:57:02 +0400 | [diff] [blame] | 96 | print $HELP_MSG; |
| 97 | exit(0); |
| 98 | } |
Andrey Ponomarenko | 07aea07 | 2012-11-12 16:15:07 +0400 | [diff] [blame^] | 99 | if($Config{"osname"}!~/linux|freebsd|openbsd|netbsd|macos|darwin|rhapsody/) |
| 100 | { |
Andrey Ponomarenko | ab28210 | 2012-03-11 11:57:02 +0400 | [diff] [blame] | 101 | print STDERR "The tool is ready-to-use without the need to install.\n"; |
Andrey Ponomarenko | 07aea07 | 2012-11-12 16:15:07 +0400 | [diff] [blame^] | 102 | print STDERR "This Makefile is for GNU/Linux, FreeBSD and Mac OS X.\n"; |
Andrey Ponomarenko | ab28210 | 2012-03-11 11:57:02 +0400 | [diff] [blame] | 103 | exit(1); |
| 104 | } |
Andrey Ponomarenko | 07aea07 | 2012-11-12 16:15:07 +0400 | [diff] [blame^] | 105 | if(not $Install and not $Update and not $Remove) |
| 106 | { |
Andrey Ponomarenko | ab28210 | 2012-03-11 11:57:02 +0400 | [diff] [blame] | 107 | 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 Ponomarenko | 07aea07 | 2012-11-12 16:15:07 +0400 | [diff] [blame^] | 127 | if($DESTDIR!~/\A\//) |
| 128 | { |
Andrey Ponomarenko | ab28210 | 2012-03-11 11:57:02 +0400 | [diff] [blame] | 129 | print STDERR "ERROR: destdir is not absolute path\n"; |
| 130 | exit(1); |
| 131 | } |
Andrey Ponomarenko | 07aea07 | 2012-11-12 16:15:07 +0400 | [diff] [blame^] | 132 | if(not -d $DESTDIR) |
| 133 | { |
Andrey Ponomarenko | ab28210 | 2012-03-11 11:57:02 +0400 | [diff] [blame] | 134 | print STDERR "ERROR: you should create destdir directory first\n"; |
| 135 | exit(1); |
| 136 | } |
| 137 | $PREFIX = $DESTDIR.$PREFIX; |
Andrey Ponomarenko | 07aea07 | 2012-11-12 16:15:07 +0400 | [diff] [blame^] | 138 | if(not -d $PREFIX) |
| 139 | { |
Andrey Ponomarenko | ab28210 | 2012-03-11 11:57:02 +0400 | [diff] [blame] | 140 | 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 Ponomarenko | 07aea07 | 2012-11-12 16:15:07 +0400 | [diff] [blame^] | 146 | if($PREFIX!~/\A\//) |
| 147 | { |
Andrey Ponomarenko | ab28210 | 2012-03-11 11:57:02 +0400 | [diff] [blame] | 148 | print STDERR "ERROR: prefix is not absolute path\n"; |
| 149 | exit(1); |
| 150 | } |
Andrey Ponomarenko | 07aea07 | 2012-11-12 16:15:07 +0400 | [diff] [blame^] | 151 | if(not -d $PREFIX) |
| 152 | { |
Andrey Ponomarenko | ab28210 | 2012-03-11 11:57:02 +0400 | [diff] [blame] | 153 | 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 Ponomarenko | 07aea07 | 2012-11-12 16:15:07 +0400 | [diff] [blame^] | 165 | if(not -w $PREFIX) |
| 166 | { |
Andrey Ponomarenko | ab28210 | 2012-03-11 11:57:02 +0400 | [diff] [blame] | 167 | 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 Ponomarenko | 989a50b | 2012-04-03 12:24:22 +0400 | [diff] [blame] | 203 | chmod(0775, $EXE_PATH."/".$TOOL_SNAME); |
Andrey Ponomarenko | ab28210 | 2012-03-11 11:57:02 +0400 | [diff] [blame] | 204 | |
| 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 | |
| 221 | sub 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 Ponomarenko | 07aea07 | 2012-11-12 16:15:07 +0400 | [diff] [blame^] | 232 | next; |
Andrey Ponomarenko | ab28210 | 2012-03-11 11:57:02 +0400 | [diff] [blame] | 233 | } |
| 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 | |
| 248 | sub 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 | |
| 259 | sub 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 | |
| 268 | scenario(); |