summaryrefslogtreecommitdiffhomepage
path: root/subr.ex/ex_rtl_configure.subr
blob: f68a0ecb8881911538b41b7123772bd802c7cf54 (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
#
# Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Lucía Andrea Illanes Albornoz <lucia@luciaillanes.de>
# set +o errexit -o noglob -o nounset is assumed.
#
# Package {configure,make} command execution
#

#
# ex_rtl_configure() - run configure script
# @_ar:				ar(1) command name or pathname
# @_cc:				C compiler command name or pathname
# @_configure:			configure script command name or pathname
# @_cxx:			C++ compiler command name or pathname
# @_ld:				ld(1) command name or pathname
# @_libtool:			libtool(1) command name or pathname or "none"
# @_pkg_config:			pkg-config(1) command name or pathname
# @_python:			python command name or pathname
# @_ranlib:			ranlib(1) command name or pathname
# @--:				(ignored)
# @_flags:			configure script flags as a whitespace-separated list
# @_flags_extra:		extra configure script flags as a whitespace-separated likst
# @_flags_list:			configure script flags as a :-separated list
# @_flags_extra_list:		extra configure script flags as a :-separated list
# @--:				(ignored)
# @_cflags:			$CFLAGS
# @_cflags_extra:		extra $CFLAGS
# @_cppflags:			$CPPFLAGS
# @_cppflags_extra:		extra $CPPFLAGS
# @_cxxflags:			$CXXFLAGS
# @_cxxflags_extra:		extra $CXXFLAGS
# @_ldflags:			$LDFLAGS
# @_ldflags_extra:		extra $LDFLAGS
# @_pkg_config_libdir:		pkg-config(1) search directory
#
# Returns:			zero (0) on success, non-zero (>0) on failure
#
ex_rtl_configure() {
	local	_erc_ar="${1}" _erc_cc="${2}" _erc_configure="${3}" _erc_cxx="${4}" _erc_ld="${5}"	\
		_erc_libtool="${6}" _erc_pkg_config="${7}" _erc_python="${8}" _erc_ranlib="${9}"	\
		_erc_ignored="${10}"									\
		_erc_flags="${11}" _erc_flags_extra="${12}"						\
		_erc_flags_list="${13}" _erc_flags_extra_list="${14}"					\
		_erc_ignored="${15}"									\
		_erc_cflags="${16}" _erc_cflags_extra="${17}" _erc_cppflags="${18}"			\
		_erc_cppflags_extra="${19}" _erc_cxxflags="${20}" _erc_cxxflags_extra="${21}"	\
		_erc_ldflags="${22}" _erc_ldflags_extra="${23}" _erc_pkg_config_libdir="${24}"	\
		_erc_rc=0;

	case "${_erc_libtool:-}" in
	none) _erc_libtool=""; ;;
	esac;

	[ "${_erc_cflags_extra:+1}" = 1 ] && _erc_cflags="${_erc_cflags:+${_erc_cflags} }${_erc_cflags_extra}";
	[ "${_erc_cppflags_extra:+1}" = 1 ] && _erc_cppflags="${_erc_cppflags:+${_erc_cppflags} }${_erc_cppflags_extra}";
	[ "${_erc_cxxflags_extra:+1}" = 1 ] && _erc_cxxflags="${_erc_cxxflags:+${_erc_cxxflags} }${_erc_cxxflags_extra}";
	[ "${_erc_ldflags_extra:+1}" = 1 ] && _erc_ldflags="${_erc_ldflags:+${_erc_ldflags} }${_erc_ldflags_extra}";

(
	if [ "${_erc_libtool:+1}" = 1 ]; then
		export MAKE="make LIBTOOL=${_erc_libtool}";
	fi;

	[ "${_erc_ar:+1}" = 1 ] && export AR="${_erc_ar}";
	[ "${_erc_cc:+1}" = 1 ] && export CC="${_erc_cc}";
	[ "${_erc_cxx:+1}" = 1 ] && export CXX="${_erc_cxx}";
	[ "${_erc_ld:+1}" = 1 ] && export LD="${_erc_ld}";
	[ "${_erc_libtool:+1}" = 1 ] && export LIBTOOL="${_erc_libtool}";
	[ "${_erc_pkg_config:+1}" = 1 ] && export PKG_CONFIG="${_erc_pkg_config}";
	[ "${_erc_python:+1}" = 1 ] && export PYTHON="${_erc_python}";
	[ "${_erc_ranlib:+1}" = 1 ] && export RANLIB="${_erc_ranlib}";

	[ "${_erc_cflags:+1}" = 1 ] && export CFLAGS="${_erc_cflags}";
	[ "${_erc_cppflags:+1}" = 1 ] && export CPPFLAGS="${_erc_cppflags}";
	[ "${_erc_cxxflags:+1}" = 1 ] && export CXXFLAGS="${_erc_cxxflags}";
	[ "${_erc_ldflags:+1}" = 1 ] && export LDFLAGS="${_erc_ldflags}";
	[ "${_erc_pkg_config_libdir:+1}" = 1 ] && export PKG_CONFIG_LIBDIR="${_erc_pkg_config_libdir}";

	if [ "${_erc_flags_list:+1}" = 1 ]; then
		rtl_run_cmdlineV ":" "${_erc_configure}"	\
			"${_erc_flags_list}"			\
			"${_erc_flags_extra_list:-}"		\
			;
		exit "${?}";
	elif [ "${_erc_flags_extra_list:+1}" = 1 ]; then
		rtl_run_cmdlineV ":" "${_erc_configure}"	\
			${_erc_flags:-}				\
			"${_erc_flags_extra_list:-}"		\
			;
		exit "${?}";
	else
		rtl_run_cmdlineV ":" "${_erc_configure}"	\
			${_erc_flags:-}				\
			${_erc_flags_extra:-}			\
			;
		exit "${?}";
	fi;
);
	_erc_rc="${?}";

	return "${_erc_rc}";
};

