Skip to content

Commit

Permalink
Adding test support for WINRT
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgenyAgafonchikov committed Jun 30, 2015
1 parent 8869150 commit 6a6d58d
Show file tree
Hide file tree
Showing 19 changed files with 303 additions and 21 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ OCV_OPTION(BUILD_ANDROID_EXAMPLES "Build examples for Android platform"
OCV_OPTION(BUILD_DOCS "Create build rules for OpenCV Documentation" ON IF NOT WINRT)
OCV_OPTION(BUILD_EXAMPLES "Build all examples" OFF )
OCV_OPTION(BUILD_PACKAGE "Enables 'make package_source' command" ON IF NOT WINRT)
OCV_OPTION(BUILD_PERF_TESTS "Build performance tests" ON IF (NOT IOS AND NOT WINRT) )
OCV_OPTION(BUILD_TESTS "Build accuracy & regression tests" ON IF (NOT IOS AND NOT WINRT) )
OCV_OPTION(BUILD_PERF_TESTS "Build performance tests" ON IF (NOT IOS) )
OCV_OPTION(BUILD_TESTS "Build accuracy & regression tests" ON IF (NOT IOS) )
OCV_OPTION(BUILD_WITH_DEBUG_INFO "Include debug info into debug libs (not MSCV only)" ON )
OCV_OPTION(BUILD_WITH_STATIC_CRT "Enables use of staticaly linked CRT for staticaly linked OpenCV" ON IF MSVC )
OCV_OPTION(BUILD_WITH_DYNAMIC_IPP "Enables dynamic linking of IPP (only for standalone IPP)" OFF )
Expand Down
17 changes: 17 additions & 0 deletions cmake/OpenCVCompilerOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ macro(add_extra_compiler_option option)
endif()
endmacro()

# Gets environment variable and puts its value to the corresponding preprocessor definition
# Useful for WINRT that has no access to environment variables
macro(add_env_definitions option)
set(value $ENV{${option}})
if("${value}" STREQUAL "")
message(WARNING "${option} environment variable is empty. Please set it to appropriate location to get correct results")
else()
string(REPLACE "\\" "\\\\" value ${value})
endif()
add_definitions("-D${option}=\"${value}\"")
endmacro()

# OpenCV fails some tests when 'char' is 'unsigned' by default
add_extra_compiler_option(-fsigned-char)

Expand Down Expand Up @@ -286,6 +298,11 @@ if(MSVC12 AND NOT CMAKE_GENERATOR MATCHES "Visual Studio")
set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} /FS")
endif()

# Adding additional using directory for WindowsPhone 8.0 to get Windows.winmd properly
if(WINRT_PHONE AND WINRT_8_0)
set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} /AI\$(WindowsSDK_MetadataPath)")
endif()

# Extra link libs if the user selects building static libs:
if(NOT BUILD_SHARED_LIBS AND CMAKE_COMPILER_IS_GNUCXX AND NOT ANDROID)
# Android does not need these settings because they are already set by toolchain file
Expand Down
47 changes: 46 additions & 1 deletion cmake/OpenCVModule.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,28 @@ macro(ocv_create_module)
_ocv_create_module(${ARGN})
set(the_module_target ${the_module})
endif()

if(WINRT)
# removing APPCONTAINER from modules to run from console
# in case of usual starting of WinRT test apps output is missing
# so starting of console version w/o APPCONTAINER is required to get test results
# also this allows to use opencv_extra test data for these tests
if(NOT "${the_module}" STREQUAL "opencv_ts" AND NOT "${the_module}" STREQUAL "opencv_hal")
add_custom_command(TARGET ${the_module}
POST_BUILD
COMMAND link.exe /edit /APPCONTAINER:NO $(TargetPath))
endif()

if("${the_module}" STREQUAL "opencv_ts")
# copy required dll files; WinRT apps need these dlls that are usually substituted by Visual Studio
# however they are not on path and need to be placed with executables to run from console w/o APPCONTAINER
add_custom_command(TARGET ${the_module}
POST_BUILD
COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\msvcp$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\msvcp$(PlatformToolsetVersion)_app.dll\""
COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\msvcr$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\msvcr$(PlatformToolsetVersion)_app.dll\""
COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\vccorlib$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\vccorlib$(PlatformToolsetVersion)_app.dll\"")
endif()
endif()
endmacro()

