• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
9
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.
12
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
16
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
21# in the environment.
22
23# Example usage:
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
29
30# `xcrun -find clang` should return the installed clang, and `clang --version`
31# should show 3.8.0svn.
32
33if(NOT APPLE)
34  return()
35endif()
36
37option(LLVM_CREATE_XCODE_TOOLCHAIN "Create a target to install LLVM into an Xcode toolchain" Off)
38
39if(NOT LLVM_CREATE_XCODE_TOOLCHAIN)
40  return()
41endif()
42
43# XCODE_VERSION is set by CMake when using the Xcode generator, otherwise we need
44# to detect it manually here.
45if(NOT XCODE_VERSION)
46  execute_process(
47    COMMAND xcodebuild -version
48    OUTPUT_VARIABLE xcodebuild_version
49    OUTPUT_STRIP_TRAILING_WHITESPACE
50    ERROR_FILE /dev/null
51  )
52  string(REGEX MATCH "Xcode ([0-9][0-9]?([.][0-9])+)" version_match ${xcodebuild_version})
53  if(version_match)
54    message(STATUS "Identified Xcode Version: ${CMAKE_MATCH_1}")
55    set(XCODE_VERSION ${CMAKE_MATCH_1})
56  else()
57    # If detecting Xcode version failed, set a crazy high version so we default
58    # to the newest.
59    set(XCODE_VERSION 99)
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.")
61  endif()
62endif()
63
64# Xcode 8 requires CompatibilityVersion 2
65set(COMPAT_VERSION 2)
66if(XCODE_VERSION VERSION_LESS 8.0.0)
67  # Xcode 7.3 (the first version supporting external toolchains) requires
68  # CompatibilityVersion 1
69  set(COMPAT_VERSION 1)
70endif()
71
72execute_process(
73  COMMAND xcrun -find otool
74  OUTPUT_VARIABLE clang_path
75  OUTPUT_STRIP_TRAILING_WHITESPACE
76  ERROR_FILE /dev/null
77)
78string(REGEX MATCH "(.*/Toolchains)/.*" toolchains_match ${clang_path})
79if(NOT toolchains_match)
80  message(FATAL_ERROR "Could not identify toolchain dir")
81endif()
82set(toolchains_dir ${CMAKE_MATCH_1})
83
84set(LLVMToolchainDir "${CMAKE_INSTALL_PREFIX}/Toolchains/LLVM${PACKAGE_VERSION}.xctoolchain/")
85
86add_custom_command(OUTPUT ${LLVMToolchainDir}
87                    COMMAND ${CMAKE_COMMAND} -E make_directory ${LLVMToolchainDir})
88
89add_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"
94                  )
95
96add_custom_target(build-xcode-toolchain
97                  COMMAND "${CMAKE_COMMAND}" --build ${CMAKE_BINARY_DIR} --target all)
98add_llvm_install_targets(install-xcode-toolchain
99                         DEPENDS ${LLVMToolchainDir}/Info.plist build-xcode-toolchain
100                         PREFIX ${LLVMToolchainDir}/usr/)
101
102if(LLVM_DISTRIBUTION_COMPONENTS)
103  if(LLVM_ENABLE_IDE)
104    message(FATAL_ERROR "LLVM_DISTRIBUTION_COMPONENTS cannot be specified with multi-configuration generators (i.e. Xcode or Visual Studio)")
105  endif()
106
107  add_custom_target(install-distribution-toolchain
108                  DEPENDS ${LLVMToolchainDir}/Info.plist distribution)
109
110  foreach(target ${LLVM_DISTRIBUTION_COMPONENTS})
111    add_llvm_install_targets(install-distribution-${target}
112                             DEPENDS ${target}
113                             COMPONENT ${target}
114                             PREFIX ${LLVMToolchainDir}/usr/)
115    add_dependencies(install-distribution-toolchain install-distribution-${target})
116  endforeach()
117endif()
118