1#=============================================================================
2# Copyright 2005-2011 Kitware, Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8#
9# * Redistributions of source code must retain the above copyright
10#   notice, this list of conditions and the following disclaimer.
11#
12# * Redistributions in binary form must reproduce the above copyright
13#   notice, this list of conditions and the following disclaimer in the
14#   documentation and/or other materials provided with the distribution.
15#
16# * Neither the name of Kitware, Inc. nor the names of its
17#   contributors may be used to endorse or promote products derived
18#   from this software without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31#=============================================================================
32
33include(MacroAddFileDependencies)
34
35include(CMakeParseArguments)
36
37function(qt5_add_dbus_interface _sources _interface _basename)
38    get_filename_component(_infile ${_interface} ABSOLUTE)
39    set(_header "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.h")
40    set(_impl   "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.cpp")
41    set(_moc    "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.moc")
42
43    get_source_file_property(_nonamespace ${_interface} NO_NAMESPACE)
44    if(_nonamespace)
45        set(_params -N -m)
46    else()
47        set(_params -m)
48    endif()
49
50    get_source_file_property(_classname ${_interface} CLASSNAME)
51    if(_classname)
52        set(_params ${_params} -c ${_classname})
53    endif()
54
55    get_source_file_property(_include ${_interface} INCLUDE)
56    if(_include)
57        set(_params ${_params} -i ${_include})
58    endif()
59
60    add_custom_command(OUTPUT "${_impl}" "${_header}"
61        COMMAND ${Qt5DBus_QDBUSXML2CPP_EXECUTABLE} ${_params} -p ${_basename} ${_infile}
62        DEPENDS ${_infile} VERBATIM)
63
64    set_source_files_properties("${_impl}" "${_header}" PROPERTIES
65        SKIP_AUTOMOC TRUE
66        SKIP_AUTOUIC TRUE
67    )
68
69    qt5_generate_moc("${_header}" "${_moc}")
70
71    list(APPEND ${_sources} "${_impl}" "${_header}" "${_moc}")
72    macro_add_file_dependencies("${_impl}" "${_moc}")
73    set(${_sources} ${${_sources}} PARENT_SCOPE)
74endfunction()
75
76if(NOT QT_NO_CREATE_VERSIONLESS_FUNCTIONS)
77    function(qt_add_dbus_interface sources)
78        if(QT_DEFAULT_MAJOR_VERSION EQUAL 5)
79            qt5_add_dbus_interface("${sources}" ${ARGN})
80        elseif(QT_DEFAULT_MAJOR_VERSION EQUAL 6)
81            qt6_add_dbus_interface("${sources}" ${ARGN})
82        endif()
83        set("${sources}" "${${sources}}" PARENT_SCOPE)
84    endfunction()
85endif()
86
87
88function(qt5_add_dbus_interfaces _sources)
89    foreach(_current_FILE ${ARGN})
90        get_filename_component(_infile ${_current_FILE} ABSOLUTE)
91        get_filename_component(_basename ${_current_FILE} NAME)
92        # get the part before the ".xml" suffix
93        string(TOLOWER ${_basename} _basename)
94        string(REGEX REPLACE "(.*\\.)?([^\\.]+)\\.xml" "\\2" _basename ${_basename})
95        qt5_add_dbus_interface(${_sources} ${_infile} ${_basename}interface)
96    endforeach()
97    set(${_sources} ${${_sources}} PARENT_SCOPE)
98endfunction()
99
100if(NOT QT_NO_CREATE_VERSIONLESS_FUNCTIONS)
101    function(qt_add_dbus_interfaces sources)
102        if(QT_DEFAULT_MAJOR_VERSION EQUAL 5)
103            qt5_add_dbus_interfaces("${sources}" ${ARGN})
104        elseif(QT_DEFAULT_MAJOR_VERSION EQUAL 6)
105            qt6_add_dbus_interfaces("${sources}" ${ARGN})
106        endif()
107        set("${sources}" "${${sources}}" PARENT_SCOPE)
108    endfunction()
109endif()
110
111
112function(qt5_generate_dbus_interface _header) # _customName OPTIONS -some -options )
113    set(options)
114    set(oneValueArgs)
115    set(multiValueArgs OPTIONS)
116
117    cmake_parse_arguments(_DBUS_INTERFACE "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
118
119    set(_customName ${_DBUS_INTERFACE_UNPARSED_ARGUMENTS})
120
121    get_filename_component(_in_file ${_header} ABSOLUTE)
122    get_filename_component(_basename ${_header} NAME_WE)
123
124    if(_customName)
125        if(IS_ABSOLUTE ${_customName})
126          get_filename_component(_containingDir ${_customName} PATH)
127          if(NOT EXISTS ${_containingDir})
128              file(MAKE_DIRECTORY "${_containingDir}")
129          endif()
130          set(_target ${_customName})
131        else()
132            set(_target ${CMAKE_CURRENT_BINARY_DIR}/${_customName})
133        endif()
134    else()
135        set(_target ${CMAKE_CURRENT_BINARY_DIR}/${_basename}.xml)
136    endif()
137
138    add_custom_command(OUTPUT ${_target}
139        COMMAND ${Qt5DBus_QDBUSCPP2XML_EXECUTABLE} ${_DBUS_INTERFACE_OPTIONS} ${_in_file} -o ${_target}
140        DEPENDS ${_in_file} VERBATIM
141    )
142endfunction()
143
144if(NOT QT_NO_CREATE_VERSIONLESS_FUNCTIONS)
145    function(qt_generate_dbus_interface)
146        if(QT_DEFAULT_MAJOR_VERSION EQUAL 5)
147            qt5_generate_dbus_interface(${ARGV})
148        elseif(QT_DEFAULT_MAJOR_VERSION EQUAL 6)
149            qt6_generate_dbus_interface(${ARGV})
150        endif()
151    endfunction()
152endif()
153
154
155function(qt5_add_dbus_adaptor _sources _xml_file _include _parentClass) # _optionalBasename _optionalClassName)
156    get_filename_component(_infile ${_xml_file} ABSOLUTE)
157
158    set(_optionalBasename "${ARGV4}")
159    if(_optionalBasename)
160        set(_basename ${_optionalBasename} )
161    else()
162        string(REGEX REPLACE "(.*[/\\.])?([^\\.]+)\\.xml" "\\2adaptor" _basename ${_infile})
163        string(TOLOWER ${_basename} _basename)
164    endif()
165
166    set(_optionalClassName "${ARGV5}")
167    set(_header "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.h")
168    set(_impl   "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.cpp")
169    set(_moc    "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.moc")
170
171    if(_optionalClassName)
172        add_custom_command(OUTPUT "${_impl}" "${_header}"
173          COMMAND ${Qt5DBus_QDBUSXML2CPP_EXECUTABLE} -m -a ${_basename} -c ${_optionalClassName} -i ${_include} -l ${_parentClass} ${_infile}
174          DEPENDS ${_infile} VERBATIM
175        )
176    else()
177        add_custom_command(OUTPUT "${_impl}" "${_header}"
178          COMMAND ${Qt5DBus_QDBUSXML2CPP_EXECUTABLE} -m -a ${_basename} -i ${_include} -l ${_parentClass} ${_infile}
179          DEPENDS ${_infile} VERBATIM
180        )
181    endif()
182
183    qt5_generate_moc("${_header}" "${_moc}")
184    set_source_files_properties("${_impl}" "${_header}" PROPERTIES
185        SKIP_AUTOMOC TRUE
186        SKIP_AUTOUIC TRUE
187    )
188    macro_add_file_dependencies("${_impl}" "${_moc}")
189
190    list(APPEND ${_sources} "${_impl}" "${_header}" "${_moc}")
191    set(${_sources} ${${_sources}} PARENT_SCOPE)
192endfunction()
193
194if(NOT QT_NO_CREATE_VERSIONLESS_FUNCTIONS)
195    function(qt_add_dbus_adaptor sources)
196        if(QT_DEFAULT_MAJOR_VERSION EQUAL 5)
197            qt5_add_dbus_adaptor("${sources}" ${ARGN})
198        elseif(QT_DEFAULT_MAJOR_VERSION EQUAL 6)
199            qt6_add_dbus_adaptor("${sources}" ${ARGN})
200        endif()
201        set("${sources}" "${${sources}}" PARENT_SCOPE)
202    endfunction()
203endif()
204