<div class="gmail_quote">On Fri, Oct 15, 2010 at 12:17 PM, Michael Hertling <span dir="ltr">&lt;<a href="mailto:mhertling@online.de">mhertling@online.de</a>&gt;</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>
&gt; Hello:<br>
&gt;<br>
&gt; If my app requires relinking, then at link time<br>
&gt; I would like to capture the current date and time and place it<br>
&gt; in a last-minute source file which is linked with the application.<br>
&gt; Following is my attempt at this with CMake.<br>
&gt;<br>
&gt; The problems I can see are:<br>
&gt; 1 - It is not portable as it uses the OS date command.<br>
&gt; 2 - It is not portable as it calls gcc directly.<br>
&gt; 3 - It will not work with Debug or Release builds unless<br>
&gt;      I set additional target LINK_FLAGS_&lt;type&gt; properties.<br>
&gt; 4 - I wish the creation of the link_date.cc file could be<br>
&gt;      done inline of the add_custom_command instead of requiring<br>
&gt;      a separate file. (I got into quoting hell.)<br>
&gt; 5 - Sneaking link_date.o onto the link line with the LINK_FLAGS<br>
&gt;      property is kind of a hack.<br>
&gt;<br>
&gt; Suggestions please.<br>
&gt;<br>
&gt; Thanks in advance.<br>
&gt;<br>
&gt; Bill<br>
&gt;<br>
&gt; -------------------- CMake code --------------<br>
&gt;<br>
&gt; # Note that this is a simplified version of the real thing<br>
&gt; # and may contain syntax errors.<br>
&gt;<br>
&gt; add_executable( myapp myapp.cc  )<br>
&gt; target_link_libraries( myapp lib1 lib2 )<br>
&gt; set_target_properties( myapp PROPERTIES LINK_FLAGS link_date.o )<br>
&gt;<br>
&gt; add_custom_command(<br>
&gt;      TARGET myapp PRE_LINK<br>
&gt;      COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/create_link_date<br>
&gt;      COMMAND gcc -m32 -c -I${UTILDIR} link_date.cc<br>
&gt;      COMMENT &quot;Make the link_date file&quot;<br>
&gt;      VERBATIM<br>
&gt; )<br>
&gt;<br>
&gt; ------------- Contents of create_link_date ----------<br>
&gt;<br>
&gt; #!/bin/csh<br>
&gt;<br>
&gt; echo &#39;const char *link_date() { return(&quot;&#39;`date`&#39;&quot;); }&#39; &gt; link_date.cc<br>
<br>
</div></div>You might use CMake&#39;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 &quot;<br>
#include &lt;stdio.h&gt;<br>
int main(void)<br>
{<br>
    printf(\&quot;%s\\n\&quot;,linkdate());<br>
    return 0;<br>
}&quot;)<br>
FILE(WRITE ${CMAKE_BINARY_DIR}/<a href="http://linkdate.c.in" target="_blank">linkdate.c.in</a> &quot;<br>
const char *linkdate() { return(\&quot;@LINKDATE@\&quot;); }<br>
&quot;)<br>
FILE(WRITE ${CMAKE_BINARY_DIR}/linkdate.cmake &quot;<br>
EXECUTE_PROCESS(<br>
    COMMAND date<br>
    OUTPUT_VARIABLE LINKDATE<br>
    OUTPUT_STRIP_TRAILING_WHITESPACE<br>
)<br>
CONFIGURE_FILE(\${SRC} \${DST} @ONLY)<br>
&quot;)<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&#39;re going to write a C program anyway....<br><br><div>...then just use &quot;time&quot; and/or &quot;gmtime&quot; standard library functions to print the date/time out in whatever format you want.</div>
<div><br></div>