[CMake] What is the most reliable way to concatanate files on Windows?

Alan W. Irwin irwin at beluga.phys.uvic.ca
Sun Nov 1 13:09:18 EST 2009


On 2009-11-01 12:10+0100 Hendrik Sattler wrote:

> Am Sonntag 01 November 2009 05:25:03 schrieb Alan W. Irwin:
>> The PLplot build system needs to contatanate some files together.  The
>> cross-platform way we do this right now is to use cat if it is available
>> (which handles the Unix case) and otherwise if the Windows cmd or command
>> are available use the copy command.  This method is somewhat fragile (one
>> Windows user could not find any of cat, cmd, or command), and even when
>>  copy is used (for the cmd or command case), we are currently experimenting
>>  to see if Windows native paths are needed or not.
>>
>> Just in case the PLplot developers have missed something, is there some
>>  more reliable way for us to concatanate files in a cross-platform way?
>>
>> Of course, the ideal way to avoid this mess would be to use the "cmake -E
>> concatanate" command, but as far as I know that hasn't been implemented
>>  yet. File concatanation is a pretty fundamental capability so I am
>>  entering a plea here to the CMake developers to put this idea on their
>>  ToDo list (assuming it isn't there already).  If the implementation is
>>  non-trivial and therefore has to be put off, let me know, and I will make
>>  a wish-list bug report for it.
>
> See the file() command. You can easily write a cmake script to use at
> configuration or at build time.

Thanks, Hendrik, for that idea which turns out to be a good workaround for
the currently missing "cmake -E concatanate" functionality for my particular
needs.

For those following this thread, here is the CMake code fragment that
implements this (I didn't bother with a function or macro because I
only need this once for the PLplot build system.).

file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/pkgIndex.tcl "")
foreach(filename ${index})
   file(READ ${filename} filecontents)
   file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/pkgIndex.tcl "${filecontents}")
endforeach(filename ${index})

In my case, all the files in ${index} were configured so I didn't need a
CMake script to do this at run-time.  However, the contents of those files
included semicolons which caused trouble in my first version of this where I
didn't quote filecontents, but the above second version works fine and
produces identical results to "cat" on Linux (at least for the three small
files where I am using this).  Also, I haven't experimented to see whether
the first null filewrite is necessary or not.  In an ideal world file(APPEND
...) would be identical to file(WRITE ...) if the file didn't exist, but I
didn't check that and just went with the above code which works.

One other general caveat about this method that should be mentioned is that
the above file(READ ... reads the contents of the file into memory.  That
could be an issue for large files.

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