toxcore/cmake/ModulePackage.cmake
Carsten Brandt 6ec23c98bd
Add soversion to library files to generate proper symlinks
As mentioned in https://github.com/TokTok/c-toxcore/issues/359#issuecomment-270710463
the current CMake build does not generate version symlinks for library .so files.
This is because the version is not specified for library targets.

See
- https://cmake.org/cmake/help/v3.0/prop_tgt/SOVERSION.html#prop_tgt:SOVERSION
- https://cmake.org/cmake/help/v3.0/prop_tgt/VERSION.html#prop_tgt:VERSION

Use PROJECT_VERSION_MAJOR and MINOR for SOVERSION because
api may break from 0.1 to 0.2 in the 0.x release cycle.
2017-01-08 19:18:06 +01:00

96 lines
2.9 KiB
CMake

option(ENABLE_SHARED "Build shared (dynamic) libraries for all modules" ON)
option(ENABLE_STATIC "Build static libraries for all modules" ON)
option(COMPILE_AS_CXX "Compile all C code as C++ code" OFF)
if(NOT ENABLE_SHARED AND NOT ENABLE_STATIC)
message(WARNING
"Both static and shared libraries are disabled; "
"enabling only shared libraries. Use -DENABLE_SHARED or -DENABLE_STATIC to "
"select one manually.")
set(ENABLE_SHARED ON)
endif()
find_package(PkgConfig REQUIRED)
if(COMPILE_AS_CXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__STDC_FORMAT_MACROS=1")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__STDC_LIMIT_MACROS=1")
endif()
macro(set_source_language)
if(COMPILE_AS_CXX)
foreach(srcfile ${ARGN})
get_filename_component(srcext ${srcfile} EXT)
if(${srcext} STREQUAL ".c")
set_source_files_properties(${srcfile} PROPERTIES LANGUAGE CXX)
endif()
endforeach()
endif()
endmacro()
function(add_c_executable exec)
set_source_language(${ARGN})
add_executable(${exec} ${ARGN})
endfunction()
function(pkg_use_module mod)
pkg_search_module(${mod} ${ARGN})
if(${mod}_FOUND)
link_directories(${${mod}_LIBRARY_DIRS})
include_directories(${${mod}_INCLUDE_DIRS})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${${mod}_CFLAGS_OTHER}" PARENT_SCOPE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${${mod}_CFLAGS_OTHER}" PARENT_SCOPE)
foreach(dir ${${mod}_INCLUDE_DIRS})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem ${dir}" PARENT_SCOPE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -isystem ${dir}" PARENT_SCOPE)
endforeach()
endif()
endfunction()
function(add_module lib)
set_source_language(${ARGN})
if(ENABLE_SHARED)
add_library(${lib}_shared SHARED ${ARGN})
set_target_properties(${lib}_shared PROPERTIES
OUTPUT_NAME ${lib}
VERSION ${PROJECT_VERSION}
# While on 0.x, the x behaves like the major version. 0.2 will be
# incompatible with 0.1. Change this, when releasing 1.0!
SOVERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
)
install(TARGETS ${lib}_shared DESTINATION "lib")
endif()
if(ENABLE_STATIC)
add_library(${lib}_static STATIC ${ARGN})
set_target_properties(${lib}_static PROPERTIES OUTPUT_NAME ${lib})
install(TARGETS ${lib}_static DESTINATION "lib")
endif()
endfunction()
function(target_link_modules target)
if(TARGET ${target}_shared)
set(_targets ${_targets} ${target}_shared)
endif()
if(TARGET ${target}_static)
set(_targets ${_targets} ${target}_static)
endif()
if(NOT _targets)
set(_targets ${_targets} ${target})
endif()
foreach(target ${_targets})
foreach(dep ${ARGN})
if(TARGET ${dep}_shared)
target_link_libraries(${target} ${dep}_shared)
elseif(TARGET ${dep}_static)
target_link_libraries(${target} ${dep}_static)
else()
target_link_libraries(${target} ${dep})
endif()
endforeach()
endforeach()
endfunction()