xref: /aosp_15_r20/external/pigweed/pw_toolchain/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_toolchain:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker============
4*61c4878aSAndroid Build Coastguard Workerpw_toolchain
5*61c4878aSAndroid Build Coastguard Worker============
6*61c4878aSAndroid Build Coastguard Worker.. pigweed-module::
7*61c4878aSAndroid Build Coastguard Worker   :name: pw_toolchain
8*61c4878aSAndroid Build Coastguard Worker
9*61c4878aSAndroid Build Coastguard WorkerGN toolchains function both as a set of tools for compilation and as a workspace
10*61c4878aSAndroid Build Coastguard Workerfor evaluating build files. The same compilations and actions can be executed by
11*61c4878aSAndroid Build Coastguard Workerdifferent toolchains. Each toolchain maintains its own set of build args, and
12*61c4878aSAndroid Build Coastguard Workerbuild steps from all toolchains can be executed in parallel.
13*61c4878aSAndroid Build Coastguard Worker
14*61c4878aSAndroid Build Coastguard Worker---------------------------------
15*61c4878aSAndroid Build Coastguard WorkerC/C++ toolchain support libraries
16*61c4878aSAndroid Build Coastguard Worker---------------------------------
17*61c4878aSAndroid Build Coastguard Worker``pw_toolchain`` provides some toolchain-related C/C++ libraries.
18*61c4878aSAndroid Build Coastguard Worker
19*61c4878aSAndroid Build Coastguard Worker``std:abort`` wrapper
20*61c4878aSAndroid Build Coastguard Worker=====================
21*61c4878aSAndroid Build Coastguard WorkerThe `std::abort <https://en.cppreference.com/w/cpp/utility/program/abort>`_
22*61c4878aSAndroid Build Coastguard Workerfunction is used to terminate a program abnormally. This function may be called
23*61c4878aSAndroid Build Coastguard Workerby standard library functions, so is often linked into binaries, even if users
24*61c4878aSAndroid Build Coastguard Workernever intentionally call it.
25*61c4878aSAndroid Build Coastguard Worker
26*61c4878aSAndroid Build Coastguard WorkerFor embedded builds, the ``abort`` implementation likely does not work as
27*61c4878aSAndroid Build Coastguard Workerintended. For example, it may pull in undesired dependencies (e.g.
28*61c4878aSAndroid Build Coastguard Worker``std::raise``) and end in an infinite loop.
29*61c4878aSAndroid Build Coastguard Worker
30*61c4878aSAndroid Build Coastguard Worker``pw_toolchain`` provides the ``pw_toolchain:wrap_abort`` library that replaces
31*61c4878aSAndroid Build Coastguard Worker``abort`` in builds where the default behavior is undesirable. It uses the
32*61c4878aSAndroid Build Coastguard Worker``-Wl,--wrap=abort`` linker option to redirect to ``abort`` calls to
33*61c4878aSAndroid Build Coastguard Worker``PW_CRASH`` instead.
34*61c4878aSAndroid Build Coastguard Worker
35*61c4878aSAndroid Build Coastguard Workerarm-none-eabi-gcc support
36*61c4878aSAndroid Build Coastguard Worker=========================
37*61c4878aSAndroid Build Coastguard WorkerTargets building with the GNU Arm Embedded Toolchain (``arm-none-eabi-gcc``)
38*61c4878aSAndroid Build Coastguard Workershould depend on the ``pw_toolchain/arm_gcc:arm_none_eabi_gcc_support``
39*61c4878aSAndroid Build Coastguard Workerlibrary. In GN, that target should be included in ``pw_build_LINK_DEPS``. In
40*61c4878aSAndroid Build Coastguard WorkerBazel, it should be added to `link_extra_lib
41*61c4878aSAndroid Build Coastguard Worker<https://bazel.build/reference/be/c-cpp#cc_binary.link_extra_lib>`__ or
42*61c4878aSAndroid Build Coastguard Workerdirectly to the `deps` of any binary being build with that toolchain:
43*61c4878aSAndroid Build Coastguard Worker
44*61c4878aSAndroid Build Coastguard Worker.. code-block:: python
45*61c4878aSAndroid Build Coastguard Worker
46*61c4878aSAndroid Build Coastguard Worker   cc_binary(
47*61c4878aSAndroid Build Coastguard Worker      deps = [
48*61c4878aSAndroid Build Coastguard Worker        # Other deps, omitted
49*61c4878aSAndroid Build Coastguard Worker      ] + select({
50*61c4878aSAndroid Build Coastguard Worker        "@platforms//cpu:armv7e-m": [
51*61c4878aSAndroid Build Coastguard Worker          "@pigweed//pw_toolchain/arm_gcc:arm_none_eabi_gcc_support",
52*61c4878aSAndroid Build Coastguard Worker        ],
53*61c4878aSAndroid Build Coastguard Worker        "//conditions:default": [],
54*61c4878aSAndroid Build Coastguard Worker      }),
55*61c4878aSAndroid Build Coastguard Worker   )
56*61c4878aSAndroid Build Coastguard Worker
57*61c4878aSAndroid Build Coastguard WorkerNewlib OS interface
58*61c4878aSAndroid Build Coastguard Worker-------------------
59*61c4878aSAndroid Build Coastguard Worker`Newlib <https://sourceware.org/newlib/>`_, the C Standard Library
60*61c4878aSAndroid Build Coastguard Workerimplementation provided with ``arm-none-eabi-gcc``, defines a set of `OS
61*61c4878aSAndroid Build Coastguard Workerinterface functions <https://sourceware.org/newlib/libc.html#Stubs>`_ that
62*61c4878aSAndroid Build Coastguard Workershould be implemented. Newlib provides default implementations, but using these
63*61c4878aSAndroid Build Coastguard Workerresults in linker warnings like the following:
64*61c4878aSAndroid Build Coastguard Worker
65*61c4878aSAndroid Build Coastguard Worker.. code-block:: none
66*61c4878aSAndroid Build Coastguard Worker
67*61c4878aSAndroid Build Coastguard Worker   readr.c:(.text._read_r+0x10): warning: _read is not implemented and will always fail
68*61c4878aSAndroid Build Coastguard Worker
69*61c4878aSAndroid Build Coastguard WorkerMost of the OS interface functions should never be called in embedded builds.
70*61c4878aSAndroid Build Coastguard WorkerThe ``pw_toolchain/arg_gcc:newlib_os_interface_stubs`` library, which is
71*61c4878aSAndroid Build Coastguard Workerprovided through ``pw_toolchain/arm_gcc:arm_none_eabi_gcc_support``, implements
72*61c4878aSAndroid Build Coastguard Workerthese functions and forces a linker error if they are used. It also
73*61c4878aSAndroid Build Coastguard Workerautomatically includes a wrapper for ``abort`` for use of ``stdout`` and
74*61c4878aSAndroid Build Coastguard Worker``stderr`` which abort if they are called.
75*61c4878aSAndroid Build Coastguard Worker
76*61c4878aSAndroid Build Coastguard WorkerIf you need to use your own wrapper for ``abort``, include the library directly
77*61c4878aSAndroid Build Coastguard Workerusing ``pw_toolchain/arm_gcc:newlib_os_interface_stubs``.
78*61c4878aSAndroid Build Coastguard Worker
79*61c4878aSAndroid Build Coastguard Workerpw_toolchain/no_destructor.h
80*61c4878aSAndroid Build Coastguard Worker============================
81*61c4878aSAndroid Build Coastguard Worker.. doxygenclass:: pw::NoDestructor
82*61c4878aSAndroid Build Coastguard Worker
83*61c4878aSAndroid Build Coastguard Workerbuiltins
84*61c4878aSAndroid Build Coastguard Worker========
85*61c4878aSAndroid Build Coastguard Workerbuiltins are LLVM's equivalent of libgcc, the compiler will insert calls to
86*61c4878aSAndroid Build Coastguard Workerthese routines. Setting the ``dir_pw_third_party_builtins`` gn var to your
87*61c4878aSAndroid Build Coastguard Workercompiler-rt/builtins checkout will enable building builtins from source instead
88*61c4878aSAndroid Build Coastguard Workerof relying on the shipped libgcc.
89*61c4878aSAndroid Build Coastguard Worker
90*61c4878aSAndroid Build Coastguard Worker.. toctree::
91*61c4878aSAndroid Build Coastguard Worker   :hidden:
92*61c4878aSAndroid Build Coastguard Worker   :maxdepth: 1
93*61c4878aSAndroid Build Coastguard Worker
94*61c4878aSAndroid Build Coastguard Worker   bazel
95*61c4878aSAndroid Build Coastguard Worker   gn
96