1#!/bin/bash
2
3#
4# Copyright (C) 2012 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#      http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19#
20# Generate all files we have templates for:
21#   docs.html
22#   ../src/camera_metadata_tag_info.c
23#   ../src/camera_metadata_tags.h
24#   ../../../../frameworks/av/include/camera/ndk/NdkCameraMetadataTags.h
25#   ../../../../frameworks/av/camera/ndk/impl/ACameraMetadata.cpp
26#   ../../../../cts/tests/camera/src/android/hardware/camera2/cts/CaptureResultTest.java
27#   ../../../../frameworks/base/core/java/android/hardware/camera2/CameraCharacteristics.java
28#   ../../../../frameworks/base/core/java/android/hardware/camera2/CaptureRequest.java
29#   ../../../../frameworks/base/core/java/android/hardware/camera2/CaptureResult.java
30
31if [[ -z $ANDROID_BUILD_TOP ]]; then
32    echo "Please source build/envsetup.sh before running script" >& 2
33    exit 1
34fi
35
36thisdir=$(cd "$(dirname "$0")"; pwd)
37fwkdir="$ANDROID_BUILD_TOP/frameworks/base/core/java/android/hardware/camera2/"
38fwkdir_html="$ANDROID_BUILD_TOP/frameworks/base/docs/html"
39ctsdir="$ANDROID_BUILD_TOP/cts/tests/camera/src/android/hardware/camera2/cts"
40outdir="$ANDROID_PRODUCT_OUT/obj/ETC/system-media-camera-docs_intermediates"
41ndk_header_dir="$ANDROID_BUILD_TOP/frameworks/av/include/camera/ndk"
42ndk_impl_dir="$ANDROID_BUILD_TOP/frameworks/av/camera/ndk/impl"
43device_info_dir="$ANDROID_BUILD_TOP/cts/tools/cts-device-info/"`
44        `"src/com/android/cts/deviceinfo"
45out_files=()
46
47function relpath() {
48    python -c "import os.path; print os.path.relpath('$1', '$PWD')"
49}
50
51# Generates a file. Appends to $out_files array as a side effect.
52function gen_file() {
53    local in=$thisdir/$1
54    local out=$thisdir/$2
55
56    gen_file_abs "$in" "$out"
57    return $?
58}
59
60function gen_file_abs() {
61    local in="$1"
62    local out="$2"
63    local intermediates="$3"
64    local ndk="$4"
65    local spec_file=$thisdir/metadata_properties.xml
66
67    if [[ "$ndk" == "yes" ]]; then
68      spec_file=$thisdir/ndk_metadata_properties.xml
69    fi
70
71    python $thisdir/metadata_parser_xml.py $spec_file $in $out
72
73    local succ=$?
74
75    if [[ $succ -eq 0 ]]
76    then
77        echo "OK: Generated $(relpath "$out")"
78        if [[ "$intermediates" != "no" ]]; then
79          out_files+=$'\n'" $out"
80        fi
81    else
82        echo "FAIL: Errors while generating $(relpath "$out")" >& 2
83    fi
84
85    return $succ
86}
87
88# Print a list of git repository paths which were affected after file generation
89function affected_git_directories() {
90    local input_files=($@)
91    local git_directories=()
92
93    for file in "${input_files[@]}"; do
94        local dir_path="$(dirname "$file")"
95        echo "Trying to cd into $dir_path" >& /dev/null
96        # Absolute path to the git repository root of that file
97        local git_path="$(cd "$dir_path";
98                          git rev-parse --show-toplevel 2> /dev/null)"
99        if [[ $? -eq 0 ]]; then
100            # Both staged and unstaged changes
101            local diff_result="$(cd "$dir_path";
102                                 git status --porcelain | egrep -c -v '^[?][?]')"
103            echo "Diff result was $diff_result" >& /dev/null
104            echo "Diff result was $diff_result" >& /dev/null
105            if [[ $diff_result -eq 0 ]]; then
106                echo "No changes in ${git_path}" >& /dev/null
107            else
108                echo "There are changes in ${git_path}" >& /dev/null
109                git_directories+=("$git_path")
110            fi
111        fi
112    done
113
114    # print as result the unique list of git directories affected
115    printf %s\\n "${git_directories[@]}" | sort | uniq
116}
117
118# Insert a file into the middle of another, starting at the line containing the
119# start delim and ending on the end delim, both of which are replaced
120function insert_file() {
121    local src_part="$1"
122    local dst_file="$2"
123    local start_delim="/*@O~"
124    local end_delim="~O@*/"
125
126    local start_line="$(grep -n -F "${start_delim}" "${dst_file}" | cut -d: -f1)"
127    local end_line="$(grep -n -F "${end_delim}" "${dst_file}" | cut -d: -f1)"
128
129    # Adjust cutoff points to use start/end line from inserted file
130    (( start_line-- ))
131    (( end_line++ ))
132
133    # Do some basic sanity checks
134
135    if [[ -z "$start_line" ]]; then
136       echo "No starting delimiter found in ${dst_file}" >& 2
137       echo "FAIL: Errors in inserting into $(relpath ${dst_file})" >& 2
138       return 1
139    fi
140
141    if [[ -z "$end_line" ]]; then
142       echo "No ending delimiter found in ${dst_file}" >& 2
143       echo "FAIL: Errors in inserting into $(relpath ${dst_file})" >& 2
144       return 1
145    fi
146
147    if [[ "$start_line" -ge "$end_line" ]]; then
148       echo "Starting delim later than ending delim: $start_line vs $end_line" >& 2
149       echo "FAIL: Errors in inserting into $(relpath ${dst_file})" >& 2
150       return 1
151    fi
152
153    local tmp_name=$(mktemp -t XXXXXXXX)
154
155    # Compose the three parts of the final file together
156
157    head -n "$start_line" "${dst_file}" > "${tmp_name}"
158    cat "${src_part}" >> "${tmp_name}"
159    tail -n "+${end_line}" "${dst_file}" >> "${tmp_name}"
160
161    # And replace the destination file with the new version
162
163    mv "${tmp_name}" "${dst_file}"
164    echo "OK: Inserted $(relpath "$src_part") into $(relpath "$dst_file")"
165    out_files+=$'\n'" $dst_file"
166}
167
168# Recursively copy a directory tree from $1 to $2. Pretty-prints status.
169function copy_directory() {
170    local src="$thisdir/$1" # Relative to directory of this script
171    local dst="$2" # Absolute path
172
173    if ! [[ -d $src ]]; then
174        echo "FAIL: Source directory $src does not exist" >& 2
175        return 1
176    fi
177    if ! [[ -d $dst ]]; then
178        echo "FAIL: Destination directory $dst does not exist" >& 2
179        return 1
180    fi
181
182    cp -R "$src" "$dst"
183    local retval=$?
184
185    if [[ $retval -ne 0 ]]; then
186        echo "ERROR: Failed to copy $(relpath "$src") to $(relpath "$dst")" >& 2
187    else
188        echo "OK: Copied $(relpath "$src") to $(relpath "$dst")"
189    fi
190
191    return $retval
192}
193
194$thisdir/metadata-check-dependencies || exit 1
195$thisdir/metadata-validate $thisdir/metadata_properties.xml || exit 1
196$thisdir/metadata-parser-sanity-check || exit 1
197
198# Generate HTML properties documentation
199gen_file html.mako docs.html || exit 1
200
201# Generate C API headers and implementation
202gen_file camera_metadata_tag_info.mako ../src/camera_metadata_tag_info.c || exit 1
203gen_file camera_metadata_tags.mako ../include/system/camera_metadata_tags.h || exit 1
204
205#Generate NDK header
206gen_file_abs ndk_camera_metadata_tags.mako "$ndk_header_dir/NdkCameraMetadataTags.h" yes yes || exit 1
207
208# Generate Java API definitions
209mkdir -p "${outdir}"
210gen_file_abs CameraMetadataEnums.mako "$outdir/CameraMetadataEnums.java.part" no || exit 1
211gen_file_abs CameraCharacteristicsKeys.mako "$outdir/CameraCharacteristicsKeys.java.part" no || exit 1
212gen_file_abs CaptureRequestKeys.mako "$outdir/CaptureRequestKeys.java.part" no || exit 1
213gen_file_abs CaptureResultKeys.mako "$outdir/CaptureResultKeys.java.part" no || exit 1
214
215insert_file "$outdir/CameraMetadataEnums.java.part" "$fwkdir/CameraMetadata.java" || exit 1
216insert_file "$outdir/CameraCharacteristicsKeys.java.part" "$fwkdir/CameraCharacteristics.java" || exit 1
217insert_file "$outdir/CaptureRequestKeys.java.part" "$fwkdir/CaptureRequest.java" || exit 1
218insert_file "$outdir/CaptureResultKeys.java.part" "$fwkdir/CaptureResult.java" || exit 1
219
220# Generate CTS test code
221gen_file_abs CaptureResultTest.mako "$outdir/CaptureResultTest.java.part" no || exit 1
222insert_file "$outdir/CaptureResultTest.java.part" "$ctsdir/CaptureResultTest.java" || exit 1
223
224# Generate NDK implementation
225gen_file_abs ACameraMetadata.mako "$outdir/ACameraMetadata.cpp.part" no || exit 1
226insert_file "$outdir/ACameraMetadata.cpp.part" "$ndk_impl_dir/ACameraMetadata.cpp" || exit 1
227
228# Generate CameraDeviceInfo code
229gen_file_abs CameraDeviceInfo.mako "$outdir/CameraDeviceInfo.java.part" no || exit 1
230insert_file "$outdir/CameraDeviceInfo.java.part" "$device_info_dir/CameraDeviceInfo.java" || exit 1
231
232# Generate protocol buffer definition corresponding to CameraDeviceInfo
233gen_file camera_device_info.mako ./camera_device_info.proto || exit 1
234
235# Copy ./images directory into javadoc directory
236copy_directory "images" "$fwkdir_html" || exit 1
237
238echo ""
239echo "===================================================="
240echo "Successfully generated all metadata source files"
241echo "===================================================="
242echo ""
243
244echo "****************************************************"
245echo "The following git repositories need to be committed:"
246echo "****************************************************"
247echo ""
248affected_git_directories "${out_files[@]}"
249echo ""
250
251exit 0
252