diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e5a4084..ca95f61d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,9 +9,15 @@ if (NOT DEFINED CMAKE_BUILD_TYPE) set (CMAKE_BUILD_TYPE Release CACHE STRING "Build type") endif () -set(CMAKE_CXX_FLAGS "-Wall -Wextra") -set(CMAKE_CXX_FLAGS_DEBUG "-g") -set(CMAKE_CXX_FLAGS_RELEASE "-O3") +if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + set(CMAKE_CXX_FLAGS "/W4") + set(CMAKE_CXX_FLAGS_DEBUG "/Zi /Od") + set(CMAKE_CXX_FLAGS_RELEASE "/O2") +else () + set(CMAKE_CXX_FLAGS "-Wall -Wextra") + set(CMAKE_CXX_FLAGS_DEBUG "-g") + set(CMAKE_CXX_FLAGS_RELEASE "-O3") +endif () list (APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) @@ -27,6 +33,20 @@ option (LIBCMAES_BUILD_TESTS "build tests" OFF) option (LIBCMAES_BUILD_EXAMPLES "build examples" ${LIBCMAES_TOP_LEVEL}) option (LIBCMAES_USE_OPENMP "Use OpenMP for multithreading" ON) option (LIBCMAES_ENABLE_SURROG "support for surrogates" ON) +option (LIBCMAES_USE_GLOG "Use glog for logging" OFF) +option (LIBCMAES_USE_GFLAGS "Use gflags for command line parsing" OFF) +option (LIBCMAES_CRT_DYNAMIC "Use dynamic CRT for MSVC" OFF) + +# Use static/dynamic CRT for MSVC +if (LIBCMAES_CRT_DYNAMIC AND CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + message(STATUS "Using dynamic CRT for MSVC") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD") +else () + message(STATUS "Using static CRT for MSVC") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") +endif () # Offer the user the choice of overriding the installation directories set (INSTALL_LIB_DIR lib${LIB_SUFFIX} @@ -68,6 +88,39 @@ check_include_file (sys/stat.h HAVE_SYS_STAT_H) # ---------- dependencies ---------- find_package (Eigen3 3.0.0 REQUIRED) +if (LIBCMAES_USE_GFLAGS) + find_package (gflags CONFIG REQUIRED) + message(STATUS "GFLAGS_INCLUDE_DIR: ${GFLAGS_INCLUDE_DIR}") + include_directories(${GFLAGS_INCLUDE_DIR}) + + if(CMAKE_BUILD_TYPE STREQUAL "Release") + get_target_property(GFLAGS_LOCATION_RELEASE gflags LOCATION_RELEASE) + get_filename_component(GFLAGS_LIB_DIR ${GFLAGS_LOCATION_RELEASE} DIRECTORY) + else() + get_target_property(GFLAGS_LOCATION_DEBUG gflags LOCATION_DEBUG) + get_filename_component(GFLAGS_LIB_DIR ${GFLAGS_LOCATION_DEBUG} DIRECTORY) + endif() + get_filename_component(GFLAGS_LIB_DIR ${GFLAGS_LIB_DIR} DIRECTORY) + set (GFLAGS_LIB_DIR ${GFLAGS_LIB_DIR}/lib) + message(STATUS "GFLAGS_LIB_DIR: ${GFLAGS_LIB_DIR}") + link_directories(${GFLAGS_LIB_DIR}) +else() + if (LIBCMAES_BUILD_TESTS OR LIBCMAES_BUILD_EXAMPLES) + message(FATAL_ERROR "LIBCMAES_BUILD_TESTS or LIBCMAES_BUILD_EXAMPLES requires LIBCMAES_USE_GFLAGS") + endif() +endif () + +if (LIBCMAES_USE_GLOG) + if (NOT LIBCMAES_USE_GFLAGS) + message(FATAL_ERROR "LIBCMAES_USE_GLOG requires LIBCMAES_USE_GFLAGS") + endif () + message(STATUS "Using glog for logging") + find_package(glog CONFIG REQUIRED) + add_definitions( -DGLOG_USE_GLOG_EXPORT) + add_definitions (-DHAVE_GLOG) + add_definitions (-DHAVE_LIB_GLOG) +endif () + if (LIBCMAES_USE_OPENMP) find_package (OpenMP QUIET) if(NOT OpenMP_CXX_FOUND) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index d5696cc2..e4cbdeb3 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,6 +1,12 @@ macro (cmaes_add_example name) add_executable (sample-${name} sample-${name}.cc) target_link_libraries (sample-${name} cmaes) + if (LIBCMAES_USE_GLOG) + target_link_libraries (sample-${name} glog gflags) + if (WIN32) + target_link_libraries (sample-${name} Dbghelp winmm shlwapi) + endif() + endif () endmacro () cmaes_add_example (code) diff --git a/include/libcmaes/contour.h b/include/libcmaes/contour.h index 19f47533..8cbd987a 100644 --- a/include/libcmaes/contour.h +++ b/include/libcmaes/contour.h @@ -47,11 +47,12 @@ namespace libcmaes /** * \brief function contour as a set of points and values. */ - class contour + template + class contour_ { public: - contour() {} - ~contour() {} + contour_() {} + ~contour_() {} /** * \brief add a contour point. @@ -85,7 +86,10 @@ namespace libcmaes std::vector> _points; }; - std::ostream& operator<<(std::ostream &out, const contour &c) + typedef contour_<0> contour; + + template + std::ostream& operator<<(std::ostream &out, const contour_ &c) { c.print(out); return out; diff --git a/include/libcmaes/eigenmvn.h b/include/libcmaes/eigenmvn.h index fb8b11d4..a39beba8 100644 --- a/include/libcmaes/eigenmvn.h +++ b/include/libcmaes/eigenmvn.h @@ -57,9 +57,12 @@ namespace Eigen { public: static std::mt19937 rng; // The uniform pseudo-random algorithm mutable std::normal_distribution norm; // gaussian combinator - +#if defined(_MSC_VER) && _MSC_VER > 1900 + scalar_normal_dist_op() {} + scalar_normal_dist_op(const scalar_normal_dist_op&) {} +#else EIGEN_EMPTY_STRUCT_CTOR(scalar_normal_dist_op) - +#endif scalar_normal_dist_op &operator=(scalar_normal_dist_op &&other) { if (this != &other) { diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 397bb461..1c196cb0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -60,6 +60,9 @@ target_link_libraries (cmaes PUBLIC Eigen3::Eigen) if (LIBCMAES_USE_OPENMP) target_link_libraries (cmaes PUBLIC OpenMP::OpenMP_CXX) endif () +if (LIBCMAES_USE_GLOG) + target_link_libraries (cmaes PUBLIC glog) +endif () target_compile_features (cmaes PUBLIC cxx_nonstatic_member_init) if (${CMAKE_VERSION} VERSION_GREATER 3.8) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index cf8620b6..e403743c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,12 @@ macro (cmaes_add_test name) add_executable (${name} ${name}.cc) target_link_libraries (${name} cmaes gflags) + if (LIBCMAES_USE_GLOG) + target_link_libraries (${name} glog) + if (WIN32) + target_link_libraries (${name} Dbghelp winmm shlwapi) + endif() + endif () add_test (NAME ${name} COMMAND t_${name}) if (WIN32) set_tests_properties ( diff --git a/tests/test-functions.cc b/tests/test-functions.cc index d4144c48..f34595c8 100644 --- a/tests/test-functions.cc +++ b/tests/test-functions.cc @@ -19,6 +19,9 @@ * along with libcmaes. If not, see . */ +#if defined(_MSC_VER) +#define _USE_MATH_DEFINES +#endif #include "libcmaes/cmaes.h" #include "libcmaes/errstats.h" #include