The serial number, as the name suggests, is the device’s serial number, and the number in the Module columns is the number of modules present according to the column type.
Based on this information, I need to parameterize the compilation and execution of the code
Would this be possible with CMake, and what options do I have to do this? It would be a great help if I could build a .h file based on an h.in where there would be precompilation macros for decision-making within the code.
Yes, it’s certainly possible. The commands you want to look into are file(READ), string(), and list() for parsing the CSV, and then either configure_file() or file(WRITE) for creating the header file.
Here’s a quick example:
CMakeLists.txt
set(generated_h "")
file(READ path/to/the.csv csv) # read entire file
string(REPLACE \n ";" lines "${csv}") # transform string into list of lines
list(POP_FRONT lines header_line)
string(REGEX_REPLACE ", *" ";" header_line "${header_line}") # transform string into list of fields
list(POP_FRONT header_line serial)
list(LENGTH header_line num_modules)
math(EXPR max_module "${num_modules} - 1")
foreach(line IN LISTS lines)
list(POP_FRONT line ser_num)
foreach(idx RANGE ${max_module})
list(GET header_line ${idx} mod_name)
list(GET line ${idx} value)
string(APPEND generated "#define ${serial}_${ser_num}_${mod_name} ${value}\n")
endforeach()
endforeach()
configure_file(generated.h.in generated.h)
Seeing that the processing is not trivial, you might want to consider caching the last value of csv (or perhaps the CSV file’s timestamp) and skip the parsing step if there was no change since last CMake run.
If you provide a concrete (simplified) example of the input you have and output you’d want to get from it, I can try coming up with a more tailored solution.
It turned out really well, but the problem is that I’ll have to call the build/compiler for each serial number, that is, for each line a new serial number and a separate compilation process, generating the binary and burning it on the device (fashing/burning).
I managed to write the code in Python, but the ideal would be to avoid using too many tools.
If I understand you correctly, you can modify the body of the foreach(line IN LISTS lines) loop to include an add_library() or add_executable() call, ending up with one binary for each line in the file.