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