Risan | edc20e2 | 2017-12-07 12:35:19 +0900 | [diff] [blame] | 1 | #!/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 | |
| 22 | set -e |
| 23 | |
| 24 | args=() |
| 25 | files=() |
| 26 | |
| 27 | mojom_bindings_generator="" |
| 28 | package="" |
| 29 | output_dir="" |
Risan | d3b0199 | 2017-12-13 15:27:58 +0900 | [diff] [blame] | 30 | generators="" |
Risan | 1db2645 | 2018-02-05 17:09:01 +0900 | [diff] [blame] | 31 | private_mojo_root="$(pwd)/external/libmojo" |
Risan | edc20e2 | 2017-12-07 12:35:19 +0900 | [diff] [blame] | 32 | |
| 33 | # Given a path to directory or file, return the absolute path. |
| 34 | get_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 | |
| 49 | for 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})" |
Risan | 7db3d20 | 2018-01-20 16:13:25 +0900 | [diff] [blame] | 65 | args=("${args[@]}" "--typemap=${typemap}") |
Risan | edc20e2 | 2017-12-07 12:35:19 +0900 | [diff] [blame] | 66 | ;; |
| 67 | --bytecode_path=*) |
| 68 | bytecode_path="${arg#'--bytecode_path='}" |
| 69 | bytecode_path="$(get_abs_path ${bytecode_path})" |
| 70 | ;; |
Risan | d3b0199 | 2017-12-13 15:27:58 +0900 | [diff] [blame] | 71 | --generators=*) |
| 72 | generators="${arg#'--generators='}" |
| 73 | ;; |
Risan | edc20e2 | 2017-12-07 12:35:19 +0900 | [diff] [blame] | 74 | --*) |
| 75 | args=("${args[@]}" "${arg}") |
| 76 | ;; |
| 77 | *) |
| 78 | files=("${files[@]}" "$(get_abs_path ${arg})") |
| 79 | ;; |
| 80 | esac |
| 81 | done |
| 82 | |
| 83 | cd "${package}" |
| 84 | "${mojom_bindings_generator}" precompile -o "${output_dir}" |
| 85 | |
| 86 | for file in "${files[@]}"; do |
Risan | d3b0199 | 2017-12-13 15:27:58 +0900 | [diff] [blame] | 87 | # 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 | |
Risan | 1db2645 | 2018-02-05 17:09:01 +0900 | [diff] [blame] | 94 | # 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. |
Risan | edc20e2 | 2017-12-07 12:35:19 +0900 | [diff] [blame] | 98 | "${mojom_bindings_generator}" generate -o "${output_dir}" "${args[@]}" \ |
Risan | 7db3d20 | 2018-01-20 16:13:25 +0900 | [diff] [blame] | 99 | --bytecode_path="${bytecode_path}" \ |
Risan | 1db2645 | 2018-02-05 17:09:01 +0900 | [diff] [blame] | 100 | -I "${private_mojo_root}:${private_mojo_root}" \ |
Risan | d3b0199 | 2017-12-13 15:27:58 +0900 | [diff] [blame] | 101 | --generators=${generators} "${file}" |
Risan | ceaad1a | 2018-01-24 02:24:16 +0900 | [diff] [blame] | 102 | if [[ "${generators}" =~ .*c\+\+.* ]] ; then |
Risan | d3b0199 | 2017-12-13 15:27:58 +0900 | [diff] [blame] | 103 | "${mojom_bindings_generator}" generate -o "${output_dir}" \ |
Risan | 7db3d20 | 2018-01-20 16:13:25 +0900 | [diff] [blame] | 104 | --generate_non_variant_code "${args[@]}" \ |
Risan | 1db2645 | 2018-02-05 17:09:01 +0900 | [diff] [blame] | 105 | -I "${private_mojo_root}:${private_mojo_root}" \ |
Risan | d3b0199 | 2017-12-13 15:27:58 +0900 | [diff] [blame] | 106 | --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 |
Risan | edc20e2 | 2017-12-07 12:35:19 +0900 | [diff] [blame] | 112 | done |