Skip to content

Commit

Permalink
Improve the build system (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtraglia authored Aug 15, 2024
1 parent 6ed2dc7 commit de4f9eb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
src/.blst_hash
src/tests
src/tests_*
coverage.html
Expand Down
1 change: 1 addition & 0 deletions bindings/java/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
55 changes: 41 additions & 14 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand All @@ -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))
Expand All @@ -106,38 +108,58 @@ 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

###############################################################################
# Coverage
###############################################################################

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 \
Expand All @@ -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

Expand All @@ -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 $@
Expand All @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion src/test/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit de4f9eb

Please sign in to comment.