@@ -179,9 +179,87 @@ ifneq ($(SYSROOT),)
179179 KCONFIG_LDFLAGS += --sysroot=$(SYSROOT )
180180endif
181181
182- # Note: LLVM 18 detection for T2C is done inline in Makefile,
183- # guarded by ifeq ($(CONFIG_T2C),y) to avoid expensive shell calls
184- # when T2C is disabled (most builds).
182+ # LLVM Detection for T2C (Tier-2 Compiler)
183+ #
184+ # Supports LLVM 18-21. Detection is deferred until CONFIG_T2C is enabled
185+ # to avoid expensive shell calls in most builds.
186+ #
187+ # User can override: make LLVM_CONFIG=/path/to/llvm-config
188+
189+ # Supported LLVM version range
190+ LLVM_MIN_VERSION := 18
191+ LLVM_MAX_VERSION := 21
192+
193+ # Check if a versioned llvm-config exists in PATH
194+ # Usage: $(call llvm-config-path,VERSION)
195+ define llvm-config-path
196+ $(shell which llvm-config-$(1 ) 2>/dev/null)
197+ endef
198+
199+ # Check if Homebrew LLVM version exists
200+ # Usage: $(call llvm-homebrew-config,VERSION)
201+ define llvm-homebrew-config
202+ $(shell brew --prefix llvm@$(1 ) 2>/dev/null | xargs -I{} sh -c 'test -x {}/bin/llvm-config && echo {}/bin/llvm-config' 2>/dev/null)
203+ endef
204+
205+ # Check generic llvm-config with version validation
206+ # Usage: $(call llvm-config-generic,MIN,MAX)
207+ define llvm-config-generic
208+ $(shell which llvm-config 2>/dev/null | xargs -I{} sh -c 'ver=$$({} --version 2>/dev/null | cut -d. -f1 ) ; [ "$$ver" -ge $(1 ) ] && [ "$$ver" -le $(2 ) ] && echo {}' 2>/dev/null)
209+ endef
210+
211+ # Auto-detect LLVM configuration
212+ # Priority: versioned binaries (18-21), Homebrew versioned, Homebrew generic, generic with version check
213+ # Note: Prefers oldest supported version (18) for stability; override with LLVM_CONFIG= for newer
214+ define detect-llvm-config
215+ $(strip $(or \
216+ $(call llvm-config-path,18) ,\
217+ $(call llvm-config-path,19) ,\
218+ $(call llvm-config-path,20) ,\
219+ $(call llvm-config-path,21) ,\
220+ $(call llvm-homebrew-config,18) ,\
221+ $(call llvm-homebrew-config,19) ,\
222+ $(call llvm-homebrew-config,20) ,\
223+ $(call llvm-homebrew-config,21) ,\
224+ $(shell brew --prefix llvm 2>/dev/null | xargs -I{} sh -c 'test -x {}/bin/llvm-config && echo {}/bin/llvm-config' 2>/dev/null) ,\
225+ $(call llvm-config-generic,$(LLVM_MIN_VERSION ) ,$(LLVM_MAX_VERSION ) ) ) )
226+ endef
227+
228+ # Detect Homebrew LLVM prefix for library path
229+ define detect-homebrew-llvm-prefix
230+ $(strip $(or \
231+ $(shell which brew >/dev/null 2>&1 && brew --prefix llvm@18 2>/dev/null) ,\
232+ $(shell which brew >/dev/null 2>&1 && brew --prefix llvm@19 2>/dev/null) ,\
233+ $(shell which brew >/dev/null 2>&1 && brew --prefix llvm@20 2>/dev/null) ,\
234+ $(shell which brew >/dev/null 2>&1 && brew --prefix llvm@21 2>/dev/null) ,\
235+ $(shell which brew >/dev/null 2>&1 && brew --prefix llvm 2>/dev/null) ) )
236+ endef
237+
238+ # Get LLVM version major number
239+ # Usage: $(call llvm-version,LLVM_CONFIG_PATH)
240+ define llvm-version
241+ $(shell $(1 ) --version 2>/dev/null | cut -d. -f1)
242+ endef
243+
244+ # Check if LLVM libraries are available
245+ # Usage: $(call llvm-check-libs,LLVM_CONFIG_PATH)
246+ # Returns: "0" (string) on success, non-zero string on failure
247+ # Example: ifeq ($(call llvm-check-libs,$(LLVM_CONFIG)),0)
248+ define llvm-check-libs
249+ $(shell $(1 ) --libs 2>/dev/null 1>&2; echo $$? )
250+ endef
251+
252+ # Get LLVM compiler flags
253+ # Usage: $(call llvm-cflags,LLVM_CONFIG_PATH)
254+ define llvm-cflags
255+ $(shell $(1 ) --cflags 2>/dev/null)
256+ endef
257+
258+ # Get LLVM library files for linking
259+ # Usage: $(call llvm-libfiles,LLVM_CONFIG_PATH)
260+ define llvm-libfiles
261+ $(shell $(1 ) --libfiles 2>/dev/null)
262+ endef
185263
186264# Version Comparison Utilities
187265
0 commit comments