|
| 1 | +#!/usr/bin/env -S python3 -B |
| 2 | + |
| 3 | +# Copyright (c) 2025 Project CHIP Authors |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +import logging |
| 17 | + |
| 18 | +import click |
| 19 | +import coloredlogs |
| 20 | +import jinja2 |
| 21 | + |
| 22 | +# Supported log levels, mapping string values required for argument |
| 23 | +# parsing into logging constants |
| 24 | +__LOG_LEVELS__ = { |
| 25 | + "debug": logging.DEBUG, |
| 26 | + "info": logging.INFO, |
| 27 | + "warn": logging.WARN, |
| 28 | + "fatal": logging.FATAL, |
| 29 | +} |
| 30 | + |
| 31 | +# This repesents the code that is generated according to |
| 32 | +# https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#using-the-profiling-runtime-without-static-initializers |
| 33 | +# |
| 34 | +# So that the output of coverage is customized and does not go to `default.profraw` |
| 35 | +_CPP_TEMPLATE = """\ |
| 36 | +extern "C" { |
| 37 | +
|
| 38 | +int __llvm_profile_runtime = 0; |
| 39 | +void __llvm_profile_initialize_file(); |
| 40 | +void __llvm_profile_write_file(); |
| 41 | +void __llvm_profile_set_filename(const char *); |
| 42 | +
|
| 43 | +} // extern "C" |
| 44 | +
|
| 45 | +struct StaticInitLLVMProfile { |
| 46 | + StaticInitLLVMProfile() { |
| 47 | + __llvm_profile_set_filename("{{raw_profile_filename}}"); |
| 48 | + } |
| 49 | + ~StaticInitLLVMProfile() { |
| 50 | + __llvm_profile_write_file(); |
| 51 | + } |
| 52 | +} gInitLLVMProfilingPaths; |
| 53 | +""" |
| 54 | + |
| 55 | + |
| 56 | +@click.command() |
| 57 | +@click.option( |
| 58 | + "--log-level", |
| 59 | + default="INFO", |
| 60 | + type=click.Choice([k for k in __LOG_LEVELS__.keys()], case_sensitive=False), |
| 61 | + help="Determines the verbosity of script output.", |
| 62 | +) |
| 63 | +@click.option( |
| 64 | + "--no-log-timestamps", |
| 65 | + default=False, |
| 66 | + is_flag=True, |
| 67 | + help="Skip timestaps in log output", |
| 68 | +) |
| 69 | +@click.option( |
| 70 | + "--output", |
| 71 | + help="What file to output when runnning under clang profiling", |
| 72 | +) |
| 73 | +@click.option( |
| 74 | + "--raw-profile-filename", |
| 75 | + help="Filename to use for output", |
| 76 | +) |
| 77 | +def main(log_level, no_log_timestamps, output, raw_profile_filename): |
| 78 | + log_fmt = "%(asctime)s %(levelname)-7s %(message)s" |
| 79 | + if no_log_timestamps: |
| 80 | + log_fmt = "%(levelname)-7s %(message)s" |
| 81 | + coloredlogs.install(level=__LOG_LEVELS__[log_level], fmt=log_fmt) |
| 82 | + |
| 83 | + logging.info("Writing output to %s (profile name: %s)", output, raw_profile_filename) |
| 84 | + with open(output, "wt") as f: |
| 85 | + f.write(jinja2.Template(_CPP_TEMPLATE).render(raw_profile_filename=raw_profile_filename)) |
| 86 | + |
| 87 | + logging.debug("Writing completed") |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + main(auto_envvar_prefix="CHIP") |
0 commit comments