blob: f1fac9e7599797fe13c458073d3c9b9fdd915077 [file] [log] [blame]
Risanedc20e22017-12-07 12:35:19 +09001#!/bin/bash
2#
3# Copyright (C) 2017 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# Generates mojo sources given a list of .mojom files and args.
18# Usage: $0 --mojom_bindings_generator=<abs_path> --package=<package_directory>
19# --output_dir=<output_directory>
20# [<extra_args_for_bindings_generator>] <list_of_mojom_files>
21
22set -e
23
24args=()
25files=()
26
27mojom_bindings_generator=""
28package=""
29output_dir=""
Risand3b01992017-12-13 15:27:58 +090030generators=""
Risan1db26452018-02-05 17:09:01 +090031private_mojo_root="$(pwd)/external/libmojo"
Risanedc20e22017-12-07 12:35:19 +090032
33# Given a path to directory or file, return the absolute path.
34get_abs_path() {
35 if [[ -d $1 ]] ; then
36 cd "$1"
37 filename=""
38 else
39 filepath=$1
40 dir="${filepath%/*}"
41 cd "${dir}"
42 filename="${filepath#${dir}/}"
43 fi
44 absdir=`pwd`
45 cd - > /dev/null
46 echo "${absdir}/${filename}"
47}
48
49for arg in "$@"; do
50 case "${arg}" in
51 --mojom_bindings_generator=*)
52 mojom_bindings_generator="${arg#'--mojom_bindings_generator='}"
53 mojom_bindings_generator="$(get_abs_path ${mojom_bindings_generator})"
54 ;;
55 --package=*)
56 package="${arg#'--package='}"
57 ;;
58 --output_dir=*)
59 output_dir="${arg#'--output_dir='}"
60 output_dir="$(get_abs_path ${output_dir})"
61 ;;
62 --typemap=*)
63 typemap="${arg#'--typemap='}"
64 typemap="$(get_abs_path ${typemap})"
Risan7db3d202018-01-20 16:13:25 +090065 args=("${args[@]}" "--typemap=${typemap}")
Risanedc20e22017-12-07 12:35:19 +090066 ;;
67 --bytecode_path=*)
68 bytecode_path="${arg#'--bytecode_path='}"
69 bytecode_path="$(get_abs_path ${bytecode_path})"
70 ;;
Risand3b01992017-12-13 15:27:58 +090071 --generators=*)
72 generators="${arg#'--generators='}"
73 ;;
Risanedc20e22017-12-07 12:35:19 +090074 --*)
75 args=("${args[@]}" "${arg}")
76 ;;
77 *)
78 files=("${files[@]}" "$(get_abs_path ${arg})")
79 ;;
80 esac
81done
82
83cd "${package}"
84"${mojom_bindings_generator}" precompile -o "${output_dir}"
85
86for file in "${files[@]}"; do
Risand3b01992017-12-13 15:27:58 +090087 # Java source generations depends on zipfile that assumes the output directory
88 # already exists. So, we need to create the directory beforehand.
89 rel_path="${file#`pwd`/}"
90 rel_dir="${rel_path%/*}"
91
92 mkdir -p "${output_dir}/${rel_dir}"
93
Risan1db26452018-02-05 17:09:01 +090094 # The calls to mojom_bindings_generator below uses -I option to include the
95 # libmojo root directory as part of searchable directory for imports. With
96 # this, we can have a mojo file located in some arbitrary directories that
97 # imports a mojo file under external/libmojo.
Risanedc20e22017-12-07 12:35:19 +090098 "${mojom_bindings_generator}" generate -o "${output_dir}" "${args[@]}" \
Risan7db3d202018-01-20 16:13:25 +090099 --bytecode_path="${bytecode_path}" \
Risan1db26452018-02-05 17:09:01 +0900100 -I "${private_mojo_root}:${private_mojo_root}" \
Risand3b01992017-12-13 15:27:58 +0900101 --generators=${generators} "${file}"
Risanceaad1a2018-01-24 02:24:16 +0900102 if [[ "${generators}" =~ .*c\+\+.* ]] ; then
Risand3b01992017-12-13 15:27:58 +0900103 "${mojom_bindings_generator}" generate -o "${output_dir}" \
Risan7db3d202018-01-20 16:13:25 +0900104 --generate_non_variant_code "${args[@]}" \
Risan1db26452018-02-05 17:09:01 +0900105 -I "${private_mojo_root}:${private_mojo_root}" \
Risand3b01992017-12-13 15:27:58 +0900106 --bytecode_path="${bytecode_path}" --generators=${generators} \
107 "${file}"
108 fi
109 if [[ "${generators}" =~ .*java.* ]] ; then
110 unzip -qo -d "${output_dir}"/src "${output_dir}/${rel_path}".srcjar
111 fi
Risanedc20e22017-12-07 12:35:19 +0900112done