From 45cf6c25571d35c9442a5f36e4f635a943924ea0 Mon Sep 17 00:00:00 2001 From: cursey Date: Wed, 31 Jan 2024 23:03:32 -0800 Subject: [PATCH] Examples: Use more descriptive names and quit calling them tests --- .github/workflows/build.yml | 14 +- CMakeLists.txt | 235 +++++++++++----------- cmake.toml | 68 ++++--- tests/test4.cpp => example/dll.cpp | 0 tests/test2.cpp => example/midhook.cpp | 0 tests/test1.cpp => example/minimal.cpp | 0 tests/test0.cpp => example/multiple.cpp | 0 tests/test3.cpp => example/threadsafe.cpp | 0 {unittest => test}/allocator.cpp | 0 {unittest => test}/inline_hook.cpp | 0 {unittest => test}/inline_hook.x86_64.cpp | 0 {unittest => test}/main.cpp | 0 {unittest => test}/mid_hook.cpp | 0 {unittest => test}/vmt_hook.cpp | 0 14 files changed, 155 insertions(+), 162 deletions(-) rename tests/test4.cpp => example/dll.cpp (100%) rename tests/test2.cpp => example/midhook.cpp (100%) rename tests/test1.cpp => example/minimal.cpp (100%) rename tests/test0.cpp => example/multiple.cpp (100%) rename tests/test3.cpp => example/threadsafe.cpp (100%) rename {unittest => test}/allocator.cpp (100%) rename {unittest => test}/inline_hook.cpp (100%) rename {unittest => test}/inline_hook.x86_64.cpp (100%) rename {unittest => test}/main.cpp (100%) rename {unittest => test}/mid_hook.cpp (100%) rename {unittest => test}/vmt_hook.cpp (100%) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 97420fa..500dc07 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,8 +11,8 @@ jobs: runs-on: windows-2022 strategy: matrix: - arch: [x64, Win32] - build_type: [Debug, Release] + arch: [ x64, Win32 ] + build_type: [ Debug, Release ] steps: - name: Checkout @@ -22,16 +22,16 @@ jobs: run: | mkdir build cd build - cmake -A ${{matrix.arch}} -DCMAKE_BUILD_TYPE=${{matrix.build_type}} -DSAFETYHOOK_FETCH_ZYDIS=ON -DSAFETYHOOK_BUILD_TESTS=ON -DSAFETYHOOK_AMALGAMATE=ON .. + cmake -A ${{matrix.arch}} -DCMAKE_BUILD_TYPE=${{matrix.build_type}} -DSAFETYHOOK_FETCH_ZYDIS=ON -DSAFETYHOOK_BUILD_EXAMPLES=ON -DSAFETYHOOK_BUILD_TEST=ON -DSAFETYHOOK_AMALGAMATE=ON .. - name: Build run: | cmake --build build --config ${{matrix.build_type}} --parallel - + - name: Test run: | - ./build/${{matrix.build_type}}/unittest.exe - ./build/${{matrix.build_type}}/unittest-amalgamated.exe + ./build/${{matrix.build_type}}/test.exe + ./build/${{matrix.build_type}}/test-amalgamated.exe amalgamated-dist: runs-on: windows-2022 @@ -44,7 +44,7 @@ jobs: run: | python3 amalgamate.py - - name: Compress amalgamated release + - name: Compress amalgamated release uses: vimtor/action-zip@v1.1 with: files: amalgamated-dist/ diff --git a/CMakeLists.txt b/CMakeLists.txt index b693b06..6e9c75d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,7 +26,8 @@ endif() # Options option(SAFETYHOOK_BUILD_DOCS "" OFF) -option(SAFETYHOOK_BUILD_TESTS "" OFF) +option(SAFETYHOOK_BUILD_TEST "" OFF) +option(SAFETYHOOK_BUILD_EXAMPLES "" OFF) option(SAFETYHOOK_AMALGAMATE "" OFF) option(SAFETYHOOK_FETCH_ZYDIS "" OFF) @@ -34,7 +35,7 @@ project(safetyhook) include(FetchContent) -if(SAFETYHOOK_BUILD_TESTS) # build-tests +if(SAFETYHOOK_BUILD_TEST) # build-test message(STATUS "Fetching ut (v2.0.1)...") FetchContent_Declare(ut GIT_REPOSITORY @@ -47,7 +48,7 @@ if(SAFETYHOOK_BUILD_TESTS) # build-tests FetchContent_MakeAvailable(ut) endif() -if(SAFETYHOOK_BUILD_TESTS) # build-tests +if(SAFETYHOOK_BUILD_TEST) # build-test message(STATUS "Fetching xbyak (v6.69)...") FetchContent_Declare(xbyak GIT_REPOSITORY @@ -199,220 +200,208 @@ if(SAFETYHOOK_AMALGAMATE) # amalgamate source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${amalgamation_SOURCES}) endif() -# Target: test0 -if(SAFETYHOOK_BUILD_TESTS) # build-tests - set(test0_SOURCES - "tests/test0.cpp" +# Target: example-minimal +if(SAFETYHOOK_BUILD_EXAMPLES) # build-examples + set(example-minimal_SOURCES + "example/minimal.cpp" cmake.toml ) - add_executable(test0) + add_executable(example-minimal) - target_sources(test0 PRIVATE ${test0_SOURCES}) - source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${test0_SOURCES}) + target_sources(example-minimal PRIVATE ${example-minimal_SOURCES}) + source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${example-minimal_SOURCES}) - target_compile_features(test0 PRIVATE + target_compile_features(example-minimal PRIVATE cxx_std_23 ) - target_link_libraries(test0 PRIVATE + target_link_libraries(example-minimal PRIVATE safetyhook::safetyhook ) get_directory_property(CMKR_VS_STARTUP_PROJECT DIRECTORY ${PROJECT_SOURCE_DIR} DEFINITION VS_STARTUP_PROJECT) if(NOT CMKR_VS_STARTUP_PROJECT) - set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT test0) + set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT example-minimal) endif() endif() -# Target: test1 -if(SAFETYHOOK_BUILD_TESTS) # build-tests - set(test1_SOURCES - "tests/test1.cpp" +# Target: example-multiple +if(SAFETYHOOK_BUILD_EXAMPLES) # build-examples + set(example-multiple_SOURCES + "example/multiple.cpp" cmake.toml ) - add_executable(test1) + add_executable(example-multiple) - target_sources(test1 PRIVATE ${test1_SOURCES}) - source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${test1_SOURCES}) + target_sources(example-multiple PRIVATE ${example-multiple_SOURCES}) + source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${example-multiple_SOURCES}) - target_compile_features(test1 PRIVATE + target_compile_features(example-multiple PRIVATE cxx_std_23 ) - target_link_libraries(test1 PRIVATE + target_link_libraries(example-multiple PRIVATE safetyhook::safetyhook ) get_directory_property(CMKR_VS_STARTUP_PROJECT DIRECTORY ${PROJECT_SOURCE_DIR} DEFINITION VS_STARTUP_PROJECT) if(NOT CMKR_VS_STARTUP_PROJECT) - set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT test1) + set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT example-multiple) endif() endif() -# Target: test2 -if(SAFETYHOOK_BUILD_TESTS) # build-tests - set(test2_SOURCES - "tests/test2.cpp" +# Target: example-midhook +if(SAFETYHOOK_BUILD_EXAMPLES) # build-examples + set(example-midhook_SOURCES + "example/midhook.cpp" cmake.toml ) - add_executable(test2) + add_executable(example-midhook) - target_sources(test2 PRIVATE ${test2_SOURCES}) - source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${test2_SOURCES}) + target_sources(example-midhook PRIVATE ${example-midhook_SOURCES}) + source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${example-midhook_SOURCES}) - target_compile_features(test2 PRIVATE + target_compile_features(example-midhook PRIVATE cxx_std_23 ) - target_link_libraries(test2 PRIVATE + target_link_libraries(example-midhook PRIVATE safetyhook::safetyhook ) get_directory_property(CMKR_VS_STARTUP_PROJECT DIRECTORY ${PROJECT_SOURCE_DIR} DEFINITION VS_STARTUP_PROJECT) if(NOT CMKR_VS_STARTUP_PROJECT) - set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT test2) + set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT example-midhook) endif() endif() -# Target: test3 -if(SAFETYHOOK_BUILD_TESTS) # build-tests - set(test3_SOURCES - "tests/test3.cpp" +# Target: example-threadsafe +if(SAFETYHOOK_BUILD_EXAMPLES) # build-examples + set(example-threadsafe_SOURCES + "example/threadsafe.cpp" cmake.toml ) - add_executable(test3) + add_executable(example-threadsafe) - target_sources(test3 PRIVATE ${test3_SOURCES}) - source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${test3_SOURCES}) + target_sources(example-threadsafe PRIVATE ${example-threadsafe_SOURCES}) + source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${example-threadsafe_SOURCES}) - target_compile_features(test3 PRIVATE + target_compile_features(example-threadsafe PRIVATE cxx_std_23 ) - target_link_libraries(test3 PRIVATE + target_link_libraries(example-threadsafe PRIVATE safetyhook::safetyhook ) get_directory_property(CMKR_VS_STARTUP_PROJECT DIRECTORY ${PROJECT_SOURCE_DIR} DEFINITION VS_STARTUP_PROJECT) if(NOT CMKR_VS_STARTUP_PROJECT) - set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT test3) + set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT example-threadsafe) endif() endif() -# Target: test4 -if(SAFETYHOOK_BUILD_TESTS) # build-tests - set(test4_SOURCES - "tests/test4.cpp" +# Target: example-dll +if(SAFETYHOOK_BUILD_EXAMPLES) # build-examples + set(example-dll_SOURCES + "example/dll.cpp" cmake.toml ) - add_library(test4 SHARED) + add_library(example-dll SHARED) - target_sources(test4 PRIVATE ${test4_SOURCES}) - source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${test4_SOURCES}) + target_sources(example-dll PRIVATE ${example-dll_SOURCES}) + source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${example-dll_SOURCES}) - target_compile_features(test4 PUBLIC + target_compile_features(example-dll PUBLIC cxx_std_23 ) - target_link_libraries(test4 PUBLIC + target_link_libraries(example-dll PUBLIC safetyhook::safetyhook ) endif() -# Target: unittest -if(SAFETYHOOK_BUILD_TESTS) # build-tests - set(unittest_SOURCES - "unittest/allocator.cpp" - "unittest/inline_hook.cpp" - "unittest/inline_hook.x86_64.cpp" - "unittest/main.cpp" - "unittest/mid_hook.cpp" - "unittest/vmt_hook.cpp" +# Target: test +set(test_SOURCES + "test/allocator.cpp" + "test/inline_hook.cpp" + "test/inline_hook.x86_64.cpp" + "test/main.cpp" + "test/mid_hook.cpp" + "test/vmt_hook.cpp" + cmake.toml +) + +add_executable(test) + +target_sources(test PRIVATE ${test_SOURCES}) +source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${test_SOURCES}) + +target_compile_definitions(test PRIVATE + BOOST_UT_DISABLE_MODULE +) + +target_compile_features(test PRIVATE + cxx_std_23 +) + +target_link_libraries(test PRIVATE + Boost::ut + safetyhook::safetyhook + xbyak::xbyak +) + +get_directory_property(CMKR_VS_STARTUP_PROJECT DIRECTORY ${PROJECT_SOURCE_DIR} DEFINITION VS_STARTUP_PROJECT) +if(NOT CMKR_VS_STARTUP_PROJECT) + set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT test) +endif() + +# Target: test-amalgamated +if(SAFETYHOOK_BUILD_TEST AND SAFETYHOOK_AMALGAMATE) # build-amalgamate-test + set(test-amalgamated_SOURCES + "test/allocator.cpp" + "test/inline_hook.cpp" + "test/inline_hook.x86_64.cpp" + "test/main.cpp" + "test/mid_hook.cpp" + "test/vmt_hook.cpp" + "amalgamated-dist/safetyhook.cpp" cmake.toml ) - add_executable(unittest) + add_executable(test-amalgamated) - target_sources(unittest PRIVATE ${unittest_SOURCES}) - source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${unittest_SOURCES}) + target_sources(test-amalgamated PRIVATE ${test-amalgamated_SOURCES}) + source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${test-amalgamated_SOURCES}) - target_compile_features(unittest PRIVATE - cxx_std_23 + target_compile_definitions(test-amalgamated PRIVATE + BOOST_UT_DISABLE_MODULE ) - target_link_libraries(unittest PRIVATE - safetyhook::safetyhook + target_compile_features(test-amalgamated PRIVATE + cxx_std_23 ) - target_compile_definitions(unittest PRIVATE - BOOST_UT_DISABLE_MODULE + target_include_directories(test-amalgamated PRIVATE + "amalgamated-dist/" ) - target_link_libraries(unittest PRIVATE + target_link_libraries(test-amalgamated PRIVATE + Zydis Boost::ut - safetyhook::safetyhook xbyak::xbyak ) get_directory_property(CMKR_VS_STARTUP_PROJECT DIRECTORY ${PROJECT_SOURCE_DIR} DEFINITION VS_STARTUP_PROJECT) if(NOT CMKR_VS_STARTUP_PROJECT) - set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT unittest) + set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT test-amalgamated) endif() -endif() -# Target: unittest-amalgamated -if(SAFETYHOOK_BUILD_TESTS) # build-tests - if(SAFETYHOOK_BUILD_TESTS AND SAFETYHOOK_AMALGAMATE) # build-amalgamate-tests - set(unittest-amalgamated_SOURCES - "unittest/allocator.cpp" - "unittest/inline_hook.cpp" - "unittest/inline_hook.x86_64.cpp" - "unittest/main.cpp" - "unittest/mid_hook.cpp" - "unittest/vmt_hook.cpp" - "amalgamated-dist/safetyhook.cpp" - cmake.toml - ) - - add_executable(unittest-amalgamated) - - target_sources(unittest-amalgamated PRIVATE ${unittest-amalgamated_SOURCES}) - source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${unittest-amalgamated_SOURCES}) - - target_compile_features(unittest-amalgamated PRIVATE - cxx_std_23 - ) - - target_link_libraries(unittest-amalgamated PRIVATE - safetyhook::safetyhook - ) - - target_compile_definitions(unittest-amalgamated PRIVATE - BOOST_UT_DISABLE_MODULE - ) - - target_include_directories(unittest-amalgamated PRIVATE - "amalgamated-dist/" - ) - - target_link_libraries(unittest-amalgamated PRIVATE - Zydis - Boost::ut - xbyak::xbyak - ) - - get_directory_property(CMKR_VS_STARTUP_PROJECT DIRECTORY ${PROJECT_SOURCE_DIR} DEFINITION VS_STARTUP_PROJECT) - if(NOT CMKR_VS_STARTUP_PROJECT) - set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT unittest-amalgamated) - endif() - - set(CMKR_TARGET unittest-amalgamated) - add_dependencies(unittest-amalgamated Amalgamate) + set(CMKR_TARGET test-amalgamated) + add_dependencies(test-amalgamated Amalgamate) - endif() endif() diff --git a/cmake.toml b/cmake.toml index 14c238f..91f3ed3 100644 --- a/cmake.toml +++ b/cmake.toml @@ -4,25 +4,27 @@ name = "safetyhook" [options] SAFETYHOOK_BUILD_DOCS = false -SAFETYHOOK_BUILD_TESTS = false +SAFETYHOOK_BUILD_TEST = false +SAFETYHOOK_BUILD_EXAMPLES = false SAFETYHOOK_AMALGAMATE = false SAFETYHOOK_FETCH_ZYDIS = false [conditions] build-docs = "SAFETYHOOK_BUILD_DOCS" -build-tests = "SAFETYHOOK_BUILD_TESTS" +build-test = "SAFETYHOOK_BUILD_TEST" +build-examples = "SAFETYHOOK_BUILD_EXAMPLES" amalgamate = "SAFETYHOOK_AMALGAMATE" -build-amalgamate-tests = "SAFETYHOOK_BUILD_TESTS AND SAFETYHOOK_AMALGAMATE" +build-amalgamate-test = "SAFETYHOOK_BUILD_TEST AND SAFETYHOOK_AMALGAMATE" fetch-zydis = "SAFETYHOOK_FETCH_ZYDIS" [fetch-content.ut] -condition = "build-tests" +condition = "build-test" git = "https://github.com/boost-ext/ut.git" tag = "v2.0.1" shallow = true [fetch-content.xbyak] -condition = "build-tests" +condition = "build-test" git = "https://github.com/herumi/xbyak.git" tag = "v6.69" shallow = true @@ -95,51 +97,53 @@ add_custom_command( add_custom_target(Amalgamate ALL DEPENDS ${AMALGAMATED_FILE} ${AMALGAMATED_HEADER}) """ -[template.test] -condition = "build-tests" +[template.example] +condition = "build-examples" type = "executable" link-libraries = ["safetyhook::safetyhook"] compile-features = ["cxx_std_23"] -[template.test-dll] -condition = "build-tests" +[template.example-dll] +condition = "build-examples" type = "shared" link-libraries = ["safetyhook::safetyhook"] compile-features = ["cxx_std_23"] -[target.test0] -type = "test" -sources = ["tests/test0.cpp"] +[target.example-minimal] +type = "example" +sources = ["example/minimal.cpp"] -[target.test1] -type = "test" -sources = ["tests/test1.cpp"] +[target.example-multiple] +type = "example" +sources = ["example/multiple.cpp"] -[target.test2] -type = "test" -sources = ["tests/test2.cpp"] +[target.example-midhook] +type = "example" +sources = ["example/midhook.cpp"] -[target.test3] -type = "test" -sources = ["tests/test3.cpp"] +[target.example-threadsafe] +type = "example" +sources = ["example/threadsafe.cpp"] -[target.test4] -type = "test-dll" -sources = ["tests/test4.cpp"] +[target.example-dll] +type = "example-dll" +sources = ["example/dll.cpp"] -[target.unittest] -type = "test" -sources = ["unittest/*.cpp"] +[target.test] +type = "executable" +sources = ["test/*.cpp"] link-libraries = ["Boost::ut", "safetyhook::safetyhook", "xbyak::xbyak"] compile-definitions = ["BOOST_UT_DISABLE_MODULE"] +compile-features = ["cxx_std_23"] -[target.unittest-amalgamated] -condition = "build-amalgamate-tests" -type = "test" -sources = ["unittest/*.cpp", "amalgamated-dist/safetyhook.cpp"] +[target.test-amalgamated] +condition = "build-amalgamate-test" +type = "executable" +sources = ["test/*.cpp", "amalgamated-dist/safetyhook.cpp"] include-directories = ["amalgamated-dist/"] link-libraries = ["Zydis", "Boost::ut", "xbyak::xbyak"] compile-definitions = ["BOOST_UT_DISABLE_MODULE"] +compile-features = ["cxx_std_23"] cmake-after = """ -add_dependencies(unittest-amalgamated Amalgamate) +add_dependencies(test-amalgamated Amalgamate) """ \ No newline at end of file diff --git a/tests/test4.cpp b/example/dll.cpp similarity index 100% rename from tests/test4.cpp rename to example/dll.cpp diff --git a/tests/test2.cpp b/example/midhook.cpp similarity index 100% rename from tests/test2.cpp rename to example/midhook.cpp diff --git a/tests/test1.cpp b/example/minimal.cpp similarity index 100% rename from tests/test1.cpp rename to example/minimal.cpp diff --git a/tests/test0.cpp b/example/multiple.cpp similarity index 100% rename from tests/test0.cpp rename to example/multiple.cpp diff --git a/tests/test3.cpp b/example/threadsafe.cpp similarity index 100% rename from tests/test3.cpp rename to example/threadsafe.cpp diff --git a/unittest/allocator.cpp b/test/allocator.cpp similarity index 100% rename from unittest/allocator.cpp rename to test/allocator.cpp diff --git a/unittest/inline_hook.cpp b/test/inline_hook.cpp similarity index 100% rename from unittest/inline_hook.cpp rename to test/inline_hook.cpp diff --git a/unittest/inline_hook.x86_64.cpp b/test/inline_hook.x86_64.cpp similarity index 100% rename from unittest/inline_hook.x86_64.cpp rename to test/inline_hook.x86_64.cpp diff --git a/unittest/main.cpp b/test/main.cpp similarity index 100% rename from unittest/main.cpp rename to test/main.cpp diff --git a/unittest/mid_hook.cpp b/test/mid_hook.cpp similarity index 100% rename from unittest/mid_hook.cpp rename to test/mid_hook.cpp diff --git a/unittest/vmt_hook.cpp b/test/vmt_hook.cpp similarity index 100% rename from unittest/vmt_hook.cpp rename to test/vmt_hook.cpp