1#!/bin/bash
2#
3# Copyright 2015 Google Inc. All rights reserved
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# A wrapper for kati which generates build.ninja mainly for Android.
18#
19
20set -e
21
22kati_dir=$(cd $(dirname $0) && pwd)
23extra_flags=
24goma_flag=
25goma_dir=${GOMA_DIR:-$HOME/goma}
26
27while [ x"$1" != x"" ]; do
28  case "$1" in
29    --help)
30      cat - <<EOF
31Usage:
32  m2n               # for default full-build
33  m2n --goma        # use goma. \$HOME/goma must exist, or set \$GOMA_DIR
34  m2n --go          # use go version. Slower but maybe more portable.
35  m2n cts           # for target 'cts'
36EOF
37      exit 1
38      ;;
39
40    --go)
41      kati=${kati_dir}/kati
42      shift
43      ${kati} --m2n "$@"
44      echo
45      echo ninja.sh and build.ninja were generated, please run ./ninja.sh
46      exit
47      ;;
48
49    --goma)
50      if [ ! -e $goma_dir/goma_ctl.py ]; then
51        echo "To use Goma you must set GOMA_DIR, or install goma in $HOME/goma"
52        exit 1
53      fi
54      $goma_dir/goma_ctl.py ensure_start
55      goma_flag=--goma_dir=$goma_dir
56      shift
57      ;;
58
59    --mmm)
60      echo 'Note: --mmm may not work'
61      shift;
62      mmm="$1"
63      shift
64      ;;
65
66    -*)
67      extra_flags+=" $1"
68      shift
69      ;;
70
71    *)
72      targets="${targets} $1"
73      shift
74      ;;
75  esac
76done
77
78if [ x"${goma_flag}" != x ]; then
79  for k in CC_WRAPPER CXX_WRAPPER JAVAC_WRAPPER; do
80    val=$(eval echo \$$k)
81    if [ x"${val}" != x ]; then
82      echo "Note: \$$k=${val} may confuse m2n --goma, unsetting"
83      unset $k
84    fi
85  done
86fi
87
88kati=${kati_dir}/ckati
89
90ninja_suffix=
91ninja_suffix_flag=
92
93if [ x"${mmm}" != x"" ]; then
94  mk="${mmm}/Android.mk"
95  if [ ! -f ${mk} ]; then
96    echo "${mk} does not exist"
97    exit 1
98  fi
99
100  export ONE_SHOT_MAKEFILE=${mk}
101  echo ONE_SHOT_MAKEFILE=${ONE_SHOT_MAKEFILE}
102
103  ninja_suffix+=-mmm-${mmm}
104fi
105
106if [ x"${targets}" != x"" ]; then
107  ninja_suffix+=-$(echo ${targets} | sed 's/ /-/')
108elif [ x"${mmm}" != x"" ]; then
109  targets=all_modules
110fi
111
112if [ x"${ninja_suffix}" != x"" ]; then
113  ninja_suffix=$(echo ${ninja_suffix} | sed 'y/\//_/')
114  ninja_suffix_flag=--ninja_suffix=${ninja_suffix}
115fi
116
117${kati} --ninja ${ninja_suffix_flag} --ignore_optional_include=out/%.P --ignore_dirty=out/% --use_find_emulator --detect_android_echo --detect_depfiles --gen_all_targets ${goma_flag} ${extra_flags} ${targets}
118
119echo
120echo ninja${ninja_suffix}.sh and build${ninja_suffix}.ninja were generated, please run ./ninja${ninja_suffix}.sh
121