|
CMAKE_MINIMUM_REQUIRED(VERSION 3.0.2 FATAL_ERROR) |
|
|
|
PROJECT(core VERSION 0.0.1 LANGUAGES CXX) |
|
|
|
# Sets up some sane compiler flags for C++14 with appropriate warnings; |
|
# tested to work with GCC 4.9+ and Clang 3.6+ --- feel free to modify |
|
|
|
IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux") |
|
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra -pedantic -Wold-style-cast -Wuninitialized -Wunreachable-code -Wstrict-overflow=3 -D_FORTIFY_SOURCE=2 -ffunction-sections -fdata-sections") |
|
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-O1 -Wl,--hash-style=gnu -Wl,--sort-common -Wl,--gc-sections") |
|
IF(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") |
|
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fmax-errors=1") |
|
IF (${CMAKE_BUILD_TYPE} MATCHES "Debug") |
|
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Og -ggdb3 -fno-omit-frame-pointer") |
|
ENDIF() |
|
ELSEIF(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") |
|
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ferror-limit=1") |
|
IF (${CMAKE_BUILD_TYPE} MATCHES "Debug") |
|
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer") |
|
ENDIF() |
|
ENDIF() |
|
ELSE() |
|
MESSAGE(FATAL_ERROR "Unsupported system") |
|
ENDIF() |
|
|
|
FIND_PACKAGE(Threads REQUIRED) |
|
FIND_PACKAGE(PythonLibs 2.7 REQUIRED) |
|
FIND_PACKAGE(Boost 1.59.0 REQUIRED COMPONENTS filesystem system program_options thread timer log log_setup unit_test_framework python) |
|
ADD_DEFINITIONS(-DBOOST_ALL_DYN_LINK) |
|
ADD_DEFINITIONS(-DBOOST_FILESYSTEM_NO_DEPRECATED) |
|
|
|
|
|
# Optional: you can make it more modular by putting the following into |
|
# subdir/CMakeLists.txt and ADD_SUBDIRECTORY(subdir) here |
|
|
|
|
|
# By default Python searches for module.so when doing import module |
|
# force .so on OSX instead of .dylib |
|
IF(APPLE) |
|
SET(CMAKE_SHARED_LIBRARY_SUFFIX ".so") |
|
ENDIF() |
|
|
|
ADD_LIBRARY(core SHARED "core.cc") |
|
TARGET_LINK_LIBRARIES(core ${Boost_LIBRARIES} ${PYTHON_LIBRARIES}) |
|
TARGET_INCLUDE_DIRECTORIES(core SYSTEM PUBLIC ${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS}) |
|
|
|
# By default CMake prefixes shared libraries with lib; we would get |
|
# libmodule but want module in order to be able to do import module |
|
SET_TARGET_PROPERTIES(core PROPERTIES PREFIX "") |
|
|
|
# Optional install target |
|
INSTALL(TARGETS core DESTINATION lib) |
Note how requiring CMake 3.0.2, Boost 1.59 and Python 2.7 is somewhat arbitrary (we used those in a project of mine), adapt to your needs.
If you want this to work with Python3 you probably have to not only change the 2.7 to e.g. 3.4 (3 only makes sense for 3.4+ imho), but probably build a local Boost linked against Python 3.
Good luck and be safe out there!