From fd2726c1b16271c446272283584ac95f2519cf53 Mon Sep 17 00:00:00 2001 From: xulei Date: Fri, 7 Jan 2022 15:02:26 +0800 Subject: [PATCH] improve mac complie and bugfix --- CMakeLists.txt | 39 ++++++++++++++++++++++++++-- mindspore/ccsrc/ps/core/comm_util.cc | 2 +- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ab84e29916..fbbeff379cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,10 +19,45 @@ if(NOT CMAKE_SYSTEM_NAME MATCHES "Windows") endif() if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") - exec_program(sw_vers ARGS -productVersion OUTPUT_VARIABLE MACOSX_SYSTEM_VERSION) + # find appropriate macosx SDK and set SDKROOT + if(NOT DEFINED ENV{SDKROOT}) + # arm64: macosx11.x + # x86_64: macosx10.x, macosx11.x + if(${CMAKE_HOST_SYSTEM_PROCESSOR} MATCHES "arm64") + set(MACOSX_SDK_REGEX "MacOSX11(\\.\\d+)?") + else() + set(MACOSX_SDK_REGEX "MacOSX1[01](\\.\\d+)?") + endif() + exec_program(xcrun ARGS --show-sdk-path OUTPUT_VARIABLE MACOSX_SDK_PATH) + get_filename_component(MACOSX_SDK_PATH ${MACOSX_SDK_PATH} DIRECTORY) + file(GLOB ALL_SDK_NAME RELATIVE ${MACOSX_SDK_PATH} ${MACOSX_SDK_PATH}/*) + # get highest SDK version meets the requirements + execute_process( + COMMAND bash -c "echo '${ALL_SDK_NAME}' | grep -Eo '${MACOSX_SDK_REGEX}' | sort -n | tail -1 | tr -d '\\n'" + OUTPUT_VARIABLE MACOSX_FIND_SDK_NAME + ) + if(NOT MACOSX_FIND_SDK_NAME) + message(FATAL_ERROR "can not find appropriate macosx SDK, you may need upgrade xcode") + endif() + set(ENV{SDKROOT} "${MACOSX_SDK_PATH}/${MACOSX_FIND_SDK_NAME}.sdk") + endif() + message("macosx sdkroot: $ENV{SDKROOT}") + # set macosx deployment target based on SDK if(NOT DEFINED ENV{MACOSX_DEPLOYMENT_TARGET}) - set(CMAKE_OSX_DEPLOYMENT_TARGET ${MACOSX_SYSTEM_VERSION} CACHE STRING + execute_process( + COMMAND bash -c "cat $ENV{SDKROOT}/SDKSettings.json | \ + grep -Eo 'MACOSX_DEPLOYMENT_TARGET\\\":\\\"\\d{2}\\.\\d+' | cut -d '\"' -f 3 | tr -d '\\n'" + OUTPUT_VARIABLE MACOSX_FIND_SDK_VERSION + ) + if(NOT MACOSX_FIND_SDK_VERSION) + message(FATAL_ERROR "can not find MACOSX_DEPLOYMENT_TARGET in SDKROOT, \ + please check whether it's a valid SDK path") + endif() + set(CMAKE_OSX_DEPLOYMENT_TARGET ${MACOSX_FIND_SDK_VERSION} CACHE STRING "minimum macosx deployment target version" FORCE) + if(${CMAKE_HOST_SYSTEM_PROCESSOR} MATCHES "arm64") + set(CMAKE_OSX_DEPLOYMENT_TARGET "11.0") + endif() endif() message("macosx deployment target version: ${CMAKE_OSX_DEPLOYMENT_TARGET}") set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O2 -Winconsistent-missing-override -Wno-user-defined-warnings \ diff --git a/mindspore/ccsrc/ps/core/comm_util.cc b/mindspore/ccsrc/ps/core/comm_util.cc index 30afb11f0d2..0513927d1bd 100644 --- a/mindspore/ccsrc/ps/core/comm_util.cc +++ b/mindspore/ccsrc/ps/core/comm_util.cc @@ -233,7 +233,7 @@ bool CommUtil::IsFileEmpty(const std::string &file) { bool CommUtil::CreateDirectory(const std::string &directoryPath) { uint32_t dirPathLen = directoryPath.length(); - uint32_t MAX_PATH_LEN = 512; + constexpr uint32_t MAX_PATH_LEN = 512; if (dirPathLen > MAX_PATH_LEN) { return false; }