[CMake] File concatenation in cmake add_custom_command()

Ravi Raman ravi.raman at Xoriant.Com
Mon Aug 25 07:35:57 EDT 2014


Thanks for the reply.

Thanks & Regards

Ravi Raman 
Xoriant Solutions Pvt. Ltd
4th Floor, Winchester, Hiranandani Business Park, Powai, Mumbai 400076, INDIA. 
Tel: +91 22 30511000,9930100026 Extn: 2144 Voip No. 4088344495/96/97/98 Voip Extn:1178| Fax: +91 22 30511111 
ravi.raman at xoriant.comhttp://www.xoriant.com

-----Original Message-----
From: David Cole [mailto:dlrdave at aol.com] 
Sent: Monday, August 25, 2014 4:59 PM
To: Ravi Raman
Cc: cmake at cmake.org
Subject: Re: File concatenation in cmake add_custom_command()

First: thanks for your questions to the CMake list. However, when you 
send an email to the list, please just ask the question -- that is, 
please do *not* ask me by name. I know I've answered a few questions 
for you in the last few weeks, but I am not the only one here, and 
asking me by name may discourage somebody else from answering earlier 
if they are able to. The list is a community resource, and all should 
feel welcome to chime in on any discussion where they can add something 
useful.

I think using "file(WRITE..." followed by "file(APPEND..." is the 
simplest way of performing file concatenation using the CMake language.

However, I would never write a custom command rule that writes files 
into the source tree of a project. For one thing, it precludes having 
two separate build trees for a single source tree that do not clobber 
each other - the build trees should be independent of each other... but 
when you push output to the source tree, multiple builds can never be 
independent of each other.

It might be simpler or more elegant to use a different language 
(python, perl, ?) for this task, but if you must do it in CMake, what 
you have is just fine.

I'm certain you could make the code easier to understand by naming 
variables more descriptively (FILE1, FILE2, FILE3, ... are not very 
descriptive) and by adding some comments. Also, there's no reason to 
file(WRITE the script itself in your example: the script could simply 
be written, and be a part of your source code, and then just called 
 from the custom command. That would remove a leap of indirection that a 
developer reading the code has to make mentally to understand what 
you've got. If the script file is just a script file, and they can look 
at it as such, then the add_custom_command becomes easier to read, 
because it doesn't have the crazy file(WRITE with backslashes in it to 
interpret.

HTH,
David C.



More information about the CMake mailing list