Skip to content

Commit

Permalink
HermeticFetchContent v1.0.X : Controlling build parallelism level
Browse files Browse the repository at this point in the history
CHANGELOG

- Improved the way that build parallelism settings are forwarded through the build graph so that the correct outcome is achieved depending on setting CMAKE_BUILD_PARALLEL_LEVEL via environment

Change-Id: I33ae3760b2a0ab00048f07ac2a7c14d3bbc460b7
  • Loading branch information
pysco68 committed Feb 13, 2025
1 parent 7d0e6e5 commit 4795d39
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 6 deletions.
3 changes: 1 addition & 2 deletions cmake/modules/hfc_autotools_register_content_build.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ function(hfc_autotools_register_content_build content_name)
endif()
set (AUTOTOOLS_MAKE_PROGRAM "${MAKE_PROGRAM}")

ProcessorCount(NUM_JOBS)
set (build_command "${MAKE_PROGRAM} -j ${NUM_JOBS}")
set (build_command "${MAKE_PROGRAM} -j @NUM_JOB_PLACEHOLDER@") # the @@ placeholder will be replaced in the generated external project

if (CMAKE_RE_ENABLE)
# CMake RE manages the install tree by version
Expand Down
3 changes: 1 addition & 2 deletions cmake/modules/hfc_cmake_register_content_build.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ function(hfc_cmake_register_content_build content_name)
# notes:
# the project was already configured correctly in the calling function, in this spot we just need
# to setup the external project to build & install (on demand or at configure time depending on preferences)
ProcessorCount(NUM_JOBS)
set(build_command "${CMAKE_COMMAND} --build . -j ${NUM_JOBS}")
set(build_command "${CMAKE_COMMAND} --build . -j @NUM_JOB_PLACEHOLDER@")
set(install_commands_list "")

# Ensure that before installing we have a clean install tree, not used by anyone
Expand Down
14 changes: 13 additions & 1 deletion cmake/templates/externalProject_Add_install.CMakeLists.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(HERMETIC_FETCHCONTENT_ROOT_DIR "@HERMETIC_FETCHCONTENT_ROOT_DIR@")
list(APPEND CMAKE_MODULE_PATH "${HERMETIC_FETCHCONTENT_ROOT_DIR}/modules")

include(ExternalProject)
include(ProcessorCount)
include(hfc_goldilock_helpers)

set(CMAKE_PROGRAM_PATH "@CMAKE_PROGRAM_PATH@") # inherit the CMAKE_PROGRAM_PATH of the project being configured/generating this file
Expand All @@ -31,10 +32,21 @@ if(NOT "${effective_source_dir}" STREQUAL "${input_BINARY_DIR}")
goldilock_command_append_lockfile("${input_BINARY_DIR}")
endif()

ProcessorCount(NUM_JOBS_default)
if(DEFINED ENV{CMAKE_BUILD_PARALLEL_LEVEL})
set(NUM_JOBS_default $ENV{CMAKE_BUILD_PARALLEL_LEVEL})
endif()

# this is going to be interpreted by bash and is basically:
# "use the value of CMAKE_BUILD_PARALLEL_LEVEL or fall back to number of CPUs as told to cmake during configuration"
set(NUM_JOB_PLACEHOLDER "\${CMAKE_BUILD_PARALLEL_LEVEL:-${NUM_JOBS_default}}")

string(CONFIGURE "@TEMPLATE_BUILD_CMD@" configure_command @ONLY)

# build the command (with explanations)
set(command_str "")
string(APPEND command_str "test -d ${effective_source_dir}") # fail is the source dir is not here for some reason
string(APPEND command_str " && ${goldilock_commands} -- /bin/bash -c \"@TEMPLATE_BUILD_CMD@ && @TEMPLATE_INSTALL_CMD@\"") # acquire all locks
string(APPEND command_str " && ${goldilock_commands} -- /bin/bash -c \"${configure_command} && @TEMPLATE_INSTALL_CMD@\"") # acquire all locks

