summaryrefslogtreecommitdiff
path: root/dev-lang/ghc/ghc-9.8.4-r1.ebuild
blob: a0e699f699cf3d90430adbe96e694682e26c1680 (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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
# Copyright 1999-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

# to make make a crosscompiler use crossdev and symlink ghc tree into
# cross overlay. result would look like 'cross-sparc-unknown-linux-gnu/ghc'
export CBUILD=${CBUILD:-${CHOST}}
export CTARGET=${CTARGET:-${CHOST}}
if [[ ${CTARGET} = ${CHOST} ]] ; then
	if [[ ${CATEGORY/cross-} != ${CATEGORY} ]] ; then
		export CTARGET=${CATEGORY/cross-}
	fi
fi

PYTHON_COMPAT=( python3_{13..14} )
LLVM_COMPAT=( {15..20} )

inherit python-any-r1
inherit autotools bash-completion-r1 flag-o-matic ghc-package
inherit toolchain-funcs prefix check-reqs llvm-r2 unpacker haskell-cabal verify-sig

DESCRIPTION="The Glasgow Haskell Compiler"
HOMEPAGE="https://www.haskell.org/ghc/"

VERIFY_SIG_OPENPGP_KEY_PATH=/usr/share/openpgp-keys/ghc.asc

GHC_BRANCH_COMMIT="a3401159f2846605abb517e71af463df47398e72" # ghc-9.8.4-release

GHC_BINARY_PV="9.6.2"
SRC_URI="
	https://downloads.haskell.org/~ghc/${PV}/${P}-src.tar.xz
	verify-sig? ( https://downloads.haskell.org/~ghc/${PV}/${P}-src.tar.xz.sig )
	!ghcbootstrap? (
		https://downloads.haskell.org/~ghc/9.8.2/hadrian-bootstrap-sources/hadrian-bootstrap-sources-${GHC_BINARY_PV}.tar.gz
		amd64? ( https://downloads.haskell.org/~ghc/${GHC_BINARY_PV}/ghc-${GHC_BINARY_PV}-x86_64-alpine3_12-linux-static-int_native.tar.xz )
		arm64? ( elibc_glibc? (
			https://downloads.haskell.org/~ghc/${GHC_BINARY_PV}/ghc-${GHC_BINARY_PV}-aarch64-deb10-linux.tar.xz
		) )
	)
	test? (
		https://gitlab.haskell.org/ghc/ghc/-/archive/${GHC_BRANCH_COMMIT}.tar.gz
			-> ${PN}-${GHC_BRANCH_COMMIT}.tar.gz
	)
"

GHC_BUGGY_TESTS=(
	# Actual stderr output differs from expected:
	# +/usr/libexec/gcc/x86_64-pc-linux-gnu/ld: warning: ManySections.o: missing .note.GNU-stack section implies executable stack
	# +/usr/libexec/gcc/x86_64-pc-linux-gnu/ld: NOTE: This behaviour is deprecated and will be removed in a future version of the linker
	"testsuite/tests/driver/recomp015"
)

yet_binary() {
	case ${ARCH} in
		amd64)
			return 0
			;;
		arm64)
			case "${ELIBC}" in
				glibc)
					return 0
					;;
				*)
					return 1
					;;
			esac
			;;
		*)
			return 1
			;;
	esac
}

# We are using the upstream static Alpine Linux binaries to bootstrap some
# archs. These binaries have different properties than the ones we build
# ourselves, so we need a way to check to see if they are in use.
upstream_binary() {
	case ${ARCH} in
		amd64)
			return 0
			;;
		arm64)
			case "${ELIBC}" in
				glibc)
					return 0
					;;
				*)
					return 1
					;;
			esac
			;;
		*)
			return 1
			;;
	esac
}

# The location of the unpacked Alpine Linux tarball
ghc_bin_path() {
	local ghc_bin_triple
	case ${ARCH} in
		amd64)
			ghc_bin_triple="x86_64-unknown-linux"
			;;
		arm64)
			if use elibc_musl; then
				ghc_bin_triple="aarch64-alpine-linux"
			else
				ghc_bin_triple="aarch64-unknown-linux"
			fi
			;;
		*)
			die "Unknown ghc binary triple. The list here should match yet_binary."
			;;
	esac

	echo "${WORKDIR}/ghc-${GHC_BINARY_PV}-${ghc_bin_triple}"
}

GHC_PV=${PV}
#GHC_PV=8.10.0.20200123 # uncomment only for -alpha, -beta, -rc ebuilds
GHC_P=${PN}-${GHC_PV} # using ${P} is almost never correct

S="${WORKDIR}"/${GHC_P}

BUMP_LIBRARIES=(
	# "hackage-name          hackage-version"
	#Match 9.6.7
	"directory 1.3.9.0"
	"file-io 0.1.5"
)

