diff --git a/bytebeat_compiler.py b/bytebeat_compiler.py index 9150a70..3823f2d 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 subprocess @@ -86,12 +87,27 @@ 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") if extra := fetch("CFLAGS_EXTRA"): CFLAGS += " " + extra +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, " @@ -238,10 +254,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), @@ -251,4 +285,5 @@ if __name__ == "__main__": "-I" + PATHS["include_directory"] ] print(" ".join(command), flush=True) + exit(subprocess.run(command).returncode)