#
# ex_rtl_configure_cmake() - run configure script
# @_ar:				ar(1) command name or pathname
# @_cc:				C compiler command name or pathname
# @_ccache:			ccache(1) command name or pathname or ""
# @_cmake:			CMake command name or pathname
# @_cxx:			C++ compiler command name or pathname
# @_ld:				ld(1) command name or pathname
# @_pkg_config:			pkg-config(1) command name or pathname
# @_python:			python command name or pathname
# @_ranlib:			ranlib(1) command name or pathname
# @--:				(ignored)
# @_build_type:			CMake build type (host, cross, native)
# @_cmake_args:			additional CMake arguments as a whitespace-separated list
# @_cmake_args_extra:		additional CMake extra arguments as a whitespace-separated likst
# @_prefix:			build prefix pathname
# @_subdir:			CMake build directory pathname
# @_system_name:		CMake system name
# @_system_processor:		CMake system processor
# @--:				(ignored)
# @_cflags:			$CFLAGS
# @_cflags_extra:		extra $CFLAGS
# @_cppflags:			$CPPFLAGS
# @_cppflags_extra:		extra $CPPFLAGS
# @_cxxflags:			$CXXFLAGS
# @_cxxflags_extra:		extra $CXXFLAGS
# @_ldflags:			$LDFLAGS
# @_ldflags_extra:		extra $LDFLAGS
# @_pkg_config_libdir:		pkg-config(1) search directory
#
# Returns:			zero (0) on success, non-zero (>0) on failure
#
ex_rtl_configure_cmake() {
	local	_ercc_ar="${1}" _ercc_cc="${2}" _ercc_ccache="${3}" _ercc_cmake="${4}"			\
		_ercc_cxx="${5}" _ercc_ld="${6}" _ercc_pkg_config="${7}" _ercc_python="${8}"		\
		_ercc_ranlib="${9}"									\
		_ercc_ignored="${10}"									\
		_ercc_build_type="${11}"								\
		_ercc_cmake_args="${12}" _ercc_cmake_args_extra="${13}"					\
		_ercc_prefix="${14}" _ercc_subdir="${15}"						\
		_ercc_system_name="${16}" _ercc_system_processor="${17}"				\
		_ercc_ignored="${18}"									\
		_ercc_cflags="${19}" _ercc_cflags_extra="${20}" _ercc_cppflags="${21}"			\
		_ercc_cppflags_extra="${22}" _ercc_cxxflags="${23}" _ercc_cxxflags_extra="${24}"	\
		_ercc_ldflags="${25}" _ercc_ldflags_extra="${26}" _ercc_pkg_config_libdir="${27}"	\
		_ercc_cmd_name="" _ercc_cmake_args_auto="" _ercc_rc=0 _ercc_vname="" _ercc_vval=""

	[ "${_ercc_cflags_extra:+1}" = 1 ] && _ercc_cflags="${_ercc_cflags:+${_ercc_cflags} }${_ercc_cflags_extra}";
	[ "${_ercc_cppflags_extra:+1}" = 1 ] && _ercc_cppflags="${_ercc_cppflags:+${_ercc_cppflags} }${_ercc_cppflags_extra}";
	[ "${_ercc_cxxflags_extra:+1}" = 1 ] && _ercc_cxxflags="${_ercc_cxxflags:+${_ercc_cxxflags} }${_ercc_cxxflags_extra}";
	[ "${_ercc_ldflags_extra:+1}" = 1 ] && _ercc_ldflags="${_ercc_ldflags:+${_ercc_ldflags} }${_ercc_ldflags_extra}";

(
	[ "${_ercc_pkg_config:+1}" = 1 ] && export PKG_CONFIG="${_ercc_pkg_config}";
	[ "${_ercc_pkg_config_libdir:+1}" = 1 ] && export PKG_CONFIG_LIBDIR="${_ercc_pkg_config_libdir}";
	[ "${_ercc_python:+1}" = 1 ] && export PYTHON="${_ercc_python}";

	for _ercc_vname in ar cc cxx ld pkg_config ranlib; do
		case "${_ercc_vname}" in
		cc|cxx)
			_ercc_vname="_ercc_${_ercc_vname}";
			if [ "${_ercc_ccache:+1}" = 1 ]; then
				eval ${_ercc_vname}="\${${_ercc_vname}#${_ercc_ccache} }";
			fi;
			;;

		*)
			_ercc_vname="_ercc_${_ercc_vname}";
			;;
		esac;

		if eval [ '"${'"${_ercc_vname}"':+1}"' = 1 ]\
		&& eval [ '"${'"${_ercc_vname}"'#/}"' = '"${'"${_ercc_vname}"'}"' ]; then
			eval _ercc_cmd_name="\${${_ercc_vname}% *}";
			eval _ercc_vval="\${${_ercc_vname}#* }";
			eval ${_ercc_vname}='$(which "${_ercc_cmd_name}")' || return 1;
		fi;
	done;

	rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_AR=${_ercc_ar}" ":";
	rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_BUILD_TYPE=${_ercc_build_type}" ":";
	rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_C_COMPILER=${_ercc_cc}" ":";
	if [ "${_ercc_ccache:+1}" = 1 ]; then
		rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_C_COMPILER_LAUNCHER=${_ercc_ccache}" ":";
	fi;
	rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_C_FLAGS=${_ercc_cflags}" ":";
	rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_CPP_FLAGS=${_ercc_cppflags}" ":";
	rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_CXX_COMPILER=${_ercc_cxx}" ":";
	if [ "${_ercc_ccache:+1}" = 1 ]; then
		rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_CXX_COMPILER_LAUNCHER=${_ercc_ccache}" ":";
	fi;
	rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_CXX_FLAGS=${_ercc_cxxflags}" ":";
	rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_EXE_LINKER_FLAGS=${_ercc_ldflags}" ":";
	rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_FIND_ROOT_PATH=${_ercc_prefix}" ":";
	rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_INSTALL_PREFIX=" ":";
	rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_LINKER=${_ercc_ld}" ":";
	rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_MODULE_LINKER_FLAGS=${_ercc_ldflags}" ":";
	rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_RANLIB=${_ercc_ranlib}" ":";
	rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_SHARED_LINKER_FLAGS=${_ercc_ldflags}" ":";
	rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_SYSTEM_PROCESSOR=${_ercc_system_processor}" ":";
	rtl_lconcat \$_ercc_cmake_args_auto "-DPKG_CONFIG_EXECUTABLE=${_ercc_pkg_config}" ":";

	case "${_ercc_build_type}" in
	native)
		rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_SYSROOT=${_ercc_prefix}" ":";
		rtl_lconcat \$_ercc_cmake_args_auto "-DCMAKE_SYSTEM_NAME=${_ercc_system_name}" ":";
		;;
	esac;

	rtl_run_cmdlineV ":" "${_ercc_cmake}"	\
		"${_ercc_cmake_args_auto}"	\
		${_ercc_cmake_args:-}		\
		${_ercc_cmake_args_extra:-}	\
		"${_ercc_subdir}";
	exit "${?}";
);
	_ercc_rc="${?}";

	return "${_ercc_rc}";
};

