cmake: Build shared libraries
authorManuel Nickschas <sputnick@quassel-irc.org>
Wed, 22 Aug 2018 21:47:22 +0000 (23:47 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Sun, 18 Nov 2018 10:06:43 +0000 (11:06 +0100)
The days of static builds are finally over. Build dynamic libraries
for all Quassel modules.

Let CMake handle the RPATH in a way that doesn't cause inconvenience;
always set an RPATH in the build tree so the correct libraries
are loaded, and also set an RPATH for installed binaries unless
they go into a system location.

Also define output directories for all build artifacts.

CMakeLists.txt
cmake/QuasselMacros.cmake

index e4f5d61..8af98b1 100644 (file)
@@ -388,6 +388,31 @@ endif()
 # This needs to come after setting up KDE integration, so we can use KDE-specific paths
 include(QuasselInstallDirs)
 
+# RPATH and output settings
+#####################################################################
+
+# Build artifacts in a well-known location; especially important for Windows DLLs
+# (which go into RUNTIME_OUTPUT_DIRECTORY and can thus be found by executables)
+set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
+set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
+set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
+
+# These RPATH settings allow for running directly from the build dir
+set(CMAKE_SKIP_BUILD_RPATH            FALSE)
+set(CMAKE_BUILD_WITH_INSTALL_RPATH    FALSE)
+set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE )
+
+# Set install RPATH only if libdir isn't a system directory
+if (IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}")
+    set(libdir "${CMAKE_INSTALL_LIBDIR}")
+else()
+    set(libdir "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
+endif()
+list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${libdir}" is_systemdir)
+if ("${is_systemdir}" STREQUAL "-1")
+   set(CMAKE_INSTALL_RPATH "${libdir}")
+endif()
+
 # Various config-dependent checks and settings
 #####################################################################
 
@@ -468,7 +493,6 @@ add_subdirectory(icons)
 add_subdirectory(pics)
 add_subdirectory(po)
 
-
 # Set up and display feature summary
 #####################################################################
 
index 02b3165..e282506 100644 (file)
@@ -30,20 +30,33 @@ function(quassel_add_module _module)
     string(REPLACE "::" "_" target ${target})
     string(REPLACE "_" "-" output_name ${target})
 
-    add_library(${target} STATIC "")
-    add_library(${alias} ALIAS ${target})
+    # On Windows, building shared libraries requires export headers.
+    # Let's bother with that later.
+    if (WIN32)
+        set(buildmode STATIC)
+    else()
+        set(buildmode SHARED)
+    endif()
 
-    set_target_properties(${target} PROPERTIES
-        OUTPUT_NAME ${output_name}
-    )
+    add_library(${target} ${buildmode} "")
+    add_library(${alias} ALIAS ${target})
 
+    target_link_libraries(${target} PRIVATE Qt5::Core)
     target_include_directories(${target}
         PUBLIC  ${CMAKE_CURRENT_SOURCE_DIR}
         PRIVATE ${CMAKE_CURRENT_BINARY_DIR} # for generated files
     )
-
     target_compile_features(${target} PUBLIC ${QUASSEL_COMPILE_FEATURES})
 
+    set_target_properties(${target} PROPERTIES
+        OUTPUT_NAME ${output_name}
+        VERSION ${QUASSEL_MAJOR}.${QUASSEL_MINOR}.${QUASSEL_PATCH}
+    )
+
+    if (buildmode STREQUAL "SHARED")
+        install(TARGETS ${target} DESTINATION ${CMAKE_INSTALL_LIBDIR})
+    endif()
+
     # Export the target name for further use
     set(TARGET ${target} PARENT_SCOPE)
 endfunction()