<h3>Contribution description</h3>
<p>This is an alternative to <a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="462821084" data-permission-text="Issue title is private" data-url="https://github.com/RIOT-OS/RIOT/issues/11771" data-hovercard-type="pull_request" data-hovercard-url="/RIOT-OS/RIOT/pull/11771/hovercard" href="https://github.com/RIOT-OS/RIOT/pull/11771">#11771</a> and fixes the root of the problem for all normal uses outside of <code>RIOT_CI_BUILD=1</code>. <a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="462821084" data-permission-text="Issue title is private" data-url="https://github.com/RIOT-OS/RIOT/issues/11771" data-hovercard-type="pull_request" data-hovercard-url="/RIOT-OS/RIOT/pull/11771/hovercard?comment_id=507357262&comment_type=issue_comment" href="https://github.com/RIOT-OS/RIOT/pull/11771#issuecomment-507357262">#11771 (comment)</a></p>
<p>It only call 'git describe' and 'git rev-parse' through <code>GIT_VERSION</code> when it is used.</p>
<p>There are three steps</p>
<ol>
<li>Define <code>GIT_VERSION</code> that only calls 'git' when used. It will also only call each commands only once thanks to <code>memoized</code></li>
<li>Use it for <code>RIOT_VERSION</code>.<br>
At that point, when <code>RIOT_VERSION_OVERRIDE</code> is set, <code>git</code> will not be called for this.</li>
<li>Have a deferred not exported <code>CFLAGS_WITH_MACROS</code>.<br>
This prevents evaluating the value when unused, so not evaluated when doing <code>make clean</code> for example as it would have been before.</li>
</ol>
<h3>Testing procedure</h3>
<p>I will compare different uses and the evolution starting from step 3, step 2 and what was in master.</p>
<blockquote>
<p>I used RIOTPROJECT=${PWD} to hide the first git call to get the current repository.<br>
It removes this call from the <code>strace</code> output as it is not helping the tests</p>
</blockquote>
<h4>State at pull request</h4>
<p>When <code>RIOT_VERSION</code> is not used, it is not called:</p>
<pre><code>RIOTPROJECT=${PWD} strace -e trace=execve -ff make --no-print-directory -C examples/hello-world/ clean 2>&1 | grep '"git"'
</code></pre>
<p>Each command is still called only once (two git calls) when the value is used</p>
<pre><code>RIOTPROJECT=${PWD} strace -e trace=execve -ff make --no-print-directory -C examples/hello-world/ all 2>&1 | grep '"git"'
[pid  9986] execve("/usr/bin/git", ["git", "--git-dir=/home/harter/work/git/"..., "describe", "--always", "--abbrev=4"], 0x564734141288 /* 64 vars */) = 0
[pid  9988] execve("/usr/bin/git", ["git", "--git-dir=/home/harter/work/git/"..., "rev-parse", "--abbrev-ref", "HEAD"], 0x55ee4a169248 /* 64 vars */) = 0
</code></pre>
<p>It is not called when <code>RIOT_VERSION_OVERRIDE</code> is set, here through <code>RIOT_CI_BUILD=1</code></p>
<pre><code>RIOT_CI_BUILD=1 RIOTPROJECT=${PWD} strace -e trace=execve -ff make --no-print-directory -C examples/hello-world/ all 2>&1 | grep '"git"'
</code></pre>
<h4>Step 2, when <code>CFLAGS_WITH_MACROS</code> was still evaluated</h4>
<details><summary>Without the third commit, when the value was not used, like for `clean` for example, `git` would still be called.</summary>
<pre><code>RIOTPROJECT=${PWD} strace -e trace=execve -ff make --no-print-directory -C examples/hello-world/ clean 2>&1 | grep '"git"'
[pid 11517] execve("/usr/bin/git", ["git", "--git-dir=/home/harter/work/git/"..., "describe", "--always", "--abbrev=4"], 0x559c5ed68288 /* 64 vars */) = 0
[pid 11519] execve("/usr/bin/git", ["git", "--git-dir=/home/harter/work/git/"..., "rev-parse", "--abbrev-ref", "HEAD"], 0x556039163248 /* 64 vars */) = 0
</code></pre>
</details>
<h4>In master</h4>
<details><summary>The value was always called</summary>
<pre><code>RIOTPROJECT=${PWD} strace -e trace=execve -ff make --no-print-directory -C examples/hello-world/ clean 2>&1 | grep '"git"'
[pid 11682] execve("/usr/bin/git", ["git", "--git-dir=/home/harter/work/git/"..., "describe", "--always", "--abbrev=4"], 0x5618db343288 /* 64 vars */) = 0
[pid 11684] execve("/usr/bin/git", ["git", "--git-dir=/home/harter/work/git/"..., "rev-parse", "--abbrev-ref", "HEAD"], 0x55d4af04c248 /* 64 vars */) = 0
</code></pre>
<p>even when <code>RIOT_VERSION_OVERRIDE</code> was set</p>
<pre><code>RIOT_CI_BUILD=1 RIOTPROJECT=${PWD} strace -e trace=execve -ff make --no-print-directory -C examples/hello-world/ all 2>&1 | grep '"git"'
[pid 11784] execve("/usr/bin/git", ["git", "--git-dir=/home/harter/work/git/"..., "describe", "--always", "--abbrev=4"], 0x557bd39f72b8 /* 65 vars */) = 0
[pid 11786] execve("/usr/bin/git", ["git", "--git-dir=/home/harter/work/git/"..., "rev-parse", "--abbrev-ref", "HEAD"], 0x564bd2c78278 /* 65 vars */) = 0
</code></pre>
</details>
<h4>Speed update</h4>
<p>On my machine I do not manage to have reliable timing anymore… Like they are completely inconsistent. I use the one from my build machine that are consistent:</p>
<details><summary>Speed update from 14.5 to 7.5 seconds</summary>
<p>Here <code>GIT_VERSION</code> is not evaluated as it is using <code>RIOT_VERSION_OVERRIDE</code> instead:</p>
<pre><code>time for i in {0..100}; do RIOT_CI_BUILD=1 make --no-print-directory -C examples/hello-world/ info-debug-variable-CFLAGS_WITH_MACROS >/dev/null 2>/dev/null; done

