Skip to content

Commit

Permalink
tests: refactor build logic into a Makefile
Browse files Browse the repository at this point in the history
This saves us from a lot of recompilation overhead during development.
It also lets us build faster due parallelising with `make -j`.
  • Loading branch information
rsekman authored and Oleksiy-Yakovenko committed Oct 7, 2023
1 parent db6b74a commit bf751b6
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 61 deletions.
105 changes: 105 additions & 0 deletions Tests.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
default: runtests

BUILD=testbuild
.PHONY: runtests

CC=clang
CXX=clang++

ORIGIN=$(shell pwd)
STATIC_DEPS:=$(ORIGIN)/static-deps
STATIC_ROOT:=$(STATIC_DEPS)/lib-x86-64
INCLUDE=-I external/googletest/googletest \
-I external/googletest/googletest/include \
-I external/mp4p/include \
-I plugins/libparser \
-I shared \
-I . \
-I src \
-I include \
-I src/ConvertUTF \
-I src/scriptable \
-I plugins/coreaudio \
-I$(STATIC_ROOT)/include

CFLAGS=-fblocks -fcommon -O3 $(INCLUDE) \
-D_FORTIFY_SOURCE=0 \
-D_GNU_SOURCE \
-DHAVE_LOG2=1 \
-DDOCDIR=\"\" \
-DPREFIX=\"\" \
-DLIBDIR=\"\" \
-DVERSION=\"\" \
-DUSE_LIBMAD \
-DUSE_LIBMPG123 \
-DXCTEST \
-DGOOGLETEST_STATIC
LIBRARIES=-lmad -lmpg123 -lcurl -ldispatch -lpthread -lBlocksRuntime -lm -ljansson -ldl
LDFLAGS=-L$(STATIC_ROOT)/lib -L$(STATIC_ROOT)/lib/x86_64-linux-gnu


