diff --git a/tools/bootconfig/Makefile b/tools/bootconfig/Makefile
index 566c3e0ee561..605c0798a83d 100644
--- a/tools/bootconfig/Makefile
+++ b/tools/bootconfig/Makefile
@@ -5,8 +5,8 @@ include ../scripts/Makefile.include
 bindir ?= /usr/bin
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
 endif
 
 LIBSRC = $(srctree)/lib/bootconfig.c $(srctree)/include/linux/bootconfig.h
diff --git a/tools/bpf/Makefile b/tools/bpf/Makefile
index 243b79f2b451..b12ff0126cf6 100644
--- a/tools/bpf/Makefile
+++ b/tools/bpf/Makefile
@@ -23,8 +23,8 @@ ifndef building_out_of_srctree
 update_srctree := 1
 endif
 ifeq ($(update_srctree),1)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
 endif
 
 ifeq ($(V),1)
diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile
index e9154ace80ff..34b55ae53d0a 100644
--- a/tools/bpf/bpftool/Makefile
+++ b/tools/bpf/bpftool/Makefile
@@ -2,9 +2,9 @@
 include ../../scripts/Makefile.include
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
+srctree := $(dir $(srctree:%/=%))
 endif
 
 ifeq ($(V),1)
diff --git a/tools/build/Makefile b/tools/build/Makefile
index 17cdf01e29a0..cdebad6f1bfb 100644
--- a/tools/build/Makefile
+++ b/tools/build/Makefile
@@ -1,7 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
 endif
 
 include $(srctree)/tools//scripts/Makefile.include
diff --git a/tools/counter/Makefile b/tools/counter/Makefile
index d82d35a520f6..3d23639fa8fc 100644
--- a/tools/counter/Makefile
+++ b/tools/counter/Makefile
@@ -4,8 +4,8 @@ include ../scripts/Makefile.include
 bindir ?= /usr/bin
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
 endif
 
 # Do not use make's built-in rules
diff --git a/tools/gpio/Makefile b/tools/gpio/Makefile
index d29c9c49e251..0828ee9e8a16 100644
--- a/tools/gpio/Makefile
+++ b/tools/gpio/Makefile
@@ -8,8 +8,8 @@ bindir ?= /usr/bin
 # is set to ".". building_out_of_srctree is undefined for in srctree
 # builds
 ifndef building_out_of_srctree
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
 endif
 
 # Do not use make's built-in rules
diff --git a/tools/hv/Makefile b/tools/hv/Makefile
index fe770e679ae8..6b9f42743c57 100644
--- a/tools/hv/Makefile
+++ b/tools/hv/Makefile
@@ -7,8 +7,8 @@ libexecdir ?= /usr/libexec
 sharedstatedir ?= /var/lib
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
 endif
 
 # Do not use make's built-in rules
diff --git a/tools/iio/Makefile b/tools/iio/Makefile
index fa720f062229..f4f64a152457 100644
--- a/tools/iio/Makefile
+++ b/tools/iio/Makefile
@@ -4,8 +4,8 @@ include ../scripts/Makefile.include
 bindir ?= /usr/bin
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
 endif
 
 # Do not use make's built-in rules
diff --git a/tools/include/nolibc/Makefile b/tools/include/nolibc/Makefile
index e69c26abe1ea..372a17bf7d36 100644
--- a/tools/include/nolibc/Makefile
+++ b/tools/include/nolibc/Makefile
@@ -4,7 +4,7 @@ include ../../scripts/Makefile.include
 
 # we're in ".../tools/include/nolibc"
 ifeq ($(srctree),)
-srctree := $(patsubst %/tools/include/,%,$(dir $(CURDIR)))
+srctree := $(patsubst %tools/include/,%,$(dir $(CURDIR)))
 endif
 
 # when run as make -C tools/ nolibc_<foo> the arch is not set