macro(_ocv_create_module)
Expand Down Expand Up @@ -902,6 +924,10 @@ endmacro()
function(ocv_add_perf_tests)
ocv_debug_message("ocv_add_perf_tests(" ${ARGN} ")")

if(WINRT)
set(OPENCV_DEBUG_POSTFIX "")
endif()

set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf")
if(BUILD_PERF_TESTS AND EXISTS "${perf_path}")
__ocv_parse_test_sources(PERF ${ARGN})
Expand Down Expand Up @@ -936,11 +962,18 @@ function(ocv_add_perf_tests)
DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
)

if(ENABLE_SOLUTION_FOLDERS)
set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
endif()

if(WINRT)
# removing APPCONTAINER from tests to run from console
# look for detailed description inside of ocv_create_module macro above
add_custom_command(TARGET "opencv_perf_${name}"
POST_BUILD
COMMAND link.exe /edit /APPCONTAINER:NO $(TargetPath))
endif()

if(NOT BUILD_opencv_world)
_ocv_add_precompiled_headers(${the_target})
endif()
Expand All @@ -958,6 +991,10 @@ endfunction()
function(ocv_add_accuracy_tests)
ocv_debug_message("ocv_add_accuracy_tests(" ${ARGN} ")")

if(WINRT)
set(OPENCV_DEBUG_POSTFIX "")
endif()

set(test_path "${CMAKE_CURRENT_LIST_DIR}/test")
if(BUILD_TESTS AND EXISTS "${test_path}")
__ocv_parse_test_sources(TEST ${ARGN})
Expand Down Expand Up @@ -1000,6 +1037,14 @@ function(ocv_add_accuracy_tests)
get_target_property(LOC ${the_target} LOCATION)
add_test(${the_target} "${LOC}")

if(WINRT)
# removing APPCONTAINER from tests to run from console
# look for detailed description inside of ocv_create_module macro above
add_custom_command(TARGET "opencv_test_${name}"
POST_BUILD
COMMAND link.exe /edit /APPCONTAINER:NO $(TargetPath))
endif()

if(NOT BUILD_opencv_world)
_ocv_add_precompiled_headers(${the_target})
endif()
Expand Down
1 change: 1 addition & 0 deletions modules/calib3d/test/test_fisheye.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "test_precomp.hpp"
#include <opencv2/ts/cuda_test.hpp>
#include "../src/fisheye.hpp"
#include "opencv2/videoio.hpp"

