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