diff --git a/tools/lib/api/Makefile b/tools/lib/api/Makefile
index 044860ac1ed1..74e774984d83 100644
--- a/tools/lib/api/Makefile
+++ b/tools/lib/api/Makefile
@@ -3,9 +3,9 @@ include ../../scripts/Makefile.include
 include ../../scripts/utilities.mak		# QUIET_CLEAN
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
+srctree := $(dir $(srctree:%/=%))
 #$(info Determined 'srctree' to be $(srctree))
 endif
 
diff --git a/tools/lib/bpf/Makefile b/tools/lib/bpf/Makefile
index 4be7144e4803..66970cf138fc 100644
--- a/tools/lib/bpf/Makefile
+++ b/tools/lib/bpf/Makefile
@@ -18,9 +18,9 @@ MAKEFLAGS += --no-print-directory
 # is a ".". building_out_of_srctree is undefined for in srctree
 # builds
 ifndef building_out_of_srctree
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
+srctree := $(dir $(srctree:%/=%))
 #$(info Determined 'srctree' to be $(srctree))
 endif
 
diff --git a/tools/lib/perf/Makefile b/tools/lib/perf/Makefile
index 3a9b2140aa04..3e998b2af153 100644
--- a/tools/lib/perf/Makefile
+++ b/tools/lib/perf/Makefile
@@ -8,9 +8,9 @@ LIBPERF_EXTRAVERSION = 1
 MAKEFLAGS += --no-print-directory
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
+srctree := $(dir $(srctree:%/=%))
 #$(info Determined 'srctree' to be $(srctree))
 endif
 
diff --git a/tools/lib/subcmd/Makefile b/tools/lib/subcmd/Makefile
index b87213263a5e..fdd0bf03fd6d 100644
--- a/tools/lib/subcmd/Makefile
+++ b/tools/lib/subcmd/Makefile
@@ -3,9 +3,9 @@ include ../../scripts/Makefile.include
 include ../../scripts/utilities.mak		# QUIET_CLEAN
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
+srctree := $(dir $(srctree:%/=%))
 #$(info Determined 'srctree' to be $(srctree))
 endif
 
diff --git a/tools/lib/symbol/Makefile b/tools/lib/symbol/Makefile
index 13d43c6f92b4..1f2390d1c0e0 100644
--- a/tools/lib/symbol/Makefile
+++ b/tools/lib/symbol/Makefile
@@ -3,9 +3,9 @@ include ../../scripts/Makefile.include
 include ../../scripts/utilities.mak		# QUIET_CLEAN
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
+srctree := $(dir $(srctree:%/=%))
 #$(info Determined 'srctree' to be $(srctree))
 endif
 
diff --git a/tools/lib/thermal/Makefile b/tools/lib/thermal/Makefile
index 2d0d255fd0e1..8c78b2540156 100644
--- a/tools/lib/thermal/Makefile
+++ b/tools/lib/thermal/Makefile
@@ -8,9 +8,9 @@ LIBTHERMAL_EXTRAVERSION = 1
 MAKEFLAGS += --no-print-directory
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
+srctree := $(dir $(srctree:%/=%))
 # $(info Determined 'srctree' to be $(srctree))
 endif
 
diff --git a/tools/objtool/Makefile b/tools/objtool/Makefile
index 83b100c1e7f6..714bf362b59c 100644
--- a/tools/objtool/Makefile
+++ b/tools/objtool/Makefile
@@ -3,8 +3,8 @@ include ../scripts/Makefile.include
 include ../scripts/Makefile.arch
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
 endif
 
 LIBSUBCMD_DIR = $(srctree)/tools/lib/subcmd/
diff --git a/tools/pci/Makefile b/tools/pci/Makefile
index 57744778b518..913c9d29bfb2 100644
--- a/tools/pci/Makefile
+++ b/tools/pci/Makefile
@@ -4,8 +4,8 @@ include ../scripts/Makefile.include
 bindir ?= /usr/bin
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
 endif
 
 # Do not use make's built-in rules
diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index f8774a9b1377..4ff08babcecc 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -144,8 +144,8 @@ LC_NUMERIC=C
 export LC_COLLATE LC_NUMERIC
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
 #$(info Determined 'srctree' to be $(srctree))
 endif
 
