Skip to content

Commit

Permalink
[cmake] Backport recent compiler-rt CMake changes to cleanup and fix …
Browse files Browse the repository at this point in the history
…cmake on MacOSX.
  • Loading branch information
neboat committed Jan 19, 2021
1 parent 35c6b24 commit 2f5e199
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 186 deletions.
39 changes: 28 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ cmake_minimum_required(VERSION 3.4.3)
if(POLICY CMP0068)
cmake_policy(SET CMP0068 NEW)
set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)
cmake_policy(SET CMP0075 NEW)
endif()

# Check if cilktools is built as a standalone project.
Expand Down Expand Up @@ -56,12 +55,34 @@ if (CILKTOOLS_STANDALONE_BUILD)
set_target_properties(intrinsics_gen PROPERTIES FOLDER "Cilktools Misc")
endif()

# Find Python interpreter.
include(FindPythonInterp)
if(NOT PYTHONINTERP_FOUND)
message(FATAL_ERROR "
Unable to find Python interpreter required testing. Please install Python
or specify the PYTHON_EXECUTABLE CMake variable.")
if(CMAKE_VERSION VERSION_LESS 3.12)
# Find Python interpreter.
include(FindPythonInterp)
if(NOT PYTHONINTERP_FOUND)
message(FATAL_ERROR "
Unable to find Python interpreter required testing. Please install Python
or specify the PYTHON_EXECUTABLE CMake variable.")
endif()

add_executable(Python3::Interpreter IMPORTED)
set_target_properties(Python3::Interpreter PROPERTIES
IMPORTED_LOCATION ${PYTHON_EXECUTABLE})
set(Python3_EXECUTABLE ${PYTHON_EXECUTABLE})
else()
find_package(Python3 COMPONENTS Interpreter)
if(NOT Python3_Interpreter_FOUND)
message(WARNING "Python3 not found, using python2 as a fallback")
find_package(Python2 COMPONENTS Interpreter REQUIRED)
if(Python2_VERSION VERSION_LESS 2.7)
message(SEND_ERROR "Python 2.7 or newer is required")
endif()

# Treat python2 as python3
add_executable(Python3::Interpreter IMPORTED)
set_target_properties(Python3::Interpreter PROPERTIES
IMPORTED_LOCATION ${Python2_EXECUTABLE})
set(Python3_EXECUTABLE ${Python2_EXECUTABLE})
endif()
endif()

# Ensure that fat libraries are built correctly on Darwin
Expand Down Expand Up @@ -273,7 +294,6 @@ append_list_if(CILKTOOLS_HAS_NODEFAULTLIBS_FLAG -nodefaultlibs SANITIZER_COMMON_
append_list_if(CILKTOOLS_HAS_Z_TEXT -Wl,-z,text SANITIZER_COMMON_LINK_FLAGS)

# if (CILKTOOLS_USE_BUILTINS_LIBRARY)
# list(APPEND SANITIZER_COMMON_LINK_LIBS ${CILKTOOLS_BUILTINS_LIBRARY})
# string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
# else()
append_list_if(CILKTOOLS_HAS_GCC_S_LIB gcc_s SANITIZER_COMMON_LINK_LIBS)
Expand Down Expand Up @@ -357,14 +377,11 @@ else()
set(CILKTOOLS_LLD_PATH ${LLVM_MAIN_SRC_DIR}/../lld)
if(EXISTS ${CILKTOOLS_LLD_PATH}/ AND LLVM_TOOL_LLD_BUILD)
set(CILKTOOLS_HAS_LLD TRUE)
else()
set(CILKTOOLS_HAS_LLD ${CILKTOOLS_HAS_FUSE_LD_LLD_FLAG})
endif()
endif()
pythonize_bool(CILKTOOLS_HAS_LLD)

include(AddCilktools)
include(SanitizerUtils)

set(ALL_CILKTOOLS csi;cilksan;cilkscale)
set(CILKTOOLS_TO_BUILD all CACHE STRING
Expand Down
18 changes: 12 additions & 6 deletions cmake/Modules/AddCilktools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ endfunction()

function(add_asm_sources output)
set(${output} ${ARGN} PARENT_SCOPE)
# Xcode will try to compile asm files as C ('clang -x c'), and that will fail.
if (${CMAKE_GENERATOR} STREQUAL "Xcode")
enable_language(ASM)
else()
# Pass ASM file directly to the C++ compiler.
# CMake doesn't pass the correct architecture for Apple prior to CMake 3.19. https://gitlab.kitware.com/cmake/cmake/-/issues/20771
# MinGW didn't work correctly with assembly prior to CMake 3.17. https://gitlab.kitware.com/cmake/cmake/-/merge_requests/4287 and https://reviews.llvm.org/rGb780df052dd2b246a760d00e00f7de9ebdab9d09
# Workaround these two issues by compiling as C.
# Same workaround used in libunwind. Also update there if changed here.
if((APPLE AND CMAKE_VERSION VERSION_LESS 3.19) OR (MINGW AND CMAKE_VERSION VERSION_LESS 3.17))
set_source_files_properties(${ARGN} PROPERTIES LANGUAGE C)
endif()
endfunction()
Expand Down Expand Up @@ -468,7 +468,13 @@ function(add_cilktools_test test_suite test_name arch)
# trump. With MSVC we can't do that because CMake is set up to run link.exe
# when linking, not the compiler. Here, we hack it to use the compiler
# because we want to use -fsanitize flags.
if(NOT MSVC)

# Only add CMAKE_EXE_LINKER_FLAGS when in a standalone bulid.
# Or else CMAKE_EXE_LINKER_FLAGS contains flags for build compiler of Clang/llvm.
# This might not be the same as what the COMPILER_RT_TEST_COMPILER supports.
# eg: the build compiler use lld linker and we build clang with default ld linker
# then to be tested clang will complain about lld options like --color-diagnostics.
if(NOT MSVC AND CILKTOOLS_STANDALONE_BUILD)
set(TEST_LINK_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TEST_LINK_FLAGS}")
separate_arguments(TEST_LINK_FLAGS)
endif()
Expand Down
95 changes: 77 additions & 18 deletions cmake/Modules/CilktoolsDarwinUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ function(find_darwin_sdk_dir var sdk_name)
endfunction()

function(find_darwin_sdk_version var sdk_name)
# We deliberately don't cache the result here because
# CMake's caching causes too many problems.
if (DARWIN_${sdk_name}_OVERRIDE_SDK_VERSION)
message(WARNING "Overriding ${sdk_name} SDK version to ${DARWIN_${sdk_name}_OVERRIDE_SDK_VERSION}")
set(${var} "${DARWIN_${sdk_name}_OVERRIDE_SDK_VERSION}" PARENT_SCOPE)
return()
endif()
set(result_process 1)
if(NOT DARWIN_PREFER_PUBLIC_SDK)
# Let's first try the internal SDK, otherwise use the public SDK.
Expand Down Expand Up @@ -166,25 +169,46 @@ function(darwin_test_archs os valid_archs)
CACHE STRING "List of valid architectures for platform ${os}." FORCE)
endfunction()

# This function checks the host cpusubtype to see if it is post-haswell. Haswell
# and later machines can run x86_64h binaries. Haswell is cpusubtype 8.
# This function checks the host cputype/cpusubtype to filter supported
# architecture for the host OS. This is used to determine which tests are
# available for the host.
function(darwin_filter_host_archs input output)
list_intersect(tmp_var DARWIN_osx_ARCHS ${input})
execute_process(
COMMAND sysctl hw.cpusubtype
OUTPUT_VARIABLE SUBTYPE)

string(REGEX MATCH "hw.cpusubtype: ([0-9]*)"
SUBTYPE_MATCHED "${SUBTYPE}")
set(HASWELL_SUPPORTED Off)
if(SUBTYPE_MATCHED)
if(${CMAKE_MATCH_1} GREATER 7)
set(HASWELL_SUPPORTED On)
COMMAND sysctl hw.cputype
OUTPUT_VARIABLE CPUTYPE)
string(REGEX MATCH "hw.cputype: ([0-9]*)"
CPUTYPE_MATCHED "${CPUTYPE}")
set(ARM_HOST Off)
if(CPUTYPE_MATCHED)
# ARM cputype is (0x01000000 | 12) and X86(_64) is always 7.
if(${CMAKE_MATCH_1} GREATER 11)
set(ARM_HOST On)
endif()
endif()
if(NOT HASWELL_SUPPORTED)
list(REMOVE_ITEM tmp_var x86_64h)

if(ARM_HOST)
list(REMOVE_ITEM tmp_var i386)
else()
list(REMOVE_ITEM tmp_var arm64)
list(REMOVE_ITEM tmp_var arm64e)
execute_process(
COMMAND sysctl hw.cpusubtype
OUTPUT_VARIABLE SUBTYPE)
string(REGEX MATCH "hw.cpusubtype: ([0-9]*)"
SUBTYPE_MATCHED "${SUBTYPE}")

set(HASWELL_SUPPORTED Off)
if(SUBTYPE_MATCHED)
if(${CMAKE_MATCH_1} GREATER 7)
set(HASWELL_SUPPORTED On)
endif()
endif()
if(NOT HASWELL_SUPPORTED)
list(REMOVE_ITEM tmp_var x86_64h)
endif()
endif()

set(${output} ${tmp_var} PARENT_SCOPE)
endfunction()

Expand Down Expand Up @@ -320,6 +344,38 @@ function(darwin_lipo_libs name)
endif()
endfunction()

# Filter the list of builtin sources for Darwin, then delegate to the generic
# filtering.
#
# `exclude_or_include` must be one of:
# - EXCLUDE: remove every item whose name (w/o extension) matches a name in
# `excluded_list`.
# - INCLUDE: keep only items whose name (w/o extension) matches something
# in `excluded_list`.
function(darwin_filter_builtin_sources output_var name exclude_or_include excluded_list)
if(exclude_or_include STREQUAL "EXCLUDE")
set(filter_action GREATER)
set(filter_value -1)
elseif(exclude_or_include STREQUAL "INCLUDE")
set(filter_action LESS)
set(filter_value 0)
else()
message(FATAL_ERROR "darwin_filter_builtin_sources called without EXCLUDE|INCLUDE")
endif()

set(intermediate ${ARGN})
foreach(_file ${intermediate})
get_filename_component(_name_we ${_file} NAME_WE)
list(FIND ${excluded_list} ${_name_we} _found)
if(_found ${filter_action} ${filter_value})
list(REMOVE_ITEM intermediate ${_file})
endif()
endforeach()

filter_builtin_sources(intermediate ${name})
set(${output_var} ${intermediate} PARENT_SCOPE)
endfunction()

# Generates builtin libraries for all operating systems specified in ARGN. Each
# OS library is constructed by lipo-ing together single-architecture libraries.
macro(darwin_add_builtin_libraries)
Expand All @@ -342,7 +398,8 @@ macro(darwin_add_builtin_libraries)
ARCH ${arch}
MIN_VERSION ${DARWIN_${os}_BUILTIN_MIN_VER})

filter_builtin_sources(filtered_sources
darwin_filter_builtin_sources(filtered_sources
${os}_${arch}
EXCLUDE ${arch}_${os}_EXCLUDED_BUILTINS
${${arch}_SOURCES})

Expand All @@ -364,7 +421,8 @@ macro(darwin_add_builtin_libraries)
OS ${os}
ARCH ${arch})

filter_builtin_sources(filtered_sources
darwin_filter_builtin_sources(filtered_sources
cc_kext_${os}_${arch}
EXCLUDE ${arch}_${os}_EXCLUDED_BUILTINS
${${arch}_SOURCES})

Expand Down Expand Up @@ -460,7 +518,8 @@ macro(darwin_add_embedded_builtin_libraries)
set(x86_64_FUNCTIONS ${common_FUNCTIONS})

foreach(arch ${DARWIN_macho_embedded_ARCHS})
filter_builtin_sources(${arch}_filtered_sources
darwin_filter_builtin_sources(${arch}_filtered_sources
macho_embedded_${arch}
INCLUDE ${arch}_FUNCTIONS
${${arch}_SOURCES})
if(NOT ${arch}_filtered_sources)
Expand Down
60 changes: 32 additions & 28 deletions cmake/Modules/CilktoolsUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ macro(detect_target_arch)
check_symbol_exists(__sparcv9 "" __SPARCV9)
check_symbol_exists(__wasm32__ "" __WEBASSEMBLY32)
check_symbol_exists(__wasm64__ "" __WEBASSEMBLY64)
check_symbol_exists(__ve__ "" __VE)
if(__ARM)
add_default_target_arch(arm)
elseif(__AARCH64)
Expand Down Expand Up @@ -200,6 +201,8 @@ macro(detect_target_arch)
add_default_target_arch(wasm32)
elseif(__WEBASSEMBLY64)
add_default_target_arch(wasm64)
elseif(__VE)
add_default_target_arch(ve)
endif()
endmacro()

Expand Down Expand Up @@ -248,6 +251,8 @@ macro(load_llvm_config)
string(REGEX REPLACE "[ \t]*[\r\n]+[ \t]*" ";" CONFIG_OUTPUT ${CONFIG_OUTPUT})
list(GET CONFIG_OUTPUT 0 LDFLAGS)
list(GET CONFIG_OUTPUT 1 LIBLIST)
file(TO_CMAKE_PATH "${LDFLAGS}" LDFLAGS)
file(TO_CMAKE_PATH "${LIBLIST}" LIBLIST)
set(LLVM_XRAY_LDFLAGS ${LDFLAGS} CACHE STRING "Linker flags for LLVMXRay library")
set(LLVM_XRAY_LIBLIST ${LIBLIST} CACHE STRING "Library list for LLVMXRay")
set(CILKTOOLS_HAS_LLVMXRAY TRUE)
Expand All @@ -261,13 +266,15 @@ macro(load_llvm_config)
ERROR_QUIET)
if (HAD_ERROR)
message(WARNING "llvm-config finding testingsupport failed with status ${HAD_ERROR}")
else()
elseif(CILKTOOLS_INCLUDE_TESTS)
string(REGEX REPLACE "[ \t]*[\r\n]+[ \t]*" ";" CONFIG_OUTPUT ${CONFIG_OUTPUT})
list(GET CONFIG_OUTPUT 0 LDFLAGS)
list(GET CONFIG_OUTPUT 1 LIBLIST)
if (LIBLIST STREQUAL "")
message(WARNING "testingsupport library not installed, some tests will be skipped")
else()
file(TO_CMAKE_PATH "${LDFLAGS}" LDFLAGS)
file(TO_CMAKE_PATH "${LIBLIST}" LIBLIST)
set(LLVM_TESTINGSUPPORT_LDFLAGS ${LDFLAGS} CACHE STRING "Linker flags for LLVMTestingSupport library")
set(LLVM_TESTINGSUPPORT_LIBLIST ${LIBLIST} CACHE STRING "Library list for LLVMTestingSupport")
set(CILKTOOLS_HAS_LLVMTESTINGSUPPORT TRUE)
Expand Down Expand Up @@ -325,33 +332,30 @@ macro(construct_cilktools_default_triple)
endif()
endmacro()

# Filter out generic versions of routines that are re-implemented in
# architecture specific manner. This prevents multiple definitions of the
# same symbols, making the symbol selection non-deterministic.
function(filter_builtin_sources output_var exclude_or_include excluded_list)
if(exclude_or_include STREQUAL "EXCLUDE")
set(filter_action GREATER)
set(filter_value -1)
elseif(exclude_or_include STREQUAL "INCLUDE")
set(filter_action LESS)
set(filter_value 0)
else()
message(FATAL_ERROR "filter_builtin_sources called without EXCLUDE|INCLUDE")
endif()

set(intermediate ${ARGN})
foreach (_file ${intermediate})
get_filename_component(_name_we ${_file} NAME_WE)
list(FIND ${excluded_list} ${_name_we} _found)
if(_found ${filter_action} ${filter_value})
list(REMOVE_ITEM intermediate ${_file})
elseif(${_file} MATCHES ".*/.*\\.S" OR ${_file} MATCHES ".*/.*\\.c")
# Filter out generic versions of routines that are re-implemented in an
# architecture specific manner. This prevents multiple definitions of the same
# symbols, making the symbol selection non-deterministic.
#
# We follow the convention that a source file that exists in a sub-directory
# (e.g. `ppc/divtc3.c`) is architecture-specific and that if a generic
# implementation exists it will be a top-level source file with the same name
# modulo the file extension (e.g. `divtc3.c`).
function(filter_builtin_sources inout_var name)
set(intermediate ${${inout_var}})
foreach(_file ${intermediate})
get_filename_component(_file_dir ${_file} DIRECTORY)
if (NOT "${_file_dir}" STREQUAL "")
# Architecture specific file. If a generic version exists, print a notice
# and ensure that it is removed from the file list.
get_filename_component(_name ${_file} NAME)
string(REPLACE ".S" ".c" _cname "${_name}")
list(REMOVE_ITEM intermediate ${_cname})
endif ()
endforeach ()
set(${output_var} ${intermediate} PARENT_SCOPE)
string(REGEX REPLACE "\\.S$" ".c" _cname "${_name}")
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_cname}")
message(STATUS "For ${name} builtins preferring ${_file} to ${_cname}")
list(REMOVE_ITEM intermediate ${_cname})
endif()
endif()
endforeach()
set(${inout_var} ${intermediate} PARENT_SCOPE)
endfunction()

function(get_cilktools_target arch variable)
Expand All @@ -361,7 +365,7 @@ function(get_cilktools_target arch variable)
# Use exact spelling when building only for the target specified to CMake.
set(target "${CILKTOOLS_DEFAULT_TARGET_TRIPLE}")
elseif(ANDROID AND ${arch} STREQUAL "i386")
set(target "i686${CILKTOOLS_OS_SUFFIX}${triple_suffix}")
set(target "i686${triple_suffix}")
else()
set(target "${arch}${triple_suffix}")
endif()
Expand Down
Loading

0 comments on commit 2f5e199

Please sign in to comment.