Debian Codegraph @

CMakeModules/CalamaresAddLibrary.cmake

raw
1 # === This file is part of Calamares - <https://calamares.io> ===
2 #
3 # SPDX-FileCopyrightText: 2014 Teo Mrnjavac <[email protected]>
4 # SPDX-FileCopyrightText: 2017 Adriaan de Groot <[email protected]>
5 # SPDX-License-Identifier: BSD-2-Clause
6 #
7 # Calamares is Free Software: see the License-Identifier above.
8 #
9 #
10 ###
11 #
12 # Support functions for building plugins.
13 #
14 # Usage:
15 #
16 # calamares_add_library(
17 # library-name
18 # EXPORT_MACRO macro-name
19 # TARGET_TYPE <STATIC|MODULE|...>
20 # EXPORT export-name
21 # VERSION version
22 # SOVERSION version
23 # INSTALL_BINDIR dir
24 # RESOURCES resource-file
25 # SOURCES source-file...
26 # UI ui-file...
27 # LINK_LIBRARIES lib...
28 # LINK_PRIVATE_LIBRARIES lib...
29 # COMPILE_DEFINITIONS def...
30 # [NO_INSTALL]
31 # [NO_VERSION]
32 # )
33 #
34 # The COMPILE_DEFINITIONS are set on the resulting module with a suitable
35 # flag (i.e. `-D`) so only state the name (optionally, also the value)
36 # without a `-D` prefixed to it. Pass in a CMake list as needed.
37 include( CMakeParseArguments )
38 include( CalamaresAutomoc )
39
40 function(calamares_add_library)
41 # parse arguments (name needs to be saved before passing ARGN into the macro)
42 set(NAME ${ARGV0})
43 set(options NO_INSTALL NO_VERSION)
44 set(oneValueArgs NAME EXPORT_MACRO TARGET_TYPE EXPORT VERSION SOVERSION INSTALL_BINDIR RESOURCES)
45 set(multiValueArgs SOURCES UI LINK_LIBRARIES LINK_PRIVATE_LIBRARIES COMPILE_DEFINITIONS)
46 cmake_parse_arguments(LIBRARY "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
47 set(LIBRARY_NAME ${NAME})
48
49 set(target ${LIBRARY_NAME})
50
51 # qt stuff
52 include_directories(${CMAKE_CURRENT_LIST_DIR})
53 include_directories(${CMAKE_CURRENT_BINARY_DIR})
54
55 # add resources from current dir
56 if(LIBRARY_RESOURCES)
57 list(APPEND LIBRARY_SOURCES ${LIBRARY_RESOURCES})
58 endif()
59
60 # add target
61 if(LIBRARY_TARGET_TYPE STREQUAL "STATIC")
62 add_library(${target} STATIC ${LIBRARY_SOURCES})
63 elseif(LIBRARY_TARGET_TYPE STREQUAL "MODULE")
64 add_library(${target} MODULE ${LIBRARY_SOURCES})
65 elseif(LIBRARY_TARGET_TYPE STREQUAL "SHARED")
66 add_library(${target} SHARED ${LIBRARY_SOURCES})
67 else() # default
68 message(FATAL_ERROR "Invalid library type '${LIBRARY_TARGET_TYPE}'")
69 endif()
70
71 calamares_automoc(${target})
72 if(LIBRARY_UI)
73 calamares_autouic(${target} ${LIBRARY_UI})
74 endif()
75 if(LIBRARY_RESOURCES)
76 calamares_autorcc(${target} ${LIBRARY_RESOURCES})
77 endif()
78
79 if(LIBRARY_EXPORT_MACRO)
80 set_target_properties(${target} PROPERTIES COMPILE_DEFINITIONS ${LIBRARY_EXPORT_MACRO})
81 endif()
82
83 if(LIBRARY_COMPILE_DEFINITIONS)
84 set( _lib_definitions "${LIBRARY_EXPORT_MACRO}" ${LIBRARY_COMPILE_DEFINITIONS} )
85 set_target_properties(${target} PROPERTIES COMPILE_DEFINITIONS "${_lib_definitions}")
86 endif()
87
88 # add link targets
89 target_link_libraries(${target}
90 LINK_PUBLIC ${Calamares_LIBRARIES}
91 ${qtname}::Core
92 ${qtname}::Gui
93 ${qtname}::Widgets
94 )
95 if(LIBRARY_LINK_LIBRARIES)
96 target_link_libraries(${target} LINK_PUBLIC ${LIBRARY_LINK_LIBRARIES})
97 endif()
98 if(LIBRARY_LINK_PRIVATE_LIBRARIES)
99 target_link_libraries(${target} LINK_PRIVATE ${LIBRARY_LINK_PRIVATE_LIBRARIES})
100 endif()
101
102 # add soversion
103 if(NOT LIBRARY_NO_VERSION)
104 set_target_properties(${target} PROPERTIES VERSION ${LIBRARY_VERSION})
105
106 if(NOT LIBRARY_SOVERSION)
107 set(LIBRARY_SOVERSION ${LIBRARY_VERSION})
108 endif()
109
110 set_target_properties(${target} PROPERTIES SOVERSION ${LIBRARY_SOVERSION})
111 endif()
112
113
114 if(NOT LIBRARY_INSTALL_BINDIR)
115 set(LIBRARY_INSTALL_BINDIR "${CMAKE_INSTALL_BINDIR}")
116 set(LIBRARY_INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}")
117 else()
118 set(LIBRARY_INSTALL_LIBDIR "${LIBRARY_INSTALL_BINDIR}")
119 endif()
120
121 # make installation optional, maybe useful for dummy plugins one day
122 if(NOT LIBRARY_NO_INSTALL)
123 include(GNUInstallDirs)
124 if(NOT LIBRARY_EXPORT)
125 install( TARGETS ${target}
126 RUNTIME DESTINATION ${LIBRARY_INSTALL_BINDIR}
127 LIBRARY DESTINATION ${LIBRARY_INSTALL_LIBDIR}
128 ARCHIVE DESTINATION ${LIBRARY_INSTALL_LIBDIR}
129 )
130 else()
131 install( TARGETS ${target}
132 EXPORT ${LIBRARY_EXPORT}
133 RUNTIME DESTINATION ${LIBRARY_INSTALL_BINDIR}
134 LIBRARY DESTINATION ${LIBRARY_INSTALL_LIBDIR}
135 ARCHIVE DESTINATION ${LIBRARY_INSTALL_LIBDIR}
136 )
137 endif()
138 endif()
139 endfunction()