ExternalProject_Add(@TEMPLATE_TARGET_NAME@
SOURCE_DIR "@TEMPLATE_SOURCE_DIR@"
Expand Down
1 change: 1 addition & 0 deletions docs/_sources/index.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Recipes

/recipes/HowToAddALibrary
/recipes/HowToDefineBuildEnvironments
/recipes/ControllingBuildParallelism


Index and Search
Expand Down
44 changes: 44 additions & 0 deletions docs/_sources/recipes/ControllingBuildParallelism.rst.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.. title:: HermeticFetchContent / Recipes / Controlling Build Parallelism

Controlling Build Parallelism
#############################

Because of the complex nature of the build graph in Hermetic FetchContent you may want to adjust the build
parallelity in some situations like CI or shared build nodes.

Hermetic FetchContent does use a combination of the following parameters to set the build parallelism:

- the number of CPU cores available on your system
- the value of the environment variable ``CMAKE_BUILD_PARALLEL_LEVEL``
- the value supplied to ``cmake --build`` via the ``-j <jobs>`` / ``--parallel <jobs>`` argument

Generally not that dependencies that are made available at **build time** will have their own, separate
allocation of CPU ressources as these dependent builds are generally unaware of the ressource allocations
of peer and parent projects. This means that you may end up with sever over-loading of the system depending
on the build graph of your project and available ressources.

When no setting is provided, Hermetic FetchContent will defer to the invoked build system (``ninja`` or ``make``
for example) to select the build parallelism (which typically fall back to the number of CPU cores).

The value provided via ``CMAKE_BUILD_PARALLEL_LEVEL`` is taken into account by CMake throughout the build graph,
meaning that the set value is used for each of the builds that make up the graph of your project.

The value passed to CMake via the ``-j <jobs>`` / ``--parallel <jobs>`` argument are taken into account **only
for the top level build graph**. This detail enables you to control the build parallelism for the project
and the dependency builds independently:

.. code-block:: bash
# $PWD = project root
export CMAKE_BUILD_PARALLEL_LEVEL=16
cmake -S . -B build/release/ -DCMAKE_BUILD_TYPE=Release ...
export CMAKE_BUILD_PARALLEL_LEVEL=2
cmake --build . -j10
The example above will have the following behavior

- all dependencies **made available at configure time** will use 16 CPU cores (which is not an issue as they are run in sequence)
- during the build
- dependencies **made available at build time** will use 2 CPU cores
- the top level build graph (your project) will use 10 CPU cores

1 change: 1 addition & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ <h2>Recipes<a class="headerlink" href="#recipes" title="Permalink to this headin
<li class="toctree-l2"><a class="reference internal" href="recipes/HowToDefineBuildEnvironments.html#cmake-re-and-tipi-remote-build-support">CMake-RE and tipi remote build support</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="recipes/ControllingBuildParallelism.html">Controlling Build Parallelism</a></li>
</ul>
</div>
</section>
Expand Down
Binary file modified docs/objects.inv
Binary file not shown.
173 changes: 173 additions & 0 deletions docs/recipes/ControllingBuildParallelism.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@

<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.19: https://docutils.sourceforge.io/" />

<title>HermeticFetchContent / Recipes / Controlling Build Parallelism &mdash; hermetic-fetchcontent Documentation</title>

<link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../_static/cmake.css" />

<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/sphinx_highlight.js"></script>

<link rel="icon" href="../_static/favicon.ico"/>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="prev" title="How to define build Environments" href="HowToDefineBuildEnvironments.html" />
<link rel="search" type="application/opensearchdescription+xml"
title="Search within CMake Documentation of Latest Version"
href="../../latest-opensearch.xml"/>
<script type="text/javascript" src="../../version_switch.js"></script>


</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="HowToDefineBuildEnvironments.html" title="How to define build Environments"
accesskey="P">previous</a> |</li>
<li>
<img src="../_static/tipi-logo.png" alt=""
style="vertical-align: middle; margin-top: -2px" />
</li>
<li>
<a href="https://tipi.build/">tipi</a> &#187;
</li>
<li>
<span class="version_switch"></span>
<a href="../index.html">Documentation</a> &#187;
</li>

<li class="nav-item nav-item-this"><a href="">HermeticFetchContent / Recipes / Controlling Build Parallelism</a></li>
</ul>
</div>

<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">

<section id="controlling-build-parallelism">
<h1>Controlling Build Parallelism<a class="headerlink" href="#controlling-build-parallelism" title="Permalink to this heading"></a></h1>
<p>Because of the complex nature of the build graph in Hermetic FetchContent you may want to adjust the build
parallelity in some situations like CI or shared build nodes.</p>
<p>Hermetic FetchContent does use a combination of the following parameters to set the build parallelism:</p>
<blockquote>
<div><ul class="simple">
<li><p>the number of CPU cores available on your system</p></li>
<li><p>the value of the environment variable <code class="docutils literal notranslate"><span class="pre">CMAKE_BUILD_PARALLEL_LEVEL</span></code></p></li>
<li><p>the value supplied to <code class="docutils literal notranslate"><span class="pre">cmake</span> <span class="pre">--build</span></code> via the <code class="docutils literal notranslate"><span class="pre">-j</span> <span class="pre">&lt;jobs&gt;</span></code> / <code class="docutils literal notranslate"><span class="pre">--parallel</span> <span class="pre">&lt;jobs&gt;</span></code> argument</p></li>
</ul>
</div></blockquote>
<p>Generally not that dependencies that are made available at <strong>build time</strong> will have their own, separate
allocation of CPU ressources as these dependent builds are generally unaware of the ressource allocations
of peer and parent projects. This means that you may end up with sever over-loading of the system depending
on the build graph of your project and available ressources.</p>
<p>When no setting is provided, Hermetic FetchContent will defer to the invoked build system (<code class="docutils literal notranslate"><span class="pre">ninja</span></code> or <code class="docutils literal notranslate"><span class="pre">make</span></code>
for example) to select the build parallelism (which typically fall back to the number of CPU cores).</p>
<p>The value provided via <code class="docutils literal notranslate"><span class="pre">CMAKE_BUILD_PARALLEL_LEVEL</span></code> is taken into account by CMake throughout the build graph,
meaning that the set value is used for each of the builds that make up the graph of your project.</p>
<p>The value passed to CMake via the <code class="docutils literal notranslate"><span class="pre">-j</span> <span class="pre">&lt;jobs&gt;</span></code> / <code class="docutils literal notranslate"><span class="pre">--parallel</span> <span class="pre">&lt;jobs&gt;</span></code> argument are taken into account <strong>only
for the top level build graph</strong>. This detail enables you to control the build parallelism for the project
and the dependency builds independently:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="c1"># $PWD = project root</span>
<span class="nb">export</span><span class="w"> </span><span class="nv">CMAKE_BUILD_PARALLEL_LEVEL</span><span class="o">=</span><span class="m">16</span>
cmake<span class="w"> </span>-S<span class="w"> </span>.<span class="w"> </span>-B<span class="w"> </span>build/release/<span class="w"> </span>-DCMAKE_BUILD_TYPE<span class="o">=</span>Release<span class="w"> </span>...
<span class="nb">export</span><span class="w"> </span><span class="nv">CMAKE_BUILD_PARALLEL_LEVEL</span><span class="o">=</span><span class="m">2</span>
cmake<span class="w"> </span>--build<span class="w"> </span>.<span class="w"> </span>-j10
</pre></div>
</div>
<p>The example above will have the following behavior</p>
<blockquote>
<div><ul class="simple">
<li><p>all dependencies <strong>made available at configure time</strong> will use 16 CPU cores (which is not an issue as they are run in sequence)</p></li>
<li><p>during the build
- dependencies <strong>made available at build time</strong> will use 2 CPU cores
- the top level build graph (your project) will use 10 CPU cores</p></li>
</ul>
</div></blockquote>
</section>


<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<div>
<h4>Previous topic</h4>
<p class="topless"><a href="HowToDefineBuildEnvironments.html"
title="previous chapter">How to define build Environments</a></p>
</div>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/recipes/ControllingBuildParallelism.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="HowToDefineBuildEnvironments.html" title="How to define build Environments"
>previous</a> |</li>
<li>
<img src="../_static/tipi-logo.png" alt=""
style="vertical-align: middle; margin-top: -2px" />
</li>
<li>
<a href="https://tipi.build/">tipi</a> &#187;
</li>
<li>
<span class="version_switch"></span>
<a href="../index.html">Documentation</a> &#187;
</li>

<li class="nav-item nav-item-this"><a href="">HermeticFetchContent / Recipes / Controlling Build Parallelism</a></li>
</ul>
</div>

<div class="footer" role="contentinfo">
&#169; Copyright tipi technologies Ltd..
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 6.2.1.
</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-6042509-4");
pageTracker._trackPageview();
} catch(err) {}
</script>
</body>
</html>
12 changes: 12 additions & 0 deletions docs/recipes/HowToDefineBuildEnvironments.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<link rel="icon" href="../_static/favicon.ico"/>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Controlling Build Parallelism" href="ControllingBuildParallelism.html" />
<link rel="prev" title="How to Add a Library with Hermetic FetchContent" href="HowToAddALibrary.html" />
<link rel="search" type="application/opensearchdescription+xml"
title="Search within CMake Documentation of Latest Version"
Expand All @@ -32,6 +33,9 @@ <h3>Navigation</h3>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="ControllingBuildParallelism.html" title="Controlling Build Parallelism"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="HowToAddALibrary.html" title="How to Add a Library with Hermetic FetchContent"
accesskey="P">previous</a> |</li>
Expand Down Expand Up @@ -204,6 +208,11 @@ <h4>Previous topic</h4>
<p class="topless"><a href="HowToAddALibrary.html"
title="previous chapter">How to Add a Library with Hermetic FetchContent</a></p>
</div>
<div>
<h4>Next topic</h4>
<p class="topless"><a href="ControllingBuildParallelism.html"
title="next chapter">Controlling Build Parallelism</a></p>
</div>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
Expand Down Expand Up @@ -231,6 +240,9 @@ <h3>Navigation</h3>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="ControllingBuildParallelism.html" title="Controlling Build Parallelism"
>next</a> |</li>
<li class="right" >
<a href="HowToAddALibrary.html" title="How to Add a Library with Hermetic FetchContent"
>previous</a> |</li>
Expand Down
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions documentation_src/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Recipes

