Skip to content

Commit

Permalink
CMake dependencies management improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
pinam45 committed Mar 22, 2019
1 parent 8b21d3b commit 9ede091
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 132 deletions.
62 changes: 25 additions & 37 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,43 +44,31 @@ add_executable(MagicPlayer)
# Add sources
cmutils_target_sources_folders(
MagicPlayer PRIVATE
"${CMAKE_SOURCE_DIR}/src"
"${CMAKE_SOURCE_DIR}/include"
"${CMAKE_CURRENT_SOURCE_DIR}/src"
"${CMAKE_CURRENT_SOURCE_DIR}/include"
)

# Add includes
target_include_directories(
MagicPlayer PRIVATE
"${CMAKE_SOURCE_DIR}/include"
)

# Add external includes
target_include_directories(
MagicPlayer SYSTEM PRIVATE
"${OPENGL_INCLUDE_DIR}"
"${SFML_INCLUDE_DIR}"
"${IMGUI_INCLUDE_DIR}"
"${IMGUI_SFML_INCLUDE_DIR}"
"${LIBMPG123_INCLUDE_DIR}"
"${SPDLOG_INCLUDE_DIR}"
"${ICON_FONT_CPP_HEADERS_INCLUDE_DIR}"
"${UTFCPP_INCLUDE_DIR}"
"${TAGLIB_INCLUDE_DIR}"
"${JSON_INCLUDE_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/include"
)

# Link dependencies
target_link_libraries(
MagicPlayer PRIVATE
${OPENGL_LIBRARY}
${SFML_LIBRARY}
${IMGUI_SFML_LIBRARY}
${LIBMPG123_LIBRARY}
${SPDLOG_LIBRARY}
${ICON_FONT_CPP_HEADERS_LIBRARY}
${UTFCPP_LIBRARY}
${TAGLIB_LIBRARY}
${JSON_LIBRARY}
MagicPlayer PRIVATE
OpenGL::GL
sfml-system
sfml-window
sfml-graphics
sfml-audio
imgui-sfml
libmpg123
spdlog
IconFontCppHeaders
utf8cpp
taglib
nlohmann_json
)

# Link threads library
Expand Down Expand Up @@ -123,25 +111,25 @@ endif()
cmutils_target_set_ide_folder(MagicPlayer "MagicPlayer")

# Group sources for IDEs
cmutils_target_source_group(MagicPlayer "${CMAKE_SOURCE_DIR}")
cmutils_target_source_group(MagicPlayer "${CMAKE_CURRENT_SOURCE_DIR}")

# Visual studio config
get_target_property(magicplayer_binary_folder MagicPlayer RUNTIME_OUTPUT_DIRECTORY)
set_target_properties(
MagicPlayer PROPERTIES
VS_DEBUGGER_WORKING_DIRECTORY "${magicplayer_binary_folder}"
MagicPlayer PROPERTIES
VS_DEBUGGER_WORKING_DIRECTORY "${magicplayer_binary_folder}"
)
set_property(
DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR} PROPERTY
VS_STARTUP_PROJECT MagicPlayer
DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR} PROPERTY
VS_STARTUP_PROJECT MagicPlayer
)

# Resources
cmutils_configure_folder(
"${CMAKE_SOURCE_DIR}/resources"
"${magicplayer_binary_folder}/resources"
COPYONLY
"${CMAKE_CURRENT_SOURCE_DIR}/resources"
"${magicplayer_binary_folder}/resources"
COPYONLY
)

# Enable console log in debug
Expand Down
18 changes: 13 additions & 5 deletions cmake/IconFontCppHeaders.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ message(STATUS "Configuring IconFontCppHeaders")

get_filename_component(ICON_FONT_CPP_HEADERS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/extlibs/IconFontCppHeaders ABSOLUTE)

# Variables
get_filename_component(ICON_FONT_CPP_HEADERS_INCLUDE_DIR ${ICON_FONT_CPP_HEADERS_DIR} ABSOLUTE)
set(ICON_FONT_CPP_HEADERS_LIBRARY "")
# Declare IconFontCppHeaders
add_library(IconFontCppHeaders INTERFACE)

# Add includes
target_include_directories(
IconFontCppHeaders SYSTEM INTERFACE
"${ICON_FONT_CPP_HEADERS_DIR}"
)

