How to build and run test on a Mac?

I just have code downloaded from github, and I am working on a Mac. I just don’t know how to build clang test.

I changed code in llvm-project/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp, and I wrote two test cases for that. How can I build the test binary, and run them? Here is my workspace:

~/Documents/GitHub/llvm-project (add_func_name ✗) ls
bin                           compiler-rt                   libcxxabi                     orc-rt
bolt                          CONTRIBUTING.md               libsycl                       polly
build                         cross-project-tests           libunwind                     pyproject.toml
clang                         examples                      LICENSE.TXT                   README.md
clang-tools-extra             flang                         lld                           runtime
clang.order                   flang-rt                      lldb                          runtimes
cmake                         include                       llvm                          SECURITY.md
cmake_install.cmake           lib                           llvm-libgcc                   tablegen_compile_commands.yml
CMakeCache.txt                libc                          mlir                          third-party
CMakeFiles                    libclc                        offload                       tools
CODE_OF_CONDUCT.md            libcxx                        openmp                        utils

I tried ninja clang in this directory but I got this:

ninja: error: loading 'build.ninja': No such file or directory

What is wrong here? Need help.

I found the solution.

I should build Clang project first, at the same time specify the generator Ninja, then I fount build.ninija file in build directory. Then I can run ninja clang successfully.

The command I use:

cd llvm-project
mkdir build
cd build
cmake -G Ninja \
  -DLLVM_ENABLE_PROJECTS="clang" \
  -DCMAKE_BUILD_TYPE=Release \
  -DLLVM_INCLUDE_TESTS=OFF \
  ../llvm

ninja clang
1 Like

You figured it out I think, but for future reference, Getting Started with the LLVM System — LLVM 22.0.0git documentation for the build part and then LLVM Testing Infrastructure Guide — LLVM 22.0.0git documentation for running tests (it uses make but everything works for ninja as well).

Usually good practice to build and run all tests without any changes so that you have a baseline to work from. But it’s not essential. If you later see failures that seem unrelated to your changes, check whether they were always failing on your machine.

During development it’s best to work out how to run specific tests instead of the whole lot. This is usually llvm-lit <path to the relevant folder>.

If you don’t know what folder that would be, a “chaotic” way to figure it out is to deliberately break the area of code you’re interested in, and see what fails. Or you can look at the git log for changes to the code and see what accompanying tests were changed.

2 Likes

Make sure to enable the ClangIR project in your CMake command -DCLANG_ENABLE_CIR=1, For example, here is my CMake command

cmake -G Ninja \
-DCMAKE_BUILD_TYPE=Debug \
-DLLVM_ENABLE_PROJECTS=“clang;mlir;lld;clang-tools-extra;llvm” \
-DCMAKE_C_COMPILER=clang \
-DLLVM_ENABLE_ASSERTIONS=ON \
-DCMAKE_CXX_COMPILER=clang++ \
-DLLVM_RUNTIME_TARGETS=“AArch64” \
-DLLVM_TARGETS_TO_BUILD=“host” \
-DCLANG_ENABLE_CIR=1 ../llvm

To run a specific lit test, you can use what @DavidSpickett is suggested, and to run full clangir codegen tests, you can run the check-clang-cir-codegen target, for example ninja check-clang-cir-codegen

1 Like

Thanks a lot.

I changed my command like this:

cmake -G Ninja \
	-DCMAKE_BUILD_TYPE=Debug \
	-DLLVM_ENABLE_PROJECTS="clang;mlir;lld;clang-tools-extra;llvm" \
	-DCMAKE_C_COMPILER=clang \
	-DLLVM_ENABLE_ASSERTIONS=ON \
	-DCMAKE_CXX_COMPILER=clang++ \
	-DLLVM_RUNTIME_TARGETS="AArch64" \
	-DLLVM_INCLUDE_TESTS=OFF \
	-DCLANG_ENABLE_CIR=1 ../llvm

This command is OK. And then when I run `ninja clang`, I met some compile errors, most of them are about missing header files, like this:

/opt/homebrew/Cellar/llvm/20.1.5/bin/../include/c++/v1/cerrno:33:5: error: <cerrno> tried including <errno.h> but didn't find libc++'s <errno.h> header.           This usually means that your header search paths are not configured properly.           The header search paths should contain the C++ Standard Library headers before           any C Standard Library, and you are probably using compiler flags that make that           not be the case.
   33 | #   error <cerrno> tried including <errno.h> but didn't find libc++'s <errno.h> header. \
      |     ^
12 errors generated.

It seems something is not configured.

One error is about conflict declaration:

/opt/homebrew/Cellar/llvm/20.1.5/bin/../include/c++/v1/cstdlib:134:9: error: target of using declaration conflicts with declaration already in scope
  134 | using ::abs _LIBCPP_USING_IF_EXISTS;
      |         ^

How can I fix this.

This is a guess, but often on Mac header problems are solved by using xcrun to find the header paths. For instance https://stackoverflow.com/a/67572103. So you could use the environment variable, or add the flags to CMAKE_C_FLAGS and CMAKE_CXX_FLAGS when running cmake.

