CMAKE_IGNORE_PREFIX_PATH

I am trying to use CMAKE_IGNORE_PREFIX_PATH to avoid having to set a long list of CMAKE_IGNORE_PATH subdirectories. However, it appears not to work for the use case described in the MR discussion.

My case is Anaconda Python’s HDF5 library interfering with system HDF5, since Anaconda puts itself first in PATH, and I can’t disable Anaconda since it’s also used by the project. I currently fix this by having a long list of manually set CMAKE_IGNORE_PATH. It appears that this script below should work, but CMAKE_IGNORE_PREFIX_PATH seems to have no effect. This is tested with CMake 3.23.1 on Linux and Windows below.

NOTE: if I “conda deactivate”, removing Anaconda from PATH, then the find* works. Also using CMAKE_IGNORE_PATH with numerous Anaconda subdirectories works.

set(CMAKE_IGNORE_PREFIX_PATH $ENV{CONDA_PREFIX})
message(STATUS "$ENV{CONDA_PREFIX}")

find_program(exe NAMES h5cc)
find_library(lib NAMES hdf5)

message(STATUS "${exe}  ${lib}")

On Linux, I would expect “exe” and “lib” to find the system HDF5, but instead:

-- /opt/miniconda3
-- /opt/miniconda3/bin/h5cc /usr/lib/libhdf5.a

On Windows I would expect “exe” and “lib” to find my installed HDF5, but instead:

-- C:\miniconda3
-- c:\<path to installed h5cc.exe> C:/miniconda3/Library/lib/hdf5.lib

Cc: @kyle.edwards

The problem is that CMAKE_IGNORE_PREFIX_PATH doesn’t have a way of deducing the prefix from $ENV{PATH}. Sure, if the component ends in /bin then there’s a chance that the preceding component might be the prefix, but do we want to bet on that?

How is it finding the libraries? Is there an equivalent of PATH that it’s using for that?

I don’t set anything special. If I set CMAKE_PREFIX_PATH I see via --debug-find that that is searched first, then environment variable PATH.

The idea is that I have a known “bad” directory tree like CONDA_PREFIX I always want to avoid on user systems.

This does works, but seems less elegant than what I thought CMAKE_IGNORE_PREFIX_PATH would do.

set(CMAKE_IGNORE_PATH
    $ENV{CONDA_PREFIX}/bin $ENV{CONDA_PREFIX}/lib $ENV{CONDA_PREFIX}/include
    $ENV{CONDA_PREFIX}/Library/bin $ENV{CONDA_PREFIX}/Library/lib $ENV{CONDA_PREFIX}/Library/include
  )

The first row is Linux/Mac, second row is Windows. There’s more paths than this so I have to keep adding to this list as packages change. But I want to ignore all subdirectories of CONDA_PREFIX

Please post the entire output from --debug-find. That will help me track down the problem.

The end result was, CMAKE_IGNORE_PREFIX_PATH must be set outside any custom Find*.cmake the project has. So I set that variable before my custom find_package(HDF5) is called and it works to ignore the entire tree under the prefix.