[cmake-developers] Making Config.cmake files safer

Alexander Neundorf neundorf at kde.org
Tue Nov 15 15:54:47 EST 2011


On Tuesday 15 November 2011, Brad King wrote:
> On 11/15/2011 1:24 PM, David Cole wrote:
> > On Tue, Nov 15, 2011 at 1:19 PM, Alexander Neundorf<neundorf at kde.org>  
wrote:
> >> function(check_for_file _file _target)
> >> 
> >>   if(NOT EXISTS _file)
> >>   
> >>     message(FATAL_ERROR "... long error message...")
> >> 
> >> endfunction()
> >> 
> >> check_for_file(libFoo123.so  Foo)
> >> ...
> >> check_for_file(libBar123.so  Bar)
> >> 
>  > I think the function approach is reasonable...
> 
> I don't like the overhead of a function.
> 
> >> Or I could collect the files and targets together, but then the error
> >> message becomes less straigh forward.
> 
> You don't need the error message to do all the files at once.  Just use
> a foreach() to test them one at a time.  Die on the first missing one.

In the meantime after Daves Ok I implemented the version using a function.
It works and is IMO ok. I pushed the branch, didn't merge it yet.

If I generate such a loop, I have to iterate over two lists in sync (one 
containing the files, one containing the imported targets), since it should be 
only once per file, and not once after each target.
This will also be not too pretty cmake code, something like

list(APPEND targets foo foo)
list(APPEND filenames libfoo.dll libfoo.lib)
...
list(APPEND targets bar)
list(APPEND filenames libbar.dll)
...

list(LENGTH length filenames)
set(index 0)
foreach(length)
   list(GET index file filenames)
   list(GET index target targets)
   if (NOT EXISTS ....)
      message(FATAL_ERROR "....")
   endif()
   math(EXPR index "index + 1")
endforeach()


I'm not sure you'll like this version.
Maybe you'll prefer a more map-like implementation ?


list(APPEND _check_targets foo)
set(_filesToCheck_foo libfoo.dll libfoo.lib)
...
list(APPEND _check_targets bar)
set(_filesToCheck_bar libbar.dll libbar.lib)
...

foreach(target ${_check_targets} )
  foreach(file ${_filesToCheck_${target}})
    if (NOT EXISTS ${file})
      message(FATAL_ERROR "...") 
    endif()
  endforeach()
endforach()


So, if you are really not ok with the function as it is in the branch now 
please let me know how you want to code to look like, so I need to change the 
code only one more time.

Alex



More information about the cmake-developers mailing list