[CMake] string replacement without external commands help (ex. sed)

Alan W. Irwin irwin at beluga.phys.uvic.ca
Tue Aug 21 11:27:11 EDT 2007


On 2007-08-21 18:10+0300 Dizzy wrote:

> Hello
>
> Short version:
> Is there some built-in support in cmake for string replacement in files (ie to
> generate a file from another by replacing a given string with another,
> similar to configure_file() but more general) and could this string
> replacement operation be used as a "command" for add_custom_command() ?
>
> Long version:
> I have recently started to test a cmake based build system for one of my
> projects. I am interested in cmake mostly because currently we have to deal
> with autotools for Unices (which even works for exotic ones such as BeOS) and
> separate project files for Win32. With cmake theoretically should be easier
> to manage a single build system for all these systems.
>
> For the configuration files of the project with autotools we added a new rule
> that said for building project.conf from project.conf.in we used sed to
> replace some strings in project.conf.in with values from the build system
> (dependent on the installation prefix and so on).
>
> Doing this in cmake seems to me to require for one thing to use
> add_custom_command(target project.conf command <sed invocation>). This would
> probably work well enough on Unices but as I said with cmake I expect more,
> ie I expect to have something portable for Win32 too without requiring win32
> package builders to install a port of sed or something:)
>
> I noticed that configure_file() can be used to generate a file from another by
> replacing variable placeholders. It may not be as general as using sed to
> replace any string sequence but we could make it work (initially we could
> modify project.conf.in to have variable placehoders and use configure_file()
> to replace them). But then I have no idea how to use configure_file() from
> within add_custom_command() as I understand the later wants external commands
> to execute. Even if it's possible to chain configure_file() invocation with
> add_custom_command() it would be nicer if there was some built-in cmake
> support

I think the source of your difficulty is you are confusing cmake time with
build time.  Normally, CONFIGURE_FILE() is used to create a configured file
at cmake time (i.e., when you execute the "cmake" command), and it should be
ideal for your purposes. It is documented at
http://www.cmake.org/HTML/Documentation.html.  In contrast,
ADD_CUSTOM_COMMAND() configures a special Makefile rule (assuming you are
using that back end) at cmake time which is then used at build time (i.e.,
when you execute "make").

There are ways to execute ordinary cmake commands like CONFIGURE_FILE at
build time (usually through an ADD_CUSTOM_COMMAND() involving cmake -P
script), but that complication is usually not necessary. If all the
information you wish to configure is available at cmake time, then all you
have to do is prepare the template file with placeholders and run
CONFIGURE_FILE() to produce the configured file at cmake time.

Alan
__________________________
Alan W. Irwin

Astronomical research affiliation with Department of Physics and Astronomy,
University of Victoria (astrowww.phys.uvic.ca).

Programming affiliations with the FreeEOS equation-of-state implementation
for stellar interiors (freeeos.sf.net); PLplot scientific plotting software
package (plplot.org); the libLASi project (unifont.org/lasi); the Loads of
Linux Links project (loll.sf.net); and the Linux Brochure Project
(lbproject.sf.net).
__________________________

Linux-powered Science
__________________________


More information about the CMake mailing list