cmake的 - 如何与输入数据的文件复制到生成输出文件夹
我使用的克利翁和CMake的项目建设。我创建了谷歌测试版本,配置和我的项目树的样子:cmake的 - 如何与输入数据的文件复制到生成输出文件夹
的标记生成器的测试很简单:分词器应该打开源文件和输出令牌。
这是tokenizer_test我的CMakeLists.txt文件:
include_directories(${gtest_SOURCE_DIRS}/include ${gtest_SOURCE_DIRS})
add_subdirectory(test_src)
add_executable(run_tokenizer_tests
tokenizer_test.cpp ${CMAKE_SOURCE_DIR}/includes/tokenizer.h
${CMAKE_SOURCE_DIR}/src/tokenizer.cpp
)
target_link_libraries(run_tokenizer_tests gtest gtest_main)
我是否能够把测试源(就像图片上的0.cpp)附近的可执行文件或者我应该写我自己的测试脚本?我该怎么做?
可以使用configure_file CMake的功能,与COPYONLY
标志。它将复制文件而不替换任何变量引用或其他内容。你CMakeLists.txt
附近tokenizer_test.cpp
应包含以下内容:
configure_file(test_src/0.cpp 0.cpp COPYONLY)
PS:我建议你根据测试你在干什么,你的情况0.cpp
名称应该像tokenizer_test_parse_input.cpp
重命名每一个源文件并输入文件,它会更好这个文件放在靠近`tokenizer_test.cpp”。
您可以拨打file(COPY source DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
其他变量见https://cmake.org/Wiki/CMake_Useful_Variables。
这是我们项目的一小部分,它可以做到这一点,还有更多。
总体来看,这段代码可以让你避免在文件系统上的特定文件位置的依存关系,只要你的build目录是活的,因为文件路径会在你的单元测试进行硬编码。
它简化自动化生成和检查 - 无需调用测试跑步之前跟踪到哪里cd
。
这也提高了单元测试的代码的可读性。
CMakeLists.txt
:
# list all test images
set(test_data
orig_5_15Fps_3_27.png
orig_5_15Fps_5_34.png
....
)
# Loop over all items in the "test_data" list
# Copy PNG, JPEG and BMP images to the directory, where test binaries are created
# And generate full paths to them for hardcoding in the include file test_config.h
foreach(df ${test_data})
# copy images to build dir
if(${df} MATCHES "((jp|pn)g|bmp)$")
file(COPY ${df} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
set(df_file_path ${CMAKE_CURRENT_BINARY_DIR}/${df})
else()
set(df_file_path ${CMAKE_CURRENT_SOURCE_DIR}/${df})
endif()
# generate some C++ code in CMake variables IMAGE_PATHS and IMAGE_IDS
# (see below)
if (NOT IMAGE_PATHS)
set(IMAGE_PATHS " \"${df_file_path}\"")
else()
set(IMAGE_PATHS "${IMAGE_PATHS},\n \"${df_file_path}\"")
endif()
string(REGEX REPLACE "[^a-zA-Z0-9]" "_" df_id ${df})
if (NOT IMAGE_IDS)
set(IMAGE_IDS " img_${df_id}")
else()
set(IMAGE_IDS "${IMAGE_IDS},\n img_${df_id}")
endif()
endforeach()
set(TEST_PATH \"${CMAKE_CURRENT_BINARY_DIR}\")
configure_file(test_config.h.in test_config.h @ONLY) # see below for test_config.h.in
...
include_directories(${CMAKE_CURRENT_BINARY_DIR}) # make test_config.h visible for compiler
add_executable (some_unit_test some_unit_test.cpp)
add_test(NAME some_unit_test COMMAND some_unit_test WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH})
...
add_executable (...)
add_test( ...)
...
文件test_config.h.in
。
/* DO NOT EDIT THIS FILE, IT IS AUTOGENERATED!
All your changes will be overwritten.
If you want to add new test data files,
add them to the `test_data` list in file CMakeLists.txt
*/
#ifndef __TEST_CONFIG_H__
#define __TEST_CONFIG_H__
const char* test_path = @[email protected]; //!< full path to test data, without trailing slash
//! full paths to all test images
const char* image_paths[] = {
@[email protected]
};
enum image_ids { //!< test file names, converted to enum constants
@[email protected]
};
#endif
电话configure_file
取代@[email protected]
,@[email protected]
和@[email protected]
与他们的价值观。
这里是build目录文件test_config.h
配置方式中的模样。
/* DO NOT EDIT THIS FILE, IT IS AUTOGENERATED!
All your changes will be overwritten.
If you want to add new test data files,
add them to the `test_data` list in file CMakeLists.txt
*/
#ifndef __TEST_CONFIG_H__
#define __TEST_CONFIG_H__
const char* test_path = "F:/projects/project/build64/test"; //!< full path to test data, without trailing slash
//! full paths to all test images
const char* image_paths[] = {
"F:/projects/project/build64/test/orig_5_15Fps_3_27.png",
"F:/projects/project/build64/test/orig_5_15Fps_5_34.png",
...
};
enum image_ids { //!< test file names, converted to enum constants
img_orig_5_15Fps_3_27_png,
img_orig_5_15Fps_5_34_png,
...
};
#endif
使用在测试:
enum image_ids
用于image_paths
阵列中读取索引。恕我直言,它比image_paths[0]
好得多,因为它清楚地显示了读取哪个图像。
看一看[CMake变量](https://cmake.org/cmake/help/latest/manual/cmake-variables.7.html)。有几个为您提供可用于测试的项目和源代码目录(通过在构建时将它们添加为宏,或在运行测试时将它们作为参数传递)。 –
@Someprogrammerdude,不幸的是,我找不到,所以我写了这个。 –
@ДмитрийТерехов“测试来源”是什么意思“0.cpp”包含什么?为tokenizer_tests输入数据? – Liastre