Skip to content

Commit

Permalink
build: Improve build system (#327)
Browse files Browse the repository at this point in the history
* *Add top level CMakeLists.txt, to make it easier to use both independently and from within other projects
* Add a unit_test target, to attach all unit tests to

* * Fix unit tests for windows
* Silence cmake warning regarding missing top project
* update .gitignore for vscode

---------

Co-authored-by: BobSmun <[email protected]>
  • Loading branch information
BobSmun and BobSmun authored Dec 16, 2024
1 parent 63504f5 commit e026ff1
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 30 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ jobs:

- name: 🛠️ Build
run: |
cd tests
mkdir -p build
cd build
CC=gcc-12 CXX=g++-12 cmake \
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
-DCMAKE_C_FLAGS="-fuse-ld=lld --coverage" \
-DCMAKE_CXX_FLAGS="-fuse-ld=lld --coverage" \
-DIMHEX_PATTERNS_ENABLE_UNIT_TESTS=ON \
-DLIBPL_ENABLE_TESTS=OFF \
-DLIBPL_ENABLE_CLI=OFF \
..
make -j4
make -j4 unit_tests
- name: 🧪 Perform Unit Tests
run: |
cd tests/build
cd build
ctest --output-on-failure -j 4
- name: 📎 Validate JSON Files
Expand Down
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@

tests/cmake*/
tests/build*/
build/

.vscode/
.devcontainer/
.cache/
.idea/
.DS_Store
.DS_Store

compile_commands.json
30 changes: 30 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
cmake_minimum_required(VERSION 3.16)

project(ImHex-Patterns)

option(IMHEX_PATTERNS_ENABLE_UNIT_TESTS "Enable building unit tests for ImHex-Patterns" OFF)

# if enabled, add a unit_test custom target for all the unit tests to be registered against
if(IMHEX_PATTERNS_ENABLE_UNIT_TESTS)
if(NOT TARGET unit_tests)
enable_testing()
add_custom_target(unit_tests)
endif()
endif()

# If this has been manually cloned into another project, libpl may already have been set up
if(NOT TARGET libpl)
include(FetchContent)

FetchContent_Declare(
pattern_language
GIT_REPOSITORY https://github.com/WerWolv/PatternLanguage
GIT_TAG master
)

FetchContent_MakeAvailable(pattern_language)
endif()

if(IMHEX_PATTERNS_ENABLE_UNIT_TESTS)
add_subdirectory(tests)
endif()
18 changes: 1 addition & 17 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
cmake_minimum_required(VERSION 3.16)

project(tests)

set(CMAKE_CXX_STANDARD 20)

include(FetchContent)

FetchContent_Declare(
pattern_language
GIT_REPOSITORY https://github.com/WerWolv/PatternLanguage
GIT_TAG master
)

FetchContent_MakeAvailable(pattern_language)

enable_testing()

add_subdirectory(patterns)
add_subdirectory(includes)
add_subdirectory(magic)

add_custom_target(test_all DEPENDS patterns_tests includes_test magic_test)
add_subdirectory(magic)
7 changes: 4 additions & 3 deletions tests/includes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ add_executable(includes_test
target_include_directories(includes_test PRIVATE include)
target_link_libraries(includes_test PRIVATE libpl fmt::fmt-header-only)

set_target_properties(includes_test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set_target_properties(includes_test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

message(STATUS "Adding Include Tests")
foreach (include IN LISTS INCLUDES)
file(RELATIVE_PATH INCLUDE_NAME ${PATTERN_INCLUDES} ${include})

set(TEST_NAME "Includes/${INCLUDE_NAME}")

add_test(NAME ${TEST_NAME} COMMAND includes_test "${INCLUDE_NAME}" "${include}" "${PATTERN_INCLUDES}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME ${TEST_NAME} COMMAND includes_test "${INCLUDE_NAME}" "${include}" "${PATTERN_INCLUDES}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set_tests_properties(${TEST_NAME} PROPERTIES SKIP_RETURN_CODE 77)
endforeach ()
endforeach ()
add_dependencies(unit_tests ${PROJECT_NAME})
7 changes: 4 additions & 3 deletions tests/magic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ pkg_search_module(MAGIC libmagic>=5.39 REQUIRED)
target_include_directories(magic_test PRIVATE include ${MAGIC_INCLUDE_DIRS})
target_link_libraries(magic_test PRIVATE libpl ${MAGIC_LINK_LIBRARIES} fmt::fmt-header-only)

set_target_properties(magic_test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set_target_properties(magic_test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

message(STATUS "Adding Magic Tests")
foreach (magic_file IN LISTS MAGIC_FILES)
file(RELATIVE_PATH MAGIC_NAME ${MAGIC_FOLDER} ${magic_file})

set(TEST_NAME "Magic/${MAGIC_NAME}")

add_test(NAME ${TEST_NAME} COMMAND magic_test "${MAGIC_NAME}" "${magic_file}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME ${TEST_NAME} COMMAND magic_test "${MAGIC_NAME}" "${magic_file}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set_tests_properties(${TEST_NAME} PROPERTIES SKIP_RETURN_CODE 77)
endforeach ()
endforeach ()
add_dependencies(unit_tests ${PROJECT_NAME})
7 changes: 4 additions & 3 deletions tests/patterns/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ add_executable(patterns_tests
target_include_directories(patterns_tests PRIVATE include)
target_link_libraries(patterns_tests PRIVATE libpl fmt::fmt-header-only)

set_target_properties(patterns_tests PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set_target_properties(patterns_tests PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

message(STATUS "Adding Pattern Tests")
foreach (pattern IN LISTS PATTERNS)
Expand All @@ -33,13 +33,14 @@ foreach (pattern IN LISTS PATTERNS)
foreach (TEST_FILE IN LISTS TEST_FILES)
get_filename_component(TEST_FILENAME ${TEST_FILE} NAME)
set(TEST_NAME "Patterns/${PATTERN_NAME}/${TEST_FILENAME}")
add_test(NAME ${TEST_NAME} COMMAND patterns_tests "${PATTERN_NAME}" "${pattern}" "${PATTERN_INCLUDES}" "${TEST_FILE}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME ${TEST_NAME} COMMAND patterns_tests "${PATTERN_NAME}" "${pattern}" "${PATTERN_INCLUDES}" "${TEST_FILE}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set_tests_properties(${TEST_NAME} PROPERTIES SKIP_RETURN_CODE 77)
endforeach ()
else ()
set(TEST_NAME "Patterns/${PATTERN_NAME}")
add_test(NAME ${TEST_NAME} COMMAND patterns_tests "${PATTERN_NAME}" "${pattern}" "${PATTERN_INCLUDES}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME ${TEST_NAME} COMMAND patterns_tests "${PATTERN_NAME}" "${pattern}" "${PATTERN_INCLUDES}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set_tests_properties(${TEST_NAME} PROPERTIES SKIP_RETURN_CODE 77)
message(STATUS " No test file available for ${PATTERN_NAME} pattern")
endif ()
endforeach ()
add_dependencies(unit_tests ${PROJECT_NAME})

0 comments on commit e026ff1

Please sign in to comment.