[CMake] Several questions/problems on FIND_* search behavior

E. Wing ewmailing at gmail.com
Wed Apr 11 22:43:43 EDT 2007


> FIND_FILE does not search in /usr/lib only FIND_LIBRARY search in
> the library directories.  FIND_FILE only looks in PATH.

You're right. I got confused by the framework stuff.

> That should work, please give a specific example, note that
> CMAKE_LIBRARY_PATH
> is only searched by FIND_LIBRARY and CMAKE_INCLUDE_PATH is only searched
> by FIND_PATH, as per the docs.

By magic, the problem went away once you responded. I must have been
mistyping something.

> > 3) Is it possible to list multiple potential paths for an $ENV{FOO}
> > variable? So if I do:
> > export FOO=../Dir1:../Dir2
> > and my script has:
> > FIND_LIBRARY(FOO foo PATHS $ENV{FOO}),
> > will this work?
> > (So far my tests say no, but I would like to know if this can work.)
> >
> That is not what the docs say for FIND_*:
>
>    FIND_FILE(
>              <VAR>
>              name | NAMES name1 [name2 ...]
>              PATHS path1 [path2 ... ENV var]
>            .....
>             )
> ....
>
> if ENV var is found in the PATHS section the environment variable var
> will be read and converted from a system environment variable to a cmake
> style list of paths. For example ENV PATH would be a way to list the
> system path variable.
> .....
> To use and env var in a FIND_* command you do it like this:
> PATHS ENV FOO
>
> This is because path separators are OS specific ; on windows and : on
> unix, so $ENV{PATH} will only work on windows for cmake.  But the ENV
> PATH syntax of FIND_* will let cmake do the correct conversion of the
> native path into cmake.

Okay, to be clear, this is an example of what I like to do:
FIND_PATH(OPENTHREADS_INCLUDE_DIR OpenThreads/Thread
    $ENV{OPENTHREADS_DIR}/include
)
FIND_LIBRARY(OPENTHREADS_LIBRARY
        NAMES OpenThreads OpenThreadsd
        PATHS
        $ENV{OPENTHREADS_DIR}/lib
)

Then I might do something like:
export OPENTHREADS_DIR=/optional
and things pick up.

But I was thinking about a corner case where the include and lib don't
share a common root. So that's what got me wondering about setting
multiple paths in my environemental variable:
export OPENTHREADS_DIR=/optional_libs:/optional_headers

I didn't expect this to work, but I was hoping I would be corrected.


> > 4) Finally, back to FIND_LIBRARY(FOO foo PATHS $ENV{FOO}),
> > how do I make CMake search my system paths last. I want my $ENV{FOO}
> > to be like an override, but my system installed stuff keeps getting
> > found first, before my $ENV{FOO} is checked.
> >
> Again, back to the docs....
> ...
> If NO_DEFAULT_PATH is specified, then no additional paths are added to
> the search. If NO_DEFAULT_PATH is not specified, the search process is
> as follows:
> ....
>
> So, you can do this:
> FIND_*(.... PATHS mystuff NO_DEFAULT_PATH)  # first look with no extra paths
> FIND_*(....)  # now look in all the default paths.
>

So here's the thing. I do want to search the default system paths, but
I want them searched last. My motivation is for automated/one-touch
build systems used for packaging/testing/etc. Basically, I would like
to write a shell script that presets all the proper environmental
variables I need and then invoke cmake.
So my shell script might look something like:
export OPENTHREADS_DIR=/optional
export SDL_DIR=/branch_stuff
export CMAKE_LIBRARY_PATH=/otherthings/lib
export CMAKE_INCLUDE_PATH=/otherthings/include
cmake ../OpenSceneGraph
make

My current problem is that all my libraries and headers are getting
picked up from system locations first. These happen to be different
versions than the ones in my alternative directories so I don't want
to use those.

The problem with the two step FIND_ using NO_DEFAULT_PATH is that I
need to modify all the official Find*.cmake packages that I use, and
not just the ones I control.


It would be extremely helpful if you could make an environmental
variable or option that flipped the search order so system locations
are searched after anything in my PATHS list for FIND_*.

Thanks,
Eric


More information about the CMake mailing list