CMake continued: Try to detect g++, clang, windows and osx and setup appropriate defines and build flags. Improve so that each setup can define platform, boost and ssl libs. I tried to copy everything that I could from the scons scripts, probably some of this can be removed (might be automated from cmake already), left todo:s for that. Added building simple examples, more to follow. NOTE: This has only been tested on windows.

This commit is contained in:
Jonne Nauha
2013-04-03 01:56:10 +03:00
parent 75b2497f2b
commit df4d7167fd
7 changed files with 198 additions and 49 deletions

3
.gitignore vendored
View File

@@ -31,8 +31,11 @@ install_manifest.txt
*.suo
*.ncb
*/Debug/*
*/*/Debug/*
*/Release/*
*/*/Release/*
*/RelWithDebInfo/*
*/*/RelWithDebInfo/*
objs_shared/
objs_static/

View File

@@ -1,4 +1,6 @@
############ Setup project and cmake
# Project name
project (websocketpp)
@@ -6,20 +8,98 @@ project (websocketpp)
# cmake for the dependency find macros etc. to be up to date.
cmake_minimum_required (VERSION 2.5)
# Include our cmake macros
set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} cmake)
include (CMakeHelpers)
# Build customization:
# - Override from command line "CMake -D<OPTION>=TRUE/FALSE/0/1". Remove CMakeCache.txt before the run.
option (ENABLE_CPP11 "Build websocketpp with CPP11 features enabled." FALSE) # todo
option (BUILD_EXAMPLES "Build websocketpp examples." FALSE)
option (BUILD_TESTS "Build websocketpp tests." FALSE) # todo
# Set CMake library search policy
if (COMMAND cmake_policy)
cmake_policy (SET CMP0003 NEW)
cmake_policy (SET CMP0005 NEW)
endif ()
# Disable unnecessary build types
set (CMAKE_CONFIGURATION_TYPES "Release;RelWithDebInfo;Debug" CACHE STRING "Configurations" FORCE)
# Spesify useful paths
# Include our cmake macros
set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include (CMakeHelpers)
############ Build customization
# Override from command line "CMake -D<OPTION>=TRUE/FALSE/0/1"
option (ENABLE_CPP11 "Build websocketpp with CPP11 features enabled." TRUE)
option (BUILD_EXAMPLES "Build websocketpp examples." TRUE)
option (BUILD_TESTS "Build websocketpp tests." FALSE)
############ Compiler specific setup
set (WEBSOCKETPP_PLATFORM_LIBS "")
set (WEBSOCKETPP_PLATFORM_TSL_LIBS "")
set (WEBSOCKETPP_BOOST_LIBS "")
# VC9 and C++11 reasoning
if (ENABLE_CPP11 AND MSVC AND MSVC90)
message("* Detected Visual Studio 9 2008, disabling C++11 support.")
set (ENABLE_CPP11 FALSE)
endif ()
# Detect clang. Not officially reported by cmake.
if ("${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}" MATCHES ".*clang")
set (CMAKE_COMPILER_IS_CLANGXX 1)
endif ()
# C++11 defines
if (ENABLE_CPP11)
add_definitions (-D_WEBSOCKETPP_CPP11_STL_)
endif ()
# Visual studio
if (MSVC)
set (WEBSOCKETPP_BOOST_LIBS system thread regex)
set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GL /Gy /GF /Ox /Ob2 /Ot /Oi /MP /arch:SSE2 /fp:fast")
set (CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG /INCREMENTAL:NO /OPT:REF /OPT:ICF")
add_definitions (/W3 /wd4996 /wd4995 /wd4355)
add_definitions (-DUNICODE -D_UNICODE)
add_definitions (-D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS)
add_definitions (-DNOMINMAX)
endif ()
# g++
if (CMAKE_COMPILER_IS_GNUCXX)
set (WEBSOCKETPP_PLATFORM_LIBS pthread rt)
set (WEBSOCKETPP_PLATFORM_TSL_LIBS ssl crypto)
set (WEBSOCKETPP_BOOST_LIBS system thread)
set (CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++0x")
if (NOT APPLE)
add_definitions (-DNDEBUG -Wall -Wcast-align) # todo: should we use CMAKE_C_FLAGS for these?
endif ()
# Try to detect version. Note: Not tested!
execute_process (COMMAND ${CMAKE_CXX_COMPILER} "-dumpversion" OUTPUT_VARIABLE GCC_VERSION)
if ("${GCC_VERSION}" STRGREATER "4.4.0")
message("* C++11 support partially enabled due to GCC version ${GCC_VERSION}")
set (WEBSOCKETPP_BOOST_LIBS system thread regex)
add_definitions (-D_WEBSOCKETPP_NO_CPP11_REGEX_)
endif ()
endif ()
# clang
if (CMAKE_COMPILER_IS_CLANGXX)
set (WEBSOCKETPP_PLATFORM_LIBS pthread rt)
set (WEBSOCKETPP_PLATFORM_TSL_LIBS ssl crypto)
set (CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++0x -stdlib=libc++") # todo: is libc++ really needed here?
if (NOT APPLE)
add_definitions (-DNDEBUG -Wall -Wno-padded) # todo: should we use CMAKE_C_FLAGS for these?
endif ()
endif ()
# OSX, can override above.
if (APPLE)
add_definitions (-DNDEBUG -Wall -O0)
endif ()
############ Paths
set (WEBSOCKETPP_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
set (WEBSOCKETPP_BIN ${WEBSOCKETPP_ROOT}/bin)
set (WEBSOCKETPP_LIB ${WEBSOCKETPP_ROOT}/lib)
@@ -27,16 +107,26 @@ set (WEBSOCKETPP_INCLUDE ${WEBSOCKETPP_ROOT}/websocketpp)
set (WEBSOCKETPP_PREFIX ${WEBSOCKETPP_ROOT}/build)
# CMake install step prefix. I assume linux users want the prefix to
# be the default usr so this is only adjusted on Windows.
# How to invoke:
# be the default /usr or /usr/local so this is only adjusted on Windows.
# - Windows: Build the INSTALL project in your solution file.
# - Linux/OSX: make install.
if (MSVC)
set (CMAKE_INSTALL_PREFIX ${WEBSOCKETPP_PREFIX})
endif ()
# Find and configure Boost dependency. You should define BOOST_ROOT environment variable or
# pass it to cmake with -DBOOST_ROOT=path. CMake variable BOOST_ROOT can be defined at this stage by -D or from cache.
############ Dependencies
# Set BOOST_ROOT env variable or pass with cmake -DBOOST_ROOT=path.
# BOOST_ROOT can also be defined by a previous run from cmake cache.
if (NOT "$ENV{BOOST_ROOT_CPP11}" STREQUAL "")
# Scons documentation for BOOST_ROOT_CPP11:
# "look for optional second boostroot compiled with clang's libc++ STL library
# this prevents warnings/errors when linking code built with two different
# incompatible STL libraries."
file (TO_CMAKE_PATH "$ENV{BOOST_ROOT_CPP11}" BOOST_ROOT)
set (BOOST_ROOT ${BOOST_ROOT} CACHE PATH "BOOST_ROOT dependency path" FORCE)
endif ()
if ("${BOOST_ROOT}" STREQUAL "")
file (TO_CMAKE_PATH "$ENV{BOOST_ROOT}" BOOST_ROOT)
# Cache BOOST_ROOT for runs that do not define $ENV{BOOST_ROOT}.
@@ -61,7 +151,7 @@ set (Boost_DEBUG FALSE)
set (Boost_USE_MULTITHREADED TRUE)
set (Boost_ADDITIONAL_VERSIONS "1.39.0" "1.40.0" "1.41.0" "1.42.0" "1.43.0" "1.44.0" "1.46.1") # todo: someone who knows better spesify these!
find_package (Boost 1.39.0 COMPONENTS system thread regex) # todo: What do we need for each platform?
find_package (Boost 1.39.0 COMPONENTS "${WEBSOCKETPP_BOOST_LIBS}")
if (Boost_FOUND)
# Boost is a project wide global dependency.
@@ -77,11 +167,18 @@ if (Boost_FOUND)
foreach (library_dir ${Boost_LIBRARY_DIRS})
message (STATUS " " ${library_dir})
endforeach ()
message (STATUS "-- Libraries")
foreach (boost_lib ${Boost_LIBRARIES})
message (STATUS " " ${boost_lib})
endforeach ()
message ("")
else ()
message (FATAL_ERROR "Failed to find required dependency: boost")
endif ()
############ Add projects
# Add main library
add_subdirectory (websocketpp)
@@ -94,3 +191,5 @@ endif ()
if (BUILD_TESTS)
add_subdirectory (test)
endif ()
print_used_build_config()

View File

@@ -1,4 +1,22 @@
# Print build configuration
macro (print_used_build_config)
message ("\n=========== Used Build Configuration =============\n")
message (STATUS "ENABLE_CPP11 = " ${ENABLE_CPP11})
message (STATUS "BUILD_EXAMPLES = " ${BUILD_EXAMPLES})
message (STATUS "BUILD_TESTS = " ${BUILD_TESTS})
message ("")
message (STATUS "WEBSOCKETPP_ROOT = " ${WEBSOCKETPP_ROOT})
message (STATUS "WEBSOCKETPP_BIN = " ${WEBSOCKETPP_BIN})
message (STATUS "WEBSOCKETPP_LIB = " ${WEBSOCKETPP_LIB})
message (STATUS "Install prefix = " ${CMAKE_INSTALL_PREFIX})
message ("")
message (STATUS "WEBSOCKETPP_BOOST_LIBS = ${WEBSOCKETPP_BOOST_LIBS}")
message (STATUS "WEBSOCKETPP_PLATFORM_LIBS = ${WEBSOCKETPP_PLATFORM_LIBS}")
message (STATUS "WEBSOCKETPP_PLATFORM_TSL_LIBS = ${WEBSOCKETPP_PLATFORM_TSL_LIBS}")
message ("")
endmacro ()
# Lists all subdirectories to the RESULT variable.
macro (list_subdirectories RESULT curdir)
set (_TEMP_DIRLIST "")
@@ -11,9 +29,21 @@ macro (list_subdirectories RESULT curdir)
set (${RESULT} ${_TEMP_DIRLIST})
endmacro ()
# Adds the given folder_name into the source files of the current project.
# Use this macro when your module contains .cpp and .h files in several subdirectories.
# Your sources variable needs to be SOURCE_FILES and headers variable HEADER_FILES.
macro(add_source_folder folder_name)
file(GLOB H_FILES_IN_FOLDER_${folder_name} ${folder_name}/*.hpp ${folder_name}/*.h)
file(GLOB CPP_FILES_IN_FOLDER_${folder_name} ${folder_name}/*.cpp ${folder_name}/*.c)
source_group("Header Files\\${folder_name}" FILES ${H_FILES_IN_FOLDER_${folder_name}})
source_group("Source Files\\${folder_name}" FILES ${CPP_FILES_IN_FOLDER_${folder_name}})
set(HEADER_FILES ${HEADER_FILES} ${H_FILES_IN_FOLDER_${folder_name}})
set(SOURCE_FILES ${SOURCE_FILES} ${CPP_FILES_IN_FOLDER_${folder_name}})
endmacro()
# Initialize target.
macro (init_target NAME)
set (TARGET_NAME ${NAME})
set (TARGET_NAME ${NAME})
message ("** " ${TARGET_NAME})
# Include our own module path. This makes #include "x.h"
@@ -21,18 +51,19 @@ macro (init_target NAME)
include_directories (${CMAKE_CURRENT_SOURCE_DIR})
endmacro ()
# Build library for static and shared libraries
macro (build_library TARGET_NAME LIB_TYPE)
# STATIC or SHARED
set (TARGET_LIB_TYPE ${LIB_TYPE})
message (STATUS "-- Build Type:")
message (STATUS " " ${TARGET_LIB_TYPE} " library")
add_library (${TARGET_NAME} ${TARGET_LIB_TYPE} ${ARGN})
target_link_libraries (${TARGET_NAME} ${WEBSOCKETPP_PLATFORM_LIBS})
if (MSVC)
if (${TARGET_LIB_TYPE} STREQUAL "SHARED")
set_target_properties (${TARGET_NAME} PROPERTIES LINK_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG /INCREMENTAL:NO /OPT:REF /OPT:ICF")
set_target_properties (${TARGET_NAME} PROPERTIES LINK_FLAGS_RELEASE ${CMAKE_SHARED_LINKER_FLAGS_RELEASE})
else ()
set_target_properties (${TARGET_NAME} PROPERTIES STATIC_LIBRARY_FLAGS_RELEASE "/LTCG")
endif ()
@@ -45,14 +76,23 @@ macro (build_library TARGET_NAME LIB_TYPE)
endif ()
set_target_properties (${TARGET_NAME} PROPERTIES DEBUG_POSTFIX d)
endmacro (build_library)
endmacro ()
# Build executable for executables
macro (build_executable TARGET_NAME)
set (TARGET_LIB_TYPE "EXECUTABLE")
add_executable (${TARGET_NAME} ${ARGN})
set_target_properties (${TARGET_NAME} PROPERTIES DEBUG_POSTFIX d)
endmacro (build_executable)
message (STATUS "-- Build Type:")
message (STATUS " " ${TARGET_LIB_TYPE})
add_executable (${TARGET_NAME} ${ARGN})
include_directories (${WEBSOCKETPP_ROOT} ${WEBSOCKETPP_INCLUDE})
set_target_properties (${TARGET_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${WEBSOCKETPP_BIN})
set_target_properties (${TARGET_NAME} PROPERTIES DEBUG_POSTFIX d)
endmacro ()
# Finalize target for all types
macro (final_target)
if (${TARGET_LIB_TYPE} STREQUAL "SHARED")
install (TARGETS ${TARGET_NAME}
@@ -83,4 +123,8 @@ macro (final_target)
# Pretty printing
message ("")
endmacro (final_target)
endmacro ()
macro (link_boost)
target_link_libraries (${TARGET_NAME} ${Boost_LIBRARIES})
endmacro ()

View File

@@ -1,17 +1,8 @@
# Roams all subdirs for CMakeLists.txt
list_subdirectories(SUBDIRS ${CMAKE_CURRENT_SOURCE_DIR})
foreach (SUBDIR ${SUBDIRS})
# Check if this folder has source/header files
file (GLOB SOURCE_FILES ${SUBDIR}/*.cpp)
file (GLOB HEADER_FILES ${SUBDIR}/*.hpp)
list (LENGTH SOURCE_FILES SRC_LEN)
list (LENGTH HEADER_FILES H_LEN)
if (NOT SRC_LEN EQUAL 0 OR NOT H_LEN EQUAL 0)
init_target (${SUBDIR})
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
include_directories (${WEBSOCKETPP_ROOT} ${WEBSOCKETPP_INCLUDE})
link_directories (${WEBSOCKETPP_LIB})
target_link_libraries (${TARGET_NAME} optimized libwebsocketpp debug libwebsocketppd)
final_target ()
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/CMakeLists.txt")
add_subdirectory (${SUBDIR})
endif ()
endforeach ()

View File

@@ -0,0 +1,11 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (echo_client)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()

View File

@@ -0,0 +1,10 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (echo_server)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()

View File

@@ -6,23 +6,14 @@ file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
# Sources from subdirectories.
# todo: Make this generate source groups in visual
# studio solutions for a cleaner looking project.
list_subdirectories(SUBDIRS ${CMAKE_CURRENT_SOURCE_DIR})
list_subdirectories (SUBDIRS ${CMAKE_CURRENT_SOURCE_DIR})
foreach (SUBDIR ${SUBDIRS})
file (GLOB SUBDIR_SOURCE_FILES ${SUBDIR}/*.cpp ${SUBDIR}/*.c)
file (GLOB SUBDIR_HEADER_FILES ${SUBDIR}/*.hpp ${SUBDIR}/*.h)
# Filter out certain files from 3rd party code
if (NOT ${SUBDIR} STREQUAL "sha1" AND NOT ${SUBDIR} STREQUAL "md5")
set (SOURCE_FILES ${SOURCE_FILES} ${SUBDIR_SOURCE_FILES})
# Filter out certain subdirs from 3rd party code
if (NOT ${SUBDIR} STREQUAL "sha1" AND NOT ${SUBDIR} STREQUAL "md5") # todo: AND NOT ${SUBDIR} STREQUAL "base64"
add_source_folder (${SUBDIR})
endif ()
set (HEADER_FILES ${HEADER_FILES} ${SUBDIR_HEADER_FILES})
endforeach ()
# In preparation for building as dynamic lib on windows.
# This should probably be supported at some point.
# add_definitions (-DWEBSOCKETPP_MODULE_EXPORTS)
build_library (${TARGET_NAME} STATIC ${SOURCE_FILES} ${HEADER_FILES})
final_target ()