Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options for building and installing shared, static libraries #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 60 additions & 31 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ project(matroska VERSION 1.6.2)

option(DISABLE_PKGCONFIG "Disable PkgConfig module generation" OFF)
option(DISABLE_CMAKE_CONFIG "Disable CMake package config module generation" OFF)
option(DISABLE_SHARED_LIBS "Disable build and install shared libraries" OFF)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The preferred name for this option is BUILD_SHARED_LIBS. I've seen it in a lot of libraries.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And both libEBML & libMatroska have gained support for BUILD_SHARED_LIBS in the meantime. Maybe this PR is simply outdated & superfluous now? I'd just close it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As link the suggests, it can be turned into an option to make it more visible:

This variable is often added to projects as an option() so that each user of a project can decide if they want to build the project using shared or static libraries.

It works without the option (we force it in VLC).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The option has been added in #79

option(DISABLE_STATIC_LIBS "Disable build and install static libraries" OFF)

find_package(EBML 1.4.0 REQUIRED)

Expand Down Expand Up @@ -62,44 +64,69 @@ set (libmatroska_C_PUBLIC_HEADERS
matroska/c/libmatroska.h
matroska/c/libmatroska_t.h)

add_library(matroska ${libmatroska_SOURCES} ${libmatroska_PUBLIC_HEADERS} ${libmatroska_C_PUBLIC_HEADERS})
target_link_libraries(matroska PUBLIC EBML::ebml)
set_target_properties(matroska PROPERTIES
VERSION 7.0.0
SOVERSION 7
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON
)
target_include_directories(matroska
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
if(MSVC)
target_compile_definitions(matroska PRIVATE _CRT_SECURE_NO_WARNINGS)
include(GenerateExportHeader)
foreach (TYPE IN ITEMS STATIC SHARED)
if (NOT DISABLE_${TYPE}_LIBS)
set(type_suffix "")
if ("${TYPE}" STREQUAL "STATIC")
string(TOLOWER "${TYPE}" type)
set(type_suffix "-${type}")
endif()

add_library(matroska${type_suffix} ${TYPE}
${libmatroska_SOURCES}
${libmatroska_PUBLIC_HEADERS}
${libmatroska_C_PUBLIC_HEADERS})

target_include_directories(matroska${type_suffix}
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

if(MSVC)
target_compile_definitions(matroska${type_suffix}
PRIVATE
_CRT_SECURE_NO_WARNINGS)
endif()
generate_export_header(matroska${type_suffix} EXPORT_MACRO_NAME MATROSKA_DLL_API BASE_NAME matroska)
target_sources(matroska${type_suffix}
PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/matroska_export.h
)
endif()
endforeach()

if(NOT DISABLE_SHARED_LIBS)
target_link_libraries(matroska PUBLIC EBML::ebml)
set_target_properties(matroska PROPERTIES
VERSION 7.0.0
SOVERSION 7
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON
)
install(TARGETS matroska
EXPORT MatroskaTargets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

include(GenerateExportHeader)
generate_export_header(matroska EXPORT_MACRO_NAME MATROSKA_DLL_API)
target_sources(matroska
PRIVATE
${CMAKE_CURRENT_BINARY_DIR}/matroska_export.h
)

if(NOT BUILD_SHARED_LIBS)
target_compile_definitions(matroska PUBLIC MATROSKA_STATIC_DEFINE)
if(NOT DISABLE_STATIC_LIBS)
set_target_properties(matroska-static PROPERTIES OUTPUT_NAME matroska)
target_compile_definitions(matroska-static PUBLIC MATROSKA_STATIC_DEFINE)
install(TARGETS matroska-static
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

install(TARGETS matroska
EXPORT MatroskaTargets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

install(FILES ${libmatroska_PUBLIC_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/matroska)
install(FILES ${libmatroska_C_PUBLIC_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/matroska/c)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/matroska_export.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/matroska)
if(NOT DISABLE_SHARED_LIBS)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/matroska_export.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/matroska)
endif()

if(NOT DISABLE_PKGCONFIG)
set(prefix ${CMAKE_INSTALL_PREFIX})
Expand All @@ -117,7 +144,9 @@ if(NOT DISABLE_CMAKE_CONFIG)
configure_package_config_file(MatroskaConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/MatroskaConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_PACKAGEDIR})
write_basic_package_version_file(MatroskaConfigVersion.cmake COMPATIBILITY SameMajorVersion)
if(NOT DISABLE_SHARED_LIBS)
install(EXPORT MatroskaTargets NAMESPACE Matroska:: DESTINATION ${CMAKE_INSTALL_PACKAGEDIR})
endif()
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/MatroskaConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/MatroskaConfigVersion.cmake
Expand Down