08eaaa8c43e9ceb4bbe6da80d607fd9d5f967333
[quassel.git] / cmake / QuasselMacros.cmake
1 # This file contains various functions and macros useful for building Quassel.
2 #
3 # (C) 2014-2018 by the Quassel Project <devel@quassel-irc.org>
4 #
5 # Redistribution and use is allowed according to the terms of the BSD license.
6 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
7 ###################################################################################################
8
9 ###################################################################################################
10 # Adds a library target for a Quassel module.
11 #
12 # It expects the (CamelCased) module name as a parameter, and derives various
13 # strings from it. For example, quassel_add_module(Client) produces
14 #  - a library target named quassel_client with output name (lib)quassel-client(.so)
15 #  - an alias target named Quassel::Client in global scope
16 #
17 # The function exports the TARGET variable which can be used in the current scope
18 # for setting source files, properties, link dependencies and so on.
19 # To refer to the target outside of the current scope, e.g. for linking, use
20 # the alias name.
21 #
22 function(quassel_add_module _module)
23     # Derive target, alias target, output name from the given module name
24     set(alias "Quassel::${_module}")
25     set(target ${alias})
26     string(TOLOWER ${target} target)
27     string(REPLACE "::" "_" target ${target})
28     string(REPLACE "_" "-" output_name ${target})
29
30     add_library(${target} STATIC "")
31     add_library(${alias} ALIAS ${target})
32
33     set_target_properties(${target} PROPERTIES
34         OUTPUT_NAME ${output_name}
35     )
36
37     target_include_directories(${target}
38         PUBLIC  ${CMAKE_CURRENT_SOURCE_DIR}
39         PRIVATE ${CMAKE_CURRENT_BINARY_DIR} # for generated files
40     )
41
42     # Export the target name for further use
43     set(TARGET ${target} PARENT_SCOPE)
44 endfunction()
45
46 ######################################
47 # Macros for dealing with translations
48 ######################################
49
50 # This generates a .ts from a .po file
51 macro(generate_ts outvar basename)
52   set(input ${basename}.po)
53   set(output ${CMAKE_BINARY_DIR}/po/${basename}.ts)
54   add_custom_command(OUTPUT ${output}
55           COMMAND $<TARGET_PROPERTY:Qt5::lconvert,LOCATION>
56           ARGS -i ${input}
57                -of ts
58                -o ${output}
59           WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/po
60 # This is a workaround to add (duplicate) strings that lconvert missed to the .ts
61           COMMAND $<TARGET_PROPERTY:Qt5::lupdate,LOCATION>
62           ARGS -silent
63                ${CMAKE_SOURCE_DIR}/src/
64                -ts ${output}
65           DEPENDS ${basename}.po)
66   set(${outvar} ${output})
67 endmacro(generate_ts outvar basename)
68
69 # This generates a .qm from a .ts file
70 macro(generate_qm outvar basename)
71   set(input ${CMAKE_BINARY_DIR}/po/${basename}.ts)
72   set(output ${CMAKE_BINARY_DIR}/po/${basename}.qm)
73   add_custom_command(OUTPUT ${output}
74           COMMAND $<TARGET_PROPERTY:Qt5::lrelease,LOCATION>
75           ARGS -silent
76                ${input}
77           DEPENDS ${basename}.ts)
78   set(${outvar} ${output})
79 endmacro(generate_qm outvar basename)