Skip to content

Add documentation for Multilib custom flags #114998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jan 16, 2025

Conversation

vhscampos
Copy link
Member

@vhscampos vhscampos commented Nov 5, 2024

This patch is the fourth step to extend the current multilib system to support the selection of library variants which do not correspond to existing command-line options.

Proposal can be found in https://discourse.llvm.org/t/rfc-multilib-custom-flags/81058

The multilib mechanism supports libraries that target code generation or language options such as --target, -mcpu, -mfpu, -mbranch-protection. However, some library variants are particular to features that do not correspond to any command-line options. Examples include variants for multithreading and semihosting.

This work introduces a way to instruct the multilib system to consider these features in library selection. This particular patch updates the documentation.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Nov 5, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 5, 2024

@llvm/pr-subscribers-clang

Author: Victor Campos (vhscampos)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/114998.diff

1 Files Affected:

  • (modified) clang/docs/Multilib.rst (+116)
diff --git a/clang/docs/Multilib.rst b/clang/docs/Multilib.rst
index 7637d0db9565b8..7c165d149d7a4c 100644
--- a/clang/docs/Multilib.rst
+++ b/clang/docs/Multilib.rst
@@ -122,6 +122,104 @@ subclass and a suitable base multilib variant is present then the
 It is the responsibility of layered multilib authors to ensure that headers and
 libraries in each layer are complete enough to mask any incompatibilities.
 
+Multilib custom flags
+=====================
+
+Introduction
+------------
+
+The multilib mechanism supports library variants that correspond to target,
+code generation or language command-line flags. Examples include ``--target``,
+``-mcpu``, ``-mfpu``, ``-mbranch-protection``, ``-fno-rtti``. However, some library
+variants are particular to features that do not correspond to any command-line
+option. Multithreading and semihosting, for instance, have no associated
+compiler option.
+
+In order to support the selection of variants for which no compiler option
+exists, the multilib specification includes the concept of *custom flags*.
+These flags have no impact on code generation and are only used in the multilib
+processing.
+
+Multilib custom flags follow this format in the driver invocation:
+
+::
+
+  -fmultilib-flag=<value>
+
+They are fed into the multilib system alongside the remaining flags.
+
+Custom flag declarations
+------------------------
+
+Custom flags can be declared in the YAML file under the *Flags* section.
+
+.. code-block:: yaml
+
+  Flags:
+  - Name: multithreaded
+    Values:
+    - Name: no-multithreaded
+      ExtraBuildArgs: [-D__SINGLE_THREAD__]
+    - Name: multithreaded
+    Default: no-multithreaded
+
+* Name: the name to categorize a flag.
+* Values: a list of flag *Value*s (defined below).
+* Default: it specifies the name of the value this flag should take if not
+  specified in the command-line invocation. It must be one value from the Values
+  field.
+
+A Default value is useful to save users from specifying custom flags that have a
+most commonly used value.
+
+Each flag *Value* is defined as:
+
+* Name: name of the value. This is the string to be used in
+  ``-fmultilib-flag=<string>``.
+* ExtraBuildArgs: a list of strings corresponding to the extra build arguments
+  used to build a library variant that's in accordance to this specific custom
+  flag value.
+
+The namespace of flag values is common across all flags. This means that flag
+value names must be unique.
+
+Usage of custom flags in the *Variants* specifications
+------------------------------------------------------
+
+Library variants should list their requirement on one or more custom flags like
+they do for any other flag. Each requirement must be listed as
+``-fmultilib-flag=<value>``.
+
+A variant that does not specify a requirement on one particular flag can be
+matched against any value of that flag.
+
+Use of ``-print-multi-lib`` by build systems
+--------------------------------------------
+
+Some library build systems use the ``-print-multi-lib`` command-line option to
+query what library variants are shipped or supported by the target compiler and
+what command-line options should be used to build the variants.
+
+In this use case, a build system queries the target toolchain about what library
+variants should be built. With this information in hand, the build system may
+launch the build of each variant using the collected command-line arguments.
+
+For example, in *newlib*, multithreading is enabled by default and can be
+disabled by defining the ``__SINGLE_THREAD__`` macro. Therefore a multilib YAML
+file that is used to drive a *newlib* build must encode this information in the
+output of ``-print-multi-lib``.
+
+To account for this use case, custom flag values may specify the
+*ExtraBuildArgs* field. This optional field is a list of strings to be printed
+alongside the other command-line arguments in the output of
+``-print-multi-lib``. In the example of *newlib* and its multithreading support,
+a variant specific for single threaded execution should list
+``-D__SINGLE_THREAD__`` in its corresponding flag value's *ExtraBuildArgs*
+field.
+
+Since this information is specific for users of ``-print-multi-lib``, that is,
+for *builders* of library variants, it has no relevance in library *selection*.
+
 Stability
 =========
 
@@ -222,6 +320,24 @@ For a more comprehensive example see
     # Flags is a list of one or more strings.
     Flags: [--target=thumbv7m-none-eabi]
 
