Skip to content

Commit

Permalink
Add CMake based build support
Browse files Browse the repository at this point in the history
- Fix super build (e.g. FetchContent) integration
- cmake: option() honors normal variables (CMP0077)
- add some Osi test to cmake

adapt test to ignore when mps file are not present
  • Loading branch information
Mizux committed Dec 6, 2024
1 parent cafff3e commit a3d0a30
Show file tree
Hide file tree
Showing 12 changed files with 721 additions and 108 deletions.
23 changes: 4 additions & 19 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
# Ignore VS files
*.suo
*.db
*.bak
*.user
**/.vs/
**/MSVisualStudio/**/Release/
**/MSVisualStudio/**/ReleaseParallel/
**/MSVisualStudio/**/Debug/

# Ignore files created during unit tests
**/MSVisualStudio/**/*.mps
**/MSVisualStudio/**/*.lp
**/MSVisualStudio/**/*.out

# Ignore autotools cache files
aclocal.m4*
acinclude.m4
autom4te*
*.swp
.vs/
build/
cache/
139 changes: 139 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
cmake_minimum_required(VERSION 3.16)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

include(ParseAc)
parse_ac(VERSION MAJOR MINOR PATCH)

project(Osi VERSION ${VERSION} LANGUAGES CXX C)
set(PROJECT_NAMESPACE coin)
message(STATUS "${PROJECT_NAME} version: ${PROJECT_VERSION}")
#message(STATUS "major: ${PROJECT_VERSION_MAJOR}")
#message(STATUS "minor: ${PROJECT_VERSION_MINOR}")
#message(STATUS "patch: ${PROJECT_VERSION_PATCH}")

# Default Build Type to be Release
get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(isMultiConfig)
if(NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_CONFIGURATION_TYPES "Release;Debug" CACHE STRING
"Choose the type of builds, options are: Debug Release RelWithDebInfo MinSizeRel. (default: Release;Debug)"
FORCE)
endif()
message(STATUS "Configuration types: ${CMAKE_CONFIGURATION_TYPES}")
else()
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING
"Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel. (default: Release)"
FORCE)
endif()
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
endif()

# Layout build dir like install dir
include(GNUInstallDirs)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_BUILD_RPATH_USE_ORIGIN ON)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH ON)
if(UNIX)
option(BUILD_SHARED_LIBS "Build shared libraries (.so or .dylib)." ON)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
# for multi-config build system (e.g. Xcode, Ninja Multi-Config)
foreach(OutputConfig IN LISTS CMAKE_CONFIGURATION_TYPES)
string(TOUPPER ${OutputConfig} OUTPUTCONFIG)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/${OutputConfig}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/${OutputConfig}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/${OutputConfig}/${CMAKE_INSTALL_BINDIR})
endforeach()
else()
# Currently Only support static build for windows
option(BUILD_SHARED_LIBS "Build shared libraries (.dll)." OFF)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
# for multi-config builds (e.g. msvc)
foreach(OutputConfig IN LISTS CMAKE_CONFIGURATION_TYPES)
string(TOUPPER ${OutputConfig} OUTPUTCONFIG)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/${OutputConfig}/${CMAKE_INSTALL_BINDIR})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/${OutputConfig}/${CMAKE_INSTALL_BINDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/${OutputConfig}/${CMAKE_INSTALL_BINDIR})
endforeach()
endif()

if(MSVC AND BUILD_SHARED_LIBS)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()

# config options
if(MSVC)
# Build with multiple processes
add_definitions(/MP)
add_definitions(/D_CRT_SECURE_NO_WARNINGS /D_CRT_SECURE_NO_DEPRECATE)
# MSVC warning suppressions
add_definitions(
/wd4018 # 'expression' : signed/unsigned mismatch
/wd4065 # switch statement contains 'default' but no 'case' labels
/wd4101 # 'identifier' : unreferenced local variable
/wd4102 # 'label' : unreferenced label
/wd4244 # 'conversion' conversion from 'type1' to 'type2', possible loss of data
/wd4267 # 'var' : conversion from 'size_t' to 'type', possible loss of data
/wd4309 # 'conversion' : truncation of constant value
/wd4805 # 'operation' : unsafe mix of type 'type1' and type 'type2' in operation.
/wd4996 # The compiler encountered a deprecated declaration.
)
endif()
if(APPLE)
set(
CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -Wno-inconsistent-missing-override -Wno-unused-command-line-argument -Wno-unused-result -Wno-exceptions"
)
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15" CACHE STRING "Minimum OS X deployment version")
endif()

# ZLIB
if(NOT TARGET ZLIB::ZLIB)
find_package(ZLIB)
endif()
if(ZLIB_FOUND OR TARGET ZLIB::ZLIB)
message(STATUS "Use zlib")
set(HAVE_ZLIB_H "1" CACHE INTERNAL "Use zlib")
set(COIN_HAS_ZLIB "1" CACHE INTERNAL "Use zlib")
endif()

# PThread
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads)
if(CMAKE_USE_PTHREADS_INIT)
set(PTHREADS_FOUND TRUE)
else()
set(PTHREADS_FOUND FALSE)
endif()

# CoinUtils
if(NOT TARGET Coin::CoinUtils)
find_package(CoinUtils REQUIRED CONFIG)
endif()

include(CheckEnv)
include(CTest)

add_subdirectory(Osi)

include(GNUInstallDirs)
install(EXPORT ${PROJECT_NAME}Targets
NAMESPACE Coin::
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
include(CMakePackageConfigHelpers)
configure_package_config_file(cmake/Config.cmake.in
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
write_basic_package_version_file(
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
COMPATIBILITY SameMajorVersion)
install(
FILES
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
COMPONENT Devel)
116 changes: 116 additions & 0 deletions Osi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
set(NAME "OSI")

# PTHREAD
if(PTHREADS_FOUND)
set(${NAME}_PTHREADS "1" CACHE INTERNAL "Use pthread")
endif()

set(COIN_${NAME}_CHECKLEVEL "0" CACHE INTERNAL
"${NAME} check level")
set(COIN_${NAME}_VERBOSITY "0" CACHE INTERNAL
"${NAME} verbosity level")
configure_file(config.h.cmake.in config.h)
configure_file(config_osi.h.cmake.in config_osi.h)

set(_SRCS
src/Osi/OsiAuxInfo.cpp
src/Osi/OsiBranchingObject.cpp
src/Osi/OsiChooseVariable.cpp
src/Osi/OsiColCut.cpp
src/Osi/OsiCut.cpp
src/Osi/OsiCuts.cpp
src/Osi/OsiNames.cpp
src/Osi/OsiPresolve.cpp
src/Osi/OsiRowCut.cpp
src/Osi/OsiRowCutDebugger.cpp
src/Osi/OsiSolverBranch.cpp
src/Osi/OsiSolverInterface.cpp)

set(_HDRS
src/Osi/OsiConfig.h
src/Osi/OsiAuxInfo.hpp
src/Osi/OsiBranchingObject.hpp
src/Osi/OsiChooseVariable.hpp
src/Osi/OsiColCut.hpp
src/Osi/OsiCollections.hpp
src/Osi/OsiCut.hpp
src/Osi/OsiCuts.hpp
src/Osi/OsiPresolve.hpp
src/Osi/OsiRowCut.hpp
src/Osi/OsiRowCutDebugger.hpp
src/Osi/OsiSolverBranch.hpp
src/Osi/OsiSolverInterface.hpp
src/Osi/OsiSolverParameters.hpp)

include(GNUInstallDirs)

add_library(Osi ${_SRCS} ${_HDRS})
target_include_directories(Osi PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/Osi>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:include/coin>)
target_compile_definitions(Osi
PUBLIC HAVE_CONFIG_H
PRIVATE OSI_BUILD)
if(CMAKE_VERSION VERSION_LESS "3.8.2")
set_property(TARGET Osi PROPERTY CXX_STANDARD 11)
set_property(TARGET Osi PROPERTY CXX_STANDARD_REQUIRED ON)
else()
target_compile_features(Osi PUBLIC cxx_std_11)
endif()
if(APPLE)
set_target_properties(Osi PROPERTIES INSTALL_RPATH "@loader_path")
elseif(UNIX)
set_target_properties(Osi PROPERTIES INSTALL_RPATH "$ORIGIN")
endif()
target_link_libraries(Osi PUBLIC Coin::CoinUtils)
set_target_properties(Osi PROPERTIES
PUBLIC_HEADER "${_HDRS};${CMAKE_CURRENT_BINARY_DIR}/config_osi.h"
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR})
add_library(Coin::Osi ALIAS Osi)

# Install
install(TARGETS Osi
EXPORT ${PROJECT_NAME}Targets
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/coin
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

if(BUILD_TESTING)
add_library(OsiCommonTest "")
target_sources(OsiCommonTest PRIVATE
src/OsiCommonTest/OsiColCutTest.cpp
src/OsiCommonTest/OsiCutsTest.cpp
src/OsiCommonTest/OsiNetlibTest.cpp
src/OsiCommonTest/OsiRowCutDebuggerTest.cpp
src/OsiCommonTest/OsiRowCutTest.cpp
src/OsiCommonTest/OsiSimplexAPITest.cpp
src/OsiCommonTest/OsiSolverInterfaceTest.cpp
src/OsiCommonTest/OsiUnitTests.hpp
src/OsiCommonTest/OsiUnitTestUtils.cpp)
target_include_directories(OsiCommonTest PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/OsiCommonTest>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:include/coin>)
target_compile_definitions(OsiCommonTest
PUBLIC HAVE_CONFIG_H
PRIVATE OSI_BUILD)
target_compile_features(OsiCommonTest PUBLIC cxx_std_11)
if(APPLE)
set_target_properties(OsiCommonTest PROPERTIES INSTALL_RPATH "@loader_path")
elseif(UNIX)
set_target_properties(OsiCommonTest PROPERTIES INSTALL_RPATH "$ORIGIN")
endif()
target_link_libraries(OsiCommonTest PUBLIC Coin::Osi)
set_target_properties(OsiCommonTest PROPERTIES
PUBLIC_HEADER "src/OsiCommonTest/OsiUnitTests.hpp"
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR})
add_library(Coin::OsiCommonTest ALIAS OsiCommonTest)

add_subdirectory(test)
endif()
112 changes: 112 additions & 0 deletions Osi/config.h.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*config.h. Generated by configure_file.*/

#define COIN_HAS_COINUTILS 1

/* VERSION */
#define VERSION "${VERSION}"
/* ${NAME}_VERSION */
#define ${NAME}_VERSION "${VERSION}"
/* ${NAME}_VERSION_MAJOR */
#define ${NAME}_VERSION_MAJOR ${MAJOR}
/* ${NAME}_VERSION_MINOR */
#define ${NAME}_VERSION_MINOR ${MINOR}
/* ${NAME}_VERSION_RELEASE */
#define ${NAME}_VERSION_RELEASE ${PATCH}

/* HAVE_MATH_H */
#cmakedefine HAVE_MATH_H ${HAVE_MATH_H}
/* HAVE_CTYPE_H */
#cmakedefine HAVE_CTYPE_H ${HAVE_CTYPE_H}
/* HAVE_INTTYPES_H */
#cmakedefine HAVE_INTTYPES_H ${HAVE_INTTYPES_H}
/* HAVE_FLOAT_H */
#cmakedefine HAVE_FLOAT_H ${HAVE_FLOAT_H}
/* HAVE_IEEEFP_H */
#cmakedefine HAVE_IEEEFP_H ${HAVE_IEEEFP_H}
/* HAVE_STDARG_H */
#cmakedefine HAVE_STDARG_H ${HAVE_STDARG_H}
/* HAVE_STDDEF_H */
#cmakedefine HAVE_STDDEF_H ${HAVE_STDDEF_H}
/* HAVE_STDINT_H */
#cmakedefine HAVE_STDINT_H ${HAVE_STDINT_H}
/* HAVE_STDIO_H */
#cmakedefine HAVE_STDIO_H ${HAVE_STDIO_H}
/* HAVE_STDLIB_H */
#cmakedefine HAVE_STDLIB_H ${HAVE_STDLIB_H}
/* HAVE_ASSERT_H */
#cmakedefine HAVE_ASSERT_H ${HAVE_ASSERT_H}
/* HAVE_DLFCN_H */
#cmakedefine HAVE_DLFCN_H ${HAVE_DLFCN_H}
/* HAVE_ENDIAN_H */
#cmakedefine HAVE_ENDIAN_H ${HAVE_ENDIAN_H}
/* HAVE_MEMORY_H */
#cmakedefine HAVE_MEMORY_H ${HAVE_MEMORY_H}
/* HAVE_STRINGS_H */
#cmakedefine HAVE_STRINGS_H ${HAVE_STRINGS_H}
/* HAVE_STRING_H */
#cmakedefine HAVE_STRING_H ${HAVE_STRING_H}
/* HAVE_TIME_H */
#cmakedefine HAVE_TIME_H ${HAVE_TIME_H}
/* HAVE_UNISTD_H */
#cmakedefine HAVE_UNISTD_H ${HAVE_UNISTD_H}
/* HAVE_SYS_STAT_H */
#cmakedefine HAVE_SYS_STAT_H ${HAVE_SYS_STAT_H}
/* HAVE_SYS_TYPES_H */
#cmakedefine HAVE_SYS_TYPES_H ${HAVE_SYS_TYPES_H}

/* HAVE_CMATH */
#cmakedefine HAVE_CMATH ${HAVE_CMATH}
/* HAVE_CCTYPE */
#cmakedefine HAVE_CCTYPE ${HAVE_CCTYPE}
/* HAVE_CINTTYPES */
#cmakedefine HAVE_CINTTYPES ${HAVE_CINTTYPES}
/* HAVE_CFLOAT */
#cmakedefine HAVE_CFLOAT ${HAVE_CFLOAT}
/* HAVE_CIEEEFP */
#cmakedefine HAVE_CIEEEFP ${HAVE_CIEEEFP}
/* HAVE_CSTDARG */
#cmakedefine HAVE_CSTDARG ${HAVE_CSTDARG}
/* HAVE_CSTDDEF */
#cmakedefine HAVE_CSTDDEF ${HAVE_CSTDDEF}
/* HAVE_CSTDINT */
#cmakedefine HAVE_CSTDINT ${HAVE_CSTDINT}
/* HAVE_CSTDIO */
#cmakedefine HAVE_CSTDIO ${HAVE_CSTDIO}
/* HAVE_CSTDLIB */
#cmakedefine HAVE_CSTDLIB ${HAVE_CSTDLIB}
/* HAVE_CASSERT */
#cmakedefine HAVE_CASSERT ${HAVE_CASSERT}
/* HAVE_CSTRING */
#cmakedefine HAVE_CSTRING ${HAVE_CSTRING}
/* HAVE_CTIME */
#cmakedefine HAVE_CTIME ${HAVE_CTIME}

/* COIN_C_FINITE */
#cmakedefine COIN_C_FINITE ${COIN_C_FINITE}
/* COIN_C_ISNAN */
#cmakedefine COIN_C_ISNAN ${COIN_C_ISNAN}
/* COIN_INT64_T */
#cmakedefine COIN_INT64_T ${COIN_INT64_T}
/* COIN_UINT64_T */
#cmakedefine COIN_UINT64_T ${COIN_UINT64_T}
/* COIN_INTPTR_T */
#cmakedefine COIN_INTPTR_T ${COIN_INTPTR_T}

/* COIN_${NAME}_CHECKLEVEL */
#define COIN_${NAME}_CHECKLEVEL ${COIN_${NAME}_CHECKLEVEL}
/* COIN_${NAME}_VERBOSITY */
#define COIN_${NAME}_VERBOSITY ${COIN_${NAME}_VERBOSITY}

/* PACKAGE */
#cmakedefine PACKAGE
/* PACKAGE_NAME */
#cmakedefine PACKAGE_NAME
/* PACKAGE_VERSION */
#cmakedefine PACKAGE_VERSION
/* PACKAGE_STRING */
#cmakedefine PACKAGE_STRING
/* PACKAGE_TARNAME */
#cmakedefine PACKAGE_TARNAME
/* PACKAGE_BUGREPORT */
#cmakedefine PACKAGE_BUGREPORT

Loading

0 comments on commit a3d0a30

Please sign in to comment.