title: How do I get cmake to use my Homebrew installation of llvm exclusively?
body: <p>My goal is to alias my cmake command so that it uses my homebrew installation of llvm exclusively, and not the suite installed by Xcode. I would like to leave the system defaults as defaults as well. I am on Catalina with Xcode and homebrew llvm up to date.</p>
  
  <p>My first step is to use the homebrew clang++ and libc++, and I can do it manually </p>
  
  <pre><code>/usr/local/opt/llvm/bin/clang++ -Wall -Wextra -std=c++17 -stdlib=libc++ \
  -nostdinc++ -I/usr/local/opt/llvm/include/c++/v1 \
  -L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib \
  main.cc
  </code></pre>
  
  <p>For completion's sake, here's my CMakeLists.txt; it's pretty barebones as most of the work I want to do doesn't belong in CMakeLists.txt and I'm just trying to compile a toy program.</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.15)
  
  project(test)
  
  set(CMAKE_CXX_STANDARD 17)
  
  add_executable(test
                 main.cc)
  </code></pre>
  
  <p>But I'm already stuck with cmake here. I can specify the compiler using<br>
  <code>-DCMAKE_CXX_COMPILER="/usr/local/opt/llvm/bin/clang++</code> and similar for C/clang. I am using the <code>CMAKE_PREFIX_PATH</code> as well, but when I open my <code>build.ninja</code> files, I still see it referencing Xcode directories. Adding a <code>CMAKE_IGNORE_PATH</code> didn't seem to get it to actually ignore /Applications/Xcode.app.</p>
  
  <p>I have also tried adding the flags <code>CMAKE_CXX_STANDARD_LIBRARIES</code> and <code>CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES</code> to my cmake command, but if I do this I start to get linker errors (still using the system linker and not homebrew's ld64.lld, I believe). </p>
  
  <p>Here is my current working cmake command as a reference:</p>
  
  <pre><code>cmake -DCMAKE_PREFIX_PATH="/usr/local/opt/llvm" \
  -DCMAKE_CXX_COMPILER="/usr/local/opt/llvm/bin/clang++" \
  -DCMAKE_C_COMPILER="/usr/local/opt/llvm/bin/clang" -G Ninja ..
  </code></pre>
  
  <p>The specific compiler paths are still required because cmake just breezes straight to AppleClang.</p>
  
  <p>I know that the path repetition is annoying, but I want a full command to work before I start modifying my .zshrc. </p>
  
  <p>Here's the broken command (broken as in building the program no longer succeeds, cmake doesn't complain here):</p>
  
  <pre><code>cmake -DCMAKE_IGNORE_PATH="/Applications/Xcode.app" -DCMAKE_PREFIX_PATH="/usr/local/opt/llvm" \
  -DCMAKE_CXX_COMPILER="/usr/local/opt/llvm/bin/clang++" \
  -DCMAKE_C_COMPILER="/usr/local/opt/llvm/bin/clang" \
  -DCMAKE_CXX_STANDARD_LIBRARIES="/usr/local/opt/llvm/lib" \
  -DCMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES="/usr/local/opt/llvm/include/c++/v1" \
  -G Ninja ..
  </code></pre>
  
  <p>With the build error message:</p>
  
  <pre><code>[2/2] Linking CXX executable test
  FAILED: test 
  : &amp;&amp; /usr/local/opt/llvm/bin/clang++  -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/test.dir/main.cc.o  -o test  /usr/local/opt/llvm/lib &amp;&amp; :
  ld: can't map file, errno=22 file '/usr/local/opt/llvm/lib' for architecture x86_64
  clang-9: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja: build stopped: subcommand failed.
  </code></pre>
  
  <p>The variable <code>CMAKE_CXX_LINK_EXECUTABLE</code> just gets ignored, so it's not included.</p>
  
  <p>If anyone has any guidance, I would appreciate it. All links I have found about this usually override the system defaults, and I would like to avoid doing that if possible.</p>