Gentoo Archives: gentoo-commits

From: Sam James <sam@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] repo/gentoo:master commit in: dev-lang/ocaml/files/, dev-lang/ocaml/
Date: Tue, 22 Feb 2022 00:41:57
Message-Id: 1645490474.860e2f272f0a0a2fecb2ab6f160ce68875770f59.sam@gentoo
1 commit: 860e2f272f0a0a2fecb2ab6f160ce68875770f59
2 Author: Sam James <sam <AT> gentoo <DOT> org>
3 AuthorDate: Tue Feb 22 00:40:45 2022 +0000
4 Commit: Sam James <sam <AT> gentoo <DOT> org>
5 CommitDate: Tue Feb 22 00:41:14 2022 +0000
6 URL: https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=860e2f27
7
8 dev-lang/ocaml: fix 4.05 with glibc 2.34
9
10 Bug: https://bugs.gentoo.org/804498
11 See: https://github.com/gentoo/gentoo/pull/22851#pullrequestreview-882504245
12 Signed-off-by: Sam James <sam <AT> gentoo.org>
13
14 dev-lang/ocaml/files/ocaml-4.05.0-glibc-2.34.patch | 201 +++++++++++++++++++++
15 dev-lang/ocaml/ocaml-4.05.0-r9.ebuild | 157 ++++++++++++++++
16 2 files changed, 358 insertions(+)
17
18 diff --git a/dev-lang/ocaml/files/ocaml-4.05.0-glibc-2.34.patch b/dev-lang/ocaml/files/ocaml-4.05.0-glibc-2.34.patch
19 new file mode 100644
20 index 000000000000..28d7f48f5b27
21 --- /dev/null
22 +++ b/dev-lang/ocaml/files/ocaml-4.05.0-glibc-2.34.patch
23 @@ -0,0 +1,201 @@
24 +https://github.com/ocaml/ocaml/commit/50c2d1275e537906ea144bd557fde31e0bf16e5f
25 +https://bugs.gentoo.org/804498
26 +
27 +From 50c2d1275e537906ea144bd557fde31e0bf16e5f Mon Sep 17 00:00:00 2001
28 +From: Xavier Leroy <xavierleroy@××××××××××××××××××××.com>
29 +Date: Fri, 5 Mar 2021 19:14:07 +0100
30 +Subject: [PATCH] Dynamically allocate the alternate signal stack
31 +
32 +In Glibc 2.34 and later, SIGSTKSZ may not be a compile-time constant.
33 +It is no longer possible to statically allocate the alternate signal
34 +stack for the main thread, as we've been doing for the last 25 years.
35 +
36 +This commit implements dynamic allocation of the alternate signal stack
37 +even for the main thread. It reuses the code already in place to allocate
38 +the alternate signal stack for other threads.
39 +
40 +The alternate signal stack is freed when the main OCaml code / an OCaml thread
41 +stops.
42 +
43 +(partial back-port of PR#10266 and PR#10726)
44 +---
45 + asmrun/fail.c | 7 ++++-
46 + asmrun/signals_asm.c | 69 ++++++++++++++++++++++++++++++++++++++------
47 + asmrun/startup.c | 7 ++++-
48 + byterun/sys.c | 5 ++++
49 + 4 files changed, 77 insertions(+), 11 deletions(-)
50 +
51 +diff --git a/asmrun/fail.c b/asmrun/fail.c
52 +index d73cb88524c..2f064320185 100644
53 +--- a/asmrun/fail.c
54 ++++ b/asmrun/fail.c
55 +@@ -31,6 +31,8 @@
56 + #include "caml/roots.h"
57 + #include "caml/callback.h"
58 +
59 ++extern void caml_terminate_signals(void);
60 ++
61 + /* The globals holding predefined exceptions */
62 +
63 + typedef value caml_generated_constant[1];
64 +@@ -60,7 +62,10 @@ char * caml_exception_pointer = NULL;
65 + void caml_raise(value v)
66 + {
67 + Unlock_exn();
68 +- if (caml_exception_pointer == NULL) caml_fatal_uncaught_exception(v);
69 ++ if (caml_exception_pointer == NULL) {
70 ++ caml_terminate_signals();
71 ++ caml_fatal_uncaught_exception(v);
72 ++ }
73 +
74 + #ifndef Stack_grows_upwards
75 + #define PUSHED_AFTER <
76 +diff --git a/asmrun/signals_asm.c b/asmrun/signals_asm.c
77 +index f124a076749..b4e2516ae1a 100644
78 +--- a/asmrun/signals_asm.c
79 ++++ b/asmrun/signals_asm.c
80 +@@ -194,7 +194,6 @@ DECLARE_SIGNAL_HANDLER(trap_handler)
81 + #ifdef HAS_STACK_OVERFLOW_DETECTION
82 +
83 + static char * system_stack_top;
84 +-static char sig_alt_stack[SIGSTKSZ];
85 +
86 + #if defined(SYS_linux)
87 + /* PR#4746: recent Linux kernels with support for stack randomization
88 +@@ -295,17 +294,69 @@ void caml_init_signals(void)
89 + {
90 + stack_t stk;
91 + struct sigaction act;
92 +- stk.ss_sp = sig_alt_stack;
93 +- stk.ss_size = SIGSTKSZ;
94 +- stk.ss_flags = 0;
95 +- SET_SIGACT(act, segv_handler);
96 +- act.sa_flags |= SA_ONSTACK | SA_NODEFER;
97 +- sigemptyset(&act.sa_mask);
98 +- system_stack_top = (char *) &act;
99 +- if (sigaltstack(&stk, NULL) == 0) { sigaction(SIGSEGV, &act, NULL); }
100 ++ /* Allocate and select an alternate stack for handling signals,
101 ++ especially SIGSEGV signals.
102 ++ The alternate stack used to be statically-allocated for the main thread,
103 ++ but this is incompatible with Glibc 2.34 and newer, where SIGSTKSZ
104 ++ may not be a compile-time constant. */
105 ++ stk.ss_sp = malloc(SIGSTKSZ);
106 ++ if (stk.ss_sp != NULL) {
107 ++ stk.ss_size = SIGSTKSZ;
108 ++ stk.ss_flags = 0;
109 ++ SET_SIGACT(act, segv_handler);
110 ++ act.sa_flags |= SA_ONSTACK | SA_NODEFER;
111 ++ sigemptyset(&act.sa_mask);
112 ++ system_stack_top = (char *) &act;
113 ++ if (sigaltstack(&stk, NULL) == 0)
114 ++ sigaction(SIGSEGV, &act, NULL);
115 ++ else
116 ++ free(stk.ss_sp);
117 ++ }
118 + }
119 + #endif
120 + #if defined(_WIN32) && !defined(_WIN64)
121 + caml_win32_overflow_detection();
122 + #endif
123 + }
124 ++
125 ++/* Termination of signal stuff */
126 ++
127 ++#if defined(TARGET_power) || defined(TARGET_s390x) \
128 ++ || defined(TARGET_sparc) && defined(SYS_solaris) \
129 ++ || defined(HAS_STACK_OVERFLOW_DETECTION)
130 ++static void set_signal_default(int signum)
131 ++{
132 ++ struct sigaction act;
133 ++ sigemptyset(&act.sa_mask);
134 ++ act.sa_handler = SIG_DFL;
135 ++ act.sa_flags = 0;
136 ++ sigaction(signum, &act, NULL);
137 ++}
138 ++#endif
139 ++
140 ++void caml_terminate_signals(void)
141 ++{
142 ++#if defined(TARGET_sparc) && defined(SYS_solaris)
143 ++ set_signal_default(SIGILL);
144 ++#endif
145 ++
146 ++#if defined(TARGET_power)
147 ++ set_signal_default(SIGTRAP);
148 ++#endif
149 ++
150 ++#if defined(TARGET_s390x)
151 ++ set_signal_default(SIGFPE);
152 ++#endif
153 ++
154 ++#ifdef HAS_STACK_OVERFLOW_DETECTION
155 ++ set_signal_default(SIGSEGV);
156 ++ stack_t oldstk, stk;
157 ++ stk.ss_flags = SS_DISABLE;
158 ++ if (sigaltstack(&stk, &oldstk) == 0) {
159 ++ /* If caml_init_signals failed, we are not using an alternate signal stack.
160 ++ SS_DISABLE will be set in oldstk, and there is nothing to free in this
161 ++ case. */
162 ++ if (! (oldstk.ss_flags & SS_DISABLE)) free(oldstk.ss_sp);
163 ++ }
164 ++#endif
165 ++}
166 +diff --git a/asmrun/startup.c b/asmrun/startup.c
167 +index 70bbc4369dc..a1cb06a7d1e 100644
168 +--- a/asmrun/startup.c
169 ++++ b/asmrun/startup.c
170 +@@ -92,6 +92,7 @@ void (*caml_termination_hook)(void *) = NULL;
171 + extern value caml_start_program (void);
172 + extern void caml_init_ieee_floats (void);
173 + extern void caml_init_signals (void);
174 ++extern void caml_terminate_signals(void);
175 +
176 + #if defined(_MSC_VER) && __STDC_SECURE_LIB__ >= 200411L
177 +
178 +@@ -103,6 +104,7 @@ extern void caml_install_invalid_parameter_handler();
179 + value caml_startup_exn(char **argv)
180 + {
181 + char * exe_name, * proc_self_exe;
182 ++ value res;
183 + char tos;
184 +
185 + #ifdef WITH_SPACETIME
186 +@@ -138,10 +140,13 @@ value caml_startup_exn(char **argv)
187 + exe_name = caml_search_exe_in_path(exe_name);
188 + caml_sys_init(exe_name, argv);
189 + if (sigsetjmp(caml_termination_jmpbuf.buf, 0)) {
190 ++ caml_terminate_signals();
191 + if (caml_termination_hook != NULL) caml_termination_hook(NULL);
192 + return Val_unit;
193 + }
194 +- return caml_start_program();
195 ++ res = caml_start_program();
196 ++ caml_terminate_signals();
197 ++ return res;
198 + }
199 +
200 + void caml_startup(char **argv)
201 +diff --git a/byterun/sys.c b/byterun/sys.c
202 +index 3706e9002d5..aa152239ebf 100644
203 +--- a/byterun/sys.c
204 ++++ b/byterun/sys.c
205 +@@ -111,6 +111,8 @@ static void caml_sys_check_path(value name)
206 + }
207 + }
208 +
209 ++extern void caml_terminate_signals(void);
210 ++
211 + CAMLprim value caml_sys_exit(value retcode_v)
212 + {
213 + int retcode = Int_val(retcode_v);
214 +@@ -144,6 +146,9 @@ CAMLprim value caml_sys_exit(value retcode_v)
215 + caml_debugger(PROGRAM_EXIT);
216 + #endif
217 + CAML_INSTR_ATEXIT ();
218 ++#ifdef NATIVE_CODE
219 ++ caml_terminate_signals();
220 ++#endif
221 + CAML_SYS_EXIT(retcode);
222 + return Val_unit;
223 + }
224 +
225
226 diff --git a/dev-lang/ocaml/ocaml-4.05.0-r9.ebuild b/dev-lang/ocaml/ocaml-4.05.0-r9.ebuild
227 new file mode 100644
228 index 000000000000..f2bb2369ad69
229 --- /dev/null
230 +++ b/dev-lang/ocaml/ocaml-4.05.0-r9.ebuild
231 @@ -0,0 +1,157 @@
232 +# Copyright 1999-2022 Gentoo Authors
233 +# Distributed under the terms of the GNU General Public License v2
234 +
235 +EAPI=7
236 +
237 +inherit flag-o-matic toolchain-funcs
238 +
239 +PATCHLEVEL="9"
240 +MY_P="${P/_/-}"
241 +DESCRIPTION="Type-inferring functional programming language descended from the ML family"
242 +HOMEPAGE="https://ocaml.org"
243 +SRC_URI="https://github.com/ocaml/ocaml/archive/${PV/_/+}.tar.gz -> ${MY_P}.tar.gz
244 + mirror://gentoo/${PN}-patches-${PATCHLEVEL}.tar.bz2"
245 +
246 +LICENSE="QPL-1.0 LGPL-2"
247 +# Everytime ocaml is updated to a new version, everything ocaml must be rebuilt,
248 +# so here we go with the subslot.
249 +SLOT="0/$(ver_cut 1-2)"
250 +KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~mips ~ppc ~ppc64 ~x86 ~amd64-linux ~x86-linux ~ppc-macos ~sparc-solaris ~x86-solaris"
251 +IUSE="emacs flambda latex ncurses +ocamlopt spacetime X xemacs"
252 +
253 +RDEPEND="
254 + sys-libs/binutils-libs:=
255 + ncurses? ( sys-libs/ncurses:0= )
256 + spacetime? ( sys-libs/libunwind:= )
257 + X? ( x11-libs/libX11 )
258 + !dev-ml/num"
259 +BDEPEND="${RDEPEND}
260 + virtual/pkgconfig"
261 +PDEPEND="emacs? ( app-emacs/ocaml-mode )
262 + xemacs? ( app-xemacs/ocaml )"
263 +
264 +QA_FLAGS_IGNORED='usr/lib.*/ocaml/raw_spacetime_lib.cmxs'
265 +
266 +S="${WORKDIR}/${MY_P}"
267 +
268 +PATCHES=(
269 + "${FILESDIR}"/${PN}-4.04.2-tinfo.patch #459512
270 + "${FILESDIR}"/${P}-gcc10.patch
271 + "${FILESDIR}"/${P}-CVE-2018-9838.patch
272 + "${FILESDIR}"/${P}-glibc-2.34.patch
273 +)
274 +
275 +pkg_setup() {
276 + # dev-lang/ocaml creates its own objects but calls gcc for linking, which will
277 + # results in relocations if gcc wants to create a PIE executable
278 + if gcc-specs-pie ; then
279 + append-ldflags -nopie
280 + ewarn "Ocaml generates its own native asm, you're using a PIE compiler"
281 + ewarn "We have appended -nopie to ocaml build options"
282 + ewarn "because linking an executable with pie while the objects are not pic will not work"
283 + fi
284 +}
285 +
286 +src_prepare() {
287 + EPATCH_SUFFIX="patch" eapply "${WORKDIR}/patches"
288 +
289 + cp "${FILESDIR}"/ocaml.conf "${T}" || die
290 +
291 + default
292 +}
293 +
294 +src_configure() {
295 + export LC_ALL=C
296 + local myconf=""
297 +
298 + # Causes build failures because it builds some programs with -pg,
299 + # bug #270920
300 + filter-flags -fomit-frame-pointer
301 + # Bug #285993
302 + filter-mfpmath sse
303 +
304 + # Broken until 4.12
305 + # bug #818445
306 + filter-flags '-flto*'
307 + append-flags -fno-strict-aliasing
308 +
309 + # -ggdb3 & co makes it behave weirdly, breaks sexplib
310 + replace-flags -ggdb* -ggdb
311 +
312 + # OCaml generates textrels on 32-bit arches
313 + # We can't do anything about it, but disabling it means that tests
314 + # for OCaml-based packages won't fail on unexpected output
315 + # bug #773226
316 + if use arm || use ppc || use x86 ; then
317 + append-ldflags "-Wl,-z,notext"
318 + fi
319 +
320 + # It doesn't compile on alpha without this LDFLAGS
321 + use alpha && append-ldflags "-Wl,--no-relax"
322 +
323 + use ncurses || myconf="${myconf} -no-curses"
324 + use X || myconf="${myconf} -no-graph"
325 + use flambda && myconf="${myconf} -flambda"
326 + use spacetime && myconf="${myconf} -spacetime"
327 +
328 + # ocaml uses a home-brewn configure script, preventing it to use econf.
329 + RAW_LDFLAGS="$(raw-ldflags)" ./configure \
330 + --prefix "${EPREFIX}"/usr \
331 + --bindir "${EPREFIX}"/usr/bin \
332 + --target-bindir "${EPREFIX}"/usr/bin \
333 + --libdir "${EPREFIX}"/usr/$(get_libdir)/ocaml \
334 + --mandir "${EPREFIX}"/usr/share/man \
335 + -target "${CHOST}" \
336 + -host "${CBUILD}" \
337 + -cc "$(tc-getCC)" \
338 + -as "$(tc-getAS)" \
339 + -aspp "$(tc-getCC) -c" \
340 + -partialld "$(tc-getLD) -r" \
341 + --with-pthread ${myconf} || die "configure failed!"
342 +
343 + # http://caml.inria.fr/mantis/view.php?id=4698
344 + export CCLINKFLAGS="${LDFLAGS}"
345 +}
346 +
347 +src_compile() {
348 + emake world
349 +
350 + # Native code generation can be disabled now
351 + if use ocamlopt ; then
352 + # bug #279968
353 + emake opt
354 + emake -j1 opt.opt
355 + fi
356 +}
357 +
358 +src_test() {
359 + if use ocamlopt ; then
360 + emake -j1 tests
361 + else
362 + ewarn "${PN} was built without 'ocamlopt' USE flag; skipping tests."
363 + fi
364 +}
365 +
366 +src_install() {
367 + emake BINDIR="${ED}"/usr/bin \
368 + LIBDIR="${ED}"/usr/$(get_libdir)/ocaml \
369 + MANDIR="${ED}"/usr/share/man \
370 + install
371 +
372 + # Symlink the headers to the right place
373 + dodir /usr/include
374 + # Create symlink for header files
375 + dosym "../$(get_libdir)/ocaml/caml" /usr/include/caml
376 + dodoc Changes README.adoc
377 + # Create envd entry for latex input files
378 + if use latex ; then
379 + echo "TEXINPUTS=\"${EPREFIX}/usr/$(get_libdir)/ocaml/ocamldoc:\"" > "${T}"/99ocamldoc || die
380 + doenvd "${T}"/99ocamldoc
381 + fi
382 +
383 + sed -i -e "s:lib:$(get_libdir):" "${T}"/ocaml.conf || die
384 +
385 + # Install ocaml-rebuild portage set
386 + insinto /usr/share/portage/config/sets
387 + doins "${T}"/ocaml.conf
388 +}