/recipes/HowToAddALibrary
/recipes/HowToDefineBuildEnvironments
/recipes/ControllingBuildParallelism


Index and Search
Expand Down
44 changes: 44 additions & 0 deletions documentation_src/recipes/ControllingBuildParallelism.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.. title:: HermeticFetchContent / Recipes / Controlling Build Parallelism

Controlling Build Parallelism
#############################

Because of the complex nature of the build graph in Hermetic FetchContent you may want to adjust the build
parallelity in some situations like CI or shared build nodes.

Hermetic FetchContent does use a combination of the following parameters to set the build parallelism:

- the number of CPU cores available on your system
- the value of the environment variable ``CMAKE_BUILD_PARALLEL_LEVEL``
- the value supplied to ``cmake --build`` via the ``-j <jobs>`` / ``--parallel <jobs>`` argument

Generally not that dependencies that are made available at **build time** will have their own, separate
allocation of CPU ressources as these dependent builds are generally unaware of the ressource allocations
of peer and parent projects. This means that you may end up with sever over-loading of the system depending
on the build graph of your project and available ressources.

When no setting is provided, Hermetic FetchContent will defer to the invoked build system (``ninja`` or ``make``
for example) to select the build parallelism (which typically fall back to the number of CPU cores).

The value provided via ``CMAKE_BUILD_PARALLEL_LEVEL`` is taken into account by CMake throughout the build graph,
meaning that the set value is used for each of the builds that make up the graph of your project.

The value passed to CMake via the ``-j <jobs>`` / ``--parallel <jobs>`` argument are taken into account **only
for the top level build graph**. This detail enables you to control the build parallelism for the project
and the dependency builds independently:

.. code-block:: bash
# $PWD = project root
export CMAKE_BUILD_PARALLEL_LEVEL=16
cmake -S . -B build/release/ -DCMAKE_BUILD_TYPE=Release ...
export CMAKE_BUILD_PARALLEL_LEVEL=2
cmake --build . -j10
The example above will have the following behavior

- all dependencies **made available at configure time** will use 16 CPU cores (which is not an issue as they are run in sequence)
- during the build
- dependencies **made available at build time** will use 2 CPU cores
- the top level build graph (your project) will use 10 CPU cores

0 comments on commit 4795d39

Please sign in to comment.