From e8cc691230f70198d81f38b8523ed153af197051 Mon Sep 17 00:00:00 2001 From: James Donald Date: Thu, 17 Nov 2016 19:25:44 -0800 Subject: [PATCH 1/4] Add -pie flag for Android --- standard/toolchain-android.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/standard/toolchain-android.cmake b/standard/toolchain-android.cmake index dcb9200f8..3000d883c 100644 --- a/standard/toolchain-android.cmake +++ b/standard/toolchain-android.cmake @@ -21,7 +21,7 @@ if(NOT ANDROID_NDK_ROOT) message(FATAL_ERROR "ANDROID_NDK_ROOT could not be located. Have you installed the NDK?") endif() -set(ANDROID_COMMON_FLAGS " --sysroot=${ANDROID_NDK_ROOT}/sysroot -B${ANDROID_NDK_ROOT} ") +set(ANDROID_COMMON_FLAGS " --sysroot=${ANDROID_NDK_ROOT}/sysroot -B${ANDROID_NDK_ROOT} -pie ") set(CMAKE_C_COMPILER ${ANDROID_NDK_ROOT}/bin/${ANDROID_NDK_TOOL_PREFIX}-gcc${_exe_suffix} CACHE FILEPATH "C Compiler" FORCE) set(CMAKE_CXX_COMPILER ${ANDROID_NDK_ROOT}/bin/${ANDROID_NDK_TOOL_PREFIX}-g++${_exe_suffix} CACHE FILEPATH "C++ Compiler" FORCE) set(ANDROID_STRIP_COMMAND ${ANDROID_NDK_ROOT}/bin/${ANDROID_NDK_TOOL_PREFIX}-strip) From e2b6bdf72b123103a8bd12c2f6e8bbd0b2f2a09a Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Mon, 22 May 2017 12:57:22 -0700 Subject: [PATCH 2/4] Fix arm toolchains, allow proper aarch64 support --- standard/StandardProject.cmake | 6 ++++-- standard/standard-configVersion.cmake.in | 9 +++++++-- standard/toolchain-android.cmake | 1 - standard/toolchain-android32.cmake | 1 + standard/toolchain-android64.cmake | 1 + standard/toolchain-arm32.cmake | 5 ----- standard/toolchain-arm64.cmake | 7 +------ 7 files changed, 14 insertions(+), 16 deletions(-) diff --git a/standard/StandardProject.cmake b/standard/StandardProject.cmake index fe66a8cc4..c7aa6257f 100644 --- a/standard/StandardProject.cmake +++ b/standard/StandardProject.cmake @@ -54,8 +54,10 @@ function(standard_project_preinit) # Need to classify the architecture before we run anything else, this lets us easily # configure the find version file based on what the architecture was actually built to # be. - if(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm") - set(standard_BUILD_ARM ON PARENT_SCOPE) + if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64") + set(standard_BUILD_ARCHITECTURES "aarch64" PARENT_SCOPE) + set(standard_BUILD_64 ON PARENT_SCOPE) + elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm") set(standard_BUILD_ARCHITECTURES "arm" PARENT_SCOPE) set(standard_BUILD_64 OFF PARENT_SCOPE) elseif(CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64;i386") diff --git a/standard/standard-configVersion.cmake.in b/standard/standard-configVersion.cmake.in index 0796e2635..6e8fc2a73 100644 --- a/standard/standard-configVersion.cmake.in +++ b/standard/standard-configVersion.cmake.in @@ -16,7 +16,8 @@ if(DEFINED @ARG_NAME@_ARCHITECTURE) foreach(i IN LISTS @ARG_NAME@_ARCHITECTURE) string(REGEX MATCH "amd64|x86_64|x64" @ARG_NAME@_is_x64 ${i}) string(REGEX MATCH "i386|x86" @ARG_NAME@_is_x86 ${i}) - string(REGEX MATCH "arm" @ARG_NAME@_is_arm ${i}) + string(REGEX MATCH "arm$" @ARG_NAME@_is_arm ${i}) + string(REGEX MATCH "arm64|aarch64" @ARG_NAME@_is_aarch64) # Classify if(@ARG_NAME@_is_x86) @@ -25,13 +26,17 @@ if(DEFINED @ARG_NAME@_ARCHITECTURE) list(APPEND @ARG_NAME@_ARCHITECTURES x64) elseif(@ARG_NAME@_is_arm) list(APPEND @ARG_NAME@_ARCHITECTURES arm) + elseif(@ARG_NAME@_is_aarch64) + list(APPEND @ARG_NAME@_ARCHITECTURES aarch64) else() message(WARNING "Unrecognized architecture ${i}") endif() endforeach() else() # Try to infer what the user wants - if(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm") + if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64") + set(@ARG_NAME@_ARCHITECTURES aarch64) + elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm") set(@ARG_NAME@_ARCHITECTURES arm) elseif(CMAKE_SIZEOF_VOID_P EQUAL "8") set(@ARG_NAME@_ARCHITECTURES x64) diff --git a/standard/toolchain-android.cmake b/standard/toolchain-android.cmake index dcb9200f8..8fa52f2c3 100644 --- a/standard/toolchain-android.cmake +++ b/standard/toolchain-android.cmake @@ -1,5 +1,4 @@ set(CMAKE_SYSTEM_NAME Linux) -set(CMAKE_SYSTEM_PROCESSOR arm) set(CMAKE_SYSTEM_VERSION 1) # For reasons beyond my comprehension, this file is parsed multiple times diff --git a/standard/toolchain-android32.cmake b/standard/toolchain-android32.cmake index d8bd62954..2c64b97de 100644 --- a/standard/toolchain-android32.cmake +++ b/standard/toolchain-android32.cmake @@ -1,3 +1,4 @@ set(ARM_TARGET "armeabi-v7a") set(ANDROID_NDK_TOOL_PREFIX arm-linux-androideabi) +set(CMAKE_SYSTEM_PROCESSOR arm) include(${CMAKE_CURRENT_LIST_DIR}/toolchain-android.cmake) diff --git a/standard/toolchain-android64.cmake b/standard/toolchain-android64.cmake index 21810e7a1..4f11d886a 100644 --- a/standard/toolchain-android64.cmake +++ b/standard/toolchain-android64.cmake @@ -1,3 +1,4 @@ set(ANDROID_NDK_TOOL_PREFIX aarch64-linux-android) set(_ndk_suffix -4.9-aarch64) +set(CMAKE_SYSTEM_PROCESSOR aarch64) include(${CMAKE_CURRENT_LIST_DIR}/toolchain-android.cmake) diff --git a/standard/toolchain-arm32.cmake b/standard/toolchain-arm32.cmake index 4a5b29b89..14d2fa979 100644 --- a/standard/toolchain-arm32.cmake +++ b/standard/toolchain-arm32.cmake @@ -6,8 +6,3 @@ set(CMAKE_CXX_COMPILER /usr/bin/arm-linux-gnueabihf-g++) set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) - -foreach(config IN LISTS CMAKE_CONFIGURATION_TYPES) - string(TOUPPER ${config} config) - string(CONCAT CMAKE_${config}_POSTFIX "${CMAKE_${config}_POSTFIX}" "64") -endforeach() diff --git a/standard/toolchain-arm64.cmake b/standard/toolchain-arm64.cmake index 711b942d8..70d9f1949 100644 --- a/standard/toolchain-arm64.cmake +++ b/standard/toolchain-arm64.cmake @@ -1,13 +1,8 @@ set(CMAKE_SYSTEM_NAME Linux) -set(CMAKE_SYSTEM_PROCESSOR arm) +set(CMAKE_SYSTEM_PROCESSOR aarch64) set(CMAKE_SYSTEM_VERSION 1) set(CMAKE_C_COMPILER /usr/bin/aarch64-linux-gnu-gcc) set(CMAKE_CXX_COMPILER /usr/bin/aarch64-linux-gnu-g++) set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) - -foreach(config IN LISTS CMAKE_CONFIGURATION_TYPES) - string(TOUPPER ${config} config) - string(CONCAT CMAKE_${config}_POSTFIX "${CMAKE_${config}_POSTFIX}" "64") -endforeach() From d47d2152e2f3db52d608cb46315d6a352211f467 Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Mon, 22 May 2017 15:22:29 -0700 Subject: [PATCH 3/4] Fix serious bug in standard_project CMake searches the root Cmake file for 'project', so using stanard_project as a macro causes it to insert it's own call to project(C CXX) at the very beginning. CMAKE_SYSTEM_PROCESSOR is set in the toolchain, and so must be queried AFTER the first call to project --- standard/StandardProject.cmake | 78 ++++++++++++++++---------------- standard/toolchain-android.cmake | 5 +- 2 files changed, 42 insertions(+), 41 deletions(-) diff --git a/standard/StandardProject.cmake b/standard/StandardProject.cmake index c7aa6257f..60880ec83 100644 --- a/standard/StandardProject.cmake +++ b/standard/StandardProject.cmake @@ -18,8 +18,13 @@ include(CMakeParseArguments) # Backwards compatibility # This must be a macro since project defines scope-local variables # that we generally rely on being in the root context. +# IMPORTANT: Note that if there is no *direct* call to 'project' in the root CMakeLists.txt +# file, CMake will automatically inject one with the default languages of C and CXX. +# This will cause the toolchain file to be parsed multiple times, and will +# make much of the work done in standard_project_preinit useless. macro(standard_project project_name) cmake_parse_arguments(standard "" "VERSION" "LANGUAGES" ${ARGN}) + if(NOT standard_VERSION) message(FATAL_ERROR "Standard compliant projects must specify a version") endif() @@ -50,27 +55,23 @@ function(standard_project_preinit) set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: Debug Release." FORCE) endif() - # These do not strictly *have* to be set prior to project, but they can be so we will - # Need to classify the architecture before we run anything else, this lets us easily - # configure the find version file based on what the architecture was actually built to - # be. - if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64") - set(standard_BUILD_ARCHITECTURES "aarch64" PARENT_SCOPE) - set(standard_BUILD_64 ON PARENT_SCOPE) - elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm") - set(standard_BUILD_ARCHITECTURES "arm" PARENT_SCOPE) - set(standard_BUILD_64 OFF PARENT_SCOPE) - elseif(CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64;i386") - set(standard_BUILD_ARCHITECTURES x64 x86 PARENT_SCOPE) - set(standard_BUILD_64 ON PARENT_SCOPE) - elseif(CMAKE_SIZEOF_VOID_P STREQUAL 4) - set(standard_BUILD_ARCHITECTURES "x86" PARENT_SCOPE) - set(standard_BUILD_64 OFF PARENT_SCOPE) - else() - set(standard_BUILD_ARCHITECTURES "x64" PARENT_SCOPE) - set(standard_BUILD_64 ON PARENT_SCOPE) + if(APPLE AND NOT (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm")) + if(NOT CMAKE_OSX_SYSROOT) + # CLANG_VERSION requires a sysroot to obtain, so resort to execute_process() here + execute_process(COMMAND clang -v ERROR_VARIABLE _clang_version) + if(_clang_version MATCHES "clang-7") + set(_developer_sdk_version 10.11) + elseif(_clang_version MATCHES "clang-8") + set(_developer_sdk_version 10.12) + endif() + if(_developer_sdk_version) + set(CMAKE_OSX_SYSROOT "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX${_developer_sdk_version}.sdk" CACHE STRING "Mac OS X build environment" FORCE) + if(NOT CMAKE_OSX_DEPLOYMENT_TARGET) + set(CMAKE_OSX_DEPLOYMENT_TARGET "10.9" CACHE STRING "Mac OS X deployment target" FORCE) + endif() + endif() + endif() endif() - message(STATUS "Using architecture: ${standard_BUILD_ARCHITECTURES}") # All of our binaries go to one place: The binaries output directory. We only want to tinker # with this if we're building by ourselves, otherwise we just do whatever the enclosing project @@ -110,24 +111,6 @@ function(standard_project_preinit) set(CMAKE_CXX_STANDARD_REQUIRED ON PARENT_SCOPE) set(CMAKE_CXX_EXTENSIONS OFF PARENT_SCOPE) - if(APPLE AND NOT (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm")) - if(NOT CMAKE_OSX_SYSROOT) - # CLANG_VERSION requires a sysroot to obtain, so resort to execute_process() here - execute_process(COMMAND clang -v ERROR_VARIABLE _clang_version) - if(_clang_version MATCHES "clang-7") - set(_developer_sdk_version 10.11) - elseif(_clang_version MATCHES "clang-8") - set(_developer_sdk_version 10.12) - endif() - if(_developer_sdk_version) - set(CMAKE_OSX_SYSROOT "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX${_developer_sdk_version}.sdk" CACHE STRING "Mac OS X build environment" FORCE) - if(NOT CMAKE_OSX_DEPLOYMENT_TARGET) - set(CMAKE_OSX_DEPLOYMENT_TARGET "10.9" CACHE STRING "Mac OS X deployment target" FORCE) - endif() - endif() - endif() - endif() - # CMAKE_OSX_DEPLOYMENT_TARGET < 10.9 implies -stdlib=libstdc++, which doesn't have # complete C++11 support. Override with libc++ if(DEFINED CMAKE_OSX_DEPLOYMENT_TARGET AND CMAKE_OSX_DEPLOYMENT_TARGET VERSION_LESS 10.9) @@ -137,6 +120,25 @@ function(standard_project_preinit) endfunction() function(standard_project_postinit) + # CMAKE_SYSTEM_PROCESSOR is set by the toolchain, so must happen strictly after project() + if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64") + set(standard_BUILD_ARCHITECTURES "aarch64" PARENT_SCOPE) + set(standard_BUILD_64 ON PARENT_SCOPE) + elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm") + set(standard_BUILD_ARCHITECTURES "arm" PARENT_SCOPE) + set(standard_BUILD_64 OFF PARENT_SCOPE) + elseif(CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64;i386") + set(standard_BUILD_ARCHITECTURES x64 x86 PARENT_SCOPE) + set(standard_BUILD_64 ON PARENT_SCOPE) + elseif(CMAKE_SIZEOF_VOID_P STREQUAL 4) + set(standard_BUILD_ARCHITECTURES "x86" PARENT_SCOPE) + set(standard_BUILD_64 OFF PARENT_SCOPE) + else() + set(standard_BUILD_ARCHITECTURES "x64" PARENT_SCOPE) + set(standard_BUILD_64 ON PARENT_SCOPE) + endif() + message(STATUS "Using architecture: ${standard_BUILD_ARCHITECTURES}") + # Post-initialization steps. All of these depend on project() having been called. include(CTest) diff --git a/standard/toolchain-android.cmake b/standard/toolchain-android.cmake index 8fa52f2c3..8f31e7caf 100644 --- a/standard/toolchain-android.cmake +++ b/standard/toolchain-android.cmake @@ -1,9 +1,8 @@ set(CMAKE_SYSTEM_NAME Linux) set(CMAKE_SYSTEM_VERSION 1) -# For reasons beyond my comprehension, this file is parsed multiple times -# and in some of them, cache variables are not preserved so we have to store -# this in an environmental variable... +# Deal with weirdnesses caused by our standard_project setup. See +# notes in StandardProject.cmake if(NOT LLVM_ANDROID_TOOLCHAIN_DIR) set(LLVM_ANDROID_TOOLCHAIN_DIR $ENV{LLVM_ANDROID_TOOLCHAIN_DIR}) else() From f7491738032cc374a09c348eb4af0b8e973caedb Mon Sep 17 00:00:00 2001 From: Walter Gray Date: Mon, 22 May 2017 15:31:47 -0700 Subject: [PATCH 4/4] Set CMAKE_ Variables in post-init unless absolutely nessecary --- standard/StandardProject.cmake | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/standard/StandardProject.cmake b/standard/StandardProject.cmake index 60880ec83..5a723cb62 100644 --- a/standard/StandardProject.cmake +++ b/standard/StandardProject.cmake @@ -72,7 +72,9 @@ function(standard_project_preinit) endif() endif() endif() +endfunction() +function(standard_project_postinit) # All of our binaries go to one place: The binaries output directory. We only want to tinker # with this if we're building by ourselves, otherwise we just do whatever the enclosing project # wants us to do. @@ -117,9 +119,6 @@ function(standard_project_preinit) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++" PARENT_SCOPE) endif() -endfunction() - -function(standard_project_postinit) # CMAKE_SYSTEM_PROCESSOR is set by the toolchain, so must happen strictly after project() if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64") set(standard_BUILD_ARCHITECTURES "aarch64" PARENT_SCOPE) @@ -139,7 +138,6 @@ function(standard_project_postinit) endif() message(STATUS "Using architecture: ${standard_BUILD_ARCHITECTURES}") - # Post-initialization steps. All of these depend on project() having been called. include(CTest) if(CMAKE_COMPILER_IS_GNUCC)