1#!/bin/bash
2
3# Bash script to assert that the current version of the NDK is at least the
4# specified version. Prints 'true' to standard out if it's the right version,
5# 'false' if it's not.
6#
7# Typically used like this, in your jni/Android.mk:
8#
9#   ifneq ($(shell $(LOCAL_PATH)/assert_ndk_version.sh "r5c" "ndk-dir"), true)
10#     $(error NDK version r5c or greater required)
11#   endif
12#
13# See https://gist.github.com/2878774 for asserting SDK version.
14#
15# Retrieved from: https://gist.github.com/jorgenpt/1961404 on 2014-06-03.
16#
17# Copyright (c) 2012, Lookout, Inc. All rights reserved.
18#
19# Redistribution and use in source and binary forms, with or without
20# modification, are permitted provided that the following conditions are met:
21#
22# 1. Redistributions of source code must retain the above copyright notice,
23#    this list of conditions and the following disclaimer.
24#
25# 2. Redistributions in binary form must reproduce the above copyright
26#    notice, this list of conditions and the following disclaimer in the
27#    documentation and/or other materials provided with the distribution.
28#
29# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
33# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
34# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
36# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
37# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
38# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
39# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40#
41# Author: jorgenpt@gmail.com (Jorgen Tjerno)
42#         alexs.mac@gmail.com (Alex Stewart)
43
44# Extracts 'r5c' into '5 c', also handles newer versions of the form
45# 'r9d (64-bit)' and versions >= 10.
46function get_major_minor() {
47  # r9d (64-bit) -> '9d', also handle versions >= 10.
48  local version=$(echo "$1" | sed 's/r\([0-9]\{1,2\}[a-z]\{0,1\}\).*/\1/')
49  local major=$(echo "$version" | sed 's/\([0-9]\{1,2\}\).*/\1/')
50  local minor=$(echo "$version" | sed 's/^[0-9]*//')
51  echo "$major $minor"
52}
53
54if [[ -z "$2" ]]; then
55  echo "Usage: $0 <required version> <NDK_ROOT>" >&2
56  echo " For example: $0 r5c android-ndk-r9d" >&2
57  exit 1
58fi
59
60# Assert that the expected version is at least 4.
61declare -a expected_version
62expected_version=( $(get_major_minor "$1") )
63if [[ ${expected_version[0]} -le 4 ]]; then
64  echo "Cannot test for versions less than r5: r4 doesn't have a version file." >&2
65  echo false
66  exit 1
67fi
68
69release_file="$2/RELEASE.TXT"
70
71# NDK version r4 or earlier doesn't have a RELEASE.txt, and we just asserted
72# that the person was looking for r5 or above, so that implies that this is an
73# invalid version.
74if [ ! -s "$release_file" ]; then
75  echo false
76  exit 0
77fi
78
79# Make sure the data is at least kinda sane.
80version=$(grep '^r' $release_file)
81declare -a actual_version
82actual_version=( $(get_major_minor "$version") )
83if [ -z "$version" ] || [ -z "${actual_version[0]}" ]; then
84  echo "Invalid RELEASE.txt: $(cat $release_file)" >&2
85  echo false
86  exit 1
87fi
88
89if [[ ${actual_version[0]} -lt ${expected_version[0]} ]]; then
90  echo "false"
91elif [[ ${actual_version[0]} -eq ${expected_version[0]} ]]; then
92  # This uses < and not -lt because they're string identifiers (a, b, c, etc)
93  if [[ "${actual_version[1]}" < "${expected_version[1]}" ]]; then
94    echo "false"
95  else
96    echo "true"
97  fi
98else
99  echo "true"
100fi
101