gen_chip_version.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #!/usr/bin/env python
  2. # Copyright (c) 2020 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import optparse
  16. import sys
  17. TEMPLATE = r'''/*
  18. *
  19. * Copyright (c) 2020 Project CHIP Authors
  20. * All rights reserved.
  21. *
  22. * Licensed under the Apache License, Version 2.0 (the "License");
  23. * you may not use this file except in compliance with the License.
  24. * You may obtain a copy of the License at
  25. *
  26. * http://www.apache.org/licenses/LICENSE-2.0
  27. *
  28. * Unless required by applicable law or agreed to in writing, software
  29. * distributed under the License is distributed on an "AS IS" BASIS,
  30. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  31. * See the License for the specific language governing permissions and
  32. * limitations under the License.
  33. */
  34. /**
  35. * \@file
  36. * This file defines constants and macros for introspecting and
  37. * manipulating CHIP versions.
  38. *
  39. * !!! WARNING !!! WARNING !!! WARNING !!! WARNING !!!
  40. *
  41. * DO NOT EDIT THIS FILE! This file is automatically-generated by
  42. * the '${program}' script.
  43. *
  44. * The constants and macros defined here may be used to, for ,
  45. * example, conditionally-compile older, newer, or changed CHIP
  46. * APIs based on the CHIP version. For example:
  47. *
  48. * \@code
  49. * #if CHIP_VERSION_CODE >= CHIP_VERSION_CODE_ENCODE(1, 5, 0)
  50. * ...
  51. * #else
  52. * ...
  53. * #endif
  54. * \@endcode
  55. *
  56. */
  57. #ifndef CHIP_VERSION_H_
  58. #define CHIP_VERSION_H_
  59. #define _CHIP_VERSION_CODE_MAJOR_WIDTH 8
  60. #define _CHIP_VERSION_CODE_MINOR_WIDTH 8
  61. #define _CHIP_VERSION_CODE_PATCH_WIDTH 8
  62. #define _CHIP_VERSION_CODE_MAJOR_MASK ((1 << _CHIP_VERSION_CODE_MAJOR_WIDTH) - 1)
  63. #define _CHIP_VERSION_CODE_MINOR_MASK ((1 << _CHIP_VERSION_CODE_MINOR_WIDTH) - 1)
  64. #define _CHIP_VERSION_CODE_PATCH_MASK ((1 << _CHIP_VERSION_CODE_PATCH_WIDTH) - 1)
  65. #define _CHIP_VERSION_CODE_MAJOR_SHIFT 24
  66. #define _CHIP_VERSION_CODE_MINOR_SHIFT 16
  67. #define _CHIP_VERSION_CODE_PATCH_SHIFT 8
  68. /**
  69. * \@def CHIP_VERSION_CODE_ENCODE(major, minor, patch)
  70. *
  71. * \@brief
  72. * Encode a CHIP version code from its constituent \@a major, \@a minor, and \@a patch
  73. * components.
  74. *
  75. * This macro may be used in conjunction with CHIP_VERSION_CODE to, for
  76. * example, conditionally-compile older, newer, or changed CHIP APIs based
  77. * on the CHIP version. For example:
  78. *
  79. * \@code
  80. * #if CHIP_VERSION_CODE >= CHIP_VERSION_CODE_ENCODE(1, 5, 0)
  81. * ...
  82. * #else
  83. * ...
  84. * #endif
  85. * \@endcode
  86. *
  87. */
  88. #define CHIP_VERSION_CODE_ENCODE(major, minor, patch) \
  89. ((((major) & _CHIP_VERSION_CODE_MAJOR_MASK) << _CHIP_VERSION_CODE_MAJOR_SHIFT) | \
  90. (((minor) & _CHIP_VERSION_CODE_MINOR_MASK) << _CHIP_VERSION_CODE_MINOR_SHIFT) | \
  91. (((patch) & _CHIP_VERSION_CODE_PATCH_MASK) << _CHIP_VERSION_CODE_PATCH_SHIFT))
  92. /**
  93. * \@def CHIP_VERSION_CODE_DECODE_MAJOR(code)
  94. *
  95. * \@brief
  96. * Decode a CHIP major version component from a CHIP version \@a code.
  97. *
  98. */
  99. #define CHIP_VERSION_CODE_DECODE_MAJOR(code) (((code) >> _CHIP_VERSION_CODE_MAJOR_SHIFT) & _CHIP_VERSION_CODE_MAJOR_MASK)
  100. /**
  101. * \@def CHIP_VERSION_CODE_DECODE_MINOR(code)
  102. *
  103. * \@brief
  104. * Decode a CHIP minor version component from a CHIP version \@a code.
  105. *
  106. */
  107. #define CHIP_VERSION_CODE_DECODE_MINOR(code) (((code) >> _CHIP_VERSION_CODE_MINOR_SHIFT) & _CHIP_VERSION_CODE_MINOR_MASK)
  108. /**
  109. * \@def CHIP_VERSION_CODE_DECODE_PATCH(code)
  110. *
  111. * \@brief
  112. * Decode a CHIP patch version component from a CHIP version \@a code.
  113. *
  114. */
  115. #define CHIP_VERSION_CODE_DECODE_PATCH(code) (((code) >> _CHIP_VERSION_CODE_PATCH_SHIFT) & _CHIP_VERSION_CODE_PATCH_MASK)
  116. /**
  117. * \@def CHIP_VERSION_MAJOR
  118. *
  119. * \@brief
  120. * The CHIP version major component, as an unsigned integer.
  121. *
  122. */
  123. #define CHIP_VERSION_MAJOR %(chip_major)d
  124. /**
  125. * \@def CHIP_VERSION_MINOR
  126. *
  127. * \@brief
  128. * The CHIP version minor component, as an unsigned integer.
  129. *
  130. */
  131. #define CHIP_VERSION_MINOR %(chip_minor)d
  132. /**
  133. * \@def CHIP_VERSION_PATCH
  134. *
  135. * \@brief
  136. * The CHIP version patch component, as an unsigned integer.
  137. *
  138. */
  139. #define CHIP_VERSION_PATCH %(chip_patch)d
  140. /**
  141. * \@def CHIP_VERSION_EXTRA
  142. *
  143. * \@brief
  144. * The CHIP version extra component, as a quoted C string.
  145. *
  146. */
  147. #define CHIP_VERSION_EXTRA "%(chip_extra)s"
  148. /**
  149. * \@def CHIP_VERSION_STRING
  150. *
  151. * \@brief
  152. * The CHIP version, as a quoted C string.
  153. *
  154. */
  155. #define CHIP_VERSION_STRING "%(chip_version)s"
  156. /**
  157. * \@def CHIP_VERSION_CODE
  158. *
  159. * \@brief
  160. * The CHIP version, including the major, minor, and patch components,
  161. * encoded as an unsigned integer.
  162. *
  163. * This macro may be used in conjunction with CHIP_VERSION_CODE_ENCODE
  164. * to, for example, conditionally-compile older, newer, or changed CHIP
  165. * APIs based on the CHIP version. For example:
  166. *
  167. * \@code
  168. * #if CHIP_VERSION_CODE >= CHIP_VERSION_CODE_ENCODE(1, 5, 0)
  169. * ...
  170. * #else
  171. * ...
  172. * #endif
  173. * \@endcode
  174. *
  175. */
  176. #define CHIP_VERSION_CODE CHIP_VERSION_CODE_ENCODE( \
  177. CHIP_VERSION_MAJOR, \
  178. CHIP_VERSION_MINOR, \
  179. CHIP_VERSION_PATCH \
  180. )
  181. #endif /* CHIP_VERSION_H_ */
  182. '''
  183. def main(argv):
  184. parser = optparse.OptionParser()
  185. parser.add_option('--output_file')
  186. parser.add_option('--chip_major', type=int, default=0)
  187. parser.add_option('--chip_minor', type=int, default=0)
  188. parser.add_option('--chip_patch', type=int, default=0)
  189. parser.add_option('--chip_extra', type=str, default='')
  190. options, _ = parser.parse_args(argv)
  191. template_args = {
  192. 'chip_major': options.chip_major,
  193. 'chip_minor': options.chip_minor,
  194. 'chip_patch': options.chip_patch,
  195. 'chip_extra': options.chip_extra,
  196. }
  197. template_args['chip_version'] = '%d.%d.%d%s' % (
  198. options.chip_major, options.chip_minor, options.chip_patch, options.chip_extra)
  199. with open(options.output_file, 'w') as chip_version_file:
  200. chip_version_file.write(TEMPLATE % template_args)
  201. return 0
  202. if __name__ == '__main__':
  203. sys.exit(main(sys.argv[1:]))