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/camera/ndk/include/camera/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/reference" 39ndkdir_html="$ANDROID_BUILD_TOP/frameworks/native/docs" 40aidldir="$ANDROID_BUILD_TOP/hardware/interfaces/camera/metadata/aidl/android/hardware/camera/metadata" 41camera2ctsdir="$ANDROID_BUILD_TOP/cts/tests/camera/src/android/hardware/camera2/cts" 42ctstopdir="$ANDROID_BUILD_TOP/cts/tests/camera/" 43outdir="$ANDROID_PRODUCT_OUT/obj/ETC/system-media-camera-docs_intermediates" 44ndk_header_dir="$ANDROID_BUILD_TOP/frameworks/av/camera/ndk/include/camera" 45ndk_impl_dir="$ANDROID_BUILD_TOP/frameworks/av/camera/ndk/impl" 46libcameraservice_dir="$ANDROID_BUILD_TOP/frameworks/av/services/camera/libcameraservice" 47libcameraservice_aidl_dir="$libcameraservice_dir/aidl" 48device_info_dir="$ANDROID_BUILD_TOP/cts/tools/cts-device-info/"` 49 `"src/com/android/cts/deviceinfo" 50out_files=() 51 52function relpath() { 53 python3 -c "import os.path; print(os.path.relpath('$1', '$PWD'))" 54} 55 56# Generates a file. Appends to $out_files array as a side effect. 57function gen_file() { 58 local in=$thisdir/$1 59 local out=$thisdir/$2 60 61 gen_file_abs "$in" "$out" 62 return $? 63} 64 65function gen_file_abs() { 66 local in="$1" 67 local out="$2" 68 local intermediates="$3" 69 local copyright_year="${4:-$(date +%Y)}" 70 local spec_file=$thisdir/metadata_definitions.xml 71 72 python3 $thisdir/metadata_parser_xml.py $spec_file $in $out $copyright_year 73 74 local succ=$? 75 76 if [[ $succ -eq 0 ]] 77 then 78 echo "OK: Generated $(relpath "$out")" 79 if [[ "$intermediates" != "no" ]]; then 80 out_files+=$'\n'" $out" 81 fi 82 else 83 echo "FAIL: Errors while generating $(relpath "$out")" >& 2 84 fi 85 86 return $succ 87} 88 89function gen_enum_files() { 90 local in="$1" 91 local out_dir="$2" 92 local intermediates="$3" 93 local copyright_year="${4:-$(date +%Y)}" 94 local spec_file=$thisdir/metadata_definitions.xml 95 96 python3 $thisdir/metadata_enums.py $spec_file $in $out_dir $copyright_year 97 98 local succ=$? 99 100 if [[ $succ -eq 0 ]] 101 then 102 echo "OK: Generated enum files in $(relpath "$out_dir")" >& 2 103 if [[ "$intermediates" != "no" ]]; then 104 out_files+=$'\n'" $out_dir/*.aidl" 105 fi 106 else 107 echo "FAIL: Errors while generating enum aidl files in $(relpath "$out_dir")" >& 2 108 fi 109 110 return $succ 111} 112 113# Print a list of git repository paths which were affected after file generation 114function affected_git_directories() { 115 local input_files=($@) 116 local git_directories=() 117 118 for file in "${input_files[@]}"; do 119 local dir_path="$(dirname "$file")" 120 echo "Trying to cd into $dir_path" >& /dev/null 121 # Absolute path to the git repository root of that file 122 local git_path="$(cd "$dir_path"; 123 git rev-parse --show-toplevel 2> /dev/null)" 124 if [[ $? -eq 0 ]]; then 125 # Both staged and unstaged changes 126 local diff_result="$(cd "$dir_path"; 127 git status --porcelain | egrep -c -v '^[?][?]')" 128 echo "Diff result was $diff_result" >& /dev/null 129 echo "Diff result was $diff_result" >& /dev/null 130 if [[ $diff_result -eq 0 ]]; then 131 echo "No changes in ${git_path}" >& /dev/null 132 else 133 echo "There are changes in ${git_path}" >& /dev/null 134 git_directories+=("$git_path") 135 fi 136 fi 137 done 138 139 # print as result the unique list of git directories affected 140 printf %s\\n "${git_directories[@]}" | sort | uniq 141} 142 143# Insert a file into the middle of another, starting at the line containing the 144# start delim and ending on the end delim, both of which are replaced 145function insert_file() { 146 local src_part="$1" 147 local dst_file="$2" 148 local start_delim="/*@O~" 149 local end_delim="~O@*/" 150 151 local start_line="$(grep -n -F "${start_delim}" "${dst_file}" | cut -d: -f1)" 152 local end_line="$(grep -n -F "${end_delim}" "${dst_file}" | cut -d: -f1)" 153 154 # Adjust cutoff points to use start/end line from inserted file 155 (( start_line-- )) 156 (( end_line++ )) 157 158 # Do some basic validity checks 159 160 if [[ -z "$start_line" ]]; then 161 echo "No starting delimiter found in ${dst_file}" >& 2 162 echo "FAIL: Errors in inserting into $(relpath ${dst_file})" >& 2 163 return 1 164 fi 165 166 if [[ -z "$end_line" ]]; then 167 echo "No ending delimiter found in ${dst_file}" >& 2 168 echo "FAIL: Errors in inserting into $(relpath ${dst_file})" >& 2 169 return 1 170 fi 171 172 if [[ "$start_line" -ge "$end_line" ]]; then 173 echo "Starting delim later than ending delim: $start_line vs $end_line" >& 2 174 echo "FAIL: Errors in inserting into $(relpath ${dst_file})" >& 2 175 return 1 176 fi 177 178 local tmp_name=$(mktemp -t XXXXXXXX) 179 180 # Compose the three parts of the final file together 181 182 head -n "$start_line" "${dst_file}" > "${tmp_name}" 183 cat "${src_part}" >> "${tmp_name}" 184 tail -n "+${end_line}" "${dst_file}" >> "${tmp_name}" 185 186 # And replace the destination file with the new version 187 188 mv "${tmp_name}" "${dst_file}" 189 echo "OK: Inserted $(relpath "$src_part") into $(relpath "$dst_file")" 190 out_files+=$'\n'" $dst_file" 191} 192 193# Recursively copy a directory tree from $1 to $2. Pretty-prints status. 194function copy_directory() { 195 local src="$thisdir/$1" # Relative to directory of this script 196 local dst="$2" # Absolute path 197 198 if ! [[ -d $src ]]; then 199 echo "FAIL: Source directory $src does not exist" >& 2 200 return 1 201 fi 202 if ! [[ -d $dst ]]; then 203 echo "FAIL: Destination directory $dst does not exist" >& 2 204 return 1 205 fi 206 207 cp -R "$src" "$dst" 208 local retval=$? 209 210 if [[ $retval -ne 0 ]]; then 211 echo "ERROR: Failed to copy $(relpath "$src") to $(relpath "$dst")" >& 2 212 else 213 echo "OK: Copied $(relpath "$src") to $(relpath "$dst")" 214 out_files+=$'\n'" $dst" 215 fi 216 217 return $retval 218} 219 220$thisdir/metadata-check-dependencies || exit 1 221$thisdir/metadata-validate $thisdir/metadata_definitions.xml || exit 1 222$thisdir/metadata-parser-validity-check || exit 1 223 224# Generate AIDL metadata modules 225mkdir -p "${aidldir}" 226gen_file_abs aidl/CameraMetadataSection.mako "$aidldir/CameraMetadataSection.aidl" yes || exit 1 227gen_file_abs aidl/CameraMetadataSectionStart.mako "$aidldir/CameraMetadataSectionStart.aidl" yes || exit 1 228gen_file_abs aidl/CameraMetadataTag.mako "$aidldir/CameraMetadataTag.aidl" yes || exit 1 229gen_enum_files aidl/CameraMetadataEnum.mako "$aidldir" yes || exit 1 230 231# Generate HTML properties documentation 232gen_file html.mako docs.html || exit 1 233 234# Generate C API headers and implementation 235gen_file camera_metadata_tag_info.mako ../src/camera_metadata_tag_info.c || exit 1 236gen_file camera_metadata_tags.mako ../include/system/camera_metadata_tags.h || exit 1 237gen_file camera_metadata_asserts.mako ../src/camera_metadata_asserts.cpp || exit 1 238gen_file ndk_camera_metadata_asserts.mako ../src/ndk_camera_metadata_asserts.cpp || exit 1 239 240#Generate tags with vndk versions for filtering 241gen_file_abs vndk_camera_metadata_tags.mako "$libcameraservice_aidl_dir/VndkVersionMetadataTags.h" yes || exit 1 242 243#Generate framework only tags being filtered out before sending to HAL 244gen_file_abs fwk_only_metadata_tags.mako "$libcameraservice_dir/FwkOnlyMetadataTags.h" yes || exit 1 245 246#Generate Session Characteristics Keys 247gen_file_abs session_characteristics_tags.mako "$libcameraservice_aidl_dir/SessionCharacteristicsTags.h" yes || exit 1 248 249#Generate NDK header 250gen_file_abs ndk_camera_metadata_tags.mako "$ndk_header_dir/NdkCameraMetadataTags.h" yes || exit 1 251 252#Generate extension tags for filtering 253gen_file_abs extension_camera_metadata_tags.mako "$libcameraservice_aidl_dir/ExtensionMetadataTags.h" yes || exit 1 254 255# Generate Java API definitions 256mkdir -p "${outdir}" 257gen_file_abs CameraMetadataEnums.mako "$outdir/CameraMetadataEnums.java.part" no || exit 1 258gen_file_abs CameraCharacteristicsKeys.mako "$outdir/CameraCharacteristicsKeys.java.part" no || exit 1 259gen_file_abs CaptureRequestKeys.mako "$outdir/CaptureRequestKeys.java.part" no || exit 1 260gen_file_abs CaptureResultKeys.mako "$outdir/CaptureResultKeys.java.part" no || exit 1 261 262insert_file "$outdir/CameraMetadataEnums.java.part" "$fwkdir/CameraMetadata.java" || exit 1 263insert_file "$outdir/CameraCharacteristicsKeys.java.part" "$fwkdir/CameraCharacteristics.java" || exit 1 264insert_file "$outdir/CaptureRequestKeys.java.part" "$fwkdir/CaptureRequest.java" || exit 1 265insert_file "$outdir/CaptureResultKeys.java.part" "$fwkdir/CaptureResult.java" || exit 1 266 267# Generate CTS test code 268gen_file_abs CaptureResultTest.mako "$outdir/CaptureResultTest.java.part" no || exit 1 269insert_file "$outdir/CaptureResultTest.java.part" "$camera2ctsdir/CaptureResultTest.java" || exit 1 270 271gen_file_abs ndk_name_to_tag.mako "$ctstopdir/libctscamera2jni/NdkNameToTag.h" yes | exit 1 272 273# Generate NDK implementation 274gen_file_abs ACameraMetadata.mako "$outdir/ACameraMetadata.cpp.part" no || exit 1 275insert_file "$outdir/ACameraMetadata.cpp.part" "$ndk_impl_dir/ACameraMetadata.cpp" || exit 1 276 277# Generate CameraDeviceInfo code 278gen_file_abs CameraDeviceInfo.mako "$outdir/CameraDeviceInfo.java.part" no || exit 1 279insert_file "$outdir/CameraDeviceInfo.java.part" "$device_info_dir/CameraDeviceInfo.java" || exit 1 280 281# Generate protocol buffer definition corresponding to CameraDeviceInfo 282gen_file camera_device_info.mako ./camera_device_info.proto || exit 1 283 284# Copy ./images directory into javadoc directory 285copy_directory "images" "$fwkdir_html" || exit 1 286 287# Copy ./images directory into ndk doc directory 288copy_directory "images" "$ndkdir_html" || exit 1 289 290echo "" 291echo "====================================================" 292echo "Successfully generated all metadata source files" 293echo "====================================================" 294echo "" 295 296echo "****************************************************" 297echo "The following git repositories need to be committed:" 298echo "****************************************************" 299echo "" 300affected_git_directories "${out_files[@]}" 301echo "" 302 303exit 0 304