From a7ae4ed0a3951c45d4a59ee575951b64ae4a23fb Mon Sep 17 00:00:00 2001
From: Felix Fietkau <nbd@nbd.name>
Date: Tue, 7 May 2024 12:22:15 +0200
Subject: [PATCH] kernel: fix tools build breakage on macos with x86

Signed-off-by: Felix Fietkau <nbd@nbd.name>
---
--- a/tools/scripts/Makefile.include
+++ b/tools/scripts/Makefile.include
@@ -72,8 +72,6 @@ $(call allow-override,CXX,$(CROSS_COMPIL
 $(call allow-override,STRIP,$(CROSS_COMPILE)strip)
 endif
 
-CC_NO_CLANG := $(shell $(CC) -dM -E -x c /dev/null | grep -Fq "__clang__"; echo $$?)
-
 ifneq ($(LLVM),)
 HOSTAR  ?= $(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX)
 HOSTCC  ?= $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
@@ -84,6 +82,9 @@ HOSTCC  ?= gcc
 HOSTLD  ?= ld
 endif
 
+CC_NO_CLANG := $(shell $(CC) -dM -E -x c /dev/null | grep -Fq "__clang__"; echo $$?)
+HOSTCC_NO_CLANG := $(shell $(HOSTCC) -dM -E -x c /dev/null | grep -Fq "__clang__"; echo $$?)
+
 # Some tools require Clang, LLC and/or LLVM utils
 CLANG		?= clang
 LLC		?= llc
@@ -95,8 +96,9 @@ LLVM_STRIP	?= llvm-strip
 SYSTEM_BPFTOOL	?= bpftool
 
 ifeq ($(CC_NO_CLANG), 1)
-EXTRA_WARNINGS += -Wstrict-aliasing=3
-
+  ifeq ($(HOSTCC_NO_CLANG), 1)
+    EXTRA_WARNINGS += -Wstrict-aliasing=3
+  endif
 else ifneq ($(CROSS_COMPILE),)
 # Allow userspace to override CLANG_CROSS_FLAGS to specify their own
 # sysroots and flags or to avoid the GCC call in pure Clang builds.
--- a/tools/include/linux/types.h
+++ b/tools/include/linux/types.h
@@ -58,6 +58,7 @@ typedef unsigned long long	ullong;
 #define __user
 #endif
 #define __must_check
+#undef __cold
 #define __cold
 
 typedef __u16 __bitwise __le16;
--- a/tools/objtool/include/objtool/objtool.h
+++ b/tools/objtool/include/objtool/objtool.h
@@ -12,6 +12,7 @@
 
 #include <objtool/elf.h>
 
+#undef __weak
 #define __weak __attribute__((weak))
 
 struct pv_state {
--- a/tools/include/asm-generic/bitops/fls.h
+++ b/tools/include/asm-generic/bitops/fls.h
@@ -2,6 +2,8 @@
 #ifndef _ASM_GENERIC_BITOPS_FLS_H_
 #define _ASM_GENERIC_BITOPS_FLS_H_
 
+#include <string.h>
+
 /**
  * generic_fls - find last (most-significant) bit set
  * @x: the word to search
@@ -10,6 +12,7 @@
  * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  */
 
+#define generic_fls __linux_fls
 static __always_inline __attribute_const__ int generic_fls(unsigned int x)
 {
 	int r = 32;
--- a/tools/lib/string.c
+++ b/tools/lib/string.c
@@ -96,6 +96,7 @@ int strtobool(const char *s, bool *res)
  * If libc has strlcpy() then that version will override this
  * implementation:
  */
+#ifndef __APPLE__
 #ifdef __clang__
 #pragma clang diagnostic push
 #pragma clang diagnostic ignored "-Wignored-attributes"
@@ -114,6 +115,7 @@ size_t __weak strlcpy(char *dest, const
 #ifdef __clang__
 #pragma clang diagnostic pop
 #endif
+#endif
 
 /**
  * skip_spaces - Removes leading whitespace from @str.
--- a/tools/arch/x86/include/asm/insn.h
+++ b/tools/arch/x86/include/asm/insn.h
@@ -7,7 +7,7 @@
  * Copyright (C) IBM Corporation, 2009
  */
 
-#include <asm/byteorder.h>
+#include <linux/kernel.h>
 /* insn_attr_t is defined in inat.h */
 #include "inat.h" /* __ignore_sync_check__ */
 
--- a/tools/arch/x86/include/asm/orc_types.h
+++ b/tools/arch/x86/include/asm/orc_types.h
@@ -46,7 +46,17 @@
 #define ORC_TYPE_REGS_PARTIAL		4
 
 #ifndef __ASSEMBLER__
+#ifdef __APPLE__
+#include <endian.h>
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#define __LITTLE_ENDIAN_BITFIELD
+#elif __BYTE_ORDER == __BIG_ENDIAN
+#define __BIG_ENDIAN_BITFIELD
+#endif
+#else
 #include <asm/byteorder.h>
+#endif
 
 /*
  * This struct is more or less a vastly simplified version of the DWARF Call
--- a/tools/include/linux/rbtree.h
+++ b/tools/include/linux/rbtree.h
@@ -18,7 +18,6 @@
 #define __TOOLS_LINUX_PERF_RBTREE_H
 
 #include <linux/kernel.h>
-#include <linux/stddef.h>
 
 struct rb_node {
 	unsigned long  __rb_parent_color;
--- a/tools/lib/subcmd/exec-cmd.c
+++ b/tools/lib/subcmd/exec-cmd.c
@@ -12,7 +12,10 @@
 #include "subcmd-config.h"
 
 #define MAX_ARGS	32
+
+#ifndef PATH_MAX
 #define PATH_MAX	4096
+#endif
 
 static const char *argv_exec_path;
 static const char *argv0_path;
--- a/tools/objtool/Makefile
+++ b/tools/objtool/Makefile
@@ -42,6 +42,8 @@ OBJTOOL_LDFLAGS := $(LIBELF_LIBS) $(LIBS
 elfshdr := $(shell echo '$(pound)include <libelf.h>' | $(HOSTCC) $(OBJTOOL_CFLAGS) -x c -E - 2>/dev/null | grep elf_getshdr)
 OBJTOOL_CFLAGS += $(if $(elfshdr),,-DLIBELF_USE_DEPRECATED)
 
+OBJTOOL_CFLAGS += $(HOST_EXTRACFLAGS)
+
 # Always want host compilation.
 HOST_OVERRIDES := CC="$(HOSTCC)" LD="$(HOSTLD)" AR="$(HOSTAR)"
 
--- a/tools/arch/x86/lib/insn.c
+++ b/tools/arch/x86/lib/insn.c
@@ -15,7 +15,11 @@
 #include "../include/asm/insn.h" /* __ignore_sync_check__ */
 #include <linux/unaligned.h> /* __ignore_sync_check__ */
 
+#ifdef __KERNEL__
 #include <linux/errno.h>
+#else
+#include <errno.h>
+#endif
 #include <linux/kconfig.h>
 
 #include "../include/asm/emulate_prefix.h" /* __ignore_sync_check__ */
