From b70f16041439aa070ad1bc759d634a4c5f6587d8 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Fri, 24 Jun 2022 23:43:11 +0800 Subject: [PATCH] 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. --- third-party/CMakeLists.txt | 13 +++++++++++++ third-party/cmake/BuildLuv.cmake | 4 +++- third-party/cmake/BuildMsgpack.cmake | 4 +++- third-party/cmake/BuildTreesitterParsers.cmake | 4 +++- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/third-party/CMakeLists.txt b/third-party/CMakeLists.txt index 9f1b3f0706..6fe5c2baf5 100644 --- a/third-party/CMakeLists.txt +++ b/third-party/CMakeLists.txt @@ -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}") diff --git a/third-party/cmake/BuildLuv.cmake b/third-party/cmake/BuildLuv.cmake index 001f5a325a..6e9a333dc8 100644 --- a/third-party/cmake/BuildLuv.cmake +++ b/third-party/cmake/BuildLuv.cmake @@ -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 diff --git a/third-party/cmake/BuildMsgpack.cmake b/third-party/cmake/BuildMsgpack.cmake index a89c1e34d0..10bf1c8e37 100644 --- a/third-party/cmake/BuildMsgpack.cmake +++ b/third-party/cmake/BuildMsgpack.cmake @@ -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}) diff --git a/third-party/cmake/BuildTreesitterParsers.cmake b/third-party/cmake/BuildTreesitterParsers.cmake index 4ceb402455..11ffb792de 100644 --- a/third-party/cmake/BuildTreesitterParsers.cmake +++ b/third-party/cmake/BuildTreesitterParsers.cmake @@ -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 |)