From 3ab1c1ad299b9bf21080a4be158c7d4c96f65529 Mon Sep 17 00:00:00 2001 From: bumbread Date: Mon, 18 Jul 2022 08:32:02 +1100 Subject: [PATCH] say no to make, say no to shell scripts, python is the way to go --- Makefile | 66 ------------------------------------- bake.cmd | 41 ----------------------- bake.py | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ bake_cc.cmd | 8 ----- build_cuik.cmd | 16 --------- 5 files changed, 89 insertions(+), 131 deletions(-) delete mode 100644 Makefile delete mode 100644 bake.cmd create mode 100644 bake.py delete mode 100644 bake_cc.cmd delete mode 100644 build_cuik.cmd diff --git a/Makefile b/Makefile deleted file mode 100644 index 42e2bd1..0000000 --- a/Makefile +++ /dev/null @@ -1,66 +0,0 @@ - -CC=clang -SRC_DIR := src -OBJ_DIR := bin -IFLAGS := -Iinc -Isrc/win -Iunicope/inc - -# Detect target operating system -ifeq ($(OS),Windows_NT) - PLATFORM := win -else - PLATFORM := $(shell sh -c 'uname 2>/dev/null || echo Unknown') - PLATFORM := $(shell sh -c 'echo $(PLATFORM) | tr A-Z a-z') -endif -ifeq ($(PLATFORM),Unknown) - echo Unknown platform - exit 1 -endif - -# If we're compiling under windows we'll link to these libraries -ifeq ($(PLATFORM),win) - LIBS := -lDbghelp -lkernel32 -luser32 -lshell32 - MKDIR_P_FLAG := - NUL_FILE := NUL -else - LIBS := -lrt -lpthread -ldl - MKDIR_P_FLAG := '-p' - NUL_FILE := /dev/null -endif - -# Compiler flags -ifeq ($(CC), clang) - CFLAGS=$(GNUFLAGS) -Wall -msse2 -mfma $(IFLAGS) -else - echo BAD CC - exit 1 -endif - -# Figure out what we want to compile at the end -SRC_FILES := \ - $(wildcard $(SRC_DIR)/code/*.c) \ - $(wildcard $(SRC_DIR)/code/**/*.c) \ - $(wildcard $(SRC_DIR)/$(PLATFORM)/*.c) \ - $(wildcard $(SRC_DIR)/$(PLATFORM)/**/*.c) -OBJ_FILES := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.obj,$(SRC_FILES)) - -$(OBJ_DIR)/%.obj: $(SRC_DIR)/%.c - @-mkdir $(MKDIR_P_FLAG) "$(dir $@)" 2> $(NUL_FILE) - $(CC) $(CFLAGS) -c -o $@ $< - -ciabatta.lib: $(OBJ_FILES) unicope/unicope.lib - llvm-ar rc $@ $^ - -unicope/unicope.lib: - unicope\bake - - -test: ciabatta.lib - clang -g test/test_$(test).c ciabatta.lib -std=c11 $(LIBS) -nostdlib -Iinc - -clean: - rd/s/q bin || true - rm -Rf bin || true - rm -f ciabatta.lib || true - del ciabatta.lib || true - -.PHONY: ciabatta.lib test diff --git a/bake.cmd b/bake.cmd deleted file mode 100644 index 289f95b..0000000 --- a/bake.cmd +++ /dev/null @@ -1,41 +0,0 @@ -@echo off -setlocal enabledelayedexpansion - -:: Make sure that if I ctrl+c out of the script the dir is popped -:: If there's anything that requires user input, this script will break -:: in that case I recommend you removing the 'echo y' part. -:: although you will have to confirm termination of the script twice -if "%~1" neq "_start_" ( - pushd %~pd0 - echo y | cmd /c "%~f0" _start_ %* - popd - exit /b -) -shift /1 - -set CIABATTA_OPTIONS=-Iinc -I unicope\inc -Wall -g -gcodeview -nodefaultlibs -D_CRT_SECURE_NO_WARNINGS -mfma -set PLATFORM=win - -if "%1"=="test" ( - goto :skip_crt_compilation -) - -if exist bin rd/s/q bin -mkdir bin -mkdir bin\%PLATFORM% - -del ciabatta.lib 2> nul -call bake_cc.cmd -for /R src\%PLATFORM% %%F in (*.asm) do ( - echo %%F - nasm %%F -f win64 -o bin\%PLATFORM%\%%~nF.obj -) -llvm-ar rc ciabatta.lib bin\*.obj bin\%PLATFORM%\*.obj - -:skip_crt_compilation - -if "%TEST%"=="" set TEST=threads - -echo Compiling test_%TEST%.c -clang test\test_%TEST%.c ciabatta.lib -std=c11 -lDbghelp -lkernel32 -luser32 -lshell32 -nostdlib %CIABATTA_OPTIONS% -::cl test\test_math.c /Iinc -D_CRT_SECURE_NO_WARNINGS /Z7 /link ciabatta.lib kernel32.lib user32.lib shell32.lib -nostdlib -nodefaultlibs diff --git a/bake.py b/bake.py new file mode 100644 index 0000000..cd35948 --- /dev/null +++ b/bake.py @@ -0,0 +1,89 @@ + +import os +import subprocess + +# General compile options + +platform = 'win' +do_cuik = False + +inc_folders = [ + 'inc', + os.path.join('unicope', 'inc'), +] + +definitions = [ + '_CRT_SECURE_NO_WARNINGS', +] + +# Compiler-specific options + +clang_dbg_flags = ['-g', '-gcodeview'] +clang_common_flags = ['-c', '-nodefaultlibs', '-mfma'] + +cuik_flags = [] + +#----------------------------------------------------------------------------# +# Map lists to lists of options +inc_flags = [] +def_flags = [] +def compile(root, cmap): + global inc_flags + global def_flags + inc_flags = list(map(lambda p: '-I '+ p, inc_folders)) + def_flags = list(map(lambda d: '-D' + d, definitions)) + for path, subdirs, files in os.walk(root): + for file_name in files: + file_path = os.path.join(path, file_name) + short_name, ext = os.path.splitext(file_path) + if ext in cmap.keys(): + func = cmap[ext] + func(file_path) + +def get_bin_path(file_path): + rel_path = os.path.normpath(file_path).split(os.path.sep)[1:] + name, ext = os.path.splitext(os.path.sep.join(rel_path)) + bin_path = os.path.join('bin', name+'.obj') + os.makedirs(os.path.dirname(bin_path), exist_ok=True) + return bin_path + +def clang_compile(file_name): + bin_path = get_bin_path(file_name) + dbg_flags = clang_dbg_flags + cmn_flags = clang_common_flags + flags = dbg_flags + cmn_flags + inc_flags + def_flags + command = ' '.join(["clang", file_name, '-o', bin_path] + flags) + subprocess.run(command.split(' ')) + print(file_name, '=>', bin_path) + +def nasm_compile(file_name): + bin_path = get_bin_path(file_name) + subprocess.run(['nasm', file_name, '-f', 'win64', '-o', bin_path]) + print(file_name, '=>', bin_path) + +#-----------------------------------------------------------------------------# + +if not do_cuik: + # Compile the platform-independent part + compile_map = {} + compile_map['.c'] = clang_compile + compile_map['.asm'] = nasm_compile + compile(os.path.normpath('src/code'), compile_map) + + # Add the platform folder to includes and compile platform-dependent part + inc_folders.append(os.path.join('src', platform)) + compile(os.path.normpath(os.path.join('src', platform)), compile_map) + + # Make an archive of all object files + obj_paths = [] + for dir, _, f in os.walk('bin'): + if len(f) != 0: + obj_paths.append(os.path.join(dir, '*.obj')) + subprocess.run(['llvm-ar', 'rc', 'ciabatta.lib'] + obj_paths) +else: + src = os.path.join('src', 'code', '*.c') + os_src = os.path.join('src', platform) + cmd = cuik_flags + '-c -o ciabatta.obj'.split(' ') + [src, os_src] + print(cmd) + subprocess.run(['cuik'] + cmd) + subprocess.run('lib', '/out:ciabatta.lib', 'ciabatta.obj') \ No newline at end of file diff --git a/bake_cc.cmd b/bake_cc.cmd deleted file mode 100644 index b390db1..0000000 --- a/bake_cc.cmd +++ /dev/null @@ -1,8 +0,0 @@ -for /R src\%PLATFORM% %%F in (*.c) do ( - echo %%F - start /B clang -I unicope\inc -Isrc/win -c -o bin\%PLATFORM%\%%~nF.obj %%F %CIABATTA_OPTIONS% -) -for /R src\code %%F in (*.c) do ( - echo %%F - start /B clang -I unicope\inc -c -o bin\%%~nF.obj %%F %CIABATTA_OPTIONS% -) diff --git a/build_cuik.cmd b/build_cuik.cmd deleted file mode 100644 index 95ddce5..0000000 --- a/build_cuik.cmd +++ /dev/null @@ -1,16 +0,0 @@ -@echo off -setlocal enabledelayedexpansion - -set PLATFORM=win -set CIABATTA_OPTIONS=--crt none -I %% -I inc -I unicope/inc - -del ciabatta.lib -del unicope\unicope.lib -cuik unicope\src\unicode.c -I unicope\inc -c -o unicope\unicope.lib -cuik %CIABATTA_OPTIONS% src\code\*.c src\%PLATFORM%\*.c -c -o ciabatta.obj -lib /out:ciabatta.lib ciabatta.obj - -if "%TEST%"=="" set TEST=assert - -cuik test\test_%TEST%.c --lib ciabatta.lib,unicope/unicope.lib,kernel32.lib,user32.lib,shell32.lib %CIABATTA_OPTIONS% -del ciabatta.obj