LICENSE="BSD"
SLOT="0/${PV}"
KEYWORDS="~amd64 ~arm64"
IUSE="big-endian doc elfutils ghcbootstrap ghcmakebinary +gmp llvm numa profile test unregisterised"
RESTRICT="!test? ( test )"

RDEPEND="
	>=dev-lang/perl-5.6.1
	dev-libs/gmp:0=
	dev-libs/libffi:=
	sys-libs/ncurses:=[unicode(+)]
	elfutils? ( dev-libs/elfutils )
	numa? ( sys-process/numactl )
	llvm? (
		$(llvm_gen_dep '
			llvm-core/clang:${LLVM_SLOT}=
			llvm-core/llvm:${LLVM_SLOT}=
		')
	)
"

DEPEND="${RDEPEND}"
BDEPEND="
	virtual/pkgconfig
	doc? (
		app-text/docbook-xml-dtd:4.2
		app-text/docbook-xml-dtd:4.5
		app-text/docbook-xsl-stylesheets
		dev-python/sphinx
		>=dev-libs/libxslt-1.1.2
	)
	ghcbootstrap? (
		ghcmakebinary? ( dev-haskell/hadrian[static] )
		>=dev-haskell/hadrian-9.8.4-r1 <dev-haskell/hadrian-9.8.4-r9999
	)
	test? (
		${PYTHON_DEPS}
		${LLVM_DEPS}
	)
	verify-sig? (
		sec-keys/openpgp-keys-ghc
	)
"

needs_python() {
	# test driver is written in python
	use test && return 0
	return 1
}

# we build binaries without profiling support
REQUIRED_USE="
	?? ( llvm unregisterised )
"

# haskell libraries built with cabal in configure mode, #515354
QA_CONFIGURE_OPTIONS+=" --with-compiler --with-gcc"

is_crosscompile() {
	[[ ${CHOST} != ${CTARGET} ]]
}

is_native() {
	[[ ${CHOST} == ${CBUILD} ]] && [[ ${CHOST} == ${CTARGET} ]]
}

if ! is_crosscompile; then
	PDEPEND="!ghcbootstrap? ( >=app-admin/haskell-updater-1.2 )"
fi

# returns tool prefix for crosscompiler.
# Example:
#  CTARGET=armv7a-unknown-linux-gnueabi
#  CHOST=x86_64-pc-linux-gnu
#    "armv7a-unknown-linux-gnueabi-"
#  CTARGET=${CHOST}
#    ""
# Used in tools and library prefix:
#    "${ED}"/usr/bin/$(cross)haddock
#    "${ED}/usr/$(get_libdir)/$(cross)${GHC_P}/package.conf.d"

cross() {
	if is_crosscompile; then
		echo "${CTARGET}-"
	else
		echo ""
	fi
}

append-ghc-cflags() {
	local persistent compile assemble link
	local flag ghcflag

	for flag in $*; do
		case ${flag} in
			persistent)	persistent="yes";;
			compile)	compile="yes";;
			assemble)	assemble="yes";;
			link)		link="yes";;
			*)
				[[ ${compile}  ]] && ghcflag="-optc${flag}"  CFLAGS+=" ${flag}" && GHC_FLAGS+=" ${ghcflag}" &&
					[[ ${persistent} ]] && GHC_PERSISTENT_FLAGS+=" ${ghcflag}"
				[[ ${assemble} ]] && ghcflag="-opta${flag}"  CFLAGS+=" ${flag}" && GHC_FLAGS+=" ${ghcflag}" &&
					[[ ${persistent} ]] && GHC_PERSISTENT_FLAGS+=" ${ghcflag}"
				[[ ${link}     ]] && ghcflag="-optl${flag}" LDFLAGS+=" ${flag}" && GHC_FLAGS+=" ${ghcflag}" &&
					[[ ${persistent} ]] && GHC_PERSISTENT_FLAGS+=" ${ghcflag}"
				;;
		esac
	done
}

# $1 - directory
# $2 - lib name
# $3 - lib version
# example: bump_lib "libraries" "transformers" "0.4.2.0"
bump_lib() {
	local dir="$1" pn=$2 pv=$3
	local p=${pn}-${pv}
	local f

	einfo "Bumping ${pn} up to ${pv}"

	if [[ -d "${dir}"/"${pn}" ]] # if the library exists already, move it out of the way
	then
		mv "${dir}"/"${pn}" "${WORKDIR}"/"${pn}".old || die
	fi
	mv "${WORKDIR}"/"${p}" "${dir}"/"${pn}" || die
}

