KDECMakeSettings.cmake 14.9 KB
Newer Older
1
2
3
4
5
6
7
8
# SPDX-FileCopyrightText: 2014 Alex Merry <alex.merry@kde.org>
# SPDX-FileCopyrightText: 2013 Aleix Pol <aleixpol@kde.org>
# SPDX-FileCopyrightText: 2012-2013 Stephen Kelly <steveire@gmail.com>
# SPDX-FileCopyrightText: 2007 Matthias Kretz <kretz@kde.org>
# SPDX-FileCopyrightText: 2006-2007 Laurent Montel <montel@kde.org>
# SPDX-FileCopyrightText: 2006-2013 Alex Neundorf <neundorf@kde.org>
#
# SPDX-License-Identifier: BSD-3-Clause
9

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#[=======================================================================[.rst:
KDECMakeSettings
----------------

Changes various CMake settings to what the KDE community views as more
sensible defaults.

It is recommended to include this module with the NO_POLICY_SCOPE flag,
otherwise you may get spurious warnings with some versions of CMake.

It is split into three parts, which can be independently disabled if desired.

Runtime Paths
~~~~~~~~~~~~~

The default runtime path (used on Unix systems to search for
dynamically-linked libraries) is set to include the location that libraries
will be installed to (as set in LIB_INSTALL_DIR or, if the former is not set,
KDE_INSTALL_LIBDIR), and also the linker search path.

Note that ``LIB_INSTALL_DIR`` or alternatively ``KDE_INSTALL_LIBDIR`` needs
to be set before including this module.
Typically, this is done by including the :kde-module:`KDEInstallDirs` module.

This section can be disabled by setting ``KDE_SKIP_RPATH_SETTINGS`` to TRUE
before including this module.


Testing
~~~~~~~

Testing is enabled by default, and an option (BUILD_TESTING) is provided for
users to control this. See the CTest module documentation in the CMake manual
for more details.

This section can be disabled by setting ``KDE_SKIP_TEST_SETTINGS`` to TRUE
before including this module.


Build Settings
~~~~~~~~~~~~~~

Various CMake build defaults are altered, such as searching source and build
directories for includes first, enabling automoc by default.

