summaryrefslogtreecommitdiff
path: root/eclass/ecm-common.eclass
blob: 7f5d3d1e99153bf080ab2212f696920db284148b (plain)
1
2
3
4
5
6
7
8
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# Copyright 2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# @ECLASS: ecm-common.eclass
# @MAINTAINER:
# kde@gentoo.org
# @SUPPORTED_EAPIS: 8
# @PROVIDES: cmake
# @BLURB: Standalone CMake calling std. ECM macros to install common files only.
# @DESCRIPTION:
# This eclass is used for installing common files of packages using ECM macros,
# most of the time translations, but optionally also icons and kcfg files. This
# is mainly useful for packages split from a single upstream tarball, or for
# collision handling of slotted package versions, which need to share a common
# files package.
# Conventionally we will use ${PN}-common for these split packages.

case ${EAPI} in
	8) ;;
	*) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
esac

if [[ -z ${_ECM_COMMON_ECLASS} ]]; then
_ECM_COMMON_ECLASS=1

inherit cmake

# @ECLASS_VARIABLE: KFMIN
# @DEFAULT_UNSET
# @DESCRIPTION:
# Minimum version of Frameworks to require.  Default value for kde-frameworks
# is ${PV} and 6.0.0 baseline for everything else.
# If set to <5.240, it is assumed dependencies are fulfilled by KF5/Qt5
# alternatively, thus a block of SLOT=5 shadow dependencies added.
if [[ ${CATEGORY} = kde-frameworks ]]; then
	: "${KFMIN:=$(ver_cut 1-2)}"
fi
: "${KFMIN:=6.0.0}"

# @ECLASS_VARIABLE: _KFSLOT
# @INTERNAL
# @DESCRIPTION:
# KDE Frameworks main slot dependency for consistently finding all KF5 or KF6
# in CMakeLists.txt.  Consistency over installed KF packages is established via
# BDEPEND, then detected in pkg_setup().
# Is passed as -DQT_MAJOR_VERSION=${_KFSLOT} in src_configure() too.
_KFSLOT=6

# @ECLASS_VARIABLE: KF5_BDEPEND
# @PRE_INHERIT
# @DESCRIPTION:
# Dynamic KF5 dependency list.
if [[ ${KF5_BDEPEND} ]]; then
	[[ ${KF5_BDEPEND@a} == *a* ]] ||
		die "KF5_BDEPEND must be an array"
else
	KF5_BDEPEND=( )
fi

# @ECLASS_VARIABLE: KF6_BDEPEND
# @PRE_INHERIT
# @DESCRIPTION:
# Dynamic KF6 dependency list.
if [[ ${KF6_BDEPEND} ]]; then
	[[ ${KF6_BDEPEND@a} == *a* ]] ||
		die "KF6_BDEPEND must be an array"
else
	KF6_BDEPEND=( )
fi

# @ECLASS_VARIABLE: ECM_I18N
# @PRE_INHERIT
# @DESCRIPTION:
# Will accept "true" (default) or "false".  If set to "false", do nothing.
# Otherwise, add kde-frameworks/ki18n:* to BDEPEND, find KF[56]I18n and let
# ki18n_install(po) generate and install translations.
: "${ECM_I18N:=true}"

# @ECLASS_VARIABLE: ECM_HANDBOOK
# @PRE_INHERIT
# @DESCRIPTION:
# Will accept "true" or "false" (default).  If set to "false", do nothing.
# Otherwise, add "+handbook" to IUSE, add kde-frameworks/kdoctools:* to BDEPEND
# find KF[56]DocTools in CMake, call add_subdirectory(ECM_HANDBOOK_DIRS)
# and let let kdoctools_install(po) generate and install translated docbook
# files.
: "${ECM_HANDBOOK:=false}"

# @ECLASS_VARIABLE: ECM_HANDBOOK_DIRS
# @PRE_INHERIT
# @DESCRIPTION:
# Default is "doc" which is correct for the vast majority of packages. Specifies
# one or more directories containing untranslated docbook file(s) relative to
# ${S} to be added via add_subdirectory.
if [[ ${ECM_HANDBOOK_DIRS} ]]; then
	[[ ${ECM_HANDBOOK_DIRS@a} == *a* ]] ||
		die "ECM_HANDBOOK_DIRS must be an array"