TEST_C_SOURCES=$(wildcard src/*.c \
shared/*.c \
src/scriptable/*.c \
shared/scriptable/*.c \
plugins/libparser/*.c \
plugins/nullout/*.c \
src/ConvertUTF/*.c \
src/metadata/*.c \
plugins/m3u/*.c \
plugins/vfs_curl/*.c \
plugins/shellexec/*.c \
external/mp4p/src/*.c \
external/wcwidth/*.c \
src/md5/*.c \
plugins/mp3/*.c \
Tests/*.c)
TEST_C_SOURCES:=$(filter-out src/main.c,$(TEST_C_SOURCES))
TEST_C_OBJS:=$(addprefix $(BUILD)/,\
$(notdir $(patsubst %.c,%.o,$(TEST_C_SOURCES))) \
)
ifdef DDB_TEST_SUITES
TEST_CPP_SOURCES=$(addprefix Tests/,$(addsuffix Tests.cpp,$(DDB_TEST_SUITES)))
else
TEST_CPP_SOURCES=$(wildcard Tests/*Tests.cpp)
endif
TEST_CPP_SOURCES:=Tests/gtest-runner.cpp $(TEST_CPP_SOURCES)
TEST_CPP_OBJS:=$(addprefix $(BUILD)/,\
$(notdir $(patsubst %.cpp,%.o,$(TEST_CPP_SOURCES))) \
)

GOOGLE_TEST_SOURCES=external/googletest/googletest/src/gtest-all.cc
GOOGLE_TEST_OBJS:=$(addprefix $(BUILD)/,$(notdir $(patsubst %.cc,%.o,$(GOOGLE_TEST_SOURCES))))

VPATH=src \
$(addprefix src/,scriptable ConvertUTF md5 metadata) \
$(addprefix plugins/,libparser m3u mp3 nullout shellexec vfs_curl) \
$(addprefix external/,mp4p/src wcwidth googletest/googletest/src) \
shared \
shared/scriptable \
Tests

$(BUILD)/%.o: %.c
$(CC) -std=c99 $(CFLAGS) -c $< -o $@

$(BUILD)/%.o: %.cpp
$(CXX) -std=c++14 $(CFLAGS) -c $< -o $@

$(BUILD)/%.o: %.cc
$(CXX) -std=c++14 $(CFLAGS) -c $< -o $@

$(BUILD)/runtests: $(TEST_C_OBJS) $(GOOGLE_TEST_OBJS) $(TEST_CPP_OBJS)
$(CXX) $(LDFLAGS) $(TEST_C_OBJS) $(GOOGLE_TEST_OBJS) $(TEST_CPP_OBJS) $(LIBRARIES) -o $@

runtests: $(BUILD)/runtests
./$(BUILD)/runtests

$(BUILD)/Test%: $(TEST_C_OBJS) $(GOOGLE_TEST_OBJS) $(BUILD)/%Tests.o $(BUILD)/gtest-runner.o
$(CXX) $(LDFLAGS) $^ $(LIBRARIES) -o $@

Test%: $(BUILD)/Test%
$<

clean:
rm -f $(BUILD)/*.o
rm -f $(BUILD)/runtests
71 changes: 10 additions & 61 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,20 @@

shopt -s extglob

BUILD=testbuild
TEST_C_SOURCES="src/*.c shared/*.c src/scriptable/*.c shared/scriptable/*.c plugins/libparser/*.c plugins/nullout/*.c src/ConvertUTF/*.c src/metadata/*.c plugins/m3u/*.c plugins/vfs_curl/*.c plugins/shellexec/*.c external/mp4p/src/*.c external/wcwidth/*.c src/md5/*.c plugins/mp3/*.c Tests/*.c"
if [ $# -ne 0 ]; then
TEST_CPP_SOURCES="Tests/gtest-runner.cpp"
for t in "$@"; do
TEST_CPP_SOURCES="${TEST_CPP_SOURCES} Tests/${t}Tests.cpp"
done
else
TEST_CPP_SOURCES="Tests/*.cpp"
fi
GOOGLE_TEST_SOURCES="external/googletest/googletest/src/gtest-all.cc"
ORIGIN=$PWD
STATIC_DEPS=static-deps
INCLUDE="-I external/googletest/googletest -I external/googletest/googletest/include -I external/mp4p/include -I plugins/libparser -I shared -I . -I src -I include -I src/ConvertUTF -I src/scriptable -I plugins/coreaudio -I$ORIGIN/$STATIC_DEPS/lib-x86-64/include"
LDFLAGS="-L$ORIGIN/$STATIC_DEPS/lib-x86-64/lib -L$ORIGIN/$STATIC_DEPS/lib-x86-64/lib/x86_64-linux-gnu"
LIBRARIES="-lmad -lmpg123 -lcurl -ldispatch -lpthread -lBlocksRuntime -lm -ljansson -ldl"
export LD_LIBRARY_PATH="$ORIGIN/$STATIC_DEPS/lib-x86-64/lib"

CC="${CC:-clang}"
CXX="${CXX:-clang++}"

CFLAGS="-fblocks -fcommon -O3 -D_FORTIFY_SOURCE=0 -D_GNU_SOURCE $INCLUDE -DHAVE_LOG2=1 -DDOCDIR=\"\" -DPREFIX=\"\" -DLIBDIR=\"\" -DVERSION=\"\" -DUSE_LIBMAD -DUSE_LIBMPG123 -DXCTEST -DGOOGLETEST_STATIC"

# hack: move c++ runtime out of the lib folder, to allow linking test runner to
# the newer c++ runtime
rm -rf "$BUILD"
mkdir -p "$BUILD"

mkdir -p "$BUILD/cpplibs"
mv $ORIGIN/$STATIC_DEPS/lib-x86-64/lib/*c++*.so* "$BUILD/cpplibs/"

compile_tests() {
for file in $TEST_C_SOURCES; do
base=$(basename $file)
if [ "$base" = "main.c" ]; then
continue
fi
echo $CC -std=c99 $CFLAGS -c "$file" -o "$BUILD/${base%.@(c|cc|cpp)}.o"
$CC -std=c99 $CFLAGS -c "$file" -o "$BUILD/${base%.@(c|cc|cpp)}.o" || return $?
done

for file in $GOOGLE_TEST_SOURCES $TEST_CPP_SOURCES; do
base=$(basename $file)
echo $CXX -std=c++14 $CFLAGS -c "$file" -o "$BUILD/${base%.@(c|cc|cpp)}.o"
$CXX -std=c++14 $CFLAGS -c "$file" -o "$BUILD/${base%.@(c|cc|cpp)}.o" || return $?
done

$CXX $LDFLAGS $BUILD/*.o $LIBRARIES -o "$BUILD/runtests" || return $?

return 0
}
STATIC_DEPS=$ORIGIN/static-deps/lib-x86-64/lib
BUILD=testbuild

restore_cpp_libs(){
mv "$BUILD"/cpplibs/* $ORIGIN/$STATIC_DEPS/lib-x86-64/lib/
}
BAKDIR=$STATIC_DEPS/bak
mkdir -p $BAKDIR
mv -vt $BAKDIR $STATIC_DEPS/*c++*.so*

compile_tests
export DDB_TEST_SUITES=${@}
mkdir -p $BUILD
export LD_LIBRARY_PATH=$STATIC_DEPS
make -j --file=Tests.mk
code=$?
if [ $code -ne 0 ]; then
restore_cpp_libs
exit $code
fi

"$BUILD/runtests"
code=$?
mv -vt $STATIC_DEPS $BAKDIR/*c++*.so*

restore_cpp_libs
exit $code

0 comments on commit bf751b6

Please sign in to comment.