• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2#
3# Copyright (C) 2010 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17#  This shell script is a wrapper to launch the NDK build from the
18#  command-line inside an application project path.
19#
20#  Typical usage is:
21#
22#     cd $PROJECT_PATH
23#     ndk-build
24#
25#  Assuming that the Android NDK root path is in your PATH. However,
26#  you can also invoke it directly as:
27#
28#     $NDK_ROOT/ndk-build
29#
30#  This really is a tiny wrapper around GNU Make.
31#
32
33# Ensure we get the full path of this script's directory
34# this is needed if the caller uses the -C <path> GNU Make
35# option, as in:
36#
37#    cd ndk
38#    ./ndk-build -C <project-path>
39#
40PROGDIR=`dirname $0`
41PROGDIR=`cd $PROGDIR && pwd`
42
43# If NDK_LOG is set to 1 or true in the environment, or the command-line
44# then enable log messages below
45if [ -z "$NDK_LOG" ]; then
46  NDK_LOG=0
47fi
48
49for opt; do
50    case $opt in
51      NDK_LOG=1|NDK_LOG=true)
52        NDK_LOG=1
53        ;;
54      NDK_LOG=*)
55        NDK_LOG=0
56        ;;
57    esac
58done
59
60if [ "$NDK_LOG" = "true" ]; then
61  NDK_LOG=1
62fi
63if [ "$NDK_LOG" = 1 ]; then
64  log () {
65    echo "$@"
66  }
67else
68  log () {
69    : # nothing
70  }
71fi
72
73# Detect host operating system and architecture
74# The 64-bit / 32-bit distinction gets tricky on Linux and Darwin because
75# uname -m returns the kernel's bit size, and it's possible to run with
76# a 64-bit kernel and a 32-bit userland.
77#
78HOST_OS=$(uname -s)
79case $HOST_OS in
80  Darwin) HOST_OS=darwin;;
81  Linux) HOST_OS=linux;;
82  FreeBsd) HOST_OS=freebsd;;
83  CYGWIN*|*_NT-*) HOST_OS=cygwin;;
84  *) echo "ERROR: Unknown host operating system: $HOST_OS"
85     exit 1
86esac
87log "HOST_OS=$HOST_OS"
88
89HOST_ARCH=$(uname -m)
90case $HOST_ARCH in
91    i?86) HOST_ARCH=x86;;
92    x86_64|amd64) HOST_ARCH=x86_64;;
93    *) echo "ERROR: Unknown host CPU architecture: $HOST_ARCH"
94       exit 1
95esac
96log "HOST_ARCH=$HOST_ARCH"
97
98# Detect 32-bit userland on 64-bit kernels
99HOST_TAG="$HOST_OS-$HOST_ARCH"
100case $HOST_TAG in
101  linux-x86_64|darwin-x86_64)
102    # we look for x86_64 or x86-64 in the output of 'file' for our shell
103    # the -L flag is used to dereference symlinks, just in case.
104    file -L "$SHELL" | grep -q "x86[_-]64"
105    if [ $? != 0 ]; then
106      HOST_ARCH=x86
107      HOST_TAG=$HOST_OS-x86
108      log "HOST_ARCH=$HOST_ARCH (32-bit userland detected)"
109    fi
110    ;;
111  windows-x86) # Special case windows-x86 -> windows
112    HOST_TAG=windows
113esac
114
115# Check that we have 64-bit binaries on 64-bit system, otherwise fallback
116# on 32-bit ones. This gives us more freedom in packaging the NDK.
117if [ $HOST_ARCH = x86_64 -a ! -d $PROGDIR/prebuilt/$HOST_TAG ]; then
118  HOST_TAG=$HOST_OS-x86
119  if [ $HOST_TAG = windows-x86 ]; then
120    HOST_TAG=windows
121  fi
122  log "HOST_TAG=$HOST_TAG (no 64-bit prebuilt binaries detected)"
123else
124  log "HOST_TAG=$HOST_TAG"
125fi
126
127# If GNUMAKE is defined, check that it points to a valid file
128if [ -n "$GNUMAKE" ] ; then
129    ABS_GNUMAKE=`which $GNUMAKE 2> /dev/null`
130    if [ $? != 0 ] ; then
131        echo "ERROR: Your GNUMAKE variable is defined to an invalid name: $GNUMAKE"
132        echo "Please fix it to point to a valid make executable (e.g. /usr/bin/make)"
133        exit 1
134    fi
135    GNUMAKE="$ABS_GNUMAKE"
136    log "GNUMAKE=$GNUMAKE (from environment variable)"
137else
138    # Otherwise use the prebuilt version for our host tag, if it exists
139    # Note: we intentionally do not provide prebuilt make binaries for Cygwin
140    # or MSys.
141    GNUMAKE=$PROGDIR/prebuilt/$HOST_TAG/bin/make
142    if [ ! -f "$GNUMAKE" ]; then
143        # Otherwise, use 'make' and check that it is available
144        GNUMAKE=`which make 2> /dev/null`
145        if [ $? != 0 ] ; then
146            echo "ERROR: Cannot find 'make' program. Please install Cygwin make package"
147            echo "or define the GNUMAKE variable to point to it."
148            exit 1
149        fi
150        log "GNUMAKE=$GNUMAKE (system path)"
151    else
152        log "GNUMAKE=$GNUMAKE (NDK prebuilt)"
153    fi
154fi
155
156# On Windows, when running under cygwin, check that we are
157# invoking a cygwin-compatible GNU Make binary. It is unfortunately
158# common for app developers to have another non cygwin-compatible
159# 'make' program in their PATH.
160#
161if [ "$OSTYPE" = "cygwin" ] ; then
162    GNUMAKE=`cygpath -u $GNUMAKE`
163    PROGDIR_MIXED=`cygpath -m $PROGDIR`
164    CYGWIN_GNUMAKE=`$GNUMAKE -f "$PROGDIR_MIXED/build/core/check-cygwin-make.mk" 2>&1`
165    if [ $? != 0 ] ; then
166        echo "ERROR: You are using a non-Cygwin compatible Make program."
167        echo "Currently using: `cygpath -m $GNUMAKE`"
168        echo ""
169        echo "To solve the issue, follow these steps:"
170        echo ""
171        echo "1. Ensure that the Cygwin 'make' package is installed."
172        echo "   NOTE: You will need GNU Make 3.81 or later!"
173        echo ""
174        echo "2. Define the GNUMAKE environment variable to point to it, as in:"
175        echo ""
176        echo "     export GNUMAKE=/usr/bin/make"
177        echo ""
178        echo "3. Call 'ndk-build' again."
179        echo ""
180        exit 1
181    fi
182    log "Cygwin-compatible GNU make detected"
183fi
184
185$GNUMAKE -f $PROGDIR/build/core/build-local.mk "$@"
186