2016-02-11 12:53:42 +01:00
include ( CheckCXXCompilerFlag )
2016-02-11 12:04:49 +01:00
###################################################
# Activate C++14
#
2016-02-11 12:57:50 +01:00
# Uses: target_activate_cpp14(buildtarget)
2016-02-11 12:04:49 +01:00
###################################################
2016-02-11 12:57:50 +01:00
function ( target_activate_cpp14 TARGET )
2016-03-04 00:33:10 +01:00
if ( "${CMAKE_VERSION}" VERSION_GREATER "3.1" )
set_property ( TARGET ${ TARGET } PROPERTY CXX_STANDARD 14 )
set_property ( TARGET ${ TARGET } PROPERTY CXX_STANDARD_REQUIRED ON )
else ( "${CMAKE_VERSION}" VERSION_GREATER "3.1" )
check_cxx_compiler_flag ( "-std=c++14" COMPILER_HAS_CPP14_SUPPORT )
if ( COMPILER_HAS_CPP14_SUPPORT )
2018-10-21 20:58:19 +02:00
target_compile_options ( ${ TARGET } PRIVATE -std=c++14 )
2016-03-04 00:33:10 +01:00
else ( COMPILER_HAS_CPP14_SUPPORT )
check_cxx_compiler_flag ( "-std=c++1y" COMPILER_HAS_CPP14_PARTIAL_SUPPORT )
if ( COMPILER_HAS_CPP14_PARTIAL_SUPPORT )
2018-10-21 20:58:19 +02:00
target_compile_options ( ${ TARGET } PRIVATE -std=c++1y )
2016-03-04 00:33:10 +01:00
else ( )
message ( FATAL_ERROR "Compiler doesn't support C++14" )
endif ( )
endif ( COMPILER_HAS_CPP14_SUPPORT )
endif ( "${CMAKE_VERSION}" VERSION_GREATER "3.1" )
2016-10-26 00:50:53 +02:00
# Ideally, we'd like to use libc++ on linux as well, but:
# - http://stackoverflow.com/questions/37096062/get-a-basic-c-program-to-compile-using-clang-on-ubuntu-16
# - https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=808086
# so only use it on Apple systems...
if ( CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND APPLE )
2016-03-04 01:26:47 +01:00
target_compile_options ( ${ TARGET } PUBLIC -stdlib=libc++ )
2016-10-26 00:50:53 +02:00
endif ( CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND APPLE )
2016-02-11 12:57:50 +01:00
endfunction ( target_activate_cpp14 )
2016-02-11 12:04:49 +01:00
2017-10-27 05:02:51 +02:00
# Find clang-tidy executable (for use in target_enable_style_warnings)
2018-09-30 03:01:38 +02:00
if ( USE_CLANG_TIDY )
find_program (
C L A N G _ T I D Y _ E X E
N A M E S " c l a n g - t i d y "
D O C " P a t h t o c l a n g - t i d y e x e c u t a b l e "
)
if ( NOT CLANG_TIDY_EXE )
message ( FATAL_ERROR "clang-tidy not found. Please install clang-tidy or run without -DUSE_CLANG_TIDY=on." )
else ( )
set ( CLANG_TIDY_OPTIONS "-system-headers=0" )
if ( CLANG_TIDY_WARNINGS_AS_ERRORS )
set ( CLANG_TIDY_OPTIONS "${CLANG_TIDY_OPTIONS}" "-warnings-as-errors=*" )
endif ( )
message ( STATUS "Clang-tidy is enabled. Executable: ${CLANG_TIDY_EXE} Arguments: ${CLANG_TIDY_OPTIONS}" )
set ( CLANG_TIDY_CLI "${CLANG_TIDY_EXE}" "${CLANG_TIDY_OPTIONS}" )
endif ( )
2017-10-27 05:02:51 +02:00
endif ( )
2016-02-11 12:04:49 +01:00
#################################################
# Enable style compiler warnings
#
2016-02-11 12:57:50 +01:00
# Uses: target_enable_style_warnings(buildtarget)
2016-02-11 12:04:49 +01:00
#################################################
2016-02-11 12:57:50 +01:00
function ( target_enable_style_warnings TARGET )
2018-10-03 05:51:33 +02:00
if ( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" )
# TODO
elseif ( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang" )
target_compile_options ( ${ TARGET } PRIVATE -Wall -Wextra -Wold-style-cast -Wcast-align -Wno-unused-command-line-argument ) # TODO consider -Wpedantic -Wchkp -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wlogical-op -Wmissing-include-dirs -Wnoexcept -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-promo -Wstrict-null-sentinel -Wstrict-overflow=5 -Wundef -Wno-unused -Wno-variadic-macros -Wno-parentheses -fdiagnostics-show-option -Wconversion and others?
elseif ( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" )
2018-10-14 23:30:36 +02:00
target_compile_options ( ${ TARGET } PRIVATE -Wall -Wextra -Wold-style-cast -Wcast-align -Wno-maybe-uninitialized ) # TODO consider -Wpedantic -Wchkp -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wlogical-op -Wmissing-include-dirs -Wnoexcept -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-promo -Wstrict-null-sentinel -Wstrict-overflow=5 -Wundef -Wno-unused -Wno-variadic-macros -Wno-parentheses -fdiagnostics-show-option -Wconversion and others?
2018-08-08 05:13:01 +02:00
endif ( )
2017-10-27 05:02:51 +02:00
2018-10-03 05:51:33 +02:00
if ( USE_WERROR )
target_compile_options ( ${ TARGET } PRIVATE -Werror )
endif ( )
2017-10-27 05:02:51 +02:00
# Enable clang-tidy
2018-09-30 03:01:38 +02:00
if ( USE_CLANG_TIDY )
set_target_properties (
$ { T A R G E T } P R O P E R T I E S
C X X _ C L A N G _ T I D Y " $ { C L A N G _ T I D Y _ C L I } "
)
endif ( )
2016-02-11 12:57:50 +01:00
endfunction ( target_enable_style_warnings )
2016-02-11 12:04:49 +01:00
##################################################
# Add boost to the project
#
# Uses:
2016-02-11 12:57:50 +01:00
# target_add_boost(buildtarget) # if you're only using header-only boost libs
# target_add_boost(buildtarget system filesystem) # list all libraries to link against in the dependencies
2016-02-11 12:04:49 +01:00
##################################################
2016-02-11 12:57:50 +01:00
function ( target_add_boost TARGET )
2016-02-11 12:04:49 +01:00
# Load boost libraries
2016-02-16 20:48:37 +01:00
if ( NOT DEFINED Boost_USE_STATIC_LIBS OR Boost_USE_STATIC_LIBS )
# Many supported systems don't have boost >= 1.56. Better link it statically.
message ( STATUS "Boost will be statically linked" )
set ( Boost_USE_STATIC_LIBS ON )
else ( NOT DEFINED Boost_USE_STATIC_LIBS OR Boost_USE_STATIC_LIBS )
message ( STATUS "Boost will be dynamically linked" )
set ( Boost_USE_STATIC_LIBS OFF )
endif ( NOT DEFINED Boost_USE_STATIC_LIBS OR Boost_USE_STATIC_LIBS )
2017-02-14 09:16:38 +01:00
set ( BOOST_THREAD_VERSION 4 )
2017-09-01 00:58:57 +02:00
find_package ( Boost 1.56.0
2016-02-11 12:04:49 +01:00
R E Q U I R E D
C O M P O N E N T S $ { A R G N } )
2016-02-16 23:59:38 +01:00
target_include_directories ( ${ TARGET } SYSTEM PUBLIC ${ Boost_INCLUDE_DIRS } )
2016-02-15 20:01:13 +01:00
target_link_libraries ( ${ TARGET } PUBLIC ${ Boost_LIBRARIES } )
2017-02-14 09:16:38 +01:00
target_compile_definitions ( ${ TARGET } PUBLIC BOOST_THREAD_VERSION=4 )
2016-02-15 20:01:13 +01:00
if ( ${ CMAKE_SYSTEM_NAME } MATCHES "Linux" )
# Also link to rt, because boost thread needs that.
target_link_libraries ( ${ TARGET } PUBLIC rt )
endif ( ${ CMAKE_SYSTEM_NAME } MATCHES "Linux" )
2016-02-11 12:57:50 +01:00
endfunction ( target_add_boost )
2016-02-11 12:53:42 +01:00
##################################################
# Specify that a specific minimal version of gcc is required
#
# Uses:
2016-02-11 12:57:50 +01:00
# require_gcc_version(4.9)
2016-02-11 12:53:42 +01:00
##################################################
2016-02-13 02:08:55 +01:00
function ( require_gcc_version VERSION )
2016-02-11 12:53:42 +01:00
if ( CMAKE_COMPILER_IS_GNUCXX )
execute_process ( COMMAND ${ CMAKE_CXX_COMPILER } -dumpversion OUTPUT_VARIABLE GCC_VERSION )
2016-02-13 02:08:55 +01:00
if ( GCC_VERSION VERSION_LESS ${ VERSION } )
message ( FATAL_ERROR "Needs at least gcc version ${VERSION}, found gcc ${GCC_VERSION}" )
endif ( GCC_VERSION VERSION_LESS ${ VERSION } )
2016-02-11 12:53:42 +01:00
endif ( CMAKE_COMPILER_IS_GNUCXX )
2016-02-11 12:57:50 +01:00
endfunction ( require_gcc_version )
2016-02-13 02:08:55 +01:00
2016-02-13 02:10:20 +01:00
##################################################
# Specify that a specific minimal version of clang is required
#
# Uses:
# require_clang_version(3.5)
##################################################
2016-02-13 02:08:55 +01:00
function ( require_clang_version VERSION )
if ( CMAKE_CXX_COMPILER_ID MATCHES "Clang" )
if ( CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${ VERSION } )
message ( FATAL_ERROR "Needs at least clang version ${VERSION}, found clang ${CMAKE_CXX_COMPILER_VERSION}" )
endif ( CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${ VERSION } )
endif ( CMAKE_CXX_COMPILER_ID MATCHES "Clang" )
2016-02-14 15:55:54 +01:00
endfunction ( require_clang_version )
##################################################
# Find the location of a library and return its full path in OUTPUT_VARIABLE.
# If PATH_VARIABLE points to a defined variable, then the library will only be searched in this path.
# If PATH_VARIABLE points to a undefined variable, default system locations will be searched.
#
# Uses (the following will search for fuse in system locations by default, and if the user passes -DFUSE_LIB_PATH to cmake, it will only search in this path.
# find_library_with_path(MYLIBRARY fuse FUSE_LIB_PATH)
# target_link_library(target ${MYLIBRARY})
##################################################
function ( find_library_with_path OUTPUT_VARIABLE LIBRARY_NAME PATH_VARIABLE )
if ( ${ PATH_VARIABLE } )
find_library ( ${ OUTPUT_VARIABLE } ${ LIBRARY_NAME } PATHS ${ ${PATH_VARIABLE } } NO_DEFAULT_PATH )
if ( ${ OUTPUT_VARIABLE } MATCHES NOTFOUND )
message ( FATAL_ERROR "Didn't find ${LIBRARY_NAME} in path specified by the ${PATH_VARIABLE} parameter (${${PATH_VARIABLE}}). Pass in the correct path or remove the parameter to try common system locations." )
else ( ${ OUTPUT_VARIABLE } MATCHES NOTFOUND )
message ( STATUS "Found ${LIBRARY_NAME} in user-defined path ${${PATH_VARIABLE}}" )
endif ( ${ OUTPUT_VARIABLE } MATCHES NOTFOUND )
else ( ${ PATH_VARIABLE } )
find_library ( ${ OUTPUT_VARIABLE } ${ LIBRARY_NAME } )
if ( ${ OUTPUT_VARIABLE } MATCHES NOTFOUND )
message ( FATAL_ERROR "Didn't find ${LIBRARY_NAME} library. If ${LIBRARY_NAME} is installed, try passing in the library location with -D${PATH_VARIABLE}=/path/to/${LIBRARY_NAME}/lib." )
else ( ${ OUTPUT_VARIABLE } MATCHES NOTFOUND )
message ( STATUS "Found ${LIBRARY_NAME} in system location" )
endif ( ${ OUTPUT_VARIABLE } MATCHES NOTFOUND )
endif ( ${ PATH_VARIABLE } )
2016-02-14 18:17:37 +01:00
endfunction ( find_library_with_path )
2018-09-18 13:35:50 +02:00
2018-09-22 18:37:47 +02:00
include ( cmake-utils/TargetArch.cmake )
2018-09-18 13:35:50 +02:00
function ( get_target_architecture output_var )
target_architecture ( local_output_var )
set ( ${ output_var } ${ local_output_var } PARENT_SCOPE )
endfunction ( )