diff --git a/tools/power/acpi/Makefile.config b/tools/power/acpi/Makefile.config
index cd7106876a5f..7df61e8ad7be 100644
--- a/tools/power/acpi/Makefile.config
+++ b/tools/power/acpi/Makefile.config
@@ -6,8 +6,8 @@
 #
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(shell pwd)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
 #$(info Determined 'srctree' to be $(srctree))
 endif
 
diff --git a/tools/power/x86/intel-speed-select/Makefile b/tools/power/x86/intel-speed-select/Makefile
index 7221f2f55e8b..fcb13e08effe 100644
--- a/tools/power/x86/intel-speed-select/Makefile
+++ b/tools/power/x86/intel-speed-select/Makefile
@@ -4,10 +4,10 @@ include ../../../scripts/Makefile.include
 bindir ?= /usr/bin
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
+srctree := $(dir $(srctree:%/=%))
+srctree := $(dir $(srctree:%/=%))
 endif
 
 # Do not use make's built-in rules
diff --git a/tools/spi/Makefile b/tools/spi/Makefile
index 7fccd245a535..c511864f270b 100644
--- a/tools/spi/Makefile
+++ b/tools/spi/Makefile
@@ -4,8 +4,8 @@ include ../scripts/Makefile.include
 bindir ?= /usr/bin
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
 endif
 
 # Do not use make's built-in rules
diff --git a/tools/testing/selftests/nolibc/Makefile b/tools/testing/selftests/nolibc/Makefile
index 40dd95228051..acfd59dcba56 100644
--- a/tools/testing/selftests/nolibc/Makefile
+++ b/tools/testing/selftests/nolibc/Makefile
@@ -13,7 +13,7 @@ endif
 
 # we're in ".../tools/testing/selftests/nolibc"
 ifeq ($(srctree),)
-srctree := $(patsubst %/tools/testing/selftests/,%,$(dir $(CURDIR)))
+srctree := $(patsubst %tools/testing/selftests/,%,$(dir $(CURDIR)))
 endif
 
 ifeq ($(ARCH),)
diff --git a/tools/thermal/lib/Makefile b/tools/thermal/lib/Makefile
index 82db451935c5..f5323c5a9ca0 100644
--- a/tools/thermal/lib/Makefile
+++ b/tools/thermal/lib/Makefile
@@ -8,9 +8,9 @@ LIBTHERMAL_TOOLS_EXTRAVERSION = 1
 MAKEFLAGS += --no-print-directory
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
+srctree := $(dir $(srctree:%/=%))
 # $(info Determined 'srctree' to be $(srctree))
 endif
 
diff --git a/tools/thermal/thermal-engine/Makefile b/tools/thermal/thermal-engine/Makefile
index 6bd05ff89485..5bc3bc70a418 100644
--- a/tools/thermal/thermal-engine/Makefile
+++ b/tools/thermal/thermal-engine/Makefile
@@ -2,9 +2,9 @@
 # Makefile for thermal tools
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
+srctree := $(dir $(srctree:%/=%))
 # $(info Determined 'srctree' to be $(srctree))
 endif
 
diff --git a/tools/thermal/thermometer/Makefile b/tools/thermal/thermometer/Makefile
index d8f8bc82fe3b..624359d3503c 100644
--- a/tools/thermal/thermometer/Makefile
+++ b/tools/thermal/thermometer/Makefile
@@ -2,9 +2,9 @@
 # Makefile for cgroup tools
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=%))
+srctree := $(dir $(srctree:%/=%))
 # $(info Determined 'srctree' to be $(srctree))
 endif
 
diff --git a/tools/usb/Makefile b/tools/usb/Makefile
index c6235667dd46..6f80415d04be 100644
--- a/tools/usb/Makefile
+++ b/tools/usb/Makefile
@@ -5,8 +5,8 @@ include ../scripts/Makefile.include
 bindir ?= /usr/bin
 
 ifeq ($(srctree),)
-srctree := $(patsubst %/,%,$(dir $(CURDIR)))
-srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(dir $(CURDIR))
+srctree := $(dir $(srctree:%/=$))
 endif
 
 # Do not use make's built-in rules
