From 89135c71099a47bebb04c6a73d2d0a392d4e484f Mon Sep 17 00:00:00 2001
From: Bendeguz Pisch <pisch.beni@gmail.com>
Date: Wed, 21 Aug 2024 10:34:45 +0200
Subject: [PATCH] Make perl5 recipe to build perl 5.40.

---
 recipes/wip/dev/lang/perl5/configure_tool.sh | 351 +++++++++++++++++++
 recipes/wip/dev/lang/perl5/perl.patch        | 320 -----------------
 recipes/wip/dev/lang/perl5/recipe.toml       |  58 ++-
 recipes/wip/dev/lang/perl5/redox             |   5 +
 4 files changed, 398 insertions(+), 336 deletions(-)
 create mode 100644 recipes/wip/dev/lang/perl5/configure_tool.sh
 delete mode 100644 recipes/wip/dev/lang/perl5/perl.patch
 create mode 100644 recipes/wip/dev/lang/perl5/redox

diff --git a/recipes/wip/dev/lang/perl5/configure_tool.sh b/recipes/wip/dev/lang/perl5/configure_tool.sh
new file mode 100644
index 000000000..23d3878e1
--- /dev/null
+++ b/recipes/wip/dev/lang/perl5/configure_tool.sh
@@ -0,0 +1,351 @@
+# Toolchain detection
+
+tryprog() {
+	log "trying $1=$2"
+	if command -v $2 1>/dev/null 2>/dev/null; then
+		define "$1" "$2"
+		result "$2"
+		return 0
+	else
+		return 1
+	fi
+}
+
+tryfromenv() {
+	if [ "$mode" = "buildmini" ]; then
+		getenv ev "HOST$2"
+	else
+		getenv ev "$2"
+	fi
+
+	if [ -n "$ev" ]; then
+		tryprog $1 "$ev" && return 0
+		die "Supplied $ev is not usable"
+	fi
+
+	unset ev
+	return 1
+}
+
+# This is only a function for easy access to return-s
+# try.out contains `$cc --version` output.
+#
+# Figuring out gcc is necessary to make sure -fwrapv fix gets applied.
+
+detect_cc_version() {
+	_v=`sed -ne '/^gcc version \([0-9][0-9.]*\).*/s//\1/p' try.out`
+
+	if [ -n "$_v" ]; then
+		define cctype 'gcc'
+		define ccversion "$_v"
+		define gccversion "$_v"
+		result "gcc $_v"
+		return
+	fi
+
+	_v=`sed -ne '/^clang version \([0-9][0-9.]*\).*/s//\1/p' try.out`
+
+	if [ -n "$_v" ]; then
+		define cctype 'clang'
+		define ccversion "$_v"
+		define gccversion "0.0"
+		result "clang $_v"
+		return
+	fi
+
+	define cctype 'cc'
+	define ccversion ''
+	define gccversion '0.0'
+	result 'unknown'
+}
+
+# whichprog symbol VAR prog1 prog2
+whichprog() {
+	mstart "Checking for $1"
+	hinted "$1" && return 0
+
+	# Maybe we've got $CC or $HOSTCC?
+	tryfromenv "$1" "$2" && return 0
+
+	# For anything that sounds like a native compilation,
+	# try no-prefix tools *first*. This is to avoid using
+	# long names is case the host happens to have them.
+	if [ "$mode" = 'native' -o "$mode" = 'buildmini' ]; then
+		tryprog $1 "$3" && return 0
+	fi
+
+	# Finally, try $target-gcc
+	test -n "$toolsprefix" && tryprog $1 "$toolsprefix$3"  && return 0
+	test -n "$target"      && tryprog $1 "$target-$3"      && return 0
+	test -n "$targetarch"  && tryprog $1 "$targetarch-$3"  && return 0
+
+	result "none found"
+	return 1
+}
+
+whichprog cc CC gcc || whichprog cc CC cc || die "No C compiler found"
+#whichprog ld LD ld # while correct, this breaks MM library test
+whichprog ar AR ar || die "Cannot find ar"
+whichprog nm NM nm
+whichprog ranlib RANLIB ranlib
+whichprog readelf READELF readelf || die "Cannot find readelf"
+whichprog objdump OBJDUMP objdump || die "Cannot find objdump"
+
+# XXX: this looks wrong, but the usemmldlt code depends on $ld being able
+# to compile try.c. What kind of moron could have written that. Oh wait.
+#
+# But, there was probably a reason to assume this, likely becase mainline
+# Configure did and still does the same. So, ugh, leaving it as is for now.
+# Speak of backward bug compatibility.
+define ld "$cc"
+
+log
+
+mstart "Trying $cc"
+if not hinted 'cctype'; then
+	run $cc -v >try.out 2>&1
+	try_dump_out
+	detect_cc_version
+fi
+
+mstart "Checking whether $cc is a C++ compiler"
+if not hinted 'd_cplusplus'; then
+	try_start
+	try_cat <<END
+#if defined(__cplusplus)
+YES
+#endif
+END
+	try_dump
+	if not run $cc $ccflags -E try.c > try.out 2>>$cfglog; then
+		define d_cplusplus 'undef'
+		result "probably no"
+	else
+		_r=`grep -v '^#' try.out | grep . | head -1 | grep '^YES'`
+		if [ -n "$_r" ]; then
+			define d_cplusplus 'define'
+			result "yes"
+		else
+			define d_cplusplus 'undef'
+			result 'no'
+		fi
+	fi
+fi
+
+mstart "Deciding how to declare external symbols"
+if not hinted "extern_C"; then
+	case "$d_cplusplus" in
+		define)
+			define "extern_C" 'extern "C"'
+			result "$extern_C"
+			;;
+		*)
+			define "extern_C" 'extern'
+			result "$extern_C"
+			;;
+	esac
+fi
+
+# File name extensions, must be set before running any compile/link tests
+define _o '.o'
+define _a '.a'
+define so 'so'
+define _exe ''
+
+# Used only for modules
+define cccdlflags '-fPIC -Wno-unused-function'
+define ccdlflags '-Wl,-E'
+
+# Misc flags setup
+predef lddlflags "-shared"	# modules
+predef ccflags ''		# perl and modules
+predef ldflags ''		# perl only?
+predef cppflags ''		# unused?
+
+# setfromvar what SHELLVAR
+setfromenv() {
+	getenv v "$2"
+	test -n "$v" && append "$1" "$v"
+}
+
+if [ "$mode" = 'target' -o "$mode" = 'native' ]; then
+	setfromenv ccflags CFLAGS
+	setfromenv ldflags LDFLAGS
+	if [ -n "$sysroot" ]; then
+		msg "Adding --sysroot to {cc,ld}flags"
+		prepend ccflags "--sysroot=$sysroot"
+		prepend ldflags "--sysroot=$sysroot"
+		# While cccdlflags are used together with ccflags,
+		# ld is always called with lddlflags *instead*of* ldflags
+		prepend lddlflags "--sysroot=$sysroot"
+		# Same for cpp
+		prepend cppflags "--sysroot=$sysroot"
+	fi
+elif [ "$mode" = 'buildmini' ]; then
+	setfromenv ccflags HOSTCFLAGS
+	setfromenv ldflags HOSTLDFLAGS
+fi
+
+# Use $ldflags as default value for $lddlflags, together with whatever
+# hints provided, but avoid re-setting anyting specified in the command line
+if [ -n "$ldflags" -a "$x_lddlflags" != "user" ]; then
+	append lddlflags "$ldflags"
+fi
+
+# enddef ccflags # done later in _hdrs because of LARGEFILE_SOURCE
+enddef ldflags
+enddef lddlflags
+enddef cppflags
+
+mstart "Checking whether ld supports scripts"
+if not hinted 'ld_can_script'; then
+	cat > try.c <<EOM
+void foo() {}
+void bar() {}
+EOM
+	cat > try.h <<EOM
+LIBTEST_42 {
+ global:
+  foo;
+ local: *;
+ };
+EOM
+	log "try.c"
+	try_dump
+	log "try.h"
+	try_dump_h
+	rm -f a.out 2>/dev/null
+
+	if run $cc $cccdlflags $ccdlflags $ccflags $lddlflags -o a.out try.c \
+		-Wl,--version-script=try.h >/dev/null 2>&1 \
+		&& test -s a.out
+	then
+		define ld_can_script 'define'
+		result "yes"
+	else
+		define ld_can_script 'undef'
+		result "no"
+	fi
+fi
+
+# Guessing OS is better done with the toolchain available.
+# CC output is crucial here -- Android toolchains come with
+# generic armeabi prefix and "android" is one of the few osname
+# values that make difference later.
+
+mstart "Trying to guess target OS"
+if not hinted 'osname'; then
+	run $cc -v > try.out 2>&1
+	try_dump_out
+
+	_ct=`sed -ne '/^Target: /s///p' try.out`
+	test -z "$_ct" && _ct="$targetarch"
+
+	case "$_ct" in
+		*-mingw32)
+			define osname "MSWin32"
+			result "MSWin32"
+			;;
+		*-android|*-androideabi)
+			define osname "android"
+			result "Android"
+			;;
+		*-linux*)
+			define osname "linux"
+			result "Linux"
+			;;
+		*-netbsd*)
+			define osname "netbsd"
+			result "NetBSD"
+			;;
+		*-bsd*)
+			define osname "bsd"
+			result "BSD"
+			;;
+		*-gnu*)
+			define osname "gnu"
+			result "GNU"
+			;;
+		*-midipix*)
+			define osname "midipix"
+			result "Midipix"
+			;;
+		*-redox*)
+			define osname "redox"
+			result "Redox"
+			;;
+		*)
+			result "no"
+			;;
+	esac
+fi
+
+# Check whether debugging should be enabled
+# Allow -DEBUGGING as well (sets EBUGGING=define)
+case "$DEBUGGING:$EBUGGING" in
+	:*)
+		DEBUGGING=$EBUGGING
+		;;
+esac
+
+mstart "Checking whether to enable -g"
+predef optimize ''
+case "$DEBUGGING" in
+	both|define)
+		append optimize "-g"
+		result "yes" ;;
+	*)
+		result "no" ;;
+esac
+
+mstart "Checking whether to use -DDEBUGGING"
+case "$DEBUGGING" in
+	both|define)
+		append optimize '-DDEBUGGING'
+		result "yes" ;;
+	*)
+		result "no" ;;
+esac
+
+# gcc 4.9 and above does some optimizations that break perl.
+# see perl ticket 121505.
+if [ "$cctype" = 'gcc' ]; then
+	case "$ccversion" in
+		1.*|2.*|3.*) ;;
+		4.9*) append 'optimize' '-fwrapv -fno-strict-aliasing' ;;
+		4.*) ;;
+		*) append 'optimize' '-fwrapv -fno-strict-aliasing' ;;
+	esac
+fi
+enddef optimize
+
+# These are kind-of part of toolchain, but we do not test them
+
+# For newer gcc-s, -E alone is *not* enough! Perl expects cpp not to break
+# lines, but gcc injects #line directives in-between tokens, subtly breaking
+# try_preproc and Errno.pm
+define cpp "$cc -E -P"
+define cpprun "$cpp"
+define cppstdin "$cpp"
+
+define cpplast -
+define cppminus -
+define cppsymbols
+
+define nm_opt
+define nm_so_opt
+
+# cperl wants to know this for some reason
+mstart "Checking whether address sanitizer is enabled"
+if not hinted sanitize_address 'yes' 'no'; then
+	case "$ccflags" in
+		*-fsanitize=address*|*-faddress-sanitizer*)
+			define sanitize_address 'define'
+			result 'yes'
+			;;
+		*)
+			define sanitize_address 'undef'
+			result 'no'
+			;;
+	esac
+fi
diff --git a/recipes/wip/dev/lang/perl5/perl.patch b/recipes/wip/dev/lang/perl5/perl.patch
deleted file mode 100644
index f3067191f..000000000
--- a/recipes/wip/dev/lang/perl5/perl.patch
+++ /dev/null
@@ -1,320 +0,0 @@
-diff -ru source/ext/POSIX/POSIX.xs source-new/ext/POSIX/POSIX.xs
---- source/ext/POSIX/POSIX.xs	2017-06-30 14:03:22.000000000 -0700
-+++ source-new/ext/POSIX/POSIX.xs	2017-08-17 19:21:59.498359355 -0700
-@@ -1302,6 +1302,7 @@
- #endif
- #include <fcntl.h>
- 
-+#if 0
- #ifdef HAS_TZNAME
- #  if !defined(WIN32) && !defined(__CYGWIN__) && !defined(NETWARE) && !defined(__UWIN__)
- extern char *tzname[];
-@@ -1311,6 +1312,7 @@
- char *tzname[] = { "" , "" };
- #endif
- #endif
-+#endif
- 
- #if defined(__VMS) && !defined(__POSIX_SOURCE)
- 
-@@ -1327,10 +1329,11 @@
- #if defined (__CYGWIN__)
- #    define tzname _tzname
- #endif
--#if defined (WIN32) || defined (NETWARE)
-+#if 1
- #  undef mkfifo
- #  define mkfifo(a,b) not_here("mkfifo")
- #  define ttyname(a) (char*)not_here("ttyname")
-+#  define pause() not_here("pause")
- #  define sigset_t long
- #  define pid_t long
- #  ifdef _MSC_VER
-@@ -1397,7 +1400,7 @@
- typedef HV* POSIX__SigAction;
- typedef int POSIX__SigNo;
- typedef int POSIX__Fd;
--#ifdef I_TERMIOS
-+#if 0
- typedef struct termios* POSIX__Termios;
- #else /* Define termios types to int, and call not_here for the functions.*/
- #define POSIX__Termios int
-@@ -1829,7 +1832,7 @@
-     const char *	packname
-     CODE:
- 	{
--#ifdef I_TERMIOS
-+#if 0
- 	    void *const p = allocate_struct(aTHX_ (ST(0) = sv_newmortal()),
- 					    sizeof(struct termios), packname);
- 	    /* The previous implementation stored a pointer to an uninitialised
-@@ -1896,7 +1899,7 @@
- 	getcflag = 2
- 	getlflag = 3
-     CODE:
--#ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
-+#if 0 /* References a termios structure member so ifdef it out. */
- 	switch(ix) {
- 	case 0:
- 	    RETVAL = termios_ref->c_iflag;
-@@ -1925,7 +1928,7 @@
- 	POSIX::Termios	termios_ref
- 	unsigned int	ccix
-     CODE:
--#ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
-+#if 0 /* References a termios structure member so ifdef it out. */
- 	if (ccix >= NCCS)
- 	    croak("Bad getcc subscript");
- 	RETVAL = termios_ref->c_cc[ccix];
-@@ -1957,7 +1960,7 @@
- 	setcflag = 2
- 	setlflag = 3
-     CODE:
--#ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
-+#if 0 /* References a termios structure member so ifdef it out. */
- 	switch(ix) {
- 	case 0:
- 	    termios_ref->c_iflag = flag;
-@@ -1982,7 +1985,7 @@
- 	unsigned int	ccix
- 	cc_t		cc
-     CODE:
--#ifdef I_TERMIOS /* References a termios structure member so ifdef it out. */
-+#if 0 /* References a termios structure member so ifdef it out. */
- 	if (ccix >= NCCS)
- 	    croak("Bad setcc subscript");
- 	termios_ref->c_cc[ccix] = cc;
-@@ -3540,15 +3543,15 @@
- void
- times()
- 	PPCODE:
--	struct tms tms;
--	clock_t realtime;
--	realtime = times( &tms );
-+	//struct tms tms;
-+	//clock_t realtime;
-+	//realtime = times( &tms );
- 	EXTEND(SP,5);
--	PUSHs( sv_2mortal( newSViv( (IV) realtime ) ) );
--	PUSHs( sv_2mortal( newSViv( (IV) tms.tms_utime ) ) );
--	PUSHs( sv_2mortal( newSViv( (IV) tms.tms_stime ) ) );
--	PUSHs( sv_2mortal( newSViv( (IV) tms.tms_cutime ) ) );
--	PUSHs( sv_2mortal( newSViv( (IV) tms.tms_cstime ) ) );
-+	PUSHs( sv_2mortal( newSViv( (IV) 0 ) ) );
-+	PUSHs( sv_2mortal( newSViv( (IV) 0 ) ) );
-+	PUSHs( sv_2mortal( newSViv( (IV) 0 ) ) );
-+	PUSHs( sv_2mortal( newSViv( (IV) 0 ) ) );
-+	PUSHs( sv_2mortal( newSViv( (IV) 0 ) ) );
- 
- double
- difftime(time1, time2)
-diff -ru source/mg.c source-new/mg.c
---- source/mg.c	2017-06-30 14:03:22.000000000 -0700
-+++ source-new/mg.c	2017-08-17 14:40:37.669507085 -0700
-@@ -3034,16 +3034,16 @@
- #ifdef HAS_SETRESUID
-         PERL_UNUSED_RESULT(setresuid(new_uid, (Uid_t)-1, (Uid_t)-1));
- #else
--	if (new_uid == PerlProc_geteuid()) {		/* special case $< = $> */
-+//	if (new_uid == PerlProc_geteuid()) {		/* special case $< = $> */
- #ifdef PERL_DARWIN
- 	    /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */
- 	    if (new_uid != 0 && PerlProc_getuid() == 0)
-                 PERL_UNUSED_RESULT(PerlProc_setuid(0));
- #endif
--            PERL_UNUSED_RESULT(PerlProc_setuid(new_uid));
--	} else {
-+//            PERL_UNUSED_RESULT(PerlProc_setuid(new_uid));
-+//	} else {
- 	    Perl_croak(aTHX_ "setruid() not implemented");
--	}
-+//	}
- #endif
- #endif
- #endif
-@@ -3067,11 +3067,11 @@
- #ifdef HAS_SETRESUID
- 	PERL_UNUSED_RESULT(setresuid((Uid_t)-1, new_euid, (Uid_t)-1));
- #else
--	if (new_euid == PerlProc_getuid())		/* special case $> = $< */
--	    PERL_UNUSED_RESULT(PerlProc_setuid(new_euid));
--	else {
-+//	if (new_euid == PerlProc_getuid())		/* special case $> = $< */
-+//	    PERL_UNUSED_RESULT(PerlProc_setuid(new_euid));
-+//	else {
- 	    Perl_croak(aTHX_ "seteuid() not implemented");
--	}
-+//	}
- #endif
- #endif
- #endif
-@@ -3095,11 +3095,11 @@
- #ifdef HAS_SETRESGID
-         PERL_UNUSED_RESULT(setresgid(new_gid, (Gid_t)-1, (Gid_t) -1));
- #else
--	if (new_gid == PerlProc_getegid())			/* special case $( = $) */
--	    PERL_UNUSED_RESULT(PerlProc_setgid(new_gid));
--	else {
-+//	if (new_gid == PerlProc_getegid())			/* special case $( = $) */
-+//	    PERL_UNUSED_RESULT(PerlProc_setgid(new_gid));
-+//	else {
- 	    Perl_croak(aTHX_ "setrgid() not implemented");
--	}
-+//	}
- #endif
- #endif
- #endif
-@@ -3178,11 +3178,11 @@
- #ifdef HAS_SETRESGID
- 	PERL_UNUSED_RESULT(setresgid((Gid_t)-1, new_egid, (Gid_t)-1));
- #else
--	if (new_egid == PerlProc_getgid())			/* special case $) = $( */
--	    PERL_UNUSED_RESULT(PerlProc_setgid(new_egid));
--	else {
-+//	if (new_egid == PerlProc_getgid())			/* special case $) = $( */
-+//	    PERL_UNUSED_RESULT(PerlProc_setgid(new_egid));
-+//	else {
- 	    Perl_croak(aTHX_ "setegid() not implemented");
--	}
-+//	}
- #endif
- #endif
- #endif
-diff -ru source/perl.h source-new/perl.h
---- source/perl.h	2017-06-30 14:03:22.000000000 -0700
-+++ source-new/perl.h	2017-08-17 13:14:01.806883580 -0700
-@@ -1061,7 +1061,6 @@
- #ifndef PERL_MICRO
- #ifndef memchr
- #   ifndef HAS_MEMCHR
--#       define memchr(s,c,n) ninstr((char*)(s), ((char*)(s)) + n, &(c), &(c) + 1)
- #   endif
- #endif
- #endif
-diff -ru source/pp_hot.c source-new/pp_hot.c
---- source/pp_hot.c	2017-06-30 14:03:22.000000000 -0700
-+++ source-new/pp_hot.c	2017-08-17 13:16:20.234635700 -0700
-@@ -1583,11 +1583,13 @@
- 		PL_delaymagic &= ~DM_EUID;
- 	    }
- #    endif /* HAS_SETEUID */
-+#if 0
- 	    if (PL_delaymagic & DM_UID) {
- 		if (PL_delaymagic_uid != PL_delaymagic_euid)
- 		    DIE(aTHX_ "No setreuid available");
- 		PERL_UNUSED_RESULT(PerlProc_setuid(PL_delaymagic_uid));
- 	    }
-+#endif
- #  endif /* HAS_SETREUID */
- #endif /* HAS_SETRESUID */
- 
-@@ -1619,11 +1621,13 @@
- 		PL_delaymagic &= ~DM_EGID;
- 	    }
- #    endif /* HAS_SETEGID */
-+#if 0
- 	    if (PL_delaymagic & DM_GID) {
- 		if (PL_delaymagic_gid != PL_delaymagic_egid)
- 		    DIE(aTHX_ "No setregid available");
- 		PERL_UNUSED_RESULT(PerlProc_setgid(PL_delaymagic_gid));
- 	    }
-+#endif
- #  endif /* HAS_SETREGID */
- #endif /* HAS_SETRESGID */
- 
-diff -ru source/pp_sys.c source-new/pp_sys.c
---- source/pp_sys.c	2017-06-30 14:03:22.000000000 -0700
-+++ source-new/pp_sys.c	2017-08-17 14:57:00.983541027 -0700
-@@ -2413,7 +2413,7 @@
-     TAINT_PROPER(PL_op_desc[optype]);
- 
-     if (optype == OP_IOCTL)
--#ifdef HAS_IOCTL
-+#if 0
- 	retval = PerlLIO_ioctl(PerlIO_fileno(IoIFP(io)), func, s);
- #else
- 	DIE(aTHX_ "ioctl is not implemented");
-@@ -2705,7 +2705,7 @@
-     if (!IoIFP(io))
- 	goto nuts;
- 
--    PUSHi( PerlSock_shutdown(PerlIO_fileno(IoIFP(io)), how) >= 0 );
-+    PUSHi( 0 );
-     RETURN;
- 
-   nuts:
-@@ -2980,7 +2980,7 @@
-         sv_setuid(PUSHmortal, PL_statcache.st_uid);
-         sv_setgid(PUSHmortal, PL_statcache.st_gid);
- 
--#ifdef USE_STAT_RDEV
-+#if 0
- 	mPUSHi(PL_statcache.st_rdev);
- #else
- 	PUSHs(newSVpvs_flags("", SVs_TEMP));
-@@ -4186,7 +4186,7 @@
- 
- PP(pp_fork)
- {
--#ifdef HAS_FORK
-+#if 1
-     dSP; dTARGET;
-     Pid_t childpid;
- #ifdef HAS_SIGPROCMASK
-@@ -4332,7 +4332,7 @@
- 	TAINT_PROPER("system");
-     }
-     PERL_FLUSHALL_FOR_CHILD;
--#if (defined(HAS_FORK) || defined(__amigaos4__)) && !defined(VMS) && !defined(OS2) || defined(PERL_MICRO)
-+#if 1
-     {
- #ifdef __amigaos4__
-         struct UserData userdata;
-@@ -5582,7 +5582,7 @@
- 
- PP(pp_ggrent)
- {
--#ifdef HAS_GROUP
-+#if 0
-     dSP;
-     const I32 which = PL_op->op_type;
-     const struct group *grent;
-diff -ru source/util.c source-new/util.c
---- source/util.c	2017-06-30 14:03:22.000000000 -0700
-+++ source-new/util.c	2017-08-17 15:42:05.328932377 -0700
-@@ -2456,7 +2456,7 @@
- PerlIO *
- Perl_my_popen_list(pTHX_ const char *mode, int n, SV **args)
- {
--#if (!defined(DOSISH) || defined(HAS_FORK)) && !defined(OS2) && !defined(VMS) && !defined(NETWARE) && !defined(__LIBCATAMOUNT__) && !defined(__amigaos4__)
-+#if 1
-     int p[2];
-     I32 This, that;
-     Pid_t pid;
-@@ -2821,7 +2821,7 @@
- Pid_t
- Perl_my_fork(void)
- {
--#if defined(HAS_FORK)
-+#if 1
-     Pid_t pid;
- #if defined(USE_ITHREADS) && !defined(HAS_PTHREAD_ATFORK)
-     atfork_lock();
-@@ -2842,7 +2842,7 @@
- #endif /* HAS_FORK */
- }
- 
--#ifndef HAS_DUP2
-+#if 0
- int
- dup2(int oldfd, int newfd)
- {
-@@ -3196,7 +3196,7 @@
- 
- #if defined(OS2)
- int pclose();
--#ifdef HAS_FORK
-+#if 1
- int					/* Cannot prototype with I32
- 					   in os2ish.h. */
- my_syspclose(PerlIO *ptr)
diff --git a/recipes/wip/dev/lang/perl5/recipe.toml b/recipes/wip/dev/lang/perl5/recipe.toml
index d40745280..2ea366e21 100644
--- a/recipes/wip/dev/lang/perl5/recipe.toml
+++ b/recipes/wip/dev/lang/perl5/recipe.toml
@@ -1,24 +1,50 @@
-#TODO incomplete script
-#TODO does the patch is still needed?
-#TODO update the patch to match the current version
+#TODO compiles, works in a basic way, but needs figuring out why -ldl is ignored
+#TODO also until new signal implementation gets to be the default, remove siginfo struct from relibc's include/bits/signal.h
+#     and modify relibc/src/header/signal/mod.rs sigtimedwait second parameter's type from siginfo_t to siginfo (cbindgen needs a reference)
+#TODO needs further testing
 [source]
 tar = "https://www.cpan.org/src/5.0/perl-5.40.0.tar.gz"
-patches = [
-    "perl.patch",
-]
 [build]
 template = "custom"
 script = """
-curl -L -O --time-cond perl-cross-1.5.3.tar.gz https://github.com/arsv/perl-cross/releases/download/1.5.3/perl-cross-1.5.3.tar.gz
-tar --strip-components=1 -xvf perl-cross-1.5.3.tar.gz
-wget -O cnf/config.sub "https://gitlab.redox-os.org/redox-os/gnu-config/-/raw/master/config.sub?inline=false"
-sysroot="$($HOST-gcc -print-sysroot)" # does it is still needed?
-sed -i "s/^#define Netdb_name_t.*/#define Netdb_name_t const char*/" config.h # XXX
-sed -i 's/#define Strerror(e).*$/#define Strerror(e) strerror(e)/' config.h #
-echo "#define HAS_VPRINTF" >> config.h
-COOKBOOK_CONFIGURE_FLAGS+=(
+rsync -av --delete "${COOKBOOK_SOURCE}/" ./ #move it to the top of the script after debugging - BP
+curl -L -O --time-cond perl-cross-1.6.tar.gz https://github.com/arsv/perl-cross/releases/download/1.6/perl-cross-1.6.tar.gz
+tar --strip-components=1 -xvf perl-cross-1.6.tar.gz
+wget -O ./cnf/config.sub "https://gitlab.redox-os.org/redox-os/gnu-config/-/raw/master/config.sub?inline=false"
+#Note: since perl-cross can run only inside the source-tree (out-of-tree is bugged) it's easier to do everything in the build directory
+COOKBOOK_CONFIGURE=${COOKBOOK_BUILD}/configure
+#Note: non-standard configure, familiar flags can have different meaning!
+COOKBOOK_CONFIGURE_FLAGS=(
+--host-cc=gcc
+--host-cpp=g++
+--target=x86_64-unknown-redox
+--prefix=
+--sysroot=${COOKBOOK_SYSROOT}
     --disable-mod=Sys-Syslog,Time-HiRes
-    --with-libs='m'
+    #--with-libs='m'
 )
-cookbook_configure
+cp ${COOKBOOK_RECIPE}/configure_tool.sh ${COOKBOOK_BUILD}/cnf/configure_tool.sh
+cp ${COOKBOOK_RECIPE}/redox ${COOKBOOK_BUILD}/cnf/hints/redox
+mkdir ${COOKBOOK_SYSROOT}/usr
+cp -r /mnt/c/commd/redox/redox/prefix/x86_64-unknown-redox/relibc-install/x86_64-unknown-redox/include ${COOKBOOK_SYSROOT}/usr
+./configure --host-cc=gcc --host-cpp=g++ --target=x86_64-unknown-redox --sysroot=${COOKBOOK_SYSROOT} --disable-mod=Sys-Syslog
+sed -i "s/^#define Netdb_name_t.*/#define Netdb_name_t const char*/" config.h
+#OSNAME sed -i 's/^#define OSNAME.*/#define OSNAME "redox"/' config.h
+sed -i "s/^# HAS_NANOSLEEP.*/#define HAS_NANOSLEEP/" config.h
+sed -i "s|^/.#define I_GRP.*|#define I_GRP|" config.h
+echo "#define HAS_GROUP" >> config.h
+#sed -i 's/#define Strerror(e).*$/#define Strerror(e) strerror(e)/' config.h
+#echo "#define HAS_VPRINTF" >> config.h
+make -j4
+make install DESTDIR="${COOKBOOK_STAGE}"
+pushd .
+cd "${COOKBOOK_STAGE}/usr/share/man/man3"
+for f in *; do
+case "$f" in
+*::*)
+mv "$f" "${f//::/__}";
+;;
+esac
+done
+popd
 """
diff --git a/recipes/wip/dev/lang/perl5/redox b/recipes/wip/dev/lang/perl5/redox
new file mode 100644
index 000000000..5b62f5bc4
--- /dev/null
+++ b/recipes/wip/dev/lang/perl5/redox
@@ -0,0 +1,5 @@
+# NetBSD syscalls
+d_nanosleep='define'
+
+# libraries to test
+libswanted='m dl'
-- 
GitLab