Loading data file

Hello,

I want to set CMake variables from a text file. For example, the text file contains a set of paths where special header files are located and these paths change for different projects.
Is something like this possible, e.g. parse a csv file into CMake variables?

Thanks!

Sure, it’s possible. You can use file(STRINGS) and then parse each line. Quoted entries won’t be simple, but if you don’t need to handle that, just replace the , instances with ; and you can treat the line as a CMake list.

You should be able to use file(READ) and string() and/or list() processing for this.

For example, if you have a csv file where the first column has variable name and subsequent columns store values to put in that list, you could do something like this:

file(READ data.csv data)
string(REPLACE "\n" ";" data "${data}")
foreach(line IN LISTS data)
  string(REPLACE "," ";" line "${line}")
  list(POP_FRONT line var)
  set(${var} ${line})
endforeach()

Thanks!
This works nicely. I need to check for an empty line at the end, but for the rest I will use this approach. Thanks for your help.