<div dir="ltr">Ah, sorry, I misunderstood what CCACHE_DIR was for. Nevertheless, the method in that linked article could be easily modified to do what you want. Simply add the setting of the environment variable to the launch scripts. Since those launch scripts are copied across to the build dir at configure time and they support variable substitutions, you can even have the value of CCACHE_DIR be settable from CMake if you want. For example, your <a href="http://launch-c.in">launch-c.in</a> file could look like this:<div><br></div><div><div style="box-sizing:border-box;border:0px;font-family:lato,sans-serif;font-size:18px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(68,68,68)"><div id="gmail-highlighter_415122" class="gmail-syntaxhighlighter gmail-nogutter gmail-plain" style="box-sizing:border-box;border:0px;font-family:inherit;font-style:inherit;outline:0px;padding:0px;vertical-align:baseline;font-size:1em;margin:1em 0px;width:100%;overflow-x:auto;overflow-y:hidden"><table border="0" cellpadding="0" cellspacing="0" style="border-collapse:collapse;empty-cells:show;box-sizing:content-box;border:0px;font-family:consolas,'bitstream vera sans mono','courier new',courier,monospace;font-size:1em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;width:100%;border-top-left-radius:0px;border-top-right-radius:0px;border-bottom-right-radius:0px;border-bottom-left-radius:0px;background-image:none;float:none;height:auto;line-height:1.1em;overflow:visible;direction:ltr;table-layout:auto"><tbody style="box-sizing:content-box;border:0px;font-size:1em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;border-top-left-radius:0px;border-top-right-radius:0px;border-bottom-right-radius:0px;border-bottom-left-radius:0px;background-image:none;float:none;height:auto;line-height:1.1em;overflow:visible;width:auto;direction:ltr"><tr style="box-sizing:content-box;border:0px;font-size:1em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;border-top-left-radius:0px;border-top-right-radius:0px;border-bottom-right-radius:0px;border-bottom-left-radius:0px;background-image:none;float:none;height:auto;line-height:1.1em;overflow:visible;width:auto;direction:ltr"><td class="gmail-code" style="box-sizing:content-box;border:0px;outline:0px;padding:0px;vertical-align:baseline;line-height:1.1em;overflow:visible;border-top-left-radius:0px;border-top-right-radius:0px;border-bottom-right-radius:0px;border-bottom-left-radius:0px;background-image:none;float:none;height:auto;width:100%;direction:ltr"><font size="2" face="monospace, monospace">#!/bin/sh<br>export CCACHE_CPP2=true<br>export CCACHE_DIR=${CCACHE_DIR}<br>exec "${RULE_LAUNCH_COMPILE}" "${CMAKE_C_COMPILER}" "$@"</font></td></tr></tbody></table></div></div></div><div>When you run CMake, it substitutes the value of each of the above variables as they are set when the configure_file() command is called. Therefore, the configure step bakes in the value of CCACHE_DIR and it won't need to be set in your environment when you do the build step. Note that the substitutions are substituting CMake variables, not environment variables, so if you wanted to pass through a CCACHE_DIR environment variable instead, you would do something like this:</div><div><br></div><div><div><div style="box-sizing:border-box;border:0px;font-family:lato,sans-serif;font-size:18px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(68,68,68)"><div id="gmail-highlighter_415122" class="gmail-syntaxhighlighter gmail-nogutter gmail-plain" style="box-sizing:border-box;border:0px;font-family:inherit;font-style:inherit;outline:0px;padding:0px;vertical-align:baseline;font-size:1em;margin:1em 0px;width:708px;overflow-x:auto;overflow-y:hidden"><table border="0" cellpadding="0" cellspacing="0" style="border-collapse:collapse;empty-cells:show;box-sizing:content-box;border:0px;font-family:consolas,'bitstream vera sans mono','courier new',courier,monospace;font-size:1em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;width:708px;border-top-left-radius:0px;border-top-right-radius:0px;border-bottom-right-radius:0px;border-bottom-left-radius:0px;background-image:none;float:none;height:auto;line-height:1.1em;overflow:visible;direction:ltr;table-layout:auto"><tbody style="box-sizing:content-box;border:0px;font-size:1em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;border-top-left-radius:0px;border-top-right-radius:0px;border-bottom-right-radius:0px;border-bottom-left-radius:0px;background-image:none;float:none;height:auto;line-height:1.1em;overflow:visible;width:auto;direction:ltr"><tr style="box-sizing:content-box;border:0px;font-size:1em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;border-top-left-radius:0px;border-top-right-radius:0px;border-bottom-right-radius:0px;border-bottom-left-radius:0px;background-image:none;float:none;height:auto;line-height:1.1em;overflow:visible;width:auto;direction:ltr"><td class="gmail-code" style="box-sizing:content-box;border:0px;outline:0px;padding:0px;vertical-align:baseline;line-height:1.1em;overflow:visible;border-top-left-radius:0px;border-top-right-radius:0px;border-bottom-right-radius:0px;border-bottom-left-radius:0px;background-image:none;float:none;height:auto;width:708px;direction:ltr"><font size="2" face="monospace, monospace">#!/bin/sh<br>export CCACHE_CPP2=true<br>export CCACHE_DIR=$<b>ENV</b>{CCACHE_DIR}<br>exec "${RULE_LAUNCH_COMPILE}" "${CMAKE_C_COMPILER}" "$@"</font></td></tr></tbody></table></div></div></div></div><div>Probably better to do that in your CMakeLists.txt instead though and leave the <a href="http://launch-c.in">launch-c.in</a> script as in the first case above. This would mean your CMakeLists.txt would have a line like this somewhere:</div><div><br></div><div>set(CCACHE_DIR $ENV{CCACHE_DIR}).</div><div><br></div><div>Note, however, that relying on environment variables for configure or build steps is not so robust. If, for example, you change a CMakeLists.txt file or something else in your project requires CMake to be re-run, then CMake can re-run as part of a build. This then means the environment variables have to be the same between your CMake runs and your build runs. That's normally not a problem for most people, but thought I'd mention it just in case. Personally, I try to avoid relying on environment variables and instead have such values passed in as CMake cache variables like so:</div><div><br></div><div>cmake -G Ninja -DCCACHE_DIR=${CCACHE_DIR} ../src</div><div><br></div><div>This saves the value in the cache and then it is preserved regardless of what environment I have when I do subsequent build steps.</div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 20, 2017 at 7:44 AM, David Lind <span dir="ltr"><<a href="mailto:davidklind@gmail.com" target="_blank">davidklind@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Scott,</div><div><br></div><div>I have find_program implemented to find ccache, as shown below.</div><div><br></div><div><div>    find_program(CCACHE ccache)</div><div>    if(CCACHE)</div><div>        set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE})</div><div>        set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ${CCACHE})</div><div>    endif()</div></div><div><br></div><div>That’s not the issue. The issue is telling ccache where to place it’s cache files. If I compiles for toolchain X, Y and X, I need to set CCACHE_DIR accordingly. Otherwise the cache will be useless.</div><div><br></div><div>—Dave</div><br><div><blockquote type="cite"><div>On Jan 19, 2017, at 12:57 PM, Craig Scott <<a href="mailto:craig.scott@crascit.com" target="_blank">craig.scott@crascit.com</a>> wrote:</div><br class="m_7085982639646993848Apple-interchange-newline"><div><div dir="ltr">Rather than relying on environment variables, you can use CMake's find_program() command to find ccache on your path and then tell CMake to use that as a launcher. You can find an article with a detailed explanation of how to set this up here: <div><br></div><div><a href="https://crascit.com/2016/04/09/using-ccache-with-cmake/" target="_blank">https://crascit.com/2016/04/<wbr>09/using-ccache-with-cmake/</a><br></div><div><br></div><div>An advantage of this approach is that the build will work with or without ccache installed. We've been using this in production for some time now and it works very smoothly. The technique can probably also be extended to support Windows too with <a href="https://github.com/frerich/clcache" target="_blank">clcache</a>, but I haven't tried that yet.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 20, 2017 at 5:00 AM, David Lind <span dir="ltr"><<a href="mailto:davidklind@gmail.com" target="_blank">davidklind@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I am porting existing makefiles to cmake. One of the key features of these makefiles is setting the CCACHE_DIR environment variable based upon the tool chain selected.<br>
<br>
I have TC_<toolchain>.cmake files created. Ideally, I would add a line to these files to set the CCACHE_DIR. But, CMake doesn’t have the ability to set environment variables during the build step. So, I’m stuck.<br>
<br>
Has anyone encountered this kind of situation?<br>
<br>
—Dave<br>
--<br>
<br>
Powered by <a href="http://www.kitware.com/" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" target="_blank">http://www.cmake.org/Wiki/CMak<wbr>e_FAQ</a><br>
<br>
Kitware offers various services to support the CMake community. For more information on each offering, please visit:<br>
<br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/su<wbr>pport.html</a><br>
CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/co<wbr>nsulting.html</a><br>
CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/tr<wbr>aining.html</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensou<wbr>rce/opensource.html</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/cmake" rel="noreferrer" target="_blank">http://public.kitware.com/mail<wbr>man/listinfo/cmake</a></blockquote></div><br><br clear="all"><span class="HOEnZb"><font color="#888888"><div><br></div>-- <br><div class="m_7085982639646993848gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr">Craig Scott<br><div>Melbourne, Australia</div><div><a href="https://crascit.com/" target="_blank">https://crascit.com</a><br></div></div></div></div></div></div></div>
</font></span></div>
</div></blockquote></div><br></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr">Craig Scott<br><div>Melbourne, Australia</div><div><a href="https://crascit.com" target="_blank">https://crascit.com</a><br></div></div></div></div></div></div></div>
</div>