else
	ECM_HANDBOOK_DIRS=( doc )
fi

# @ECLASS_VARIABLE: ECM_INSTALL_FILES
# @DEFAULT_UNSET
# @DESCRIPTION:
# Array of <file>:<destination_path> tuples to install by CMake via
# install(FILES <file> DESTINATION <destination_path>)
if [[ ${ECM_INSTALL_FILES} ]]; then
	[[ ${ECM_INSTALL_FILES@a} == *a* ]] ||
		die "ECM_INSTALL_FILES must be an array"
fi

# @ECLASS_VARIABLE: ECM_INSTALL_ICONS
# @DEFAULT_UNSET
# @DESCRIPTION:
# Array of <icon>:<icon_install_dir> tuples to feed to ECMInstallIcons
# via ecm_install_icons(ICONS <icon> DESTINATION <icon_install_dir)
if [[ ${ECM_INSTALL_ICONS} ]]; then
	[[ ${ECM_INSTALL_ICONS@a} == *a* ]] ||
		die "ECM_INSTALL_ICONS must be an array"
fi

# @ECLASS_VARIABLE: ECM_KCM_TARGETS
# @DEFAULT_UNSET
# @PRE_INHERIT
# @DESCRIPTION:
# Array of <target>:<subdir> tuples to feed to ECMInstallIcons via
# ecmcommon_generate_desktop_file(<target> <subdir>), which is this
# eclass adaptation of kcmutils_generate_desktop_file.
if [[ ${ECM_KCM_TARGETS} ]]; then
	[[ ${ECM_KCM_TARGETS@a} == *a* ]] ||
		die "ECM_KCM_TARGETS must be an array"
fi

DESCRIPTION="Common files for ${PN/-common/}"

BDEPEND=">=kde-frameworks/extra-cmake-modules-${KFMIN}:*"

case ${ECM_I18N} in
	true)
		KF5_BDEPEND+=( "kde-frameworks/ki18n:5" )
		KF6_BDEPEND+=( "kde-frameworks/ki18n:6" )
		;;
	false) ;;
	*)
		eerror "Unknown value for \${ECM_I18N}"
		die "Value ${ECM_I18N} is not supported"
		;;
esac

case ${ECM_HANDBOOK} in
	true)
		IUSE+=" +handbook"
		KF5_BDEPEND+=( "handbook? ( kde-frameworks/kdoctools:5 )" )
		KF6_BDEPEND+=( "handbook? ( kde-frameworks/kdoctools:6 )" )
		;;
	false) ;;
	*)
		eerror "Unknown value for \${ECM_HANDBOOK}"
		die "Value ${ECM_HANDBOOK} is not supported"
		;;
esac

if [[ ${ECM_KCM_TARGETS} ]]; then
	KF5_BDEPEND+=( "kde-frameworks/kcmutils:5" )
	KF6_BDEPEND+=( "kde-frameworks/kcmutils:6" )
fi

KF6_BDEPEND+=( "dev-qt/qtbase:6" )

if $(ver_test ${KFMIN} -lt 5.240) && [[ ${KF6_BDEPEND} && ${KF5_BDEPEND} ]]; then
	BDEPEND+=" || (
		( ${KF6_BDEPEND[*]} )
		(
			${KF5_BDEPEND[*]}
			dev-qt/qtcore:5
		)
	)"
else
	BDEPEND+=" ${KF6_BDEPEND[*]}"
fi

# @FUNCTION: _ecm-common_preamble
# @INTERNAL
# @DESCRIPTION:
# Create a CMakeLists.txt file with minimum ECM setup.
_ecm-common_preamble() {
	cat > CMakeLists.txt <<- _EOF_ || die
		cmake_minimum_required(VERSION 3.16)
		project(${PN} VERSION ${PV})

		find_package(ECM "${KFMIN}" REQUIRED NO_MODULE)
		set(CMAKE_MODULE_PATH \${ECM_MODULE_PATH})

		# Set by pkg_setup(); Use this if need to differ between KF5 or KF6
		set(KFSLOT ${_KFSLOT})
		set(KDE_INSTALL_DOCBUNDLEDIR "${EPREFIX}/usr/share/help" CACHE PATH "")

		include(KDEInstallDirs)
		include(ECMOptionalAddSubdirectory) # commonly used
		include(FeatureSummary)
	_EOF_

	if [[ ${ECM_INSTALL_ICONS} ]]; then
		cat >> CMakeLists.txt <<- _EOF_ || die
			include(ECMInstallIcons)
		_EOF_
	fi
}

