You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmake: passing options from depends build to main build #30813
Created a separate issue for this because I have some thoughts about it and don't want to hijack the discussion in #30800 which is more about _FORTIFY_SOURCE and detecting which optimization flags are set (as opposed to setting them). Working on an idea which I'll post below.
I saw #29796 too, but it seems to be a discussion of which optimization flags are set, not how the flags passed from the depends build to the main build.
I saw #29796 too, but it seems to be a discussion of which optimization flags are set, not how the flags are set.
Sure. If the discussion is in general, about how options should be passed from depends to CMake, then I think (ideally) the answer is just, a toolchain file, and our CMake build should take (pretty much verbatim) what is set in that file (and not try and detect if it's a "depends" build, and behave differently if that is the case). That seems like the only way to do things, if we want to have an interface into our CMake build system, that isn't Core specific, which can be used by any other projects/builders/distros etc, that will/or might want to bring their own toolchains.
Should the optimization and debugging flags coincide, or are they allowed to differ?
Not sure I understand this question, but they have to be allowed to differ. There's no "correct" answer for what the optimisation flags might be when debugging.
If the optimization and debugging can differ between the depends subsystem and in the main build system, which should take precedence?
I guess this is, in the default case, answered by the above, but also requires a change like #29796, so the options are coherent.
I think the way things would work ideally is that depends system would set default build flags, and main build system would pick them up reliably, but the defaults could be freely overridden and edited by users running ccmake or passing -D values to cmake. Specifically this would mean:
Depends settings in the toolchain file would reliably take precedence over cmake defaults and over defaults normally set by the main build system.
User-defined -D and ccmake settings would reliably take precedence over depends settings in the toolchain file.
Right now looking at depends/toolchain.cmake.in it seems like both of these things can happen in some cases, but neither of them happen reliably. Specifically in the current system:
The toolchain file force-writes into the cache and overwrites user settings.
The toolchain file is only able to weakly set *_INIT build flags that are later modified by cmake, instead of setting variables that will be used reliably.
The depends system sets DEPENDS_COMPILE_DEFINITIONS* variables that don't show up in the cmake configuration and users can't edit.
I think to implement desired behavior, toolchain.cmake.in could be changed to:
Stop setting cache variables like BUILD_GUI and WITH_QRENCODE and instead set custom non-cache variables like DEFAULT_BUILD_GUIDEFAULT_WITH_QRENCODE.
Stop setting CMAKE_*_FLAGS*_INIT variables and instead set custom DEFAULT_*_FLAGS* variables.
Stop setting DEPENDS_COMPILE_DEFINITIONS* variables and instead use DEFAULT_*_FLAGS*.
To support this, main build system CMakeLists.txt file could be changed to:
Use DEFAULT_* variables (if set), as default values for corresponding * options.
Use DEFAULT_*_FLAGS* variables (if set) as default values for CMAKE_*_FLAGS* variables.
if (DEFINED DEFAULT_CXX_FLAGS_DEBUG)
set(CMAKE_CXX_FLAGS_DEBUG_INIT "${DEFAULT_CXX_FLAGS_DEBUG}")
endif()
repeated for different DEFAULT_*_FLAGS* variables
I think also for this to work, our current ProcessConfigurations.cmake file would need to be tweaked to not force-set cache variables that have a corresponding DEFAULT_* variable set.
I only did a little experimentation with toolchain and make_rules_overrides settings, so I'm not sure if this idea fully works, or is the best way implement the desired behavior.
I'm also not sure the desired behavior described above (having depends defaults take precedence over non-depends defaults, and not letting depends system clobber or ignore values explicitly set by the user) is really the best behavior either. Maybe it's not safe or flexible enough, or wouldn't work in an important use-case. Just posting this as a suggestion to consider or discuss.
Right now looking at depends/toolchain.cmake.in it seems like both of these things can happen in some cases, but neither of them happen reliably. Specifically in the current system:
The toolchain file force-writes into the cache and overwrites user settings.
...
I think to implement desired behavior, toolchain.cmake.in could be changed to:
Stop setting cache variables like BUILD_GUI and WITH_QRENCODE and instead set custom non-cache variables like DEFAULT_BUILD_GUIDEFAULT_WITH_QRENCODE.
I don't think this describes the current behaviour precisely. Let's consider these lines of code:
I don't think this describes the current behaviour precisely.
Yes this is definitely not describing current behavior, it's trying to describe how current behavior "could be changed" to avoid writing the cache. The first part of #30813 (comment) describes ideal behavior and contrasts it with current behavior, and the second part describes a specific way I think ideal behavior could be implemented.
I think current behavior is ok, but if we are running into problems or are looking for ways to improve it, this approach could get rid of current limitations and remove surprises.
FWIW, something similar was considered during work on the CMake staging branch.
Interesting! Although that PR was using CMAKE_USER_MAKE_RULES_OVERRIDE it was doing something pretty different with it. It was overriding cmake's low level compile command lines, which is definitely not something I would suggest doing here.
Suggestion above is to use CMAKE_USER_MAKE_RULES_OVERRIDE only to set _INIT variables more reliably than they can be set from the toolchain file. If they are set in the toolchain file, cmake will modify the _INIT values and append from the environment, while if they are set in the override file, cmake will use the set values with no changes. Doing this may be helpful if we want to give depends system more control over the flags that are used and get rid of DEPENDS_COMPILE_DEFINITIONS* variables. I believe it would be good to get rid of DEPENDS_COMPILE_DEFINITIONS* variables because they are not visible in the cache, not customizable, and nonstandard while also being redundant with standard variables.
I don't think this describes the current behaviour precisely.
Yes this is definitely not describing current behavior, it's trying to describe how current behavior "could be changed" to avoid writing the cache.
I'm still confused. Why should we "avoid writing the cache" for variables such as WITH_ZMQ? Using cache variables is a standard CMake method that allows the user to set their values.
For example cmake appends -g to CMAKE_CXX_FLAGS_DEBUG_INIT after the toolchain sets it
That's true. From CMake CMAKE_<LANG>_FLAGS_<CONFIG>_INIT variable's docs:
CMake may prepend or append content to the value based on the environment and target platform.
Suggestion above is to use CMAKE_USER_MAKE_RULES_OVERRIDE only to set _INIT variables more reliably than they can be set from the toolchain file.I agree that this approach would work. But it has a drawback making the main build system less compatible with toolchain files provided by other developers / distro maintainers etc.
I agree that this approach would work, but it has a drawback: it makes the main build system less compatible with toolchain files provided by other developers, distro maintainers, etc.
I'm still confused. Why should we "avoid writing the cache" for variables such as WITH_ZMQ? Using cache variables is a standard CMake method that allows the user to set their values.
Sorry that was not clear. I should have said that "force writing the cache" and overwriting values set by users should be avoided. Ideal behavior is that user can run depends build and it will set default cache values. Then they can run ccmake or plain cmake and change settings freely and those values will be respected, instead of overwritten. Approach where depends build provides non-cached DEFAULT_<option> values instead of force-setting <option> values in the cache provides this, and avoid user value being silently discarded, which happens currently.
I agree that this approach would work, but it has a drawback: it makes the main build system less compatible with toolchain files provided by other developers, distro maintainers, etc.
This is not true because suggestion is for toolchain to set DEFAULT_<option> variables, not to overwrite <option> variables. If the CMAKE_USER_MAKE_RULES_OVERRIDE file is populated as suggested with:
if (DEFINED DEFAULT_CXX_FLAGS_DEBUG)
set(CMAKE_CXX_FLAGS_DEBUG_INIT "${DEFAULT_CXX_FLAGS_DEBUG}")
endif()
The build would be compatible with outside or pre-existing toolchain files because those toolchains would not define DEFAULT_<option> values, so any <option>_INIT variables they did set would be unchanged.
Additionally, if we remove settings of non-toolchain variables, such as BUILD_GUI or ENABLE_WALLET from the toolchain file, it allows the reuse of this toolchain file to build depends themselves, instead of relying on this code:
Activity
fanquake commentedon Sep 4, 2024
I think this is a duplicate of #29796? (or at least, it'd be good to try and keep the discussion in one place).
ryanofsky commentedon Sep 4, 2024
Created a separate issue for this because I have some thoughts about it and don't want to hijack the discussion in #30800 which is more about _FORTIFY_SOURCE and detecting which optimization flags are set (as opposed to setting them). Working on an idea which I'll post below.
I saw #29796 too, but it seems to be a discussion of which optimization flags are set, not how the flags passed from the depends build to the main build.
fanquake commentedon Sep 4, 2024
Sure. If the discussion is in general, about how options should be passed from depends to CMake, then I think (ideally) the answer is just, a toolchain file, and our CMake build should take (pretty much verbatim) what is set in that file (and not try and detect if it's a "depends" build, and behave differently if that is the case). That seems like the only way to do things, if we want to have an interface into our CMake build system, that isn't Core specific, which can be used by any other projects/builders/distros etc, that will/or might want to bring their own toolchains.
Not sure I understand this question, but they have to be allowed to differ. There's no "correct" answer for what the optimisation flags might be when debugging.
I guess this is, in the default case, answered by the above, but also requires a change like #29796, so the options are coherent.
ryanofsky commentedon Sep 4, 2024
I think the way things would work ideally is that depends system would set default build flags, and main build system would pick them up reliably, but the defaults could be freely overridden and edited by users running
ccmakeor passing-Dvalues tocmake. Specifically this would mean:-Dandccmakesettings would reliably take precedence over depends settings in the toolchain file.Right now looking at
depends/toolchain.cmake.init seems like both of these things can happen in some cases, but neither of them happen reliably. Specifically in the current system:*_INITbuild flags that are later modified by cmake, instead of setting variables that will be used reliably.DEPENDS_COMPILE_DEFINITIONS*variables that don't show up in the cmake configuration and users can't edit.I think to implement desired behavior,
toolchain.cmake.incould be changed to:BUILD_GUIandWITH_QRENCODEand instead set custom non-cache variables likeDEFAULT_BUILD_GUIDEFAULT_WITH_QRENCODE.CMAKE_*_FLAGS*_INITvariables and instead set customDEFAULT_*_FLAGS*variables.DEPENDS_COMPILE_DEFINITIONS*variables and instead useDEFAULT_*_FLAGS*.To support this, main build system
CMakeLists.txtfile could be changed to:DEFAULT_*variables (if set), as default values for corresponding*options.DEFAULT_*_FLAGS*variables (if set) as default values forCMAKE_*_FLAGS*variables.Implementing the second part is a little tricky because setting
CMAKE_*_FLAGS*directly would override user values. And CMake does provideCMAKE_*_FLAGS*_INITvariables to be able to influence the defaults of the corresponding flag variables, but these are not used verbatim. For example cmake appends-gtoCMAKE_CXX_FLAGS_DEBUG_INITafter the toolchain sets it, uses the modified value as the defaultCMAKE_CXX_FLAGS_DEBUGvalue before it is cached. There are number of solutions for this listed in https://stackoverflow.com/questions/28732209/change-default-value-of-cmake-cxx-flags-debug-and-friends-in-cmake but it seems like a very clean one is to set theCMAKE_USER_MAKE_RULES_OVERRIDEhook variable to point to a cmake file that contains:repeated for different
DEFAULT_*_FLAGS*variablesI think also for this to work, our current
ProcessConfigurations.cmakefile would need to be tweaked to not force-set cache variables that have a correspondingDEFAULT_*variable set.I only did a little experimentation with toolchain and make_rules_overrides settings, so I'm not sure if this idea fully works, or is the best way implement the desired behavior.
I'm also not sure the desired behavior described above (having depends defaults take precedence over non-depends defaults, and not letting depends system clobber or ignore values explicitly set by the user) is really the best behavior either. Maybe it's not safe or flexible enough, or wouldn't work in an important use-case. Just posting this as a suggestion to consider or discuss.
hebasto commentedon Sep 6, 2024
I don't think this describes the current behaviour precisely. Let's consider these lines of code:
bitcoin/depends/toolchain.cmake.in
Lines 114 to 119 in 0e5cd60
CMake documents the
set(... CACHE ...)command behaviour as follows:hebasto commentedon Sep 6, 2024
FWIW, something similar was considered during work on the CMake staging branch.
ryanofsky commentedon Sep 6, 2024
Yes this is definitely not describing current behavior, it's trying to describe how current behavior "could be changed" to avoid writing the cache. The first part of #30813 (comment) describes ideal behavior and contrasts it with current behavior, and the second part describes a specific way I think ideal behavior could be implemented.
I think current behavior is ok, but if we are running into problems or are looking for ways to improve it, this approach could get rid of current limitations and remove surprises.
ryanofsky commentedon Sep 6, 2024
Interesting! Although that PR was using CMAKE_USER_MAKE_RULES_OVERRIDE it was doing something pretty different with it. It was overriding cmake's low level compile command lines, which is definitely not something I would suggest doing here.
Suggestion above is to use CMAKE_USER_MAKE_RULES_OVERRIDE only to set _INIT variables more reliably than they can be set from the toolchain file. If they are set in the toolchain file, cmake will modify the _INIT values and append from the environment, while if they are set in the override file, cmake will use the set values with no changes. Doing this may be helpful if we want to give depends system more control over the flags that are used and get rid of DEPENDS_COMPILE_DEFINITIONS* variables. I believe it would be good to get rid of DEPENDS_COMPILE_DEFINITIONS* variables because they are not visible in the cache, not customizable, and nonstandard while also being redundant with standard variables.
hebasto commentedon Sep 7, 2024
I'm still confused. Why should we "avoid writing the cache" for variables such as
WITH_ZMQ? Using cache variables is a standard CMake method that allows the user to set their values.hebasto commentedon Sep 7, 2024
That's true. From CMake
CMAKE_<LANG>_FLAGS_<CONFIG>_INITvariable's docs:I agree that this approach would work, but it has a drawback: it makes the main build system less compatible with toolchain files provided by other developers, distro maintainers, etc.
ryanofsky commentedon Sep 9, 2024
Sorry that was not clear. I should have said that "force writing the cache" and overwriting values set by users should be avoided. Ideal behavior is that user can run depends build and it will set default cache values. Then they can run
ccmakeor plaincmakeand change settings freely and those values will be respected, instead of overwritten. Approach where depends build provides non-cachedDEFAULT_<option>values instead of force-setting<option>values in the cache provides this, and avoid user value being silently discarded, which happens currently.This is not true because suggestion is for toolchain to set
DEFAULT_<option>variables, not to overwrite<option>variables. If theCMAKE_USER_MAKE_RULES_OVERRIDEfile is populated as suggested with:The build would be compatible with outside or pre-existing toolchain files because those toolchains would not define
DEFAULT_<option>values, so any<option>_INITvariables they did set would be unchanged.hebasto commentedon Sep 25, 2024
Additionally, if we remove settings of non-toolchain variables, such as
BUILD_GUIorENABLE_WALLETfrom the toolchain file, it allows the reuse of this toolchain file to build depends themselves, instead of relying on this code:bitcoin/depends/funcs.mk
Lines 173 to 199 in 39219fe
hebasto commentedon Oct 2, 2024
This discussion might be related: https://discourse.cmake.org/t/is-there-a-downside-for-a-toolchain-to-set-vars-in-cache-instead-of-init/12034.