• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2#
3# Copyright 2018 The Bazel Authors. 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# ---
18# Milestone 2 demo script to convert Android.bp modules in //bionic/libc to
19# buildable Bazel BUILD targets. Run "./demo help" for more info.
20# ---
21
22set -euo pipefail
23
24function help() {
25  cat <<EOF
26A demo script for the Android.bp to BUILD file converter.
27
28Usage:
29
30  ./demo.sh generate -- runs the bp2build converter to generate BUILD files from Android.bp files.
31  ./demo.sh query -- runs the bazel query command for all targets in //bionic/libc, recursively.
32  ./demo.sh build -- runs the bazel build command for all targets in //bionic/libc, recursively.
33  ./demo.sh full -- runs the generate, query and build steps in sequence.
34  ./demo.sh help -- prints this message.
35
36EOF
37}
38
39# We're in <root>/build/bazel/scripts/milestone-2
40AOSP_ROOT="$(dirname $0)/../../../.."
41
42RED="\031[0;32m"
43GREEN="\033[0;32m"
44RESET="\033[0m"
45
46function error() {
47  local message=$1; shift;
48  echo -e "${RED}ERROR[Milestone 2 Demo]: $message${RESET}"
49}
50
51function log() {
52  local message=$1; shift;
53  echo -e "${GREEN}INFO[Milestone 2 Demo]: $message${RESET}"
54}
55
56# Ensure that this script uses the checked-in Bazel binary.
57function bazel() {
58  "${AOSP_ROOT}/tools/bazel" "$@"
59}
60
61# Run the bp2build converter to generate BUILD files into out/soong/bp2build.
62function generate() {
63  log "Running the bp2build converter.."
64  GENERATE_BAZEL_FILES=true "${AOSP_ROOT}/build/soong/soong_ui.bash" --make-mode nothing --skip-soong-tests
65  log "Successfully generated BUILD files in out/soong/bp2build."
66}
67
68# Run bazel query for the generated targets in the //bionic/libc package.
69function query-bionic-package() {
70  log "Running bazel query //bionic/..."
71  bazel query //bionic/...
72}
73
74# Use bazel to build the generated targets in the //bionic/libc package.
75function build-bionic-package() {
76  log "Running bazel build //bionic/..."
77  bazel build --platforms //build/bazel/platforms:android_x86 //bionic/...
78  bazel build --platforms //build/bazel/platforms:android_x86_64 //bionic/...
79  bazel build --platforms //build/bazel/platforms:android_arm //bionic/...
80  bazel build --platforms //build/bazel/platforms:android_arm64 //bionic/...
81}
82
83function run() {
84  action=${1:-full}
85
86  case $action in
87    "help")
88      help
89      ;;
90    "generate")
91      generate
92      ;;
93    "query")
94      query-bionic-package
95      ;;
96    "build")
97      build-bionic-package
98      ;;
99    "full")
100      generate
101      query-bionic-package
102      build-bionic-package
103      ;;
104    *)
105      error "Unknown action: $action"
106      help
107      exit 1
108  esac
109
110  log "($action) done."
111}
112
113run $@
114