1################################################################################
2#
3# flutter-engine
4#
5################################################################################
6
7# Flutter-engine has a release on the GitHub page. However, that release is
8# only for Google. Its intended purpose is for the gclient tool provided by
9# Google in their depot-tools package. To use the source code, we must use
10# gclient to download the flutter-engine source code along with several other
11# projects. Unfortunately, the Buildroot download system does not have the
12# capability of using gclient, and considering this package is the only
13# package that uses gclient, we side-step the entire download process and
14# perform the following steps if a tarball does not exist already:
15#
16#  - Copy the pre-made gclient config file to a temporary scratch directory.
17#  - Run gclient sync to generate a source directory with the proper
18#    flutter-engine source code in the correct places.
19#  - Create a source tarball.
20#  - Copy the source tarball to the $(FLUTTER_ENGINE_DL_DIR) directory.
21#
22# There is no hash provided, as the gn binary (used for configuration) relies
23# on the .git directories. As such, a reproducible tarball is not possible.
24FLUTTER_ENGINE_VERSION = 3.19.5
25
26# There is nothing for Buildroot to download. This is handled by gclient.
27FLUTTER_ENGINE_SITE =
28FLUTTER_ENGINE_SOURCE =
29FLUTTER_ENGINE_LICENSE = BSD-3-Clause
30FLUTTER_ENGINE_LICENSE_FILES = LICENSE
31FLUTTER_ENGINE_TARBALL_PATH = $(FLUTTER_ENGINE_DL_DIR)/flutter-$(FLUTTER_ENGINE_VERSION).tar.gz
32FLUTTER_ENGINE_INSTALL_STAGING = YES
33FLUTTER_ENGINE_DOWNLOAD_DEPENDENCIES = host-depot-tools
34FLUTTER_ENGINE_DEPENDENCIES = \
35	host-flutter-sdk-bin \
36	host-ninja \
37	host-pkgconf \
38	freetype \
39	zlib
40
41# Dispatch all architectures of flutter
42# FLUTTER_ENGINE_TARGET_TRIPPLE must match the directory name found in
43# buildtools/linux-x64/clang/lib/clang/*/lib
44ifeq ($(BR2_aarch64),y)
45FLUTTER_ENGINE_TARGET_ARCH = arm64
46FLUTTER_ENGINE_TARGET_TRIPPLE = aarch64-unknown-linux-gnu
47else ifeq ($(BR2_arm)$(BR2_armeb),y)
48FLUTTER_ENGINE_TARGET_ARCH = arm
49FLUTTER_ENGINE_TARGET_TRIPPLE = armv7-unknown-linux-gnueabihf
50else ifeq ($(BR2_x86_64),y)
51FLUTTER_ENGINE_TARGET_ARCH = x64
52FLUTTER_ENGINE_TARGET_TRIPPLE = x86_64-unknown-linux-gnu
53endif
54
55ifeq ($(FLUTTER_ENGINE_RUNTIME_MODE_PROFILE),y)
56FLUTTER_ENGINE_RUNTIME_MODE=profile
57else ifeq ($(BR2_ENABLE_RUNTIME_DEBUG),y)
58FLUTTER_ENGINE_RUNTIME_MODE=debug
59else
60FLUTTER_ENGINE_RUNTIME_MODE=release
61endif
62
63FLUTTER_ENGINE_BUILD_DIR = \
64	$(@D)/out/linux_$(FLUTTER_ENGINE_RUNTIME_MODE)_$(FLUTTER_ENGINE_TARGET_ARCH)
65
66FLUTTER_ENGINE_INSTALL_FILES = libflutter_engine.so
67
68# Flutter engine includes a bundled patched clang that must be used for
69# compiling or else there are linking errors.
70FLUTTER_ENGINE_CLANG_PATH = $(@D)/buildtools/linux-x64/clang
71
72FLUTTER_ENGINE_CONF_OPTS = \
73	--clang \
74	--embedder-for-target \
75	--linux-cpu $(FLUTTER_ENGINE_TARGET_ARCH) \
76	--no-build-embedder-examples \
77	--no-clang-static-analyzer \
78	--no-enable-unittests \
79	--no-goma \
80	--no-prebuilt-dart-sdk \
81	--no-stripped \
82	--runtime-mode $(FLUTTER_ENGINE_RUNTIME_MODE) \
83	--target-os linux \
84	--target-sysroot $(STAGING_DIR) \
85	--target-toolchain $(FLUTTER_ENGINE_CLANG_PATH) \
86	--target-triple $(FLUTTER_ENGINE_TARGET_TRIPPLE)
87
88ifeq ($(BR2_arm)$(BR2_armeb),y)
89FLUTTER_ENGINE_CONF_OPTS += \
90	--arm-float-abi $(call qstrip,$(BR2_GCC_TARGET_FLOAT_ABI))
91endif
92
93# We must specify a full path to ccache and a full path to the flutter-engine
94# provided clang in order to use ccache, or else flutter-engine will error out
95# attempting to find ccache in the target-toolchain provided path.
96ifeq ($(BR2_CCACHE),y)
97define FLUTTER_ENGINE_COMPILER_PATH_FIXUP
98	$(SED) "s%cc =.*%cc = \"$(HOST_DIR)/bin/ccache $(FLUTTER_ENGINE_CLANG_PATH)/bin/clang\""%g \
99		$(@D)/build/toolchain/custom/BUILD.gn
100
101	$(SED) "s%cxx =.*%cxx = \"$(HOST_DIR)/bin/ccache $(FLUTTER_ENGINE_CLANG_PATH)/bin/clang++\""%g \
102		$(@D)/build/toolchain/custom/BUILD.gn
103endef
104FLUTTER_ENGINE_PRE_CONFIGURE_HOOKS += FLUTTER_ENGINE_COMPILER_PATH_FIXUP
105endif
106
107ifeq ($(BR2_ENABLE_LTO),y)
108FLUTTER_ENGINE_CONF_OPTS += --lto
109else
110FLUTTER_ENGINE_CONF_OPTS += --no-lto
111endif
112
113ifeq ($(BR2_OPTIMIZE_0),y)
114FLUTTER_ENGINE_CONF_OPTS += --unoptimized
115endif
116
117# There is no --disable-fontconfig option.
118ifeq ($(BR2_PACKAGE_FONTCONFIG),y)
119FLUTTER_ENGINE_DEPENDENCIES += fontconfig
120FLUTTER_ENGINE_CONF_OPTS += --enable-fontconfig
121endif
122
123ifeq ($(BR2_PACKAGE_HAS_LIBGL),y)
124FLUTTER_ENGINE_DEPENDENCIES += libgl
125endif
126
127ifeq ($(BR2_PACKAGE_HAS_LIBGLES),y)
128FLUTTER_ENGINE_DEPENDENCIES += libgles
129FLUTTER_ENGINE_CONF_OPTS += --enable-impeller-opengles
130endif
131
132ifeq ($(BR2_PACKAGE_LIBGLFW),y)
133FLUTTER_ENGINE_DEPENDENCIES += libglfw
134FLUTTER_ENGINE_CONF_OPTS += --build-glfw-shell
135else
136FLUTTER_ENGINE_CONF_OPTS += --no-build-glfw-shell
137endif
138
139ifeq ($(BR2_PACKAGE_LIBGTK3),y)
140FLUTTER_ENGINE_DEPENDENCIES += libgtk3
141FLUTTER_ENGINE_INSTALL_FILES += libflutter_linux_gtk.so
142else
143FLUTTER_ENGINE_CONF_OPTS += --disable-desktop-embeddings
144endif
145
146# There is no --disable-vulkan option
147ifeq ($(BR2_PACKAGE_MESA3D_VULKAN_DRIVER),y)
148FLUTTER_ENGINE_CONF_OPTS += --enable-vulkan --enable-impeller-vulkan
149endif
150
151ifeq ($(BR2_PACKAGE_XORG7)$(BR2_PACKAGE_LIBXCB),yy)
152FLUTTER_ENGINE_DEPENDENCIES += libxcb
153else
154define FLUTTER_ENGINE_VULKAN_X11_SUPPORT_FIXUP
155	$(SED) "s%vulkan_use_x11.*%vulkan_use_x11 = false%g" -i \
156		$(@D)/build_overrides/vulkan_headers.gni
157
158	$(SED) "s%ozone_platform_x11.*%ozone_platform_x11 = false%g" \
159		$(@D)/build/config/BUILDCONFIG.gn
160endef
161FLUTTER_ENGINE_PRE_CONFIGURE_HOOKS += FLUTTER_ENGINE_VULKAN_X11_SUPPORT_FIXUP
162endif
163
164ifeq ($(BR2_PACKAGE_WAYLAND),y)
165FLUTTER_ENGINE_DEPENDENCIES += wayland
166else
167define FLUTTER_ENGINE_VULKAN_WAYLAND_SUPPORT_FIXUP
168	$(SED) "s%vulkan_use_wayland.*%vulkan_use_wayland = false%g" \
169		$(@D)/build_overrides/vulkan_headers.gni
170
171	$(SED) "s%ozone_platform_wayland.*%ozone_platform_wayland = false%g" \
172		$(@D)/build/config/BUILDCONFIG.gn
173endef
174FLUTTER_ENGINE_PRE_CONFIGURE_HOOKS += FLUTTER_ENGINE_VULKAN_WAYLAND_SUPPORT_FIXUP
175endif
176
177# Generate a tarball if one does not already exist.
178define FLUTTER_ENGINE_GENERATE_TARBALL
179	TAR="$(TAR)" \
180	PATH=$(HOST_DIR)/share/depot_tools:$(BR_PATH) \
181	PYTHONPATH=$(HOST_DIR)/lib/python$(PYTHON3_VERSION_MAJOR) \
182	PUB_CACHE=$(FLUTTER_SDK_BIN_PUB_CACHE) \
183	$(FLUTTER_ENGINE_PKGDIR)/gen-tarball \
184		--dot-gclient $(TOPDIR)/$(FLUTTER_ENGINE_PKGDIR)/dot-gclient \
185		--jobs $(PARALLEL_JOBS) \
186		--scratch-dir $(@D)/dl-tmp \
187		--tarball-dl-path $(FLUTTER_ENGINE_TARBALL_PATH) \
188		--version $(FLUTTER_ENGINE_VERSION)
189endef
190FLUTTER_ENGINE_POST_DOWNLOAD_HOOKS += FLUTTER_ENGINE_GENERATE_TARBALL
191
192define FLUTTER_ENGINE_EXTRACT_CMDS
193	$(call suitable-extractor,$(FLUTTER_ENGINE_TARBALL_PATH)) $(FLUTTER_ENGINE_TARBALL_PATH) \
194	| $(TAR) --strip-components=1 -C $(@D) $(TAR_OPTIONS) -
195endef
196
197# We must set the home directory to the sdk directory or else flutter will
198# place .dart, and .flutter in ~/.
199define FLUTTER_ENGINE_CONFIGURE_CMDS
200	cd $(@D) && \
201		rm -rf $(FLUTTER_ENGINE_BUILD_DIR) && \
202		PATH=$(HOST_DIR)/share/depot_tools:$(BR_PATH) \
203		PUB_CACHE=$(FLUTTER_SDK_BIN_PUB_CACHE) \
204		HOME=$(HOST_FLUTTER_SDK_BIN_SDK) \
205		./flutter/tools/gn \
206			$(FLUTTER_ENGINE_CONF_OPTS)
207endef
208
209define FLUTTER_ENGINE_BUILD_CMDS
210	cd $(@D) && \
211		PATH=$(HOST_DIR)/share/depot_tools:$(BR_PATH) \
212		PUB_CACHE=$(FLUTTER_SDK_BIN_PUB_CACHE) \
213		HOME=$(HOST_FLUTTER_SDK_BIN_SDK) \
214		$(HOST_DIR)/bin/ninja \
215			-j $(PARALLEL_JOBS) \
216			-C $(FLUTTER_ENGINE_BUILD_DIR)
217endef
218
219# Flutter-engine builds several host applications use for flutter development.
220# One of those applications is gen_snapshot, which turns applications into
221# usable .so files for the target architecture. Compiling a host version of
222# flutter-engine would result in binaries producing host-architecture
223# flutter applications .so files. As such, copy gen_snapshot to the host
224# directory here and name it flutter_gen_snapshot for clarity.
225FLUTTER_ENGINE_GEN_SNAPSHOT = $(HOST_DIR)/bin/flutter_gen_snapshot
226define FLUTTER_ENGINE_INSTALL_GEN_SNAPSHOT
227	$(INSTALL) -D -m 0755 $(FLUTTER_ENGINE_BUILD_DIR)/clang_x64/gen_snapshot \
228		$(HOST_DIR)/bin/flutter_gen_snapshot
229endef
230FLUTTER_ENGINE_POST_INSTALL_STAGING_HOOKS += FLUTTER_ENGINE_INSTALL_GEN_SNAPSHOT
231
232define FLUTTER_ENGINE_INSTALL_STAGING_CMDS
233	$(foreach i,$(FLUTTER_ENGINE_INSTALL_FILES),
234		$(INSTALL) -D -m 0755 $(FLUTTER_ENGINE_BUILD_DIR)/so.unstripped/$(i) \
235			$(STAGING_DIR)/usr/lib/$(i); \
236	)
237	$(INSTALL) -D -m 0755 $(FLUTTER_ENGINE_BUILD_DIR)/flutter_embedder.h \
238		$(STAGING_DIR)/usr/include/flutter_embedder.h
239
240	$(INSTALL) -D -m 0755 $(FLUTTER_ENGINE_BUILD_DIR)/icudtl.dat \
241		$(STAGING_DIR)/usr/share/flutter/$(FLUTTER_ENGINE_RUNTIME_MODE)/data/icudtl.dat
242endef
243
244define FLUTTER_ENGINE_INSTALL_TARGET_CMDS
245	$(foreach i,$(FLUTTER_ENGINE_INSTALL_FILES),
246		$(INSTALL) -D -m 0755 $(FLUTTER_ENGINE_BUILD_DIR)/so.unstripped/$(i) \
247			$(TARGET_DIR)/usr/lib/$(i); \
248	)
249	$(INSTALL) -D -m 0755 $(FLUTTER_ENGINE_BUILD_DIR)/icudtl.dat \
250		$(TARGET_DIR)/usr/share/flutter/$(FLUTTER_ENGINE_RUNTIME_MODE)/data/icudtl.dat
251endef
252
253$(eval $(generic-package))
254