#
# ex_rtl_configure_prepare() - prepare configure script for running
# @_build_type:			build type (host, native)
# @_builddir:			build directory pathname
# @_config_cache:		optional configuration cache directives
# @_config_cache_local:		optional local configuration cache directives
# @_config_cache_extra:		optional extra configuration cache directives
# @_config_guess_fname:		config.guess file pathname
# @_config_sub_global_fname:	global config.sub file pathname
# @_copy_config_guess:		copy config.guess (copy) or keep local config.guess (keep)
# @_force_autoreconf:		1 to force reconfiguration if already configured, 0 otherwise
# @_subdir:			subdirectory pathname
# @_pkg_config:			pkg-config(1) command name or pathname
# @_pkg_config_libdir:		pkg-config(1) search directory
# @_prefix:			prefix pathname
#
# Returns:			zero (0) on success, non-zero (>0) on failure
#
ex_rtl_configure_prepare() {
	local	_ercp_build_type="${1}" _ercp_builddir="${2}" _ercp_config_cache="${3}"		\
		_ercp_config_cache_local="${4}" _ercp_config_cache_extra="${5}"			\
		_ercp_config_guess_fname="${6}" _ercp_config_sub_global_fname="${7}"		\
		_ercp_copy_config_guess=="${8}" _ercp_force_autoreconf="${9}"			\
		_ercp_subdir="${10}" _ercp_pkg_config="${11}" _ercp_pkg_config_libdir="${12}"	\
		_ercp_prefix="${13}";

	if rtl_exists_any "${_ercp_subdir}"	\
			"configure.ac"		\
			"configure.in"		\
			"configure";
	then
		rtl_export_vars							\
			PKG_CONFIG		"${_ercp_pkg_config}"		\
			PKG_CONFIG_LIBDIR	"${_ercp_pkg_config_libdir}";

		if [ "${_ercp_force_autoreconf:-0}" -eq 1 ]\
		&& ! (cd "${_ercp_subdir}" && autoreconf -fiv); then
			return 1;
		fi;

		if [ -e "${_ercp_subdir}/configure.ac" ]\
		&& [ ! -e "${_ercp_subdir}/configure" ]; then
			for _ercp_fname in bootstrap bootstrap.sh autogen.sh; do
				if [ -e "${_ercp_subdir}/${_ercp_fname}" ]; then
					for _ercp_subdir_tgt in 			\
							"${_ercp_subdir}/build-aux"	\
							"${_ercp_subdir}";
					do	if [ -e "${_ercp_subdir_tgt}" ]; then
							if ! rtl_fileop install -m 0700				\
									"${_ercp_config_sub_global_fname}"	\
									"${_ercp_subdir_tgt}/config.sub"; then
								return 1;
							fi;

							if [ "${_ercp_copy_config_guess}" = "copy" ]; then
								if ! rtl_fileop install -m 0700			\
										"${_ercp_config_guess_fname}"	\
										"${_ercp_subdir_tgt}/config.guess"; then
									return 1;
								else
									break;
								fi;
							fi;
						fi;
					done;

					if ! (rtl_fileop cd "${_ercp_subdir}" &&	\
							/bin/sh "${_ercp_fname}"); then
						return 1;
					else
						break;
					fi;
				fi;
			done;

			if ! (rtl_fileop cd "${_ercp_subdir}" && autoconf); then
				return 1;
			fi;
		fi;

		for _ercp_fname in $(find "${_ercp_subdir}" -name config.sub); do
			if ! rtl_fileop mv "${_ercp_fname}" "${_ercp_fname}.orig"\
			|| ! rtl_fileop install -m 0700 "${_ercp_config_sub_global_fname}" "${_ercp_fname}"; then
				return 1;
			fi;
		done;

		if [ "${_ercp_copy_config_guess}" = "copy" ]; then
			for _ercp_fname in $(find "${_ercp_subdir}" -name config.guess); do
				if ! rtl_fileop mv "${_ercp_fname}" "${_ercp_fname}.orig"\
				|| ! rtl_fileop install -m 0700 "${_ercp_config_guess_fname}" "${_ercp_fname}"; then
					return 1;
				fi;
			done;
		fi;

		if [ "${_ercp_build_type}" != host ]; then
			if ! rtl_fileop touch "${_ercp_builddir}/config.cache"; then
				return 1;
			else
				for _ercp_config_cache in 			\
						"${_ercp_config_cache:-}"	\
						"${_ercp_config_cache_local:-}"	\
						"${_ercp_config_cache_extra:-}";
				do
					if [ "${_ercp_config_cache:+1}" = 1 ]\
					&& ! printf "%s\n" "${_ercp_config_cache}" |\
							tr " " "\n" >> "${_ercp_builddir}/config.cache"; then
						return 1;
					fi;
				done;

				if ! sed -i"" "s,%PREFIX%,${_ercp_prefix},g" "${_ercp_builddir}/config.cache"; then
					return 1;
				fi;
			fi;
		fi;

		rtl_export_vars -u						\
			PKG_CONFIG		"${_ercp_pkg_config}"		\
			PKG_CONFIG_LIBDIR	"${_ercp_pkg_config_libdir}";
	fi;

	return 0;
};

# vim:filetype=sh textwidth=0