From 3a656ad07d7ea00be5e5bcf71e9d45813e46f8c5 Mon Sep 17 00:00:00 2001
From: Michal Hrusecky <michal.hrusecky@turris.com>
Date: Wed, 6 Mar 2024 15:24:14 +0100
Subject: [PATCH] knot-resolver{,6}: Fix lua on 32-bit systems with 64-bit
 time_t

This improves the heuristics.
The problem would be detected by meson, but not when cross-compiling,
in which case things would mostly run OK, except some lua code/modules.
---
 ...Fix-lua-on-32-bit-systems-with-64-bi.patch | 311 ++++++++++++++++++
 1 file changed, 311 insertions(+)
 create mode 100644 patches/turrispackages/wip/0001-knot-resolver-6-Fix-lua-on-32-bit-systems-with-64-bi.patch

diff --git a/patches/turrispackages/wip/0001-knot-resolver-6-Fix-lua-on-32-bit-systems-with-64-bi.patch b/patches/turrispackages/wip/0001-knot-resolver-6-Fix-lua-on-32-bit-systems-with-64-bi.patch
new file mode 100644
index 000000000..ee901124c
--- /dev/null
+++ b/patches/turrispackages/wip/0001-knot-resolver-6-Fix-lua-on-32-bit-systems-with-64-bi.patch
@@ -0,0 +1,311 @@
+From 795b790adf7a2c057f5b59de49fa4e62b22e63f1 Mon Sep 17 00:00:00 2001
+From: Michal Hrusecky <michal.hrusecky@turris.com>
+Date: Wed, 6 Mar 2024 15:22:08 +0100
+Subject: [PATCH] knot-resolver{,6}: Fix lua on 32-bit systems with 64-bit
+ time_t
+
+This improves the heuristics.
+The problem would be detected by meson, but not when cross-compiling,
+in which case things would mostly run OK, except some lua code/modules.
+---
+ .../patches/040-lua_time_t.patch              | 140 ++++++++++++++++++
+ .../patches/040-lua_time_t.patch              | 140 ++++++++++++++++++
+ 2 files changed, 280 insertions(+)
+ create mode 100644 net/knot-resolver/patches/040-lua_time_t.patch
+ create mode 100644 net/knot-resolver6/patches/040-lua_time_t.patch
+
+diff --git a/net/knot-resolver/patches/040-lua_time_t.patch b/net/knot-resolver/patches/040-lua_time_t.patch
+new file mode 100644
+index 000000000..461748d81
+--- /dev/null
++++ b/net/knot-resolver/patches/040-lua_time_t.patch
+@@ -0,0 +1,140 @@
++From 6261af1cd54339ba7d8944c539a12912f1571eff Mon Sep 17 00:00:00 2001
++From: =?UTF-8?q?Vladim=C3=ADr=20=C4=8Cun=C3=A1t?= <vladimir.cunat@nic.cz>
++Date: Wed, 6 Mar 2024 12:19:28 +0100
++Subject: [PATCH] daemon/lua: fix on 32-bit systems with 64-bit time_t
++
++This improves the heuristics.
++The problem would be detected by meson, but not when cross-compiling,
++in which case things would mostly run OK, except some lua code/modules.
++---
++ daemon/lua/kres-gen-30.lua |  7 ++++---
++ daemon/lua/kres-gen-31.lua |  7 ++++---
++ daemon/lua/kres-gen-32.lua |  7 ++++---
++ daemon/lua/kres-gen.sh     | 15 +++++++++------
++ daemon/lua/meson.build     | 19 ++++++++++++++++---
++ 6 files changed, 45 insertions(+), 18 deletions(-)
++
++diff --git a/daemon/lua/kres-gen-30.lua b/daemon/lua/kres-gen-30.lua
++index 7639e797e..f3f8a499c 100644
++--- a/daemon/lua/kres-gen-30.lua
+++++ b/daemon/lua/kres-gen-30.lua
++@@ -2,9 +2,10 @@
++ 
++ local ffi = require('ffi')
++ --[[ This file is generated by ./kres-gen.sh ]] ffi.cdef[[
++-typedef long time_t;
++-typedef long __time_t;
++-typedef long __suseconds_t;
+++
+++typedef @time_t@ time_t;
+++typedef @time_t@ __time_t;
+++typedef @time_t@ __suseconds_t;
++ struct timeval {
++ 	__time_t tv_sec;
++ 	__suseconds_t tv_usec;
++diff --git a/daemon/lua/kres-gen-31.lua b/daemon/lua/kres-gen-31.lua
++index e555a6a07..46b349ea1 100644
++--- a/daemon/lua/kres-gen-31.lua
+++++ b/daemon/lua/kres-gen-31.lua
++@@ -2,9 +2,10 @@
++ 
++ local ffi = require('ffi')
++ --[[ This file is generated by ./kres-gen.sh ]] ffi.cdef[[
++-typedef long time_t;
++-typedef long __time_t;
++-typedef long __suseconds_t;
+++
+++typedef @time_t@ time_t;
+++typedef @time_t@ __time_t;
+++typedef @time_t@ __suseconds_t;
++ struct timeval {
++ 	__time_t tv_sec;
++ 	__suseconds_t tv_usec;
++diff --git a/daemon/lua/kres-gen-32.lua b/daemon/lua/kres-gen-32.lua
++index 31a5c5dde..88b50ded0 100644
++--- a/daemon/lua/kres-gen-32.lua
+++++ b/daemon/lua/kres-gen-32.lua
++@@ -2,9 +2,10 @@
++ 
++ local ffi = require('ffi')
++ --[[ This file is generated by ./kres-gen.sh ]] ffi.cdef[[
++-typedef long time_t;
++-typedef long __time_t;
++-typedef long __suseconds_t;
+++
+++typedef @time_t@ time_t;
+++typedef @time_t@ __time_t;
+++typedef @time_t@ __suseconds_t;
++ struct timeval {
++ 	__time_t tv_sec;
++ 	__suseconds_t tv_usec;
++diff --git a/daemon/lua/kres-gen.sh b/daemon/lua/kres-gen.sh
++index 70afb408b..3befd5dae 100755
++--- a/daemon/lua/kres-gen.sh
+++++ b/daemon/lua/kres-gen.sh
++@@ -53,12 +53,15 @@ printf -- "local ffi = require('ffi')\n"
++ printf -- "--[[ This file is generated by ./kres-gen.sh ]] ffi.cdef[[\n"
++ 
++ # Some system dependencies.  TODO: this generated part isn't perfectly portable.
++-${CDEFS} ${LIBKRES} types <<-EOF
++-	typedef time_t
++-	__time_t
++-	__suseconds_t
++-	struct timeval
++-EOF
+++printf "
+++typedef @time_t@ time_t;
+++typedef @time_t@ __time_t;
+++typedef @time_t@ __suseconds_t;
+++struct timeval {
+++	__time_t tv_sec;
+++	__suseconds_t tv_usec;
+++};
+++"
++ 
++ ## Various types (mainly), from libknot and libkres
++ 
++diff --git a/daemon/lua/meson.build b/daemon/lua/meson.build
++index b19777c2d..6df5bc5b2 100644
++--- a/daemon/lua/meson.build
+++++ b/daemon/lua/meson.build
++@@ -47,10 +47,23 @@ else
++   kres_gen_fname = 'kres-gen-30.lua'
++ endif
++ 
+++# Exact types around time_t aren't easy to detect, but at least we need the same size.
+++time_t_size = meson.get_compiler('c').sizeof('time_t', prefix: '#include <sys/time.h>')
+++kres_gen_config = {}
+++foreach t: [ 'long', 'long long' ]
+++  if meson.get_compiler('c').sizeof(t) == time_t_size
+++    kres_gen_config = { 'time_t': t }
+++    break
+++  endif
+++endforeach
+++if kres_gen_config == {}
+++  error('Unexpected sizeof(time_t) == @0@'.format(time_t_size))
+++endif
+++
++ kres_gen_lua = configure_file(
++   input: kres_gen_fname,
++   output: 'kres-gen.lua',
++-  copy: true,
+++  configuration: kres_gen_config,
++ )
++ 
++ run_target(  # run manually to re-generate kres-gen.lua
++@@ -72,9 +85,9 @@ if get_option('kres_gen_test') and not meson.is_cross_build()
++   ]
++   # Construct the lua tester as a meson string.
++   kres_gen_test_luastr = '''
++-    dofile('@0@')
+++    dofile('@0@/../../@1@')
++     local ffi = require('ffi')
++-  '''.format(meson.current_source_dir() / kres_gen_fname)
+++  '''.format(meson.current_build_dir(), kres_gen_lua)
++   foreach ttc: types_to_check
++     # We're careful with adding just includes; otherwise it's more fragile (e.g. linking flags).
++     if 'dep' in ttc
++-- 
++GitLab
++
+diff --git a/net/knot-resolver6/patches/040-lua_time_t.patch b/net/knot-resolver6/patches/040-lua_time_t.patch
+new file mode 100644
+index 000000000..461748d81
+--- /dev/null
++++ b/net/knot-resolver6/patches/040-lua_time_t.patch
+@@ -0,0 +1,140 @@
++From 6261af1cd54339ba7d8944c539a12912f1571eff Mon Sep 17 00:00:00 2001
++From: =?UTF-8?q?Vladim=C3=ADr=20=C4=8Cun=C3=A1t?= <vladimir.cunat@nic.cz>
++Date: Wed, 6 Mar 2024 12:19:28 +0100
++Subject: [PATCH] daemon/lua: fix on 32-bit systems with 64-bit time_t
++
++This improves the heuristics.
++The problem would be detected by meson, but not when cross-compiling,
++in which case things would mostly run OK, except some lua code/modules.
++---
++ daemon/lua/kres-gen-30.lua |  7 ++++---
++ daemon/lua/kres-gen-31.lua |  7 ++++---
++ daemon/lua/kres-gen-32.lua |  7 ++++---
++ daemon/lua/kres-gen.sh     | 15 +++++++++------
++ daemon/lua/meson.build     | 19 ++++++++++++++++---
++ 6 files changed, 45 insertions(+), 18 deletions(-)
++
++diff --git a/daemon/lua/kres-gen-30.lua b/daemon/lua/kres-gen-30.lua
++index 7639e797e..f3f8a499c 100644
++--- a/daemon/lua/kres-gen-30.lua
+++++ b/daemon/lua/kres-gen-30.lua
++@@ -2,9 +2,10 @@
++ 
++ local ffi = require('ffi')
++ --[[ This file is generated by ./kres-gen.sh ]] ffi.cdef[[
++-typedef long time_t;
++-typedef long __time_t;
++-typedef long __suseconds_t;
+++
+++typedef @time_t@ time_t;
+++typedef @time_t@ __time_t;
+++typedef @time_t@ __suseconds_t;
++ struct timeval {
++ 	__time_t tv_sec;
++ 	__suseconds_t tv_usec;
++diff --git a/daemon/lua/kres-gen-31.lua b/daemon/lua/kres-gen-31.lua
++index e555a6a07..46b349ea1 100644
++--- a/daemon/lua/kres-gen-31.lua
+++++ b/daemon/lua/kres-gen-31.lua
++@@ -2,9 +2,10 @@
++ 
++ local ffi = require('ffi')
++ --[[ This file is generated by ./kres-gen.sh ]] ffi.cdef[[
++-typedef long time_t;
++-typedef long __time_t;
++-typedef long __suseconds_t;
+++
+++typedef @time_t@ time_t;
+++typedef @time_t@ __time_t;
+++typedef @time_t@ __suseconds_t;
++ struct timeval {
++ 	__time_t tv_sec;
++ 	__suseconds_t tv_usec;
++diff --git a/daemon/lua/kres-gen-32.lua b/daemon/lua/kres-gen-32.lua
++index 31a5c5dde..88b50ded0 100644
++--- a/daemon/lua/kres-gen-32.lua
+++++ b/daemon/lua/kres-gen-32.lua
++@@ -2,9 +2,10 @@
++ 
++ local ffi = require('ffi')
++ --[[ This file is generated by ./kres-gen.sh ]] ffi.cdef[[
++-typedef long time_t;
++-typedef long __time_t;
++-typedef long __suseconds_t;
+++
+++typedef @time_t@ time_t;
+++typedef @time_t@ __time_t;
+++typedef @time_t@ __suseconds_t;
++ struct timeval {
++ 	__time_t tv_sec;
++ 	__suseconds_t tv_usec;
++diff --git a/daemon/lua/kres-gen.sh b/daemon/lua/kres-gen.sh
++index 70afb408b..3befd5dae 100755
++--- a/daemon/lua/kres-gen.sh
+++++ b/daemon/lua/kres-gen.sh
++@@ -53,12 +53,15 @@ printf -- "local ffi = require('ffi')\n"
++ printf -- "--[[ This file is generated by ./kres-gen.sh ]] ffi.cdef[[\n"
++ 
++ # Some system dependencies.  TODO: this generated part isn't perfectly portable.
++-${CDEFS} ${LIBKRES} types <<-EOF
++-	typedef time_t
++-	__time_t
++-	__suseconds_t
++-	struct timeval
++-EOF
+++printf "
+++typedef @time_t@ time_t;
+++typedef @time_t@ __time_t;
+++typedef @time_t@ __suseconds_t;
+++struct timeval {
+++	__time_t tv_sec;
+++	__suseconds_t tv_usec;
+++};
+++"
++ 
++ ## Various types (mainly), from libknot and libkres
++ 
++diff --git a/daemon/lua/meson.build b/daemon/lua/meson.build
++index b19777c2d..6df5bc5b2 100644
++--- a/daemon/lua/meson.build
+++++ b/daemon/lua/meson.build
++@@ -47,10 +47,23 @@ else
++   kres_gen_fname = 'kres-gen-30.lua'
++ endif
++ 
+++# Exact types around time_t aren't easy to detect, but at least we need the same size.
+++time_t_size = meson.get_compiler('c').sizeof('time_t', prefix: '#include <sys/time.h>')
+++kres_gen_config = {}
+++foreach t: [ 'long', 'long long' ]
+++  if meson.get_compiler('c').sizeof(t) == time_t_size
+++    kres_gen_config = { 'time_t': t }
+++    break
+++  endif
+++endforeach
+++if kres_gen_config == {}
+++  error('Unexpected sizeof(time_t) == @0@'.format(time_t_size))
+++endif
+++
++ kres_gen_lua = configure_file(
++   input: kres_gen_fname,
++   output: 'kres-gen.lua',
++-  copy: true,
+++  configuration: kres_gen_config,
++ )
++ 
++ run_target(  # run manually to re-generate kres-gen.lua
++@@ -72,9 +85,9 @@ if get_option('kres_gen_test') and not meson.is_cross_build()
++   ]
++   # Construct the lua tester as a meson string.
++   kres_gen_test_luastr = '''
++-    dofile('@0@')
+++    dofile('@0@/../../@1@')
++     local ffi = require('ffi')
++-  '''.format(meson.current_source_dir() / kres_gen_fname)
+++  '''.format(meson.current_build_dir(), kres_gen_lua)
++   foreach ttc: types_to_check
++     # We're careful with adding just includes; otherwise it's more fragile (e.g. linking flags).
++     if 'dep' in ttc
++-- 
++GitLab
++
+-- 
+2.44.0
+
-- 
GitLab