From 8838c21b954088560632ced52b49993166a7d4ba Mon Sep 17 00:00:00 2001 From: jsberg-bnl <24276120+jsberg-bnl@users.noreply.github.com> Date: Tue, 23 Apr 2024 11:27:06 -0400 Subject: [PATCH] Automate MacOS package detection, etc. (#925) * Autodetect HomeBrew and MacPorts on MacOS. Set appropriate paths. HomeBrew paths set assuming ARM Mac. * Use STREQUAL and not EQUAL when comparing strings in CMake When determining whether we are running MacPorts or HomeBrew * If both HomeBrew and MacPorts are present, requie user to set BMAD_MAC_PACKAGE * ACC_USE_MACPORTS is no longer needed, remove it from util/dist_prefs * Automate setting CC/CXX on MacOS * Fixed typo in util/dist_source_me * Typo in gcc search for HomeBrew in util/dist_source_me * Do proper error logging in MacOS gcc detection, and don't exit --- util/Master.cmake | 46 +++++++++++++++++++++++---- util/dist_prefs | 10 ------ util/dist_source_me | 77 ++++++++++++++++++++++++++++++++------------- 3 files changed, 95 insertions(+), 38 deletions(-) diff --git a/util/Master.cmake b/util/Master.cmake index a320b0bce5..7f8438a694 100644 --- a/util/Master.cmake +++ b/util/Master.cmake @@ -230,12 +230,6 @@ ENDIF () IF (${CMAKE_SYSTEM_NAME} MATCHES "Linux" AND NOT $ENV{ACC_PLOT_PACKAGE} MATCHES "none") SET (ACC_PLOT_LIB_DIRS /usr/lib64) -ELSEIF (${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND NOT $ENV{ACC_CONDA_BUILD} MATCHES "Y") # See: RT 60204 - IF ($ENV{ACC_USE_MACPORTS} MATCHES "Y") - SET (ACC_PLOT_LIB_DIRS /opt/local/lib /opt/X11/lib) - ELSE () - SET (ACC_PLOT_LIB_DIRS /opt/X11/lib) - ENDIF () ENDIF () IF (${CMAKE_Fortran_COMPILER} STREQUAL "ifort" AND "$ENV{ACC_ENABLE_SHARED}" MATCHES "Y") @@ -315,6 +309,46 @@ ENDIF () string (STRIP "${ACC_LINK_FLAGS}" ACC_LINK_FLAGS) +IF (${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND NOT $ENV{ACC_CONDA_BUILD} MATCHES "Y") + find_program(BREW "brew") + find_program(PORT "port") + set(DO_BREW "NO") + set(DO_PORT "NO") + if (NOT BREW STREQUAL "BREW-NOTFOUND") + set(DO_BREW "YES") + endif() + if (NOT PORT STREQUAL "PORT-NOTFOUND") + set(DO_PORT "YES") + endif() + if (DO_BREW AND DO_PORT) + set(DO_BREW "NO") + set(DO_PORT "NO") + if (DEFINED ENV{BMAD_MAC_PACKAGE}) + string(TOLOWER "$ENV{BMAD_MAC_PACKAGE}" MAC_PACKAGE) + if (MAC_PACKAGE STREQUAL "macports") + set (DO_PORT "YES") + elseif (MAC_PACKAGE STREQUAL "homebrew") + set (DO_BREW "YES") + endif () + endif() + if (NOT (DO_BREW OR DO_PORT)) + message(FATAL_ERROR + "Both HomeBrew and Macports are installed.\n" + "Set BMAD_MAC_PACKAGE to either \"macports\" or \"homebrew\" to choose which to use") + endif() + endif() + if (DO_BREW) + # Assuming an ARM mac here, path is different for an x86 Mac + set(ACC_INC_DIRS ${ACC_INC_DIRS} /opt/homebrew/include /opt/homebrew/opt/readline/include) + set(ACC_LIB_DIRS ${ACC_LIB_DIRS} /opt/homebrew/lib /opt/homebrew/opt/readline/lib) + elseif (DO_PORT) + set(ACC_INC_DIRS ${ACC_INC_DIRS} /opt/local/include) + set(ACC_LIB_DIRS ${ACC_LIB_DIRS} /opt/local/lib) + else() + message(FATAL_ERROR "This is MacOS, but neither HomeBrew nor MacPorts is installed") + endif() +ENDIF() + # Environment variables BMAD_USER_INC_DIRS and BMAD_USER_LIB_DIRS hold include/library directories # to search for include/module or libraries respectively. Multiple directories separated by semicolons. SET (ACC_INC_DIRS $ENV{BMAD_USER_INC_DIRS} ${ACC_PLOT_INC_DIRS} ${ACC_INC_DIRS} ${MPI_INC_DIRS}) diff --git a/util/dist_prefs b/util/dist_prefs index f3de6e1910..4d2b6d92e9 100644 --- a/util/dist_prefs +++ b/util/dist_prefs @@ -181,13 +181,3 @@ export ACC_CONDA_BUILD="N" # environment. #----------------------------------------------------------- export ACC_CONDA_PATH="${CONDA_PREFIX}" - - -#----------------------------------------------------------- -# By default this flag is on. -# When ACC_USE_MACPORTS is set to "Y", this will enable the -# MacPorts path (/opt/local/) to be added to library search path. -# This option is only valid for MacOS (Darwin). -#----------------------------------------------------------- -export ACC_USE_MACPORTS="Y" - diff --git a/util/dist_source_me b/util/dist_source_me index 23ab2427d2..b3be2c57fc 100644 --- a/util/dist_source_me +++ b/util/dist_source_me @@ -381,26 +381,59 @@ func_add_bmad_path () { -#----------------------------------------------------------------- -# Function to set CC and CXX to the MacPorts gcc/g++, under macOS. -# Addresses issue with Conda support Patch (RT#59013). -# Issue was raised in RT#61171 by cmayes@stanford.edu -#----------------------------------------------------------------- -func_use_MacPorts_gcc () { - - case "${ACC_USE_MACPORTS}" in - "Y" | "y" | "1" ) - USE_MACPORTS=1 - ;; - esac - - if [ "$(uname)" == "Darwin" ] ; then - if ( [[ "${USE_MACPORTS}" ]] ) ; then - if [ -e /opt/local/bin/gcc ] ; then - export CC=gcc - export CXX=g++ - fi - fi +func_macos_gcc () { + + if [ "$(uname)" = "Darwin" ] && [ -z "$CC" ] + then + brew= + port= + command -v brew > /dev/null && brew=y + command -v port > /dev/null && port=y + if [ "$brew" ] && [ "$port" ] + then + brew= + port= + mac_package=$(echo $BMAD_MAC_PACKAGE | tr '[:upper:]' '[:lower:]') + [ "$mac_package" = homebrew ] && brew=y + [ "$mac_package" = macports ] && port=y + if [ -z "$brew" ] && [ -z "$port" ] + then + echo "You have both HomeBrew and MacPorts installed" >> ${DIST_SETUP_LOG} + echo "You must set BMAD_MAC_PACKAGE to either 'homebrew' or 'macports'" >> ${DIST_SETUP_LOG} + fi + unset mac_package + fi + if [ "$port" ] + then + if [ -x /opt/local/bin/gcc ] + then + export CC=gcc + export CXX=g++ + else + echo "You need to install gcc under MacPorts" >> ${DIST_SETUP_LOG} + fi + elif [ "$brew" ] + then + p=/opt/homebrew/bin/gcc- + v=0 + for g in $p[0-9]* + do + vv=${g##$p} + [ "$vv" -gt "$v" ] && v="$vv" + done + unset p + unset g + if [ "$v" != 0 ] + then + export CC=gcc-$v + export CXX=g++-$v + else + echo You need to install gcc on HomeBrew >> ${DIST_SETUP_LOG} + fi + unset v + else + echo You need to install HomeBrew or MacPorts >> ${DIST_SETUP_LOG} + fi fi } @@ -459,7 +492,7 @@ if ( [ "${CWD_DIR_CHECK}" == "Y" ] && [[ ! ${CWD} -ef ${DIST_BASE_DIR} ]]) ; the func_add_bmad_path if ( [[ ! "${CONDA_BUILD}" ]] ) ; then func_check_fortran_version - func_use_MacPorts_gcc + func_macos_gcc fi elif ( [ "${DIST_BASE_DIR_CHECK}" == "Y" ] ) ; then @@ -471,7 +504,7 @@ elif ( [ "${DIST_BASE_DIR_CHECK}" == "Y" ] ) ; then func_add_bmad_path if ( [[ ! "${CONDA_BUILD}" ]] ) ; then func_check_fortran_version - func_use_MacPorts_gcc + func_macos_gcc fi else