[CMake] How do I write variables from CMake out to a file?

Andy Cedilnik andy.cedilnik at kitware.com
Tue May 31 08:23:43 EDT 2005


Hi Andrew,

If the file is the same format except the numbers change, you may want
to use CONFIGURE_FILE.

As far as FOREACH goes, you can do something like:

SET(vars "Longitude;Latitude;Height;...")
FILE(WRITE "${...}" "") # Empty file
FOREACH(var ${vars})
  FILE(APPEND "${...}" "${${var}}\t\;${${var}Comment}\n)
ENDFOREACH(...)

Now, if you want to do this every time you compile, you will have to use
custom target. What you can do is to write the generation of this file
in a special file, let say:

generateINI.cmake

Then in your CMakeLists.txt you can put:

ADD_CUSTOM_TARGET(generateINI ALL "${CMAKE_COMMAND}" -P
"${CMAKE_CURRENT_SOURCE_DIR}/generateINI.cmake")

Is this what you need?

       Andy


Andrew Maclean wrote:

>I want to create an ini file called aa.ini that is in the same directory as
>the executable. The user must also be able to change the values in this file
>using CMake.
>The file has to have this type of format:
>-77.0       ;Terrestrial east longitude of observer, degrees
>42.27       ;Geodetic latitude, degrees
>0.0         ;Height above sea level, meters
>12.0        ;Atmospheric temperature, deg C
>1010.0	;Atmospheric pressure, millibars
>1           ;0 - TDT=UT, 1 - input=TDT, 2 - input=UT
>0.0         ;0.0 
>
>I also want the user to have the ability to change the values in this file
>in CMake.
>
>So I did the following:
>#---------------------------------------------------------------------------
>--
># Where is the executable stored?
>SET (EXE_DIR
>  ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}
>)
>
>SET (Longitude "-77.0" CACHE STRING
>  "Terrestrial east longitude of observer, degrees"
>)
>SET (Latitude "42.27" CACHE STRING
>  "Geodetic latitude, degrees"
>)
>SET (Height "0.0" CACHE STRING
>  "Height above sea level, meters"
>)
>SET (Temperature "12.0" CACHE STRING
>  "Atmospheric temperature, deg C"
>)
>SET (Pressure "1010.0" CACHE STRING
>  "Atmospheric pressure, millibars"
>)
>SET (TimeSystem "1" CACHE STRING
>  "0 - TDT=UT, 1 - input=TDT, 2 - input=UT"
>)
>SET (DeltaT "0.0" CACHE STRING
>  "Use this deltaT (sec) if nonzero, else compute it."
>)
># Comments to be added to the file with a leading semicolon.
>SET (LongitudeComment
>  "Terrestrial east longitude of observer, degrees"
>)
>SET (LatitudeComment 
>  "Geodetic latitude, degrees"
>)
>SET (HeightComment
>  "Height above sea level, meters"
>)
>SET (TemperatureComment
>  "Atmospheric temperature, deg C"
>)
>SET (PressureComment
>  "Atmospheric pressure, millibars"
>)
>SET (TimeSystemComment
>  "0 - TDT=UT, 1 - input=TDT, 2 - input=UT"
>)
>SET (DeltaTComment "0.0" CACHE PATH
>  "Use this deltaT (sec) if nonzero, else compute it."
>)
># These next statements create a file called aa.ini 
># in a directory called $(IntDir). [What happens under Unix?]
># This is not quite correct because it won't put it in the
># correct directory. 
>FILE (WRITE ${EXE_DIR}/aa.ini ${Longitude}\t\;${LongitudeComment}\n)
>FILE (APPEND ${EXE_DIR}/aa.ini ${Latitude}\t\;${LatitudeComment}\n)
>FILE (APPEND ${EXE_DIR}/aa.ini ${Height}\t\;${HeightComment}\n)
>FILE (APPEND ${EXE_DIR}/aa.ini ${Temperature}\t\;${TemperatureComment}\n)
>FILE (APPEND ${EXE_DIR}/aa.ini ${Pressure}\t\;${PressureComment}\n)
>FILE (APPEND ${EXE_DIR}/aa.ini ${TimeSystem}\t\;${TimeSystemComment}\n)
>FILE (APPEND ${EXE_DIR}/aa.ini ${DeltaT}\t\;${DeltaTComment}\n)
>#---------------------------------------------------------------------------
>--
>
>How do I get aa.ini to be written to the correct directory, namely
>${EXE_DIR} and also get this done whenever a compile happens?
>
>I tried using ADD_CUSTOM_TARGET but I can't seem to get it to work at all.
>
>By the way, can the above be done as a FOREACH loop?
>
>It has to be platform independent and the aa.ini file has to match what is
>outlined above.
>  
>


-- 
Andy Cedilnik
Kitware Inc.



More information about the CMake mailing list