diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..1d7157b --- /dev/null +++ b/CMakeLists.txt @@ -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) diff --git a/README b/README index aa1dc1d..afd0899 100644 --- a/README +++ b/README @@ -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). diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt new file mode 100644 index 0000000..d83b1e2 --- /dev/null +++ b/lib/CMakeLists.txt @@ -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) diff --git a/scripts/build.sh b/scripts/build.sh index bbf7bd7..f640a4a 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -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 diff --git a/scripts/test.sh b/scripts/test.sh index 4920bd9..697409a 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -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 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..946ba70 --- /dev/null +++ b/tests/CMakeLists.txt @@ -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 +)