cmake: Modernize use of Qt Linguist Tools
[quassel.git] / cmake / QuasselMacros.cmake
index eb9dca6..08eaaa8 100644 (file)
@@ -1,93 +1,46 @@
-# This file contains various macros useful for building Quassel.
+# This file contains various functions and macros useful for building Quassel.
 #
-# (C) 2014 by the Quassel Project <devel@quassel-irc.org>
-#
-# The qt4_use_modules function was taken from CMake's Qt4Macros.cmake:
-# (C) 2005-2009 Kitware, Inc.
+# (C) 2014-2018 by the Quassel Project <devel@quassel-irc.org>
 #
 # Redistribution and use is allowed according to the terms of the BSD license.
 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
+###################################################################################################
 
-############################
-# Macros for dealing with Qt
-############################
-
-# CMake gained this function in 2.8.10. To be able to use older versions, we've copied
-# this here. If present, the function from CMake will take precedence and our copy will be ignored.
-function(quassel_qt4_use_modules _target _link_type)
-    message("SPUT calling")
-    if ("${_link_type}" STREQUAL "LINK_PUBLIC" OR "${_link_type}" STREQUAL "LINK_PRIVATE")
-        set(modules ${ARGN})
-        set(link_type ${_link_type})
-    else()
-        set(modules ${_link_type} ${ARGN})
-    endif()
-    foreach(_module ${modules})
-        string(TOUPPER ${_module} _ucmodule)
-        set(_targetPrefix QT_QT${_ucmodule})
-        if (_ucmodule STREQUAL QAXCONTAINER OR _ucmodule STREQUAL QAXSERVER)
-            if (NOT QT_Q${_ucmodule}_FOUND)
-                message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
-            endif()
-            set(_targetPrefix QT_Q${_ucmodule})
-        else()
-            if (NOT QT_QT${_ucmodule}_FOUND)
-                message(FATAL_ERROR "Can not use \"${_module}\" module which has not yet been found.")
-            endif()
-            if ("${_ucmodule}" STREQUAL "MAIN")
-                message(FATAL_ERROR "Can not use \"${_module}\" module with qt4_use_modules.")
-            endif()
-        endif()
-        target_link_libraries(${_target} ${link_type} ${${_targetPrefix}_LIBRARIES})
-        set_property(TARGET ${_target} APPEND PROPERTY INCLUDE_DIRECTORIES ${${_targetPrefix}_INCLUDE_DIR} ${QT_HEADERS_DIR} ${QT_MKSPECS_DIR}/default)
-        set_property(TARGET ${_target} APPEND PROPERTY COMPILE_DEFINITIONS ${${_targetPrefix}_COMPILE_DEFINITIONS})
-    endforeach()
-endfunction()
-
-# Some wrappers for simplifying dual-Qt support
-
-function(qt_use_modules)
-    if (WITH_QT5)
-        qt5_use_modules(${ARGN})
-    else()
-        qt4_use_modules(${ARGN})
-    endif()
-endfunction()
+###################################################################################################
+# Adds a library target for a Quassel module.
+#
+# It 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
+#
+# 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)
+    # Derive target, alias target, output name from the given module name
+    set(alias "Quassel::${_module}")
+    set(target ${alias})
+    string(TOLOWER ${target} target)
+    string(REPLACE "::" "_" target ${target})
+    string(REPLACE "_" "-" output_name ${target})
 
-function(qt_wrap_ui _var)
-    if (WITH_QT5)
-        qt5_wrap_ui(var ${ARGN})
-    else()
-        qt4_wrap_ui(var ${ARGN})
-    endif()
-    set(${_var} ${${_var}} ${var} PARENT_SCOPE)
-endfunction()
+    add_library(${target} STATIC "")
+    add_library(${alias} ALIAS ${target})
 
-function(qt_add_resources _var)
-    if (WITH_QT5)
-        qt5_add_resources(var ${ARGN})
-    else()
-        qt4_add_resources(var ${ARGN})
-    endif()
-    set(${_var} ${${_var}} ${var} PARENT_SCOPE)
-endfunction()
+    set_target_properties(${target} PROPERTIES
+        OUTPUT_NAME ${output_name}
+    )
 
-function(qt_add_dbus_interface _var)
-    if (WITH_QT5)
-        qt5_add_dbus_interface(var ${ARGN})
-    else()
-        qt4_add_dbus_interface(var ${ARGN})
-    endif()
-    set(${_var} ${${_var}} ${var} PARENT_SCOPE)
-endfunction()
+    target_include_directories(${target}
+        PUBLIC  ${CMAKE_CURRENT_SOURCE_DIR}
+        PRIVATE ${CMAKE_CURRENT_BINARY_DIR} # for generated files
+    )
 
-function(qt_add_dbus_adaptor _var)
-    if (WITH_QT5)
-        qt5_add_dbus_adaptor(var ${ARGN})
-    else()
-        qt4_add_dbus_adaptor(var ${ARGN})
-    endif()
-    set(${_var} ${${_var}} ${var} PARENT_SCOPE)
+    # Export the target name for further use
+    set(TARGET ${target} PARENT_SCOPE)
 endfunction()
 
 ######################################
@@ -99,13 +52,13 @@ macro(generate_ts outvar basename)
   set(input ${basename}.po)
   set(output ${CMAKE_BINARY_DIR}/po/${basename}.ts)
   add_custom_command(OUTPUT ${output}
-          COMMAND ${QT_LCONVERT_EXECUTABLE}
+          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 ${QT_LUPDATE_EXECUTABLE}
+          COMMAND $<TARGET_PROPERTY:Qt5::lupdate,LOCATION>
           ARGS -silent
                ${CMAKE_SOURCE_DIR}/src/
                -ts ${output}
@@ -118,7 +71,7 @@ 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 ${QT_LRELEASE_EXECUTABLE}
+          COMMAND $<TARGET_PROPERTY:Qt5::lrelease,LOCATION>
           ARGS -silent
                ${input}
           DEPENDS ${basename}.ts)