<div class="gmail_quote">On Fri, Oct 15, 2010 at 12:17 PM, Michael Hertling <span dir="ltr"><<a href="mailto:mhertling@online.de">mhertling@online.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">On 10/15/2010 04:14 PM, <a href="mailto:william.crocker@analog.com">william.crocker@analog.com</a> wrote:<br>
> Hello:<br>
><br>
> If my app requires relinking, then at link time<br>
> I would like to capture the current date and time and place it<br>
> in a last-minute source file which is linked with the application.<br>
> Following is my attempt at this with CMake.<br>
><br>
> The problems I can see are:<br>
> 1 - It is not portable as it uses the OS date command.<br>
> 2 - It is not portable as it calls gcc directly.<br>
> 3 - It will not work with Debug or Release builds unless<br>
> I set additional target LINK_FLAGS_<type> properties.<br>
> 4 - I wish the creation of the link_date.cc file could be<br>
> done inline of the add_custom_command instead of requiring<br>
> a separate file. (I got into quoting hell.)<br>
> 5 - Sneaking link_date.o onto the link line with the LINK_FLAGS<br>
> property is kind of a hack.<br>
><br>
> Suggestions please.<br>
><br>
> Thanks in advance.<br>
><br>
> Bill<br>
><br>
> -------------------- CMake code --------------<br>
><br>
> # Note that this is a simplified version of the real thing<br>
> # and may contain syntax errors.<br>
><br>
> add_executable( myapp myapp.cc )<br>
> target_link_libraries( myapp lib1 lib2 )<br>
> set_target_properties( myapp PROPERTIES LINK_FLAGS link_date.o )<br>
><br>
> add_custom_command(<br>
> TARGET myapp PRE_LINK<br>
> COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/create_link_date<br>
> COMMAND gcc -m32 -c -I${UTILDIR} link_date.cc<br>
> COMMENT "Make the link_date file"<br>
> VERBATIM<br>
> )<br>
><br>
> ------------- Contents of create_link_date ----------<br>
><br>
> #!/bin/csh<br>
><br>
> echo 'const char *link_date() { return("'`date`'"); }' > link_date.cc<br>
<br>
</div></div>You might use CMake's --build option when hooking into the linking stage:<br>
<br>
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)<br>
PROJECT(LINKDATE C)<br>
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "<br>
#include <stdio.h><br>
int main(void)<br>
{<br>
printf(\"%s\\n\",linkdate());<br>
return 0;<br>
}")<br>
FILE(WRITE ${CMAKE_BINARY_DIR}/<a href="http://linkdate.c.in" target="_blank">linkdate.c.in</a> "<br>
const char *linkdate() { return(\"@LINKDATE@\"); }<br>
")<br>
FILE(WRITE ${CMAKE_BINARY_DIR}/linkdate.cmake "<br>
EXECUTE_PROCESS(<br>
COMMAND date<br>
OUTPUT_VARIABLE LINKDATE<br>
OUTPUT_STRIP_TRAILING_WHITESPACE<br>
)<br>
CONFIGURE_FILE(\${SRC} \${DST} @ONLY)<br>
")<br>
ADD_CUSTOM_COMMAND(<br>
OUTPUT ${CMAKE_BINARY_DIR}/linkdate.c<br>
COMMAND ${CMAKE_COMMAND} -DSRC=<a href="http://linkdate.c.in" target="_blank">linkdate.c.in</a> -DDST=linkdate.c<br>
-P linkdate.cmake<br>
DEPENDS ${CMAKE_BINARY_DIR}/<a href="http://linkdate.c.in" target="_blank">linkdate.c.in</a>)<br>
ADD_LIBRARY(linkdate STATIC EXCLUDE_FROM_ALL linkdate.c)<br>
ADD_EXECUTABLE(main main.c)<br>
TARGET_LINK_LIBRARIES(main linkdate)<br>
ADD_CUSTOM_COMMAND(<br>
TARGET main<br>
PRE_LINK<br>
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_BINARY_DIR}/<a href="http://linkdate.c.in" target="_blank">linkdate.c.in</a><br>
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}<br>
--target linkdate)<br>
<br>
This rebuilds the target linkdate - a static library containing the<br>
desired timestamp - just before the main target is linked against it.<br>
Thus, the problems you mentioned should be solved except for #1 which<br>
can not be done in a platform-independent manner at the moment, AFAIK.<br>
<br>
Regards,<br>
<font color="#888888"><br>
Michael<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" target="_blank">http://www.cmake.org/Wiki/CMake_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://www.cmake.org/mailman/listinfo/cmake" target="_blank">http://www.cmake.org/mailman/listinfo/cmake</a><br>
</div></div></blockquote></div><div><br></div>But if you're going to write a C program anyway....<br><br><div>...then just use "time" and/or "gmtime" standard library functions to print the date/time out in whatever format you want.</div>
<div><br></div>