Skip to content

Commit

Permalink
Refactor CMakeLists.txt per recommended guidelines
Browse files Browse the repository at this point in the history
* Set default symbol visibility to hidden
  • Loading branch information
9prady9 committed Feb 13, 2020
1 parent 4c0ba40 commit 0582bdb
Show file tree
Hide file tree
Showing 16 changed files with 195 additions and 181 deletions.
16 changes: 7 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ project(Forge VERSION 1.1.0 LANGUAGES C CXX)

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules")
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_BINARY_DIR}/cmake")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMakeModules")
list(APPEND CMAKE_PREFIX_PATH "${Forge_BINARY_DIR}/cmake")

include(ForgeBuildType)
include(ForgeVersion)
include(FGInstallDirs)
include(ForgeInstallDirs)
include(ForgeInternalUtils)
include(ForgePlatformSetup)
include(ForgeVersion)

set_policies(
TYPE NEW
POLICIES CMP0072 CMP0077)
set_policies(TYPE NEW POLICIES CMP0072 CMP0077)

option(BUILD_SHARED_LIBS "Build shared/static library" ON)
option(FG_ENABLE_HUNTER "Use Hunter cmake package handler" OFF)
Expand Down Expand Up @@ -126,7 +124,7 @@ write_basic_package_version_file(
set(INCLUDE_DIRS include)
set(CMAKE_DIR ${FG_INSTALL_CMAKE_DIR})
configure_package_config_file(
"${PROJECT_SOURCE_DIR}/CMakeModules/ForgeConfig.cmake.in"
"${Forge_SOURCE_DIR}/CMakeModules/ForgeConfig.cmake.in"
"cmake_install/ForgeConfig.cmake"
INSTALL_DESTINATION "${FG_INSTALL_CMAKE_DIR}"
PATH_VARS INCLUDE_DIRS CMAKE_DIR
Expand All @@ -146,7 +144,7 @@ install(EXPORT ForgeTargets
set(INCLUDE_DIRS "${Forge_SOURCE_DIR}/include" "${Forge_BINARY_DIR}/include")
set(CMAKE_DIR "${Forge_BINARY_DIR}")
configure_package_config_file(
"${PROJECT_SOURCE_DIR}/CMakeModules/ForgeConfig.cmake.in"
"${Forge_SOURCE_DIR}/CMakeModules/ForgeConfig.cmake.in"
"ForgeConfig.cmake"
INSTALL_DESTINATION "${Forge_BINARY_DIR}"
PATH_VARS INCLUDE_DIRS CMAKE_DIR
Expand Down
10 changes: 5 additions & 5 deletions CMakeModules/ForgeBuildType.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
# CMAKE_CONFIGURATION_TYPES variable indicated multi-config, but developers
# might modify.
if(NOT CMAKE_VERSION VERSION_LESS 3.9)
get_property(_isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
elseif(CMAKE_CONFIGURATION_TYPES)
# CMAKE_CONFIGURATION_TYPES is set by project() call
set(_isMultiConfig True)
set(isMultiConfig True)
else()
set(_isMultiConfig False)
set(isMultiConfig False)
endif()


if(_isMultiConfig)
if(isMultiConfig)
set(CMAKE_CONFIGURATION_TYPES "Debug;MinSizeRel;Release;RelWithDebInfo"
CACHE STRING "Configurations for Multi-Config CMake Generator" FORCE)
else()
set(default_build_type "Release")
if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
if(EXISTS "${Forge_SOURCE_DIR}/.git")
set(default_build_type "Debug")
endif()
if(NOT CMAKE_BUILD_TYPE)
Expand Down
File renamed without changes.
5 changes: 2 additions & 3 deletions CMakeModules/GLSLtoH.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
#
# where ns is the contents of kernel.cl.namespace.

include(CMakeParseArguments)

set(GLSL2CPP_PROGRAM "glsl2cpp")

macro(convert_glsl_shaders_to_headers)
CMAKE_PARSE_ARGUMENTS(RTCS "" "VARNAME;EXTENSION;OUTPUT_DIR;TARGETS;NAMESPACE;EOF" "SOURCES" ${ARGN})
cmake_parse_arguments(
RTCS "" "VARNAME;EXTENSION;OUTPUT_DIR;TARGETS;NAMESPACE;EOF" "SOURCES" ${ARGN})

set(_output_files "")
foreach(_input_file ${RTCS_SOURCES})
Expand Down
5 changes: 4 additions & 1 deletion CMakeModules/HunterGate.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@
# * https://github.com/ruslo/hunter

cmake_minimum_required(VERSION 3.0) # Minimum for Hunter
include(CMakeParseArguments) # cmake_parse_arguments

# min cmake required for forge is 3.5, hence
# no need to include the below module to use cmake_parse_arguments
# include(CMakeParseArguments)

option(HUNTER_ENABLED "Enable Hunter package manager support" ON)
option(HUNTER_STATUS_PRINT "Print working status" ON)
Expand Down
2 changes: 1 addition & 1 deletion examples/CMakeModules/build_cl2hpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if (OpenCL_FOUND)
"https://github.com/KhronosGroup/OpenCL-CLHPP/releases/download/v2.0.10/cl2.hpp")
file(DOWNLOAD ${file_url} ${cl2hpp_header}
EXPECTED_HASH MD5=c38d1b78cd98cc809fa2a49dbd1734a5
SHOW_PROGRESS STATUS download_result)
STATUS download_result)
list(GET download_result 0 download_code)
if (NOT ${download_code} EQUAL 0)
file(REMOVE ${cl2hpp_header}) #empty file have to be removed
Expand Down
8 changes: 6 additions & 2 deletions extern/glad/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ add_library(forge_glad_interface INTERFACE)

target_include_directories(forge_glad_interface
INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/include
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)

target_sources(forge_glad_interface
INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/src/glad.c
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/glad.c>
)

target_link_libraries(forge_glad_interface
INTERFACE
${CMAKE_DL_LIBS}
)

if (NOT BUILD_SHARED_LIBS)
install(TARGETS forge_glad_interface EXPORT ForgeTargets)
endif ()
22 changes: 13 additions & 9 deletions src/api/c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
add_library(forge_c_api_interface INTERFACE)

# non private sources that are used for build only in INTERFACE target types
# should make sense only for builds, not when installed. Hence, wrapped with
# $<BUILD_INTERFACE:...> generator expression. Without it, cmake generate step
# fails when building static library.
target_sources(forge_c_api_interface
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/chart.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/exception.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/font.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/histogram.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/image.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/plot.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/surface.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/vector_field.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/window.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/chart.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/exception.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/font.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/histogram.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/image.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/plot.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/surface.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/vector_field.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/window.cpp>
)

target_include_directories(forge_c_api_interface
Expand Down
26 changes: 15 additions & 11 deletions src/api/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
add_library(forge_cpp_api_interface INTERFACE)

# non private sources that are used for build only in INTERFACE target types
# should make sense only for builds, not when installed. Hence, wrapped with
# $<BUILD_INTERFACE:...> generator expression. Without it, cmake generate step
# fails when building static library.
target_sources(forge_cpp_api_interface
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/chart.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/error.hpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/exception.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/font.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/histogram.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/image.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/plot.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/surface.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/vector_field.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/window.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/chart.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/error.hpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/exception.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/font.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/histogram.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/image.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/plot.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/surface.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/vector_field.cpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/window.cpp>
)

target_include_directories(forge_cpp_api_interface
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
)

if (NOT BUILD_SHARED_LIBS)
Expand Down
125 changes: 64 additions & 61 deletions src/backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,95 +10,98 @@ add_library(${BkndTargetName} "")

add_dependencies(${BkndTargetName} ${glsl_shader_targets})

if (${FG_RENDERING_BACKEND} STREQUAL "OpenGL")
find_package(OpenGL REQUIRED)
add_subdirectory(opengl)
target_include_directories(${BkndTargetName} PUBLIC ${OPENGL_INCLUDE_DIR})
target_link_libraries(${BkndTargetName} PUBLIC ${OPENGL_gl_LIBRARY})
elseif (${FG_RENDERING_BACKEND} STREQUAL "Vulkan") # arm is not possible now
message(FATAL_ERROR "Vulkan rendering support not added yet")
endif ()
if(${FG_RENDERING_BACKEND} STREQUAL "OpenGL")
find_package(OpenGL REQUIRED)
add_subdirectory(opengl)
target_include_directories(${BkndTargetName} PUBLIC ${OPENGL_INCLUDE_DIR})
target_link_libraries(${BkndTargetName} PUBLIC ${OPENGL_gl_LIBRARY})
elseif(${FG_RENDERING_BACKEND} STREQUAL "Vulkan") # arm is not possible now
message(FATAL_ERROR "Vulkan rendering support not added yet")
endif()

set_target_properties(${BkndTargetName}
PROPERTIES
PROPERTIES
FOLDER ${PROJECT_NAME}
POSITION_INDEPENDENT_CODE ON
CXX_STANDARD 14
CXX_STANDARD_REQUIRED ON
CXX_VISIBILITY_PRESET hidden
CXX_VISIBILITY_INLINES_HIDDEN YES
LINKER_LANGUAGE CXX
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
)
VERSION ${Forge_VERSION}
SOVERSION ${Forge_VERSION_MAJOR}
)

if (WIN32)
target_compile_definitions(${BkndTargetName}
PRIVATE FGDLL OS_WIN WIN32_MEAN_AND_LEAN)
if(WIN32)
target_compile_definitions(${BkndTargetName}
PRIVATE FGDLL OS_WIN WIN32_MEAN_AND_LEAN)

# C4068: Warnings about unknown pragmas
# C4275: Warnings about using non-exported classes as base class of an
# exported class
set_target_properties(${BkndTargetName} PROPERTIES COMPILE_FLAGS "/wd4068 /wd4275")
elseif (APPLE)
target_compile_definitions(${BkndTargetName} PRIVATE OS_MAC)
# C4068: Warnings about unknown pragmas
# C4275: Warnings about using non-exported classes as base class of an
# exported class
set_target_properties(${BkndTargetName} PROPERTIES COMPILE_FLAGS "/wd4068 /wd4275")
elseif(APPLE)
target_compile_definitions(${BkndTargetName} PRIVATE OS_MAC)
else(WIN32)
target_compile_definitions(${BkndTargetName} PRIVATE OS_LNX)
target_compile_definitions(${BkndTargetName} PRIVATE OS_LNX)
endif(WIN32)

target_include_directories(${BkndTargetName}
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
SYSTEM PRIVATE
PUBLIC
$<BUILD_INTERFACE:${Forge_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${Forge_BINARY_DIR}/include>
$<INSTALL_INTERFACE:${FG_INSTALL_INC_DIR}>
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
SYSTEM PRIVATE
${FREETYPE_INCLUDE_DIRS}
${PROJECT_SOURCE_DIR}/extern/glm
)
${Forge_SOURCE_DIR}/extern/glm
)

if(FG_WITH_FREEIMAGE)
target_compile_definitions(${BkndTargetName} PRIVATE USE_FREEIMAGE)
if (FG_USE_STATIC_FREEIMAGE)
target_link_libraries(${BkndTargetName} PRIVATE FreeImage::FreeImage_STATIC)
else ()
target_link_libraries(${BkndTargetName} PRIVATE FreeImage::FreeImage)
endif ()
target_compile_definitions(${BkndTargetName} PRIVATE USE_FREEIMAGE)
if(FG_USE_STATIC_FREEIMAGE)
target_link_libraries(${BkndTargetName} PRIVATE FreeImage::FreeImage_STATIC)
else()
target_link_libraries(${BkndTargetName} PRIVATE FreeImage::FreeImage)
endif()
endif()

target_link_libraries(${BkndTargetName}
PRIVATE
PRIVATE
Boost::boost
forge_backend_interface
forge_c_api_interface
forge_cpp_api_interface
forge_glad_interface
forge_wtk_interface
)
)

if (FG_ENABLE_HUNTER)
target_link_libraries(${BkndTargetName} PRIVATE freetype::freetype)
else ()
target_link_libraries(${BkndTargetName} PRIVATE ${FREETYPE_LIBRARIES})
endif ()
if(FG_ENABLE_HUNTER)
target_link_libraries(${BkndTargetName} PRIVATE freetype::freetype)
else()
target_link_libraries(${BkndTargetName} PRIVATE ${FREETYPE_LIBRARIES})
endif()

if(UNIX)
target_link_libraries(${BkndTargetName}
PRIVATE
FontConfig::FontConfig
$<$<AND:$<CXX_COMPILER_ID:GNU>,$<BOOL:${FG_USE_STATIC_CPPFLAGS}>>:-static-libstdc++>
$<$<AND:$<C_COMPILER_ID:GNU>,$<BOOL:${FG_USE_STATIC_CPPFLAGS}>>:-static-libgcc>
)
target_link_libraries(${BkndTargetName}
PRIVATE
FontConfig::FontConfig
$<$<AND:$<CXX_COMPILER_ID:GNU>,$<BOOL:${FG_USE_STATIC_CPPFLAGS}>>:-static-libstdc++>
$<$<AND:$<C_COMPILER_ID:GNU>,$<BOOL:${FG_USE_STATIC_CPPFLAGS}>>:-static-libgcc>
)
endif(UNIX)

source_group(include
REGULAR_EXPRESSION ${PROJECT_SOURCE_DIR}/include/*)
REGULAR_EXPRESSION ${Forge_SOURCE_DIR}/include/*)
source_group("api\\cpp"
REGULAR_EXPRESSION ${PROJECT_SOURCE_DIR}/src/api/cpp/*)
REGULAR_EXPRESSION ${Forge_SOURCE_DIR}/src/api/cpp/*)
source_group("api\\c"
REGULAR_EXPRESSION ${PROJECT_SOURCE_DIR}/src/api/c/*)
REGULAR_EXPRESSION ${Forge_SOURCE_DIR}/src/api/c/*)
source_group("backend"
REGULAR_EXPRESSION ${PROJECT_SOURCE_DIR}/src/backend/common/*)
REGULAR_EXPRESSION ${Forge_SOURCE_DIR}/src/backend/common/*)
source_group("backend\\shaders"
REGULAR_EXPRESSION ${PROJECT_SOURCE_DIR}/src/backend/glsl_shaders/*)
REGULAR_EXPRESSION ${Forge_SOURCE_DIR}/src/backend/glsl_shaders/*)

#--------------------------------------------------------------------
# Install forge target
Expand All @@ -114,9 +117,9 @@ install(TARGETS ${BkndTargetName}
INCLUDES DESTINATION ${FG_INSTALL_INC_DIR})

# install dependencies
if (WIN32 AND BUILD_SHARED_LIBS AND
FG_WITH_FREEIMAGE AND NOT FG_USE_STATIC_FREEIMAGE)
install(FILES $<TARGET_FILE:FreeImage::FreeImage>
DESTINATION ${FG_INSTALL_BIN_DIR}
COMPONENT dependencies)
endif ()
if(WIN32 AND BUILD_SHARED_LIBS AND
FG_WITH_FREEIMAGE AND NOT FG_USE_STATIC_FREEIMAGE)
install(FILES $<TARGET_FILE:FreeImage::FreeImage>
DESTINATION ${FG_INSTALL_BIN_DIR}
COMPONENT dependencies)
endif()
Loading

0 comments on commit 0582bdb

Please sign in to comment.