1 # OS X 10.11 El Capitan has just been released. One of the new features, System
2 # Integrity Protection, prevents modifying the base OS install, even with sudo.
3 # This prevents LLVM developers on OS X from being able to easily install new
4 # system compilers. The feature can be disabled, but to make it easier for
5 # developers to work without disabling SIP, this file can generate an Xcode
6 # toolchain. Xcode toolchains are a mostly-undocumented feature that allows
7 # multiple copies of low level tools to be installed to different locations, and
8 # users can easily switch between them.
10 # Setting an environment variable TOOLCHAINS to the toolchain's identifier will
11 # result in /usr/bin/<tool> or xcrun <tool> to find the tool in the toolchain.
13 # To make this work with Xcode 7.1 and later you can install the toolchain this
14 # file generates anywhere on your system and set EXTERNAL_TOOLCHAINS_DIR to the
15 # path specified by $CMAKE_INSTALL_PREFIX/Toolchains
17 # This file generates a custom install-xcode-toolchain target which constructs
18 # and installs a toolchain with the identifier in the pattern:
19 # org.llvm.${PACKAGE_VERSION}. This toolchain can then be used to override the
20 # system compiler by setting TOOLCHAINS=org.llvm.${PACKAGE_VERSION} in the
24 # cmake -G Ninja -DLLVM_CREATE_XCODE_TOOLCHAIN=On
25 # -DCMAKE_INSTALL_PREFIX=$PWD/install
26 # ninja install-xcode-toolchain
27 # export EXTERNAL_TOOLCHAINS_DIR=$PWD/install/Toolchains
28 # export TOOLCHAINS=org.llvm.3.8.0svn
30 # `xcrun -find clang` should return the installed clang, and `clang --version`
31 # should show 3.8.0svn.
37 option(LLVM_CREATE_XCODE_TOOLCHAIN "Create a target to install LLVM into an Xcode toolchain" Off)
39 if(NOT LLVM_CREATE_XCODE_TOOLCHAIN)
43 # XCODE_VERSION is set by CMake when using the Xcode generator, otherwise we need
44 # to detect it manually here.
47 COMMAND xcodebuild -version
48 OUTPUT_VARIABLE xcodebuild_version
49 OUTPUT_STRIP_TRAILING_WHITESPACE
52 string(REGEX MATCH "Xcode ([0-9][0-9]?([.][0-9])+)" version_match ${xcodebuild_version})
54 message(STATUS "Identified Xcode Version: ${CMAKE_MATCH_1}")
55 set(XCODE_VERSION ${CMAKE_MATCH_1})
57 # If detecting Xcode version failed, set a crazy high version so we default
60 message(WARNING "Failed to detect the version of an installed copy of Xcode, falling back to highest supported version. Set XCODE_VERSION to override.")
64 # Xcode 8 requires CompatibilityVersion 2
66 if(XCODE_VERSION VERSION_LESS 8.0.0)
67 # Xcode 7.3 (the first version supporting external toolchains) requires
68 # CompatibilityVersion 1
73 COMMAND xcrun -find otool
74 OUTPUT_VARIABLE clang_path
75 OUTPUT_STRIP_TRAILING_WHITESPACE
78 string(REGEX MATCH "(.*/Toolchains)/.*" toolchains_match ${clang_path})
79 if(NOT toolchains_match)
80 message(FATAL_ERROR "Could not identify toolchain dir")
82 set(toolchains_dir ${CMAKE_MATCH_1})
84 set(LLVMToolchainDir "${CMAKE_INSTALL_PREFIX}/Toolchains/LLVM${PACKAGE_VERSION}.xctoolchain/")
86 add_custom_command(OUTPUT ${LLVMToolchainDir}
87 COMMAND ${CMAKE_COMMAND} -E make_directory ${LLVMToolchainDir})
89 add_custom_command(OUTPUT ${LLVMToolchainDir}/Info.plist
90 DEPENDS ${LLVMToolchainDir}
91 COMMAND ${CMAKE_COMMAND} -E remove ${LLVMToolchainDir}/Info.plist
92 COMMAND /usr/libexec/PlistBuddy -c "Add:CFBundleIdentifier string org.llvm.${PACKAGE_VERSION}" "${LLVMToolchainDir}/Info.plist"
93 COMMAND /usr/libexec/PlistBuddy -c "Add:CompatibilityVersion integer ${COMPAT_VERSION}" "${LLVMToolchainDir}/Info.plist"
96 add_custom_target(build-xcode-toolchain
97 COMMAND "${CMAKE_COMMAND}" --build ${CMAKE_BINARY_DIR} --target all)
98 add_llvm_install_targets(install-xcode-toolchain
99 DEPENDS ${LLVMToolchainDir}/Info.plist build-xcode-toolchain
100 PREFIX ${LLVMToolchainDir}/usr/)
102 if(LLVM_DISTRIBUTION_COMPONENTS)
103 if(CMAKE_CONFIGURATION_TYPES)
104 message(FATAL_ERROR "LLVM_DISTRIBUTION_COMPONENTS cannot be specified with multi-configuration generators (i.e. Xcode or Visual Studio)")
107 add_custom_target(install-distribution-toolchain
108 DEPENDS ${LLVMToolchainDir}/Info.plist distribution)
110 foreach(target ${LLVM_DISTRIBUTION_COMPONENTS})
111 add_llvm_install_targets(install-distribution-${target}
114 PREFIX ${LLVMToolchainDir}/usr/)
115 add_dependencies(install-distribution-toolchain install-distribution-${target})