aboutsummaryrefslogtreecommitdiff
blob: affe3cb58143d8a15048abc8438d325f59bbbbda (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
# Copyright 2022 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# @ECLASS: nimble.eclass
# @MAINTAINER:
# Anna Vyalkova <cyber+gentoo@sysrq.in>
# @AUTHOR:
# Anna Vyalkova <cyber+gentoo@sysrq.in>
# @SUPPORTED_EAPIS: 8
# @PROVIDES: nim-utils
# @BLURB: eclass to build Nim packages that use nimble as a build system
# @EXAMPLE:
# Typical ebuild for a Nim application:
#
# @CODE@
# EAPI=8
#
# inherit nimble
#
# ...
#
# DEPEND="dev-nim/foo"
#
# src_compile() {
# 	nimble_src_compile
# 	nimble_build scss
# }
# @CODE@
#
#
# Typical ebuild for a Nim library:
#
# @CODE@
# EAPI=8
#
# inherit nimble
#
# ...
#
# SLOT=${PV}
#
# RDEPEND="
# 	dev-nim/bar
# 	dev-nim/baz
# "
#
# set_package_url "https://github.com/example/example"
# @CODE@


case ${EAPI} in
	8) ;;
	*) die "${ECLASS}: EAPI ${EAPI} unsupported."
esac

if [[ ! ${_NIMBLE_ECLASS} ]]; then

# @ECLASS_VARIABLE: BUILD_DIR
# @DEFAULT_UNSET
# @DESCRIPTION:
# Build directory, location where all generated files should be placed.
# If this isn't set, it defaults to ${WORKDIR}/${P}-build.

# @ECLASS_VARIABLE: NINJA
# @INTERNAL
# @DESCRIPTION:
# Force ninja because samu doesn't work correctly ('-n' flag is required).
NINJA="ninja"

inherit nim-utils ninja-utils

BDEPEND="${NINJA_DEPEND}
	>=dev-nim/nimbus-1.1.0
"

# @FUNCTION: set_package_url
# @USAGE: <url>
# @DESCRIPTION:
# If this function is called, nimbus will generate and install a nimblemeta.json
# file.  Some packages specify their dependencies using URLs and nimbus is
# unable to find them unless a metadata file exists.
set_package_url() {
	debug-print-function ${FUNCNAME} "${@}"

	(( $# == 1 )) || \
		die "${FUNCNAME} takes exactly one argument"

	readonly _PACKAGE_URL="${1}"
}

# @FUNCTION: get_package_url
# @USAGE:
# @INTERNAL
# @RETURN: package URL
get_package_url() {
	echo "${_PACKAGE_URL}"
}

# @FUNCTION: nimble_comment_requires
# @USAGE: <dep...>
# @DESCRIPTION:
# Comment out one or more 'requires' calls in the Nimble file.
nimble_comment_requires() {
	debug-print-function ${FUNCNAME} "${@}"

	local dep
	for dep in "${@}"; do
		dep=${dep//\//\\/}
		sed "/requires[[:space:]]*\"${dep}\>.*\"/ s/^/#/" -i *.nimble || die
	done
}

# @FUNCTION: nimble_src_configure
# @USAGE:
# @DESCRIPTION:
# Configure the package with nimbus.  This will start an out-of-source build.
# Passes arguments to Nim by reading from an optionally pre-defined local
# mynimargs bash array.
# @CODE
# src_configure() {
#       local mynimargs=(
#               --threads:on
#       )
#       nimble_src_configure
# }
# @CODE
nimble_src_configure() {
	debug-print-function ${FUNCNAME} "${@}"

	readonly BUILD_DIR="${BUILD_DIR:-${WORKDIR}/${P}-build}"

	[[ -z ${mynimargs} ]] && local -a mynimargs=()
	local mynimargstype=$(declare -p mynimargs 2>&-)
	if [[ "${mynimargstype}" != "declare -a mynimargs="* ]]; then
		die "mynimargs must be declared as array"
	fi

	nim_gen_config

	local nimbusargs=(
		--nimbleDir:"${EPREFIX}"/opt/nimble
		--binDir:"${EPREFIX}"/usr/bin
		"${mynimargs[@]}"
	)

	[[ -n "$(get_package_url)" ]] && \
		nimbusargs+=( --url:"$(get_package_url)" )

	set -- nimbus "${nimbusargs[@]}" "${S}" "${BUILD_DIR}"
	echo "${@}" >&2
	"${@}" || die "${*} failed"
}

# @FUNCTION: nimble_build
# @USAGE: [ninja args...]
# @DESCRIPTION:
# Function for building the package.  All arguments are passed to eninja.
nimble_build() {
	debug-print-function ${FUNCNAME} "${@}"

	eninja -C "${BUILD_DIR}" "${@}"
}

# @FUNCTION: nimble_src_compile
# @USAGE: [ninja args...]
# @DESCRIPTION:
# Build the package with Ninja.  All arguments are passed to nimble_build.
nimble_src_compile() {
	debug-print-function ${FUNCNAME} "${@}"

	nimble_build "${@}"
}

# @FUNCTION: nimble_src_test
# @USAGE: [ninja args...]
# @DESCRIPTION:
# Test the package.  All arguments are passed to nimble_build.
nimble_src_test() {
	debug-print-function ${FUNCNAME} "${@}"

	if nonfatal nimble_build -n test &> /dev/null; then
		nimble_build test "${@}"
	fi
}

# @FUNCTION: nimble_src_install
# @DESCRIPTION:
# Install the package with Ninja.  All arguments are passed to nimble_build.
nimble_src_install() {
	debug-print-function ${FUNCNAME} "${@}"

	DESTDIR="${D}" nimble_build install "${@}"
	einstalldocs
}

_NIMBLE_ECLASS=1
fi

EXPORT_FUNCTIONS src_configure src_compile src_test src_install