# Headers target for ide
cmutils_interface_target_generate_headers_target(IconFontCppHeaders IconFontCppHeaders_headers_for_ide)
cmutils_target_source_group(IconFontCppHeaders_headers_for_ide "${ICON_FONT_CPP_HEADERS_DIR}")
cmutils_target_set_ide_folder(IconFontCppHeaders_headers_for_ide "deps/IconFontCppHeaders")

# Message
message("> include: ${ICON_FONT_CPP_HEADERS_INCLUDE_DIR}")
message(STATUS "Configuring IconFontCppHeaders - Done")
50 changes: 20 additions & 30 deletions cmake/SFML.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
message(STATUS "Configuring SFML")

# Cache config
set(MAGICPLAYER_COMPILE_SFML_WITH_PROJECT OFF CACHE BOOL "Compile SFML with project, don't search system installation")
# Compile with project option
option(MAGICPLAYER_COMPILE_SFML_WITH_PROJECT "Compile SFML with project, don't search system installation" OFF)

# Config
set(SFML_MINIMUM_SYSTEM_VERSION 2.5)
Expand All @@ -15,17 +15,11 @@ if(NOT SFML_USE_EMBEDED)
else()
find_package(SFML ${SFML_MINIMUM_SYSTEM_VERSION} COMPONENTS system window graphics audio CONFIG)
if(SFML_FOUND)
# Variables
set(SFML_INCLUDE_DIR "")
set(SFML_LIBRARY sfml-system sfml-window sfml-graphics sfml-audio)

# Message
message(WARNING "If the program crashes when using the clipboard,"
" it is an SFML bug not yet fixed on your system but already fixed on Github (see https://github.com/SFML/SFML/pull/1437),"
" use CMake with -DMAGICPLAYER_COMPILE_SFML_WITH_PROJECT=ON to compile and use the fixed version with the project")
message("> include: ${SFML_INCLUDE_DIR}")
message("> library: ${SFML_LIBRARY}")
message(STATUS "Configuring SFML - Done")
message(WARNING
"If the program crashes when using the clipboard,"
" it is an SFML bug not yet fixed on your system but already fixed on Github (see https://github.com/SFML/SFML/pull/1437),"
" use CMake with -DMAGICPLAYER_COMPILE_SFML_WITH_PROJECT=ON to compile and use the fixed version with the project"
)
else()
set(SFML_USE_EMBEDED ON)
message(STATUS "SFML system installation not found, compile SFML with project")
Expand All @@ -45,10 +39,19 @@ if(SFML_USE_EMBEDED)
# Subproject
add_subdirectory(${SFML_DIR})

# Configure SFML folder in IDE
# Configure targets
foreach(sfml_target IN ITEMS sfml-system sfml-network sfml-window sfml-graphics sfml-audio sfml-main)
if(TARGET ${sfml_target})
set_target_properties(${sfml_target} PROPERTIES FOLDER deps/SFML)
# Configure SFML folder in IDE
cmutils_target_set_ide_folder(${sfml_target} "deps/SFML")

# Disable warnings on headers
get_target_property(target_include_directories ${sfml_target} INTERFACE_INCLUDE_DIRECTORIES)
set_target_properties(${sfml_target} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "")
target_include_directories(${sfml_target} SYSTEM INTERFACE ${target_include_directories})