update_SRC_URI() {
	local p pn pv
	for p in "${BUMP_LIBRARIES[@]}"; do
		set -- $p
		pn=$1 pv=$2

		SRC_URI+=" https://hackage.haskell.org/package/${pn}-${pv}/${pn}-${pv}.tar.gz"
	done
}

update_SRC_URI

bump_libs() {
	local p pn pv dir
	for p in "${BUMP_LIBRARIES[@]}"; do
		set -- $p
		pn=$1 pv=$2

		if [[ "$pn" == "Cabal-syntax" ]] || [[ "$pn" == "Cabal" ]]; then
			dir="libraries/Cabal"
		else
			dir="libraries"
		fi

		bump_lib "${dir}" "${pn}" "${pv}"
	done
}

ghc_setup_toolchain() {
	tc-export CC CXX LD AR RANLIB
	einfo "ghc_setup_toolchain: CC=${CC} CXX=${CXX} LD=${LD} AR=${AR} RANLIB=${RANLIB}"
}

ghc_setup_cflags() {
	# TODO: plumb CFLAGS and BUILD_CFLAGS to respective CONF_CC_OPTS_STAGE<N>
	if ! is_native; then
		export CFLAGS=${GHC_CFLAGS-"-O2 -pipe"}
		export LDFLAGS=${GHC_LDFLAGS-"-Wl,-O1"}
		einfo "Crosscompiling mode:"
		einfo "   CHOST:   ${CHOST}"
		einfo "   CTARGET: ${CTARGET}"
		einfo "   CFLAGS:  ${CFLAGS}"
		einfo "   LDFLAGS: ${LDFLAGS}"
		einfo "   prefix: $(cross)"
		return
	fi
	# We need to be very careful with the CFLAGS we ask ghc to pass through to
	# gcc. There are plenty of flags which will make gcc produce output that
	# breaks ghc in various ways. The main ones we want to pass through are
	# -mcpu / -march flags. These are important for arches like alpha & sparc.
	# We also use these CFLAGS for building the C parts of ghc, ie the rts.
	strip-flags
	strip-unsupported-flags

	# Cmm can't parse line numbers #482086
	replace-flags -ggdb[3-9] -ggdb2

	GHC_FLAGS=""
	GHC_PERSISTENT_FLAGS=""
	for flag in ${CFLAGS}; do
		case ${flag} in

			# Ignore extra optimisation (ghc passes -O to gcc anyway)
			# -O2 and above break on too many systems
			-O*) ;;

			# Arch and ABI flags are what we're really after
			-m*) append-ghc-cflags compile assemble ${flag};;

			# Sometimes it's handy to see backtrace of RTS
			# to get an idea what happens there
			-g*) append-ghc-cflags compile ${flag};;

			# Ignore all other flags, including all -f* flags
		esac
	done

	for flag in ${LDFLAGS}; do
		append-ghc-cflags link ${flag}
	done
}

# substitutes string $1 to $2 in files $3 $4 ...
relocate_path() {
	local from=$1
	local   to=$2
	shift 2
	local file=
	for file in "$@"
	do
		sed -i -e "s|$from|$to|g" \
			"$file" || die "path relocation failed for '$file'"
	done
}

