build(deps): support universal builds on macOS

CMake can handle building universal binaries on macOS using the
`CMAKE_OSX_ARCHITECTURES` variable. Let's pass this variable to the
relevant dep builds. We use the `LIST_SEPARATOR` argument to prevent the
shell from interpreting the `;` that CMake uses as a list separator.

For dependencies that don't build using CMake, we only need to make sure
that the compiler is invoked with the correct `-arch` flags. The
compiler does the rest.

The only exception to this is the LuaJIT build, which we handle
separately as a special case.
This commit is contained in:
Carlo Cabrera 2022-06-24 23:43:11 +08:00
parent adcaf24f42
commit b70f160414
No known key found for this signature in database
GPG Key ID: C74D447FC549A1D0
4 changed files with 22 additions and 3 deletions

View File

@ -119,6 +119,19 @@ if(CMAKE_OSX_SYSROOT)
endif()
endif()
if(CMAKE_OSX_ARCHITECTURES)
string(REPLACE ";" "|" CMAKE_OSX_ARCHITECTURES_ALT_SEP "${CMAKE_OSX_ARCHITECTURES}")
# The LuaJIT build does not like being passed multiple `-arch` flags
# so we handle a universal build the old-fashioned way.
set(LUAJIT_C_COMPILER "${DEPS_C_COMPILER}")
foreach(ARCH IN LISTS CMAKE_OSX_ARCHITECTURES)
set(DEPS_C_COMPILER "${DEPS_C_COMPILER} -arch ${ARCH}")
if(DEPS_CXX_COMPILER)
set(DEPS_CXX_COMPILER "${DEPS_CXX_COMPILER} -arch ${ARCH}")
endif()
endforeach()
endif()
set(HOSTDEPS_INSTALL_DIR "${DEPS_INSTALL_DIR}")
set(HOSTDEPS_BIN_DIR "${DEPS_BIN_DIR}")
set(HOSTDEPS_LIB_DIR "${DEPS_LIB_DIR}")

View File

@ -48,7 +48,8 @@ function(BuildLuv)
PATCH_COMMAND "${_luv_PATCH_COMMAND}"
CONFIGURE_COMMAND "${_luv_CONFIGURE_COMMAND}"
BUILD_COMMAND "${_luv_BUILD_COMMAND}"
INSTALL_COMMAND "${_luv_INSTALL_COMMAND}")
INSTALL_COMMAND "${_luv_INSTALL_COMMAND}"
LIST_SEPARATOR |)
endfunction()
set(LUV_SRC_DIR ${DEPS_BUILD_DIR}/src/luv)
@ -65,6 +66,7 @@ set(LUV_CONFIGURE_COMMAND_COMMON
-DCMAKE_GENERATOR=${CMAKE_GENERATOR}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_INSTALL_PREFIX=${DEPS_INSTALL_DIR}
-DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES_ALT_SEP}
-DLUA_BUILD_TYPE=System
-DLUA_COMPAT53_DIR=${DEPS_BUILD_DIR}/src/lua-compat-5.3
-DWITH_SHARED_LIBUV=ON

View File

@ -27,7 +27,8 @@ function(BuildMsgpack)
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/DownloadAndExtractFile.cmake
CONFIGURE_COMMAND "${_msgpack_CONFIGURE_COMMAND}"
BUILD_COMMAND "${_msgpack_BUILD_COMMAND}"
INSTALL_COMMAND "${_msgpack_INSTALL_COMMAND}")
INSTALL_COMMAND "${_msgpack_INSTALL_COMMAND}"
LIST_SEPARATOR |)
endfunction()
set(MSGPACK_CONFIGURE_COMMAND ${CMAKE_COMMAND} ${DEPS_BUILD_DIR}/src/msgpack
@ -36,6 +37,7 @@ set(MSGPACK_CONFIGURE_COMMAND ${CMAKE_COMMAND} ${DEPS_BUILD_DIR}/src/msgpack
-DCMAKE_INSTALL_PREFIX=${DEPS_INSTALL_DIR}
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES_ALT_SEP}
"-DCMAKE_C_FLAGS:STRING=${CMAKE_C_COMPILER_ARG1} -fPIC"
-DCMAKE_GENERATOR=${CMAKE_GENERATOR})

View File

@ -19,9 +19,11 @@ CMAKE_ARGS
-DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DCMAKE_INSTALL_PREFIX=${DEPS_INSTALL_DIR}
-DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES_ALT_SEP}
# Pass toolchain
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
-DPARSERLANG=c
BUILD_COMMAND ${CMAKE_COMMAND} --build . --config ${CMAKE_BUILD_TYPE}
INSTALL_COMMAND ${CMAKE_COMMAND} --build . --target install --config ${CMAKE_BUILD_TYPE})
INSTALL_COMMAND ${CMAKE_COMMAND} --build . --target install --config ${CMAKE_BUILD_TYPE}
LIST_SEPARATOR |)