When find_package(ECM 5.38) or higher is called, this also selects
a layout for the build dir that helps running executables without installing:
all executables are built into a toplevel "bin" dir, making it possible to find
helper binaries, and to find uninstalled plugins (provided that you use
kcoreaddons_add_plugin or set LIBRARY_OUTPUT_DIRECTORY as documented on
https://community.kde.org/Guidelines_and_HOWTOs/Making_apps_run_uninstalled).

This section can be disabled by setting ``KDE_SKIP_BUILD_SETTINGS`` to TRUE
before including this module.

This section also provides an "uninstall" target that can be individually
disabled by setting ``KDE_SKIP_UNINSTALL_TARGET`` to TRUE before including
this module.

By default on OS X, X11 and XCB related detections are disabled. However if
the need would arise to use these technologies, the detection can be enabled
by setting ``APPLE_FORCE_X11`` to ``ON``.

A warning is printed for the developer to know that the detection is disabled on OS X.
This message can be turned off by setting ``APPLE_SUPPRESS_X11_WARNING`` to ``ON``.

Since pre-1.0.0.

``ENABLE_CLAZY`` option is added (OFF by default) when clang is being used.
Turning this option on will force clang to load the clazy plugins for richer
warnings on Qt-related code.

If clang is not being used, this won't have an effect.
See https://commits.kde.org/clazy?path=README.md

Since 5.17.0

- Uninstall target functionality since 1.7.0.
- ``APPLE_FORCE_X11`` option since 5.14.0 (detecting X11 was previously the default behavior)
- ``APPLE_SUPPRESS_X11_WARNING`` option since 5.14.0
- CMAKE_AUTORCC enabled by default when supported by cmake (>= 3.0) since 5.62.0

Translations
~~~~~~~~~~~~
A fetch-translations target will be set up that will download translations
for projects using l10n.kde.org.

``KDE_L10N_BRANCH`` will be responsible for choosing which l10n branch to use
for the translations.

``KDE_L10N_AUTO_TRANSLATIONS`` (OFF by default) will indicate whether translations
should be downloaded when building the project.

Since 5.34.0

``KDE_L10N_SYNC_TRANSLATIONS`` (OFF by default) will download the translations at configuration
time instead of build time.

Since 5.50.0
#]=======================================================================]

111
112
113
################# RPATH handling ##################################

if(NOT KDE_SKIP_RPATH_SETTINGS)
Alex Merry's avatar
Alex Merry committed
114

115
116
   # Set the default RPATH to point to useful locations, namely where the
   # libraries will be installed and the linker search path
117

118
   # First look for the old LIB_INSTALL_DIR, then fallback to newer KDE_INSTALL_LIBDIR
119
   if(NOT LIB_INSTALL_DIR)
120
121
122
123
124
125
126
      if(NOT KDE_INSTALL_LIBDIR)
         message(FATAL_ERROR "Neither KDE_INSTALL_LIBDIR nor LIB_INSTALL_DIR is set. Setting one is necessary for using the RPATH settings.")
      else()
         set(_abs_LIB_INSTALL_DIR "${KDE_INSTALL_LIBDIR}")
      endif()
   else()
      set(_abs_LIB_INSTALL_DIR "${LIB_INSTALL_DIR}")
127
128
   endif()

Alexander Neundorf's avatar
Alexander Neundorf committed
129
130
131
132
   if (NOT IS_ABSOLUTE "${_abs_LIB_INSTALL_DIR}")
      set(_abs_LIB_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${LIB_INSTALL_DIR}")
   endif()

133
   if (UNIX)
134
135
      # for mac os: add install name dir in addition
      # check: is the rpath stuff below really required on mac os? at least it seems so to use a stock qt from qt.io
136
      if (APPLE)
Alexander Neundorf's avatar
Alexander Neundorf committed
137
         set(CMAKE_INSTALL_NAME_DIR ${_abs_LIB_INSTALL_DIR})
138
      endif ()
139
140
141
142
143
144
145
146
147
148
149
150

      # add our LIB_INSTALL_DIR to the RPATH (but only when it is not one of
      # the standard system link directories - such as /usr/lib on UNIX)
      list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${_abs_LIB_INSTALL_DIR}" _isSystemLibDir)
      list(FIND CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES      "${_abs_LIB_INSTALL_DIR}" _isSystemCxxLibDir)
      list(FIND CMAKE_C_IMPLICIT_LINK_DIRECTORIES        "${_abs_LIB_INSTALL_DIR}" _isSystemCLibDir)
      if("${_isSystemLibDir}" STREQUAL "-1"  AND  "${_isSystemCxxLibDir}" STREQUAL "-1"  AND  "${_isSystemCLibDir}" STREQUAL "-1")
         set(CMAKE_INSTALL_RPATH "${_abs_LIB_INSTALL_DIR}")
      endif()

      # Append directories in the linker search path (but outside the project)
      set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
151
152
153
154
   endif (UNIX)

endif()

155
156
################ Testing setup ####################################

157
158
find_program(APPSTREAMCLI appstreamcli)
function(appstreamtest)
159
160
    if(APPSTREAMCLI AND NOT appstreamtest_added)
        set(appstreamtest_added TRUE PARENT_SCOPE)
161
        add_test(NAME appstreamtest COMMAND ${CMAKE_COMMAND} -DAPPSTREAMCLI=${APPSTREAMCLI} -DINSTALL_FILES=${CMAKE_BINARY_DIR}/install_manifest.txt -P ${CMAKE_CURRENT_LIST_DIR}/appstreamtest.cmake)
162
163
164
165
    else()
        message(STATUS "Could not set up the appstream test. appstreamcli is missing.")
    endif()
endfunction()
166
167

if(NOT KDE_SKIP_TEST_SETTINGS)
Alex Merry's avatar
Alex Merry committed
168
169
170
171
172

   # If there is a CTestConfig.cmake, include CTest.
   # Otherwise, there will not be any useful settings, so just
   # fake the functionality we care about from CTest.

173
174
   if (EXISTS ${CMAKE_SOURCE_DIR}/CTestConfig.cmake)
      include(CTest)
175
176
177
178
   else()
      option(BUILD_TESTING "Build the testing tree." ON)
      if(BUILD_TESTING)
         enable_testing()
179
         appstreamtest()
180
181
      endif ()
   endif ()
Alex Merry's avatar
Alex Merry committed
182

183
184
185
186
187
endif()



################ Build-related settings ###########################
188

189
if(NOT KDE_SKIP_BUILD_SETTINGS)
190

191
192
193
194
195
196
197
   # Always include srcdir and builddir in include path
   # This saves typing ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} in about every subdir
   # since cmake 2.4.0
   set(CMAKE_INCLUDE_CURRENT_DIR ON)

   # put the include dirs which are in the source or build tree
   # before all other include dirs, so the headers in the sources
Yuri Chornoivan's avatar
Yuri Chornoivan committed
198
   # are preferred over the already installed ones
199
200
201
   # since cmake 2.4.1
   set(CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE ON)

202
203
   # Add the src and build dir to the BUILD_INTERFACE include directories
   # of all targets. Similar to CMAKE_INCLUDE_CURRENT_DIR, but transitive.
Alexander Neundorf's avatar
Alexander Neundorf committed
204
   # Since CMake 2.8.11
205
   set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE ON)
206

207
208
   # When a shared library changes, but its includes do not, don't relink
   # all dependencies. It is not needed.
Alexander Neundorf's avatar
Alexander Neundorf committed
209
   # Since CMake 2.8.11
210
211
   set(CMAKE_LINK_DEPENDS_NO_SHARED ON)

Yuri Chornoivan's avatar
Yuri Chornoivan committed
212
   # Default to shared libs for KDE, if no type is explicitly given to add_library():
213
214
   set(BUILD_SHARED_LIBS TRUE CACHE BOOL "If enabled, shared libs will be built by default, otherwise static libs")

215
   # Enable automoc in cmake
Alexander Neundorf's avatar
Alexander Neundorf committed
216
   # Since CMake 2.8.6
217
218
   set(CMAKE_AUTOMOC ON)

219
   # Enable autorcc and in cmake so qrc files get generated.
220
   # Since CMake 3.0
221
222
   # TODO KF6: discuss enabling AUTOUIC and note porting requirements. autouic
   #   acts on all #include "ui_*.h" assuming *.ui exists
223
   set(CMAKE_AUTORCC ON)
224

Alexander Neundorf's avatar
Alexander Neundorf committed
225
226
227
228
229
230
   # By default, create 'GUI' executables. This can be reverted on a per-target basis
   # using ECMMarkNonGuiExecutable
   # Since CMake 2.8.8
   set(CMAKE_WIN32_EXECUTABLE ON)
   set(CMAKE_MACOSX_BUNDLE ON)

231
232
233
234
   # By default, don't put a prefix on MODULE targets. add_library(MODULE) is basically for plugin targets,
   # and in KDE plugins don't have a prefix.
   set(CMAKE_SHARED_MODULE_PREFIX "")

235
236
   unset(EXECUTABLE_OUTPUT_PATH)
   unset(LIBRARY_OUTPUT_PATH)
237
238
239
240
241
242
243
244
   unset(CMAKE_ARCHIVE_OUTPUT_DIRECTORY)
   unset(CMAKE_LIBRARY_OUTPUT_DIRECTORY)
   unset(CMAKE_RUNTIME_OUTPUT_DIRECTORY)

   # under Windows, output all executables and dlls into
   # one common directory, and all static|import libraries and plugins
   # into another one. This way test executables can find their dlls
   # even without installation.
245
246

   # We do the same under Unix to make it possible to run tests and apps without installing
247
   if (WIN32 OR ECM_GLOBAL_FIND_VERSION VERSION_GREATER_EQUAL 5.38.0)
248
249
250
       set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
       set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
       set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
251
252
   endif()

253
254
255
256
257
258
259
   # For Android we need to put shared libraries into "lib" for androiddeployqt to work without prior installation.
   # That fact that this conflicts with the above isn't really an issue, as we can't run things while cross-compiling
   # for Android anyway.
   if (ANDROID)
      set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
   endif()

260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
   if (APPLE)
       # Disable detection of X11 and related package on OS X because when using
       # brew or macports, X11 can be installed and thus is detected.
       option(APPLE_FORCE_X11 "Force enable X11 related detection on OS X" OFF)
       option(APPLE_SUPPRESS_X11_WARNING "Suppress X11 and related technologies search disabling warning on OS X" OFF)

       if(NOT APPLE_FORCE_X11)
           if (NOT APPLE_SUPPRESS_X11_WARNING)
               message(WARNING "Searching for X11 and related technologies is disabled on Apple systems. Set APPLE_FORCE_X11 to ON to change this behaviour. Set APPLE_SUPPRESS_X11_WARNING to ON to hide this warning.")
           endif()
           set(CMAKE_DISABLE_FIND_PACKAGE_X11 true)
           set(CMAKE_DISABLE_FIND_PACKAGE_XCB true)
           set(CMAKE_DISABLE_FIND_PACKAGE_Qt5X11Extras true)
       endif()
    endif()
275

276
    option(KDE_SKIP_UNINSTALL_TARGET "Prevent an \"uninstall\" target from being generated." OFF)
277
278
279
280
   if(NOT KDE_SKIP_UNINSTALL_TARGET)
       include("${ECM_MODULE_DIR}/ECMUninstallTarget.cmake")
   endif()

281
endif()
282

283
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
284
285
286
    option(ENABLE_CLAZY "Enable Clazy warnings" OFF)

    if(ENABLE_CLAZY)
287
288
289
290
291
292
        find_library(CLAZY_v1_5_FOUND ClazyPlugin${CMAKE_SHARED_LIBRARY_SUFFIX})
        if(CLAZY_v1_5_FOUND) # clazy >= 1.5
            set(CMAKE_CXX_COMPILE_OBJECT "${CMAKE_CXX_COMPILE_OBJECT} -Xclang -load -Xclang ClazyPlugin${CMAKE_SHARED_LIBRARY_SUFFIX} -Xclang -add-plugin -Xclang clazy")
        else() # clazy < 1.5
            set(CMAKE_CXX_COMPILE_OBJECT "${CMAKE_CXX_COMPILE_OBJECT} -Xclang -load -Xclang ClangLazy${CMAKE_SHARED_LIBRARY_SUFFIX} -Xclang -add-plugin -Xclang clang-lazy")
        endif()
293
294
295
    endif()
endif()

296
###################################################################
297
298
# Download translations

299
function(_repository_name reponame dir)
300
301
    execute_process(COMMAND git rev-parse --symbolic-full-name @{u}
        OUTPUT_VARIABLE upstream_ref
302
        RESULT_VARIABLE exitCode
303
        WORKING_DIRECTORY "${dir}")
304
    if(exitCode EQUAL 0)
305
        string(REGEX REPLACE "refs/remotes/([^/]+)/.*" "\\1" gitorigin "${upstream_ref}")
306
        message(DEBUG "Git upstream inferred as ${gitorigin}, upstream ref was ${upstream_ref}")
307
308
309
    else()
        set(gitorigin "origin")
        message(DEBUG "Assuming origin as the git remote as we are in detached mode")
310
    endif()
311

312
313
314
315
316
    execute_process(COMMAND git remote get-url --all "${gitorigin}"
        OUTPUT_VARIABLE giturl
        RESULT_VARIABLE exitCode
        WORKING_DIRECTORY "${dir}")

317
    if(exitCode EQUAL 0)
318
        message(DEBUG "Git URL inferred as ${giturl}")
319
        string(REGEX MATCHALL ".+kde\\.org[:\\/]([-A-Za-z0-9\\/]+)(.git)?\\s*" "" ${giturl})
320
        set(${reponame} ${CMAKE_MATCH_1})
321
        message(DEBUG "Repository inferred as ${${reponame}}")
322
323
324
    endif()

    if(NOT ${reponame})
325
        set(${reponame} ${CMAKE_PROJECT_NAME})
326
    endif()
327
    set(${reponame} ${${reponame}} PARENT_SCOPE)
328
329
endfunction()

330
if(NOT EXISTS ${CMAKE_SOURCE_DIR}/po AND NOT TARGET fetch-translations)
331
    option(KDE_L10N_AUTO_TRANSLATIONS "Automatically 'make fetch-translations`" OFF)
332
    option(KDE_L10N_SYNC_TRANSLATIONS "Fetch translations when KDECMakeSettings.cmake is processed." OFF)
333
    set(KDE_L10N_BRANCH "trunk" CACHE STRING "Branch from l10n.kde.org to fetch from: trunk | stable | lts | trunk_kde4 | stable_kde4")
334

335
    if(KDE_L10N_AUTO_TRANSLATIONS AND NOT KDE_L10N_SYNC_TRANSLATIONS)
336
337
338
        set(_EXTRA_ARGS "ALL")
    else()
        set(_EXTRA_ARGS)
339
340
    endif()

341
342
    set(_reponame "")
    _repository_name(_reponame "${CMAKE_SOURCE_DIR}")
343

344
    set(releaseme_clone_commands
345
        COMMAND git clone --depth 1 https://invent.kde.org/sdk/releaseme.git
346
    )
347
348
    add_custom_command(
        OUTPUT "${CMAKE_BINARY_DIR}/releaseme"
349
        ${releaseme_clone_commands}
350
        WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
351
352
353
        COMMENT "Fetching releaseme scripts to download translations..."
    )

354
355
356
    set(_l10n_po_dir "${CMAKE_BINARY_DIR}/po")
    set(_l10n_poqm_dir "${CMAKE_BINARY_DIR}/poqm")

357
    set(extra BYPRODUCTS ${_l10n_po_dir} ${_l10n_poqm_dir})
358

359
    set(fetch_commands
360
361
362
363
364
365
        COMMAND ruby "${CMAKE_BINARY_DIR}/releaseme/fetchpo.rb"
            --origin ${KDE_L10N_BRANCH}
            --project "${_reponame}"
            --output-dir "${_l10n_po_dir}"
            --output-poqm-dir "${_l10n_poqm_dir}"
            "${CMAKE_CURRENT_SOURCE_DIR}"
366
367
368
369
370
371
372
373
    )

    add_custom_target(fetch-translations ${_EXTRA_ARGS}
        COMMENT "Downloading translations for ${_reponame} branch ${KDE_L10N_BRANCH}..."
        COMMAND git -C "${CMAKE_BINARY_DIR}/releaseme" pull
        COMMAND cmake -E remove_directory ${_l10n_po_dir}
        COMMAND cmake -E remove_directory ${_l10n_poqm_dir}
        ${fetch_commands}
374
375
376
        ${extra}
        DEPENDS "${CMAKE_BINARY_DIR}/releaseme"
    )
377
378
379
380
381

    if (KDE_L10N_SYNC_TRANSLATIONS AND (NOT EXISTS ${_l10n_po_dir} OR NOT EXISTS ${_l10n_poqm_dir}))
        execute_process(${releaseme_clone_commands})
        execute_process(${fetch_commands})
    endif()
382
endif()