Skip to content

Conversation

daminetreg
Copy link
Contributor

@daminetreg daminetreg commented Jun 24, 2025

This provides project options that can be overriden in cmake toolchain files, or can be marked as NON_OVERRIDABLE in toolchains files to error out if an user tries to override settings that should not be overriden from toolchain files.

It is meant to replace option() in project with a reliable and constant behaviour even when CMP077 is off.

It behaves with the following priority :

  1. -DOPTION passed by user always overrides or results in an error when an option is marked NON_OVERRIDABLE
  2. CMAKE_TOOLCHAIN_FILE options (marked with TOOLCHAIN_DEFAULT) always override project options values ( unless overriden by user ).

The tracking of the default option allows user to get a warning whenever the previous default of an user-overriden option changes.

This module exists to permit :

  • overridable toolchain options
  • configurable toolchains options
  • reliable project options

Rationale

Before CMake 3.13 if set() was used without CACHE the value would not be taken by option(), leading to a reconfiguration changing the toolchain prescribed value on the first build.

Also this leads to using CACHE FORCE because otherwise any modification of the value in the toolchain file would not be taken in an already configured build tree, requiring full rebuilds to test a simple toolchain value change.

Finally the new behaviour of option() since CMP0077 is for option() to respect scope variables in priority. This makes essentially plain option() set in a toolchain file non-overridable on the command line, as the cmake -D command line sets CACHE variables.

Usage:

  toolchain_overridable_option(
      <variable_name>
      "<cache comment string>"
      <default value>
      [TYPE [BOOL/PATH/FILEPATH/STRING]]
      [NON_OVERRIDABLE]
      [TOOLCHAIN_DEFAULT]
  )

Change-Id: I4f1f00d71039288eb344b8a0969fa12ed1091224

…ption()s

This provides project options that can be overriden in cmake toolchain
files, or can be marked as NON_OVERRIDABLE in toolchains files to error
out if an user tries to override settings that should not be overriden
from toolchain files.

It is meant to replace option() in project with a reliable and constant
behaviour even when CMP077 is off.

It behaves with the following priority :

1. -DOPTION passed by user always overrides or results in an error
     when an option is marked NON_OVERRIDABLE
2. CMAKE_TOOLCHAIN_FILE options (marked with TOOLCHAIN_DEFAULT)
   always override project options values ( unless overriden by user ).

The tracking of the default option allows user to get a warning
whenever the previous default of an user-overriden option changes.

This module exists to permit :
  * overridable toolchain options
  * configurable toolchains options
  * reliable project options

Rationale
=========
Before CMake 3.13 if set() was used without CACHE the value would not
be taken by option(), leading to a reconfiguration changing the toolchain
prescribed value on the first build.

Also this leads to using CACHE FORCE because otherwise any modification
of the value in the toolchain file would not be taken in an already
configured build tree, requiring full rebuilds to test a simple
toolchain value change.

Finally the new behaviour of option() since CMP0077 is for option() to
respect scope variables in priority. This makes essentially plain
option() set in a toolchain file non-overridable on the command line,
as the cmake -D command line sets CACHE variables.

Usage:

  toolchain_overridable_option(
      <variable_name>
      "<cache comment string>"
      <default value>
      [TYPE [BOOL/PATH/FILEPATH/STRING]]
      [NON_OVERRIDABLE]
      [TOOLCHAIN_DEFAULT]
  )

Change-Id: I4f1f00d71039288eb344b8a0969fa12ed1091224
@daminetreg daminetreg changed the title toolchain_overridable_option() : user + toolchain overridable project o… toolchain_overridable_option() : user + toolchain overridable project options()s Jun 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant