Skip to content

Commit

Permalink
Add CMake config file generation for use with find_package
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRhiem committed May 27, 2020
1 parent a7c4faf commit 24ca167
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 36 deletions.
127 changes: 92 additions & 35 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ project(
LANGUAGES C CXX
)

include(GNUInstallDirs)

# Honor `C_VISIBILITY_PRESET hidden` in static libraries
cmake_policy(SET CMP0063 NEW)

Expand Down Expand Up @@ -159,27 +161,36 @@ add_library(gks_static STATIC ${GKS_SOURCES})
add_library(gks_shared SHARED ${GKS_SOURCES})

foreach(LIBRARY gks_static gks_shared)
target_link_libraries(${LIBRARY} PUBLIC pthread)
target_link_libraries(${LIBRARY} PUBLIC m)
if(LIBRARY MATCHES "static")
set(GKS_LINK_MODE PUBLIC)
else()
set(GKS_LINK_MODE PRIVATE)
endif()
target_link_libraries(${LIBRARY} ${GKS_LINK_MODE} pthread)
target_link_libraries(${LIBRARY} ${GKS_LINK_MODE} m)
if(FREETYPE_FOUND)
target_link_libraries(${LIBRARY} PUBLIC Freetype::Freetype)
target_link_libraries(${LIBRARY} ${GKS_LINK_MODE} Freetype::Freetype)
else()
target_compile_definitions(${LIBRARY} PUBLIC NO_FT)
target_compile_definitions(
${LIBRARY}
${GKS_LINK_MODE}
NO_FT
)
endif()
target_link_libraries(${LIBRARY} PUBLIC Zlib::Zlib)
target_link_libraries(${LIBRARY} ${GKS_LINK_MODE} Zlib::Zlib)
if(UNIX)
target_link_libraries(${LIBRARY} PUBLIC dl)
target_link_libraries(${LIBRARY} ${GKS_LINK_MODE} dl)
elseif(WIN32)
target_link_libraries(${LIBRARY} PUBLIC wsock32)
target_link_libraries(${LIBRARY} PUBLIC msimg32)
target_link_libraries(${LIBRARY} PUBLIC gdi32)
target_link_libraries(${LIBRARY} ${GKS_LINK_MODE} wsock32)
target_link_libraries(${LIBRARY} ${GKS_LINK_MODE} msimg32)
target_link_libraries(${LIBRARY} ${GKS_LINK_MODE} gdi32)
endif()
target_compile_options(${LIBRARY} PUBLIC -pthread)
target_compile_options(${LIBRARY} ${GKS_LINK_MODE} -pthread)
target_include_directories(
${LIBRARY} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lib/gks/>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_compile_definitions(${LIBRARY} PUBLIC GRDIR="${GR_DIRECTORY}")
target_compile_definitions(${LIBRARY} PRIVATE GRDIR="${GR_DIRECTORY}")
set_target_properties(
${LIBRARY}
PROPERTIES C_STANDARD 90
Expand All @@ -190,7 +201,8 @@ foreach(LIBRARY gks_static gks_shared)
INSTALL_RPATH "${INSTALL_RPATH}"
)
endforeach()
set_target_properties(gks_shared PROPERTIES SUFFIX "${GR_SHARED_LIBRARY_SUFFIX}")
unset(GKS_LINK_MODE)
set_target_properties(gks_shared PROPERTIES SUFFIX "${GR_SHARED_LIBRARY_SUFFIX}" EXPORT_NAME GKS)
add_library(GR::GKS ALIAS gks_static)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/gr_version.h
"#ifndef GR_VERSION\n#define GR_VERSION \"${GR_VERSION_FULL}\"\n#endif\n"
Expand Down Expand Up @@ -223,19 +235,25 @@ add_library(gr_static STATIC ${GR_SOURCES})
add_library(gr_shared SHARED ${GR_SOURCES})

foreach(LIBRARY gr_static gr_shared)
target_link_libraries(${LIBRARY} PUBLIC gks_static)
target_link_libraries(${LIBRARY} PUBLIC m)
target_link_libraries(${LIBRARY} PUBLIC Qhull::Qhull)
target_link_libraries(${LIBRARY} PUBLIC Jpeg::Jpeg)
target_link_libraries(${LIBRARY} PUBLIC Libpng::Libpng)
if(LIBRARY MATCHES "static")
set(GR_LINK_MODE PUBLIC)
else()
set(GR_LINK_MODE PRIVATE)
endif()
target_link_libraries(${LIBRARY} ${GR_LINK_MODE} gks_static)
target_link_libraries(${LIBRARY} ${GR_LINK_MODE} m)
target_link_libraries(${LIBRARY} ${GR_LINK_MODE} Qhull::Qhull)
target_link_libraries(${LIBRARY} ${GR_LINK_MODE} Jpeg::Jpeg)
target_link_libraries(${LIBRARY} ${GR_LINK_MODE} Libpng::Libpng)
if(WIN32)
target_link_libraries(${LIBRARY} PUBLIC ws2_32)
target_link_libraries(${LIBRARY} ${GR_LINK_MODE} ws2_32)
endif()
target_include_directories(
${LIBRARY} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lib/gr/>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_include_directories(${LIBRARY} PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
target_compile_definitions(${LIBRARY} PRIVATE GRDIR="${GR_DIRECTORY}")
set_target_properties(
${LIBRARY}
PROPERTIES C_STANDARD 90
Expand All @@ -246,7 +264,8 @@ foreach(LIBRARY gr_static gr_shared)
OUTPUT_NAME GR
)
endforeach()
set_target_properties(gr_shared PROPERTIES SUFFIX "${GR_SHARED_LIBRARY_SUFFIX}")
unset(GR_LINK_MODE)
set_target_properties(gr_shared PROPERTIES SUFFIX "${GR_SHARED_LIBRARY_SUFFIX}" EXPORT_NAME GR)
add_library(GR::GR ALIAS gr_shared)

set(GR3_SOURCES
Expand All @@ -265,23 +284,32 @@ set(GR3_SOURCES
add_library(gr3_static STATIC ${GR3_SOURCES})
add_library(gr3_shared SHARED ${GR3_SOURCES})
foreach(LIBRARY gr3_static gr3_shared)
if(LIBRARY MATCHES "static")
set(GR3_LINK_MODE PUBLIC)
else()
set(GR3_LINK_MODE PRIVATE)
endif()
if(APPLE)
target_sources(${LIBRARY} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/lib/gr3/gr3_cgl.c)
elseif(UNIX AND NOT APPLE)
target_sources(${LIBRARY} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/lib/gr3/gr3_glx.c)
elseif(WIN32)
target_sources(${LIBRARY} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/lib/gr3/gr3_win.c)
endif()
target_link_libraries(${LIBRARY} PUBLIC GR::GR)
target_link_libraries(${LIBRARY} PUBLIC Jpeg::Jpeg)
target_link_libraries(${LIBRARY} PUBLIC Libpng::Libpng)
target_link_libraries(${LIBRARY} ${GR3_LINK_MODE} GR::GR)
target_link_libraries(${LIBRARY} ${GR3_LINK_MODE} Jpeg::Jpeg)
target_link_libraries(${LIBRARY} ${GR3_LINK_MODE} Libpng::Libpng)
target_link_libraries(${LIBRARY} ${GR3_LINK_MODE} pthread)
target_link_libraries(${LIBRARY} ${GR3_LINK_MODE} m)
if((APPLE OR WIN32) AND OpenGL_FOUND)
target_link_libraries(${LIBRARY} PUBLIC OpenGL::GL)
target_link_libraries(${LIBRARY} ${GR3_LINK_MODE} OpenGL::GL)
endif()
if(APPLE)
# Apple has deprecated OpenGL in macOS 10.14
target_compile_definitions(${LIBRARY} PRIVATE GL_SILENCE_DEPRECATION)
endif()
target_compile_definitions(${LIBRARY} PRIVATE GRDIR="${GR_DIRECTORY}")
target_compile_options(${LIBRARY} ${GR3_LINK_MODE} -pthread)
target_include_directories(
${LIBRARY} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lib/gr3/>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
Expand All @@ -296,7 +324,8 @@ foreach(LIBRARY gr3_static gr3_shared)
OUTPUT_NAME GR3
)
endforeach()
set_target_properties(gr3_shared PROPERTIES SUFFIX "${GR_SHARED_LIBRARY_SUFFIX}")
unset(GR3_LINK_MODE)
set_target_properties(gr3_shared PROPERTIES SUFFIX "${GR_SHARED_LIBRARY_SUFFIX}" EXPORT_NAME GR3)
add_library(GR::GR3 ALIAS gr3_shared)

if(UNIX AND NOT APPLE)
Expand Down Expand Up @@ -360,11 +389,17 @@ add_library(grm_shared SHARED ${GRM_SOURCES})
add_library(grm_shared_internal SHARED ${GRM_SOURCES})

foreach(LIBRARY grm_static grm_shared grm_shared_internal)
target_link_libraries(${LIBRARY} PUBLIC GR::GR)
target_link_libraries(${LIBRARY} PUBLIC GR::GR3)
target_link_libraries(${LIBRARY} PUBLIC m)
if(LIBRARY MATCHES "static")
set(GRM_LINK_MODE PUBLIC)
else()
set(GRM_LINK_MODE PRIVATE)
endif()
target_link_libraries(${LIBRARY} ${GRM_LINK_MODE} GR::GKS)
target_link_libraries(${LIBRARY} ${GRM_LINK_MODE} GR::GR)
target_link_libraries(${LIBRARY} ${GRM_LINK_MODE} GR::GR3)
target_link_libraries(${LIBRARY} ${GRM_LINK_MODE} m)
if(WIN32)
target_link_libraries(${LIBRARY} PUBLIC ws2_32)
target_link_libraries(${LIBRARY} ${GRM_LINK_MODE} ws2_32)
endif()
target_include_directories(
${LIBRARY}
Expand All @@ -382,13 +417,15 @@ foreach(LIBRARY grm_static grm_shared grm_shared_internal)
OUTPUT_NAME GRM
)
endforeach()
unset(GRM_LINK_MODE)
set_target_properties(grm_shared_internal PROPERTIES C_VISIBILITY_PRESET default)
set_target_properties(grm_shared_internal PROPERTIES OUTPUT_NAME GRM_int)
target_include_directories(grm_shared_internal PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lib/gks/>)
if(WIN32)
target_compile_definitions(grm_shared PRIVATE BUILDING_DLL)
target_compile_definitions(grm_shared_internal PRIVATE BUILDING_DLL)
endif()
set_target_properties(grm_shared PROPERTIES SUFFIX "${GR_SHARED_LIBRARY_SUFFIX}")
set_target_properties(grm_shared PROPERTIES SUFFIX "${GR_SHARED_LIBRARY_SUFFIX}" EXPORT_NAME GRM)
set_target_properties(grm_shared_internal PROPERTIES SUFFIX "${GR_SHARED_LIBRARY_SUFFIX}")
add_library(GR::GRM ALIAS grm_shared)

Expand Down Expand Up @@ -838,21 +875,43 @@ if(GR_BUILD_DEMOS)

add_executable(grdemo lib/gr/demo.c)
target_link_libraries(grdemo PUBLIC GR::GR)
target_link_libraries(grdemo PUBLIC GR::GKS)
set_target_properties(grdemo PROPERTIES C_STANDARD 90 C_EXTENSIONS OFF C_STANDARD_REQUIRED ON)

add_subdirectory(lib/grm/test/public_api grm_test_public_api)
add_subdirectory(lib/grm/test/internal_api grm_test_internal_api)
endif()

if(GR_INSTALL)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
configure_package_config_file(
"cmake/Config.cmake.in" "GRConfig.cmake" INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/GR"
)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/GRConfigVersion.cmake
VERSION ${GR_VERSION}
COMPATIBILITY SameMajorVersion
)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/GRConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/GRConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/GR"
)
install(
TARGETS gks_shared gr_shared gr3_shared grm_shared
EXPORT GRTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(
EXPORT GRTargets
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/GR"
NAMESPACE GR::
)
install(
TARGETS gks_static
gks_shared
gr_static
gr_shared
gr3_static
grm_static
grm_shared
cairoplugin
glplugin
gsplugin
Expand All @@ -865,8 +924,6 @@ if(GR_INSTALL)
zmqplugin
qtplugin
qt5plugin
gr3_static
gr3_shared
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
Expand Down
2 changes: 2 additions & 0 deletions cmake/Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@PACKAGE_INIT@
include( "${CMAKE_CURRENT_LIST_DIR}/GRTargets.cmake" )
3 changes: 2 additions & 1 deletion cmake/FindCairo.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# If false, do not try to use Cairo.

find_package(Pixman)
find_package(Freetype)
find_package(Libpng)

if(NOT CAIRO_INCLUDE_DIR)
Expand Down Expand Up @@ -70,7 +71,7 @@ if(Cairo_FOUND)
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${CAIRO_INCLUDE_DIRS}"
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${CAIRO_LIBRARY}"
INTERFACE_LINK_LIBRARIES "Pixman::Pixman;Libpng::Libpng"
INTERFACE_LINK_LIBRARIES "Pixman::Pixman;Freetype::Freetype;Libpng::Libpng"
)
endif()
elseif(${CMAKE_FIND_PACKAGE_NAME}_FIND_REQUIRED)
Expand Down

0 comments on commit 24ca167

Please sign in to comment.