4 # Copyright (c) 2020 Project CHIP Authors
5 # Copyright (c) 2019 Google LLC.
6 # Copyright (c) 2014-2017 Nest Labs, Inc.
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
13 # http://www.apache.org/licenses/LICENSE-2.0
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
24 # This file implements a script to parse a version number from
25 # the command line, split it into its constituent components, and
26 # then auto-generate a C-language header file containing
27 # preprocessor mnemonics for those components.
34 # This routine prints out the proper command line usage for this
38 # status - Flag determining what usage information will be printed and what
39 # the exit status of the program will be after the information is
46 # This subroutine does not return.
52 ((status)) && echo "Error: $me: $*
53 Try '$me --help' for more information." >&2
55 echo "Usage: $me [ options ... ] [ -o OUTPUT ] <version>" >&2
57 ((!status)) && echo "General Options:
59 -h, --help Display this information.
61 Input and Output Options:
63 -o, --output PATH Specify PATH as the output file to write with the
64 generated ouput (default: standard output).
73 # Parse options from the command line
85 usage 1 "unknown option \"$1\""
93 # At this point, the version to parse should be the only argument.
94 ((${#@} < 1)) && usage 2 please pass a version on the command line
95 ((${#@} > 1)) && usage 2 too many arguments
97 declare chip_version="$1"
99 # Establish the copyright year and script base name scalar variables
100 # for substitution in the here document output.
104 declare copyright_year
106 current_year="$(date "+%Y")"
107 copyright_year="$first_year"
109 ((first_year < current_year)) && {
110 copyright_year+="-$current_year"
118 # Parse the version into its constituent components.
119 read -r chip_major chip_minor chip_patch chip_extra <<<"${chip_version//./ }"
121 # preserve the "." in any extra
122 [[ -n ${chip_extra} ]] && chip_extra=".$chip_extra"
124 # Major and minor should be numbers. If they're not use the full input version as
125 # the "extra" and set everything else back to 0
126 [ "$chip_major" -eq "$chip_major" -a "$chip_minor" -eq "$chip_minor" ] 2>/dev/null || {
130 chip_extra=.$chip_version
133 # If chip_patch isn't a number, set chip_patch to zero and push the non-number back into chip_extra
134 [ "$chip_patch" -eq "$chip_patch" ] || {
135 chip_extra=.$chip_patch$chip_extra
139 # Canonically recompose the version from its components. zero-patchlevel is no patchlevel
140 chip_version=$chip_major.$chip_minor
142 # Zero patchlevel means no patchlevel
143 ((chip_patch)) && chip_version+=.$chip_patch
145 chip_version+=$chip_extra
147 # Initialize the output stream, if is a filename
148 [[ -n $outfile ]] && exec >"$outfile"
150 # Generate the output file as a "here document" with
151 # variable interpolation (escaping where necessary).
155 * Copyright (c) ${copyright_year} Project CHIP Authors
156 * All rights reserved.
158 * Licensed under the Apache License, Version 2.0 (the "License");
159 * you may not use this file except in compliance with the License.
160 * You may obtain a copy of the License at
162 * http://www.apache.org/licenses/LICENSE-2.0
164 * Unless required by applicable law or agreed to in writing, software
165 * distributed under the License is distributed on an "AS IS" BASIS,
166 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
167 * See the License for the specific language governing permissions and
168 * limitations under the License.
173 * This file defines constants and macros for introspecting and
174 * manipulating CHIP versions.
176 * !!! WARNING !!! WARNING !!! WARNING !!! WARNING !!!
178 * DO NOT EDIT THIS FILE! This file is automatically-generated by
179 * the '${me}' script.
181 * The constants and macros defined here may be used to, for ,
182 * example, conditionally-compile older, newer, or changed CHIP
183 * APIs based on the CHIP version. For example:
186 * #if CHIP_VERSION_CODE >= CHIP_VERSION_CODE_ENCODE(1, 5, 0)
195 #ifndef CHIP_VERSION_H_
196 #define CHIP_VERSION_H_
198 #define _CHIP_VERSION_CODE_MAJOR_WIDTH 8
199 #define _CHIP_VERSION_CODE_MINOR_WIDTH 8
200 #define _CHIP_VERSION_CODE_PATCH_WIDTH 8
202 #define _CHIP_VERSION_CODE_MAJOR_MASK ((1 << _CHIP_VERSION_CODE_MAJOR_WIDTH) - 1)
203 #define _CHIP_VERSION_CODE_MINOR_MASK ((1 << _CHIP_VERSION_CODE_MINOR_WIDTH) - 1)
204 #define _CHIP_VERSION_CODE_PATCH_MASK ((1 << _CHIP_VERSION_CODE_PATCH_WIDTH) - 1)
206 #define _CHIP_VERSION_CODE_MAJOR_SHIFT 24
207 #define _CHIP_VERSION_CODE_MINOR_SHIFT 16
208 #define _CHIP_VERSION_CODE_PATCH_SHIFT 8
211 * @def CHIP_VERSION_CODE_ENCODE(major, minor, patch)
214 * Encode a CHIP version code from its constituent @a major, @a minor, and @a patch
217 * This macro may be used in conjunction with CHIP_VERSION_CODE to, for
218 * example, conditionally-compile older, newer, or changed CHIP APIs based
219 * on the CHIP version. For example:
222 * #if CHIP_VERSION_CODE >= CHIP_VERSION_CODE_ENCODE(1, 5, 0)
230 #define CHIP_VERSION_CODE_ENCODE(major, minor, patch) \\
231 ((((major) & _CHIP_VERSION_CODE_MAJOR_MASK) << _CHIP_VERSION_CODE_MAJOR_SHIFT) | \\
232 (((minor) & _CHIP_VERSION_CODE_MINOR_MASK) << _CHIP_VERSION_CODE_MINOR_SHIFT) | \\
233 (((patch) & _CHIP_VERSION_CODE_PATCH_MASK) << _CHIP_VERSION_CODE_PATCH_SHIFT))
236 * @def CHIP_VERSION_CODE_DECODE_MAJOR(code)
239 * Decode a CHIP major version component from a CHIP version @a code.
242 #define CHIP_VERSION_CODE_DECODE_MAJOR(code) (((code) >> _CHIP_VERSION_CODE_MAJOR_SHIFT) & _CHIP_VERSION_CODE_MAJOR_MASK)
245 * @def CHIP_VERSION_CODE_DECODE_MINOR(code)
248 * Decode a CHIP minor version component from a CHIP version @a code.
251 #define CHIP_VERSION_CODE_DECODE_MINOR(code) (((code) >> _CHIP_VERSION_CODE_MINOR_SHIFT) & _CHIP_VERSION_CODE_MINOR_MASK)
254 * @def CHIP_VERSION_CODE_DECODE_PATCH(code)
257 * Decode a CHIP patch version component from a CHIP version @a code.
260 #define CHIP_VERSION_CODE_DECODE_PATCH(code) (((code) >> _CHIP_VERSION_CODE_PATCH_SHIFT) & _CHIP_VERSION_CODE_PATCH_MASK)
263 * @def CHIP_VERSION_MAJOR
266 * The CHIP version major component, as an unsigned integer.
269 #define CHIP_VERSION_MAJOR ${chip_major}
272 * @def CHIP_VERSION_MINOR
275 * The CHIP version minor component, as an unsigned integer.
278 #define CHIP_VERSION_MINOR ${chip_minor}
281 * @def CHIP_VERSION_PATCH
284 * The CHIP version patch component, as an unsigned integer.
287 #define CHIP_VERSION_PATCH ${chip_patch}
290 * @def CHIP_VERSION_EXTRA
293 * The CHIP version extra component, as a quoted C string.
296 #define CHIP_VERSION_EXTRA "${chip_extra}"
299 * @def CHIP_VERSION_STRING
302 * The CHIP version, as a quoted C string.
305 #define CHIP_VERSION_STRING "${chip_version}"
308 * @def CHIP_VERSION_CODE
311 * The CHIP version, including the major, minor, and patch components,
312 * encoded as an unsigned integer.
314 * This macro may be used in conjunction with CHIP_VERSION_CODE_ENCODE
315 * to, for example, conditionally-compile older, newer, or changed CHIP
316 * APIs based on the CHIP version. For example:
319 * #if CHIP_VERSION_CODE >= CHIP_VERSION_CODE_ENCODE(1, 5, 0)
327 #define CHIP_VERSION_CODE CHIP_VERSION_CODE_ENCODE( \\
328 CHIP_VERSION_MAJOR, \\
329 CHIP_VERSION_MINOR, \\
330 CHIP_VERSION_PATCH \\
333 #endif /* CHIP_VERSION_H_ */