aced/make.py

83 lines
2.1 KiB
Python

import os
import shutil
import subprocess as sp
from sane import _Help as Sane
from glob import glob
from sane import *
COMPILER = 'gcc'
COMPILE_FLAGS = ['-g',
'-Wall',
'--std=c99',
'-fopenmp']
LINK_FLAGS = []
OBJ_DIR = 'obj'
SRC_DIR = 'src'
EXE_NAME = 'main.exe'
ROOT = os.path.dirname(os.path.realpath(__file__))
if 'RELEASE' in os.environ:
COMPILE_FLAGS.append('-O2')
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)
object_name = source_path + '.obj'
object_path = os.path.join(OBJ_DIR, object_name)
return object_path
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)
condition = Sane.file_condition(
sources=[source_path],
targets=[object_path])
@recipe(name=f'compile_{source_path}',
conditions=[condition],
hooks=['obj_compile'])
def compile_obj():
sp.run([COMPILER, *COMPILE_FLAGS,
'-c', source_path,
'-o', object_path],
shell=True)
source_files = glob('**/*.c', recursive=True)
for source_file in source_files:
make_object_recipe(source_file)
def exe_not_exists():
return not os.path.exists(EXE_NAME)
@recipe(hook_deps=['obj_compile'],
info='Compile the source files.')
def compile():
pass
@recipe(recipe_deps=[compile],
conditions=[exe_not_exists],
info='Links the main executable.')
def link():
obj_files = glob(os.path.join(OBJ_DIR, '**', '*.obj').replace('\\', '/'),
recursive=True)
sp.run([COMPILER, *obj_files, *LINK_FLAGS, '-o', EXE_NAME],
shell=True)
@recipe(info='Removes all compiled objects.')
def clean():
shutil.rmtree(OBJ_DIR)
@recipe(recipe_deps=[link],
conditions=[lambda: True],
info='Runs the main executable.')
def run():
sp.run(EXE_NAME)
sane_run(run)