common.sh revision 34d165185730eac6ddc4e7a23d192d22aa3d4dfb
1# Copyright (C) 2008 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# 15# This file is included by other shell scripts; do not execute it directly. 16# It contains common definitions. 17# 18PROGNAME=`basename $0` 19 20## Logging support 21## 22VERBOSE=yes 23VERBOSE2=no 24 25log () 26{ 27 if [ "$VERBOSE" = "yes" ] ; then 28 echo "$1" 29 fi 30} 31 32log2 () 33{ 34 if [ "$VERBOSE2" = "yes" ] ; then 35 echo "$1" 36 fi 37} 38 39## Utilities 40## 41 42# return the value of a given named variable 43# $1: variable name 44# 45var_value () 46{ 47 # find a better way to do that ? 48 local result 49 eval result="$`echo $1`" 50 echo $result 51} 52 53# convert to uppercase 54to_uppercase () 55{ 56 echo $1 | tr "[:lower:]" "[:upper:]" 57} 58 59## Normalize OS and CPU 60## 61 62CPU=`uname -m` 63case "$CPU" in 64 i?86) CPU=x86 65 ;; 66 amd64) CPU=x86_64 67 ;; 68 powerpc) CPU=ppc 69 ;; 70esac 71 72log2 "CPU=$CPU" 73 74# at this point, the supported values for CPU are: 75# x86 76# x86_64 77# ppc 78# 79# other values may be possible but haven't been tested 80# 81 82EXE="" 83OS=`uname -s` 84case "$OS" in 85 Darwin) 86 OS=darwin-$CPU 87 ;; 88 Linux) 89 # note that building 32-bit binaries on x86_64 is handled later 90 OS=linux-$CPU 91 ;; 92 FreeBSD) 93 OS=freebsd-$CPU 94 ;; 95 CYGWIN*|*_NT-*) 96 OS=windows 97 EXE=.exe 98 if [ "x$OSTYPE" = xcygwin ] ; then 99 OS=cygwin 100 HOST_CFLAGS="$CFLAGS -mno-cygwin" 101 HOST_LDFLAGS="$LDFLAGS -mno-cygwin" 102 fi 103 ;; 104esac 105 106log2 "OS=$OS" 107log2 "EXE=$EXE" 108 109# at this point, the value of OS should be one of the following: 110# linux-x86 111# linux-x86_64 112# darwin-x86 113# darwin-ppc 114# windows (MSys) 115# cygwin 116# 117# Note that cygwin is treated as a special case because it behaves very differently 118# for a few things 119# 120# other values may be possible but have not been tested 121 122# define HOST_OS as $OS without any cpu-specific suffix 123# 124case $OS in 125 linux-*) HOST_OS=linux 126 ;; 127 darwin-*) HOST_OS=darwin 128 ;; 129 freebsd-*) HOST_OS=freebsd 130 ;; 131 *) HOST_OS=$OS 132esac 133 134# define HOST_ARCH as the $CPU 135HOST_ARCH=$CPU 136 137# define HOST_TAG 138# special case: windows-x86 => windows 139compute_host_tag () 140{ 141 case $HOST_OS-$HOST_ARCH in 142 cygwin-x86|windows-x86) 143 HOST_TAG=windows 144 ;; 145 *) 146 HOST_TAG=$HOST_OS-$HOST_ARCH 147 ;; 148 esac 149} 150compute_host_tag 151 152#### Toolchain support 153#### 154 155# Various probes are going to need to run a small C program 156TMPC=/tmp/android-$$-test.c 157TMPO=/tmp/android-$$-test.o 158TMPE=/tmp/android-$$-test$EXE 159TMPL=/tmp/android-$$-test.log 160 161# cleanup temporary files 162clean_temp () 163{ 164 rm -f $TMPC $TMPO $TMPL $TMPE 165} 166 167# cleanup temp files then exit with an error 168clean_exit () 169{ 170 clean_temp 171 exit 1 172} 173 174# this function should be called to enforce the build of 32-bit binaries on 64-bit systems 175# that support it. 176FORCE_32BIT=no 177force_32bit_binaries () 178{ 179 if [ $CPU = x86_64 ] ; then 180 FORCE_32BIT=yes 181 case $OS in 182 linux-x86_64) OS=linux-x86 ;; 183 darwin-x86_64) OS=darwin-x86 ;; 184 freebsd-x86_64) OS=freebsd-x86 ;; 185 esac 186 HOST_ARCH=x86 187 CPU=x86 188 compute_host_tag 189 log "Check32Bits: Forcing generation of 32-bit binaries (--try-64 to disable)" 190 fi 191} 192 193# Cygwin is normally not supported, unless you call this function 194# 195enable_cygwin () 196{ 197 if [ $OS = cygwin ] ; then 198 CFLAGS="$CFLAGS -mno-cygwin" 199 LDFLAGS="$LDFLAGS -mno-cygwin" 200 OS=windows 201 HOST_OS=windows 202 fi 203} 204 205# this function will setup the compiler and linker and check that they work as advertized 206# note that you should call 'force_32bit_binaries' before this one if you want it to work 207# as advertized. 208# 209setup_toolchain () 210{ 211 if [ "$OS" = cygwin ] ; then 212 echo "Do not compile this program or library with Cygwin, use MSYS instead !!" 213 echo "As an experimental feature, you can try to --try-cygwin option to override this" 214 exit 2 215 fi 216 217 if [ -z "$CC" ] ; then 218 CC=gcc 219 if [ $CPU = "powerpc" ] ; then 220 CC=gcc-3.3 221 fi 222 fi 223 224 # check that we can compile a trivial C program with this compiler 225 cat > $TMPC <<EOF 226int main(void) {} 227EOF 228 229 if [ $FORCE_32BIT = yes ] ; then 230 CFLAGS="$CFLAGS -m32" 231 LDFLAGS="$LDFLAGS -m32" 232 compile 233 if [ $? != 0 ] ; then 234 # sometimes, we need to also tell the assembler to generate 32-bit binaries 235 # this is highly dependent on your GCC installation (and no, we can't set 236 # this flag all the time) 237 CFLAGS="$CFLAGS -Wa,--32" 238 compile 239 fi 240 fi 241 242 compile 243 if [ $? != 0 ] ; then 244 echo "your C compiler doesn't seem to work:" 245 cat $TMPL 246 clean_exit 247 fi 248 log "CC : compiler check ok ($CC)" 249 250 # check that we can link the trivial program into an executable 251 if [ -z "$LD" ] ; then 252 LD=$CC 253 fi 254 link 255 if [ $? != 0 ] ; then 256 OLD_LD=$LD 257 LD=gcc 258 compile 259 link 260 if [ $? != 0 ] ; then 261 LD=$OLD_LD 262 echo "your linker doesn't seem to work:" 263 cat $TMPL 264 clean_exit 265 fi 266 fi 267 log "LD : linker check ok ($LD)" 268} 269 270# try to compile the current source file in $TMPC into an object 271# stores the error log into $TMPL 272# 273compile () 274{ 275 log2 "Object : $CC -o $TMPO -c $CFLAGS $TMPC" 276 $CC -o $TMPO -c $CFLAGS $TMPC 2> $TMPL 277} 278 279# try to link the recently built file into an executable. error log in $TMPL 280# 281link() 282{ 283 log2 "Link : $LD -o $TMPE $TMPO $LDFLAGS" 284 $LD -o $TMPE $TMPO $LDFLAGS 2> $TMPL 285} 286 287# run a command 288# 289execute() 290{ 291 log2 "Running: $*" 292 $* 293} 294 295# perform a simple compile / link / run of the source file in $TMPC 296compile_exec_run() 297{ 298 log2 "RunExec : $CC -o $TMPE $CFLAGS $TMPC" 299 compile 300 if [ $? != 0 ] ; then 301 echo "Failure to compile test program" 302 cat $TMPC 303 cat $TMPL 304 clean_exit 305 fi 306 link 307 if [ $? != 0 ] ; then 308 echo "Failure to link test program" 309 cat $TMPC 310 echo "------" 311 cat $TMPL 312 clean_exit 313 fi 314 $TMPE 315} 316 317## Feature test support 318## 319 320# Each feature test allows us to check against a single target-specific feature 321# We run the feature checks in a Makefile in order to be able to do them in 322# parallel, and we also have some cached values in our output directory, just 323# in case. 324# 325# check that a given C program in $TMPC can be compiled on the host system 326# $1: variable name which will be set to "yes" or "no" depending on result 327# you can define EXTRA_CFLAGS for extra C compiler flags 328# for convenience, this variable will be unset by the function 329# 330feature_check_compile () 331{ 332 local result_cc=yes 333 local OLD_CFLAGS 334 OLD_CFLAGS="$CFLAGS" 335 CFLAGS="$CFLAGS $EXTRA_CFLAGS" 336 compile 337 if [ $? != 0 ] ; then 338 result_cc=no 339 fi 340 eval $1=$result_cc 341 EXTRA_CFLAGS= 342 CFLAGS=$OLD_CFLAGS 343} 344 345# check that a given C program $TMPC can be linked on the host system 346# $1: variable name which will be set to "yes" or "no" depending on result 347# you can define EXTRA_CFLAGS for extra C compiler flags 348# you can define EXTRA_LDFLAGS for extra linker flags 349# for convenience, these variables will be unset by the function 350# 351feature_check_link () 352{ 353 local result_cl=yes 354 local OLD_CFLAGS OLD_LDFLAGS 355 OLD_CFLAGS=$CFLAGS 356 OLD_LDFLAGS=$LDFLAGS 357 CFLAGS="$CFLAGS $EXTRA_CFLAGS" 358 LDFLAGS="$LDFLAGS $EXTRA_LDFLAGS" 359 compile 360 if [ $? != 0 ] ; then 361 result_cl=no 362 else 363 link 364 if [ $? != 0 ] ; then 365 result_cl=no 366 fi 367 fi 368 CFLAGS=$OLD_CFLAGS 369 LDFLAGS=$OLD_LDFLAGS 370 eval $1=$result_cl 371} 372 373# check that a given C header file exists on the host system 374# $1: variable name which will be set to "yes" or "no" depending on result 375# $2: header name 376# 377# you can define EXTRA_CFLAGS for extra C compiler flags 378# for convenience, this variable will be unset by the function. 379# 380feature_check_header () 381{ 382 local result_ch 383 log2 "HeaderChk : $2" 384 echo "#include $2" > $TMPC 385 cat >> $TMPC <<EOF 386 int main(void) { return 0; } 387EOF 388 feature_check_compile result_ch 389 eval $1=$result_ch 390 #eval result=$`echo $1` 391 #log "Host : $1=$result_ch" 392} 393 394# run the test program that is in $TMPC and set its exit status 395# in the $1 variable. 396# you can define EXTRA_CFLAGS and EXTRA_LDFLAGS 397# 398feature_run_exec () 399{ 400 local run_exec_result 401 local OLD_CFLAGS="$CFLAGS" 402 local OLD_LDFLAGS="$LDFLAGS" 403 CFLAGS="$CFLAGS $EXTRA_CFLAGS" 404 LDFLAGS="$LDFLAGS $EXTRA_LDFLAGS" 405 compile_exec_run 406 run_exec_result=$? 407 CFLAGS="$OLD_CFLAGS" 408 LDFLAGS="$OLD_LDFLAGS" 409 eval $1=$run_exec_result 410 log "Host : $1=$run_exec_result" 411} 412 413## Android build system auto-detection 414## 415 416# check whether we're running within the Android build system 417# sets the variable IN_ANDROID_BUILD to either "yes" or "no" 418# 419# in case of success, defines ANDROID_TOP to point to the top 420# of the Android source tree. 421# 422check_android_build () 423{ 424 unset ANDROID_TOP 425 IN_ANDROID_BUILD=no 426 427 if [ -z "$ANDROID_PRODUCT_OUT" ] ; then 428 return ; 429 fi 430 431 ANDROID_TOP=`cd $ANDROID_PRODUCT_OUT/../../../.. && pwd` 432 log "ANDROID_TOP found at $ANDROID_TOP" 433 # $ANDROID_TOP/config/envsetup.make is for the old tree layout 434 # $ANDROID_TOP/build/envsetup.sh is for the new one 435 ANDROID_CONFIG_MK=$ANDROID_TOP/build/core/config.mk 436 if [ ! -f $ANDROID_CONFIG_MK ] ; then 437 ANDROID_CONFIG_MK=$ANDROID_TOP/config/envsetup.make 438 fi 439 if [ ! -f $ANDROID_CONFIG_MK ] ; then 440 echo "Weird: Cannot find build system root defaulting to non-Android build" 441 unset ANDROID_TOP 442 return 443 fi 444 # normalize ANDROID_TOP, we don't want a trailing / 445 ANDROID_TOPDIR=`dirname $ANDROID_TOP` 446 if [ "$ANDROID_TOPDIR" != "." ] ; then 447 ANDROID_TOP=$ANDROID_TOPDIR/`basename $ANDROID_TOP` 448 fi 449 IN_ANDROID_BUILD=yes 450} 451 452# Get the value of an Android build variable as an absolute path. 453# you should only call this if IN_ANDROID_BUILD is "yes" 454# 455get_android_abs_build_var () 456{ 457 (cd $ANDROID_TOP && CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core make -f $ANDROID_CONFIG_MK dumpvar-abs-$1) 458} 459 460# Locate the Android prebuilt directory for your os 461# you should only call this if IN_ANDROID_BUILD is "yes" 462# 463# This will set ANDROID_PREBUILT_HOST_TAG and ANDROID_PREBUILT 464# 465locate_android_prebuilt () 466{ 467 # locate prebuilt directory 468 ANDROID_PREBUILT_HOST_TAG=$OS 469 ANDROID_PREBUILT=$ANDROID_TOP/prebuilt/$ANDROID_PREBUILT_HOST_TAG 470 if [ ! -d $ANDROID_PREBUILT ] ; then 471 # this can happen when building on x86_64 472 case $OS in 473 linux-x86_64) 474 ANDROID_PREBUILT_HOST_TAG=linux-x86 475 ANDROID_PREBUILT=$ANDROID_TOP/prebuilt/$ANDROID_PREBUILT_HOST_TAG 476 log "Forcing usage of 32-bit prebuilts" 477 force_32bit_binaries 478 ;; 479 *) 480 esac 481 if [ ! -d $ANDROID_PREBUILT ] ; then 482 echo "Can't find the prebuilt directory $ANDROID_PREBUILT in Android build" 483 exit 1 484 fi 485 fi 486 log "Prebuilt : ANDROID_PREBUILT=$ANDROID_PREBUILT" 487} 488 489## Build configuration file support 490## you must define $config_mk before calling this function 491## 492create_config_mk () 493{ 494 # create the directory if needed 495 local config_dir 496 config_mk=${config_mk:-objs/config.make} 497 config_dir=`dirname $config_mk` 498 mkdir -p $config_dir 2> $TMPL 499 if [ $? != 0 ] ; then 500 echo "Can't create directory for build config file: $config_dir" 501 cat $TMPL 502 clean_exit 503 fi 504 505 # re-create the start of the configuration file 506 log "Generate : $config_mk" 507 508 echo "# This file was autogenerated by $PROGNAME. Do not edit !" > $config_mk 509 echo "OS := $OS" >> $config_mk 510 echo "HOST_OS := $HOST_OS" >> $config_mk 511 echo "HOST_ARCH := $HOST_ARCH" >> $config_mk 512 echo "CC := $CC" >> $config_mk 513 echo "HOST_CC := $CC" >> $config_mk 514 echo "LD := $LD" >> $config_mk 515 echo "CFLAGS := $CFLAGS" >> $config_mk 516 echo "LDFLAGS := $LDFLAGS" >> $config_mk 517} 518 519add_android_config_mk () 520{ 521 echo "" >> $config_mk 522 echo "TARGET_ARCH := arm" >> $config_mk 523 echo "HOST_PREBUILT_TAG := $HOST_TAG" >> $config_mk 524 echo "PREBUILT := $ANDROID_PREBUILT" >> $config_mk 525} 526