# changes hardcoded ghc paths and updates package index
# $1 - new absolute root path
relocate_ghc() {
	local to=$1 ghc_v=${BIN_PV}

	# libdir for prebuilt binary and for current system may mismatch
	# It does for prefix installation for example: bug #476998
	local bin_ghc_prefix=${WORKDIR}/usr
	local bin_libpath=$(echo "${bin_ghc_prefix}"/lib*)
	local bin_libdir=${bin_libpath#${bin_ghc_prefix}/}

	# backup original script to use it later after relocation
	local gp_back="${T}/ghc-pkg-${ghc_v}-orig"
	cp "${WORKDIR}/usr/bin/ghc-pkg-${ghc_v}" "$gp_back" || die "unable to backup ghc-pkg wrapper"

	if [[ ${bin_libdir} != $(get_libdir) ]]; then
		einfo "Relocating '${bin_libdir}' to '$(get_libdir)' (bug #476998)"
		# moving the dir itself is not strictly needed
		# but then USE=binary would result in installing
		# in '${bin_libdir}'
		mv "${bin_ghc_prefix}/${bin_libdir}" "${bin_ghc_prefix}/$(get_libdir)" || die

		relocate_path "/usr/${bin_libdir}" "/usr/$(get_libdir)" \
			"${WORKDIR}/usr/bin/ghc-${ghc_v}" \
			"${WORKDIR}/usr/bin/ghci-${ghc_v}" \
			"${WORKDIR}/usr/bin/ghc-pkg-${ghc_v}" \
			"${WORKDIR}/usr/bin/hsc2hs" \
			"${WORKDIR}/usr/bin/runghc-${ghc_v}" \
			"$gp_back" \
			"${WORKDIR}/usr/$(get_libdir)/${PN}-${ghc_v}/lib/package.conf.d/"*
	fi

	# Relocate from /usr to ${EPREFIX}/usr
	relocate_path "/usr" "${to}/usr" \
		"${WORKDIR}/usr/bin/ghc-${ghc_v}" \
		"${WORKDIR}/usr/bin/ghci-${ghc_v}" \
		"${WORKDIR}/usr/bin/ghc-pkg-${ghc_v}" \
		"${WORKDIR}/usr/bin/haddock-ghc-${ghc_v}" \
		"${WORKDIR}/usr/bin/hp2ps" \
		"${WORKDIR}/usr/bin/hpc" \
		"${WORKDIR}/usr/bin/hsc2hs" \
		"${WORKDIR}/usr/bin/runghc-${ghc_v}" \
		"${WORKDIR}/usr/$(get_libdir)/${PN}-${ghc_v}/lib/package.conf.d/"*

	# this one we will use to regenerate cache
	# so it should point to current tree location
	relocate_path "/usr" "${WORKDIR}/usr" "$gp_back"

	if use prefix; then
		hprefixify "${bin_libpath}"/${PN}*/settings
	fi

	# regenerate the binary package cache
	"$gp_back" recache || die "failed to update cache after relocation"
	rm "$gp_back"
}

ghc-check-reqs() {
	# These are pessimistic values (slightly bigger than worst-case)
	# Worst case is UNREG USE=profile ia64. See bug #611866 for some
	# numbers on various arches.
	CHECKREQS_DISK_BUILD=20G
	CHECKREQS_DISK_USR=4G

	"$@"
}

ghc-check-bootstrap-version () {
	local diemsg version
	ebegin "Checking for appropriate installed GHC version for bootstrapping"
	if version=$(ghc-version); then
		if ver_test "${version}" -gt "9.0.0"; then
			eend 0
			return 0
		else
			diemsg="Inappropriate GHC version for bootstrapping: ${version}"
		fi
	else
		diemsg="Could not find installed GHC for bootstrapping"
	fi

	eend 1
	eerror "USE=ghcbootstrap _requires_ an existing GHC already installed on the system."
	eerror "Furthermore, the hadrian build system requires that the existing ghc be"
	eerror "version 9.0 or higher."
	die "$diemsg"
}

ghc-check-bootstrap-mismatch () {
	local diemsg ghc_version cabal_version
	ebegin "Checking for mismatched GHC and Cabal versions for bootstrapping"
	if ver_test "$(ghc-version)" -lt "9.4" && ver_test "$(cabal-version)" -gt "3.8"; then
		eend 1
		eerror "There have been issues bootstrapping ghc-9.4 with <ghc-9.4 and >Cabal-3.8"
		eerror "Please install dev-haskell/cabal-3.6.* instead first."
		die "Mismatched GHC and Cabal versions for bootstrapping"
	else
		eend 0
	fi
}

# TODO: Break out into hadrian.eclass
# Uses $_hadrian_args, if set
run_hadrian() {
	if use ghcbootstrap; then
		local cmd=("${BROOT}/usr/bin/hadrian")
	else
		local cmd=("${S}/hadrian/bootstrap/_build/bin/hadrian")
	fi

	cmd+=( "${_hadrian_args[@]}" "$@" )

	einfo "Running: ${cmd[@]}"
	"${cmd[@]}" || die
}

pkg_pretend() {
	if [[ ${MERGE_TYPE} != binary ]] && use ghcbootstrap; then
		ghc-check-bootstrap-version
		ghc-check-bootstrap-mismatch
	fi
	ghc-check-reqs check-reqs_pkg_pretend
}

pkg_setup() {
	ghc-check-reqs check-reqs_pkg_setup

	[[ ${MERGE_TYPE} == binary ]] && return

	if use ghcbootstrap; then
		ewarn "You requested ghc bootstrapping, this is usually only used"
		ewarn "by Gentoo developers to make binary .tbz2 packages."

		[[ -z $(type -P ghc) ]] && \
			die "Could not find a ghc to bootstrap with."
	else
		if ! yet_binary; then
			eerror "Please try emerging with USE=ghcbootstrap and report build"
			eerror "success or failure to the haskell team (haskell@gentoo.org)"
			die "No binary available for '${ARCH}' arch yet, USE=ghcbootstrap"
		fi
	fi

	if needs_python; then
		python-any-r1_pkg_setup
	fi

	use llvm && llvm-r2_pkg_setup
}

src_unpack() {
	# the Solaris and Darwin binaries from ghc (maeder) need to be
	# unpacked separately, so prevent them from being unpacked
	local ONLYA=${A}
	case ${CHOST} in
		*-darwin* | *-solaris*)  ONLYA=${GHC_P}-src.tar.xz  ;;
	esac
	if use verify-sig; then
		verify-sig_verify_detached "${DISTDIR}"/${P}-src.tar.xz{,.sig}
	fi
	# Strip signature files from the list of files to unpack
	for f in ${ONLYA}; do
		if [[ ${f} != *.sig ]]; then
			nosig="${nosig} ${f}"
		fi
	done
	unpacker ${nosig}
}

