reorganize cmake in small scripts with some fix from https://github.com/cnjinhao/nana/pull/278

This commit is contained in:
qPCR4vir
2018-10-02 18:47:04 +02:00
parent b430f82855
commit 2e6a85bf89
10 changed files with 233 additions and 275 deletions

70
build/cmake/OS.cmake Normal file
View File

@@ -0,0 +1,70 @@
########### OS
# https://blog.kowalczyk.info/article/j/guide-to-predefined-macros-in-c-compilers-gcc-clang-msvc-etc..html
# http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system
if(WIN32)
target_compile_definitions(nana PUBLIC WIN32) # todo: why not simple test for _WIN32 in code??
set(CMAKE_DEBUG_POSTFIX "_d") # ??
# Global MSVC definitions. You may prefer the hand-tuned sln and projects from the nana repository.
if(MSVC)
option(MSVC_USE_MP "Set to ON to build nana with the /MP option (Visual Studio 2005 and above)." ON)
option(MSVC_USE_STATIC_RUNTIME "Set to ON to build nana with the /MT(d) option." ON)
# Change the MSVC Compiler flags
if(MSVC_USE_MP)
target_compile_options(nana PUBLIC "/MP" )
endif()
if(MSVC_USE_STATIC_RUNTIME)
foreach(flag
CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
if(${flag} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${flag} "${${flag}}")
endif()
endforeach()
endif()
endif()
if(MINGW)
if(NANA_CMAKE_ENABLE_MINGW_STD_THREADS_WITH_MEGANZ) # deprecated ?????
target_compile_definitions(nana PUBLIC STD_THREAD_NOT_SUPPORTED
PUBLIC NANA_ENABLE_MINGW_STD_THREADS_WITH_MEGANZ )
endif()
endif()
endif()
if(APPLE)
target_compile_definitions(nana PUBLIC APPLE) # ??? not added by compilers? use __APPLE__ ?
target_include_directories(nana PUBLIC /opt/X11/include/)
target_link_libraries(nana PRIVATE iconv)
set(ENABLE_AUDIO OFF)
endif()
if(UNIX)
find_package(X11 REQUIRED) # X11 - todo test PRIVATE
target_link_libraries(nana
PUBLIC ${X11_LIBRARIES}
PUBLIC ${X11_Xft_LIB}
)
target_include_directories(nana SYSTEM
PUBLIC ${X11_Xft_INCLUDE_PATH}
PUBLIC ${X11_INCLUDE_DIR}
)
find_package(Freetype) # Freetype - todo test PRIVATE
if (FREETYPE_FOUND)
find_package(Fontconfig REQUIRED)
target_include_directories(nana SYSTEM
PUBLIC ${FREETYPE_INCLUDE_DIRS}
PUBLIC ${FONTCONFIG_INCLUDE_DIR}
)
target_link_libraries(nana
PUBLIC ${FREETYPE_LIBRARIES}
PUBLIC ${FONTCONFIG_LIBRARIES}
)
endif(FREETYPE_FOUND)
endif(UNIX)

View File

@@ -0,0 +1,48 @@
########### Compilers
#
# Using gcc: gcc 4.8 don't support C++14 and make_unique. You may want to update at least to 4.9.
# gcc 5.3 and 5.4 include filesytem, but you need to add the link flag: -lstdc++fs
#
# In Windows, with CLion Allways check in File/Settings.../toolchains
# You could install MinGW-w64 from the TDM-GCC Compiler Suite for Windows which will update you to gcc 5.1.
# It is posible to follow https://computingabdn.com/softech/mingw-howto-install-gcc-for-windows/
# and install MinGW with gcc 7.1 with has STD_THREADS and fs, from: https://sourceforge.net/projects/mingw-w64/files/
#
# 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") # AND NOT MINGW??
target_compile_options(nana PRIVATE -Wall
PUBLIC -g )
set(THREADS_PREFER_PTHREAD_FLAG ON) # todo - test this
find_package(Threads REQUIRED)
target_link_libraries(nana PRIVATE Threads::Threads)
# target_compile_options(nana PUBLIC -pthread)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
if("${CMAKE_SYSTEM_NAME}" MATCHES "FreeBSD")
target_compile_options(nana PUBLIC -I/usr/local/include)
endif()
endif()
# target_link_libraries(nana PRIVATE stdc++fs) # ??
endif()
if (APPLE AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") # APPLE Clang
target_compile_options(nana PUBLIC -stdlib=libstdc++)
endif ()
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
target_compile_options(nana PRIVATE -fmax-errors=3)
endif()

View File

@@ -4,10 +4,10 @@ option(NANA_CMAKE_ENABLE_AUDIO "Enable class audio::play for PCM playback." OFF)
if(NANA_CMAKE_ENABLE_AUDIO)
target_compile_definitions(nana PUBLIC NANA_ENABLE_AUDIO)
if(UNIX)
find_package(ASOUND)
find_package(ASOUND) # ? https://github.com/hintjens/demidi/blob/master/Findasound.cmake
if(ASOUND_FOUND)
target_include_directories(nana PUBLIC ${ASOUND_INCLUDE_DIRS})
target_link_libraries(nana PUBLIC ${ASOUND_LIBRARY})
target_link_libraries(nana PUBLIC ${ASOUND_LIBRARIES})
else()
message(FATAL_ERROR "libasound is not found")
endif()

View File

@@ -9,9 +9,9 @@ if(NANA_CMAKE_ENABLE_JPEG)
if(NANA_CMAKE_LIBJPEG_FROM_OS)
find_package(JPEG)
if(JPEG_FOUND)
target_include_directories(nana PUBLIC ${JPEG_INCLUDE_DIR})
target_link_libraries (nana PUBLIC ${JPEG_LIBRARY})
target_compile_definitions(nana PUBLIC USE_LIBJPEG_FROM_OS)
target_include_directories(nana PUBLIC ${JPEG_INCLUDE_DIRS})
target_link_libraries (nana PUBLIC ${JPEG_LIBRARIES})
target_compile_definitions(nana PUBLIC USE_LIBJPEG_FROM_OS)
endif()
else()
target_compile_definitions(nana PUBLIC -ljpeg)

View File

@@ -11,8 +11,10 @@ if(NANA_CMAKE_ENABLE_PNG)
target_include_directories(nana PUBLIC ${PNG_INCLUDE_DIRS})
target_link_libraries (nana PUBLIC ${PNG_LIBRARIES})
target_compile_definitions(nana PUBLIC USE_LIBPNG_FROM_OS ${PNG_DEFINITIONS})
# target_include_directories (nana SYSTEM PUBLIC PNG::PNG) # ??
# target_compile_definitions (nana PUBLIC USE_LIBPNG_FROM_OS)
endif()
else()
target_compile_definitions(nana PUBLIC -lpng)
target_link_libraries(nana PUBLIC png) # provided by nana?
endif()
endif()

View File

@@ -0,0 +1,23 @@
option(NANA_CMAKE_INSTALL "Install nana when compile the library (to be consumed without cmake)" OFF)
# Install the include directories too.
if(NANA_CMAKE_INSTALL)
# this is the prefered method to consume nana directly with some specific bulid system
# Is your responsability to ensure all compiler options are compatible with the compilation
# of the project linking to the nana lib here generated
target_sources(nana PRIVATE ${HEADERS})
target_include_directories(nana PRIVATE ${NANA_INCLUDE_DIR})
message("The compiled Nana library will be installed in ${CMAKE_INSTALL_PREFIX}/lib")
# Actually in DESTDIR/CMAKE_INSTALL_PREFIX/lib but in windows there is no DESTDIR/ part.
install(TARGETS nana
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)
install(DIRECTORY ${NANA_INCLUDE_DIR}/nana DESTINATION include) # in ${CMAKE_INSTALL_PREFIX}/include/nana
message("The Nana include files will be installed in ${CMAKE_INSTALL_PREFIX}/include")
else()
# this is the prefered method to consume nana with cmake
target_sources(nana PUBLIC ${HEADERS})
target_include_directories(nana PUBLIC ${NANA_INCLUDE_DIR})
endif()

View File

@@ -21,27 +21,33 @@ if(NANA_CMAKE_NANA_FILESYSTEM_FORCE)
elseif(NANA_CMAKE_STD_FILESYSTEM_FORCE)
target_compile_definitions(nana PUBLIC STD_FILESYSTEM_FORCE)
target_compile_options (nana PUBLIC -lstdc++fs)
elseif(NANA_CMAKE_FIND_BOOST_FILESYSTEM OR NANA_CMAKE_BOOST_FILESYSTEM_FORCE)
if(NANA_CMAKE_BOOST_FILESYSTEM_FORCE)
target_compile_definitions(nana PUBLIC BOOST_FILESYSTEM_FORCE)
else()
target_compile_options (nana PUBLIC -lstdc++fs)
endif()
target_link_libraries (nana PUBLIC stdc++fs)
elseif(NANA_CMAKE_BOOST_FILESYSTEM_FORCE)
target_compile_definitions(nana PUBLIC BOOST_FILESYSTEM_FORCE)
# 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,
# even if system is not specified when using find_package and if Boost::system is not added to target_link_libraries.
# If using Boost::thread, then Thread::Thread will also be added automatically.
find_package(Boost COMPONENTS filesystem)
find_package(Boost REQUIRED COMPONENTS filesystem)
if(Boost_FOUND)
target_compile_definitions(nana PUBLIC BOOST_FILESYSTEM_AVAILABLE)
target_include_directories(nana PUBLIC "${Boost_INCLUDE_DIR}") # ?? SYSTEM
target_link_libraries (nana PUBLIC ${Boost_LIBRARIES})
# target_link_libraries (nana PUBLIC Boost::Boost)
endif()
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME ON)
else()
# todo test for std (for now just force nana or boost if there no std)
target_link_libraries (nana PUBLIC stdc++fs)
# todo if not test for boost
# if not add nana filesystem
endif()

View File

@@ -1,3 +1,8 @@
option(BUILD_SHARED_LIBS "Compile nana as a shared library." OFF)
if(BUILD_SHARED_LIBS) # todo test
if(WIN32)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
if(MSVC)
@@ -25,6 +30,22 @@
--output-lib "libnana.lib")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libnana.def"
"${CMAKE_CURRENT_BINARY_DIR}/libnana.lib" DESTINATION lib)
"${CMAKE_CURRENT_BINARY_DIR}/libnana.lib" DESTINATION lib)
endif()
endif()
endif()
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") # AND NOT MINGW??
if(BUILD_SHARED_LIBS)
target_compile_options(nana PUBLIC -lgcc -lstdc++)
else()
if(MINGW)
target_compile_options(nana PUBLIC -static) # -static ?? cmake knows BUILD_SHARED_LIBS
else()
target_compile_options(nana PUBLIC -static-libgcc -static-libstdc++)
endif()
endif(BUILD_SHARED_LIBS)
endif()