summaryrefslogtreecommitdiffstats
path: root/camera/docs/metadata-generate
blob: ec9e94760b3cfad6302837293ac2cee1a51b0da0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/bash

#
# Copyright (C) 2012 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#
# Generate all files we have templates for:
#   docs.html
#   ../src/camera_metadata_tag_info.c
#   ../src/camera_metadata_tags.h
#   ../../../../cts/tests/tests/hardware/src/android/hardware/camera2/cts/CameraCharacteristicsTest.java
#   ../../../../cts/tests/tests/hardware/src/android/hardware/camera2/cts/CameraCaptureResultTest.java
#   ../../../../frameworks/base/core/java/android/hardware/camera2/CameraCharacteristics.java
#   ../../../../frameworks/base/core/java/android/hardware/camera2/CaptureRequest.java
#   ../../../../frameworks/base/core/java/android/hardware/camera2/CaptureResult.java

if [[ -z $ANDROID_BUILD_TOP ]]; then
    echo "Please source build/envsetup.sh before running script" >& 2
    exit 1
fi

thisdir=$(cd "$(dirname "$0")"; pwd)
fwkdir="$ANDROID_BUILD_TOP/frameworks/base/core/java/android/hardware/camera2/"
ctsdir="$ANDROID_BUILD_TOP/cts/tests/tests/hardware/src/android/hardware/camera2/cts"
outdir="$ANDROID_PRODUCT_OUT/obj/ETC/system-media-camera-docs_intermediates"
out_files=()

function relpath() {
    python -c "import os.path; print os.path.relpath('$1', '$PWD')"
}

# Generates a file. Appends to $out_files array as a side effect.
function gen_file() {
    local in=$thisdir/$1
    local out=$thisdir/$2

    gen_file_abs "$in" "$out"
    return $?
}

function gen_file_abs() {
    local in="$1"
    local out="$2"
    local intermediates="$3"

    python $thisdir/metadata_parser_xml.py $thisdir/metadata_properties.xml $in $out

    local succ=$?

    if [[ $succ -eq 0 ]]
    then
        echo "OK: Generated $(relpath "$out")"
        if [[ "$intermediates" != "no" ]]; then
          out_files+=$'\n'" $out"
        fi
    else
        echo "FAIL: Errors while generating $(relpath "$out")" >& 2
    fi

    return $succ
}

# Print a list of git repository paths which were affected after file generation
function affected_git_directories() {
    local input_files=($@)
    local git_directories=()

    for file in "${input_files[@]}"; do
        local dir_path="$(dirname "$file")"
        echo "Trying to cd into $dir_path" >& /dev/null
        # Absolute path to the git repository root of that file
        local git_path="$(cd "$dir_path";
                          git rev-parse --show-toplevel 2> /dev/null)"
        if [[ $? -eq 0 ]]; then
            # Both staged and unstaged changes
            local diff_result="$(cd "$dir_path";
                                 git status --porcelain | egrep -c -v '^[?][?]')"
            echo "Diff result was $diff_result" >& /dev/null
            echo "Diff result was $diff_result" >& /dev/null
            if [[ $diff_result -eq 0 ]]; then
                echo "No changes in ${git_path}" >& /dev/null
            else
                echo "There are changes in ${git_path}" >& /dev/null
                git_directories+=("$git_path")
            fi
        fi
    done

    # print as result the unique list of git directories affected
    printf %s\\n "${git_directories[@]}" | sort | uniq
}

# Insert a file into the middle of another, starting at the line containing the
# start delim and ending on the end delim, both of which are replaced
function insert_file() {
    local src_part="$1"
    local dst_file="$2"
    local start_delim="/*@O~"
    local end_delim="~O@*/"

    local start_line="$(grep -n -F "${start_delim}" "${dst_file}" | cut -d: -f1)"
    local end_line="$(grep -n -F "${end_delim}" "${dst_file}" | cut -d: -f1)"

    # Adjust cutoff points to use start/end line from inserted file
    (( start_line-- ))
    (( end_line++ ))

    # Do some basic sanity checks

    if [[ -z "$start_line" ]]; then
       echo "No starting delimiter found in ${dst_file}" >& 2
       echo "FAIL: Errors in inserting into $(relpath ${dst_file})" >& 2
       return 1
    fi

    if [[ -z "$end_line" ]]; then
       echo "No ending delimiter found in ${dst_file}" >& 2
       echo "FAIL: Errors in inserting into $(relpath ${dst_file})" >& 2
       return 1
    fi

    if [[ "$start_line" -ge "$end_line" ]]; then
       echo "Starting delim later than ending delim: $start_line vs $end_line" >& 2
       echo "FAIL: Errors in inserting into $(relpath ${dst_file})" >& 2
       return 1
    fi

    local tmp_name=$(mktemp -t XXXXXXXX)

    # Compose the three parts of the final file together

    head -n "$start_line" "${dst_file}" > "${tmp_name}"
    cat "${src_part}" >> "${tmp_name}"
    tail -n "+${end_line}" "${dst_file}" >> "${tmp_name}"

    # And replace the destination file with the new version

    mv "${tmp_name}" "${dst_file}"
    echo "OK: Inserted $(relpath "$src_part") into $(relpath "$dst_file")"
    out_files+=$'\n'" $dst_file"
}

$thisdir/metadata-check-dependencies || exit 1
$thisdir/metadata-validate $thisdir/metadata_properties.xml || exit 1
$thisdir/metadata-parser-sanity-check || exit 1

# Generate HTML properties documentation
gen_file html.mako docs.html || exit 1

# Generate C API headers and implementation
gen_file camera_metadata_tag_info.mako ../src/camera_metadata_tag_info.c || exit 1
gen_file camera_metadata_tags.mako ../include/system/camera_metadata_tags.h || exit 1

# Generate Java API definitions
mkdir -p "${outdir}"
gen_file_abs CameraMetadataEnums.mako "$outdir/CameraMetadataEnums.java.part" no || exit 1
gen_file_abs CameraCharacteristicsKeys.mako "$outdir/CameraCharacteristicsKeys.java.part" no || exit 1
gen_file_abs CaptureRequestKeys.mako "$outdir/CaptureRequestKeys.java.part" no || exit 1
gen_file_abs CaptureResultKeys.mako "$outdir/CaptureResultKeys.java.part" no || exit 1
gen_file_abs CameraCaptureResultTest.mako "$outdir/CameraCaptureResultTest.java.part" no || exit 1

insert_file "$outdir/CameraMetadataEnums.java.part" "$fwkdir/CameraMetadata.java" || exit 1
insert_file "$outdir/CameraCharacteristicsKeys.java.part" "$fwkdir/CameraCharacteristics.java" || exit 1
insert_file "$outdir/CaptureRequestKeys.java.part" "$fwkdir/CaptureRequest.java" || exit 1
insert_file "$outdir/CaptureResultKeys.java.part" "$fwkdir/CaptureResult.java" || exit 1

# Generate CTS tests
gen_file_abs CameraCharacteristicsTest.mako "$ctsdir/CameraCharacteristicsTest.java" || exit 1
insert_file "$outdir/CameraCaptureResultTest.java.part" "$ctsdir/CameraCaptureResultTest.java" || exit 1

echo ""
echo "===================================================="
echo "Successfully generated all metadata source files"
echo "===================================================="
echo ""

echo "****************************************************"
echo "The following git repositories need to be committed:"
echo "****************************************************"
echo ""
affected_git_directories "${out_files[@]}"
echo ""

exit 0