class fisheyeTest : public ::testing::Test {

Expand Down
1 change: 0 additions & 1 deletion modules/features2d/test/test_detectors_regression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
//M*/

#include "test_precomp.hpp"
#include "opencv2/highgui.hpp"

using namespace std;
using namespace cv;
Expand Down
1 change: 0 additions & 1 deletion modules/features2d/test/test_keypoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
//M*/

#include "test_precomp.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/core/core_c.h"

using namespace std;
Expand Down
1 change: 0 additions & 1 deletion modules/features2d/test/test_matchers_algorithmic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
//M*/

#include "test_precomp.hpp"
#include "opencv2/highgui.hpp"

using namespace std;
using namespace cv;
Expand Down
1 change: 0 additions & 1 deletion modules/features2d/test/test_orb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
//M*/

#include "test_precomp.hpp"
#include "opencv2/highgui.hpp"

using namespace std;
using namespace cv;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
//M*/

#include "test_precomp.hpp"
#include "opencv2/highgui.hpp"

using namespace std;
using namespace cv;
Expand Down
1 change: 0 additions & 1 deletion modules/imgproc/test/test_contours.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
//M*/

#include "test_precomp.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;
using namespace std;
Expand Down
9 changes: 8 additions & 1 deletion modules/ts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
set(the_description "The ts module")

if(IOS OR WINRT)
if(IOS)
ocv_module_disable(ts)
endif()

set(OPENCV_MODULE_TYPE STATIC)
set(OPENCV_MODULE_IS_PART_OF_WORLD FALSE)

if(WINRT)
# WINRT doesn't have access to environment variables
# so adding corresponding macros during CMake run
add_env_definitions(OPENCV_TEST_DATA_PATH)
add_env_definitions(OPENCV_PERF_VALIDATION_DIR)
endif()

ocv_warnings_disable(CMAKE_CXX_FLAGS -Wundef)

ocv_add_module(ts INTERNAL opencv_core opencv_imgproc opencv_imgcodecs opencv_videoio opencv_highgui)
Expand Down
99 changes: 99 additions & 0 deletions modules/ts/include/opencv2/ts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,102 @@ int main(int argc, char **argv) \
#endif

#include "opencv2/ts/ts_perf.hpp"

#ifdef WINRT
#ifndef __FSTREAM_EMULATED__
#define __FSTREAM_EMULATED__
#include <stdlib.h>
#include <fstream>
#include <sstream>

#undef ifstream
#undef ofstream
#define ifstream ifstream_emulated
#define ofstream ofstream_emulated

namespace std {

class ifstream : public stringstream
{
FILE* f;
public:
ifstream(const char* filename, ios_base::openmode mode = ios_base::in)
: f(NULL)
{
string modeStr("r");
printf("Open file (read): %s\n", filename);
if (mode & ios_base::binary)
modeStr += "b";
f = fopen(filename, modeStr.c_str());

if (f == NULL)
{
printf("Can't open file: %s\n", filename);
return;
}
fseek(f, 0, SEEK_END);
size_t sz = ftell(f);
if (sz > 0)
{
char* buf = (char*) malloc(sz);
fseek(f, 0, SEEK_SET);
if (fread(buf, 1, sz, f) == sz)
{
this->str(std::string(buf, sz));
}
free(buf);
}
}

~ifstream() { close(); }
bool is_open() const { return f != NULL; }
void close()
{
if (f)
fclose(f);
f = NULL;
this->str("");
}
};

class ofstream : public stringstream
{
FILE* f;
public:
ofstream(const char* filename, ios_base::openmode mode = ios_base::out)
: f(NULL)
{
open(filename, mode);
}
~ofstream() { close(); }
void open(const char* filename, ios_base::openmode mode = ios_base::out)
{
string modeStr("w+");
if (mode & ios_base::trunc)
modeStr = "w";
if (mode & ios_base::binary)
modeStr += "b";
f = fopen(filename, modeStr.c_str());
printf("Open file (write): %s\n", filename);
if (f == NULL)
{
printf("Can't open file (write): %s\n", filename);
return;
}
}
bool is_open() const { return f != NULL; }
void close()
{
if (f)
{
fwrite(reinterpret_cast<const char *>(this->str().c_str()), this->str().size(), 1, f);
fclose(f);
}
f = NULL;
this->str("");
}
};

} // namespace std
#endif // __FSTREAM_EMULATED__
#endif // WINRT
4 changes: 2 additions & 2 deletions modules/ts/include/opencv2/ts/ts_gtest.h
Original file line number Diff line number Diff line change
Expand Up @@ -2924,7 +2924,7 @@ inline const char* StrNCpy(char* dest, const char* src, size_t n) {
// StrError() aren't needed on Windows CE at this time and thus not
// defined there.

#if !GTEST_OS_WINDOWS_MOBILE
#if !GTEST_OS_WINDOWS_MOBILE && !defined WINRT
inline int ChDir(const char* dir) { return chdir(dir); }
#endif
inline FILE* FOpen(const char* path, const char* mode) {
Expand All @@ -2948,7 +2948,7 @@ inline int Close(int fd) { return close(fd); }
inline const char* StrError(int errnum) { return strerror(errnum); }
#endif
inline const char* GetEnv(const char* name) {
#if GTEST_OS_WINDOWS_MOBILE
#if GTEST_OS_WINDOWS_MOBILE || defined WINRT
// We are on Windows CE, which has no environment variables.
return NULL;
#elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)
Expand Down
8 changes: 8 additions & 0 deletions modules/ts/src/ts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,11 @@ static int tsErrorCallback( int status, const char* func_name, const char* err_m

void TS::init( const string& modulename )
{
#ifndef WINRT
char* datapath_dir = getenv("OPENCV_TEST_DATA_PATH");
#else
char* datapath_dir = OPENCV_TEST_DATA_PATH;
#endif

if( datapath_dir )
{
Expand Down Expand Up @@ -684,7 +688,11 @@ void parseCustomOptions(int argc, char **argv)

test_ipp_check = parser.get<bool>("test_ipp_check");
if (!test_ipp_check)
#ifndef WINRT
test_ipp_check = getenv("OPENCV_IPP_CHECK") != NULL;
#else
test_ipp_check = false;
#endif
}

/* End of file. */
Loading

0 comments on commit 6a6d58d

Please sign in to comment.