From ca282d20c86b52cc041e284d185eea7fc91c801b Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Sun, 2 Jun 2024 19:34:49 +0300 Subject: [PATCH] bytebeat_compiler.py: search for compiler, fix section names and reformat --- bytebeat_compiler.py | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/bytebeat_compiler.py b/bytebeat_compiler.py index 25642dc..a46376b 100644 --- a/bytebeat_compiler.py +++ b/bytebeat_compiler.py @@ -4,9 +4,10 @@ if __name__ == "__main__": print(":: C bytebeat generator: compiler unit") from argparse import ArgumentParser -from os import environ, makedirs +from os import environ, makedirs, name as os_name from os.path import exists, join as path_join from shlex import split as command_line_split +from shutil import which from sys import stdin, stdout, exit from typing import Dict, Union import re @@ -87,6 +88,18 @@ preprocessor_bool = lambda value: "1" if value else "0" C_str_repr = lambda s: '"' + s.replace("\\", "\\\\").replace(r'"', r'\"') + '"' CC = fetch("CC") + +CC_SEARCH_LIST = [ + "gcc", + "clang", + "tcc" +] +if os_name == "nt": + CC_SEARCH_LIST = [ + "msc", + *CC_SEARCH_LIST + ] + CFLAGS = fetch("CFLAGS") INPUT_FILE = fetch("INPUT_FILE") OUTPUT_FILE = fetch("OUTPUT_FILE") @@ -113,6 +126,9 @@ DEFAULT_ARGS = { "no_return": False } +is_cmd_available = lambda cmd: which(cmd) is not None +is_cmd_unavailable = lambda cmd: which(cmd) is None + if __name__ == "__main__": parser = ArgumentParser(description=\ "Substitutes supplied C (non-JavaScript!) bytebeat into the template, " @@ -333,10 +349,28 @@ if __name__ == "__main__": "ansi_escape_codes_supported": ansi_escape_codes_supported }, read_file(PATHS["template"]), args.show_substituted_values)) - # Compile by invoking the shell script + if is_cmd_unavailable(CC): + print(f"Compiler {CC} not available, searching:") + + still_unavailable = True + for compiler in CC_SEARCH_LIST: + print(f"* Trying CC={compiler}", end="") + if is_cmd_available(compiler): + print(": OK") + CC = compiler + still_unavailable = False + break + else: + print() + + if still_unavailable: + print("Could not find an available compiler. Please specify it by " + "setting\nenvironmental variable CC.") + exit(2) + + # Compile print("Compiling") - # Let the system execute aliases by calling os.system command = [ CC, *command_line_split(CFLAGS), @@ -346,4 +380,5 @@ if __name__ == "__main__": "-I" + PATHS["include_directory"] ] print(" ".join(command), flush=True) + exit(subprocess.run(command).returncode)