Building clang + LLVM on macOS Sierra

Hi everyone,

This is my first post here on cfe, so please be kind – I apologize if some of these issues have already been discussed previously on the list. I’m building clang + LLVM mostly following the instructions at http://clang.llvm.org/get_started.html and http://llvm.org/docs/CMake.html. The goal is for me to be able to produce a clang installation that I can use and play with locally on my MBP.

When building LLVM + clang, I had to manually add the include paths for Valgrind, libxml2, and Python (currently just using -DCMAKE_C_FLAGS and -DCMAKE_CXX_FLAGS when invoking cmake for configuration). Is this intended, or should the cmake scripts be able to automatically discover these dependencies? (I believe the header include errors occurred when attempting to compile lldb).

After making these changes, an installation of clang is produced, but it does not seem to know about the standard library headers – e.g. compilation fails when attempting to include <stdio.h>. (Note that this build was produced with ‘libcxx’ available in the ‘llvm/projects’ subdirectory). This did seem to work automagically when building with clang-3.9; has anything changed here? Is there some option I can set that says “please install + use the version of libc++ in the projects directory by default”? For reference:

$ clang-4.0 -E - -v < /dev/null
clang version 4.0.0 (http://llvm.org/git/clang.git 2f1019880e3fca33b8c7ac8f8bd272e546d930ac) (http://llvm.org/git/llvm.git 20bb0322fd381dd7e265eaa05b7fb1c292278abe)
Target: x86_64-apple-darwin16.0.0
Thread model: posix
InstalledDir: /usr/local/bin
“/usr/local/llvm/bin/clang-4.0” -cc1 -triple x86_64-apple-macosx10.12.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -E -disable-free -disable-llvm-verifier -discard-value-names -main-file-name - -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 274.1 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir /usr/local/llvm/bin/…/lib/clang/4.0.0 -fdebug-compilation-dir /Users/kevin/scratch -ferror-limit 19 -fmessage-length 101 -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.12.0 -fencode-extended-block-signature -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o - -x c -
clang -cc1 version 4.0.0 based upon LLVM 4.0.0svn default target x86_64-apple-darwin16.0.0
ignoring nonexistent directory “/usr/include”
#include “…” search starts here:
#include <…> search starts here:
/usr/local/include
/usr/local/llvm/bin/…/lib/clang/4.0.0/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.

For reference, I am configuring LLVM from a build directory within the LLVM project tree with:

cmake -G “Ninja” …
-DCMAKE_C_COMPILER=/usr/bin/clang
-DCMAKE_C_FLAGS=“${INCLUDE_FLAGS}”
-DCMAKE_CXX_COMPILER=/usr/bin/clang++
-DCMAKE_CXX_FLAGS=“${INCLUDE_FLAGS}”
-DCMAKE_SHARED_LINKER_FLAGS=“-L/usr/local/lib”
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_INSTALL_PREFIX=“${LLVM_INSTALL_DIR}”
-DLLVM_ENABLE_CXX1Y=Yes
-DLLVM_ENABLE_LTO=Yes

with INCLUDE_FLAGS set with the include paths as noted before.

As an aside, it would be incredibly helpful if a ‘one-button’ shell script for download, build and installation of LLVM + clang could be provided in the documentation; that is, something users could play with and tweak themselves. Or, perhaps, links to scripts used by existing projects that need to build LLVM + clang themselves?

Thanks,
Kevin

Hi Kevin:

On my system, I find stdio.h, etc., in /usr/include, but you get a warning saying it doesn’t exist. Did you install the command line tools? Here’s what I get:

//////// system version

local:/Users/dhinton $ /usr/bin/clang -xc++ -E - -v </dev/null
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
“/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang” -cc1 -triple x86_64-apple-macosx10.11.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -E -disable-free -disable-llvm-verifier -main-file-name - -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 264.3.102 -v -dwarf-column-info -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/…/lib/clang/7.3.0 -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/dhinton -ferror-limit 19 -fmessage-length 204 -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.11.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o - -x c++ -
clang -cc1 version 7.3.0 (clang-703.0.31) default target x86_64-apple-darwin15.6.0
ignoring nonexistent directory “/usr/include/c++/v1”
ignoring nonexistent directory “/usr/local/include”
#include “…” search starts here:
#include <…> search starts here:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/…/include/c++/v1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/…/lib/clang/7.3.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.

1 “”

1 “” 1

1 “” 3

336 “” 3

1 “” 1

1 “” 2

1 “” 2

//////// and my locally installed version:

local:/Users/dhinton $ ~/usr/bin/clang -xc++ -E - -v </dev/null
clang version 4.0.0 (http://llvm.org/git/llvm.git f970c6e67b13351e92c33667b991155d9569b5da)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Users/dhinton/usr/bin
“/Users/dhinton/usr/bin/clang-4.0” -cc1 -triple x86_64-apple-macosx10.11.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -E -disable-free -disable-llvm-verifier -discard-value-names -main-file-name - -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 264.3.102 -v -dwarf-column-info -debugger-tuning=lldb -resource-dir /Users/dhinton/usr/bin/…/lib/clang/4.0.0 -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/dhinton -ferror-limit 19 -fmessage-length 204 -stack-protector 1 -fblocks -fobjc-runtime=macosx-10.11.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o - -x c++ -
clang -cc1 version 4.0.0 based upon LLVM 4.0.0svn default target x86_64-apple-darwin15.6.0
ignoring nonexistent directory “/usr/include/c++/v1”
ignoring nonexistent directory “/usr/local/include”
#include “…” search starts here:
#include <…> search starts here:
/Users/dhinton/usr/bin/…/include/c++/v1
/Users/dhinton/usr/bin/…/lib/clang/4.0.0/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.

1 “”

1 “” 1

1 “” 3

337 “” 3

1 “” 1

1 “” 2

1 “” 2

hth…
don

Hi Don,

Doh, I think you’re exactly right. It looks like command line tools (+ the associated headers) were lost in the update from El Capitan to Sierra. I assumed installing Xcode 8.0 would also install the command line tools as a side effect, but was mistaken.

A simple ‘xcode-select --install’ got the headers installed back into /usr/include and everything seems fine now.

Thanks!
Kevin

Hi everyone,

This is my first post here on cfe, so please be kind – I apologize if some of these issues have already been discussed previously on the list. I’m building clang + LLVM mostly following the instructions at http://clang.llvm.org/get_started.html and http://llvm.org/docs/CMake.html. The goal is for me to be able to produce a clang installation that I can use and play with locally on my MBP.

When I follow these steps, I can usually build clang on MacOS with just cmake path/to/llvm && make

When building LLVM + clang, I had to manually add the include paths for Valgrind, libxml2, and Python (currently just using -DCMAKE_C_FLAGS and -DCMAKE_CXX_FLAGS when invoking cmake for configuration). Is this intended, or should the cmake scripts be able to automatically discover these dependencies? (I believe the header include errors occurred when attempting to compile lldb).

The getting started page you linker does not mention lldb, how did you ended up building it?

Hi everyone,

This is my first post here on cfe, so please be kind -- I apologize if
some of these issues have already been discussed previously on the list.
I'm building clang + LLVM mostly following the instructions at
Clang - Getting Started and Building LLVM with CMake — LLVM 18.0.0git documentation.
The goal is for me to be able to produce a clang installation that I can
use and play with locally on my MBP.

When I follow these steps, I can usually build clang on MacOS with just
`cmake path/to/llvm && make`

It looks like the issue was ultimately me just missing the command line
tools (and, with that, the headers it installs into `/usr/include`).
Although I do wonder how I might configure clang on build to use a
particular (set of) directories for the default header include paths.
(Unless there's a good reason why I shouldn't be doing this?)

When building LLVM + clang, I had to manually add the include paths for
Valgrind, libxml2, and Python (currently just using -DCMAKE_C_FLAGS and
-DCMAKE_CXX_FLAGS when invoking cmake for configuration). Is this intended,
or should the cmake scripts be able to automatically discover these
dependencies? (I believe the header include errors occurred when attempting
to compile lldb).

The getting started page you linker does not mention lldb, how did you
ended up building it?

I followed the instructions here related to building on MacOSX with CMake:

http://lldb.llvm.org/build.html#BuildingLldbOnMacOSX

In other words, checking out the lldb sources in the 'projects' subfolder,
and letting the build system handle the rest when building everything.

You can do this in CMake with CMAKE_OSX_SYSROOT, although if there is no xcodebuild or xcrun in that path, CMake may complain. You can also append (or prepend) to the CMAKE_PREFIX_PATH variable, and CMake will search for headers, libraries, etc. in there, although I these may be searched AFTER system search paths.

You can also create a CMake “Toolchain File” that will be loaded early in the process to populate whatever variables you want to set related to system headers and libraries. Documentation about CMake toolchains can be found here: https://cmake.org/cmake/help/v3.6/manual/cmake-toolchains.7.html

Docs regarding the different CMake variables here:
https://cmake.org/cmake/help/v3.6/variable/CMAKE_OSX_SYSROOT.html
https://cmake.org/cmake/help/v3.6/variable/CMAKE_PREFIX_PATH.html
https://cmake.org/cmake/help/v3.6/variable/CMAKE_SYSTEM_PREFIX_PATH.html
https://cmake.org/cmake/help/v3.6/variable/CMAKE_TOOLCHAIN_FILE.html

Best,
Morgan

Hi everyone,

This is my first post here on cfe, so please be kind -- I apologize if some of these issues have already been discussed previously on the list. I'm building clang + LLVM mostly following the instructions at Clang - Getting Started and Building LLVM with CMake — LLVM 18.0.0git documentation. The goal is for me to be able to produce a clang installation that I can use and play with locally on my MBP.

When I follow these steps, I can usually build clang on MacOS with just `cmake path/to/llvm && make`

It looks like the issue was ultimately me just missing the command line tools (and, with that, the headers it installs into `/usr/include`).

I don't usually install the command-line tools. If you add a checkout of libcxx, an installing Clang should include the headers from libc++ parallel with your Clang (i.e., ../include/c++/v1).