Getting compilation issue with packages like perl and xtables addons while using external toolchain for compilation

I am trying to create an external toolchain setup for my Intel based Virtual machine with the following steps.

[M3-openwrt-23.05-toy](awolf_23.05.2_initrd) make menuconfig
 [ ]   Target Options  ----                                                                                          
       BPF toolchain (Build LLVM toolchain for eBPF)  --->                                                           
 [*]   Use external toolchain  --->  
 
     --- Use external toolchain                                                                                  
         [ ]   Use host's toolchain                                                                                  
         (x86_64-m3-linux-gnu) Target name                                                                           
         (/home/cmuthusamy/opt/openwrt/openwrt-sdk-23.05.2-x86-64_gcc-12.3.0_glibc.Linux-x86_64/staging_dir/toolchain-x86_64_gcc-12.3.0_glibc/bin/x86_64-openwrt-linux-gnu-) Toolchain prefix
         (/home/cmuthusamy/opt/openwrt/openwrt-sdk-23.05.2-x86-64_gcc-12.3.0_glibc.Linux-x86_64/staging_dir/toolchain-x86_64_gcc-12.3.0_glibc) Toolchain root
         Toolchain libc (glibc)  --->                                                                                
         (./usr/bin ./bin) Toolchain program path                                                                    
         (./usr/include ./include) Toolchain include path                                                                     
         (./usr/lib ./lib) Toolchain library path

I am getting the following compilation errors one by one when I use the external toolchain for compilation, but I am not getting this compilation error when using the built-in internal toolchain. Can someone please clarify what's wrong with my external toolchain setup ?

ERROR: package/feeds/packages/perl failed to build.
make -r world: build failed. Please re-run make with -j1 V=s or V=sc for a higher verbosity level to see what's going on 
 
make package/perl/compile -j1 V=s
 
LD_LIBRARY_PATH=/home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/build_dir/target-x86_64-m3-linux-gnu_glibc/perl/perl-5.28.1 ./miniperl -Ilib -I. make_ext.pl ext/Errno/pm_to_blib  MAKE="make" LIBPERL_A=libperl.so
make[4]: Entering directory '/home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/build_dir/target-x86_64-m3-linux-gnu_glibc/perl/perl-5.28.1/ext/Errno'
"../../../../../../staging_dir/hostpkg/usr/bin/perl" "-I../../lib" Errno_pm.PL Errno.pm
Cannot find errno.h at Errno_pm.PL line 138.
make[4]: *** [Makefile:429: Errno.pm] Error 2
make[4]: Leaving directory '/home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/build_dir/target-x86_64-m3-linux-gnu_glibc/perl/perl-5.28.1/ext/Errno'
make[4]: Entering directory '/home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/build_dir/target-x86_64-m3-linux-gnu_glibc/perl/perl-5.28.1/ext/Errno'
"../../../../../../staging_dir/hostpkg/usr/bin/perl" "-I../../lib" Errno_pm.PL Errno.pm
Cannot find errno.h at Errno_pm.PL line 138.
make[4]: *** [Makefile:429: Errno.pm] Error 2
make[4]: Leaving directory '/home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/build_dir/target-x86_64-m3-linux-gnu_glibc/perl/perl-5.28.1/ext/Errno'
Unsuccessful make(ext/Errno): code=512 at make_ext.pl line 570.
make[3]: *** [makefile:585: ext/Errno/pm_to_blib] Error 2
make[3]: Leaving directory '/home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/build_dir/target-x86_64-m3-linux-gnu_glibc/perl/perl-5.28.1'
make[2]: *** [Makefile:154: /home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/build_dir/target-x86_64-m3-linux-gnu_glibc/perl/perl-5.28.1/.built] Error 2
make[2]: Leaving directory '/home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/feeds/packages/lang/perl'
time: package/feeds/packages/perl/compile#1.42#0.58#1.96
    ERROR: package/feeds/packages/perl failed to build.
make[1]: *** [package/Makefile:128: package/feeds/packages/perl/compile] Error 1
make[1]: Leaving directory '/home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy'
ERROR: package/feeds/packages/xtables-addons failed to build.
 
make[5]: Entering directory '/home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/build_dir/target-x86_64-m3-linux-gnu_glibc/linux-x86_64/xtables-addons-3.24/extensions'
if [ -n "/home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/build_dir/target-x86_64-m3-linux-gnu_glibc/linux-x86_64/linux-5.15.137" ]; then make -C /home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/build_dir/target-x86_64-m3-linux-gnu_glibc/linux-x86_64/linux-5.15.137 M=/home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/build_dir/target-x86_64-m3-linux-gnu_glibc/linux-x86_64/xtables-addons-3.24/extensions INSTALL_MOD_PATH= ext-mod-dir='${INSTALL_MOD_DIR}' modules_install; fi;
make[6]: Entering directory '/home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/build_dir/target-x86_64-m3-linux-gnu_glibc/linux-x86_64/linux-5.15.137'
  INSTALL /lib/modules/5.15.137/extra/ACCOUNT/xt_ACCOUNT.ko
mkdir: cannot create directory '/lib/modules/5.15.137': Permission denied
make[7]: *** [scripts/Makefile.modinst:81: /lib/modules/5.15.137/extra/ACCOUNT/xt_ACCOUNT.ko] Error 1
make[6]: *** [Makefile:1838: modules_install] Error 2
make[6]: Leaving directory '/home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/build_dir/target-x86_64-m3-linux-gnu_glibc/linux-x86_64/linux-5.15.137'
make[5]: *** [Makefile:471: modules_install] Error 2
make[5]: Leaving directory '/home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/build_dir/target-x86_64-m3-linux-gnu_glibc/linux-x86_64/xtables-addons-3.24/extensions'
make[4]: *** [Makefile:354: install-am] Error 2
make[4]: Leaving directory '/home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/build_dir/target-x86_64-m3-linux-gnu_glibc/linux-x86_64/xtables-addons-3.24/extensions'
make[3]: *** [Makefile:625: install-recursive] Error 1
make[3]: Leaving directory '/home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/build_dir/target-x86_64-m3-linux-gnu_glibc/linux-x86_64/xtables-addons-3.24'
make[2]: *** [Makefile:179: /home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/build_dir/target-x86_64-m3-linux-gnu_glibc/linux-x86_64/xtables-addons-3.24/.built] Error 2
make[2]: Leaving directory '/home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy/feeds/packages/net/xtables-addons'
time: package/feeds/packages/xtables-addons/compile#2.89#2.11#4.86
    ERROR: package/feeds/packages/xtables-addons failed to build.
make[1]: *** [package/Makefile:128: package/feeds/packages/xtables-addons/compile] Error 1
make[1]: Leaving directory '/home/cmuthusamy/OpenWrt_Toy/external_toolchain/M3-openwrt-23.05-toy'
 
This package compilation error alone can be fixed by "export INSTALL_MOD_PATH="./lib/modules/5.15.137"", however linux compilation fails with this env variable exported.

For xtables addons, you need to backport this to 23.05:

Why am not facing this issue with internal toolchain. Getting these kind of many such compilation issues only with external toolchain.

Read the commit description

Thanks @robimarko I will use this patch for resolving xtables-addons . Will it resolve the compilation error for other packages like perl which I posted in the original question ?

No, I doubt that will fix perl

Oh in that case I need to search for some similar patch like we had for xtables-addons. So has anyone tested and got success in using the external toolchain option like using the external toolchain for compiling Openwrt packages ??