Skip to content

Commit 8e71ce3

Browse files
authored
perf: enhance tests with vcpkg feature (#181)
Move test dependencies to vcpkg manifest with the explict declaration. See vcpkg feature: - <https://learn.microsoft.com/en-us/vcpkg/concepts/features> - <https://learn.microsoft.com/en-us/vcpkg/users/examples/selecting-llvm-features> Signed-off-by: msclock <msclock@qq.com>
1 parent 09a7771 commit 8e71ce3

File tree

22 files changed

+160
-22
lines changed

22 files changed

+160
-22
lines changed

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ include(cmake-modules/build/Cppcheck)
2929
include(cmake-modules/build/Ccache)
3030
include(cmake-modules/build/LinkOptimization)
3131
include(cmake-modules/test/Default)
32-
include(cmake-modules/test/GoogleTest)
3332
include(cmake-modules/test/Valgrind)
3433
include(cmake-modules/install/Default)
3534
include(cmake-modules/install/Common)
@@ -47,6 +46,10 @@ include(ConfigureVersion)
4746
include(ConfigureDocs)
4847
include(ConfigureCoverage)
4948

49+
if(test IN_LIST VCPKG_MANIFEST_FEATURES)
50+
include(ConfigureTest)
51+
endif()
52+
5053
add_subdirectory(src)
5154

5255
# Print project final information

cmake/ConfigureTest.cmake

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#[[
2+
Enable tests in CMake, and use CTest module to create a ``BUILD_TESTING``
3+
option to select whether to enable tests.
4+
5+
Note:
6+
Use the module has some requirements:
7+
- Being placed in front of test configuration instructions in cmake.
8+
- Being placed in the source directory root because ctest expects to
9+
find a test file in the build directory root.
10+
]]
11+
12+
include_guard(GLOBAL)
13+
14+
# Prevent the module from being used in the wrong location
15+
if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
16+
message(
17+
FATAL_ERROR
18+
"This module should be in the project root directory and placed "
19+
"in front of any test configuration instructions in cmake")
20+
endif()
21+
22+
# Include CTest module to enable testing support. It creates a ``BUILD_TESTING``
23+
# option that selects whether to enable testing support (``ON`` by default).
24+
include(CTest)
25+
26+
function(add_test_subdirectory src)
27+
if(NOT IS_ABSOLUTE src)
28+
cmake_path(ABSOLUTE_PATH src BASE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
29+
OUTPUT_VARIABLE src)
30+
endif()
31+
message(STATUS "Adding tests from ${src}")
32+
add_subdirectory(${src})
33+
endfunction()

cmake/presets/toolchains/vcpkg.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"toolchainFile": "${sourceDir}/cmake/vcpkg/vcpkg.toolchain.cmake",
88
"cacheVariables": {
99
"VCPKG_INSTALL_OPTIONS": "--no-print-usage",
10+
"VCPKG_MANIFEST_FEATURES": "test",
1011
"VCPKG_OVERLAY_TRIPLETS": "${sourceDir}/cmake/vcpkg/triplets",
1112
"VCPKG_OVERLAY_PORTS": "${sourceDir}/cmake/vcpkg/ports"
1213
}

cmake/vcpkg/bootstrap/vcpkg_export_mode.cmake

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ function(_vcpkg_setup_export export_export_dir export_vcpkg_root)
2929
)
3030
endif()
3131

32+
if(DEFINED VCPKG_FEATURE_FLAGS OR DEFINED CACHE{VCPKG_FEATURE_FLAGS})
33+
list(JOIN VCPKG_FEATURE_FLAGS "," vcpkg_feature_flags)
34+
set(vcpkg_feature_flags "--feature-flags=${vcpkg_feature_flags}")
35+
endif()
36+
37+
foreach(_vcpkg_feature IN LISTS VCPKG_MANIFEST_FEATURES)
38+
list(APPEND vcpkg_additional_manifest_params
39+
"--x-feature=${_vcpkg_feature}")
40+
endforeach()
41+
42+
if(VCPKG_MANIFEST_NO_DEFAULT_FEATURES)
43+
list(APPEND vcpkg_additional_manifest_params "--x-no-default-features")
44+
endif()
45+
3246
set(_additional_args
3347
--vcpkg-root
3448
${_VCPKG_ROOT}
@@ -44,8 +58,11 @@ function(_vcpkg_setup_export export_export_dir export_vcpkg_root)
4458
"${_export_install_dir}")
4559

4660
execute_process(
47-
COMMAND ${vcpkg_cmd} install --x-manifest-root ${CMAKE_SOURCE_DIR}
48-
${_additional_args} ${VCPKG_INSTALL_OPTIONS}
61+
COMMAND
62+
${vcpkg_cmd} install "--x-wait-for-lock"
63+
"--x-manifest-root=${CMAKE_SOURCE_DIR}" ${_additional_args}
64+
${vcpkg_feature_flags} ${vcpkg_additional_manifest_params}
65+
${VCPKG_INSTALL_OPTIONS}
4966
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
5067
RESULT_VARIABLE install_packages_result)
5168

src/compile/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ install_target(
7171

7272
install_dependency(TARGETS ${target_name})
7373

74-
# Tests
75-
message(STATUS "BUILD_TESTING:${BUILD_TESTING}")
76-
7774
if(BUILD_TESTING)
78-
add_subdirectory(tests)
75+
add_test_subdirectory(tests)
7976
endif()

src/compile/tests/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
find_package(GTest CONFIG REQUIRED)
2+
include(GoogleTest)
3+
14
file(GLOB files "*.cpp")
25

36
foreach(_file ${files})

src/exe/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ install(TARGETS ${target_name}
3232

3333
install_dependency(TARGETS ${target_name})
3434

35-
# Test
3635
if(BUILD_TESTING)
37-
add_subdirectory(tests)
36+
add_test_subdirectory(tests)
3837
endif()

src/exe/tests/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
find_package(GTest CONFIG REQUIRED)
2+
include(GoogleTest)
3+
14
file(GLOB files "*.cpp")
25

36
foreach(_file ${files})

src/header/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ install_target(
2525
DEPENDENCIES
2626
spdlog::spdlog)
2727

28-
# Test
2928
if(BUILD_TESTING)
30-
add_subdirectory(tests)
29+
add_test_subdirectory(tests)
3130
endif()

src/header/tests/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
find_package(GTest CONFIG REQUIRED)
2+
include(GoogleTest)
3+
14
file(GLOB files "*.cpp")
25

36
foreach(_file ${files})

0 commit comments

Comments
 (0)