src_prepare() {
	# Force the use of C.utf8 locale
	# <https://github.com/gentoo-haskell/gentoo-haskell/issues/1287>
	# <https://github.com/gentoo-haskell/gentoo-haskell/issues/1289>
	export LC_ALL=C.utf8

	ghc_setup_toolchain
	ghc_setup_cflags

	if ! use ghcbootstrap && ! upstream_binary; then
		# Make GHC's settings file comply with user's settings
		GHC_SETTINGS="$(ghc_bin_path)/lib/settings"

		sed -i "s/,(\"C compiler command\", \".*\")/,(\"C compiler command\", \"${CC}\")/" "${GHC_SETTINGS}" || die
		sed -i "s/,(\"C++ compiler command\", \".*\")/,(\"C++ compiler command\", \"${CXX}\")/" "${GHC_SETTINGS}" || die
		sed -i "s/,(\"Haskell CPP command\", \".*\")/,(\"Haskell CPP command\", \"${CC}\")/" "${GHC_SETTINGS}" || die
		sed -i "s/,(\"ld command\", \".*\")/,(\"ld command\", \"${LD}\")/" "${GHC_SETTINGS}" || die
		sed -i "s/,(\"Merge objects command\", \".*\")/,(\"Merge objects command\", \"${LD}\")/" "${GHC_SETTINGS}" || die
		sed -i "s/,(\"ar command\", \".*\")/,(\"ar command\", \"${AR}\")/" "${GHC_SETTINGS}" || die
		sed -i "s/,(\"ranlib command\", \".*\")/,(\"ranlib command\", \"${RANLIB}\")/" "${GHC_SETTINGS}" || die
	fi

	# binpkg may have been built with FEATURES=splitdebug
	if [[ -d "${WORKDIR}/usr/lib/debug" ]] ; then
		rm -rf "${WORKDIR}/usr/lib/debug" || die
	fi
	find "${WORKDIR}/usr/lib" -type d -empty -delete 2>/dev/null # do not die on failure here

	# ffi headers don't get included in the binpkg for some reason
	for f in "${WORKDIR}/usr/$(get_libdir)/${PN}-${BIN_PV}/include/"{ffi.h,ffitarget.h}
	do
		mkdir -p "$(dirname "${f}")"
		[[ -e "${f}" ]] || ln -sf "$($(tc-getPKG_CONFIG) --cflags-only-I libffi | sed "s/-I//g" | tr -d " ")/$(basename "${f}")" "${f}" || die
	done

	if ! use ghcbootstrap && ! upstream_binary; then
		relocate_ghc "${WORKDIR}"
	fi

	sed -i -e "s|\"\$topdir\"|\"\$topdir\" ${GHC_PERSISTENT_FLAGS}|" \
		"${S}/ghc/ghc.wrapper"

	# Incorrectly assumes modern Alex uses -v as an alias for --version
	sed -i -e 's/\(fptools_cv_alex_version=`"$AlexCmd" \)-v/\1--version/' \
		"${S}/m4/fptools_alex.m4" || die

	# Release tarball does not contain needed test data
	if use test; then
		cp -a "${WORKDIR}/${PN}-${GHC_BRANCH_COMMIT}/testsuite" "${S}" || die

		[[ ${#GHC_BUGGY_TESTS[@]} -gt 0 ]] && einfo "Tests have been marked as buggy and will be deleted:"

		local t

		for t in "${GHC_BUGGY_TESTS[@]}"; do
			einfo "     * ${t}"
		done

		for t in "${GHC_BUGGY_TESTS[@]}"; do
			rm -r "${S}/${t}" || die
		done
	fi

	cd "${S}" # otherwise eapply will break

	# GHCi must resolve linker scripts using the C compiler's sysroot.
	use prefix && eapply "${FILESDIR}/ghc-9.8.4-prefix-ld-linker-script.patch"

	# https://bugs.gentoo.org/978912
	eapply "${FILESDIR}/ghc-9.8.4-fix-terminfo-build.patch"

	# cdddeb0f is already in 9.8.4; synchronize the bindist configure (c9731d6d).
	eapply "${FILESDIR}/${PN}-9.8.4-bindist-compiler-flags.patch"

	eapply "${FILESDIR}"/${PN}-8.10.1-allow-cross-bootstrap.patch

	# https://gitlab.haskell.org/ghc/ghc/-/issues/22954
	# https://gitlab.haskell.org/ghc/ghc/-/issues/21936
	eapply "${FILESDIR}"/${PN}-9.6.4-llvm-20.patch

	# Fix issue caused by non-standard "musleabi" target in
	# https://gitlab.haskell.org/ghc/ghc/-/blob/ghc-9.4.5-release/m4/ghc_llvm_target.m4#L39
	eapply "${FILESDIR}"/${PN}-9.4.5-musl-target.patch

	# Fix QA Notice: Found the following implicit function declarations in configure logs
	eapply "${FILESDIR}/${PN}-9.10.1-fix-configure-implicit-function.patch"

	pushd "${S}/hadrian" || die
		# Fix QA Notice: Unrecognized configure options: --with-cc
		eapply "${FILESDIR}/hadrian-9.4.8-remove-with-cc-configure-flag.patch"
		# Fix QA Notice: One or more compressed files were found in docompress-ed directories
		eapply "${FILESDIR}/hadrian-9.4.8-disable-doc-archives.patch"
		# Add support for file-io
		eapply "${FILESDIR}/hadrian-9.8.4-add-packages.patch"
	popd

	# mingw32 target
	pushd "${S}/libraries/Win32"
		eapply "${FILESDIR}"/${PN}-8.2.1_rc1-win32-cross-2-hack.patch # bad workaround
	popd

	# Only applies to the testsuite directory copied from the git snapshot
	if use test; then
		eapply "${FILESDIR}/${PN}-9.8.2-fix-ipe-test.patch"
		eapply "${FILESDIR}/${PN}-9.8.2-fix-buggy-tests.patch"
	fi

	# <https://github.com/gentoo-haskell/gentoo-haskell/issues/1579>
	eapply "${FILESDIR}/${PN}-9.8.4-add-missing-rts-include.patch"

	# <https://github.com/gentoo-haskell/gentoo-haskell/issues/1775>
	# <https://gitlab.haskell.org/ghc/ghc/-/issues/25662>
	eapply "${FILESDIR}/${PN}-9.12.2-hp2ps-c23-compat.patch"

	# <https://gitlab.haskell.org/ghc/ghc/-/issues/26810>
	eapply "${FILESDIR}/${PN}-9.8.4-fix-sphinx-9.patch"

	bump_libs

	eapply_user
	# as we have changed the build system
	eautoreconf
}

src_configure() {
	### Gather the arguments we will pass to Hadrian:

	# Create an array of arguments for hadrian, which will be used internally by
	# run_hadrian() (so don't set this as local)
	_hadrian_args=()

	# Could be added/tested at some later point
	_hadrian_args+=("--docs=no-sphinx-pdfs")

	# Any non-native build has to skip as it needs
	# target haddock binary to be runnable.
	if ! use doc || ! is_native; then
		_hadrian_args+=(
			"--docs=no-sphinx-html"
			# this controls presence on 'xhtml' and 'haddock' in final install
			# !is_native: disable docs generation as it requires running stage2
			"--docs=no-haddocks"
		)
	fi

	use doc || _hadrian_args+=( "--docs=no-sphinx-man" )

	###
	# TODO: Move these env vars to a hadrian eclass, for better
	# documentation and clarity
	###

	# Control the build flavour
	local hadrian_flavour="default" f
	use profile || hadrian_flavour+="+no_profiled_libs"
	use llvm && hadrian_flavour+="+llvm"

	# Enable production of native debugging information (via GHC/GCC's `-g3`)
	# during stage1+ compilations. (Toggled via detecting -g* in CFLAGS)
	for f in $CFLAGS; do
		case $f in
			-g*) hadrian_flavour+="+debug_info"; break ;;
		esac
	done

	: ${HADRIAN_FLAVOUR:="${hadrian_flavour}"}

	_hadrian_args+=("--flavour=${HADRIAN_FLAVOUR}")

	# Control the verbosity of hadrian. Default is one level of --verbose
	: ${HADRIAN_VERBOSITY:=1}

	local n="${HADRIAN_VERBOSITY}"
	until [[ $n -le 0 ]]; do
		_hadrian_args+=("--verbose")
		n=$(($n - 1 ))
	done

	# Add any -j* flags passed in via $MAKEOPTS
	for i in $MAKEOPTS; do
		case $i in
			-j*) _hadrian_args+=("$i") ;;
			*) true ;;
		esac
	done

	### Prepare hadrian build settings files

	mkdir _build
	touch _build/hadrian.settings

	# We also need to use the GHC_FLAGS flags when building ghc itself
	echo "*.*.ghc.hs.opts += ${GHC_FLAGS}" >> _build/hadrian.settings
	echo "*.*.ghc.c.opts += ${GHC_FLAGS}" >> _build/hadrian.settings

	# Don't let it pre-strip the stage 1 bootstrapping libraries (which will be
	# installed to the system)
	echo "stage1.*.cabal.configure.opts += --disable-library-stripping" >> _build/hadrian.settings

	### Gather configuration variables for GHC

	# Get ghc from the binary
	# except when bootstrapping we just pick ghc up off the path
	if ! use ghcbootstrap; then
		export PATH="${WORKDIR}/ghc-bin/$(get_libdir)/ghc-${GHC_BINARY_PV}/bin:${PATH}"
	fi

	local econf_args=()

	# GHC embeds toolchain it was built by and uses it later.
	# Don't allow things like ccache or versioned binary slip.
	# We use stable thing across gcc upgrades.
	# User can use EXTRA_ECONF=CC=... to override this default.
	econf_args+=(
		# these should be inferred by GHC but ghc defaults
		# to using bundled tools on windows.
		Windres=${CTARGET}-windres
		DllWrap=${CTARGET}-dllwrap
		# we set the linker explicitly below
		--disable-ld-override

		# Put docs into the right place, ie /usr/share/doc/ghc-${GHC_PV}
		--docdir="${EPREFIX}/usr/share/doc/$(cross)${PF}"
	)

	if [[ ${CBUILD} != ${CHOST} ]]; then
		# GHC bug: ghc claims not to support cross-building.
		# It does, but does not distinct --host= value
		# for stage1 and stage2 compiler.
		econf_args+=(--host=${CBUILD})
	fi

	if use ghcmakebinary; then
		# When building booting libary we are trying to
		# bundle or restrict most of external depends
		# with unstable ABI:
		#  - embed libffi (default GHC behaviour)
		#  - disable ncurses support for ghci (via haskeline)
		#    https://bugs.gentoo.org/557478
		#  - disable ncurses support for ghc-pkg
		echo "*.haskeline.cabal.configure.opts += --flag=-terminfo" >> _build/hadrian.settings
		echo "*.ghc-pkg.cabal.configure.opts += --flag=-terminfo" >> _build/hadrian.settings
	fi

	# User-supplied block to be added to hadrian.settings
	echo "${HADRIAN_SETTINGS_EXTRA}" >> _build/hadrian.settings

	einfo "Final _build/hadrian.settings:"
	cat _build/hadrian.settings || die

	### Bootstrap Hadrian, then final configure (should this be here or in src_compile?)

	if ! use ghcbootstrap; then
		einfo "Installing bootstrap GHC"

		( cd "$(ghc_bin_path)" || die
			# 9.6.2 calls this undefined m4 macro in its bindist configure.
			# Autoconf leaves the call in the generated script as a shell command.
			sed -i '/^[[:space:]]*FP_PROG_LD_BUILD_ID[[:space:]]*$/d' configure || die
			econf "${econf_args[@]}" \
				--prefix="" \
				--libdir="/$(get_libdir)" || die
			emake DESTDIR="${WORKDIR}/ghc-bin" install
		)

		einfo "Bootstrapping hadrian"
		( cd "${S}/hadrian/bootstrap" || die
			./bootstrap.py \
				-w "${WORKDIR}/ghc-bin/$(get_libdir)/ghc-${GHC_BINARY_PV}/bin/ghc" \
				-s "${DISTDIR}/hadrian-bootstrap-sources-${GHC_BINARY_PV}.tar.gz" || die "Hadrian bootstrap failed"
		)
	fi

#		--enable-bootstrap-with-devel-snapshot \
	# The bootstrap bindist configure does not accept these source-tree options.
	econf "${econf_args[@]}" \
		--with-system-libffi \
		--with-ffi-includes="$($(tc-getPKG_CONFIG) --cflags-only-I libffi | sed 's/-I//g')" \
		$(use_enable elfutils dwarf-unwind) \
		$(use_enable numa) \
		$(use_enable unregisterised)

	if [[ ${PV} == *9999* ]]; then
		GHC_PV="$(grep 'S\[\"PACKAGE_VERSION\"\]' config.status | sed -e 's@^.*=\"\(.*\)\"@\1@')"
		GHC_P=${PN}-${GHC_PV}
	fi
}