+  # Custom flag declarations. Each item is a different declaration.
+  Flags:
+    # Name of the flag
+  - Name: multithreaded
+    # List of custom flag values
+    Values:
+      # Name of the custom flag value. To be used in -fmultilib-flag=<string>.
+    - Name: no-multithreaded
+      # Extra build arguments to be printed with -print-multi-lib. Useful for
+      # specifying extra arguments for building the the associated library
+      # variant(s).
+      ExtraBuildArgs: [-D__SINGLE_THREAD__]
+    - Name: multithreaded
+    # Default flag value. If no value for this flag declaration is used in the
+    # command-line, the multilib system will use this one. Must be equal to one
+    # of the flag value names from this flag declaration.
+    Default: no-multithreaded
+
 Design principles
 =================
 

@vhscampos vhscampos force-pushed the users/vhscampos/multilib-flags-processing branch from 0b76e71 to b2de258 Compare November 20, 2024 14:47
@vhscampos vhscampos force-pushed the users/vhscampos/multilib-flags-documentation branch 3 times, most recently from 3425219 to a940ccd Compare November 25, 2024 17:01
@vhscampos vhscampos force-pushed the users/vhscampos/multilib-flags-processing branch from f6ff48d to 3bc43d3 Compare December 10, 2024 12:51
@vhscampos vhscampos force-pushed the users/vhscampos/multilib-flags-documentation branch from a940ccd to 226531d Compare December 10, 2024 12:53
@vhscampos vhscampos force-pushed the users/vhscampos/multilib-flags-processing branch from 3bc43d3 to 414c55e Compare December 11, 2024 13:30
@vhscampos vhscampos force-pushed the users/vhscampos/multilib-flags-documentation branch from 226531d to 60ea585 Compare December 11, 2024 13:30
Copy link
Member

@lenary lenary left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

This patch is the first step to extend the current multilib system to
support the selection of library variants which do not correspond to
existing command-line options.

Proposal can be found in
https://discourse.llvm.org/t/rfc-multilib-custom-flags/81058

The multilib mechanism supports libraries that target code generation or
language options such as `--target`, `-mcpu`, `-mfpu`,
`-mbranch-protection`. However, some library variants are particular to
features that do not correspond to any command-line options. Examples
include variants for multithreading and semihosting.

This work introduces a way to instruct the multilib system to consider
these features in library selection. This particular patch comprises a
new section in `multilib.yaml` to declare flags for which no option
exists. Henceforth this sort of flag will be called `custom flag` for
clarity.

The `multilib.yaml` file will have a new section called Flags which
contains the declarations of the target’s custom flags:

```yaml
Flags:
- Name: multithreaded
  Values:
  - Name: no-multithreaded
    MacroDefines: [__SINGLE_THREAD__]
  - Name: multithreaded
  Default: no-multithreaded

- Name: io
  Values:
    - Name: io-none
    - Name: io-semihosting
      MacroDefines: [SEMIHOSTING]
    - Name: io-linux-syscalls
      MacroDefines: [LINUX_SYSCALLS, HOSTED=1]
   Default: io-none
```
- Name: the name to categorize a flag.
- Values: a list of possible values.
- Default: it specifies which value this flag should take if not
specified in the command-line invocation. It must be one value from the
Values field.

Each flag Value follows this description:
- Name (required): the name of the custom flag value (string). This is
the string to be used in `-fmultilib-flag=<string>`.
- MacroDefines (optional): a list of strings to be used as macro
definitions. Each string
  is fed into the driver as ``-D<string>``.

A Default value is useful to save users from specifying custom flags
that have a most commonly used value.

The namespace of flag values is common across all flags. This means that
flag values must be unique.
Select library variants in the multilib system using the flags passed
following the '-fmultilib-flag=' format.

Multilib flags that were not passed in the command-line have their
default value fed into the library selection mechanism.

A warning is shown if the flag's value name is invalid. If the wrong
name is close enough to any valid one, according to edit distance, the
closest valid value name is suggested.

Details about this change can be found in this thread:
https://discourse.llvm.org/t/rfc-multilib-custom-flags/81058
The expected output is reliant on the syntax of file hierarchy. This
varies significantly between Linux and Windows. Following what other
multilib tests do, I am disabling the test on Windows.
@vhscampos vhscampos force-pushed the users/vhscampos/multilib-flags-processing branch from 06a5187 to 71dd4c2 Compare January 14, 2025 14:09
@vhscampos vhscampos force-pushed the users/vhscampos/multilib-flags-documentation branch from f4024af to fbe3c1c Compare January 14, 2025 14:14
@lenary
Copy link
Member

lenary commented Jan 14, 2025

I'm still happy with this, and it is for docs, so I don't think the barrier to landing it is very high.

Base automatically changed from users/vhscampos/multilib-flags-processing to main January 16, 2025 09:35
@vhscampos vhscampos merged commit 226a9d7 into main Jan 16, 2025
6 of 9 checks passed
@vhscampos vhscampos deleted the users/vhscampos/multilib-flags-documentation branch January 16, 2025 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants