llvm.org GIT mirror llvm / e0e13ae
[CMake] [Darwin] Add support for generating Xcode-compatible toolchains that xcodebuild and xcrun can search Summary: Sometimes you want to install a custom compiler and use it like the system compiler without overriding the system compiler. This patch lets you create xctoolchains that the darwin command line tools can use. To use this patch set LLVM_CREATE_XCODE_TOOLCHAIN=On in your CMake invocation and build the `install-code-toolchain` target. After installation you can set the envar EXTERNAL_TOOLCHAINS_DIR to your installed Toolchains directory, and the TOOLCHAINS envar to the toolchain identifier (ex org.llvm.3.8.0svn). This will then cause /usr/bin/clang to call your newly installed clang. Reviewers: Bigcheese, bogner Subscribers: tobiasfar, llvm-commits Differential Revision: http://reviews.llvm.org/D13605 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@250450 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Bieneman 3 years ago
1 changed file(s) with 72 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 # OS X 10.11 El Capitan has just been released. One of the new features, System
1 # Integrity Protection, prevents modifying the base OS install, even with sudo.
2 # This prevents LLVM developers on OS X from being able to easily install new
3 # system compilers. The feature can be disabled, but to make it easier for
4 # developers to work without disabling SIP, this file can generate an Xcode
5 # toolchain. Xcode toolchains are a mostly-undocumented feature that allows
6 # multiple copies of low level tools to be installed to different locations, and
7 # users can easily switch between them.
8
9 # Setting an environment variable TOOLCHAINS to the toolchain's identifier will
10 # result in /usr/bin/ or xcrun to find the tool in the toolchain.
11
12 # To make this work with Xcode 7.1 and later you can install the toolchain this
13 # file generates anywhere on your system and set EXTERNAL_TOOLCHAINS_DIR to the
14 # path specified by $CMAKE_INSTALL_PREFIX/Toolchains
15
16 # This file generates a custom install-xcode-toolchain target which constructs
17 # and installs a toolchain with the identifier in the pattern:
18 # org.llvm.${PACKAGE_VERSION}. This toolchain can then be used to override the
19 # system compiler by setting TOOLCHAINS=org.llvm.${PACKAGE_VERSION} in the
20 # in the environment.
21
22 # Example usage:
23 # cmake -G Ninja -DLLVM_CREATE_XCODE_TOOLCHAIN=On
24 # -DCMAKE_INSTALL_PREFIX=$PWD/install
25 # ninja install-xcode-toolchain
26 # export EXTERNAL_TOOLCHAINS_DIR=$PWD/install/Toolchains
27 # export TOOLCHAINS=org.llvm.3.8.0svn
28
29 # `xcrun -find clang` should return the installed clang, and `clang --version`
30 # should show 3.8.0svn.
31
32 if(NOT APPLE)
33 return()
34 endif()
35
36 option(LLVM_CREATE_XCODE_TOOLCHAIN "Create a target to install LLVM into an Xcode toolchain" Off)
37
38 if(NOT LLVM_CREATE_XCODE_TOOLCHAIN)
39 return()
40 endif()
41
42 execute_process(
43 COMMAND xcrun -find otool
44 OUTPUT_VARIABLE clang_path
45 OUTPUT_STRIP_TRAILING_WHITESPACE
46 ERROR_FILE /dev/null
47 )
48 string(REGEX MATCH "(.*/Toolchains)/.*" toolchains_match ${clang_path})
49 if(NOT toolchains_match)
50 message(FATAL_ERROR "Could not identify toolchain dir")
51 endif()
52 set(toolchains_dir ${CMAKE_MATCH_1})
53
54 set(XcodeDefaultInfo "${toolchains_dir}/XcodeDefault.xctoolchain/ToolchainInfo.plist")
55 set(LLVMToolchainDir "${CMAKE_INSTALL_PREFIX}/Toolchains/LLVM${PACKAGE_VERSION}.xctoolchain/")
56
57 add_custom_command(OUTPUT ${LLVMToolchainDir}
58 COMMAND ${CMAKE_COMMAND} -E make_directory ${LLVMToolchainDir})
59
60 add_custom_command(OUTPUT ${LLVMToolchainDir}/ToolchainInfo.plist
61 DEPENDS ${LLVMToolchainDir}
62 COMMAND ${CMAKE_COMMAND} -E copy "${XcodeDefaultInfo}" "${LLVMToolchainDir}/ToolchainInfo.plist"
63 COMMAND /usr/libexec/PlistBuddy -c "Set:Identifier org.llvm.${PACKAGE_VERSION}" "${LLVMToolchainDir}/ToolchainInfo.plist")
64
65 add_custom_target(install-xcode-toolchain
66 DEPENDS ${LLVMToolchainDir}/ToolchainInfo.plist
67 COMMAND "${CMAKE_COMMAND}" --build ${CMAKE_BINARY_DIR} --target all
68 COMMAND "${CMAKE_COMMAND}"
69 -DCMAKE_INSTALL_PREFIX=${LLVMToolchainDir}/usr/
70 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
71 ${cmake_3_2_USES_TERMINAL})