Avoiding Cygwin when searching for FLEX

In a previous question I asked for help with finding Flex. Following the advice given, I was able to find it and work with it. The version of Flex found was in my C:\Cygwin\bin directory.

For various reasons not to do with CMake, that version of Flex turns out not to be suitable, so I have installed win_flex instead. However, I am having difficulty getting CMake to find it - it seems like whatever I do, it still finds the Cygwin version instead.

I have win_flex.exe installed, and its directory is the first item in my system path.

I read in the manual that setting CMAKE_IGNORE_PATH doesn’t work with find_package (unless it’s in config mode, but FindFLEX.cmake is in /modules), so I’m using find_program.

Currently, I have the following code:

In my CMake command line:

-DCYGWIN_INSTALL_PATH=

In my actual script:

set(required_flex_version 2.5.35)  # N.B. win_flex is version 2.6.4
if (MSVC)
   set(CMAKE_IGNORE_PATH "C:/cygwin/bin") # I've also tried "C:\\cygwin\\bin")
   message("Looking specifically for win_flex")
   message("CMAKE_FIND_ROOT_PATH is ${CMAKE_FIND_ROOT_PATH}")
   set(FLEX_EXECUTABLE )
   find_program(FLEX_EXECUTABLE NAMES win_flex DOC "path to the flex executable")
   message("win_flex found at ${FLEX_EXECUTABLE}")
else()
   find_package(FLEX ${required_flex_version})
endif()

My output:

Looking specifically for win_flex
CMAKE_FIND_ROOT_PATH is
win_flex found at C:/cygwin/bin/flex.exe

How does one make sense of this?

You just unset the non-cache variable. Try using

unset(FLEX_EXECUTABLE CACHE)

OTOH, this will re-find it on every CMake run. Instead, you should start with a clean cache and not delete it in the script.

Thank you; that does indeed solve it, and I should have guessed; however, I was put off the trail by the fact that:

(a) In my previous question, the solution had been to leave the cache out of it and work on the basis of FLEX_INCLUDE_DIRS (at least) being a non-cache variable, and

(b) There’s no mention of the cache in FindFLEX.cmake.

Anyway, this one is now solved, so thank you again for your help.

Pretty much all find_* calls use the cache for their output to avoid rerunning on every configure. They have NO_CACHE arguments though.