diff --git a/justfile b/justfile index d6912e2..59926d9 100644 --- a/justfile +++ b/justfile @@ -15,5 +15,19 @@ count-file: # you need download `tokei` it can download by cargo tokei libs runtime src -e tests --files -build_lexer: - python ./tools/cbuild/cbuild.py --path libs/lexer build +clean: + cbuild clean + +build: + cbuild build -cclang + +build-install: build + cp ./build/dev/scc.exe ./scc.exe + +test-scc: + # windows: (Get-Content a.txt -Raw) -replace '\x1b\[[0-9;]*[a-zA-Z]', '' | Set-Content clean.txt + # linux: sed 's/\x1b\[[0-9;]*[a-zA-Z]//g' a.txt > clean.txt + just clean + just build-install + just clean + cbuild run -cscc -- -h diff --git a/tools/cbuild/cbuild.py b/tools/cbuild/cbuild.py index c8b8d51..6b1088e 100644 --- a/tools/cbuild/cbuild.py +++ b/tools/cbuild/cbuild.py @@ -1,4 +1,4 @@ -"""cbuild.py - 优化的轻量C构建系统""" +"""cbuild.py - 优化的轻量C构建系统""" # pylint: disable=too-many-lines from abc import ABC, abstractmethod import tomllib @@ -517,7 +517,7 @@ class BuildCache: with open(self.cache_file, "r", encoding="utf-8") as f: data = json.load(f) self.cache = {k: CacheEntry(**v) for k, v in data.items()} - except OSError, json.JSONDecodeError, TypeError: + except (OSError, json.JSONDecodeError, TypeError): self.cache = {} def save(self): @@ -679,6 +679,24 @@ class ClangCompiler(Compiler): self.run(cmd) +class SccCompiler(Compiler): + """SCC编译器""" + + def get_flags(self, mode: BuildMode) -> list[str]: + return [] + + def compile( + self, source: Path, output: Path, includes: list[Path], flags: list[str] + ): + # cmd = ["clang"] + flags + ["-c", str(source), "-o", str(output)] + cmd = ["scc", "--emit-pp", "-o", str(output), str(source)] + cmd += [f"-I{inc}" for inc in includes] + self.run(cmd) + + def link(self, objects: list[Path], output: Path, flags: list[str]): + pass + + class DummyCompiler(Compiler): """虚拟编译器(用于测试)""" @@ -1023,7 +1041,11 @@ def create_parser(): def add_common_args(subparser): subparser.add_argument( - "--compiler", "-c", choices=["gcc", "clang"], default="gcc", help="编译器" + "--compiler", + "-c", + choices=["gcc", "clang", "scc"], + default="gcc", + help="编译器", ) subparser.add_argument("--record", "-r", action="store_true", help="记录命令") subparser.add_argument( @@ -1102,6 +1124,7 @@ def create_parser(): def main(): """主函数""" + # print("current cwd: " + os.getcwd()) parser = create_parser() args = parser.parse_args() @@ -1117,6 +1140,7 @@ def main(): compiler_map = { "gcc": GccCompiler(), "clang": ClangCompiler(), + "scc": SccCompiler(), } compiler = compiler_map.get(args.compiler, GccCompiler()) diff --git a/tools/wc.py b/tools/wc.py deleted file mode 100644 index be37635..0000000 --- a/tools/wc.py +++ /dev/null @@ -1,53 +0,0 @@ -"""统计目录下C/C++文件的行数(write by AI)""" - -import os - - -def count_lines(file_path): - """统计单个文件的代码行数""" - try: - with open(file_path, "rb") as f: # 二进制模式读取避免编码问题 - return sum(1 for _ in f) - except UnicodeDecodeError: - print(f"警告:无法解码文件 {file_path}(可能不是文本文件)") - return 0 - except Exception as e: - print(f"读取 {file_path} 出错: {str(e)}") - return 0 - - -def scan_files(directory, exclude_dirs=None): - """扫描目录获取所有C/C++文件""" - if exclude_dirs is None: - exclude_dirs = [".git", "venv", "__pycache__", ".old"] # 默认排除的目录 - - c_files = [] - for root, dirs, files in os.walk(directory): - # 跳过排除目录 - dirs[:] = [d for d in dirs if d not in exclude_dirs] - - for file in files: - if file.endswith((".c", ".h")): - full_path = os.path.join(root, file) - c_files.append(full_path) - return c_files - - -def main(): - """main function""" - target_dir = input("请输入要扫描的目录路径(留空为当前目录): ") or "." - - files = scan_files(target_dir) - total_lines = 0 - - print("\n统计结果:") - for idx, file in enumerate(files, 1): - lines = count_lines(file) - total_lines += lines - print(f"{idx:4d}. {file} ({lines} 行)") - - print(f"\n总计: {len(files)} 个C/C++文件,共 {total_lines} 行代码") - - -if __name__ == "__main__": - main()