From a39999e1343f75fb5721d0dbe914ffe7b1e65846 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Sat, 24 Aug 2024 11:19:59 +0300 Subject: [PATCH] OVERHAUL: migrate to recursive make, because such way I can establish proper project structure for future make install AND have unclogged makefiles --- .gitignore | 4 +- Makefile | 86 ++++++------------- src/blackjack/Makefile | 30 +++++++ {include => src/blackjack}/debug.h | 0 src/{ => blackjack}/main.c | 0 src/{ => blackjack}/process.c | 0 {include => src/blackjack}/process.h | 0 {include => src/blackjack}/process_debug.h | 0 src/dummy_target/Makefile | 30 +++++++ src/{ => dummy_target}/dummy_destination.s | 0 src/{ => dummy_target}/dummy_target.c | 0 src/rtdisasm/Makefile | 27 ++++++ genc.py => src/rtdisasm/genc.py | 0 src/{ => rtdisasm}/rtdisasm.c | 6 +- {include => src/rtdisasm}/rtdisasm.h | 0 {include => src/rtdisasm}/rtdisasm_table.h | 0 {xml => src/rtdisasm/xml}/LICENSE | 0 .../rtdisasm/xml}/raw/x86/AMD/3DNow.xml | 0 .../rtdisasm/xml}/raw/x86/AMD/3DNow_Rules.dtd | 0 .../rtdisasm/xml}/raw/x86/AMD/SSE5.xml | 0 .../rtdisasm/xml}/raw/x86/AMD/SSE5_Rules.dtd | 0 {xml => src/rtdisasm/xml}/raw/x86/AMD/XOP.xml | 0 .../rtdisasm/xml}/raw/x86/AMD/XOP_Rules.dtd | 0 .../xml}/raw/x86/Intel/AVX512_Rules.dtd | 0 .../xml}/raw/x86/Intel/AVX512_r22.xml | 0 .../xml}/raw/x86/Intel/AVX512_r24.xml | 0 .../rtdisasm/xml}/raw/x86/Intel/AZ.xml | 0 .../rtdisasm/xml}/raw/x86/Intel/AZ_Rules.dtd | 0 src/rtdisasm_test/Makefile | 30 +++++++ src/{ => rtdisasm_test}/rtdisasm_test.c | 2 +- src/{ => rtdisasm_test}/rtdisasm_test_data.s | 0 31 files changed, 151 insertions(+), 64 deletions(-) create mode 100644 src/blackjack/Makefile rename {include => src/blackjack}/debug.h (100%) rename src/{ => blackjack}/main.c (100%) rename src/{ => blackjack}/process.c (100%) rename {include => src/blackjack}/process.h (100%) rename {include => src/blackjack}/process_debug.h (100%) create mode 100644 src/dummy_target/Makefile rename src/{ => dummy_target}/dummy_destination.s (100%) rename src/{ => dummy_target}/dummy_target.c (100%) create mode 100644 src/rtdisasm/Makefile rename genc.py => src/rtdisasm/genc.py (100%) rename src/{ => rtdisasm}/rtdisasm.c (99%) rename {include => src/rtdisasm}/rtdisasm.h (100%) rename {include => src/rtdisasm}/rtdisasm_table.h (100%) rename {xml => src/rtdisasm/xml}/LICENSE (100%) rename {xml => src/rtdisasm/xml}/raw/x86/AMD/3DNow.xml (100%) rename {xml => src/rtdisasm/xml}/raw/x86/AMD/3DNow_Rules.dtd (100%) rename {xml => src/rtdisasm/xml}/raw/x86/AMD/SSE5.xml (100%) rename {xml => src/rtdisasm/xml}/raw/x86/AMD/SSE5_Rules.dtd (100%) rename {xml => src/rtdisasm/xml}/raw/x86/AMD/XOP.xml (100%) rename {xml => src/rtdisasm/xml}/raw/x86/AMD/XOP_Rules.dtd (100%) rename {xml => src/rtdisasm/xml}/raw/x86/Intel/AVX512_Rules.dtd (100%) rename {xml => src/rtdisasm/xml}/raw/x86/Intel/AVX512_r22.xml (100%) rename {xml => src/rtdisasm/xml}/raw/x86/Intel/AVX512_r24.xml (100%) rename {xml => src/rtdisasm/xml}/raw/x86/Intel/AZ.xml (100%) rename {xml => src/rtdisasm/xml}/raw/x86/Intel/AZ_Rules.dtd (100%) create mode 100644 src/rtdisasm_test/Makefile rename src/{ => rtdisasm_test}/rtdisasm_test.c (98%) rename src/{ => rtdisasm_test}/rtdisasm_test_data.s (100%) diff --git a/.gitignore b/.gitignore index 24655fb..e970ad3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ .vscode/ -bin/ -obj/ \ No newline at end of file +**/obj/ +bin/ \ No newline at end of file diff --git a/Makefile b/Makefile index 7aec014..b050888 100644 --- a/Makefile +++ b/Makefile @@ -1,76 +1,46 @@ -INC_DIR = include -SRC_DIR = src +ROOT_DIR = $(PWD) +export ROOT_DIR + +INC_DIR = $(ROOT_DIR)/src +SRC_DIR = $(ROOT_DIR)/src +BIN_DIR = $(ROOT_DIR)/bin +LIB_DIR = $(ROOT_DIR)/bin OBJ_DIR = obj -BIN_DIR = bin -LIB_DIR = bin + +export INC_DIR +export SRC_DIR +export BIN_DIR +export LIB_DIR +export OBJ_DIR CC = gcc AS = as AR = ar LD = ld +MAKE = gmake PYTHON = python -CFLAGS = -Wall -I$(INC_DIR) -ASFLAGS = -LDFLAGS = -z noexecstack -lcap -SHARED_OBJ = -SHARED_DEPS = $(INC_DIR)/debug.h +export CC +export AS +export AR +export LD +export MAKE +export PYTHON -RTDISASM_SRC = rtdisasm.c -RTDISASM_OBJ := $(addprefix $(OBJ_DIR)/,$(patsubst %.s,%.o,$(patsubst %.c,%.o,$(RTDISASM_SRC)))) $(SHARED_OBJ) -RTDISASM_SRC := $(addprefix $(SRC_DIR)/,$(RTDISASM_SRC)) -RTDISASM_DEPS = rtdisasm.h rtdisasm_table.h -RTDISASM_DEPS := $(addprefix $(INC_DIR)/,$(RTDISASM_DEPS)) $(SHARED_DEPS) +# order matters here, build libraries first! +TARGETS = rtdisasm rtdisasm_test dummy_target blackjack -RTDISASM_TEST_SRC = rtdisasm_test.c rtdisasm_test_data.s -RTDISASM_TEST_OBJ := $(addprefix $(OBJ_DIR)/,$(patsubst %.s,%.o,$(patsubst %.c,%.o,$(RTDISASM_TEST_SRC)))) -RTDISASM_TEST_SRC := $(addprefix $(SRC_DIR)/,$(RTDISASM_TEST_SRC)) -RTDISASM_TEST_DEPS = -RTDISASM_TEST_DEPS := $(addprefix $(INC_DIR)/,$(RTDISASM_TEST_DEPS)) $(SHARED_DEPS) rtdisasm +.PHONY: $(TARGETS) debug clean -BLACKJACK_SRC = main.c process.c -BLACKJACK_OBJ := $(addprefix $(OBJ_DIR)/,$(patsubst %.s,%.o,$(patsubst %.c,%.o,$(BLACKJACK_SRC)))) $(SHARED_OBJ) -BLACKJACK_SRC := $(addprefix $(SRC_DIR)/,$(BLACKJACK_SRC)) -BLACKJACK_DEPS = process.h process_debug.h -BLACKJACK_DEPS := $(addprefix $(INC_DIR)/,$(BLACKJACK_DEPS)) rtdisasm $(SHARED_DEPS) - -DUMMY_TARGET_SRC = dummy_target.c dummy_destination.s -DUMMY_TARGET_OBJ := $(addprefix $(OBJ_DIR)/,$(patsubst %.s,%.o,$(patsubst %.c,%.o,$(DUMMY_TARGET_SRC)))) -DUMMY_TARGET_SRC := $(addprefix $(SRC_DIR)/,$(DUMMY_TARGET_SRC)) - -$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c - $(CC) $(CFLAGS) -c -o $@ $< - -$(OBJ_DIR)/%.o: $(SRC_DIR)/%.s - $(AS) $(ASFLAGS) -o $@ $< - -rtdisasm: $(RTDISASM_OBJ) $(RTDISASM_DEPS) - $(PYTHON) genc.py xml/raw/x86/Intel/AZ.xml | $(CC) -x c $(CFLAGS) -c -o $(OBJ_DIR)/rtdisasm_table.o - - $(AR) -crs $(BIN_DIR)/librtdisasm.a $(RTDISASM_OBJ) $(OBJ_DIR)/rtdisasm_table.o - -$(OBJ_DIR)/rtdisasm_test.o: $(SRC_DIR)/rtdisasm_test.c - $(CC) $(CFLAGS) -mavx -c -o $@ $< - -rtdisasm_test: $(RTDISASM_TEST_OBJ) $(RTDISASM_TEST_DEPS) - $(CC) $(LDFLAGS) -o $(BIN_DIR)/$@ $(RTDISASM_TEST_OBJ) $(LIB_DIR)/librtdisasm.a - -blackjack: $(BLACKJACK_OBJ) $(BLACKJACK_DEPS) - $(CC) $(LDFLAGS) -o $(BIN_DIR)/$@ $(BLACKJACK_OBJ) $(LIB_DIR)/librtdisasm.a - -dummy_target: $(DUMMY_TARGET_OBJ) - $(CC) $(LDFLAGS) -o $(BIN_DIR)/$@ $(DUMMY_TARGET_OBJ) - -.PHONY: all clean debug - -TARGETS = blackjack dummy_target rtdisasm_test - -all: $(TARGETS) +all: + for target in $(TARGETS); do $(MAKE) -C src/$$target; done debug: CFLAGS += -DDEBUG -g debug: LDFLAGS += -g debug: ASFLAGS += -g -debug: $(TARGETS) +debug: + for target in $(TARGETS); do $(MAKE) -C src/$$target debug; done clean: - rm -f $(OBJ_DIR)/*.o + for target in $(TARGETS); do $(MAKE) -C src/$$target clean; done rm -f $(BIN_DIR)/* \ No newline at end of file diff --git a/src/blackjack/Makefile b/src/blackjack/Makefile new file mode 100644 index 0000000..75540f5 --- /dev/null +++ b/src/blackjack/Makefile @@ -0,0 +1,30 @@ +NAME = blackjack + +CFLAGS = -Wall -I$(INC_DIR) +ASFLAGS = +LDFLAGS = -z noexecstack -lcap + +SRC = main.c process.c +OBJ := $(addprefix $(OBJ_DIR)/,$(patsubst %.s,%.o,$(patsubst %.c,%.o,$(SRC)))) +DEPS = process.h process_debug.h + +$(OBJ_DIR)/%.o: %.c + @mkdir -p $(OBJ_DIR) + $(CC) $(CFLAGS) -c -o $@ $< + +$(OBJ_DIR)/%.o: %.s + @mkdir -p $(OBJ_DIR) + $(AS) $(ASFLAGS) -o $@ $< + +.PHONY: all clean debug + +all: $(OBJ) $(DEPS) + $(CC) $(LDFLAGS) -o $(BIN_DIR)/$(NAME) $(OBJ) + +debug: CFLAGS += -DDEBUG -g +debug: LDFLAGS += -g +debug: ASFLAGS += -g +debug: all + +clean: + rm -f $(OBJ_DIR)/* \ No newline at end of file diff --git a/include/debug.h b/src/blackjack/debug.h similarity index 100% rename from include/debug.h rename to src/blackjack/debug.h diff --git a/src/main.c b/src/blackjack/main.c similarity index 100% rename from src/main.c rename to src/blackjack/main.c diff --git a/src/process.c b/src/blackjack/process.c similarity index 100% rename from src/process.c rename to src/blackjack/process.c diff --git a/include/process.h b/src/blackjack/process.h similarity index 100% rename from include/process.h rename to src/blackjack/process.h diff --git a/include/process_debug.h b/src/blackjack/process_debug.h similarity index 100% rename from include/process_debug.h rename to src/blackjack/process_debug.h diff --git a/src/dummy_target/Makefile b/src/dummy_target/Makefile new file mode 100644 index 0000000..c8ee208 --- /dev/null +++ b/src/dummy_target/Makefile @@ -0,0 +1,30 @@ +NAME = dummy_target + +CFLAGS = -Wall -I$(INC_DIR) +ASFLAGS = +LDFLAGS = -z noexecstack -lcap + +SRC = dummy_target.c dummy_destination.s +OBJ := $(addprefix $(OBJ_DIR)/,$(patsubst %.s,%.o,$(patsubst %.c,%.o,$(SRC)))) +DEPS = + +$(OBJ_DIR)/%.o: %.c + @mkdir -p $(OBJ_DIR) + $(CC) $(CFLAGS) -c -o $@ $< + +$(OBJ_DIR)/%.o: %.s + @mkdir -p $(OBJ_DIR) + $(AS) $(ASFLAGS) -o $@ $< + +.PHONY: all clean debug + +all: $(OBJ) $(DEPS) + $(CC) $(LDFLAGS) -o $(BIN_DIR)/$(NAME) $(OBJ) + +debug: CFLAGS += -DDEBUG -g +debug: LDFLAGS += -g +debug: ASFLAGS += -g +debug: all + +clean: + rm -f $(OBJ_DIR)/* \ No newline at end of file diff --git a/src/dummy_destination.s b/src/dummy_target/dummy_destination.s similarity index 100% rename from src/dummy_destination.s rename to src/dummy_target/dummy_destination.s diff --git a/src/dummy_target.c b/src/dummy_target/dummy_target.c similarity index 100% rename from src/dummy_target.c rename to src/dummy_target/dummy_target.c diff --git a/src/rtdisasm/Makefile b/src/rtdisasm/Makefile new file mode 100644 index 0000000..510c5b8 --- /dev/null +++ b/src/rtdisasm/Makefile @@ -0,0 +1,27 @@ +NAME = rtdisasm + +CFLAGS = -Wall -I$(INC_DIR) +ASFLAGS = +LDFLAGS = -z noexecstack -lcap + +SRC = rtdisasm.c +OBJ := $(addprefix $(OBJ_DIR)/,$(patsubst %.s,%.o,$(patsubst %.c,%.o,$(SRC)))) +DEPS = rtdisasm.h rtdisasm_table.h + +$(OBJ_DIR)/%.o: %.c + @mkdir -p $(OBJ_DIR) + $(CC) $(CFLAGS) -c -o $@ $< + +.PHONY: all clean debug + +all: $(OBJ) $(DEPS) + $(PYTHON) genc.py xml/raw/x86/Intel/AZ.xml | $(CC) -x c $(CFLAGS) -c -o $(OBJ_DIR)/rtdisasm_table.o - + $(AR) -crs $(BIN_DIR)/librtdisasm.a $(OBJ) $(OBJ_DIR)/rtdisasm_table.o + +debug: CFLAGS += -DDEBUG -g +debug: LDFLAGS += -g +debug: ASFLAGS += -g +debug: all + +clean: + rm -f $(OBJ_DIR)/* \ No newline at end of file diff --git a/genc.py b/src/rtdisasm/genc.py similarity index 100% rename from genc.py rename to src/rtdisasm/genc.py diff --git a/src/rtdisasm.c b/src/rtdisasm/rtdisasm.c similarity index 99% rename from src/rtdisasm.c rename to src/rtdisasm/rtdisasm.c index fd5201a..c8ba3df 100644 --- a/src/rtdisasm.c +++ b/src/rtdisasm/rtdisasm.c @@ -1,6 +1,6 @@ -#include "rtdisasm.h" -#include "rtdisasm_table.h" -#include "debug.h" +#include "rtdisasm/rtdisasm.h" +#include "rtdisasm/rtdisasm_table.h" +#include "blackjack/debug.h" #include #include diff --git a/include/rtdisasm.h b/src/rtdisasm/rtdisasm.h similarity index 100% rename from include/rtdisasm.h rename to src/rtdisasm/rtdisasm.h diff --git a/include/rtdisasm_table.h b/src/rtdisasm/rtdisasm_table.h similarity index 100% rename from include/rtdisasm_table.h rename to src/rtdisasm/rtdisasm_table.h diff --git a/xml/LICENSE b/src/rtdisasm/xml/LICENSE similarity index 100% rename from xml/LICENSE rename to src/rtdisasm/xml/LICENSE diff --git a/xml/raw/x86/AMD/3DNow.xml b/src/rtdisasm/xml/raw/x86/AMD/3DNow.xml similarity index 100% rename from xml/raw/x86/AMD/3DNow.xml rename to src/rtdisasm/xml/raw/x86/AMD/3DNow.xml diff --git a/xml/raw/x86/AMD/3DNow_Rules.dtd b/src/rtdisasm/xml/raw/x86/AMD/3DNow_Rules.dtd similarity index 100% rename from xml/raw/x86/AMD/3DNow_Rules.dtd rename to src/rtdisasm/xml/raw/x86/AMD/3DNow_Rules.dtd diff --git a/xml/raw/x86/AMD/SSE5.xml b/src/rtdisasm/xml/raw/x86/AMD/SSE5.xml similarity index 100% rename from xml/raw/x86/AMD/SSE5.xml rename to src/rtdisasm/xml/raw/x86/AMD/SSE5.xml diff --git a/xml/raw/x86/AMD/SSE5_Rules.dtd b/src/rtdisasm/xml/raw/x86/AMD/SSE5_Rules.dtd similarity index 100% rename from xml/raw/x86/AMD/SSE5_Rules.dtd rename to src/rtdisasm/xml/raw/x86/AMD/SSE5_Rules.dtd diff --git a/xml/raw/x86/AMD/XOP.xml b/src/rtdisasm/xml/raw/x86/AMD/XOP.xml similarity index 100% rename from xml/raw/x86/AMD/XOP.xml rename to src/rtdisasm/xml/raw/x86/AMD/XOP.xml diff --git a/xml/raw/x86/AMD/XOP_Rules.dtd b/src/rtdisasm/xml/raw/x86/AMD/XOP_Rules.dtd similarity index 100% rename from xml/raw/x86/AMD/XOP_Rules.dtd rename to src/rtdisasm/xml/raw/x86/AMD/XOP_Rules.dtd diff --git a/xml/raw/x86/Intel/AVX512_Rules.dtd b/src/rtdisasm/xml/raw/x86/Intel/AVX512_Rules.dtd similarity index 100% rename from xml/raw/x86/Intel/AVX512_Rules.dtd rename to src/rtdisasm/xml/raw/x86/Intel/AVX512_Rules.dtd diff --git a/xml/raw/x86/Intel/AVX512_r22.xml b/src/rtdisasm/xml/raw/x86/Intel/AVX512_r22.xml similarity index 100% rename from xml/raw/x86/Intel/AVX512_r22.xml rename to src/rtdisasm/xml/raw/x86/Intel/AVX512_r22.xml diff --git a/xml/raw/x86/Intel/AVX512_r24.xml b/src/rtdisasm/xml/raw/x86/Intel/AVX512_r24.xml similarity index 100% rename from xml/raw/x86/Intel/AVX512_r24.xml rename to src/rtdisasm/xml/raw/x86/Intel/AVX512_r24.xml diff --git a/xml/raw/x86/Intel/AZ.xml b/src/rtdisasm/xml/raw/x86/Intel/AZ.xml similarity index 100% rename from xml/raw/x86/Intel/AZ.xml rename to src/rtdisasm/xml/raw/x86/Intel/AZ.xml diff --git a/xml/raw/x86/Intel/AZ_Rules.dtd b/src/rtdisasm/xml/raw/x86/Intel/AZ_Rules.dtd similarity index 100% rename from xml/raw/x86/Intel/AZ_Rules.dtd rename to src/rtdisasm/xml/raw/x86/Intel/AZ_Rules.dtd diff --git a/src/rtdisasm_test/Makefile b/src/rtdisasm_test/Makefile new file mode 100644 index 0000000..ba06c43 --- /dev/null +++ b/src/rtdisasm_test/Makefile @@ -0,0 +1,30 @@ +NAME = rtdisasm_test + +CFLAGS = -Wall -mavx -I$(INC_DIR) +ASFLAGS = +LDFLAGS = -z noexecstack -lcap + +SRC = rtdisasm_test.c rtdisasm_test_data.s +OBJ := $(addprefix $(OBJ_DIR)/,$(patsubst %.s,%.o,$(patsubst %.c,%.o,$(SRC)))) +DEPS = + +$(OBJ_DIR)/%.o: %.c + @mkdir -p $(OBJ_DIR) + $(CC) $(CFLAGS) -c -o $@ $< + +$(OBJ_DIR)/%.o: %.s + @mkdir -p $(OBJ_DIR) + $(AS) $(ASFLAGS) -o $@ $< + +.PHONY: all clean debug + +all: $(OBJ) $(DEPS) + $(CC) $(LDFLAGS) -o $(BIN_DIR)/$(NAME) $(OBJ) $(LIB_DIR)/librtdisasm.a + +debug: CFLAGS += -DDEBUG -g +debug: LDFLAGS += -g +debug: ASFLAGS += -g +debug: all + +clean: + rm -f $(OBJ_DIR)/* \ No newline at end of file diff --git a/src/rtdisasm_test.c b/src/rtdisasm_test/rtdisasm_test.c similarity index 98% rename from src/rtdisasm_test.c rename to src/rtdisasm_test/rtdisasm_test.c index ea89a73..026d784 100644 --- a/src/rtdisasm_test.c +++ b/src/rtdisasm_test/rtdisasm_test.c @@ -1,4 +1,4 @@ -#include "rtdisasm.h" +#include "rtdisasm/rtdisasm.h" #include #include diff --git a/src/rtdisasm_test_data.s b/src/rtdisasm_test/rtdisasm_test_data.s similarity index 100% rename from src/rtdisasm_test_data.s rename to src/rtdisasm_test/rtdisasm_test_data.s