From dc07adbd41940e5c0541177af3cba668540d85ea Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 20 Jun 2023 09:44:25 -0700 Subject: [PATCH] hopefully fixed formatting with older clang-format versions --- .efrocachemap | 32 ++++---- .idea/dictionaries/ericf.xml | 1 + .../.idea/dictionaries/ericf.xml | 1 + tools/efrotools/code.py | 73 +++++++++++++------ 4 files changed, 67 insertions(+), 40 deletions(-) diff --git a/.efrocachemap b/.efrocachemap index f43654bb..7041dc69 100644 --- a/.efrocachemap +++ b/.efrocachemap @@ -4080,18 +4080,18 @@ "build/prefab/full/linux_x86_64_gui/release/ballisticakit": "https://files.ballistica.net/cache/ba1/1a/b6/9196bf7553c8380ddd37c8aba83f", "build/prefab/full/linux_x86_64_server/debug/dist/ballisticakit_headless": "https://files.ballistica.net/cache/ba1/24/56/34bbdafa6ed8d8505fa50b40e10a", "build/prefab/full/linux_x86_64_server/release/dist/ballisticakit_headless": "https://files.ballistica.net/cache/ba1/0c/9b/6dbedf9c32a5591fbc0724a7b050", - "build/prefab/full/mac_arm64_gui/debug/ballisticakit": "https://files.ballistica.net/cache/ba1/7f/5c/e76c46dbf6405298c04d5d76a254", + "build/prefab/full/mac_arm64_gui/debug/ballisticakit": "https://files.ballistica.net/cache/ba1/29/a3/32a6f6eaa811627ab7903243288a", "build/prefab/full/mac_arm64_gui/release/ballisticakit": "https://files.ballistica.net/cache/ba1/16/3e/5a02a80c844ea71c3ce1f229ff25", - "build/prefab/full/mac_arm64_server/debug/dist/ballisticakit_headless": "https://files.ballistica.net/cache/ba1/37/05/bae4f69c549764238b06c9083214", + "build/prefab/full/mac_arm64_server/debug/dist/ballisticakit_headless": "https://files.ballistica.net/cache/ba1/ad/5a/ee514a1edeba59f6a2a57691781e", "build/prefab/full/mac_arm64_server/release/dist/ballisticakit_headless": "https://files.ballistica.net/cache/ba1/c3/d0/4fc44285b21e6b3dc4d9b3c30a28", - "build/prefab/full/mac_x86_64_gui/debug/ballisticakit": "https://files.ballistica.net/cache/ba1/37/4f/bc7e504838a5691e928977c3479f", + "build/prefab/full/mac_x86_64_gui/debug/ballisticakit": "https://files.ballistica.net/cache/ba1/94/f4/e92a82cd5b74df65386dd00b58d8", "build/prefab/full/mac_x86_64_gui/release/ballisticakit": "https://files.ballistica.net/cache/ba1/a5/5f/1c69d9dab37d811da0cdde48d09f", - "build/prefab/full/mac_x86_64_server/debug/dist/ballisticakit_headless": "https://files.ballistica.net/cache/ba1/30/f5/82ec511ed3a2f96fb91b89e73464", + "build/prefab/full/mac_x86_64_server/debug/dist/ballisticakit_headless": "https://files.ballistica.net/cache/ba1/2e/4f/86fa037ee8d9899ce9c7b490d06b", "build/prefab/full/mac_x86_64_server/release/dist/ballisticakit_headless": "https://files.ballistica.net/cache/ba1/b8/3b/b2f787a50ed7a2271b0c65f0ab3e", - "build/prefab/full/windows_x86_gui/debug/BallisticaKit.exe": "https://files.ballistica.net/cache/ba1/49/5b/14ecf8ecd2bcfe789da5c70d0535", - "build/prefab/full/windows_x86_gui/release/BallisticaKit.exe": "https://files.ballistica.net/cache/ba1/c7/8b/6c8d8d2af28adb80192f266fe682", - "build/prefab/full/windows_x86_server/debug/dist/BallisticaKitHeadless.exe": "https://files.ballistica.net/cache/ba1/08/8b/129ca92c135796fd8abfd7353953", - "build/prefab/full/windows_x86_server/release/dist/BallisticaKitHeadless.exe": "https://files.ballistica.net/cache/ba1/39/9b/f81ca02ace3dcbc5c3838c859a4c", + "build/prefab/full/windows_x86_gui/debug/BallisticaKit.exe": "https://files.ballistica.net/cache/ba1/b6/f6/945d3031ffd5112fa925366fc2e6", + "build/prefab/full/windows_x86_gui/release/BallisticaKit.exe": "https://files.ballistica.net/cache/ba1/ab/05/b93b5e9d547cacd4b47fb79d6022", + "build/prefab/full/windows_x86_server/debug/dist/BallisticaKitHeadless.exe": "https://files.ballistica.net/cache/ba1/d8/3e/14b4ba0ee4d62520b5e809b9fea6", + "build/prefab/full/windows_x86_server/release/dist/BallisticaKitHeadless.exe": "https://files.ballistica.net/cache/ba1/c6/fb/a894f1c2445628fcfe3d95bbb908", "build/prefab/lib/linux_arm64_gui/debug/libballistica_plus.a": "https://files.ballistica.net/cache/ba1/8a/39/82acd22444cdde966df4838b3235", "build/prefab/lib/linux_arm64_gui/release/libballistica_plus.a": "https://files.ballistica.net/cache/ba1/82/23/3da9e812f6d9d83e9c4678645792", "build/prefab/lib/linux_arm64_server/debug/libballistica_plus.a": "https://files.ballistica.net/cache/ba1/8a/39/82acd22444cdde966df4838b3235", @@ -4108,14 +4108,14 @@ "build/prefab/lib/mac_x86_64_gui/release/libballistica_plus.a": "https://files.ballistica.net/cache/ba1/2b/1d/067ca5d1bc54339f2b8682ef1aed", "build/prefab/lib/mac_x86_64_server/debug/libballistica_plus.a": "https://files.ballistica.net/cache/ba1/65/49/cdbaca8ea343d79992618514438c", "build/prefab/lib/mac_x86_64_server/release/libballistica_plus.a": "https://files.ballistica.net/cache/ba1/2b/1d/067ca5d1bc54339f2b8682ef1aed", - "build/prefab/lib/windows/Debug_Win32/BallisticaKitGenericPlus.lib": "https://files.ballistica.net/cache/ba1/cb/95/775c7f0e22358331958cb78d8314", - "build/prefab/lib/windows/Debug_Win32/BallisticaKitGenericPlus.pdb": "https://files.ballistica.net/cache/ba1/69/37/3d76aa863c941f419af93ad1829e", - "build/prefab/lib/windows/Debug_Win32/BallisticaKitHeadlessPlus.lib": "https://files.ballistica.net/cache/ba1/82/ef/41673a5c02236cc11c1f4f8350fb", - "build/prefab/lib/windows/Debug_Win32/BallisticaKitHeadlessPlus.pdb": "https://files.ballistica.net/cache/ba1/99/a5/348f2f2a413be98ee368a0561708", - "build/prefab/lib/windows/Release_Win32/BallisticaKitGenericPlus.lib": "https://files.ballistica.net/cache/ba1/ad/9b/a895e163036f1ee37259c131afcd", - "build/prefab/lib/windows/Release_Win32/BallisticaKitGenericPlus.pdb": "https://files.ballistica.net/cache/ba1/6e/4b/e14ae214177713878fc34879220f", - "build/prefab/lib/windows/Release_Win32/BallisticaKitHeadlessPlus.lib": "https://files.ballistica.net/cache/ba1/91/ee/e4663ce72ae13a695b57a9ad38c9", - "build/prefab/lib/windows/Release_Win32/BallisticaKitHeadlessPlus.pdb": "https://files.ballistica.net/cache/ba1/34/ad/150df6dca4b7277b9817e954ecd8", + "build/prefab/lib/windows/Debug_Win32/BallisticaKitGenericPlus.lib": "https://files.ballistica.net/cache/ba1/dc/2e/2c3cd9cb11f8def5bb82b776708c", + "build/prefab/lib/windows/Debug_Win32/BallisticaKitGenericPlus.pdb": "https://files.ballistica.net/cache/ba1/41/8f/17d9b796e1b2dcd3d10372a5a239", + "build/prefab/lib/windows/Debug_Win32/BallisticaKitHeadlessPlus.lib": "https://files.ballistica.net/cache/ba1/7b/d1/a173f7d45eee76bfa0f0324d6b11", + "build/prefab/lib/windows/Debug_Win32/BallisticaKitHeadlessPlus.pdb": "https://files.ballistica.net/cache/ba1/69/e2/f9b659c214b720b823cfa30137e8", + "build/prefab/lib/windows/Release_Win32/BallisticaKitGenericPlus.lib": "https://files.ballistica.net/cache/ba1/7b/87/90863866f7402fbfbf386e2d1898", + "build/prefab/lib/windows/Release_Win32/BallisticaKitGenericPlus.pdb": "https://files.ballistica.net/cache/ba1/4a/b0/7bf4e032cd3b6510c6eb2eb25044", + "build/prefab/lib/windows/Release_Win32/BallisticaKitHeadlessPlus.lib": "https://files.ballistica.net/cache/ba1/8c/48/5a8e5335043aca743e7a03901522", + "build/prefab/lib/windows/Release_Win32/BallisticaKitHeadlessPlus.pdb": "https://files.ballistica.net/cache/ba1/d6/51/26c2f8c6aba2736ee9947ce00e48", "src/assets/ba_data/python/babase/_mgen/__init__.py": "https://files.ballistica.net/cache/ba1/f8/85/fed7f2ed98ff2ba271f9dbe3391c", "src/assets/ba_data/python/babase/_mgen/enums.py": "https://files.ballistica.net/cache/ba1/48/4b/e6974f0a4d14be8213dc00d971c3", "src/ballistica/base/mgen/pyembed/binding_base.inc": "https://files.ballistica.net/cache/ba1/3e/7a/203e2a5d2b5bb42cfe3fd2fe16c2", diff --git a/.idea/dictionaries/ericf.xml b/.idea/dictionaries/ericf.xml index 530d4899..92b54a80 100644 --- a/.idea/dictionaries/ericf.xml +++ b/.idea/dictionaries/ericf.xml @@ -437,6 +437,7 @@ certifi certpass cfconfig + cfconfigdata cfenv cfgdict cfgdir diff --git a/ballisticakit-cmake/.idea/dictionaries/ericf.xml b/ballisticakit-cmake/.idea/dictionaries/ericf.xml index fb851d5b..f8c809c9 100644 --- a/ballisticakit-cmake/.idea/dictionaries/ericf.xml +++ b/ballisticakit-cmake/.idea/dictionaries/ericf.xml @@ -287,6 +287,7 @@ certfile certifi certpass + cfconfigdata cfgdict cfgdir cfgpath diff --git a/tools/efrotools/code.py b/tools/efrotools/code.py index 4b6cada4..97b2e005 100644 --- a/tools/efrotools/code.py +++ b/tools/efrotools/code.py @@ -7,10 +7,15 @@ from __future__ import annotations import os import sys +import time +import tempfile +import datetime import subprocess from pathlib import Path from typing import TYPE_CHECKING +from efro.error import CleanError + from efrotools.filecache import FileCache @@ -26,33 +31,65 @@ def format_cpp_str( Note that some cpp formatting keys off the filename, so a fake one can be optionally provided. """ - import tempfile + + # Note: previously was explicitly passing the config path from + # toolconfigsrc so we didn't require running from the project root + # dir, but this doesn't work on older clang-format versions. So now + # just parsing and passing the values itself which should do the + # same thing. Once most people have newer clang-format we can go + # back to just passing the path. + use_built_config = True + + cfconfig = os.path.join(projroot, '.clang-format') + + if not os.path.isfile(cfconfig): + raise CleanError( + f".clang-format file not found in '{projroot}';" + " do 'make prereqs' to generate it." + ) with tempfile.TemporaryDirectory() as tempdir: filename = os.path.join(tempdir, filename) with open(filename, 'w', encoding='utf-8') as outfile: outfile.write(text) - cfg = os.path.join(projroot, 'config/toolconfigsrc/clang-format') - subprocess.run( - ['clang-format', f'--style=file:{cfg}', '-i', filename], check=True - ) + if use_built_config: + import yaml + import json + + # clang-format uses yaml but seems passing our raw yaml + # config contents doesn't work; converting to json seems to + # work though. + with open(cfconfig, encoding='utf-8') as infile: + cfconfigdata = json.dumps(yaml.safe_load(infile.read())) + style_arg = f'--style={cfconfigdata}' + else: + style_arg = f'--style=file:{cfconfig}' + subprocess.run(['clang-format', style_arg, '-i', filename], check=True) with open(filename, encoding='utf-8') as infile: return infile.read() def format_project_cpp_files(projroot: Path, full: bool) -> None: """Run clang-format on all of our source code (multithreaded).""" - import time import concurrent.futures from multiprocessing import cpu_count + from efrotools import get_files_hash - os.chdir(projroot) + if os.path.abspath(projroot) != os.getcwd(): + raise RuntimeError('We expect to be running from project root.') + cachepath = Path(projroot, '.cache/format_project_cpp_files') if full and cachepath.exists(): cachepath.unlink() cache = FileCache(cachepath) - cfconfig = Path(projroot, '.clang-format') + cfconfig = '.clang-format' + + if not os.path.isfile(cfconfig): + raise CleanError( + f".clang-format file not found in '{os.getcwd()}';" + " do 'make prereqs' to generate it." + ) # Exclude generated files or else we could mess up dependencies # by mucking with their modtimes. @@ -99,7 +136,6 @@ def check_cpplint(projroot: Path, full: bool) -> None: from efrotools import getconfig, PYVER from efro.terminal import Clr - from efro.error import CleanError os.chdir(projroot) filenames = get_code_filenames(projroot, include_generated=True) @@ -232,7 +268,6 @@ def black_base_args() -> list[str]: def format_project_python_files(projroot: Path, full: bool) -> None: """Runs formatting on all of our Python code.""" from efrotools import get_string_hash - from efro.error import CleanError os.chdir(projroot) cachepath = Path(projroot, '.cache/format_project_python_files') @@ -471,9 +506,7 @@ def _run_pylint( dirtyfiles: list[str], allfiles: list[str] | None, ) -> dict[str, Any]: - import time from pylint import lint - from efro.error import CleanError from efro.terminal import Clr start_time = time.time() @@ -521,9 +554,10 @@ def _apply_pylint_run_to_cache( # pylint: disable=too-many-locals # pylint: disable=too-many-branches # pylint: disable=too-many-statements + from astroid import modutils + from efrotools import getconfig - from efro.error import CleanError # First off, build a map of dirtyfiles to module names # (and the corresponding reverse map). @@ -752,9 +786,7 @@ def mypy_files( def mypy(projroot: Path, full: bool) -> None: """Type check all of our scripts using mypy.""" - import time from efro.terminal import Clr - from efro.error import CleanError filenames = get_script_filenames(projroot) desc = '(full)' if full else '(incremental)' @@ -772,9 +804,7 @@ def mypy(projroot: Path, full: bool) -> None: def dmypy(projroot: Path) -> None: """Type check all of our scripts using mypy in daemon mode.""" - import time from efro.terminal import Clr - from efro.error import CleanError filenames = get_script_filenames(projroot) @@ -855,10 +885,7 @@ def _run_idea_inspections( """ # pylint: disable=too-many-locals # pylint: disable=consider-using-with - import tempfile - import time - import datetime - from efro.error import CleanError + from efro.terminal import Clr start_time = time.time() @@ -933,6 +960,7 @@ def _run_idea_inspections_cached( # pylint: disable=too-many-locals import hashlib import json + from efro.terminal import Clr md5 = hashlib.md5() @@ -985,8 +1013,6 @@ def _run_idea_inspections_cached( def check_pycharm(projroot: Path, full: bool, verbose: bool) -> None: """Run pycharm inspections on all our scripts.""" - import time - # FIXME: Generalize this to work with at least linux, possibly windows. cachepath = Path('.cache/check_pycharm') filenames = get_script_filenames(projroot) @@ -1047,7 +1073,6 @@ def check_pycharm(projroot: Path, full: bool, verbose: bool) -> None: def check_clioncode(projroot: Path, full: bool, verbose: bool) -> None: """Run clion inspections on all our code.""" - import time cachepath = Path('.cache/check_clioncode') filenames = get_code_filenames(projroot, include_generated=True)