1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-12 16:22:22 +01:00

add clang-tidy for zephyr

This commit is contained in:
Tomasz Duda
2024-07-24 13:35:07 +02:00
parent 35385bb0c8
commit 05f3b71007
2 changed files with 201 additions and 51 deletions

View File

@@ -1,21 +1,6 @@
#!/usr/bin/env python3
from helpers import (
print_error_for_file,
get_output,
filter_grep,
build_all_include,
temp_header_file,
git_ls_files,
filter_changed,
load_idedata,
root_path,
basepath,
get_binary,
)
import argparse
import click
import colorama
import multiprocessing
import os
import queue
@@ -26,6 +11,20 @@ import sys
import tempfile
import threading
import click
import colorama
from helpers import (
basepath,
build_all_include,
filter_changed,
filter_grep,
get_binary,
git_ls_files,
load_idedata,
print_error_for_file,
root_path,
temp_header_file,
)
def clang_options(idedata):
@@ -40,12 +39,40 @@ def clang_options(idedata):
else:
cmd.append(f"--target={triplet}")
omit_flags = (
"-free",
"-fipa-pta",
"-fstrict-volatile-bitfields",
"-mlongcalls",
"-mtext-section-literals",
"-mfix-esp32-psram-cache-issue",
"-mfix-esp32-psram-cache-strategy=memw",
"-fno-tree-switch-conversion",
)
if "zephyr" in triplet:
omit_flags += (
"-fno-printf-return-value",
"-fno-reorder-functions",
"-format-zero-length",
"-mfp16-format=ieee",
"-std=c99",
"-fno-defer-pop",
"--param=min-pagesize=0",
"--specs=picolibc.specs",
)
else:
cmd.extend(
[
"-nostdinc++",
]
)
# set flags
cmd.extend(
[
# disable built-in include directories from the host
"-nostdinc",
"-nostdinc++",
# replace pgmspace.h, as it uses GNU extensions clang doesn't support
# https://github.com/earlephilhower/newlib-xtensa/pull/18
"-D_PGMSPACE_H_",
@@ -72,21 +99,7 @@ def clang_options(idedata):
)
# copy compiler flags, except those clang doesn't understand.
cmd.extend(
flag
for flag in idedata["cxx_flags"]
if flag
not in (
"-free",
"-fipa-pta",
"-fstrict-volatile-bitfields",
"-mlongcalls",
"-mtext-section-literals",
"-mfix-esp32-psram-cache-issue",
"-mfix-esp32-psram-cache-strategy=memw",
"-fno-tree-switch-conversion",
)
)
cmd.extend(flag for flag in idedata["cxx_flags"] if flag not in omit_flags)
# defines
cmd.extend(f"-D{define}" for define in idedata["defines"])
@@ -105,6 +118,7 @@ def clang_options(idedata):
not directory.startswith(f"{root_path}/")
or directory.startswith(f"{root_path}/.pio/")
or directory.startswith(f"{root_path}/managed_components/")
or "zephyr/include/generated" in directory
):
cmd.extend(["-isystem", directory])
@@ -116,9 +130,10 @@ def clang_options(idedata):
pids = set()
def run_tidy(executable, args, options, tmpdir, queue, lock, failed_files):
def run_tidy(executable, args, options, tmpdir, path_queue, lock, failed_files):
while True:
path = queue.get()
path = path_queue.get()
invocation = [executable]
if tmpdir is not None:
@@ -140,17 +155,20 @@ def run_tidy(executable, args, options, tmpdir, queue, lock, failed_files):
invocation.append("--")
invocation.extend(options)
proc = subprocess.run(invocation, capture_output=True, encoding="utf-8")
proc = subprocess.run(
invocation, capture_output=True, encoding="utf-8", check=False
)
if proc.returncode != 0:
with lock:
print_error_for_file(path, proc.stdout)
failed_files.append(path)
queue.task_done()
path_queue.task_done()
def progress_bar_show(value):
if value is None:
return ""
return None
def split_list(a, n):
@@ -238,7 +256,15 @@ def main():
for _ in range(args.jobs):
t = threading.Thread(
target=run_tidy,
args=(executable, args, options, tmpdir, task_queue, lock, failed_files),
args=(
executable,
args,
options,
tmpdir,
task_queue,
lock,
failed_files,
),
)
t.daemon = True
t.start()
@@ -246,14 +272,14 @@ def main():
# Fill the queue with files.
with click.progressbar(
files, width=30, file=sys.stderr, item_show_func=progress_bar_show
) as bar:
for name in bar:
) as progress_bar:
for name in progress_bar:
task_queue.put(name)
# Wait for all threads to be done.
task_queue.join()
except FileNotFoundError as ex:
except FileNotFoundError:
return 1
except KeyboardInterrupt:
print()
@@ -263,7 +289,7 @@ def main():
# Kill subprocesses (and ourselves!)
# No simple, clean alternative appears to be available.
os.kill(0, 9)
return 2 # Will not execute.
return 2 # Will not execute.
if args.fix and failed_files:
print("Applying fixes ...")
@@ -273,7 +299,10 @@ def main():
except FileNotFoundError:
subprocess.call(["clang-apply-replacements", tmpdir])
except FileNotFoundError:
print("Error please install clang-apply-replacements-14 or clang-apply-replacements.\n", file=sys.stderr)
print(
"Error please install clang-apply-replacements-14 or clang-apply-replacements.\n",
file=sys.stderr,
)
except:
print("Error applying fixes.\n", file=sys.stderr)
raise