cmake: Add support for static libs to quassel_add_module
[quassel.git] / cmake / QuasselMacros.cmake
index 318defb..3851d1b 100644 (file)
@@ -7,21 +7,35 @@
 ###################################################################################################
 
 include(CMakeParseArguments)
+include(QuasselCompileFeatures)
 
 ###################################################################################################
 # Adds a library target for a Quassel module.
 #
-# It expects the (CamelCased) module name as a parameter, and derives various
+# quassel_add_module(Module
+#                    [STATIC]
+# )
+#
+# The function expects the (CamelCased) module name as a parameter, and derives various
 # strings from it. For example, quassel_add_module(Client) produces
 #  - a library target named quassel_client with output name (lib)quassel-client(.so)
 #  - an alias target named Quassel::Client in global scope
 #
+# If the optional argument STATIC is given, a static library is built; otherwise, on
+# platforms other than Windows, a shared library is created. For shared libraries, also
+# an install rule is added.
+#
 # The function exports the TARGET variable which can be used in the current scope
 # for setting source files, properties, link dependencies and so on.
 # To refer to the target outside of the current scope, e.g. for linking, use
 # the alias name.
 #
 function(quassel_add_module _module)
+    set(options STATIC)
+    set(oneValueArgs )
+    set(multiValueArgs )
+    cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
+
     # Derive target, alias target, output name from the given module name
     set(alias "Quassel::${_module}")
     set(target ${alias})
@@ -29,17 +43,32 @@ 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 (ARG_STATIC OR 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)
@@ -158,38 +187,3 @@ function(target_link_if_exists _target)
         endforeach()
     endif()
 endfunction()
-
-######################################
-# Macros for dealing with translations
-######################################
-
-# This generates a .ts from a .po file
-macro(generate_ts outvar basename)
-  set(input ${basename}.po)
-  set(output ${CMAKE_BINARY_DIR}/po/${basename}.ts)
-  add_custom_command(OUTPUT ${output}
-          COMMAND $<TARGET_PROPERTY:Qt5::lconvert,LOCATION>
-          ARGS -i ${input}
-               -of ts
-               -o ${output}
-          WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/po
-# This is a workaround to add (duplicate) strings that lconvert missed to the .ts
-          COMMAND $<TARGET_PROPERTY:Qt5::lupdate,LOCATION>
-          ARGS -silent
-               ${CMAKE_SOURCE_DIR}/src/
-               -ts ${output}
-          DEPENDS ${basename}.po)
-  set(${outvar} ${output})
-endmacro(generate_ts outvar basename)
-
-# This generates a .qm from a .ts file
-macro(generate_qm outvar basename)
-  set(input ${CMAKE_BINARY_DIR}/po/${basename}.ts)
-  set(output ${CMAKE_BINARY_DIR}/po/${basename}.qm)
-  add_custom_command(OUTPUT ${output}
-          COMMAND $<TARGET_PROPERTY:Qt5::lrelease,LOCATION>
-          ARGS -silent
-               ${input}
-          DEPENDS ${basename}.ts)
-  set(${outvar} ${output})
-endmacro(generate_qm outvar basename)