diff --git a/CMakeLists.txt b/CMakeLists.txt index 13c315d..a7e9b33 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,13 @@ cmake_minimum_required(VERSION 3.27) +# Project and generated target name. project(boxes LANGUAGES C) - +# Boxes version, fallback in case not in a boxes Git repository. set(BVERSION "2.2.2-dev") set(GLOBALCONF "/usr/share/boxes") +# Get version from Git repository (tags). find_package(Git) if (GIT_EXECUTABLE) execute_process( @@ -20,7 +22,9 @@ if (GIT_EXECUTABLE) endif() endif () -add_custom_command( # TODO: use configure_file? +# Generate boxes.h include file. +# TODO: Use CMake "configure_file" . +add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/boxes.h COMMAND sed -e "s|--BVERSION--|${BVERSION}|; s|--GLOBALCONF--|${GLOBALCONF}|" ${CMAKE_CURRENT_SOURCE_DIR}/src/boxes.in.h > ${CMAKE_CURRENT_BINARY_DIR}/boxes.h @@ -28,11 +32,12 @@ add_custom_command( # TODO: use configure_file? VERBATIM ) - +# Find packages (probably needs some Conan packages installed, i.e., run "conan install" before). find_package(libunistring REQUIRED) find_package(PCRE2 REQUIRED) find_package(Curses REQUIRED) +# Generate parser. find_package(BISON REQUIRED) bison_target(boxes_parser ${CMAKE_CURRENT_SOURCE_DIR}/src/parser.y @@ -41,6 +46,7 @@ bison_target(boxes_parser COMPILE_FLAGS "--warnings=all --verbose" ) +# Generate lexer. find_package(FLEX REQUIRED) flex_target(boxes_scanner ${CMAKE_CURRENT_SOURCE_DIR}/src/lexer.l @@ -50,16 +56,34 @@ flex_target(boxes_scanner add_flex_bison_dependency(boxes_scanner boxes_parser) +# The executable target name is the project name. +# Note that a static library with the project name and suffix "_lib" is +# built first, and used for testing as well as linking the executable. set(TARGET ${PROJECT_NAME}) +# Includes for both, the static library and the executable. +set(INCLUDES + ./src + ${CMAKE_CURRENT_BINARY_DIR} +) + +# Libraries that are used to compile the static library, and link the executable. +set(LIBRARIES + libunistring::libunistring + pcre2::pcre2 + Curses::Curses + ${FLEX_LIBRARIES} +) + +# List all generated files. set(GENERATED_FILES ${CMAKE_CURRENT_BINARY_DIR}/boxes.h ${BISON_boxes_parser_OUTPUTS} ${FLEX_boxes_scanner_OUTPUTS} ) -set(SOURCES - src/boxes.c +# Source files for the static library. +set(SOURCES_LIB src/bxstring.c src/cmdline.c src/discovery.c @@ -76,18 +100,38 @@ set(SOURCES src/unicode.c ) -add_executable(${TARGET} - ${SOURCES} +# Source files for only the exectuable (i.e., containing "main"). +set(SOURCES_EXE + src/boxes.c +) + + +# Build static library. +add_library(${TARGET}_lib STATIC ${GENERATED_FILES} + ${SOURCES_LIB} +) +target_include_directories(${TARGET}_lib + PUBLIC ${INCLUDES} +) +target_link_libraries(${TARGET}_lib + PUBLIC ${LIBRARIES} +) + +# Build boxes exectuable. +add_executable(${TARGET} + ${SOURCES_EXE} ) target_include_directories(${TARGET} - PRIVATE - ./src - ${CMAKE_CURRENT_BINARY_DIR} + PRIVATE ${INCLUDES} ) target_link_libraries(${TARGET} - libunistring::libunistring - pcre2::pcre2 - Curses::Curses - ${FLEX_LIBRARIES} + PRIVATE + ${TARGET}_lib + ${LIBRARIES} ) + + +# Enable and include unit tests. +enable_testing() +add_subdirectory(utest) diff --git a/README-cmake-conan.md b/README-cmake-conan.md index f3f8d95..5d1febb 100644 --- a/README-cmake-conan.md +++ b/README-cmake-conan.md @@ -7,9 +7,10 @@ Building boxes requires two tools to be installed: - [Conan](https://conan.io), a package manager for C/C++, is used to resolve required library and tool dependencies - libunistring - pcre2 - - ncurses + - ncurses (official Conan 2 version should be available soon) - flex (library and tool) - bison (tool) + - cmocka (for testing) - [CMake](https://cmake.org) is used for building. Make sure that a C compiler and the tools for the underlying build environment (e.g., [GNU Make](https://www.gnu.org/software/make/) or [Ninja](https://ninja-build.org)) are installed. @@ -23,19 +24,36 @@ conan profile detect ## Building boxes -It is recommended to use the generated presets for building: +It is recommended to use the generated presets for building. For a debug build, follow these steps: ```sh export BUILD_TYPE="Debug" +export BUILD_PRESET="conan-debug" conan install . --output-folder=./build/${BUILD_TYPE} \ --settings build_type=${BUILD_TYPE} \ --build missing -cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON --preset conan-debug -cmake --build --preset conan-debug +cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON --preset ${BUILD_PRESET} +cmake --build --preset ${BUILD_PRESET} -ctest --verbose --preset conan-debug +ctest --verbose --preset ${BUILD_PRESET} +``` + +Release build is similar: + +```sh +export BUILD_TYPE="Release" +export BUILD_PRESET="conan-release" + +conan install . --output-folder=./build/${BUILD_TYPE} \ + --settings build_type=${BUILD_TYPE} \ + --build missing + +cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON --preset ${BUILD_PRESET} +cmake --build --preset ${BUILD_PRESET} + +ctest --verbose --preset ${BUILD_PRESET} ``` Of course the "classic" way works, too: diff --git a/conanfile.txt b/conanfile.txt index 97e5ba0..9ef3cc7 100644 --- a/conanfile.txt +++ b/conanfile.txt @@ -8,6 +8,9 @@ flex/2.6.4 # https://conan.io/center/recipes/flex flex/2.6.4 # https://conan.io/center/recipes/flex bison/3.8.2 # https://conan.io/center/recipes/bison +[test_requires] +cmocka/1.1.5 # https://conan.io/center/recipes/cmocka + [generators] CMakeDeps CMakeToolchain diff --git a/utest/CMakeLists.txt b/utest/CMakeLists.txt new file mode 100644 index 0000000..0ad2ff5 --- /dev/null +++ b/utest/CMakeLists.txt @@ -0,0 +1,35 @@ + +# Test project name. +project(boxes_utest LANGUAGES C) + +# Find required packages. If used from Conan, make sure to run "conan install" before. +find_package(cmocka REQUIRED) + + +set(SUT boxes_lib) +set(TARGET ${PROJECT_NAME}) + +set(SOURCES + bxstring_test.c + cmdline_test.c + regulex_test.c + tools_test.c + unicode_test.c + global_mock.c + utest_tools.c + main.c +) + +add_executable(${TARGET} + ${SOURCES} +) +target_link_libraries(${TARGET} + ${SUT} + cmocka::cmocka +) + + +add_test( + NAME ${TARGET} + COMMAND ${TARGET} +)