My understanding is that the system clang knows to do this but one you build yourself won’t. It seems likely one from brew would, but I don’t have much experience with it.

It seems that I have found the solution. I can remember that I run command ninja clang successfully once, by which time my cmake command is:

cmake -G Ninja \
  -DLLVM_ENABLE_PROJECTS="clang" \
  -DCMAKE_BUILD_TYPE=Release \
  -DLLVM_INCLUDE_TESTS=OFF \
  ../llvm

With this, I didn’t enable the ClangIR project, then I should add -DCLANG_ENABLE_CIR=1, correspondingly, I should add mlir in DLLVM_ENABLE_PROJECTS, maybe also some other subprojects. But at the same I added

-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ 

into my command. Then I met the header file issue.

By removing these flags, it seems ninja clang works well, I didn’t see any compile issues so far.

Maybe in my case, there is no need to specify the c/c++ compiler.

@DavidSpickett @amrdeveloper Thanks for your help.

My terminal output after I run ninja clang is as follows:

~/Documents/GitHub/llvm-project/build (add_func_name ✗) ninja clang
[5923/6294] Linking CXX static library lib/libclangCodeGen.a
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: warning duplicate member name 'AMDGPU.cpp.o' from 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/TargetBuiltins/AMDGPU.cpp.o(AMDGPU.cpp.o)' and 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/Targets/AMDGPU.cpp.o(AMDGPU.cpp.o)'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: warning duplicate member name 'ARM.cpp.o' from 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/TargetBuiltins/ARM.cpp.o(ARM.cpp.o)' and 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/Targets/ARM.cpp.o(ARM.cpp.o)'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: warning duplicate member name 'DirectX.cpp.o' from 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/TargetBuiltins/DirectX.cpp.o(DirectX.cpp.o)' and 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/Targets/DirectX.cpp.o(DirectX.cpp.o)'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: warning duplicate member name 'Hexagon.cpp.o' from 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/Targets/Hexagon.cpp.o(Hexagon.cpp.o)' and 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/TargetBuiltins/Hexagon.cpp.o(Hexagon.cpp.o)'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: warning duplicate member name 'NVPTX.cpp.o' from 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/Targets/NVPTX.cpp.o(NVPTX.cpp.o)' and 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/TargetBuiltins/NVPTX.cpp.o(NVPTX.cpp.o)'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: warning duplicate member name 'PPC.cpp.o' from 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/TargetBuiltins/PPC.cpp.o(PPC.cpp.o)' and 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/Targets/PPC.cpp.o(PPC.cpp.o)'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: warning duplicate member name 'RISCV.cpp.o' from 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/Targets/RISCV.cpp.o(RISCV.cpp.o)' and 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/TargetBuiltins/RISCV.cpp.o(RISCV.cpp.o)'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: warning duplicate member name 'SPIR.cpp.o' from 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/Targets/SPIR.cpp.o(SPIR.cpp.o)' and 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/TargetBuiltins/SPIR.cpp.o(SPIR.cpp.o)'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: warning duplicate member name 'SystemZ.cpp.o' from 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/TargetBuiltins/SystemZ.cpp.o(SystemZ.cpp.o)' and 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/Targets/SystemZ.cpp.o(SystemZ.cpp.o)'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: warning duplicate member name 'WebAssembly.cpp.o' from 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/TargetBuiltins/WebAssembly.cpp.o(WebAssembly.cpp.o)' and 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/Targets/WebAssembly.cpp.o(WebAssembly.cpp.o)'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: warning duplicate member name 'X86.cpp.o' from 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/Targets/X86.cpp.o(X86.cpp.o)' and 'tools/clang/lib/CodeGen/CMakeFiles/obj.clangCodeGen.dir/TargetBuiltins/X86.cpp.o(X86.cpp.o)'
[5953/6294] Building CXX object lib/Target/Hexagon/Disassembler/CMakeFiles/LLVMHexagonDisassembler.dir/HexagonDisassembler.cpp.o
[6294/6294] Creating executable symlink bin/clang

Do these have any impact? @amrdeveloper @DavidSpickett

I still can’t run ninja check-clang-cir-codegen successfully:

 ninja: error: unknown target 'check-clang-cir-codegen'

Oh my God…

Can you emit cir on a simple C++ file?

./bin/clang++ -cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cirmain.cpp -o main.cir

@Adrian_Tobar

Just to explain: by removing those CMAKE_<language>_COMPILER options, cmake will pick up your system version of clang instead.

Which is fine a majority of the time, and as you’ve seen, can dodge some problems.

No, you can ignore them. Someone was/is working on avoiding the warning ([MacOS 15][AppleClang] libtool duplicate member name warnings · Issue #133199 · llvm/llvm-project · GitHub).

Seems I can’t.

At first, I added this to avoid llvm-gtest error, with this option, I can’t build the test correctlly. It should be the cause of this problem.