real    0m7.611s
user    0m1.840s
sys     0m0.236s

</code></pre>
<p>The normal build with RIOT_VERSION being used:</p>
<pre><code>time for i in {0..100}; do  make --no-print-directory -C examples/hello-world/ info-debug-variable-CFLAGS_WITH_MACROS >/dev/null 2>/dev/null; done

real    0m14.817s
user    0m8.312s
sys     0m0.640s
</code></pre>
<p>Commands not using <code>RIOT_VERSION</code> have speedup without overwriting:</p>
<pre><code>time for i in {0..100}; do make --no-print-directory -C examples/hello-world/ clean  >/dev/null 2>/dev/null; done

real    0m7.702s
user    0m1.804s
sys     0m0.256s
</code></pre>
<p>Where in master it gives</p>
<pre><code>time for i in {0..100}; do make --no-print-directory -C examples/hello-world/ clean  >/dev/null 2>/dev/null; done

real    0m14.826s
user    0m8.424s
sys     0m0.504s
</code></pre>
</details>
<h4>The <code>RIOT_VERSION</code> has still the same behavior as before.</h4>
<details><summary>The `RIOT_VERSION` given to the program as the same value as before</summary>
<p>Version includes branch name:</p>
<pre><code>make --no-print-directory -C examples/hello-world/ info-debug-variable-RIOT_VERSION
2019.10-devel-100-g8859e-pr/git_version
</code></pre>
<p>In master it removes the branch name</p>
<pre><code>$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'riot/master'.

$ git merge pr/git_version
Merge made by the 'recursive' strategy.
 Makefile.include             | 24 +++++++-----------------
 makefiles/git_version.inc.mk | 11 +++++++++++
 2 files changed, 18 insertions(+), 17 deletions(-)
 create mode 100644 makefiles/git_version.inc.mk

