aced/make.py

112 lines
2.9 KiB
Python
Raw Permalink Normal View History

2023-04-20 16:03:54 +00:00
import os
import shutil
import subprocess as sp
from sane import _Help as Sane
from glob import glob
from sane import *
2023-05-01 16:18:32 +00:00
COMPILER = "gcc"
COMPILE_FLAGS = [
"-std=c11",
2023-05-01 19:00:23 +00:00
"-g3",
2023-05-01 16:18:32 +00:00
"-O0",
"-Wall",
"-Wextra",
"-Werror",
"-Wpedantic",
"-pedantic-errors",
"-fopenmp",
]
LINK_FLAGS = ["-fopenmp"]
LIBRARIES = ["-lm"]
OBJ_DIR = os.path.join("obj", "release" if ("RELEASE" in os.environ) else "debug")
2023-05-01 16:18:32 +00:00
SRC_DIR = "src"
EXE_NAME = "main." + ("release" if ("RELEASE" in os.environ) else "debug") + ".exe"
2023-04-20 16:03:54 +00:00
ROOT = os.path.dirname(os.path.realpath(__file__))
2023-05-01 16:18:32 +00:00
if "RELEASE" in os.environ:
COMPILE_FLAGS[COMPILE_FLAGS.index("-O0")] = "-O2"
COMPILE_FLAGS[COMPILE_FLAGS.index("-g3")] = "-g"
COMPILE_FLAGS.append("-DRELEASE")
LINK_FLAGS.append("-DRELEASE")
if "COMPILE_FLAGS" in os.environ:
COMPILE_FLAGS.extend(os.environ["COMPILE_FLAGS"].split(";"))
if "LINK_FLAGS" in os.environ:
LINK_FLAGS.extend(os.environ["LINK_FLAGS"].split(";"))
2023-05-01 16:18:32 +00:00
2023-04-20 16:03:54 +00:00
def as_object(source_path):
"""Takes a source path and returns the path to the corresponding compiled object."""
source_path = os.path.relpath(source_path, ROOT)
2023-05-01 16:18:32 +00:00
object_name = source_path + ".obj"
2023-04-20 16:03:54 +00:00
object_path = os.path.join(OBJ_DIR, object_name)
return object_path
2023-05-01 16:18:32 +00:00
2023-04-20 16:03:54 +00:00
def make_object_recipe(source_path):
object_path = os.path.realpath(as_object(source_path))
object_dir = os.path.dirname(object_path)
if not os.path.exists(object_dir):
os.makedirs(object_dir)
2023-05-01 16:18:32 +00:00
condition = Sane.file_condition(sources=[source_path], targets=[object_path])
2023-04-20 16:03:54 +00:00
2023-05-01 16:18:32 +00:00
@recipe(
name=f"compile_{source_path}", conditions=[condition], hooks=["obj_compile"]
)
2023-04-20 16:03:54 +00:00
def compile_obj():
2023-05-01 16:18:32 +00:00
sp.run([COMPILER, "-c", *COMPILE_FLAGS, "-o", object_path, source_path])
2023-04-20 16:03:54 +00:00
2023-05-01 16:18:32 +00:00
source_files = [os.path.realpath(path) for path in glob("**/*.c", recursive=True)]
2023-04-20 16:03:54 +00:00
for source_file in source_files:
make_object_recipe(source_file)
2023-05-01 16:18:32 +00:00
2023-04-20 16:03:54 +00:00
def exe_not_exists():
return not os.path.exists(EXE_NAME)
2023-05-01 16:18:32 +00:00
def old_exe():
2023-05-01 16:18:32 +00:00
object_files = [os.path.realpath(path) for path in glob("**/*.obj", recursive=True)]
return Sane.file_condition(object_files, [EXE_NAME])()
2023-04-20 16:03:54 +00:00
2023-05-01 16:18:32 +00:00
@recipe(hook_deps=["obj_compile"], info="Compile the source files.")
2023-04-20 16:03:54 +00:00
def compile():
pass
2023-05-01 16:18:32 +00:00
@recipe(
recipe_deps=[compile],
conditions=[exe_not_exists, old_exe],
info="Links the main executable.",
)
2023-04-20 16:03:54 +00:00
def link():
2023-05-01 16:18:32 +00:00
obj_files = glob(
os.path.join(OBJ_DIR, "**", "*.obj").replace("\\", "/"), recursive=True
)
sp.run([COMPILER, *LINK_FLAGS, "-o", EXE_NAME, *obj_files, *LIBRARIES])
2023-05-01 16:18:32 +00:00
2023-04-20 16:03:54 +00:00
2023-05-01 16:18:32 +00:00
@recipe(info="Removes all compiled objects.")
2023-04-20 16:03:54 +00:00
def clean():
shutil.rmtree(OBJ_DIR)
2023-05-01 16:18:32 +00:00
@recipe(recipe_deps=[link], conditions=[lambda: True])
def run():
with open(os.path.join(ROOT, "data/2222_inq.txt"), "r") as input:
exe = os.path.join(ROOT, EXE_NAME)
sp.run(
[exe, "2", "2", "2", "2"],
2023-05-01 16:18:32 +00:00
stdin=input,
)
2023-04-27 15:28:45 +00:00
sane_run(link)