Submitted By: Ken Moffat <ken at linuxfromscratch dot org>
Date: 2024-02-18
Initial Package Version: 6.6.1
Upstream Status: Not acceptable, fixes a QTBUG decision.
Origin: Gentoo
Description: Allows *some* user flags to be used.

To see what flags are being used, after the build has started
(only a second or two on a fast machine)
 grep FLAGS build/build.ninja | sort -u

A couple of gentoo patches,
 01-toolchain.patch 03-gn-bootstrap.patch -

Patch status: here forever(?)

Respect user flags, and provide readelf path.

Upstream has seemingly intentionally left out extra_* variables so
flags are not respected. It may arguably be easier to overwrite this
file with chromium's toolchain/linux/unbundle/BUILD.gn which then uses
e.g. CC+CFLAGS and BUILD_CC+BUILD_CFLAGS env vars and bypass cmake.

Albeit qtwebengine's does set a few extras in there and so
still using for now.

File is processed twice for host(CBUILD) and target(CHOST), so it
is better to use CMAKE_* variables where possible (still subpar).
Odds are cross is still completely broken for many reasons though.

https://bugs.gentoo.org/652172
--- a/src/host/BUILD.toolchain.gn.in
+++ b/src/host/BUILD.toolchain.gn.in
@@ -8,3 +8,7 @@
   nm = "@CMAKE_NM@"
-  extra_cppflags = ""
+  readelf = "@CMAKE_READELF@"
+  extra_cflags = "@CMAKE_C_FLAGS@"
+  extra_cppflags = getenv("CPPFLAGS")
+  extra_cxxflags = "@CMAKE_CXX_FLAGS@"
+  extra_ldflags = "@CMAKE_EXE_LINKER_FLAGS@"
   toolchain_args = {
Patch status: here forever(?), not respecting flags is intentional

Original gen.py respects *FLAGS to some extend, but Qt blocked
this after a user reported a bug with overzealous *FLAGS[2].

So re-add, but also put more at the end to allow overrides.
Also respect NINJAFLAGS and prevent -static-libstdc++ for libcxx.

TODO: fix cross
  Technically should use BUILD_CXXFLAGS and so here but, even
  without trying, cross is most likely broken given it insists
  on finding gn in CBUILD rather than cross-compile it. To avoid
  more patching, an option may be BDEPEND="dev-qt/qtwebengine-gn"
  that would do INSTALL_GN=ON and not build qtwebengine.

Note that qtwebengine's gn is modified, so dev-util/gn may not
be a straight forward to use as an alternative and be fragile.

[1] https://github.com/qt/qtwebengine-chromium/commit/19e8b974c8348dd6ba33153facc0b67a7ab774e1
[2] https://bugreports.qt.io/browse/QTBUG-64759
[3] https://bugs.gentoo.org/652172
--- a/src/3rdparty/gn/build/gen.py
+++ b/src/3rdparty/gn/build/gen.py
@@ -942,4 +942,7 @@
   executables['gn_unittests']['libs'].extend(static_libraries.keys())
 
+  cflags += os.environ.get('CXXFLAGS', '').split()
+  cflags += os.environ.get('CPPFLAGS', '').split()
+  ldflags += os.environ.get('LDFLAGS', '').split()
   WriteGenericNinja(path, static_libraries, executables, cxx, ar, ld,
                     platform, host, options, args_list,
--- a/src/gn/CMakeLists.txt
+++ b/src/gn/CMakeLists.txt
@@ -35,4 +35,5 @@
 endif()
 
+separate_arguments(NINJAFLAGS UNIX_COMMAND "$ENV{NINJAFLAGS}")
 file(MAKE_DIRECTORY ${GN_BINARY_DIR})
 
@@ -63,7 +64,8 @@
         --ar ${CMAKE_AR}
         --qt-version "${QT_REPO_MODULE_VERSION}.qtwebengine.qt.io"
+        --no-static-libstdc++
         $<$<PLATFORM_ID:Darwin>:--isysroot>
         $<$<PLATFORM_ID:Darwin>:${CMAKE_OSX_SYSROOT}>
-    COMMAND Ninja::ninja -C ${GN_BINARY_DIR}/$<CONFIG> ${GN_EXECUTABLE}
+    COMMAND Ninja::ninja ${NINJAFLAGS} -C ${GN_BINARY_DIR}/$<CONFIG> ${GN_EXECUTABLE}
     VERBATIM
     USES_TERMINAL
