Skip to content

Commit

Permalink
Automate MacOS package detection, etc. (#925)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jsberg-bnl authored Apr 23, 2024
1 parent 585add7 commit 8838c21
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 38 deletions.
46 changes: 40 additions & 6 deletions util/Master.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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})
Expand Down
10 changes: 0 additions & 10 deletions util/dist_prefs
Original file line number Diff line number Diff line change
Expand Up @@ -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"

77 changes: 55 additions & 22 deletions util/dist_source_me
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 8838c21

Please sign in to comment.