Refactoring the main CMakeLists.txt

This commit is contained in:
Yuchen Deng 2018-03-30 23:31:30 +08:00
parent dbf898cc50
commit 83ce5e9142

View File

@ -80,7 +80,7 @@ if(WIN32)
# Change the MSVC Compiler flags # Change the MSVC Compiler flags
if(MSVC_USE_MP) if(MSVC_USE_MP)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
endif(MSVC_USE_MP) endif()
if(MSVC_USE_STATIC_RUNTIME) if(MSVC_USE_STATIC_RUNTIME)
foreach(flag foreach(flag
@ -92,31 +92,26 @@ if(WIN32)
string(REGEX REPLACE "/MD" "/MT" ${flag} "${${flag}}") string(REGEX REPLACE "/MD" "/MT" ${flag} "${${flag}}")
endif() endif()
endforeach() endforeach()
endif(MSVC_USE_STATIC_RUNTIME) endif()
endif(MSVC) endif()
if(MINGW) if(MINGW)
if(NANA_CMAKE_ENABLE_MINGW_STD_THREADS_WITH_MEGANZ) if(NANA_CMAKE_ENABLE_MINGW_STD_THREADS_WITH_MEGANZ)
add_definitions(-DSTD_THREAD_NOT_SUPPORTED) add_definitions(-DSTD_THREAD_NOT_SUPPORTED)
add_definitions(-DNANA_ENABLE_MINGW_STD_THREADS_WITH_MEGANZ) add_definitions(-DNANA_ENABLE_MINGW_STD_THREADS_WITH_MEGANZ)
endif(NANA_CMAKE_ENABLE_MINGW_STD_THREADS_WITH_MEGANZ) endif()
endif(MINGW) endif()
if(MSVC) if(MSVC)
set(DLLTOOL OFF) set(DLLTOOL OFF)
else() else()
# mingw: If dlltool is found the def and lib file will be created # mingw: If dlltool is found the def and lib file will be created
message (STATUS "Detecting dlltool")
find_program (DLLTOOL dlltool) find_program (DLLTOOL dlltool)
if (DLLTOOL) if(NOT DLLTOOL)
message (STATUS "Found dlltool: ${DLLTOOL}")
else ()
message(WARNING "dlltool not found. Skipping import library generation.") message(WARNING "dlltool not found. Skipping import library generation.")
endif (DLLTOOL) endif()
endif (MSVC) endif()
endif()
endif(WIN32)
if(APPLE) if(APPLE)
@ -126,8 +121,7 @@ if(APPLE)
set(ENABLE_AUDIO OFF) set(ENABLE_AUDIO OFF)
elseif(UNIX) elseif(UNIX)
add_definitions(-Dlinux) add_definitions(-Dlinux)
message("added -D linux") endif()
endif(APPLE)
if(UNIX) if(UNIX)
list(APPEND NANA_LINKS -lX11) list(APPEND NANA_LINKS -lX11)
@ -135,8 +129,8 @@ if(UNIX)
if(FREETYPE_FOUND) if(FREETYPE_FOUND)
include_directories( ${FREETYPE_INCLUDE_DIRS}) include_directories( ${FREETYPE_INCLUDE_DIRS})
list(APPEND NANA_LINKS -lXft) list(APPEND NANA_LINKS -lXft)
endif(FREETYPE_FOUND) endif()
endif(UNIX) endif()
########### Compilers ########### Compilers
@ -152,42 +146,29 @@ endif(UNIX)
# #
# #
# see at end of: https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dynamic_or_shared.html # see at end of: https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dynamic_or_shared.html
if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") # Clang || GNU if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++14 -Wall -g") # Clang set(CMAKE_CXX_FLAGS "-std=gnu++14 -Wall -g")
else()
else ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") set(CMAKE_CXX_FLAGS "-std=c++14 -Wall -g")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -g") # GNU endif()
endif()
endif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
endif(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
# enable static linkage # GNU || CLang not MinGW # enable static linkage # GNU || CLang not MinGW
if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") # AND NOT MINGW if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
if(NANA_CMAKE_SHARED_LIB) if(NANA_CMAKE_SHARED_LIB)
list(APPEND NANA_LINKS -lgcc -lstdc++ -pthread) list(APPEND NANA_LINKS -lgcc -lstdc++ -pthread)
else() else()
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++ -pthread") set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++ -pthread")
# message("Setting NANA_LINKS to -static-libgcc -static-libstdc++ -pthread or ${NANA_LINKS}") endif()
endif(NANA_CMAKE_SHARED_LIB)
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.3) if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.3)
# IS_GNUCXX < 5.3
else(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.3)
# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lstdc++fs") # IS_GNUCXX 5.3 or more
list(APPEND NANA_LINKS -lstdc++fs) list(APPEND NANA_LINKS -lstdc++fs)
endif()
endif(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.3) endif()
endif(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") # AND NOT MINGW
if (APPLE AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") # APPLE Clang if(APPLE AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
# set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libstdc++")
list(APPEND NANA_LINKS -stdlib=libstdc++) list(APPEND NANA_LINKS -stdlib=libstdc++)
endif() endif()
@ -201,13 +182,12 @@ if(NANA_CMAKE_ENABLE_PNG)
if(PNG_FOUND) if(PNG_FOUND)
include_directories(${PNG_INCLUDE_DIRS}) include_directories(${PNG_INCLUDE_DIRS})
list(APPEND NANA_LINKS ${PNG_LIBRARIES}) list(APPEND NANA_LINKS ${PNG_LIBRARIES})
add_definitions("-DNANA_ENABLE_PNG" add_definitions(-DNANA_ENABLE_PNG -DUSE_LIBPNG_FROM_OS)
"-DUSE_LIBPNG_FROM_OS") endif()
endif(PNG_FOUND)
else() else()
add_definitions(-DNANA_ENABLE_PNG) add_definitions(-DNANA_ENABLE_PNG)
endif(NANA_CMAKE_LIBPNG_FROM_OS) endif()
endif(NANA_CMAKE_ENABLE_PNG) endif()
# Find JPEG # Find JPEG
if(NANA_CMAKE_ENABLE_JPEG) if(NANA_CMAKE_ENABLE_JPEG)
@ -217,13 +197,12 @@ if(NANA_CMAKE_ENABLE_JPEG)
if(JPEG_FOUND) if(JPEG_FOUND)
include_directories( ${JPEG_INCLUDE_DIR}) include_directories( ${JPEG_INCLUDE_DIR})
list(APPEND NANA_LINKS ${JPEG_LIBRARY}) list(APPEND NANA_LINKS ${JPEG_LIBRARY})
add_definitions("-DNANA_ENABLE_JPEG" add_definitions(-DNANA_ENABLE_JPEG -DUSE_LIBJPEG_FROM_OS)
"-DUSE_LIBJPEG_FROM_OS") endif()
endif(JPEG_FOUND)
else() else()
add_definitions(-DNANA_ENABLE_JPEG) add_definitions(-DNANA_ENABLE_JPEG)
endif(NANA_CMAKE_LIBJPEG_FROM_OS) endif()
endif(NANA_CMAKE_ENABLE_JPEG) endif()
# Find ASOUND # Find ASOUND
if(NANA_CMAKE_ENABLE_AUDIO) if(NANA_CMAKE_ENABLE_AUDIO)
@ -233,11 +212,11 @@ if(NANA_CMAKE_ENABLE_AUDIO)
if(ASOUND_FOUND) if(ASOUND_FOUND)
include_directories(${ASOUND_INCLUDE_DIRS}) include_directories(${ASOUND_INCLUDE_DIRS})
list(APPEND NANA_LINKS -lasound) list(APPEND NANA_LINKS -lasound)
else(ASOUND_FOUND) else()
message(FATAL_ERROR "libasound is not found") message(FATAL_ERROR "libasound is not found")
endif(ASOUND_FOUND) endif()
endif(UNIX) endif()
endif(NANA_CMAKE_ENABLE_AUDIO) endif()
# Find/Select filesystem # Find/Select filesystem
if(NANA_CMAKE_NANA_FILESYSTEM_FORCE) if(NANA_CMAKE_NANA_FILESYSTEM_FORCE)
@ -247,7 +226,7 @@ elseif (NANA_CMAKE_STD_FILESYSTEM_FORCE)
elseif(NANA_CMAKE_FIND_BOOST_FILESYSTEM OR NANA_CMAKE_BOOST_FILESYSTEM_FORCE) elseif(NANA_CMAKE_FIND_BOOST_FILESYSTEM OR NANA_CMAKE_BOOST_FILESYSTEM_FORCE)
if(NANA_CMAKE_BOOST_FILESYSTEM_FORCE) if(NANA_CMAKE_BOOST_FILESYSTEM_FORCE)
add_definitions(-DBOOST_FILESYSTEM_FORCE) add_definitions(-DBOOST_FILESYSTEM_FORCE)
endif(NANA_CMAKE_BOOST_FILESYSTEM_FORCE) endif()
# https://cmake.org/cmake/help/git-master/module/FindBoost.html # https://cmake.org/cmake/help/git-master/module/FindBoost.html
# Implicit dependencies such as Boost::filesystem requiring Boost::system will be automatically detected and satisfied, # Implicit dependencies such as Boost::filesystem requiring Boost::system will be automatically detected and satisfied,
# even if system is not specified when using find_package and if Boost::system is not added to target_link_libraries. # even if system is not specified when using find_package and if Boost::system is not added to target_link_libraries.
@ -256,11 +235,11 @@ elseif (NANA_CMAKE_FIND_BOOST_FILESYSTEM OR NANA_CMAKE_BOOST_FILESYSTEM_FORCE)
if(Boost_FOUND) if(Boost_FOUND)
add_definitions(-DBOOST_FILESYSTEM_AVAILABLE) add_definitions(-DBOOST_FILESYSTEM_AVAILABLE)
include_directories(SYSTEM "${Boost_INCLUDE_DIR}") include_directories(SYSTEM "${Boost_INCLUDE_DIR}")
list(APPEND NANA_LINKS ${Boost_LIBRARIES}) ###### FIRST !!!!!!!!!!!!!!!!! add is not first list(APPEND NANA_LINKS ${Boost_LIBRARIES})
endif (Boost_FOUND) endif()
set(Boost_USE_STATIC_LIBS ON) set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME ON) # ?? set(Boost_USE_STATIC_RUNTIME ON)
endif (NANA_CMAKE_NANA_FILESYSTEM_FORCE) endif()
######## Nana options ######## Nana options
@ -268,11 +247,11 @@ endif (NANA_CMAKE_NANA_FILESYSTEM_FORCE)
add_definitions(-DNANA_IGNORE_CONF) add_definitions(-DNANA_IGNORE_CONF)
if(NANA_CMAKE_VERBOSE_PREPROCESSOR) if(NANA_CMAKE_VERBOSE_PREPROCESSOR)
add_definitions(-DVERBOSE_PREPROCESSOR) add_definitions(-DVERBOSE_PREPROCESSOR)
endif(NANA_CMAKE_VERBOSE_PREPROCESSOR) endif()
if(NANA_CMAKE_AUTOMATIC_GUI_TESTING) if(NANA_CMAKE_AUTOMATIC_GUI_TESTING)
add_definitions(-DNANA_AUTOMATIC_GUI_TESTING) add_definitions(-DNANA_AUTOMATIC_GUI_TESTING)
enable_testing() enable_testing()
endif(NANA_CMAKE_AUTOMATIC_GUI_TESTING) endif()
####################### Main setting of Nana sources, targets and install ####################### Main setting of Nana sources, targets and install
@ -290,29 +269,29 @@ set(NANA_SOURCE_SUBDIRS /.
/paint /paint
/paint/detail /paint/detail
/system /system
/threads ) /threads
)
if(NANA_CMAKE_ENABLE_AUDIO) if(NANA_CMAKE_ENABLE_AUDIO)
list(APPEND NANA_SOURCE_SUBDIRS list(APPEND NANA_SOURCE_SUBDIRS /audio
/audio /audio/detail
/audio/detail ) )
endif(NANA_CMAKE_ENABLE_AUDIO) endif()
# collect all source files in the source-sub-dir # collect all source files in the source-sub-dir
# To show .h files in Visual Studio, add them to the list of sources in add_executable / add_library # To show .h files in Visual Studio, add them to the list of sources in add_executable / add_library
# and Use SOURCE_GROUP if all your sources are in the same directory # and Use SOURCE_GROUP if all your sources are in the same directory
foreach(subdir ${NANA_SOURCE_SUBDIRS}) foreach(subdir ${NANA_SOURCE_SUBDIRS})
aux_source_directory(${NANA_SOURCE_DIR}${subdir} sources) aux_source_directory(${NANA_SOURCE_DIR}${subdir} SOURCES)
# message("Subir: ${subdir}") # message("Files: ${sources}") endforeach()
endforeach(subdir ${NANA_SOURCE_SUBDIRS})
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
add_definitions(-fmax-errors=3) add_definitions(-fmax-errors=3)
endif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") endif()
if(NANA_CMAKE_SHARED_LIB) if(NANA_CMAKE_SHARED_LIB)
add_library(${PROJECT_NAME} SHARED ${sources} ) add_library(${PROJECT_NAME} SHARED ${SOURCES})
else() else()
add_library(${PROJECT_NAME} STATIC ${sources} ) add_library(${PROJECT_NAME} STATIC ${SOURCES})
endif(NANA_CMAKE_SHARED_LIB) endif()
target_include_directories(${PROJECT_NAME} PUBLIC ${NANA_INCLUDE_DIR}) target_include_directories(${PROJECT_NAME} PUBLIC ${NANA_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${NANA_LINKS}) target_link_libraries(${PROJECT_NAME} ${NANA_LINKS})
@ -341,10 +320,7 @@ set_property( TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14 )
if(NANA_CMAKE_SHARED_LIB) if(NANA_CMAKE_SHARED_LIB)
if(WIN32) if(WIN32)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
if(DLLTOOL) if(DLLTOOL)
#message(STATUS "CMAKE_CURRENT_BINARY_DIR = ${CMAKE_CURRENT_BINARY_DIR}")
#generate the lib and def files needed by msvc #generate the lib and def files needed by msvc
set_target_properties (${PROJECT_NAME} PROPERTIES OUTPUT_NAME "${PROJECT_NAME}" set_target_properties (${PROJECT_NAME} PROPERTIES OUTPUT_NAME "${PROJECT_NAME}"
ARCHIVE_OUTPUT_NAME "${PROJECT_NAME}" ARCHIVE_OUTPUT_NAME "${PROJECT_NAME}"
@ -360,18 +336,16 @@ if (NANA_CMAKE_SHARED_LIB)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/lib${PROJECT_NAME}.def" install(FILES "${CMAKE_CURRENT_BINARY_DIR}/lib${PROJECT_NAME}.def"
"${CMAKE_CURRENT_BINARY_DIR}/lib${PROJECT_NAME}.lib" DESTINATION lib) "${CMAKE_CURRENT_BINARY_DIR}/lib${PROJECT_NAME}.lib" DESTINATION lib)
endif() endif()
endif()
endif (WIN32) endif()
endif (NANA_CMAKE_SHARED_LIB)
message("") message("")
message("The compiled Nana library will be installed in ${CMAKE_INSTALL_PREFIX}/lib") message("The compiled Nana library will be installed in ${CMAKE_INSTALL_PREFIX}/lib")
# Install the include directories too. # Install the include directories too.
if(NANA_CMAKE_INSTALL_INCLUDES) if(NANA_CMAKE_INSTALL_INCLUDES)
install(DIRECTORY ${NANA_INCLUDE_DIR}/nana DESTINATION include) install(DIRECTORY ${NANA_INCLUDE_DIR}/nana DESTINATION include)
message("The Nana include files will be installed in ${CMAKE_INSTALL_PREFIX}/include") message("The Nana include files will be installed in ${CMAKE_INSTALL_PREFIX}/include")
endif(NANA_CMAKE_INSTALL_INCLUDES) endif()
# Just for information: # Just for information: