xref: /aosp_15_r20/external/compiler-rt/cmake/base-config-ix.cmake (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot# The CompilerRT build system requires CMake version 2.8.8 or higher in order
2*7c3d14c8STreehugger Robot# to use its support for building convenience "libraries" as a collection of
3*7c3d14c8STreehugger Robot# .o files. This is particularly useful in producing larger, more complex
4*7c3d14c8STreehugger Robot# runtime libraries.
5*7c3d14c8STreehugger Robot
6*7c3d14c8STreehugger Robotinclude(CheckIncludeFile)
7*7c3d14c8STreehugger Robotcheck_include_file(unwind.h HAVE_UNWIND_H)
8*7c3d14c8STreehugger Robot
9*7c3d14c8STreehugger Robot# Top level target used to build all compiler-rt libraries.
10*7c3d14c8STreehugger Robotadd_custom_target(compiler-rt ALL)
11*7c3d14c8STreehugger Robotset_target_properties(compiler-rt PROPERTIES FOLDER "Compiler-RT Misc")
12*7c3d14c8STreehugger Robot
13*7c3d14c8STreehugger Robot# Setting these variables from an LLVM build is sufficient that compiler-rt can
14*7c3d14c8STreehugger Robot# construct the output paths, so it can behave as if it were in-tree here.
15*7c3d14c8STreehugger Robotif (LLVM_LIBRARY_OUTPUT_INTDIR AND LLVM_RUNTIME_OUTPUT_INTDIR AND PACKAGE_VERSION)
16*7c3d14c8STreehugger Robot  set(LLVM_TREE_AVAILABLE On)
17*7c3d14c8STreehugger Robotendif()
18*7c3d14c8STreehugger Robot
19*7c3d14c8STreehugger Robotif (LLVM_TREE_AVAILABLE)
20*7c3d14c8STreehugger Robot  # Compute the Clang version from the LLVM version.
21*7c3d14c8STreehugger Robot  # FIXME: We should be able to reuse CLANG_VERSION variable calculated
22*7c3d14c8STreehugger Robot  #        in Clang cmake files, instead of copying the rules here.
23*7c3d14c8STreehugger Robot  string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
24*7c3d14c8STreehugger Robot         ${PACKAGE_VERSION})
25*7c3d14c8STreehugger Robot  # Setup the paths where compiler-rt runtimes and headers should be stored.
26*7c3d14c8STreehugger Robot  set(COMPILER_RT_OUTPUT_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/clang/${CLANG_VERSION})
27*7c3d14c8STreehugger Robot  set(COMPILER_RT_EXEC_OUTPUT_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
28*7c3d14c8STreehugger Robot  set(COMPILER_RT_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
29*7c3d14c8STreehugger Robot  option(COMPILER_RT_INCLUDE_TESTS "Generate and build compiler-rt unit tests."
30*7c3d14c8STreehugger Robot         ${LLVM_INCLUDE_TESTS})
31*7c3d14c8STreehugger Robot  option(COMPILER_RT_ENABLE_WERROR "Fail and stop if warning is triggered"
32*7c3d14c8STreehugger Robot         ${LLVM_ENABLE_WERROR})
33*7c3d14c8STreehugger Robot  # Use just-built Clang to compile/link tests on all platforms, except for
34*7c3d14c8STreehugger Robot  # Windows where we need to use clang-cl instead.
35*7c3d14c8STreehugger Robot  if(NOT MSVC)
36*7c3d14c8STreehugger Robot    set(COMPILER_RT_TEST_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
37*7c3d14c8STreehugger Robot    set(COMPILER_RT_TEST_CXX_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++)
38*7c3d14c8STreehugger Robot  else()
39*7c3d14c8STreehugger Robot    set(COMPILER_RT_TEST_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang.exe)
40*7c3d14c8STreehugger Robot    set(COMPILER_RT_TEST_CXX_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++.exe)
41*7c3d14c8STreehugger Robot  endif()
42*7c3d14c8STreehugger Robotelse()
43*7c3d14c8STreehugger Robot    # Take output dir and install path from the user.
44*7c3d14c8STreehugger Robot  set(COMPILER_RT_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR} CACHE PATH
45*7c3d14c8STreehugger Robot    "Path where built compiler-rt libraries should be stored.")
46*7c3d14c8STreehugger Robot  set(COMPILER_RT_EXEC_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin CACHE PATH
47*7c3d14c8STreehugger Robot    "Path where built compiler-rt executables should be stored.")
48*7c3d14c8STreehugger Robot  set(COMPILER_RT_INSTALL_PATH ${CMAKE_INSTALL_PREFIX} CACHE PATH
49*7c3d14c8STreehugger Robot    "Path where built compiler-rt libraries should be installed.")
50*7c3d14c8STreehugger Robot  option(COMPILER_RT_INCLUDE_TESTS "Generate and build compiler-rt unit tests." OFF)
51*7c3d14c8STreehugger Robot  option(COMPILER_RT_ENABLE_WERROR "Fail and stop if warning is triggered" OFF)
52*7c3d14c8STreehugger Robot  # Use a host compiler to compile/link tests.
53*7c3d14c8STreehugger Robot  set(COMPILER_RT_TEST_COMPILER ${CMAKE_C_COMPILER} CACHE PATH "Compiler to use for testing")
54*7c3d14c8STreehugger Robot  set(COMPILER_RT_TEST_CXX_COMPILER ${CMAKE_CXX_COMPILER} CACHE PATH "C++ Compiler to use for testing")
55*7c3d14c8STreehugger Robotendif()
56*7c3d14c8STreehugger Robot
57*7c3d14c8STreehugger Robotif("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang[+]*$")
58*7c3d14c8STreehugger Robot  set(COMPILER_RT_TEST_COMPILER_ID Clang)
59*7c3d14c8STreehugger Robotelseif("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang.*.exe$")
60*7c3d14c8STreehugger Robot  set(COMPILER_RT_TEST_COMPILER_ID Clang)
61*7c3d14c8STreehugger Robotelse()
62*7c3d14c8STreehugger Robot  set(COMPILER_RT_TEST_COMPILER_ID GNU)
63*7c3d14c8STreehugger Robotendif()
64*7c3d14c8STreehugger Robot
65*7c3d14c8STreehugger Robotstring(TOLOWER ${CMAKE_SYSTEM_NAME} COMPILER_RT_OS_DIR)
66*7c3d14c8STreehugger Robotset(COMPILER_RT_LIBRARY_OUTPUT_DIR
67*7c3d14c8STreehugger Robot  ${COMPILER_RT_OUTPUT_DIR}/lib/${COMPILER_RT_OS_DIR})
68*7c3d14c8STreehugger Robotset(COMPILER_RT_LIBRARY_INSTALL_DIR
69*7c3d14c8STreehugger Robot  ${COMPILER_RT_INSTALL_PATH}/lib/${COMPILER_RT_OS_DIR})
70*7c3d14c8STreehugger Robot
71*7c3d14c8STreehugger Robotif(APPLE)
72*7c3d14c8STreehugger Robot  # On Darwin if /usr/include doesn't exist, the user probably has Xcode but not
73*7c3d14c8STreehugger Robot  # the command line tools. If this is the case, we need to find the OS X
74*7c3d14c8STreehugger Robot  # sysroot to pass to clang.
75*7c3d14c8STreehugger Robot  if(NOT EXISTS /usr/include)
76*7c3d14c8STreehugger Robot    execute_process(COMMAND xcodebuild -version -sdk macosx Path
77*7c3d14c8STreehugger Robot       OUTPUT_VARIABLE OSX_SYSROOT
78*7c3d14c8STreehugger Robot       ERROR_QUIET
79*7c3d14c8STreehugger Robot       OUTPUT_STRIP_TRAILING_WHITESPACE)
80*7c3d14c8STreehugger Robot    set(OSX_SYSROOT_FLAG "-isysroot${OSX_SYSROOT}")
81*7c3d14c8STreehugger Robot  endif()
82*7c3d14c8STreehugger Robot
83*7c3d14c8STreehugger Robot  option(COMPILER_RT_ENABLE_IOS "Enable building for iOS" Off)
84*7c3d14c8STreehugger Robot  option(COMPILER_RT_ENABLE_WATCHOS "Enable building for watchOS - Experimental" Off)
85*7c3d14c8STreehugger Robot  option(COMPILER_RT_ENABLE_TVOS "Enable building for tvOS - Experimental" Off)
86*7c3d14c8STreehugger Robotendif()
87*7c3d14c8STreehugger Robot
88*7c3d14c8STreehugger Robotmacro(test_targets)
89*7c3d14c8STreehugger Robot  # Find and run MSVC (not clang-cl) and get its version. This will tell clang-cl
90*7c3d14c8STreehugger Robot  # what version of MSVC to pretend to be so that the STL works.
91*7c3d14c8STreehugger Robot  set(MSVC_VERSION_FLAG "")
92*7c3d14c8STreehugger Robot  if (MSVC)
93*7c3d14c8STreehugger Robot    # Find and run MSVC (not clang-cl) and get its version. This will tell
94*7c3d14c8STreehugger Robot    # clang-cl what version of MSVC to pretend to be so that the STL works.
95*7c3d14c8STreehugger Robot    execute_process(COMMAND "$ENV{VSINSTALLDIR}/VC/bin/cl.exe"
96*7c3d14c8STreehugger Robot      OUTPUT_QUIET
97*7c3d14c8STreehugger Robot      ERROR_VARIABLE MSVC_COMPAT_VERSION
98*7c3d14c8STreehugger Robot      )
99*7c3d14c8STreehugger Robot    string(REGEX REPLACE "^.*Compiler Version ([0-9.]+) for .*$" "\\1"
100*7c3d14c8STreehugger Robot      MSVC_COMPAT_VERSION "${MSVC_COMPAT_VERSION}")
101*7c3d14c8STreehugger Robot    if (MSVC_COMPAT_VERSION MATCHES "^[0-9].+$")
102*7c3d14c8STreehugger Robot      set(MSVC_VERSION_FLAG "-fms-compatibility-version=${MSVC_COMPAT_VERSION}")
103*7c3d14c8STreehugger Robot      # Add this flag into the host build if this is clang-cl.
104*7c3d14c8STreehugger Robot      if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
105*7c3d14c8STreehugger Robot        append("${MSVC_VERSION_FLAG}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
106*7c3d14c8STreehugger Robot      elseif (COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
107*7c3d14c8STreehugger Robot        # Add this flag to test compiles to suppress clang's auto-detection
108*7c3d14c8STreehugger Robot        # logic.
109*7c3d14c8STreehugger Robot        append("${MSVC_VERSION_FLAG}" COMPILER_RT_TEST_COMPILER_CFLAGS)
110*7c3d14c8STreehugger Robot      endif()
111*7c3d14c8STreehugger Robot    endif()
112*7c3d14c8STreehugger Robot  endif()
113*7c3d14c8STreehugger Robot
114*7c3d14c8STreehugger Robot  # Generate the COMPILER_RT_SUPPORTED_ARCH list.
115*7c3d14c8STreehugger Robot  if(ANDROID)
116*7c3d14c8STreehugger Robot    # Examine compiler output to determine target architecture.
117*7c3d14c8STreehugger Robot    detect_target_arch()
118*7c3d14c8STreehugger Robot    set(COMPILER_RT_OS_SUFFIX "-android")
119*7c3d14c8STreehugger Robot  elseif(NOT APPLE) # Supported archs for Apple platforms are generated later
120*7c3d14c8STreehugger Robot    if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "i[2-6]86|x86|amd64")
121*7c3d14c8STreehugger Robot      if(NOT MSVC)
122*7c3d14c8STreehugger Robot        test_target_arch(x86_64 "" "-m64")
123*7c3d14c8STreehugger Robot        # FIXME: We build runtimes for both i686 and i386, as "clang -m32" may
124*7c3d14c8STreehugger Robot        # target different variant than "$CMAKE_C_COMPILER -m32". This part should
125*7c3d14c8STreehugger Robot        # be gone after we resolve PR14109.
126*7c3d14c8STreehugger Robot        test_target_arch(i686 __i686__ "-m32")
127*7c3d14c8STreehugger Robot        test_target_arch(i386 __i386__ "-m32")
128*7c3d14c8STreehugger Robot      else()
129*7c3d14c8STreehugger Robot        if (CMAKE_SIZEOF_VOID_P EQUAL 4)
130*7c3d14c8STreehugger Robot          test_target_arch(i386 "" "")
131*7c3d14c8STreehugger Robot        else()
132*7c3d14c8STreehugger Robot          test_target_arch(x86_64 "" "")
133*7c3d14c8STreehugger Robot        endif()
134*7c3d14c8STreehugger Robot      endif()
135*7c3d14c8STreehugger Robot    elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "powerpc")
136*7c3d14c8STreehugger Robot      TEST_BIG_ENDIAN(HOST_IS_BIG_ENDIAN)
137*7c3d14c8STreehugger Robot      if(HOST_IS_BIG_ENDIAN)
138*7c3d14c8STreehugger Robot        test_target_arch(powerpc64 "" "-m64")
139*7c3d14c8STreehugger Robot      else()
140*7c3d14c8STreehugger Robot        test_target_arch(powerpc64le "" "-m64")
141*7c3d14c8STreehugger Robot      endif()
142*7c3d14c8STreehugger Robot    elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "s390x")
143*7c3d14c8STreehugger Robot      test_target_arch(s390x "" "")
144*7c3d14c8STreehugger Robot    elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mipsel|mips64el")
145*7c3d14c8STreehugger Robot      # Gcc doesn't accept -m32/-m64 so we do the next best thing and use
146*7c3d14c8STreehugger Robot      # -mips32r2/-mips64r2. We don't use -mips1/-mips3 because we want to match
147*7c3d14c8STreehugger Robot      # clang's default CPU's. In the 64-bit case, we must also specify the ABI
148*7c3d14c8STreehugger Robot      # since the default ABI differs between gcc and clang.
149*7c3d14c8STreehugger Robot      # FIXME: Ideally, we would build the N32 library too.
150*7c3d14c8STreehugger Robot      test_target_arch(mipsel "" "-mips32r2" "--target=mipsel-linux-gnu")
151*7c3d14c8STreehugger Robot      test_target_arch(mips64el "" "-mips64r2" "--target=mips64el-linux-gnu" "-mabi=n64")
152*7c3d14c8STreehugger Robot    elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mips")
153*7c3d14c8STreehugger Robot      test_target_arch(mips "" "-mips32r2" "--target=mips-linux-gnu")
154*7c3d14c8STreehugger Robot      test_target_arch(mips64 "" "-mips64r2" "--target=mips64-linux-gnu" "-mabi=n64")
155*7c3d14c8STreehugger Robot    elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "arm")
156*7c3d14c8STreehugger Robot      test_target_arch(arm "" "-march=armv7-a" "-mfloat-abi=soft")
157*7c3d14c8STreehugger Robot      test_target_arch(armhf "" "-march=armv7-a" "-mfloat-abi=hard")
158*7c3d14c8STreehugger Robot    elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "aarch32")
159*7c3d14c8STreehugger Robot      test_target_arch(aarch32 "" "-march=armv8-a")
160*7c3d14c8STreehugger Robot    elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "aarch64")
161*7c3d14c8STreehugger Robot      test_target_arch(aarch64 "" "-march=armv8-a")
162*7c3d14c8STreehugger Robot    elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm32")
163*7c3d14c8STreehugger Robot      test_target_arch(wasm32 "" "--target=wasm32-unknown-unknown")
164*7c3d14c8STreehugger Robot    elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm64")
165*7c3d14c8STreehugger Robot      test_target_arch(wasm64 "" "--target=wasm64-unknown-unknown")
166*7c3d14c8STreehugger Robot    endif()
167*7c3d14c8STreehugger Robot    set(COMPILER_RT_OS_SUFFIX "")
168*7c3d14c8STreehugger Robot  endif()
169*7c3d14c8STreehugger Robotendmacro()
170