@@ -134,23 +134,12 @@ def _find_resource_dir() -> str:
134134 """Find clang resource directory.
135135
136136 1. LLVM_RESOURCE_DIR env var
137- 2. On macOS, prefer brew's clang over Xcode's (avoids libclang/resource-dir mismatch)
138- 3. `clang -print-resource-dir` fallback
137+ 2. `clang -print-resource-dir` via PATH or common LLVM installation directories
139138 """
140139 env = os .environ .get ("LLVM_RESOURCE_DIR" )
141140 if env :
142141 return env
143142
144- # On macOS, prefer brew's clang to match brew's libclang
145- brew_clang = "/opt/homebrew/opt/llvm/bin/clang"
146- if IS_MACOS and os .path .isfile (brew_clang ):
147- try :
148- result = subprocess .check_output ([brew_clang , "-print-resource-dir" ], text = True ).strip ()
149- if result :
150- return result
151- except Exception :
152- pass
153-
154143 clang_path = shutil .which ("clang" ) or next (
155144 (shutil .which (f"clang-{ v } " ) for v in range (99 , 10 , - 1 )),
156145 None ,
@@ -198,8 +187,24 @@ def _find_libclang() -> str:
198187 candidates : list [str ] = []
199188
200189 if IS_MACOS :
201- candidates = [
202- "/opt/homebrew/opt/llvm/lib/libclang.dylib" ,
190+ # Try llvm-config --libdir first (find any version with glob)
191+ llvm_config = shutil .which ("llvm-config" )
192+ if not llvm_config :
193+ for v in range (99 , 10 , - 1 ):
194+ p = shutil .which (f"llvm-config-{ v } " )
195+ if p :
196+ llvm_config = p
197+ break
198+ if llvm_config :
199+ try :
200+ libdir = subprocess .check_output ([llvm_config , "--libdir" ], text = True ).strip ()
201+ libdir_p = Path (libdir )
202+ if libdir_p .is_dir ():
203+ for f in sorted (libdir_p .glob ("libclang*.dylib" ), reverse = True ):
204+ candidates .append (str (f ))
205+ except Exception :
206+ pass
207+ candidates += [
203208 "/usr/local/opt/llvm/lib/libclang.dylib" ,
204209 "/Library/Developer/CommandLineTools/usr/lib/libclang.dylib" ,
205210 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libclang.dylib" ,
@@ -251,7 +256,7 @@ def _find_libclang() -> str:
251256
252257import clang .cindex as ci # noqa: E402 # runtime dep from uv, not system
253258
254- # Point clang at brew's libclang (or env-overridden path )
259+ # Point clang at libclang (env var CLANG_LIBRARY_PATH overrides auto-detection )
255260ci .Config .set_library_file (LIBCLANG_PATH )
256261
257262_INDEX = ci .Index .create ()
@@ -339,6 +344,20 @@ def discover_project_includes(project_root: str) -> list[str]:
339344# ── parsing ────────────────────────────────────────────────────────
340345
341346
347+ def _find_libcxx_include () -> str | None :
348+ """Find libc++ include dir alongside the detected libclang path."""
349+ try :
350+ libclang = Path (LIBCLANG_PATH ).resolve ()
351+ except Exception :
352+ return None
353+ # Walk up from libclang path looking for include/c++/v1
354+ for parent in libclang .parents :
355+ candidate = parent / "include" / "c++" / "v1"
356+ if candidate .is_dir ():
357+ return str (candidate )
358+ return None
359+
360+
342361def _build_clang_args (
343362 extra_includes : list [str ] | None = None ,
344363) -> list [str ]:
@@ -354,12 +373,12 @@ def _build_clang_args(
354373 "-x" ,
355374 "c++" ,
356375 ]
357- # On macOS, ensure brew's libc++ headers are found (libclang may not
376+ # On macOS, ensure libc++ headers are found (libclang may not
358377 # auto-detect the SDK the same way the clang driver does).
359378 if IS_MACOS :
360- _brew_cxx = "/opt/homebrew/opt/llvm/include/c++/v1"
361- if os . path . isdir ( _brew_cxx ) :
362- args .extend (["-cxx-isystem" , _brew_cxx ])
379+ _llvm_cxx = _find_libcxx_include ()
380+ if _llvm_cxx :
381+ args .extend (["-cxx-isystem" , _llvm_cxx ])
363382 for inc in itertools .chain (
364383 GR4_INCLUDE_PATHS , PROJECT_INCLUDE_PATHS , extra_includes or []
365384 ):
@@ -1473,7 +1492,7 @@ def main() -> int:
14731492 print (f" { line } " , file = sys .stderr )
14741493 except FileNotFoundError :
14751494 print (
1476- "cue not found (install: brew install cue-lang/tap/cue )" ,
1495+ "cue not found (see https://cuelang.org/docs/install/ )" ,
14771496 file = sys .stderr ,
14781497 )
14791498 finally :
0 commit comments