[CMake] WORKING_DIRECTORY problem: custom targets vs. "testing"
Stefan
stefan at schweter.it
Fri Dec 20 05:55:45 EST 2013
Hi,
I'm not sure, if I am doing something wrong, so I made a small example program + cmake script to show the difference between the working_directory option in custom targets and the testing option.
My folder structure is very simple:
./file.txt (fill it with one simple text line)
./reader.hpp (a small c++ class for reading the content of file.txt
./main.cpp (main class for calling the reader.hpp to read the content
./main_test.cpp (boot unit test, fails, when the file is empty or does not exist)
reader.hpp:
#include <fstream>
struct reader {
static inline std::string get_content() {
std::ifstream input_fs("file.txt");
std::string content;
std::getline(input_fs, content);
return content;
}
};
main.cpp:
#include "reader.hpp"
#include <iostream>
auto main() -> int {
std::cout << reader::get_content() << std::endl;
return 0;
}
main_test.cpp:
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE abbreviation_test
#include <boost/test/unit_test.hpp>
#include "reader.hpp"
BOOST_AUTO_TEST_CASE(read_file_content)
{
std::string content = reader::get_content();
bool has_content = content.length() > 0;
BOOST_CHECK_EQUAL(has_content, true);
}
And my cmake script:
cmake_minimum_required (VERSION 2.6)
project (workingdir)
find_package(Boost 1.44.0 COMPONENTS unit_test_framework REQUIRED)
add_definitions("-Wall -std=c++0x")
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
include_directories("${PROJECT_SOURCE_DIR}")
include_directories("${PROJECT_BINARY_DIR}")
add_executable (workingdir main.cpp)
target_link_libraries(workingdir)
add_executable(workingdir_test main_test.cpp)
target_link_libraries(workingdir_test ${Boost_LIBRARIES})
add_custom_target (
run0
DEPENDS workingdir_test
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMAND workingdir_test
)
enable_testing()
add_test (
Workingdir_test ${EXECUTABLE_OUTPUT_PATH}/workingdir_test
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
)
To test it:
mkdir build && cd build
cmake ..
make
Now I could run the custom target with "make run0" -> it will show the "file.txt" content, because I set the working directory to the project source dir.
But: When I try to run "make test" -> although I set the working directory, the test will fail. I guess the working directory is ./build and not ./
How can I set the working directory to ${PROJECT_SOURCE_DIR} correctly for my testcases?
Thanks in advance + regards,
Stefan
PS: I am using cmake version 2.8.12.1 on Arch.
More information about the CMake
mailing list