Skip to content

Commit df299ca

Browse files
committed
document cross-compilation a bit more
1 parent 639da12 commit df299ca

File tree

1 file changed

+97
-4
lines changed

1 file changed

+97
-4
lines changed

sphinx/src/maintainer/knowledge_base.rst

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,68 @@ A package that needs all five compilers would define
303303
there's no need to specify ``libgcc`` or ``libgfortran``. There are additional informations about how conda-build 3 treats
304304
compilers in the `conda docs <https://docs.conda.io/projects/conda-build/en/stable/resources/compiler-tools.html>`__.
305305

306+
.. cross-compilation:
307+
306308
Cross-compilation
307309
-----------------
308310

309311
For some other architectures (like ARM), packages can be built natively on that architecture or they can be cross-compiled.
310-
In other words built on a different common architecture (like x86_64) while still targeting the original architecture (ARM).
312+
In other words, built on a different common architecture (like x86_64) while still targeting the original architecture (ARM).
311313
This helps one leverage more abundant CI resources in the build architecture (x86_64).
312314

313-
A package needs to make a few changes in their recipe to be compatible with cross-compilation. Here are a few examples.
315+
Cross-compilation settings depend on the ``build_platform`` and ``target_platform`` conda-build
316+
variables:
317+
318+
- ``build_platform``: The platform on which ``conda-build`` is running, which defines the ``build``
319+
environment in ``$BUILD_PREFIX``.
320+
- ``target_platform``: The platform on which the package will be installed. Defines the platform of
321+
the ``host`` environment in ``$PREFIX``. Defaults to the value of ``build_platform``.
322+
323+
To change the value of ``target_platform`` and enable cross-compilation, you must use
324+
the :ref:`build_platform` mapping in ``conda-forge.yml`` and then :ref:`rerender
325+
<dev_update_rerender>` the feedstock. This will generate the appropriate CI workflows and
326+
conda-build input metadata. See also :ref:`test` for how to skip the test phase when
327+
cross-compiling. Provided the requirements metadata and build scripts are written correctly, the
328+
package should just work. However, in some cases, it'll need some adjustments; see examples below
329+
for some common cases.
330+
331+
.. note::
332+
333+
The ``build_platform`` and ``target_platform`` variables are exposed as environment variables in
334+
the build scripts (e.g. ``$build_platform``), and also as Jinja variables in the ``meta.yaml``
335+
selectors (e.g. ``# [build_platform != target_platform]``).
336+
337+
In addition to these two variables, there are some more environment variables that are set by
338+
conda-forge's automation (e.g. ``conda-forge-ci-setup``, compiler activation packages, etc) that
339+
can aid in cross-compilation setups:
340+
341+
- ``CONDA_BUILD_CROSS_COMPILATION``: set to ``1`` when ``build_platform`` and ``target_platform``
342+
differ.
343+
- ``BUILD_PLATFORM``: the platform on which ``conda-build`` is running.
344+
- ``BUILD``: the autoconf triplet expected for build platform.
345+
- ``HOST_PLATFORM``: the platform on which the package will be installed.
346+
- ``HOST``: the autoconf triplet expected for host platform.
347+
- ``CMAKE_ARGS``: arguments needed to cross-compile with CMake. Pass it to ``cmake`` in your build
348+
script.
349+
- ``MESON_ARGS``: arguments needed to cross-compile with Meson. Pass it to ``meson`` in your build
350+
script. Note a `cross build definition file <https://mesonbuild.com/Cross-compilation.html>`__ is
351+
automatically created for you too.
352+
- ``CC_FOR_BUILD``: C compilers targeting the build platform.
353+
- ``CXX_FOR_BUILD``: C++ compilers targeting the build platform.
354+
355+
This is all supported by two main conda-build features introduced in version 3:
356+
357+
- How `requirements metadata
358+
<https://docs.conda.io/projects/conda-build/en/latest/resources/define-metadata.html#requirements-section>`__
359+
is expressed in ``meta.yaml``, which distinguishes between ``build`` and ``host`` platforms.
360+
- The ``compiler()`` Jinja function and underlying `conventions for the compiler packages
361+
<https://docs.conda.io/projects/conda-build/en/latest/resources/compiler-tools.html>`__.
362+
363+
Cross-compilation examples
364+
^^^^^^^^^^^^^^^^^^^^^^^^^^
365+
366+
A package needs to make a few changes in their recipe to be compatible with cross-compilation. Here
367+
are a few examples.
314368

315369
A simple C library using autotools for cross-compilation might look like this:
316370

@@ -323,7 +377,8 @@ A simple C library using autotools for cross-compilation might look like this:
323377
- pkg-config
324378
- gnuconfig
325379
326-
In the build script, it would need to update the config files and guard any tests when cross-compiling:
380+
In the build script, it would need to update the config files and guard any tests when
381+
cross-compiling:
327382

328383
.. code-block:: sh
329384
@@ -335,6 +390,44 @@ In the build script, it would need to update the config files and guard any test
335390
make check
336391
fi
337392
393+
A simple C++ library using CMake for cross-compilation might look like this:
394+
395+
.. code-block:: yaml
396+
397+
requirements:
398+
build:
399+
- {{ compiler("cxx") }}
400+
- cmake
401+
- make
402+
403+
In the build script, it would need to update ``cmake`` call and guard any tests when cross-compiling:
404+
405+
.. code-block:: sh
406+
407+
# Pass ``CMAKE_ARGS`` to ``cmake``
408+
cmake ${CMAKE_ARGS} ..
409+
410+
# Skip ``ctest`` when cross-compiling
411+
if [[ "${CONDA_BUILD_CROSS_COMPILATION}" != "1" ]]; then
412+
ctest
413+
fi
414+
415+
Similarly, with Meson:
416+
417+
.. code-block:: yaml
418+
419+
requirements:
420+
build:
421+
- {{ compiler("c") }}
422+
- {{ compiler("cxx") }}
423+
- meson
424+
- make
425+
426+
.. code-block:: sh
427+
428+
# Pass ``MESON_ARGS`` to ``meson``
429+
meson ${MESON_ARGS} builddir/
430+
338431
A simple Python extension using Cython and NumPy's C API would look like so:
339432

340433
.. code-block:: yaml
@@ -1705,7 +1798,7 @@ Apple Silicon builds
17051798
17061799
The new Apple M1 processor is the first Apple Silicon supported by conda-forge
17071800
`osx-arm64 <https://github.com/conda-forge/conda-forge.github.io/issues/1126>`__ builds.
1708-
For new builds to be available, via cross-compilation, a migration is required for
1801+
For new builds to be available, via `cross-compilation <cross-compilation>`_, a migration is required for
17091802
the package and its dependencies. These builds are experimental as many of them are untested.
17101803
17111804
To request a migration for a particular package and all its dependencies:

0 commit comments

Comments
 (0)