CMake find on MacOS

Anaconda Python puts itself first on PATH when activated. This can become a problem for libraries like HDF5, where “conda install h5py” puts compiler wrapper “h5cc” on PATH first. Tell CMake to prefer Homebrew for a library like HDF5 by doing:

export HDF5_ROOT=$HOMEBREW_PREFIX

Macports doesn’t have a similar environment variable. We instead hint to CMake by inside the CMakeLists.txt:

# --- detect Macports and hint its location
# this helps avoid issues with Anaconda overriding HDF5 with its broken compiler wrapper
if(APPLE)
  if(NOT DEFINED ENV{HOMEBREW_PREFIX} AND NOT DEFINED ENV{MACPORTS_PREFIX})
    find_program(MACPORTS NAMES port)
    if(MACPORTS)
      cmake_path(GET MACPORTS PARENT_PATH MACPORTS_PREFIX)
    endif()
  endif()
endif()