From d956601ed18fe4a25434fb5a55e636a369f28bce Mon Sep 17 00:00:00 2001
From: DarkCat09 <gh@dc09.ru>
Date: Sat, 21 Dec 2024 21:29:59 +0400
Subject: [PATCH] fix: adjust agressive optimization options

Sorry for my incompetence in C/C++

Credits: matrix @fox:matrix.org
---
 .build.sh      | 13 +++++++++++--
 build_dig.sh   |  1 +
 build_drill.sh |  1 +
 3 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/.build.sh b/.build.sh
index 5bdee2e..63a4bbb 100644
--- a/.build.sh
+++ b/.build.sh
@@ -20,8 +20,17 @@ clone () {
 # optimizations
 set_cflags () {
   export \
-    CFLAGS="-Ofast" CPPFLAGS="-Ofast" LDFLAGS="-Wl,-O3 -Wl,-S" \
-    RUSTFLAGS="-C target-cpu=native"
+    CFLAGS="-std=gnu2x -Ofast -march=native" \
+    CPPFLAGS="-Ofast -march=native" \
+    LDFLAGS="-Wl,-O3" \
+    RUSTFLAGS="-C target-cpu=native -C panic=abort"
+}
+
+# in a separate fn because ring does not build with LTO
+use_lto () {
+  export \
+    CFLAGS="$CFLAGS -flto=auto -fwhole-program" \
+    CPPFLAGS="$CPPFLAGS -flto=auto -fwhole-program"
 }
 
 # make -j12
diff --git a/build_dig.sh b/build_dig.sh
index 3bb8b40..b9784b9 100755
--- a/build_dig.sh
+++ b/build_dig.sh
@@ -16,6 +16,7 @@ clone https://gitlab.isc.org/isc-projects/bind9.git bind9
 cd bind9
 trap 'cd ..' EXIT
 set_cflags
+use_lto
 
 echo
 echo '==> autoreconf -fi'
diff --git a/build_drill.sh b/build_drill.sh
index 06312d0..83c9cfb 100755
--- a/build_drill.sh
+++ b/build_drill.sh
@@ -17,6 +17,7 @@ clone https://github.com/NLnetLabs/ldns.git ldns
 cd ldns
 trap 'cd ..' EXIT
 set_cflags
+use_lto
 
 echo
 echo '==> libtoolize -ci'