b/c...py: compile into a temporary directory
Generate intermediate files into a temporary directory
This commit is contained in:
parent
81782ef23d
commit
0ab521e2ca
@ -4,12 +4,12 @@ if __name__ == "__main__":
|
||||
print(":: C bytebeat generator: compiler unit")
|
||||
|
||||
from argparse import ArgumentParser
|
||||
from os import environ, listdir, makedirs, name as os_name, \
|
||||
remove as delete_file, rmdir
|
||||
from os.path import exists, join as path_join
|
||||
from os import environ, makedirs, name as os_name, rename
|
||||
from os.path import basename, exists, join as path_join
|
||||
from shlex import join as command_line_join, split as command_line_split
|
||||
from shutil import which
|
||||
from sys import stdin, stdout
|
||||
from tempfile import TemporaryDirectory
|
||||
from typing import Dict, Union
|
||||
import re
|
||||
import subprocess
|
||||
@ -33,8 +33,8 @@ PATHS = {
|
||||
|
||||
# Resolve paths
|
||||
PATHS["template"] = path_join(PATHS["src_dir"], PATHS["template"])
|
||||
PATHS["substitute"] = path_join(PATHS["bin_dir"], PATHS["substitute"])
|
||||
PATHS["output"] = path_join(PATHS["bin_dir"], PATHS["output"])
|
||||
PATHS["substitute_kept"] = path_join(PATHS["bin_dir"], PATHS["substitute"])
|
||||
PATHS["output_kept"] = path_join(PATHS["bin_dir"], PATHS["output"])
|
||||
PATHS["fwrite_le"] = path_join(PATHS["src_dir"], PATHS["fwrite_le"])
|
||||
|
||||
# Add `.` directory before all paths for compilation
|
||||
@ -48,8 +48,8 @@ DEFAULT_PARAMETERS = {
|
||||
"CFLAGS": "-Ofast -march=native -mtune=native -Wall -Wextra -Wpedantic "
|
||||
"-pedantic -Wno-unused-variable -Wno-unused-but-set-variable "
|
||||
"-Wno-dangling-else -Wno-parentheses -std=c99",
|
||||
"INPUT_FILE": PATHS["substitute"],
|
||||
"OUTPUT_FILE": PATHS["output"]
|
||||
"INPUT_FILE": PATHS["substitute_kept"],
|
||||
"OUTPUT_FILE": PATHS["output_kept"]
|
||||
}
|
||||
|
||||
stdout_atty = hasattr(stdout, "isatty") and stdout.isatty()
|
||||
@ -95,10 +95,6 @@ def run_command(*command: list[str]) -> None:
|
||||
if subprocess.run(command).returncode != EXIT_SUCCESS:
|
||||
raise SystemExit(EXIT_FAILURE)
|
||||
|
||||
def delete_empty_dir(path: str) -> None:
|
||||
if exists(path) and len(listdir(path)) == 0:
|
||||
rmdir(path)
|
||||
|
||||
preprocessor_bool = lambda value: "1" if value else "0"
|
||||
C_str_repr = lambda s: '"' + s.replace("\\", "\\\\").replace(r'"', r'\"') + '"'
|
||||
|
||||
@ -251,8 +247,6 @@ if __name__ == "__main__":
|
||||
args.signed = False
|
||||
|
||||
# - Compilation
|
||||
makedirs(PATHS["bin_dir"], exist_ok=True)
|
||||
|
||||
if not args.no_return: # Insert `return` statement
|
||||
# XXX: The bytebeat code is enclosed in parentheses to allow for the
|
||||
# use of commas as a comma operator, enabling more formulas to function.
|
||||
@ -337,7 +331,30 @@ if __name__ == "__main__":
|
||||
gen_length = length_formula(args.channels, samples, 0)
|
||||
loop_end = length_formula(args.channels, samples, skip_first_samples)
|
||||
|
||||
rewrite_file(PATHS["substitute"], substitute_vars({
|
||||
if is_cmd_unavailable(CC):
|
||||
print(f"Compiler {CC} is 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:
|
||||
raise SystemExit("Could not find an available compiler. Please "
|
||||
"specify it by setting\nan environmental variable "
|
||||
"CC.")
|
||||
|
||||
with TemporaryDirectory() as tmpdirname:
|
||||
temporary_path = lambda path: path_join(tmpdirname, basename(path))
|
||||
|
||||
substitute_temp = temporary_path(INPUT_FILE)
|
||||
rewrite_file(substitute_temp, substitute_vars({
|
||||
"bytebeat_contents": bytebeat_contents,
|
||||
"output_file": C_str_repr(args.output),
|
||||
"sample_rate": actual_sample_rate,
|
||||
@ -364,39 +381,23 @@ if __name__ == "__main__":
|
||||
"ansi_escape_codes_supported": ansi_escape_codes_supported
|
||||
}, read_file(PATHS["template"]), args.show_substituted_values))
|
||||
|
||||
if is_cmd_unavailable(CC):
|
||||
print(f"Compiler {CC} is 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:
|
||||
raise SystemExit("Could not find an available compiler. Please "
|
||||
"specify it by setting\nan environmental variable "
|
||||
"CC.")
|
||||
|
||||
# Compile
|
||||
print("Compiling")
|
||||
|
||||
output_file_temp = temporary_path(OUTPUT_FILE)
|
||||
|
||||
run_command(
|
||||
CC,
|
||||
*command_line_split(CFLAGS),
|
||||
INPUT_FILE,
|
||||
substitute_temp,
|
||||
PATHS["fwrite_le"],
|
||||
"-o", OUTPUT_FILE,
|
||||
"-o", output_file_temp,
|
||||
"-I" + PATHS["include_directory"]
|
||||
)
|
||||
run_command(OUTPUT_FILE)
|
||||
run_command(output_file_temp)
|
||||
|
||||
if not args.keep_files:
|
||||
delete_file(PATHS["substitute"])
|
||||
delete_file(OUTPUT_FILE)
|
||||
delete_empty_dir(PATHS["bin_dir"])
|
||||
if args.keep_files:
|
||||
makedirs(PATHS["bin_dir"], exist_ok=True)
|
||||
|
||||
rename(substitute_temp, INPUT_FILE)
|
||||
rename(output_file_temp, OUTPUT_FILE)
|
||||
|
Loading…
Reference in New Issue
Block a user