# Setup output, put MagicPlayer executable and required SFML dll in the same folder
cmutils_target_set_output_directory(${sfml_target} "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
endif()
endforeach()

Expand All @@ -62,19 +65,6 @@ if(SFML_USE_EMBEDED)
endif()
configure_file(${SFML_DIR}/extlibs/bin/${ARCH_FOLDER}/openal32.dll "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
endif()

# Setup targets output, put exe and required SFML dll in the same folder
cmutils_target_set_output_directory(sfml-system "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
cmutils_target_set_output_directory(sfml-window "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
cmutils_target_set_output_directory(sfml-graphics "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
cmutils_target_set_output_directory(sfml-audio "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")

# Variables
get_filename_component(SFML_INCLUDE_DIR ${SFML_DIR}/include ABSOLUTE)
set(SFML_LIBRARY sfml-system sfml-window sfml-graphics sfml-audio)

# Message
message("> include: ${SFML_INCLUDE_DIR}")
message("> library: [compiled with project]")
message(STATUS "Configuring SFML - Done")
endif()

message(STATUS "Configuring SFML - Done")
47 changes: 23 additions & 24 deletions cmake/imgui-sfml.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,28 @@ if(is_empty)
message(FATAL_ERROR "imgui-sfml dependency is missing, maybe you didn't pull the git submodules")
endif()

# Configure imgui for imgui-sfml
#if(NOT OPENGL_INCLUDE_DIR OR NOT OPENGL_LIBRARY)
# message(FATAL_ERROR "Missing OpenGL config")
#endif()
#if(NOT SFML_INCLUDE_DIR OR NOT SFML_LIBRARY)
if(NOT SFML_LIBRARY)
message(FATAL_ERROR "Missing SFML config")
if(NOT TARGET sfml-system)
message(FATAL_ERROR "sfml-system target is missing")
endif()

if(NOT TARGET sfml-window)
message(FATAL_ERROR "sfml-window target is missing")
endif()

if(NOT TARGET sfml-graphics)
message(FATAL_ERROR "sfml-graphics target is missing")
endif()

if(NOT TARGET OpenGL::GL)
message(FATAL_ERROR "OpenGL::GL target is missing")
endif()

# Copy imgui and imgui-sfml files to cmake build folder
cmutils_configure_folder(${IMGUI_DIR} ${IMGUI_SFML_TARGET_DIR} COPYONLY)
cmutils_configure_folder(${IMGUI_SFML_DIR} ${IMGUI_SFML_TARGET_DIR} COPYONLY)
# Include imgui-sfml config header in imgui config header
file(APPEND "${IMGUI_SFML_TARGET_DIR}/imconfig.h"
"\n#include \"imconfig-SFML.h\"\n"
"\n#include \"imconfig-SFML.h\"\n"
)

# Declare imgui-sfml
Expand All @@ -42,17 +49,17 @@ cmutils_target_sources_folders(

# Add includes
target_include_directories(
imgui-sfml PRIVATE
"${OPENGL_INCLUDE_DIR}"
"${SFML_INCLUDE_DIR}"
imgui-sfml SYSTEM PUBLIC
"${IMGUI_SFML_TARGET_DIR}"
)

# Link dependencies
target_link_libraries(
imgui-sfml PRIVATE
"${SFML_LIBRARY}"
"${OPENGL_LIBRARY}"
imgui-sfml PUBLIC
OpenGL::GL
sfml-system
sfml-window
sfml-graphics
)

# Add definitions
Expand All @@ -64,7 +71,7 @@ target_compile_definitions(
# Configure compile options
cmutils_target_configure_compile_options(imgui-sfml)

# Disable warnings
# Disable warnings
cmutils_target_disable_warnings(imgui-sfml)

# Build in C++17
Expand All @@ -74,20 +81,12 @@ cmutils_target_set_standard(imgui-sfml CXX 17)
cmutils_target_set_runtime(imgui-sfml DYNAMIC)

# Set target IDE folder
cmutils_target_set_ide_folder(imgui-sfml "generated/deps")
cmutils_target_set_ide_folder(imgui-sfml "deps/generated/imgui-sfml")

# Group sources for IDEs
cmutils_target_source_group(imgui-sfml "${IMGUI_SFML_TARGET_DIR}")

# Use ccache
cmutils_target_use_ccache(imgui-sfml)


# Variables
get_filename_component(IMGUI_SFML_INCLUDE_DIR ${IMGUI_SFML_TARGET_DIR} ABSOLUTE)
set(IMGUI_SFML_LIBRARY imgui-sfml)

# Message
message("> include: ${IMGUI_SFML_INCLUDE_DIR}")
message("> library: [compiled with project]")
message(STATUS "Configuring imgui-sfml - Done")
12 changes: 8 additions & 4 deletions cmake/json.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ if(is_empty)
message(FATAL_ERROR "Json dependency is missing, maybe you didn't pull the git submodules")
endif()

# Include json
set(JSON_BuildTests OFF CACHE INTERNAL "")
add_subdirectory(${JSON_DIR})
if(NOT TARGET nlohmann_json)
message(FATAL_ERROR "nlohmann_json target is missing")
endif()

# Variables
set(JSON_INCLUDE_DIR "")
set(JSON_LIBRARY nlohmann_json::nlohmann_json)
# Disable warnings on nlohmann_json headers
get_target_property(nlohmann_json_include_directories nlohmann_json INTERFACE_INCLUDE_DIRECTORIES)
set_target_properties(nlohmann_json PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "")
target_include_directories(nlohmann_json SYSTEM INTERFACE ${nlohmann_json_include_directories})

# Message
message(STATUS "Configuring json - Done")
44 changes: 33 additions & 11 deletions cmake/libmpg123.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ set(EMBEDDED_LIBMPG123_MSVC_SHARED_FILENAME_DLL "libmpg123.dll")

cmutils_define_os_variables()
if(OS_WINDOWS)
# Cache config
set(MAGICPLAYER_STATICALLY_LINK_LIBMPG123 OFF CACHE BOOL "Statically link libmpg123")
# Static link option
option(MAGICPLAYER_STATICALLY_LINK_LIBMPG123 "Statically link libmpg123" OFF)

# Embeded library config
cmutils_define_compiler_variables()
Expand All @@ -32,6 +32,7 @@ if(OS_WINDOWS)
set(EMBEDED_LIBMPG123_COPY_PATH ${EMBEDED_LIBMPG123_PATH}/lib/${COMPILER_FOLDER}/${ARCH_FOLDER})
if(MAGICPLAYER_STATICALLY_LINK_LIBMPG123)
# Static link
set(EMBEDED_LIBMPG123_LIBRARY_TYPE SHARED)
set(EMBEDED_LIBMPG123_LINK_PATH ${EMBEDED_LIBMPG123_LINK_PATH}/static)
if(COMPILER_CLANG OR COMPILER_MSVC)
set(EMBEDED_LIBMPG123_LINK_PATH ${EMBEDED_LIBMPG123_LINK_PATH}/${EMBEDDED_LIBMPG123_MSVC_STATIC_FILENAME})
Expand All @@ -40,6 +41,7 @@ if(OS_WINDOWS)
endif()
else()
# Dynamic link
set(EMBEDED_LIBMPG123_LIBRARY_TYPE STATIC)
set(EMBEDED_LIBMPG123_LINK_PATH ${EMBEDED_LIBMPG123_LINK_PATH}/shared)
set(EMBEDED_LIBMPG123_COPY_PATH ${EMBEDED_LIBMPG123_COPY_PATH}/shared)
if(COMPILER_CLANG OR COMPILER_MSVC)
Expand All @@ -49,13 +51,25 @@ if(OS_WINDOWS)
set(EMBEDED_LIBMPG123_LINK_PATH ${EMBEDED_LIBMPG123_LINK_PATH}/${EMBEDDED_LIBMPG123_MINGW_SHARED_FILENAME})
set(EMBEDED_LIBMPG123_COPY_PATH ${EMBEDED_LIBMPG123_COPY_PATH}/${EMBEDDED_LIBMPG123_MINGW_SHARED_FILENAME})
endif()

# Copy binary to output folder
configure_file(${EMBEDED_LIBMPG123_COPY_PATH} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} COPYONLY)
endif()

# Variables
get_filename_component(LIBMPG123_INCLUDE_DIR ${EMBEDED_LIBMPG123_PATH}/include ABSOLUTE)
get_filename_component(LIBMPG123_LIBRARY ${EMBEDED_LIBMPG123_LINK_PATH} ABSOLUTE)
# Declare libmpg123
add_library(libmpg123 ${EMBEDED_LIBMPG123_LIBRARY_TYPE} IMPORTED)

# Add includes
target_include_directories(
libmpg123 SYSTEM INTERFACE
"${EMBEDED_LIBMPG123_PATH}/include"
)

# Add linked binary
set_target_properties(
libmpg123 PROPERTIES
IMPORTED_LOCATION "${EMBEDED_LIBMPG123_LINK_PATH}"
)
else()
# Find PkgConfig
find_package(PkgConfig REQUIRED)
Expand All @@ -69,12 +83,20 @@ else()
message(FATAL_ERROR "libmpg123 not found (dev version must be installed on the system to compile)")
endif()

# Variables
set(LIBMPG123_INCLUDE_DIR ${LIBMPG123_INCLUDE_DIRS})
set(LIBMPG123_LIBRARY ${LIBMPG123_LIBRARIES})
# Declare libmpg123
add_library(libmpg123 SHARED IMPORTED)

# Add includes
target_include_directories(
libmpg123 SYSTEM INTERFACE
"${LIBMPG123_INCLUDE_DIRS}"
)

# Add linked binary
set_target_properties(
libmpg123 PROPERTIES
IMPORTED_LOCATION "${LIBMPG123_LINK_LIBRARIES}"
)
endif()

# Message
message("> include: ${LIBMPG123_INCLUDE_DIR}")
message("> library: ${LIBMPG123_LIBRARY}")
message(STATUS "Configuring libmpg123 - Done")
Loading

0 comments on commit 9ede091

Please sign in to comment.