@@ -82,11 +82,20 @@ void main(List<String> args) async {
8282 flags.addAll (_appleLinkerStripFlags);
8383 frameworks = ['UIKit' , 'Foundation' , 'CoreGraphics' ];
8484 case OS .linux:
85+ final backend = _linuxBackend (input);
86+ final linuxPackages = [
87+ backend == 'gtk4' ? 'gtk4' : 'gtk+-3.0' ,
88+ 'x11' ,
89+ 'xi' ,
90+ ];
91+ flags.add (
92+ backend == 'gtk4' ? '-DNATIVEAPI_LINUX_GTK4' : '-DNATIVEAPI_LINUX_GTK3' ,
93+ );
8594 flags.addAll (_sizeAndVisibilityFlags);
8695 flags.addAll (_elfLinkerStripFlags);
87- flags.addAll (await _pkgConfig (['--cflags' , ..._linuxPackages ]));
96+ flags.addAll (await _pkgConfig (['--cflags' , ...linuxPackages ]));
8897 // Libraries go after the sources, where --as-needed keeps them.
89- for (final flag in await _pkgConfig (['--libs' , ..._linuxPackages ])) {
98+ for (final flag in await _pkgConfig (['--libs' , ...linuxPackages ])) {
9099 if (flag.startsWith ('-l' )) {
91100 libraries.add (flag.substring (2 ));
92101 } else if (flag.startsWith ('-L' )) {
@@ -182,7 +191,21 @@ const _elfLinkerStripFlags = [
182191
183192const _appleLinkerStripFlags = ['-Wl,-dead_strip' , '-Wl,-x' ];
184193
185- const _linuxPackages = ['gtk+-3.0' , 'x11' , 'xi' ];
194+ /// The Linux GTK backend to link against: `gtk3` (default, matching Flutter's
195+ /// Linux embedder) or `gtk4` . Configured via
196+ /// `hooks.user_defines.cnativeapi.linux_backend` or `NATIVEAPI_LINUX_BACKEND` .
197+ String _linuxBackend (BuildInput input) {
198+ final defined = input.userDefines['linux_backend' ];
199+ final raw = (defined ?? Platform .environment['NATIVEAPI_LINUX_BACKEND' ] ?? 'gtk3' )
200+ .toString ()
201+ .toLowerCase ();
202+ if (raw == 'gtk3' || raw == 'gtk4' ) {
203+ return raw;
204+ }
205+ throw FormatException (
206+ 'hooks.user_defines.cnativeapi.linux_backend must be "gtk3" or "gtk4"; got $raw ' ,
207+ );
208+ }
186209
187210/// The platforms the app builds without the native library, from
188211/// `hooks.user_defines.cnativeapi.skip_platforms` in its pubspec.
0 commit comments