Replace build scripts with CMake

Rebuilding everything each time was getting a bit slow.
This commit is contained in:
Camden Dixie O'Brien 2024-10-27 00:12:17 +01:00
parent 403d081e13
commit c58cabd2e6
6 changed files with 66 additions and 45 deletions

19
CMakeLists.txt Normal file
View File

@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.10)
project(regex-engine LANGUAGES C)
option(SANITIZERS "Enable address and UB sanitizers")
enable_testing()
function(set_default_target_options target)
set_property(TARGET ${target} PROPERTY C_STANDARD 11)
target_compile_options(${target} PRIVATE -Wall -Wextra -pedantic)
if(${SANITIZERS})
target_compile_options(${target} PRIVATE -fsanitize=address,undefined)
target_link_options(${target} PRIVATE -fsanitize=address,undefined)
endif()
endfunction()
add_subdirectory(lib)
add_subdirectory(tests)

16
README
View File

@ -21,14 +21,14 @@ syntax; the expression syntax I intend to support follows.
Building and Running Tests
There are two scripts, build.sh and test.sh, which will (much to
everybody's shock) build and run tests. The build script uses Clang
but the code is ISO C11 so it should compile just fine with GCC or
something instead.
The build uses CMake. There are two scripts, build.sh and test.sh,
which will (much to everybody's shock) build the project and run the
tests. The build script specifies Clang but the code is ISO C11 so it
should compile just fine with GCC or something instead.
sh scripts/build.sh # Compile library and test code
sh scripts/test.sh # Run tests
scripts/build.sh # Compile library and tests
scripts/test.sh # Run tests
There is also an entr.sh script which will watch all the project's
files and rebuild and rerun the tests on any changes, using the entr
tool.
files and rebuild then rerun the tests on any changes (uses entr --
hence the name of the script).

9
lib/CMakeLists.txt Normal file
View File

@ -0,0 +1,9 @@
add_library(lib
construct.c
desugar.c
fsa.c
parse.c
regex.c
)
set_default_target_options(lib)
target_include_directories(lib PUBLIC include)

View File

@ -1,29 +1,7 @@
#!/bin/sh
cd "$(git rev-parse --show-toplevel)"
CFLAGS="$CFLAGS -std=c11 -pedantic -Wall -Wextra"
CFLAGS="$CFLAGS -fsanitize=address,undefined"
CFLAGS="$CFLAGS -O0 -ggdb"
if [ ! -e build ]; then mkdir build; else rm build/*; fi
# Build library
clang $CFLAGS -Ilib/include -c lib/parse.c -o build/parse.o
clang $CFLAGS -Ilib/include -c lib/desugar.c -o build/desugar.o
clang $CFLAGS -Ilib/include -c lib/regex.c -o build/regex.o
clang $CFLAGS -Ilib/include -c lib/fsa.c -o build/fsa.o
clang $CFLAGS -Ilib/include -c lib/construct.c -o build/construct.o
ar -crs build/lib.a build/parse.o build/desugar.o build/regex.o \
build/fsa.o build/construct.o
# Build tests
clang $CFLAGS -Itests/include -c tests/testing.c -o build/testing.o
clang $CFLAGS -Ilib/include -Itests/include -o build/parse_tests \
tests/parse_tests.c build/testing.o build/lib.a
clang $CFLAGS -Ilib/include -Itests/include -o build/desugar_tests \
tests/desugar_tests.c build/testing.o build/lib.a
clang $CFLAGS -Ilib/include -Itests/include -o build/fsa_tests \
tests/fsa_tests.c build/testing.o build/lib.a
clang $CFLAGS -Ilib/include -Itests/include -o build/construct_tests \
tests/construct_tests.c build/testing.o build/lib.a
if [ ! -e build ]; then
cmake -Bbuild -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=clang \
-DSANITIZERS=ON
fi
cmake --build build

View File

@ -1,12 +1,3 @@
#!/bin/sh
cd "$(git rev-parse --show-toplevel)"
fails=0
build/parse_tests || fails=`expr $fails + 1`
build/desugar_tests || fails=`expr $fails + 1`
build/fsa_tests || fails=`expr $fails + 1`
build/construct_tests || fails=`expr $fails + 1`
if [ $fails -eq 0 ]; then echo Tests OK; fi
ctest --test-dir build

24
tests/CMakeLists.txt Normal file
View File

@ -0,0 +1,24 @@
add_library(testing testing.c)
set_default_target_options(testing)
target_include_directories(testing PUBLIC include)
function(add_test_suite source)
string(REGEX REPLACE ".c$" "" name ${source})
add_executable(${name} ${source})
set_default_target_options(${name})
target_link_libraries(${name} PRIVATE lib testing)
add_test(NAME ${name} COMMAND ${name})
endfunction()
function(add_test_suites)
foreach(source ${ARGN})
add_test_suite(${source})
endforeach()
endfunction()
add_test_suites(
construct_tests.c
desugar_tests.c
fsa_tests.c
parse_tests.c
)