src_compile() {

	run_hadrian binary-dist-dir

	# The generated bindist must be configured before the install phase.
	pushd "${S}/_build/bindist/${P}-${CHOST}" || die
	econf
	popd || die

	# FIXME: This is failing, but the docs mention it:
	# <https://gitlab.haskell.org/hololeap/ghc/-/blob/master/hadrian/doc/testsuite.md?ref_type=heads#building-just-the-dependencies-needed-for-the-testsuite>
	# >    Error, file does not exist and no rule available:
	# >    test:all_deps
	#
	#use test && run_hadrian test:all_deps
}

src_test() {

	# Make sure stage 1 libraries are available
	for d in "${S}/work/${P}/_build/stage1/lib"/*${P}/*/; do
		export LD_LIBRARY_PATH="${d}${LD_LIBRARY_PATH:+:}${LD_LIBRARY_PATH}"
	done

	local args=(
		--progress-info=unicorn # good luck unicorns
	)

	run_hadrian "${args[@]}" test
}

src_install() {
	local package_confdir="${ED}/usr/$(get_libdir)/$(cross)${GHC_P}/lib/package.conf.d"

	[[ -f VERSION ]] || emake VERSION

	pushd "${S}/_build/bindist/${P}-${CHOST}" || die
	emake DESTDIR="${D}" install
	popd

	# Skip for cross-targets as they all share target location:
	# /usr/share/doc/ghc-9999/
	if ! is_crosscompile; then
		dodoc "distrib/README" "LICENSE" "VERSION"
	fi

	# rename ghc-shipped files to avoid collision
	# of external packages. Motivating example:
	#  user had installed:
	#      dev-lang/ghc-7.8.4-r0 (with transformers-0.3.0.0)
	#      dev-haskell/transformers-0.4.2.0
	#  then user tried to update to
	#      dev-lang/ghc-7.8.4-r1 (with transformers-0.4.2.0)
	#  this will lead to single .conf file collision.
	local shipped_conf renamed_conf
	for shipped_conf in "${package_confdir}"/*.conf; do
		# rename 'pkg-ver-id.conf' to 'pkg-ver-id-gentoo-${PF}.conf'
		renamed_conf=${shipped_conf%.conf}-gentoo-${PF}.conf
		mv "${shipped_conf}" "${renamed_conf}" || die
	done

	# remove link, but leave 'haddock-${GHC_P}'
	rm -f "${ED}"/usr/bin/$(cross)haddock

	if ! is_crosscompile; then
		newbashcomp "${FILESDIR}"/ghc-bash-completion ghc-pkg
		newbashcomp utils/completion/ghc.bash         ghc
	fi

	# path to the package.cache
	PKGCACHE="${package_confdir}"/package.cache
	# copy the package.conf.d, including timestamp, save it so we can help
	# users that have a broken package.conf.d
	cp -pR "${package_confdir}"{,.initial} || die "failed to backup initial package.conf.d"

	# copy the package.conf, including timestamp, save it so we later can put it
	# back before uninstalling, or when upgrading.
	cp -p "${PKGCACHE}"{,.shipped} \
		|| die "failed to copy package.conf.d/package.cache"

	if is_crosscompile; then
		# When we build a cross-compiler the layout is the following:
		#     usr/lib/${CTARGET}-ghc-${VER}/ contains target libraries
		# but
		#     usr/lib/${CTARGET}-ghc-${VER}/bin/ directory
		# containst host binaries (modulo bugs).

		# Portage's stripping mechanism does not skip stripping
		# foreign binaries. This frequently causes binaries to be
		# broken.
		#
		# Thus below we disable stripping of target libraries and allow
		# stripping hosts executables.
		dostrip -x "/usr/$(get_libdir)/$(cross)${GHC_P}"
		dostrip    "/usr/$(get_libdir)/$(cross)${GHC_P}/bin"
	fi
}

pkg_preinst() {
	# have we got an earlier version of ghc installed?
	if has_version "<${CATEGORY}/${PF}"; then
		haskell_updater_warn="1"
	fi
}

pkg_postinst() {
	ghc-reregister

	# path to the package.cache
	PKGCACHE="${EROOT}/usr/$(get_libdir)/$(cross)${GHC_P}/lib/package.conf.d/package.cache"

	# give the cache a new timestamp, it must be as recent as
	# the package.conf.d directory.
	touch "${PKGCACHE}"

	if [[ "${haskell_updater_warn}" == "1" ]]; then
		ewarn
		ewarn "\e[1;31m************************************************************************\e[0m"
		ewarn
		ewarn "You have just upgraded from an older version of GHC."
		ewarn "You may have to run"
		ewarn "      'haskell-updater'"
		ewarn "to rebuild all ghc-based Haskell libraries."
		ewarn
		ewarn "\e[1;31m************************************************************************\e[0m"
		ewarn
	fi
}

pkg_prerm() {
	PKGCACHE="${EROOT}/usr/$(get_libdir)/$(cross)${GHC_P}/lib/package.conf.d/package.cache"
	rm -rf "${PKGCACHE}"

	cp -p "${PKGCACHE}"{.shipped,}
}

pkg_postrm() {
	ghc-package_pkg_postrm
}