Here's a different attempt to solve the issue at #31338 comment:13
This will need to be tested with #29039, #31396, for which this mechanism was created.
CC: @kiwifb @antonio-rojas
Component: build
Author: Matthias Koeppe
Branch/Commit: 0867811
Reviewer: François Bissey
Issue created by migration from https://trac.sagemath.org/ticket/32057