Skip to content

Commit b01f27f

Browse files
authored
Move the C++ embedder code to a client wrapper (#272)
Rather than have a C++ interface in the shared library, make the C++ code a client wrapper layer that plugins and embedders consume. Ideally in the future more code will move out of this layer and behind a C interface in the library, so that the wrapper is as thin as possible, but this addresses the immediate issue of the shared library not having a viable interface.
1 parent 35649c1 commit b01f27f

38 files changed

+354
-181
lines changed

build/BUILDCONFIG.gn

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,27 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
default_static_configs = [ "//build:defaults" ]
16+
17+
default_dynamic_configs = [
18+
"//build:defaults",
19+
"//build:shared_library_defaults",
20+
]
21+
1522
set_defaults("executable") {
16-
configs = [ "//build:defaults" ]
23+
configs = default_static_configs
1724
}
1825
set_defaults("static_library") {
19-
configs = [ "//build:defaults" ]
26+
configs = default_static_configs
27+
}
28+
set_defaults("source_set") {
29+
configs = default_static_configs
2030
}
2131
set_defaults("shared_library") {
22-
configs = [ "//build:defaults", "//build:shared_library_defaults" ]
32+
configs = default_dynamic_configs
2333
}
2434
set_defaults("published_shared_library") {
25-
configs = [ "//build:defaults", "//build:shared_library_defaults" ]
35+
configs = default_dynamic_configs
2636
}
2737

2838
if (host_os == "linux") {
@@ -40,3 +50,10 @@ if (host_os == "linux") {
4050
is_mac = false
4151
is_win = true
4252
}
53+
54+
declare_args() {
55+
# Whether to use the GLFW embedding implementation. Currently overriding this
56+
# would not work since each platform only has one working implementation,
57+
# but in the future GLFW may be one of multiple supported implementations.
58+
use_glfw = is_linux || is_win
59+
}

example/linux_fde/Makefile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ BIN_OUT=$(OUT_DIR)/flutter_embedder_example
7878
ICU_DATA_OUT=$(OUT_DATA_DIR)/$(ICU_DATA_NAME)
7979
ALL_LIBS_OUT=$(foreach lib,$(ALL_LIBS),$(OUT_LIB_DIR)/$(notdir $(lib)))
8080

81+
# Add relevant code from the wrapper library, which is intended to be statically
82+
# built into the client.
83+
WRAPPER_ROOT = $(GN_OUT_DIR)/fde_cpp_wrapper
84+
WRAPPER_SOURCES = \
85+
$(WRAPPER_ROOT)/flutter_window_controller.cc \
86+
$(WRAPPER_ROOT)/plugin_handler.cc \
87+
$(WRAPPER_ROOT)/engine_method_result.cc
88+
SOURCES += $(WRAPPER_SOURCES)
89+
INCLUDE_DIRS += $(WRAPPER_ROOT)/include
90+
91+
8192
# Build settings
8293
CXX=g++ -std=c++14
8394
CXXFLAGS.release=-DNDEBUG
@@ -104,7 +115,7 @@ $(BIN_OUT): $(SOURCES) $(ALL_LIBS_OUT)
104115
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(SOURCES) $(LDFLAGS) -o $@
105116

106117
.PHONY: gnbuild
107-
gnbuild: $(GN_OUT_DIR)
118+
gnbuild $(WRAPPER_SOURCES): $(GN_OUT_DIR)
108119
$(NINJA_BIN) -C $(GN_OUT_DIR) $(FLUTTER_EMBEDDER_LIB_NAME) $(PLUGIN_NAMES)
109120

110121
$(GN_OUT_DIR):

example/windows_fde/GLFW Example.vcxproj

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@
4545
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug Dynamic Library|x64'">
4646
<OutDir>$(SolutionDir)bin\$(Platform)\$(Configuration)\$(ProjectName)\</OutDir>
4747
<IntDir>$(SolutionDir)bin\intermediates\$(Platform)\$(Configuration)\$(ProjectName)\</IntDir>
48-
<IncludePath>$(ProjectDir)..\..\library\windows\dependencies\;$(ProjectDir)..\..\;$(IncludePath);$(ProjectDir)..\..\library\windows\;$(ProjectDir)..\..\library\include\</IncludePath>
48+
<IncludePath>$(ProjectDir)..\..\library\windows\dependencies\;$(ProjectDir)..\..\;$(IncludePath);$(ProjectDir)..\..\library\windows\;$(ProjectDir)..\..\library\include\;$(ProjectDir)..\..\library\common\client_wrapper\include\</IncludePath>
4949
<LibraryPath>$(ProjectDir)..\..\library\windows\dependencies\GLFW\;$(SolutionDir)bin\$(Platform)\$(Configuration)\GLFW Library\;$(LibraryPath)</LibraryPath>
5050
</PropertyGroup>
5151
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release Dynamic Library|x64'">
5252
<OutDir>$(SolutionDir)bin\$(Platform)\$(Configuration)\$(ProjectName)\</OutDir>
5353
<IntDir>$(SolutionDir)bin\intermediates\$(Platform)\$(Configuration)\$(ProjectName)\</IntDir>
54-
<IncludePath>$(ProjectDir)..\..\library\windows\dependencies\;$(ProjectDir)..\..\library\include\;$(IncludePath)</IncludePath>
54+
<IncludePath>$(ProjectDir)..\..\library\windows\dependencies\;$(ProjectDir)..\..\;$(IncludePath);$(ProjectDir)..\..\library\windows\;$(ProjectDir)..\..\library\include\;$(ProjectDir)..\..\library\common\client_wrapper\include\</IncludePath>
5555
<LibraryPath>$(ProjectDir)..\..\library\windows\dependencies\GLFW\;$(SolutionDir)bin\$(Platform)\$(Configuration)\GLFW Library\;$(LibraryPath)</LibraryPath>
5656
</PropertyGroup>
5757
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug Dynamic Library|x64'">
@@ -62,6 +62,7 @@
6262
<ConformanceMode>true</ConformanceMode>
6363
<AdditionalIncludeDirectories>
6464
</AdditionalIncludeDirectories>
65+
<PreprocessorDefinitions>_MBCS;%(PreprocessorDefinitions);USE_FDE_TREE_PATHS</PreprocessorDefinitions>
6566
</ClCompile>
6667
<Link>
6768
<AdditionalDependencies>flutter_embedder.lib;glfw3.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
@@ -91,6 +92,7 @@
9192
<ConformanceMode>true</ConformanceMode>
9293
<AdditionalIncludeDirectories>
9394
</AdditionalIncludeDirectories>
95+
<PreprocessorDefinitions>_MBCS;%(PreprocessorDefinitions);USE_FDE_TREE_PATHS</PreprocessorDefinitions>
9496
</ClCompile>
9597
<Link>
9698
<EnableCOMDATFolding>true</EnableCOMDATFolding>
@@ -113,6 +115,9 @@
113115
</PostBuildEvent>
114116
</ItemDefinitionGroup>
115117
<ItemGroup>
118+
<ClCompile Include="..\..\library\common\client_wrapper\src\engine_method_result.cc" />
119+
<ClCompile Include="..\..\library\common\client_wrapper\src\glfw\flutter_window_controller.cc" />
120+
<ClCompile Include="..\..\library\common\client_wrapper\src\plugin_handler.cc" />
116121
<ClCompile Include="flutter_embedder_example.cpp" />
117122
</ItemGroup>
118123
<ItemGroup>
@@ -126,4 +131,4 @@
126131
<PropertyGroup>
127132
<LocalDebuggerWorkingDirectory>$(SolutionDir)</LocalDebuggerWorkingDirectory>
128133
</PropertyGroup>
129-
</Project>
134+
</Project>
Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<ItemGroup>
4-
<Filter Include="Source Files">
5-
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6-
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7-
</Filter>
8-
<Filter Include="Header Files">
9-
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10-
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
11-
</Filter>
12-
<Filter Include="Resource Files">
13-
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14-
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15-
</Filter>
16-
</ItemGroup>
17-
<ItemGroup>
18-
<ClCompile Include="flutter_embedder_example.cpp">
19-
<Filter>Source Files</Filter>
20-
</ClCompile>
21-
</ItemGroup>
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
<Filter Include="Source Files\Client Wrapper">
17+
<UniqueIdentifier>{2761a4b5-57b2-4d50-a677-d20ddc17a7f1}</UniqueIdentifier>
18+
</Filter>
19+
</ItemGroup>
20+
<ItemGroup>
21+
<ClCompile Include="flutter_embedder_example.cpp">
22+
<Filter>Source Files</Filter>
23+
</ClCompile>
24+
<ClCompile Include="..\..\library\common\client_wrapper\src\engine_method_result.cc">
25+
<Filter>Source Files\Client Wrapper</Filter>
26+
</ClCompile>
27+
<ClCompile Include="..\..\library\common\client_wrapper\src\glfw\flutter_window_controller.cc">
28+
<Filter>Source Files\Client Wrapper</Filter>
29+
</ClCompile>
30+
<ClCompile Include="..\..\library\common\client_wrapper\src\json_message_codec.cc">
31+
<Filter>Source Files\Client Wrapper</Filter>
32+
</ClCompile>
33+
<ClCompile Include="..\..\library\common\client_wrapper\src\json_method_codec.cc">
34+
<Filter>Source Files\Client Wrapper</Filter>
35+
</ClCompile>
36+
<ClCompile Include="..\..\library\common\client_wrapper\src\plugin_handler.cc">
37+
<Filter>Source Files\Client Wrapper</Filter>
38+
</ClCompile>
39+
</ItemGroup>
2240
</Project>

library/BUILD.gn

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,14 @@ import("//build/flutter.gni")
1616
import("//build/packaging.gni")
1717
import("//library/engine.gni")
1818

19-
declare_args() {
20-
# Whether to use the GLFW embedding implementation. Currently overriding this
21-
# would not work since each platform only has one working implementation,
22-
# but in the future GLFW may be one of multiple supported implementations.
23-
use_glfw = is_linux || is_win
24-
}
25-
2619
published_shared_library("flutter_embedder") {
2720
# GLFW embedding implementation.
2821
if (use_glfw) {
2922
public = [
30-
"include/flutter_desktop_embedding/glfw/embedder.h",
31-
"include/flutter_desktop_embedding/glfw/flutter_window_controller.h",
23+
"include/flutter_desktop_embedding_core/glfw/embedder.h",
3224
]
3325
sources = [
3426
"common/glfw/embedder.cc",
35-
"common/glfw/flutter_window_controller.cc",
3627
"common/glfw/key_event_handler.cc",
3728
"common/glfw/key_event_handler.h",
3829
"common/glfw/keyboard_hook_handler.h",
@@ -41,42 +32,32 @@ published_shared_library("flutter_embedder") {
4132
]
4233
}
4334

35+
deps = [
36+
":flutter_engine",
37+
]
38+
4439
# Embedding-agnostic shared C++.
4540
if (is_linux || is_win) {
4641
sources += [
47-
"common/engine_method_result.cc",
4842
"common/internal/incoming_message_dispatcher.cc",
4943
"common/internal/incoming_message_dispatcher.h",
50-
"common/internal/plugin_handler.cc",
51-
"common/internal/plugin_handler.h",
5244
"common/internal/text_input_model.cc",
5345
"common/internal/text_input_model.h",
54-
"common/json_message_codec.cc",
55-
"common/json_method_codec.cc",
5646
]
57-
public += [
58-
"include/flutter_desktop_embedding/basic_message_channel.h",
59-
"include/flutter_desktop_embedding/binary_messenger.h",
60-
"include/flutter_desktop_embedding/engine_method_result.h",
61-
"include/flutter_desktop_embedding/fde_export.h",
62-
"include/flutter_desktop_embedding/json_message_codec.h",
63-
"include/flutter_desktop_embedding/json_method_codec.h",
64-
"include/flutter_desktop_embedding/message_codec.h",
65-
"include/flutter_desktop_embedding/method_call.h",
66-
"include/flutter_desktop_embedding/method_channel.h",
67-
"include/flutter_desktop_embedding/method_codec.h",
68-
"include/flutter_desktop_embedding/method_result.h",
69-
"include/flutter_desktop_embedding/plugin_registrar.h",
47+
public += [ "include/flutter_desktop_embedding_core/fde_export.h" ]
48+
49+
deps += [
50+
"//library/common/client_wrapper:client_wrapper_internal",
51+
"//library/common/client_wrapper:publish_wrapper",
7052
]
7153
}
7254

73-
deps = [
74-
":flutter_engine",
55+
defines = [
56+
"FLUTTER_DESKTOP_EMBEDDING_IMPL",
57+
"USE_FDE_TREE_PATHS",
7558
]
7659

77-
defines = [ "FLUTTER_DESKTOP_EMBEDDING_IMPL" ]
78-
79-
public_header_subdir = "flutter_desktop_embedding"
60+
public_header_subdir = "flutter_desktop_embedding_core"
8061

8162
public_configs = [ ":relative_public_headers" ]
8263

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
_wrapper_includes = [
16+
"include/flutter_desktop_embedding/basic_message_channel.h",
17+
"include/flutter_desktop_embedding/binary_messenger.h",
18+
"include/flutter_desktop_embedding/engine_method_result.h",
19+
"include/flutter_desktop_embedding/json_message_codec.h",
20+
"include/flutter_desktop_embedding/json_method_codec.h",
21+
"include/flutter_desktop_embedding/message_codec.h",
22+
"include/flutter_desktop_embedding/method_call.h",
23+
"include/flutter_desktop_embedding/method_channel.h",
24+
"include/flutter_desktop_embedding/method_codec.h",
25+
"include/flutter_desktop_embedding/method_result.h",
26+
"include/flutter_desktop_embedding/plugin_registrar.h",
27+
]
28+
29+
# TODO: Re-evaluate this once more codecs are supported, since most libraries
30+
# won't need all the codecs.
31+
_wrapper_sources = [
32+
"src/engine_method_result.cc",
33+
"src/json_message_codec.cc",
34+
"src/json_method_codec.cc",
35+
"src/plugin_handler.cc",
36+
"src/plugin_handler.h",
37+
]
38+
39+
_wrapper_publish_dir = "$root_out_dir/fde_cpp_wrapper"
40+
41+
# Template for building the wrapper code in two different modes.
42+
# This is needed because the implementation of the embedding library
43+
# uses this code internally, in addition to it being published for
44+
# client use, but the internal version needs to set the flag that
45+
# controls the export macro.
46+
template("client_wrapper") {
47+
template_target_name = target_name
48+
49+
source_set(template_target_name) {
50+
forward_variables_from(invoker, "*")
51+
}
52+
53+
source_set("${template_target_name}_internal") {
54+
forward_variables_from(invoker, "*")
55+
56+
defines += [ "FLUTTER_DESKTOP_EMBEDDING_IMPL" ]
57+
}
58+
}
59+
60+
# A static library version of the client wrapper, for use both within the
61+
# framework implementation itself, as well as with the plugin builds.
62+
client_wrapper("client_wrapper") {
63+
sources = _wrapper_sources
64+
public = _wrapper_includes
65+
66+
# Use shared library settings since this target will be folded into shared
67+
# libraries.
68+
configs = default_dynamic_configs
69+
70+
if (is_linux) {
71+
configs += [ "//build/linux/config:jsoncpp" ]
72+
}
73+
if (is_win) {
74+
deps = [
75+
"//third_party/jsoncpp:jsoncpp",
76+
]
77+
}
78+
79+
public_configs = [ ":_relative_headers" ]
80+
81+
# Since these sources are for client use, they are written assuming they will
82+
# consume the embedder as a framework.
83+
include_dirs = [ "//library/include/" ]
84+
85+
# Explicitly disable flattened headers, since this is being built in-tree.
86+
# TODO: Simplify down to one define once Windows is using GN.
87+
defines = [
88+
"USE_FLATTENED_INCLUDES=0",
89+
"USE_FDE_TREE_PATHS",
90+
]
91+
}
92+
93+
# Since this code is intended to be used by clients in a source bundle, the
94+
# include paths use library-relative header paths rather than project-relative.
95+
config("_relative_headers") {
96+
include_dirs = [ "include" ]
97+
}
98+
99+
copy("_publish_includes") {
100+
sources = _wrapper_includes
101+
if (use_glfw) {
102+
sources +=
103+
[ "include/flutter_desktop_embedding/glfw/flutter_window_controller.h" ]
104+
}
105+
output_dir = "$_wrapper_publish_dir/include/flutter_desktop_embedding"
106+
outputs = [
107+
"$output_dir/{{source_file_part}}",
108+
]
109+
}
110+
111+
copy("_publish_sources") {
112+
sources = _wrapper_sources + [ "README" ]
113+
114+
if (use_glfw) {
115+
sources += [ "src/glfw/flutter_window_controller.cc" ]
116+
}
117+
output_dir = "$_wrapper_publish_dir"
118+
outputs = [
119+
"$output_dir/{{source_file_part}}",
120+
]
121+
}
122+
123+
group("publish_wrapper") {
124+
deps = [
125+
":_publish_includes",
126+
":_publish_sources",
127+
]
128+
}

0 commit comments

Comments
 (0)