# @FUNCTION: _ecm-common_i18n
# @INTERNAL
# @DESCRIPTION:
# Find KF[56]I18n and call ki18n_install(po).
_ecm-common_i18n() {
	[[ ${ECM_I18N} == true ]] || return
	cat >> CMakeLists.txt <<- _EOF_ || die
		find_package(KF\${KFSLOT}I18n REQUIRED)
		ki18n_install(po)
	_EOF_
}

# @FUNCTION: _ecm-common_docs
# @INTERNAL
# @DESCRIPTION:
# Find KF[56]DocTools, call kdoctools_install(po) and
# add_subdirectory(${ECM_HANDBOOK_DIRS})
_ecm-common_docs() {
	{ in_iuse handbook && use handbook; } || return

	cat >> CMakeLists.txt <<- _EOF_ || die
		find_package(KF\${KFSLOT}DocTools REQUIRED)
		kdoctools_install(po)
	_EOF_

	local i
	for i in "${ECM_HANDBOOK_DIRS[@]}"; do
		if [[ -d ${i} ]]; then
			cat >> CMakeLists.txt <<- _EOF_ || die
				add_subdirectory(${i})
			_EOF_
		fi
	done
}

# @FUNCTION: _ecm-common_generate_desktop_file
# @INTERNAL
# @DESCRIPTION:
# Find KF[56]KCMUtils and iterate through ECM_KCM_TARGETS to generate
# desktop files out of json.
_ecm-common_generate_desktop_file() {
	[[ ${ECM_KCM_TARGETS} ]] || return

	cat >> CMakeLists.txt <<- _EOF_ || die
		find_package(KF\${KFSLOT}KCMUtils REQUIRED)
		# extracted from kcmutils_generate_desktop_file(kcm_target)
		function(ecmcommon_generate_desktop_file kcm_target subdir)
			set(IN_FILE \${CMAKE_CURRENT_SOURCE_DIR}/\${subdir}\${kcm_target}.json)
			set(OUT_FILE \${CMAKE_CURRENT_BINARY_DIR}/\${kcm_target}.desktop)
			add_custom_target(\${kcm_target}-kcm-desktop-gen ALL
				COMMAND KF\${KFSLOT}::kcmdesktopfilegenerator \${IN_FILE} \${OUT_FILE}
				DEPENDS \${IN_FILE})
			install(FILES \${OUT_FILE} DESTINATION \${KDE_INSTALL_APPDIR})
		endfunction()
	_EOF_

	local i
	for i in "${ECM_KCM_TARGETS[@]}"; do
		cat >> CMakeLists.txt <<- _EOF_ || die
			ecmcommon_generate_desktop_file(${i%:*} ${i#*:})
		_EOF_
	done
}

# @FUNCTION: _ecm-common_ecm_install_icons
# @INTERNAL
# @DESCRIPTION:
# Installs icons listed in ECM_INSTALL_ICONS using ecm_install_icons
_ecm-common_ecm_install_icons() {
	[[ ${ECM_INSTALL_ICONS} ]] || return
	local i
	for i in "${ECM_INSTALL_ICONS[@]}"; do
		cat >> CMakeLists.txt <<- _EOF_ || die
			ecm_install_icons(ICONS ${i%:*} DESTINATION ${i#*:})
		_EOF_
	done
}

# @FUNCTION: _ecm-common_ecm_install_files
# @INTERNAL
# @DESCRIPTION:
# Installs files listed in ECM_INSTALL_FILES using install(FILES ...)
_ecm-common_ecm_install_files() {
	[[ ${ECM_INSTALL_FILES} ]] || return
	local i
	for i in "${ECM_INSTALL_FILES[@]}"; do
		cat >> CMakeLists.txt <<- _EOF_ || die
			install(FILES ${i%:*} DESTINATION ${i#*:})
		_EOF_
	done
}

# @FUNCTION: ecm-common_inject_heredoc
# @DESCRIPTION:
# Override this to inject custom Heredoc into the root CMakeLists.txt
ecm-common_inject_heredoc() {
	debug-print-function ${FUNCNAME} "$@"
}

# @FUNCTION: _ecm-common_summary
# @INTERNAL
# @DESCRIPTION:
# Just calls feature_summary
_ecm-common_summary() {
	cat >> CMakeLists.txt <<- _EOF_ || die

		feature_summary(WHAT ALL INCLUDE_QUIET_PACKAGES FATAL_ON_MISSING_REQUIRED_PACKAGES)
	_EOF_
}

# @FUNCTION: _ecm-common-check_deps
# @INTERNAL
# @DESCRIPTION:
# Check existence of requested KF6 dependencies.
_ecm-common-check_deps() {
	local chk=0
	case ${1} in
		i18n)
			if [[ ${ECM_I18N} ]]; then
				chk=$(has_version -b "kde-frameworks/ki18n:6")
			fi
			;;
		doctools)
			if [[ ${ECM_HANDBOOK} ]] && in_iuse handbook; then
				if use handbook; then
					chk=$(has_version -b "kde-frameworks/kdoctools:6")
				fi
			fi
			;;
		kcmutils)
			if [[ ${ECM_KCM_TARGETS} ]]; then
				chk=$(has_version -b "kde-frameworks/kcmutils:6")
			fi
			;;
		*)
			eerror "Unknown value for _ecm-common-check_deps()"
			die "Value ${1} is not supported"
			;;
	esac
	return ${chk}
}

# @FUNCTION: ecm-common-check_deps
# @DESCRIPTION:
# Override this to add more KF6 has_version checks to pkg_setup(),
# corresponding with any additional KF6_BDEPEND defined pre-inherit.
# If false, we'll assume KF5 dependencies are fulfilled via BDEPEND.
ecm-common-check_deps() {
	return 0
}

# @FUNCTION: ecm-common_pkg_setup
# @DESCRIPTION:
# If KFMIN is not lower than 5.240 (default is 6.0.0), do nothing.
# Otherwise, dDetermine which of KF5 or KF6-based depgraph is complete,
# preferring KF6.  The result is stored in _KFSLOT, which is then handed
# to CMakeLists.txt as KFSLOT var for further use.
ecm-common_pkg_setup() {
	$(ver_test ${KFMIN} -ge 5.240) && return

	if _ecm-common-check_deps i18n && _ecm-common-check_deps doctools &&
		_ecm-common-check_deps kcmutils && ecm-common-check_deps
	then
		_KFSLOT=6
	else
		_KFSLOT=5
	fi
}

# @FUNCTION: ecm-common_src_prepare
# @DESCRIPTION:
# Wrapper for cmake_src_prepare with a Heredoc replacing the standard
# root CMakeLists.txt file to only generate and install translations.
ecm-common_src_prepare() {
	debug-print-function ${FUNCNAME} "$@"

	_ecm-common_preamble
	_ecm-common_i18n
	_ecm-common_docs
	_ecm-common_generate_desktop_file
	_ecm-common_ecm_install_icons
	_ecm-common_ecm_install_files
	ecm-common_inject_heredoc
	_ecm-common_summary

	cmake_src_prepare
}

# @FUNCTION: ecm-common_src_configure
# @DESCRIPTION:
# Passes -DQT_MAJOR_VERSION=${_KFSLOT} only.
ecm-common_src_configure() {
	# necessary for at least KF6KCMUtils
	local cmakeargs=( -DQT_MAJOR_VERSION=${_KFSLOT} )

	# allow the ebuild to override what we set here
	mycmakeargs=("${cmakeargs[@]}" "${mycmakeargs[@]}")

	cmake_src_configure
}

fi

EXPORT_FUNCTIONS pkg_setup src_prepare src_configure