codepregen.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2022 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 enum
  16. import itertools
  17. import logging
  18. import multiprocessing
  19. import os
  20. import sys
  21. import click
  22. try:
  23. from pregenerate import FindPregenerationTargets, TargetFilter
  24. except:
  25. import os
  26. sys.path.append(os.path.abspath(os.path.dirname(__file__)))
  27. from pregenerate import FindPregenerationTargets, TargetFilter
  28. from pregenerate.executors import DryRunner, ShellRunner
  29. from pregenerate.types import IdlFileType
  30. try:
  31. import coloredlogs
  32. _has_coloredlogs = True
  33. except:
  34. _has_coloredlogs = False
  35. # Supported log levels, mapping string values required for argument
  36. # parsing into logging constants
  37. __LOG_LEVELS__ = {
  38. 'debug': logging.DEBUG,
  39. 'info': logging.INFO,
  40. 'warn': logging.WARN,
  41. 'fatal': logging.FATAL,
  42. }
  43. def _ParallelGenerateOne(arg):
  44. """
  45. Helper method to be passed to multiprocessing parallel generation of
  46. items.
  47. """
  48. arg[0].Generate(arg[1])
  49. @click.command()
  50. @click.option(
  51. '--log-level',
  52. default='INFO',
  53. type=click.Choice(__LOG_LEVELS__.keys(), case_sensitive=False),
  54. help='Determines the verbosity of script output')
  55. @click.option(
  56. '--parallel/--no-parallel',
  57. default=True,
  58. help='Do parallel/multiprocessing codegen.')
  59. @click.option(
  60. '--dry-run/--no-dry-run',
  61. default=False,
  62. help='Do not actually execute commands, just log')
  63. @click.option(
  64. '--generator',
  65. default='all',
  66. type=click.Choice(['all', 'zap', 'codegen']),
  67. help='To what code generator to restrict the generation.')
  68. @click.option(
  69. '--input-glob',
  70. default=None,
  71. multiple=True,
  72. help='Restrict file generation inputs to the specified glob patterns.')
  73. @click.option(
  74. '--sdk-root',
  75. default=None,
  76. help='Path to the SDK root (where .zap/.matter files exist).')
  77. @click.option(
  78. '--external-root',
  79. default=None,
  80. multiple=True,
  81. help='Path to an external app root (where .zap/.matter files exist).')
  82. @click.argument('output_dir')
  83. def main(log_level, parallel, dry_run, generator, input_glob, sdk_root, external_root, output_dir):
  84. if _has_coloredlogs:
  85. coloredlogs.install(level=__LOG_LEVELS__[
  86. log_level], fmt='%(asctime)s %(levelname)-7s %(message)s')
  87. else:
  88. logging.basicConfig(
  89. level=__LOG_LEVELS__[log_level],
  90. format='%(asctime)s %(levelname)-7s %(message)s',
  91. datefmt='%Y-%m-%d %H:%M:%S'
  92. )
  93. if not sdk_root:
  94. sdk_root = os.path.join(os.path.dirname(
  95. os.path.realpath(__file__)), '..')
  96. sdk_root = os.path.abspath(sdk_root)
  97. if not output_dir:
  98. raise Exception("Missing output directory")
  99. output_dir = os.path.abspath(output_dir)
  100. logging.info(f"Pre-generating {sdk_root} data into {output_dir}")
  101. if not dry_run:
  102. runner = ShellRunner()
  103. else:
  104. runner = DryRunner()
  105. filter = TargetFilter(path_glob=input_glob)
  106. if generator == 'zap':
  107. filter.file_type = IdlFileType.ZAP
  108. elif generator == 'codegen':
  109. filter.file_type = IdlFileType.MATTER
  110. targets = FindPregenerationTargets(sdk_root, external_root, filter, runner)
  111. runner.ensure_directory_exists(output_dir)
  112. if parallel:
  113. target_and_dir = zip(targets, itertools.repeat(output_dir))
  114. with multiprocessing.Pool() as pool:
  115. for _ in pool.imap_unordered(_ParallelGenerateOne, target_and_dir):
  116. pass
  117. else:
  118. for target in targets:
  119. target.Generate(output_dir)
  120. logging.info("Done")
  121. if __name__ == '__main__':
  122. main()