11

I am trying to use the new logic in CMake 3.3 that checks if a value is in a list

cmake_minimum_required(VERSION 3.3)
cmake_policy(SET CMP0057 NEW)

set(l A B C)
foreach( e ${l} ) 
  if( ${e} IN_LIST "${l}" )
    message( "element ${e} found in ${l}" )
  else()
    message( "element ${e} NOT found in ${l}" )
  endif()
endforeach()

Running this with CMake 3.4.3 on OSX Yosemite gives an unexpected result:

$ cmake -P cmakeBug.cmake
element A NOT found in A;B;C
element B NOT found in A;B;C
element C NOT found in A;B;C

Am I missing something trivial or is there a bug in CMake?

plasmacel
  • 8,183
  • 7
  • 53
  • 101
user1357687
  • 571
  • 1
  • 6
  • 9

1 Answers1

17

Try IF ( ${e} IN_LIST l ). According to the docs the second argument is a list variable, not a list.

user3159253
  • 16,836
  • 3
  • 30
  • 56
  • Something else to add... Ensure `cmake_minimum_required(VERSION)` is 3.3 or greater. I hit some problems because the old CMake script was set to 3.0 and used that version even though I had 3.16 installed. – John Rocha Feb 22 '21 at 20:18