$ make --no-print-directory -C examples/hello-world/ info-debug-variable-RIOT_VERSION
2019.10-devel-108-g1ddf2
</code></pre>
<p>Outside of a git repository, the version is said to be unknown</p>
<pre><code>$ mv .git .git_saved
$ make --no-print-directory -C examples/hello-world/ info-debug-variable-RIOT_VERSION
UNKNOWN (builddir: /home/harter/work/git/RIOT)
$ mv .git_saved/ .git
</code></pre>
<p>The value is given to the compiler in <code>CFLAGS_WITH_MACROS</code></p>
<pre><code>make --no-print-directory -C examples/hello-world/ info-debug-variable-CFLAGS_WITH_MACROS
ake --no-print-directory -C examples/hello-world/ info-debug-variable-CFLAGS_WITH_MACROS
-DDEVELHELP -Werror -Wall -Wextra -pedantic -std=gnu99 -m32 -fstack-protector-all -ffunction-sections -fdata-sections -DDEBUG_ASSERT_VERBOSE -DRIOT_APPLICATION="hello-world" -DBOARD_NATIVE="native" -DRIOT_BOARD=BOARD_NATIVE -DCPU_NATIVE="native" -DRIOT_CPU=CPU_NATIVE -DMCU_NATIVE="native" -DRIOT_MCU=MCU_NATIVE -fno-delete-null-pointer-checks -fdiagnostics-color -Wstrict-prototypes -Wold-style-definition -fno-common -Wall -Wextra -Wformat=2 -Wformat-overflow -Wformat-truncation -Wmissing-include-dirs -DMODULE_AUTO_INIT -DMODULE_BOARD -DMODULE_CORE -DMODULE_CORE_MSG -DMODULE_CPU -DMODULE_NATIVE_DRIVERS -DMODULE_PERIPH -DMODULE_PERIPH_COMMON -DMODULE_PERIPH_GPIO -DMODULE_PERIPH_PM -DMODULE_PERIPH_UART -DMODULE_SYS -DRIOT_VERSION="2019.10-devel-100-g8859e-pr/git_version"
</code></pre>
<p>And overwritten when using <code>RIOT_CI_BUILD=1</code></p>
<pre><code>RIOT_CI_BUILD=1 make --no-print-directory -C examples/hello-world/ info-debug-variable-CFLAGS_WITH_MACROS
-DDEVELHELP -Werror -Wall -Wextra -pedantic -std=gnu99 -m32 -fstack-protector-all -ffunction-sections -fdata-sections -DDEBUG_ASSERT_VERBOSE -DRIOT_APPLICATION="hello-world" -DBOARD_NATIVE="native" -DRIOT_BOARD=BOARD_NATIVE -DCPU_NATIVE="native" -DRIOT_CPU=CPU_NATIVE -DMCU_NATIVE="native" -DRIOT_MCU=MCU_NATIVE -fno-delete-null-pointer-checks -fdiagnostics-color -Wstrict-prototypes -Wold-style-definition -fno-common -Wall -Wextra -Wformat=2 -Wformat-overflow -Wformat-truncation -Wmissing-include-dirs -DMODULE_AUTO_INIT -DMODULE_BOARD -DMODULE_CORE -DMODULE_CORE_MSG -DMODULE_CPU -DMODULE_NATIVE_DRIVERS -DMODULE_PERIPH -DMODULE_PERIPH_COMMON -DMODULE_PERIPH_GPIO -DMODULE_PERIPH_PM -DMODULE_PERIPH_UART -DMODULE_SYS -DRIOT_VERSION="buildtest"
</code></pre>
</details>
<h3>Issues/PRs references</h3>
<p>A real fix compared to only the workaround done in <a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="462821084" data-permission-text="Issue title is private" data-url="https://github.com/RIOT-OS/RIOT/issues/11771" data-hovercard-type="pull_request" data-hovercard-url="/RIOT-OS/RIOT/pull/11771/hovercard" href="https://github.com/RIOT-OS/RIOT/pull/11771">#11771</a><br>
Tracking: remove harmful use of <code>export</code> in make and immediate evaluation <a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="402335760" data-permission-text="Issue title is private" data-url="https://github.com/RIOT-OS/RIOT/issues/10850" data-hovercard-type="issue" data-hovercard-url="/RIOT-OS/RIOT/issues/10850/hovercard" href="https://github.com/RIOT-OS/RIOT/issues/10850">#10850</a></p>

<hr>

<h4>You can view, comment on, or merge this pull request online at:</h4>
<p>  <a href='https://github.com/RIOT-OS/RIOT/pull/11881'>https://github.com/RIOT-OS/RIOT/pull/11881</a></p>

<h4>Commit Summary</h4>
<ul>
  <li>makefiles/git_version.inc.mk: deferred GIT_VERSION definition</li>
  <li>Makefile.include: use GIT_VERSION for RIOT_VERSION</li>
  <li>Makefile.include: Only evaluate RIOT_VERSION when needed</li>
</ul>

<h4>File Changes</h4>
<ul>
  <li>
    <strong>M</strong>
    <a href="https://github.com/RIOT-OS/RIOT/pull/11881/files#diff-0">Makefile.include</a>
    (24)
  </li>
  <li>
    <strong>A</strong>
    <a href="https://github.com/RIOT-OS/RIOT/pull/11881/files#diff-1">makefiles/git_version.inc.mk</a>
    (11)
  </li>
</ul>

<h4>Patch Links:</h4>
<ul>
  <li><a href='https://github.com/RIOT-OS/RIOT/pull/11881.patch'>https://github.com/RIOT-OS/RIOT/pull/11881.patch</a></li>
  <li><a href='https://github.com/RIOT-OS/RIOT/pull/11881.diff'>https://github.com/RIOT-OS/RIOT/pull/11881.diff</a></li>
</ul>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/RIOT-OS/RIOT/pull/11881?email_source=notifications&email_token=ABE7WYFRZTWUOKC5LXBCIELQAWUKLA5CNFSM4IFYI3YKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HAUCHKQ">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/ABE7WYCLZI6GKGQVZUIRON3QAWUKLANCNFSM4IFYI3YA">mute the thread</a>.<img src="https://github.com/notifications/beacon/ABE7WYCWPJ4TNTBFZGXUO53QAWUKLA5CNFSM4IFYI3YKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HAUCHKQ.gif" height="1" width="1" alt="" /></p>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/RIOT-OS/RIOT/pull/11881?email_source=notifications\u0026email_token=ABE7WYFRZTWUOKC5LXBCIELQAWUKLA5CNFSM4IFYI3YKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HAUCHKQ",
"url": "https://github.com/RIOT-OS/RIOT/pull/11881?email_source=notifications\u0026email_token=ABE7WYFRZTWUOKC5LXBCIELQAWUKLA5CNFSM4IFYI3YKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HAUCHKQ",
"name": "View Pull Request"
},
"description": "View this Pull Request on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>