vcpkg_configure_make: Support macOS cross-compile (#15659)

* vcpkg_configure_make: Support macOS cross-compile

* Move compiler flags logic to get_cmake_vars

* Better match the arch behavior of config.guess

* Apply suggestions from code review

Co-authored-by: Billy O'Neal <bion@microsoft.com>

Co-authored-by: Billy O'Neal <bion@microsoft.com>
This commit is contained in:
pastdue 2021-01-28 22:38:32 -05:00 committed by GitHub
parent b063d0cee7
commit 7115ef469d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 8 deletions

View File

@ -139,6 +139,26 @@ macro(_vcpkg_determine_autotools_target_cpu out_var)
endif()
endmacro()
macro(_vcpkg_determine_autotools_host_arch_mac out_var)
set(${out_var} "${VCPKG_DETECTED_CMAKE_HOST_SYSTEM_PROCESSOR}")
endmacro()
macro(_vcpkg_determine_autotools_target_arch_mac out_var)
list(LENGTH VCPKG_OSX_ARCHITECTURES _num_osx_archs)
if(_num_osx_archs GREATER_EQUAL 2)
set(${out_var} "universal")
else()
# Better match the arch behavior of config.guess
# See: https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD
if(VCPKG_OSX_ARCHITECTURES MATCHES "^(ARM|arm)64$")
set(${out_var} "aarch64")
else()
set(${out_var} "${VCPKG_OSX_ARCHITECTURES}")
endif()
endif()
unset(_num_osx_archs)
endmacro()
macro(_vcpkg_backup_env_variable envvar)
if(DEFINED ENV{${envvar}})
set(${envvar}_BACKUP "$ENV{${envvar}}")
@ -407,6 +427,25 @@ function(vcpkg_configure_make)
set(prefix_var "\${prefix}")
endif()
# macOS - cross-compiling support
if(VCPKG_TARGET_IS_OSX)
if (_csc_AUTOCONFIG AND NOT _csc_BUILD_TRIPLET OR _csc_DETERMINE_BUILD_TRIPLET)
_vcpkg_determine_autotools_host_arch_mac(BUILD_ARCH) # machine you are building on => --build=
_vcpkg_determine_autotools_target_arch_mac(TARGET_ARCH)
# --build: the machine you are building on
# --host: the machine you are building for
# --target: the machine that CC will produce binaries for
# https://stackoverflow.com/questions/21990021/how-to-determine-host-value-for-configure-when-using-cross-compiler
# Only for ports using autotools so we can assume that they follow the common conventions for build/target/host
set(_csc_BUILD_TRIPLET "--build=${BUILD_ARCH}-apple-darwin")
if(NOT "${TARGET_ARCH}" STREQUAL "${BUILD_ARCH}") # we don't need to specify the additional flags if we build natively.
list(APPEND _csc_BUILD_TRIPLET "--host=${TARGET_ARCH}-apple-darwin") # (Host activates crosscompilation; The name given here is just the prefix of the host tools for the target)
endif()
debug_message("Using make triplet: ${_csc_BUILD_TRIPLET}")
endif()
endif()
# Cleanup previous build dirs
file(REMOVE_RECURSE "${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel"
"${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg"

View File

@ -48,6 +48,12 @@ foreach(flag IN LISTS FLAGS)
endforeach()
list(REMOVE_DUPLICATES VCPKG_DEFAULT_FLAGS_TO_CHECK)
#Language-specific flags.
foreach(_lang IN LISTS VCPKG_LANGUAGES)
list(APPEND VCPKG_LANG_FLAGS CMAKE_${_lang}_FLAGS)
endforeach()
list(REMOVE_DUPLICATES VCPKG_LANG_FLAGS)
# TODO if ever necessary: Properties to check
set(VCPKG_VAR_PREFIX "VCPKG_DETECTED" CACHE STRING "Variable prefix to use for detected flags")
@ -81,17 +87,31 @@ foreach(_env IN LISTS VCPKG_ENV_VARS_TO_CHECK)
endif()
endforeach()
macro(_vcpkg_adjust_flags flag_var)
if(MSVC) # Transform MSVC /flags to -flags due to bash scripts intepreting /flag as a path.
string(REGEX REPLACE "(^| )/" "\\1-" ${flag_var} "${${flag_var}}")
endif()
if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
if("${flag_var}" IN_LIST VCPKG_LANG_FLAGS)
# macOS - append arch and isysroot if cross-compiling
if(NOT "${CMAKE_OSX_ARCHITECTURES}" STREQUAL "${CMAKE_HOST_SYSTEM_PROCESSOR}")
foreach(arch IN LISTS CMAKE_OSX_ARCHITECTURES)
string(APPEND ${flag_var} " -arch ${arch}")
endforeach()
string(APPEND ${flag_var} " -isysroot ${CMAKE_OSX_SYSROOT}")
endif()
endif()
endif()
endmacro()
foreach(flag IN LISTS VCPKG_FLAGS_TO_CHECK)
string(STRIP "${${flag}}" ${flag}) # Strip leading and trailing whitespaces
if(MSVC) # Transform MSVC /flags to -flags due to bash scripts intepreting /flag as a path.
string(REGEX REPLACE "(^| )/" "\\1-" ${flag} "${${flag}}")
endif()
_vcpkg_adjust_flags(${flag})
string(APPEND OUTPUT_STRING "set(${VCPKG_VAR_PREFIX}_RAW_${flag} \" ${${flag}}\")\n")
foreach(config IN LISTS VCPKG_CONFIGS)
string(STRIP "${${flag}_${config}}" ${flag}_${config})
if(MSVC)
string(REGEX REPLACE "(^| )/" "\\1-" ${flag}_${config} "${${flag}_${config}}")
endif()
_vcpkg_adjust_flags(${flag}_${config})
string(APPEND OUTPUT_STRING "set(${VCPKG_VAR_PREFIX}_RAW_${flag}_${config} \"${CMAKE_${flag}_FLAGS_${config}}\")\n")
set(COMBINED_${flag}_${config} "${${flag}} ${${flag}_${config}}")
string(STRIP "${COMBINED_${flag}_${config}}" COMBINED_${flag}_${config})