1#!/bin/bash
2# Copyright 2013 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# This a simple script to make building/testing Mojo components easier (on
7# Linux).
8
9# TODO(vtl): Maybe make the test runner smart and not run unchanged test
10# binaries.
11# TODO(vtl) Maybe also provide a way to pass command-line arguments to the test
12# binaries.
13
14do_help() {
15  cat << EOF
16Usage: $(basename "$0") [command|option ...]
17
18command should be one of:
19  build - Build.
20  test - Run unit tests (does not build).
21  perftest - Run Release and Debug perf tests (does not build).
22  gyp - Run gyp for mojo (does not sync), with clang.
23  sync - Sync using gclient (does not run gyp).
24  show-bash-alias - Outputs an appropriate bash alias for mojob. In bash do:
25      \$ eval \`mojo/tools/mojob.sh show-bash-alias\`
26
27option (which will only apply to following commands) should be one of:
28  Build/test options (specified before build/test/perftest):
29    --debug - Build/test in Debug mode.
30    --release - Build/test in Release mode.
31    --debug-and-release - Build/test in both Debug and Release modes (default).
32  Compiler options (specified before gyp):
33    --clang - Use clang (default).
34    --gcc - Use gcc.
35  Component options:
36    --shared Build components as shared libraries (default).
37    --static Build components as static libraries.
38
39Note: It will abort on the first failure (if any).
40EOF
41}
42
43do_build() {
44  echo "Building in out/$1 ..."
45  ninja -C "out/$1" mojo || exit 1
46}
47
48do_unittests() {
49  echo "Running unit tests in out/$1 ..."
50  "out/$1/mojo_bindings_unittests" || exit 1
51  "out/$1/mojo_common_unittests" || exit 1
52  "out/$1/mojo_js_unittests" || exit 1
53  "out/$1/mojo_public_unittests" || exit 1
54  "out/$1/mojo_system_unittests" || exit 1
55  "out/$1/mojo_utility_unittests" || exit 1
56}
57
58do_perftests() {
59  echo "Running perf tests in out/$1 ..."
60  "out/$1/mojo_public_perftests" || exit 1
61}
62
63do_gyp() {
64  local gyp_defines="$(make_gyp_defines)"
65  echo "Running gyp with GYP_DEFINES=$gyp_defines ..."
66  GYP_DEFINES="$gyp_defines" build/gyp_chromium mojo/mojo.gyp
67}
68
69# Valid values: Debug, Release, or Debug_and_Release.
70BUILD_TEST_TYPE=Debug_and_Release
71should_do_Debug() {
72  test "$BUILD_TEST_TYPE" = Debug -o "$BUILD_TEST_TYPE" = Debug_and_Release
73}
74should_do_Release() {
75  test "$BUILD_TEST_TYPE" = Release -o "$BUILD_TEST_TYPE" = Debug_and_Release
76}
77
78# Valid values: clang or gcc.
79COMPILER=clang
80# Valid values: shared or static.
81COMPONENT=shared
82make_gyp_defines() {
83  local options=()
84  # Always include these options.
85  options+=("use_aura=1")
86  case "$COMPILER" in
87    clang)
88      options+=("clang=1")
89      ;;
90    gcc)
91      options+=("clang=0")
92      ;;
93  esac
94  case "$COMPONENT" in
95    shared)
96      options+=("component=shared_library")
97      ;;
98    static)
99      options+=("component=static_library")
100      ;;
101  esac
102  echo ${options[*]}
103}
104
105# We're in src/mojo/tools. We want to get to src.
106cd "$(realpath "$(dirname "$0")")/../.."
107
108if [ $# -eq 0 ]; then
109  do_help
110  exit 0
111fi
112
113for arg in "$@"; do
114  case "$arg" in
115    # Commands -----------------------------------------------------------------
116    help|--help)
117      do_help
118      exit 0
119      ;;
120    build)
121      should_do_Debug && do_build Debug
122      should_do_Release && do_build Release
123      ;;
124    test)
125      should_do_Debug && do_unittests Debug
126      should_do_Release && do_unittests Release
127      ;;
128    perftest)
129      should_do_Debug && do_perftests Debug
130      should_do_Release && do_perftests Release
131      ;;
132    gyp)
133      do_gyp
134      ;;
135    sync)
136      # Note: sync only (with hooks, but no gyp-ing).
137      GYP_CHROMIUM_NO_ACTION=1 gclient sync
138      ;;
139    show-bash-alias)
140      # You want to type something like:
141      #   alias mojob=\
142      #       '"$(pwd | sed '"'"'s/\(.*\/src\).*/\1/'"'"')/mojo/tools/mojob.sh"'
143      # This is quoting hell, so we simply escape every non-alphanumeric
144      # character.
145      echo alias\ mojob\=\'\"\$\(pwd\ \|\ sed\ \'\"\'\"\'s\/\\\(\.\*\\\/src\\\)\
146\.\*\/\\1\/\'\"\'\"\'\)\/mojo\/tools\/mojob\.sh\"\'
147      ;;
148    # Options ------------------------------------------------------------------
149    --debug)
150      BUILD_TEST_TYPE=Debug
151      ;;
152    --release)
153      BUILD_TEST_TYPE=Release
154      ;;
155    --debug-and-release)
156      BUILD_TEST_TYPE=Debug_and_Release
157      ;;
158    --clang)
159      COMPILER=clang
160      ;;
161    --gcc)
162      COMPILER=gcc
163      ;;
164    --shared)
165      COMPONENT=shared
166      ;;
167    --static)
168      COMPONENT=static
169      ;;
170    *)
171      echo "Unknown command \"${arg}\". Try \"$(basename "$0") help\"."
172      exit 1
173      ;;
174  esac
175done
176