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

Andrew Maclean a.maclean at cas.edu.au
Tue May 31 00:43:12 EDT 2005


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.

Thanks for any help 

Andrew.

___________________________________________
Andrew J. P. Maclean
Centre for Autonomous Systems
The Rose Street Building J04
The University of Sydney  2006  NSW
AUSTRALIA
Ph: +61 2 9351 3283
Fax: +61 2 9351 7474
URL: http://www.cas.edu.au/
___________________________________________





More information about the CMake mailing list