From de4f9ebf11d516c3774f3ddc46f2d389d45124fd Mon Sep 17 00:00:00 2001 From: Justin Traglia <95511699+jtraglia@users.noreply.github.com> Date: Thu, 15 Aug 2024 09:06:17 -0500 Subject: [PATCH] Improve the build system (#483) --- .gitignore | 1 + bindings/java/Makefile | 1 + src/Makefile | 55 +++++++++++++++++++++++++++++++----------- src/test/tests.c | 2 +- 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index c46e2c79..0a32621c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +src/.blst_hash src/tests src/tests_* coverage.html diff --git a/bindings/java/Makefile b/bindings/java/Makefile index e8fba487..3f646ef7 100644 --- a/bindings/java/Makefile +++ b/bindings/java/Makefile @@ -52,6 +52,7 @@ all: build test .PHONY: build build: + $(MAKE) -C ../../src/ blst mkdir -p ${LIBRARY_FOLDER} ${CLANG_EXECUTABLE} ${CC_FLAGS} ${CLANG_FLAGS} ${OPTIMIZATION_LEVEL} -Wall -Wextra -Werror -Wno-missing-braces -Wno-unused-parameter -Wno-format ${addprefix -I,${INCLUDE_DIRS}} -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/${JNI_INCLUDE_FOLDER}" -o ${LIBRARY_FOLDER}/${LIBRARY_RESOURCE} ${TARGETS} diff --git a/src/Makefile b/src/Makefile index 4e6d955e..9a5330cf 100644 --- a/src/Makefile +++ b/src/Makefile @@ -85,6 +85,9 @@ else endif # Settings for blst. +BLST_DIR = ../blst +BLST_HASH = $(shell git -C $(BLST_DIR) rev-parse HEAD) +BLST_HASH_FILE = .blst_hash BLST_LIBRARY = ../lib/libblst.a BLST_BUILDSCRIPT = ../blst/build.sh BLST_BUILDSCRIPT_FLAGS = -D__BLST_PORTABLE__ @@ -95,7 +98,6 @@ LIBS = $(BLST_LIBRARY) # Create file lists. SOURCE_FILES := $(shell find . -name '*.c' | sed 's|^\./||' | sort) HEADER_FILES := $(shell find . -name '*.h' | sed 's|^\./||' | sort) -OBJECT_FILES := $(patsubst %.c, %.o, $(SOURCE_FILES)) # There is no tests header file. HEADER_FILES := $(filter-out test/tests.h, $(HEADER_FILES)) @@ -106,26 +108,44 @@ HEADER_FILES := $(filter-out test/tinytest.h, $(HEADER_FILES)) # Core ############################################################################### -all: $(OBJECT_FILES) test +all: test +# This will populate the blst directory will files. $(BLST_BUILDSCRIPT): + @echo "[+] initializing blst submodule" @git submodule update --init -$(BLST_LIBRARY): $(BLST_BUILDSCRIPT) +# This will build blst without condition. +# It will also copy the header files to our include directory. +.PHONY: build_blst +build_blst: $(BLST_BUILDSCRIPT) + @echo "[+] building blst" @cd $(dir $(BLST_BUILDSCRIPT)) && \ ./$(notdir $(BLST_BUILDSCRIPT)) $(BLST_BUILDSCRIPT_FLAGS) && \ cp $(notdir $(BLST_LIBRARY)) ../lib && \ cp bindings/*.h ../inc + @echo $(BLST_HASH) > $(BLST_HASH_FILE) +# This will build blst if the module is out of date. +# We track this with a hidden file. .PHONY: blst -blst: $(BLST_LIBRARY) +blst: + @if [ ! -f $(BLST_HASH_FILE) ] || \ + [ "$(BLST_HASH)" != "$$(cat $(BLST_HASH_FILE))" ]; then \ + $(MAKE) build_blst; \ + fi +# This compiles the tests with optimizations disabled. +# It will re-build if any of our source/header files change. tests: CFLAGS += -O0 -tests: $(SOURCE_FILES) $(HEADER_FILES) $(BLST_LIBRARY) +tests: blst $(SOURCE_FILES) $(HEADER_FILES) + @echo "[+] building tests" @$(CC) $(CFLAGS) -o $@ test/tests.c $(LIBS) +# This simply runs the test suite. .PHONY: test test: tests + @echo "[+] executing tests" @./tests ############################################################################### @@ -133,11 +153,13 @@ test: tests ############################################################################### tests_cov: CFLAGS += -O0 -fprofile-instr-generate -fcoverage-mapping -tests_cov: $(SOURCE_FILES) $(HEADER_FILES) $(BLST_LIBRARY) +tests_cov: blst $(SOURCE_FILES) $(HEADER_FILES) + @echo "[+] building tests with coverage" @$(CC) $(CFLAGS) -o $@ test/tests.c $(LIBS) .PHONY: coverage coverage: tests_cov + @echo "[+] executing tests with coverage" @LLVM_PROFILE_FILE="ckzg.profraw" ./$< @$(XCRUN) llvm-profdata merge --sparse ckzg.profraw -o ckzg.profdata @$(XCRUN) llvm-cov show --instr-profile=ckzg.profdata --format=html \ @@ -155,16 +177,18 @@ ifeq ($(PLATFORM),Darwin) tests_prof: CFLAGS += -L$(shell brew --prefix gperftools)/lib tests_prof: CFLAGS += -I$(shell brew --prefix gperftools)/include endif -tests_prof: $(SOURCE_FILES) $(HEADER_FILES) $(BLST_LIBRARY) +tests_prof: blst $(SOURCE_FILES) $(HEADER_FILES) + @echo "[+] building tests with profiler" @$(CC) $(CFLAGS) -o $@ test/tests.c $(LIBS) .PHONY: run_profiler run_profiler: tests_prof + @echo "[+] executing tests with profiler" @CPUPROFILE_FREQUENCY=1000000000 ./$< .PHONY: profile_% profile_%: run_profiler - @echo Profiling $*... + @echo "[+] generating profiling graph for $*" @pprof --pdf --nodefraction=0.00001 --edgefraction=0.00001 \ ./tests_prof $*.prof > $*.pdf @@ -186,9 +210,10 @@ profile: \ .PHONY: sanitize_% sanitize_%: CFLAGS += -O0 -fsanitize=$* -sanitize_%: $(SOURCE_FILES) $(HEADER_FILES) $(BLST_LIBRARY) - @echo Running sanitize=$*... +sanitize_%: blst $(SOURCE_FILES) $(HEADER_FILES) + @echo "[+] building tests with $* sanitizer" @$(CC) $(CFLAGS) -o $@ test/tests.c $(LIBS) + @echo "[+] executing tests with $* sanitizer" @ASAN_OPTIONS=allocator_may_return_null=1 \ LSAN_OPTIONS=allocator_may_return_null=1 \ ./$@; rm $@ @@ -211,10 +236,10 @@ endif ############################################################################### .PHONY: analyze -analyze: $(SOURCE_FILES) +analyze: blst $(SOURCE_FILES) @rm -rf analysis-report - @for src in $^; do \ - echo "Analyzing $$src..."; \ + @for src in $(SOURCE_FILES); do \ + echo "[+] analyzing $$src..."; \ $(CC) --analyze -Xanalyzer -analyzer-output=html -o analysis-report $(CFLAGS) -c $$src; \ [ -d analysis-report ] && exit 1; true; \ done @@ -225,10 +250,12 @@ analyze: $(SOURCE_FILES) .PHONY: format format: + @echo "[+] executing formatter" @clang-format -i --sort-includes $(SOURCE_FILES) $(HEADER_FILES) .PHONY: clean clean: + @echo "[+] cleaning" @rm -f *.o */*.o *.profraw *.profdata *.html xray-log.* *.prof *.pdf \ - tests tests_cov tests_prof + tests tests_cov tests_prof .blst_hash @rm -rf analysis-report diff --git a/src/test/tests.c b/src/test/tests.c index 1224c1bf..970a2fc6 100644 --- a/src/test/tests.c +++ b/src/test/tests.c @@ -2069,7 +2069,7 @@ static void profile_verify_blob_kzg_proof(void) { } static void profile_verify_blob_kzg_proof_batch(void) { - const int n = 16; + const int n = 4; Blob blobs[n]; Bytes48 commitments[n]; Bytes48 proofs[n];