Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows support #247

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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}
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
12 changes: 8 additions & 4 deletions include/libcmaes/contour.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ namespace libcmaes
/**
* \brief function contour as a set of points and values.
*/
class contour
template<int T_iDummy>
class contour_
{
public:
contour() {}
~contour() {}
contour_() {}
~contour_() {}

/**
* \brief add a contour point.
Expand Down Expand Up @@ -85,7 +86,10 @@ namespace libcmaes
std::vector<std::pair<double,double>> _points;
};

std::ostream& operator<<(std::ostream &out, const contour &c)
typedef contour_<0> contour;

template<int T_iDummy>
std::ostream& operator<<(std::ostream &out, const contour_<T_iDummy> &c)
{
c.print(out);
return out;
Expand Down
7 changes: 5 additions & 2 deletions include/libcmaes/eigenmvn.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ namespace Eigen {
public:
static std::mt19937 rng; // The uniform pseudo-random algorithm
mutable std::normal_distribution<Scalar> 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) {
Expand Down
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
3 changes: 3 additions & 0 deletions tests/test-functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
* along with libcmaes. If not, see <http://www.gnu.org/licenses/>.
*/

#if defined(_MSC_VER)
#define _USE_MATH_DEFINES
#endif
#include "libcmaes/cmaes.h"
#include "libcmaes/errstats.h"
#include <map>
Expand Down