Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / scripts / gen-chip-version.sh
1 #!/usr/bin/env bash
2
3 #
4 #    Copyright (c) 2020 Project CHIP Authors
5 #    Copyright (c) 2019 Google LLC.
6 #    Copyright (c) 2014-2017 Nest Labs, Inc.
7 #    All rights reserved.
8 #
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
12 #
13 #        http://www.apache.org/licenses/LICENSE-2.0
14 #
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.
20 #
21
22 #
23 #    Description:
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.
28 #
29
30 #
31 # usage
32 #
33 # Description:
34 #   This routine prints out the proper command line usage for this
35 #   program
36 #
37 # Input(s):
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
40 #            printed.
41 #
42 # Output(s):
43 #   N/A
44 #
45 # Returns:
46 #   This subroutine does not return.
47 #
48 usage() {
49     declare status="$1"
50     shift
51
52     ((status)) && echo "Error: $me: $*
53 Try '$me --help' for more information." >&2
54
55     echo "Usage: $me [ options ... ] [ -o OUTPUT ] <version>" >&2
56
57     ((!status)) && echo "General Options:
58
59   -h, --help                 Display this information.
60
61 Input and Output Options:
62
63   -o, --output PATH          Specify PATH as the output file to write with the
64                              generated ouput (default: standard output).
65 "
66     exit "$status"
67 }
68
69 me=${0##*/}
70
71 declare outfile=
72
73 # Parse options from the command line
74 while true; do
75     case $1 in
76         --help | -h)
77             usage 0
78             ;;
79         --output | -o)
80             shift
81             outfile=$1
82             shift
83             ;;
84         -*)
85             usage 1 "unknown option \"$1\""
86             ;;
87         *)
88             break
89             ;;
90     esac
91 done
92
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
96
97 declare chip_version="$1"
98
99 # Establish the copyright year and script base name scalar variables
100 # for substitution in the here document output.
101
102 declare first_year
103 declare current_year
104 declare copyright_year
105 first_year=2020
106 current_year="$(date "+%Y")"
107 copyright_year="$first_year"
108
109 ((first_year < current_year)) && {
110     copyright_year+="-$current_year"
111 }
112
113 declare chip_major
114 declare chip_minor
115 declare chip_patch
116 declare chip_extra
117
118 # Parse the version into its constituent components.
119 read -r chip_major chip_minor chip_patch chip_extra <<<"${chip_version//./ }"
120
121 # preserve the "." in any extra
122 [[ -n ${chip_extra} ]] && chip_extra=".$chip_extra"
123
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 || {
127     chip_major=0
128     chip_minor=0
129     chip_patch=0
130     chip_extra=.$chip_version
131 }
132
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
136     chip_patch=0
137 }
138
139 # Canonically recompose the version from its components.  zero-patchlevel is no patchlevel
140 chip_version=$chip_major.$chip_minor
141
142 # Zero patchlevel means no patchlevel
143 ((chip_patch)) && chip_version+=.$chip_patch
144
145 chip_version+=$chip_extra
146
147 # Initialize the output stream, if is a filename
148 [[ -n $outfile ]] && exec >"$outfile"
149
150 # Generate the output file as a "here document" with
151 # variable interpolation (escaping where necessary).
152 cat <<EOF
153 /*
154  *
155  *    Copyright (c) ${copyright_year} Project CHIP Authors
156  *    All rights reserved.
157  *
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
161  *
162  *        http://www.apache.org/licenses/LICENSE-2.0
163  *
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.
169  */
170
171 /**
172  *    @file
173  *      This file defines constants and macros for introspecting and
174  *      manipulating CHIP versions.
175  *
176  *      !!! WARNING !!! WARNING !!! WARNING !!! WARNING !!!
177  *
178  *      DO NOT EDIT THIS FILE! This file is automatically-generated by
179  *      the '${me}' script.
180  *
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:
184  *
185  *          @code
186  *          #if CHIP_VERSION_CODE >= CHIP_VERSION_CODE_ENCODE(1, 5, 0)
187  *              ...
188  *          #else
189  *              ...
190  *          #endif
191  *          @endcode
192  *
193  */
194
195 #ifndef CHIP_VERSION_H_
196 #define CHIP_VERSION_H_
197
198 #define _CHIP_VERSION_CODE_MAJOR_WIDTH     8
199 #define _CHIP_VERSION_CODE_MINOR_WIDTH     8
200 #define _CHIP_VERSION_CODE_PATCH_WIDTH     8
201
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)
205
206 #define _CHIP_VERSION_CODE_MAJOR_SHIFT    24
207 #define _CHIP_VERSION_CODE_MINOR_SHIFT    16
208 #define _CHIP_VERSION_CODE_PATCH_SHIFT     8
209
210 /**
211  *  @def CHIP_VERSION_CODE_ENCODE(major, minor, patch)
212  *
213  *  @brief
214  *    Encode a CHIP version code from its constituent @a major, @a minor, and @a patch
215  *    components.
216  *
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:
220  *
221  *        @code
222  *        #if CHIP_VERSION_CODE >= CHIP_VERSION_CODE_ENCODE(1, 5, 0)
223  *            ...
224  *        #else
225  *            ...
226  *        #endif
227  *        @endcode
228  *
229  */
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))
234
235 /**
236  *  @def CHIP_VERSION_CODE_DECODE_MAJOR(code)
237  *
238  *  @brief
239  *    Decode a CHIP major version component from a CHIP version @a code.
240  *
241  */
242 #define CHIP_VERSION_CODE_DECODE_MAJOR(code)  (((code) >> _CHIP_VERSION_CODE_MAJOR_SHIFT)  & _CHIP_VERSION_CODE_MAJOR_MASK)
243
244 /**
245  *  @def CHIP_VERSION_CODE_DECODE_MINOR(code)
246  *
247  *  @brief
248  *    Decode a CHIP minor version component from a CHIP version @a code.
249  *
250  */
251 #define CHIP_VERSION_CODE_DECODE_MINOR(code)  (((code) >> _CHIP_VERSION_CODE_MINOR_SHIFT)  & _CHIP_VERSION_CODE_MINOR_MASK)
252
253 /**
254  *  @def CHIP_VERSION_CODE_DECODE_PATCH(code)
255  *
256  *  @brief
257  *    Decode a CHIP patch version component from a CHIP version @a code.
258  *
259  */
260 #define CHIP_VERSION_CODE_DECODE_PATCH(code)  (((code) >> _CHIP_VERSION_CODE_PATCH_SHIFT)  & _CHIP_VERSION_CODE_PATCH_MASK)
261
262 /**
263  *  @def CHIP_VERSION_MAJOR
264  *
265  *  @brief
266  *    The CHIP version major component, as an unsigned integer.
267  *
268  */
269 #define CHIP_VERSION_MAJOR               ${chip_major}
270
271 /**
272  *  @def CHIP_VERSION_MINOR
273  *
274  *  @brief
275  *    The CHIP version minor component, as an unsigned integer.
276  *
277  */
278 #define CHIP_VERSION_MINOR               ${chip_minor}
279
280 /**
281  *  @def CHIP_VERSION_PATCH
282  *
283  *  @brief
284  *    The CHIP version patch component, as an unsigned integer.
285  *
286  */
287 #define CHIP_VERSION_PATCH               ${chip_patch}
288
289 /**
290  *  @def CHIP_VERSION_EXTRA
291  *
292  *  @brief
293  *    The CHIP version extra component, as a quoted C string.
294  *
295  */
296 #define CHIP_VERSION_EXTRA               "${chip_extra}"
297
298 /**
299  *  @def CHIP_VERSION_STRING
300  *
301  *  @brief
302  *    The CHIP version, as a quoted C string.
303  *
304  */
305 #define CHIP_VERSION_STRING              "${chip_version}"
306
307 /**
308  *  @def CHIP_VERSION_CODE
309  *
310  *  @brief
311  *    The CHIP version, including the major, minor, and patch components,
312  *    encoded as an unsigned integer.
313  *
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:
317  *
318  *        @code
319  *        #if CHIP_VERSION_CODE >= CHIP_VERSION_CODE_ENCODE(1, 5, 0)
320  *            ...
321  *        #else
322  *            ...
323  *        #endif
324  *        @endcode
325  *
326  */
327 #define CHIP_VERSION_CODE                CHIP_VERSION_CODE_ENCODE( \\
328           CHIP_VERSION_MAJOR,                                      \\
329           CHIP_VERSION_MINOR,                                      \\
330           CHIP_VERSION_PATCH                                       \\
331         )
332
333 #endif /* CHIP_VERSION_H_ */
334 EOF