From 5c8b98b847075467f8ed75bbe9be99a77ae86389 Mon Sep 17 00:00:00 2001 From: Kevin Worth Date: Sat, 16 May 2020 06:20:46 -0400 Subject: [PATCH] Add all linux files generated with flutter create . Accordingly with this documentation: https://flutter.dev/desktop#add-desktop-support-to-an-existing-flutter-project This commit adds all of the files in the `linux` directory which came from running `flutter create .` while on the master branch of flutter-sdk. --- flutterempires/linux/.gitignore | 1 + flutterempires/linux/Makefile | 146 ++++++++++++++++++ flutterempires/linux/app_configuration.mk | 16 ++ .../linux/flutter/.template_version | 1 + .../flutter/generated_plugin_registrant.cc | 9 ++ .../flutter/generated_plugin_registrant.h | 13 ++ .../linux/flutter/generated_plugins.mk | 25 +++ flutterempires/linux/main.cc | 67 ++++++++ flutterempires/linux/window_configuration.cc | 5 + flutterempires/linux/window_configuration.h | 15 ++ 10 files changed, 298 insertions(+) create mode 100644 flutterempires/linux/.gitignore create mode 100644 flutterempires/linux/Makefile create mode 100644 flutterempires/linux/app_configuration.mk create mode 100644 flutterempires/linux/flutter/.template_version create mode 100644 flutterempires/linux/flutter/generated_plugin_registrant.cc create mode 100644 flutterempires/linux/flutter/generated_plugin_registrant.h create mode 100644 flutterempires/linux/flutter/generated_plugins.mk create mode 100644 flutterempires/linux/main.cc create mode 100644 flutterempires/linux/window_configuration.cc create mode 100644 flutterempires/linux/window_configuration.h diff --git a/flutterempires/linux/.gitignore b/flutterempires/linux/.gitignore new file mode 100644 index 0000000..d3896c9 --- /dev/null +++ b/flutterempires/linux/.gitignore @@ -0,0 +1 @@ +flutter/ephemeral diff --git a/flutterempires/linux/Makefile b/flutterempires/linux/Makefile new file mode 100644 index 0000000..c27a1ad --- /dev/null +++ b/flutterempires/linux/Makefile @@ -0,0 +1,146 @@ +include app_configuration.mk + +# Default build type. +BUILD=debug + +FLUTTER_MANAGED_DIR=flutter +FLUTTER_EPHEMERAL_DIR=$(FLUTTER_MANAGED_DIR)/ephemeral + +# Configuration provided via flutter tool. +FLUTTER_CONFIG_FILE=$(FLUTTER_EPHEMERAL_DIR)/generated_config.mk +include $(FLUTTER_CONFIG_FILE) + +# Dependency locations +FLUTTER_APP_DIR=$(CURDIR)/.. +FLUTTER_APP_BUILD_DIR=$(FLUTTER_APP_DIR)/build + +OUT_DIR=$(FLUTTER_APP_BUILD_DIR)/linux +OBJ_DIR=$(OUT_DIR)/obj/$(BUILD) + +# Libraries +FLUTTER_LIB_NAME=flutter_linux_glfw +FLUTTER_LIB=$(FLUTTER_EPHEMERAL_DIR)/lib$(FLUTTER_LIB_NAME).so + +# Tools +FLUTTER_BIN=$(FLUTTER_ROOT)/bin/flutter +LINUX_BUILD=$(FLUTTER_ROOT)/packages/flutter_tools/bin/tool_backend.sh + +# Resources +ICU_DATA_NAME=icudtl.dat +ICU_DATA_SOURCE=$(FLUTTER_EPHEMERAL_DIR)/$(ICU_DATA_NAME) +FLUTTER_ASSETS_NAME=flutter_assets +FLUTTER_ASSETS_SOURCE=$(FLUTTER_APP_BUILD_DIR)/$(FLUTTER_ASSETS_NAME) + +# Bundle structure +BUNDLE_OUT_DIR=$(OUT_DIR)/$(BUILD) +BUNDLE_DATA_DIR=$(BUNDLE_OUT_DIR)/data +BUNDLE_LIB_DIR=$(BUNDLE_OUT_DIR)/lib + +BIN_OUT=$(BUNDLE_OUT_DIR)/$(BINARY_NAME) +ICU_DATA_OUT=$(BUNDLE_DATA_DIR)/$(ICU_DATA_NAME) +FLUTTER_LIB_OUT=$(BUNDLE_LIB_DIR)/$(notdir $(FLUTTER_LIB)) +ALL_LIBS_OUT=$(FLUTTER_LIB_OUT) \ + $(foreach lib,$(EXTRA_BUNDLED_LIBRARIES),$(BUNDLE_LIB_DIR)/$(notdir $(lib))) + +# Add relevant code from the wrapper library, which is intended to be statically +# built into the client. +# Use abspath for the wrapper root, which can contain relative paths; the +# intermediate build files will be based on the source path, which will cause +# issues if they start with one or more '../'s. +WRAPPER_ROOT=$(abspath $(FLUTTER_EPHEMERAL_DIR)/cpp_client_wrapper_glfw) +WRAPPER_SOURCES= \ + $(WRAPPER_ROOT)/flutter_window_controller.cc \ + $(WRAPPER_ROOT)/plugin_registrar.cc \ + $(WRAPPER_ROOT)/engine_method_result.cc + +# Use abspath for extra sources, which may also contain relative paths (see +# note above about WRAPPER_ROOT). +SOURCES=main.cc window_configuration.cc \ + flutter/generated_plugin_registrant.cc \ + $(WRAPPER_SOURCES) $(abspath $(EXTRA_SOURCES)) + +# Headers +WRAPPER_INCLUDE_DIR=$(WRAPPER_ROOT)/include +INCLUDE_DIRS=$(FLUTTER_EPHEMERAL_DIR) $(WRAPPER_INCLUDE_DIR) + +# Build settings +ifneq ($(strip $(SYSTEM_LIBRARIES)),) +EXTRA_CPPFLAGS+=$(patsubst -I%,-isystem%,$(shell pkg-config --cflags $(SYSTEM_LIBRARIES))) +EXTRA_LDFLAGS+=$(shell pkg-config --libs $(SYSTEM_LIBRARIES)) +endif +CXX=clang++ +CPPFLAGS.release=-DNDEBUG +CPPFLAGS.profile=$(CPPFLAGS.release) +CXXFLAGS.release=-O2 +CXXFLAGS.profile=$(CXXFLAGS.release) +CXXFLAGS=-std=c++14 -Wall -Werror $(CXXFLAGS.$(BUILD)) $(EXTRA_CXXFLAGS) +CPPFLAGS=$(patsubst %,-I%,$(INCLUDE_DIRS)) \ + $(CPPFLAGS.$(BUILD)) $(EXTRA_CPPFLAGS) +LDFLAGS=-L$(BUNDLE_LIB_DIR) \ + -l$(FLUTTER_LIB_NAME) \ + $(EXTRA_LDFLAGS) \ + -Wl,-rpath=\$$ORIGIN/lib + +# Intermediate files. +OBJ_FILES=$(SOURCES:%.cc=$(OBJ_DIR)/%.o) +DEPENDENCY_FILES=$(OBJ_FILES:%.o=%.d) + +# Targets + +.PHONY: all +all: $(BIN_OUT) bundle + +# Add the plugin targets, and their associated settings. +include $(FLUTTER_MANAGED_DIR)/generated_plugins.mk +EXTRA_BUNDLED_LIBRARIES+=$(PLUGIN_LIBRARIES) +EXTRA_LDFLAGS+=$(PLUGIN_LDFLAGS) +EXTRA_CPPFLAGS+=$(PLUGIN_CPPFLAGS) + +# This is a phony target because the flutter tool cannot describe +# its inputs and outputs yet. +.PHONY: sync +sync: $(FLUTTER_CONFIG_FILE) + $(LINUX_BUILD) linux-x64 $(BUILD) + +.PHONY: bundle +bundle: $(ICU_DATA_OUT) $(ALL_LIBS_OUT) bundleflutterassets + +$(BIN_OUT): $(OBJ_FILES) $(ALL_LIBS_OUT) + mkdir -p $(@D) + $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(OBJ_FILES) $(LDFLAGS) -o $@ + +$(WRAPPER_SOURCES) $(FLUTTER_LIB) $(ICU_DATA_SOURCE) $(FLUTTER_ASSETS_SOURCE) \ + $(PLUGIN_TARGETS): | sync + +# Plugin library bundling pattern. +$(BUNDLE_LIB_DIR)/%: $(OUT_DIR)/% + mkdir -p $(BUNDLE_LIB_DIR) + cp $< $@ + +$(FLUTTER_LIB_OUT): $(FLUTTER_LIB) + mkdir -p $(@D) + cp $< $@ + +$(ICU_DATA_OUT): $(ICU_DATA_SOURCE) + mkdir -p $(@D) + cp $< $@ + +-include $(DEPENDENCY_FILES) + +$(OBJ_DIR)/%.o : %.cc | sync + mkdir -p $(@D) + $(CXX) $(CXXFLAGS) $(CPPFLAGS) -MMD -c $< -o $@ + +# Fully re-copy the assets directory on each build to avoid having to keep a +# comprehensive list of all asset files here, which would be fragile to changes +# in other files (e.g., adding a new font to pubspec.yaml). +.PHONY: bundleflutterassets +bundleflutterassets: $(FLUTTER_ASSETS_SOURCE) + mkdir -p $(BUNDLE_DATA_DIR) + rsync -rpu --delete $(FLUTTER_ASSETS_SOURCE) $(BUNDLE_DATA_DIR) + +.PHONY: clean +clean: + rm -rf $(OUT_DIR); \ + cd $(FLUTTER_APP_DIR); \ + $(FLUTTER_BIN) clean diff --git a/flutterempires/linux/app_configuration.mk b/flutterempires/linux/app_configuration.mk new file mode 100644 index 0000000..73b2525 --- /dev/null +++ b/flutterempires/linux/app_configuration.mk @@ -0,0 +1,16 @@ +# This file contains variables that applications are likely to need to +# change, to isolate them from the main Makefile where the build rules are still +# in flux. This should simplify re-creating the runner while preserving local +# changes. + +# Executable name. +BINARY_NAME=flutterempires +# Any extra source files to build. +EXTRA_SOURCES= +# Paths of any additional libraries to be bundled in the output directory. +EXTRA_BUNDLED_LIBRARIES= +# Extra flags (e.g., for library dependencies). +SYSTEM_LIBRARIES= +EXTRA_CXXFLAGS= +EXTRA_CPPFLAGS= +EXTRA_LDFLAGS= \ No newline at end of file diff --git a/flutterempires/linux/flutter/.template_version b/flutterempires/linux/flutter/.template_version new file mode 100644 index 0000000..56a6051 --- /dev/null +++ b/flutterempires/linux/flutter/.template_version @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/flutterempires/linux/flutter/generated_plugin_registrant.cc b/flutterempires/linux/flutter/generated_plugin_registrant.cc new file mode 100644 index 0000000..4bfa0f3 --- /dev/null +++ b/flutterempires/linux/flutter/generated_plugin_registrant.cc @@ -0,0 +1,9 @@ +// +// Generated file. Do not edit. +// + +#include "generated_plugin_registrant.h" + + +void RegisterPlugins(flutter::PluginRegistry* registry) { +} diff --git a/flutterempires/linux/flutter/generated_plugin_registrant.h b/flutterempires/linux/flutter/generated_plugin_registrant.h new file mode 100644 index 0000000..9846246 --- /dev/null +++ b/flutterempires/linux/flutter/generated_plugin_registrant.h @@ -0,0 +1,13 @@ +// +// Generated file. Do not edit. +// + +#ifndef GENERATED_PLUGIN_REGISTRANT_ +#define GENERATED_PLUGIN_REGISTRANT_ + +#include + +// Registers Flutter plugins. +void RegisterPlugins(flutter::PluginRegistry* registry); + +#endif // GENERATED_PLUGIN_REGISTRANT_ diff --git a/flutterempires/linux/flutter/generated_plugins.mk b/flutterempires/linux/flutter/generated_plugins.mk new file mode 100644 index 0000000..63a60db --- /dev/null +++ b/flutterempires/linux/flutter/generated_plugins.mk @@ -0,0 +1,25 @@ +# Plugins to include in the build. +GENERATED_PLUGINS=\ + +GENERATED_PLUGINS_DIR=flutter/ephemeral/.plugin_symlinks +# A plugin library name plugin name with _plugin appended. +GENERATED_PLUGIN_LIB_NAMES=$(foreach plugin,$(GENERATED_PLUGINS),$(plugin)_plugin) + +# Variables for use in the enclosing Makefile. Changes to these names are +# breaking changes. +PLUGIN_TARGETS=$(GENERATED_PLUGINS) +PLUGIN_LIBRARIES=$(foreach plugin,$(GENERATED_PLUGIN_LIB_NAMES),\ + $(OUT_DIR)/lib$(plugin).so) +PLUGIN_LDFLAGS=$(patsubst %,-l%,$(GENERATED_PLUGIN_LIB_NAMES)) +PLUGIN_CPPFLAGS=$(foreach plugin,$(GENERATED_PLUGINS),\ + -I$(GENERATED_PLUGINS_DIR)/$(plugin)/linux) + +# Targets + +# Implicit rules don't match phony targets, so list plugin builds explicitly. + +.PHONY: $(GENERATED_PLUGINS) +$(GENERATED_PLUGINS): + make -C $(GENERATED_PLUGINS_DIR)/$@/linux \ + OUT_DIR=$(OUT_DIR) \ + FLUTTER_EPHEMERAL_DIR="$(abspath flutter/ephemeral)" diff --git a/flutterempires/linux/main.cc b/flutterempires/linux/main.cc new file mode 100644 index 0000000..47b0343 --- /dev/null +++ b/flutterempires/linux/main.cc @@ -0,0 +1,67 @@ +#include +#include +#include + +#include +#include +#include +#include + +#include "flutter/generated_plugin_registrant.h" +#include "window_configuration.h" + +namespace { + +// Returns the path of the directory containing this executable, or an empty +// string if the directory cannot be found. +std::string GetExecutableDirectory() { + char buffer[PATH_MAX + 1]; + ssize_t length = readlink("/proc/self/exe", buffer, sizeof(buffer)); + if (length > PATH_MAX) { + std::cerr << "Couldn't locate executable" << std::endl; + return ""; + } + std::string executable_path(buffer, length); + size_t last_separator_position = executable_path.find_last_of('/'); + if (last_separator_position == std::string::npos) { + std::cerr << "Unabled to find parent directory of " << executable_path + << std::endl; + return ""; + } + return executable_path.substr(0, last_separator_position); +} + +} // namespace + +int main(int argc, char **argv) { + // Resources are located relative to the executable. + std::string base_directory = GetExecutableDirectory(); + if (base_directory.empty()) { + base_directory = "."; + } + std::string data_directory = base_directory + "/data"; + std::string assets_path = data_directory + "/flutter_assets"; + std::string icu_data_path = data_directory + "/icudtl.dat"; + + // Arguments for the Flutter Engine. + std::vector arguments; + + flutter::FlutterWindowController flutter_controller(icu_data_path); + flutter::WindowProperties window_properties = {}; + window_properties.title = kFlutterWindowTitle; + window_properties.width = kFlutterWindowWidth; + window_properties.height = kFlutterWindowHeight; + + // Start the engine. + if (!flutter_controller.CreateWindow(window_properties, assets_path, + arguments)) { + return EXIT_FAILURE; + } + RegisterPlugins(&flutter_controller); + + // Run until the window is closed. + while (flutter_controller.RunEventLoopWithTimeout( + std::chrono::milliseconds::max())) { + } + return EXIT_SUCCESS; +} diff --git a/flutterempires/linux/window_configuration.cc b/flutterempires/linux/window_configuration.cc new file mode 100644 index 0000000..057c3c0 --- /dev/null +++ b/flutterempires/linux/window_configuration.cc @@ -0,0 +1,5 @@ +#include "window_configuration.h" + +const char *kFlutterWindowTitle = "flutterempires"; +const unsigned int kFlutterWindowWidth = 800; +const unsigned int kFlutterWindowHeight = 600; diff --git a/flutterempires/linux/window_configuration.h b/flutterempires/linux/window_configuration.h new file mode 100644 index 0000000..5de7011 --- /dev/null +++ b/flutterempires/linux/window_configuration.h @@ -0,0 +1,15 @@ +#ifndef WINDOW_CONFIGURATION_ +#define WINDOW_CONFIGURATION_ + +// This is a temporary approach to isolate common customizations from main.cpp, +// where the APIs are still in flux. This should simplify re-creating the +// runner while preserving local changes. +// +// Longer term there should be simpler configuration options for common +// customizations like this, without requiring native code changes. + +extern const char *kFlutterWindowTitle; +extern const unsigned int kFlutterWindowWidth; +extern const unsigned int kFlutterWindowHeight; + +#endif // WINDOW_CONFIGURATION_ -- 2.43.0