diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fa1926b..7ddc070 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -25,7 +25,7 @@ jobs: sudo apt-get install -y libxml2-dev libusb-1.0-0-dev - name: Build - run: make + run: make all - name: Run tests run: make tests @@ -35,7 +35,7 @@ jobs: mkdir dist cp `pkg-config --variable=libdir libusb-1.0`/libusb-1.0.so.0 dist chmod 0644 dist/* - cp qdl dist + cp ./build/release/bin/qdl dist patchelf --set-rpath '$ORIGIN' dist/qdl - name: Upload artifact @@ -63,7 +63,7 @@ jobs: brew install libxml2 - name: Build - run: make + run: make all - name: Run tests run: make tests @@ -75,7 +75,7 @@ jobs: cp `pkg-config --variable=libdir libusb-1.0`/libusb-1.0.0.dylib dist cp `pkg-config --variable=libdir liblzma`/liblzma.5.dylib dist chmod 0644 dist/* - cp qdl dist + cp ./build/release/bin/qdl dist if uname -a | grep -q arm64; then LIBUSB_DIR=/opt/homebrew/opt/libusb/lib @@ -124,7 +124,7 @@ jobs: mingw-w64-x86_64-libusb - name: Build - run: make + run: make all shell: msys2 {0} - name: Run tests @@ -145,7 +145,7 @@ jobs: Copy-Item (Join-Path $BIN_DIR "liblzma-5.dll") $DistDir Copy-Item (Join-Path $BIN_DIR "libiconv-2.dll") $DistDir - Copy-Item "qdl.exe" $DistDir + Copy-Item "./build/release/bin/qdl.exe" $DistDir - name: Upload artifact uses: actions/upload-artifact@v4 diff --git a/.gitignore b/.gitignore index cd245ea..009060f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +/build/ +/tests/data/*.elf +/tests/data/*.bin +/tests/data/*.img *.o qdl qdl-ramdump @@ -7,5 +11,5 @@ compile_commands.json .cache .version.h version.h -scripts +.scripts .checkpatch-camelcase.git. diff --git a/Makefile b/Makefile index a8fb0da..86c10df 100644 --- a/Makefile +++ b/Makefile @@ -1,23 +1,30 @@ -QDL := qdl -RAMDUMP := qdl-ramdump VERSION := $(or $(VERSION), $(shell git describe --dirty --always --tags 2>/dev/null), "unknown-version") -CFLAGS += -O2 -Wall -g `pkg-config --cflags libxml-2.0 libusb-1.0` +CFLAGS += -Wall `pkg-config --cflags libxml-2.0 libusb-1.0` LDFLAGS += `pkg-config --libs libxml-2.0 libusb-1.0` ifeq ($(OS),Windows_NT) LDFLAGS += -lws2_32 endif prefix := /usr/local +# Default build type +BUILD_TYPE ?= debug +BUILD_DIR = build/$(BUILD_TYPE) +OBJ_DIR = $(BUILD_DIR)/obj +BIN_DIR = $(BUILD_DIR)/bin +GEN_DIR = $(BUILD_DIR)/gen + +QDL := qdl QDL_SRCS := firehose.c io.c qdl.c sahara.c util.c patch.c program.c read.c sha2.c sim.c ufs.c usb.c ux.c oscompat.c vip.c sparse.c gpt.c -QDL_OBJS := $(QDL_SRCS:.c=.o) +QDL_OBJS = $(addprefix $(OBJ_DIR)/, $(notdir $(QDL_SRCS:.c=.o))) +RAMDUMP := qdl-ramdump RAMDUMP_SRCS := ramdump.c sahara.c io.c sim.c usb.c util.c ux.c oscompat.c -RAMDUMP_OBJS := $(RAMDUMP_SRCS:.c=.o) +RAMDUMP_OBJS = $(addprefix $(OBJ_DIR)/, $(notdir $(RAMDUMP_SRCS:.c=.o))) KS_OUT := ks KS_SRCS := ks.c sahara.c util.c ux.c oscompat.c -KS_OBJS := $(KS_SRCS:.c=.o) +KS_OBJS = $(addprefix $(OBJ_DIR)/, $(notdir $(KS_SRCS:.c=.o))) CHECKPATCH_SOURCES := $(shell find . -type f \( -name "*.c" -o -name "*.h" -o -name "*.sh" \) ! -name "sha2.c" ! -name "sha2.h" ! -name "*version.h" ! -name "list.h") CHECKPATCH_ROOT := https://raw.githubusercontent.com/torvalds/linux/v6.15/scripts @@ -26,41 +33,55 @@ CHECKPATCH_SP_URL := $(CHECKPATCH_ROOT)/spelling.txt CHECKPATCH := ./.scripts/checkpatch.pl CHECKPATCH_SP := ./.scripts/spelling.txt -default: $(QDL) $(RAMDUMP) $(KS_OUT) +default: debug +all: debug release + +release: + $(MAKE) BUILD_TYPE=release CFLAGS='$(CFLAGS) -O2 -Ibuild/release/gen' _all_internal + +debug: + $(MAKE) BUILD_TYPE=debug CFLAGS='$(CFLAGS) -O0 -g -Ibuild/debug/gen' _all_internal + +# Inner target that actually builds things with the chosen BUILD_TYPE +_all_internal: dirs $(BIN_DIR)/$(QDL) $(BIN_DIR)/$(RAMDUMP) $(BIN_DIR)/$(KS_OUT) + +dirs: + @mkdir -p $(OBJ_DIR) $(BIN_DIR) $(GEN_DIR) + +$(OBJ_DIR)/%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ -$(QDL): $(QDL_OBJS) +$(BIN_DIR)/$(QDL): $(QDL_OBJS) $(CC) -o $@ $^ $(LDFLAGS) -$(RAMDUMP): $(RAMDUMP_OBJS) +$(BIN_DIR)/$(RAMDUMP): $(RAMDUMP_OBJS) $(CC) -o $@ $^ $(LDFLAGS) -$(KS_OUT): $(KS_OBJS) +$(BIN_DIR)/$(KS_OUT): $(KS_OBJS) $(CC) -o $@ $^ $(LDFLAGS) compile_commands.json: $(QDL_SRCS) $(KS_SRCS) @echo -n $^ | jq -snR "[inputs|split(\" \")[]|{directory:\"$(PWD)\", command: \"$(CC) $(CFLAGS) -c \(.)\", file:.}]" > $@ -version.h:: - @echo "#define VERSION \"$(VERSION)\"" > .version.h - @cmp -s .version.h version.h || cp .version.h version.h +$(GEN_DIR)/version.h: + @echo "#define VERSION \"$(VERSION)\"" > $(GEN_DIR)/.version.h + @cmp -s $(GEN_DIR)/.version.h $(GEN_DIR)/version.h || cp $(GEN_DIR)/.version.h $(GEN_DIR)/version.h -util.o: version.h +$(OBJ_DIR)/util.o: $(GEN_DIR)/version.h clean: - rm -f $(QDL) $(QDL_OBJS) - rm -f $(RAMDUMP) $(RAMDUMP_OBJS) - rm -f $(KS_OUT) $(KS_OBJS) + rm -rf build rm -f compile_commands.json rm -f version.h .version.h rm -f $(CHECKPATCH) rm -f $(CHECKPATCH_SP) if [ -d .scripts ]; then rmdir .scripts; fi -install: $(QDL) $(RAMDUMP) $(KS_OUT) +install: release install -d $(DESTDIR)$(prefix)/bin - install -m 755 $^ $(DESTDIR)$(prefix)/bin + install -m 755 $(BIN_DIR)/$(QDL) $(BIN_DIR)/$(RAMDUMP) $(BIN_DIR)/$(KS_OUT) $(DESTDIR)$(prefix)/bin -tests: default +tests: debug tests: @./tests/run_tests.sh diff --git a/tests/test_vip_generation.sh b/tests/test_vip_generation.sh index 85053e3..1cada1e 100755 --- a/tests/test_vip_generation.sh +++ b/tests/test_vip_generation.sh @@ -8,6 +8,7 @@ SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" FLAT_BUILD=${SCRIPT_PATH}/data REP_ROOT=${SCRIPT_PATH}/.. +QDL_PATH=${REP_ROOT}/build/debug/bin VIP_PATH=${FLAT_BUILD}/vip EXPECTED_DIGEST="a05e1124edbe34dc504a327544fb66572591353dc3fa25e6e7eafbe4803e63e0" VIP_TABLE_FILE=${VIP_PATH}/DigestsToSign.bin @@ -15,7 +16,7 @@ VIP_TABLE_FILE=${VIP_PATH}/DigestsToSign.bin mkdir -p $VIP_PATH cd $FLAT_BUILD -${REP_ROOT}/qdl --dry-run --create-digests=${VIP_PATH} \ +${QDL_PATH}/qdl --dry-run --create-digests=${VIP_PATH} \ prog_firehose_ddr.elf rawprogram*.xml patch*.xml if command -v sha256sum >/dev/null 2>&1; then