Modernize CMake files

This commit is contained in:
Ceylo 2018-03-20 19:46:57 +01:00 committed by Lukas Dürrenberger
parent fc02cf004a
commit ee08e18726
26 changed files with 259 additions and 275 deletions

View file

@ -81,7 +81,7 @@ endif()
# detect the compiler and its version
# Note: on some platforms (OS X), CMAKE_COMPILER_IS_GNUCXX is true
# even when CLANG is used, therefore the Clang test is done first
if(CMAKE_CXX_COMPILER MATCHES ".*clang[+][+]" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
if(CMAKE_CXX_COMPILER MATCHES "clang[+][+]" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# CMAKE_CXX_COMPILER_ID is an internal CMake variable subject to change,
# but there is no other way to detect CLang at the moment
set(SFML_COMPILER_CLANG 1)
@ -117,12 +117,3 @@ else()
message(FATAL_ERROR "Unsupported compiler")
return()
endif()
# define the install directory for miscellaneous files
if(SFML_OS_WINDOWS OR SFML_OS_IOS)
set(INSTALL_MISC_DIR .)
elseif(SFML_OS_LINUX OR SFML_OS_FREEBSD OR SFML_OS_MACOSX)
set(INSTALL_MISC_DIR share/SFML)
elseif(SFML_OS_ANDROID)
set(INSTALL_MISC_DIR ${CMAKE_ANDROID_NDK}/sources/third_party/sfml)
endif()

View file

@ -6,9 +6,9 @@ function(sfml_set_stdlib target)
# for gcc >= 4.0 on Windows, apply the SFML_USE_STATIC_STD_LIBS option if it is enabled
if(SFML_OS_WINDOWS AND SFML_COMPILER_GCC AND NOT SFML_GCC_VERSION VERSION_LESS "4")
if(SFML_USE_STATIC_STD_LIBS AND NOT SFML_COMPILER_GCC_TDM)
target_link_libraries(${target} "-static-libgcc" "-static-libstdc++")
target_link_libraries(${target} PRIVATE "-static-libgcc" "-static-libstdc++")
elseif(NOT SFML_USE_STATIC_STD_LIBS AND SFML_COMPILER_GCC_TDM)
target_link_libraries(${target} "-shared-libgcc" "-shared-libstdc++")
target_link_libraries(${target} PRIVATE "-shared-libgcc" "-shared-libstdc++")
endif()
endif()
@ -17,7 +17,7 @@ function(sfml_set_stdlib target)
set_property(TARGET ${target} PROPERTY XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
else()
target_compile_options(${target} PRIVATE "-stdlib=libc++")
target_link_libraries(${target} "-stdlib=libc++")
target_link_libraries(${target} PRIVATE "-stdlib=libc++")
endif()
endif()
endfunction()
@ -25,15 +25,21 @@ endfunction()
# add a new target which is a SFML library
# ex: sfml_add_library(sfml-graphics
# SOURCES sprite.cpp image.cpp ...
# DEPENDS sfml-window sfml-system
# EXTERNAL_LIBS opengl freetype ...)
# [STATIC]) # Always create a static library and ignore BUILD_SHARED_LIBS
macro(sfml_add_library target)
# parse the arguments
cmake_parse_arguments(THIS "" "" "SOURCES;DEPENDS;EXTERNAL_LIBS" ${ARGN})
cmake_parse_arguments(THIS "STATIC" "" "SOURCES" ${ARGN})
if (NOT "${THIS_UNPARSED_ARGUMENTS}" STREQUAL "")
message(FATAL_ERROR "Extra unparsed arguments when calling sfml_add_library: ${THIS_UNPARSED_ARGUMENTS}")
endif()
# create the target
add_library(${target} ${THIS_SOURCES})
if (THIS_STATIC)
add_library(${target} STATIC ${THIS_SOURCES})
else()
add_library(${target} ${THIS_SOURCES})
endif()
# define the export symbol of the module
string(REPLACE "-" "_" NAME_UPPER "${target}")
@ -41,7 +47,7 @@ macro(sfml_add_library target)
set_target_properties(${target} PROPERTIES DEFINE_SYMBOL ${NAME_UPPER}_EXPORTS)
# adjust the output file prefix/suffix to match our conventions
if(BUILD_SHARED_LIBS)
if(BUILD_SHARED_LIBS AND NOT THIS_STATIC)
if(SFML_OS_WINDOWS)
# include the major version number in Windows shared library names (but not import library names)
set_target_properties(${target} PROPERTIES DEBUG_POSTFIX -d)
@ -84,7 +90,7 @@ macro(sfml_add_library target)
set(SFML_PDB_POSTFIX "")
endif()
if(BUILD_SHARED_LIBS)
if(BUILD_SHARED_LIBS AND NOT THIS_STATIC)
# DLLs export debug symbols in the linker PDB (the compiler PDB is an intermediate file)
set_target_properties(${target} PROPERTIES
PDB_NAME "${target}${SFML_PDB_POSTFIX}"
@ -103,13 +109,8 @@ macro(sfml_add_library target)
set_target_properties(${target} PROPERTIES COMPILE_FLAGS -fvisibility=hidden)
endif()
# link the target to its SFML dependencies
if(THIS_DEPENDS)
target_link_libraries(${target} ${THIS_DEPENDS})
endif()
# build frameworks or dylibs
if(SFML_OS_MACOSX AND BUILD_SHARED_LIBS)
if(SFML_OS_MACOSX AND BUILD_SHARED_LIBS AND NOT THIS_STATIC)
if(SFML_BUILD_FRAMEWORKS)
# adapt target to build frameworks instead of dylibs
set_target_properties(${target} PROPERTIES
@ -151,24 +152,36 @@ macro(sfml_add_library target)
endif()
endif()
# link the target to its external dependencies
if(THIS_EXTERNAL_LIBS)
target_link_libraries(${target} ${THIS_EXTERNAL_LIBS})
endif()
# add the install rule
install(TARGETS ${target}
install(TARGETS ${target} EXPORT SFMLConfigExport
RUNTIME DESTINATION bin COMPONENT bin
LIBRARY DESTINATION lib${LIB_SUFFIX} COMPONENT bin
ARCHIVE DESTINATION lib${LIB_SUFFIX} COMPONENT devel
FRAMEWORK DESTINATION ${CMAKE_INSTALL_FRAMEWORK_PREFIX} COMPONENT bin)
FRAMEWORK DESTINATION "." COMPONENT bin)
# add <project>/include as public include directory
target_include_directories(${target}
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
PRIVATE ${PROJECT_SOURCE_DIR}/src)
if (SFML_BUILD_FRAMEWORKS)
target_include_directories(${target} INTERFACE $<INSTALL_INTERFACE:SFML.framework>)
else()
target_include_directories(${target} INTERFACE $<INSTALL_INTERFACE:include>)
endif()
# define SFML_STATIC if the build type is not set to 'shared'
if(NOT BUILD_SHARED_LIBS)
target_compile_definitions(${target} PUBLIC "SFML_STATIC")
endif()
endmacro()
# add a new target which is a SFML example
# ex: sfml_add_example(ftp
# SOURCES ftp.cpp ...
# BUNDLE_RESOURCES MainMenu.nib ... # Files to be added in target but not installed next to the executable
# DEPENDS sfml-network sfml-system
# DEPENDS sfml-network
# RESOURCES_DIR resources) # A directory to install next to the executable and sources
macro(sfml_add_example target)
@ -187,7 +200,7 @@ macro(sfml_add_example target)
# create the target
if(THIS_GUI_APP AND SFML_OS_WINDOWS AND NOT DEFINED CMAKE_CONFIGURATION_TYPES AND ${CMAKE_BUILD_TYPE} STREQUAL "Release")
add_executable(${target} WIN32 ${target_input})
target_link_libraries(${target} sfml-main)
target_link_libraries(${target} PRIVATE sfml-main)
else()
add_executable(${target} ${target_input})
endif()
@ -203,17 +216,17 @@ macro(sfml_add_example target)
# link the target to its SFML dependencies
if(THIS_DEPENDS)
target_link_libraries(${target} ${THIS_DEPENDS})
target_link_libraries(${target} PRIVATE ${THIS_DEPENDS})
endif()
# add the install rule
install(TARGETS ${target}
RUNTIME DESTINATION ${INSTALL_MISC_DIR}/examples/${target} COMPONENT examples
BUNDLE DESTINATION ${INSTALL_MISC_DIR}/examples/${target} COMPONENT examples)
RUNTIME DESTINATION ${SFML_MISC_INSTALL_PREFIX}/examples/${target} COMPONENT examples
BUNDLE DESTINATION ${SFML_MISC_INSTALL_PREFIX}/examples/${target} COMPONENT examples)
# install the example's source code
install(FILES ${THIS_SOURCES}
DESTINATION ${INSTALL_MISC_DIR}/examples/${target}
DESTINATION ${SFML_MISC_INSTALL_PREFIX}/examples/${target}
COMPONENT examples)
if (THIS_RESOURCES_DIR)
@ -224,8 +237,55 @@ macro(sfml_add_example target)
message(FATAL_ERROR "Given resources directory to install does not exist: ${THIS_RESOURCES_DIR}")
endif()
install(DIRECTORY ${THIS_RESOURCES_DIR}
DESTINATION ${INSTALL_MISC_DIR}/examples/${target}
DESTINATION ${SFML_MISC_INSTALL_PREFIX}/examples/${target}
COMPONENT examples)
endif()
endmacro()
# Find the requested package and make an INTERFACE library from it
# Usage: sfml_find_package(wanted_target_name
# [INCLUDE "OPENGL_INCLUDE_DIR"]
# [LINK "OPENGL_gl_LIBRARY"])
function(sfml_find_package)
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/Modules/")
list(GET ARGN 0 target)
list(REMOVE_AT ARGN 0)
if (TARGET ${target})
message(FATAL_ERROR "Target '${target}' is already defined")
endif()
cmake_parse_arguments(THIS "" "" "INCLUDE;LINK" ${ARGN})
if (THIS_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Unknown arguments when calling sfml_import_library: ${THIS_UNPARSED_ARGUMENTS}")
endif()
if (SFML_OS_IOS)
find_host_package(${target} REQUIRED)
else()
find_package(${target} REQUIRED)
endif()
add_library(${target} INTERFACE)
if (THIS_INCLUDE)
foreach(include_dir IN LISTS "${THIS_INCLUDE}")
if (NOT include_dir)
message(FATAL_ERROR "No path given for include dir ${THIS_INCLUDE}")
endif()
target_include_directories(${target} INTERFACE "$<BUILD_INTERFACE:${include_dir}>")
endforeach()
endif()
if (THIS_LINK)
foreach(link_item IN LISTS ${THIS_LINK})
if (NOT link_item)
message(FATAL_ERROR "Missing item in ${THIS_LINK}")
endif()
target_link_libraries(${target} INTERFACE "$<BUILD_INTERFACE:${link_item}>")
endforeach()
endif()
endfunction()

View file

@ -50,3 +50,4 @@ ELSE (UDEV_FOUND)
ENDIF (UDev_FIND_REQUIRED)
ENDIF (UDEV_FOUND)
mark_as_advanced(UDEV_INCLUDE_DIR UDEV_LIBRARIES)

View file

@ -810,20 +810,16 @@ else()
endif()
set( ANDROID_NATIVE_API_LEVEL "${ANDROID_NATIVE_API_LEVEL}" CACHE STRING "Android API level for native code" FORCE )
set( CMAKE_ANDROID_API ${ANDROID_NATIVE_API_LEVEL} )
if( CMAKE_VERSION VERSION_GREATER "2.8" )
list( SORT ANDROID_SUPPORTED_NATIVE_API_LEVELS )
set_property( CACHE ANDROID_NATIVE_API_LEVEL PROPERTY STRINGS ${ANDROID_SUPPORTED_NATIVE_API_LEVELS} )
endif()
list( SORT ANDROID_SUPPORTED_NATIVE_API_LEVELS )
set_property( CACHE ANDROID_NATIVE_API_LEVEL PROPERTY STRINGS ${ANDROID_SUPPORTED_NATIVE_API_LEVELS} )
endif()
unset( __levelIdx )
# remember target ABI
set( ANDROID_ABI "${ANDROID_ABI}" CACHE STRING "The target ABI for Android. If arm, then armeabi-v7a is recommended for hardware floating point." FORCE )
if( CMAKE_VERSION VERSION_GREATER "2.8" )
list( SORT ANDROID_SUPPORTED_ABIS_${ANDROID_ARCH_NAME} )
set_property( CACHE ANDROID_ABI PROPERTY STRINGS ${ANDROID_SUPPORTED_ABIS_${ANDROID_ARCH_NAME}} )
endif()
list( SORT ANDROID_SUPPORTED_ABIS_${ANDROID_ARCH_NAME} )
set_property( CACHE ANDROID_ABI PROPERTY STRINGS ${ANDROID_SUPPORTED_ABIS_${ANDROID_ARCH_NAME}} )
# runtime choice (STL, rtti, exceptions)
@ -1137,9 +1133,6 @@ if( NOT CMAKE_C_COMPILER )
endif()
set( _CMAKE_TOOLCHAIN_PREFIX "${ANDROID_TOOLCHAIN_MACHINE_NAME}-" )
if( CMAKE_VERSION VERSION_LESS 2.8.5 )
set( CMAKE_ASM_COMPILER_ARG1 "-c" )
endif()
if( APPLE )
find_program( CMAKE_INSTALL_NAME_TOOL NAMES install_name_tool )
if( NOT CMAKE_INSTALL_NAME_TOOL )
@ -1443,7 +1436,7 @@ if( MIPS AND BUILD_WITH_ANDROID_NDK AND ANDROID_NDK_RELEASE STREQUAL "r8" )
endif()
# pie/pic
if( NOT (ANDROID_NATIVE_API_LEVEL LESS 16) AND (NOT DEFINED ANDROID_APP_PIE OR ANDROID_APP_PIE) AND (CMAKE_VERSION VERSION_GREATER 2.8.8) )
if( NOT (ANDROID_NATIVE_API_LEVEL LESS 16) AND (NOT DEFINED ANDROID_APP_PIE OR ANDROID_APP_PIE))
set( CMAKE_POSITION_INDEPENDENT_CODE TRUE )
set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIE -pie")
else()