aced/make.py

88 lines
2.4 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 = ['-std=c11',
'-g',
'-O0',
'-Wall',
'-Wextra',
'-Werror',
'-Wpedantic',
'-pedantic-errors',
'-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[COMPILE_FLAGS.index('-O0')] = '-O2'
COMPILE_FLAGS.append('-DRELEASE')
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, '-c', *COMPILE_FLAGS,
'-o', object_path,
source_path])
source_files = [os.path.realpath(path)
for path in 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)
def old_exe():
object_files = [
os.path.realpath(path)
for path in glob('**/*.obj', recursive=True)]
return Sane.file_condition(object_files, [EXE_NAME])()
@recipe(hook_deps=['obj_compile'],
info='Compile the source files.')
def compile():
pass
@recipe(recipe_deps=[compile],
conditions=[exe_not_exists, old_exe],
info='Links the main executable.')
def link():
obj_files = glob(os.path.join(OBJ_DIR, '**', '*.obj').replace('\\', '/'),
recursive=True)
sp.run([COMPILER, *LINK_FLAGS, '-o', EXE_NAME, *obj_files])
@recipe(info='Removes all compiled objects.')
def clean():
shutil.rmtree(OBJ_DIR)
sane_run(link)