From 8c89674d9339884d4bfcfe01a597cc64aebed54f Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Tue, 17 Mar 2020 19:04:18 -0700 Subject: [PATCH] Allow relative resource paths in GLFW embedding (flutter/engine#16944) Currently every Linux runner has this code to allow relative resource paths; this moves it into the framework so that any embedder can get this behavior without that code needing to be in the template. Rolls buildroot to pick up std::filesystem support in our libc++ --- DEPS | 2 +- engine/src/flutter/BUILD.gn | 1 + .../ci/licenses_golden/licenses_flutter | 3 ++ .../shell/platform/common/cpp/BUILD.gn | 41 +++++++++++++++++++ .../shell/platform/common/cpp/path_utils.cc | 37 +++++++++++++++++ .../shell/platform/common/cpp/path_utils.h | 18 ++++++++ .../common/cpp/path_utils_unittests.cc | 24 +++++++++++ .../shell/platform/glfw/flutter_glfw.cc | 25 ++++++++++- .../shell/platform/glfw/public/flutter_glfw.h | 4 ++ engine/src/flutter/testing/run_tests.py | 2 + 10 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 engine/src/flutter/shell/platform/common/cpp/path_utils.cc create mode 100644 engine/src/flutter/shell/platform/common/cpp/path_utils.h create mode 100644 engine/src/flutter/shell/platform/common/cpp/path_utils_unittests.cc diff --git a/DEPS b/DEPS index d92c34fcd3..dda1cc60fc 100644 --- a/DEPS +++ b/DEPS @@ -137,7 +137,7 @@ allowed_hosts = [ ] deps = { - 'src': 'https://github.com/flutter/buildroot.git' + '@' + 'd14f3c708fb132381f7053b4ee9e628be915ed96', + 'src': 'https://github.com/flutter/buildroot.git' + '@' + '4489a78bf94bb9d8ea6a127106f85b7710d57f70', # Fuchsia compatibility # diff --git a/engine/src/flutter/BUILD.gn b/engine/src/flutter/BUILD.gn index 25006866ca..c62b1e9610 100644 --- a/engine/src/flutter/BUILD.gn +++ b/engine/src/flutter/BUILD.gn @@ -75,6 +75,7 @@ group("flutter") { "//flutter/lib/ui:ui_unittests", "//flutter/runtime:runtime_unittests", "//flutter/shell/common:shell_unittests", + "//flutter/shell/platform/common/cpp:common_cpp_core_unittests", "//flutter/shell/platform/common/cpp/client_wrapper:client_wrapper_unittests", "//flutter/shell/platform/embedder:embedder_unittests", "//flutter/shell/platform/glfw/client_wrapper:client_wrapper_glfw_unittests", diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 21bb859d59..7a06e1efa0 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -798,6 +798,9 @@ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/standard_message FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/standard_method_codec_unittests.cc FILE: ../../../flutter/shell/platform/common/cpp/incoming_message_dispatcher.cc FILE: ../../../flutter/shell/platform/common/cpp/incoming_message_dispatcher.h +FILE: ../../../flutter/shell/platform/common/cpp/path_utils.cc +FILE: ../../../flutter/shell/platform/common/cpp/path_utils.h +FILE: ../../../flutter/shell/platform/common/cpp/path_utils_unittests.cc FILE: ../../../flutter/shell/platform/common/cpp/public/flutter_export.h FILE: ../../../flutter/shell/platform/common/cpp/public/flutter_messenger.h FILE: ../../../flutter/shell/platform/common/cpp/public/flutter_plugin_registrar.h diff --git a/engine/src/flutter/shell/platform/common/cpp/BUILD.gn b/engine/src/flutter/shell/platform/common/cpp/BUILD.gn index b4d5b0a5bf..00db206ed2 100644 --- a/engine/src/flutter/shell/platform/common/cpp/BUILD.gn +++ b/engine/src/flutter/shell/platform/common/cpp/BUILD.gn @@ -2,6 +2,8 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import("//flutter/testing/testing.gni") + config("desktop_library_implementation") { defines = [ "FLUTTER_DESKTOP_LIBRARY" ] } @@ -49,6 +51,10 @@ source_set("common_cpp") { "//flutter/shell/platform/embedder:embedder_with_symbol_prefix", ] + public_deps = [ + ":common_cpp_core", + ] + # TODO: Remove once text input model refactor lands, at which point this code # won't have a JSON dependency. defines = [ "USE_RAPID_JSON" ] @@ -60,6 +66,41 @@ source_set("common_cpp") { } } +# The portion of common_cpp that has no dependencies on the public/ +# headers. This division should be revisited once the Linux GTK +# embedding is futher along and it's clearer how much, if any, shared +# API surface there will be. +source_set("common_cpp_core") { + public = [ + "path_utils.h", + ] + + sources = [ + "path_utils.cc", + ] +} + +test_fixtures("common_cpp_core_fixtures") { + fixtures = [] +} + +executable("common_cpp_core_unittests") { + testonly = true + + sources = [ + "path_utils_unittests.cc", + ] + + deps = [ + ":common_cpp_core", + ":common_cpp_core_fixtures", + "//flutter/testing", + "//third_party/dart/runtime:libdart_jit", + ] + + public_configs = [ "//flutter:config" ] +} + copy("publish_headers") { sources = _public_headers outputs = [ diff --git a/engine/src/flutter/shell/platform/common/cpp/path_utils.cc b/engine/src/flutter/shell/platform/common/cpp/path_utils.cc new file mode 100644 index 0000000000..6da31077a7 --- /dev/null +++ b/engine/src/flutter/shell/platform/common/cpp/path_utils.cc @@ -0,0 +1,37 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/common/cpp/path_utils.h" + +#if defined(_WIN32) +#include +#elif defined(__linux__) +#include +#include +#endif + +namespace flutter { + +std::filesystem::path GetExecutableDirectory() { +#if defined(_WIN32) + wchar_t buffer[MAX_PATH]; + if (GetModuleFileName(nullptr, buffer, MAX_PATH) == 0) { + return std::filesystem::path(); + } + std::filesystem::path executable_path(buffer); + return executable_path.remove_filename(); +#elif defined(__linux__) + char buffer[PATH_MAX + 1]; + ssize_t length = readlink("/proc/self/exe", buffer, sizeof(buffer)); + if (length > PATH_MAX) { + return std::filesystem::path(); + } + std::filesystem::path executable_path(std::string(buffer, length)); + return executable_path.remove_filename(); +#else + return std::filesystem::path(); +#endif +} + +} // namespace flutter diff --git a/engine/src/flutter/shell/platform/common/cpp/path_utils.h b/engine/src/flutter/shell/platform/common/cpp/path_utils.h new file mode 100644 index 0000000000..62cd3fd840 --- /dev/null +++ b/engine/src/flutter/shell/platform/common/cpp/path_utils.h @@ -0,0 +1,18 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_GLFW_PATH_UTILS_H_ +#define FLUTTER_SHELL_PLATFORM_GLFW_PATH_UTILS_H_ + +#include + +namespace flutter { + +// Returns the path of the directory containing this executable, or an empty +// path if the directory cannot be found. +std::filesystem::path GetExecutableDirectory(); + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_GLFW_PATH_UTILS_H_ diff --git a/engine/src/flutter/shell/platform/common/cpp/path_utils_unittests.cc b/engine/src/flutter/shell/platform/common/cpp/path_utils_unittests.cc new file mode 100644 index 0000000000..2044d34662 --- /dev/null +++ b/engine/src/flutter/shell/platform/common/cpp/path_utils_unittests.cc @@ -0,0 +1,24 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/common/cpp/path_utils.h" + +#include "gtest/gtest.h" + +namespace flutter { + +// Tests that GetExecutableDirectory returns a valid, absolute path. +TEST(PathUtilsTest, ExecutableDirector) { + std::filesystem::path exe_directory = GetExecutableDirectory(); +#if defined(__linux__) || defined(_WIN32) + EXPECT_EQ(exe_directory.empty(), false); + EXPECT_EQ(exe_directory.is_absolute(), true); +#else + // On platforms where it's not implemented, it should indicate that + // by returning an empty path. + EXPECT_EQ(exe_directory.empty(), true); +#endif +} + +} // namespace flutter diff --git a/engine/src/flutter/shell/platform/glfw/flutter_glfw.cc b/engine/src/flutter/shell/platform/glfw/flutter_glfw.cc index da1176f4f0..f79c360fa8 100644 --- a/engine/src/flutter/shell/platform/glfw/flutter_glfw.cc +++ b/engine/src/flutter/shell/platform/glfw/flutter_glfw.cc @@ -10,10 +10,12 @@ #include #include #include +#include #include #include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/plugin_registrar.h" #include "flutter/shell/platform/common/cpp/incoming_message_dispatcher.h" +#include "flutter/shell/platform/common/cpp/path_utils.h" #include "flutter/shell/platform/embedder/embedder.h" #include "flutter/shell/platform/glfw/glfw_event_loop.h" #include "flutter/shell/platform/glfw/key_event_handler.h" @@ -507,6 +509,25 @@ static FLUTTER_API_SYMBOL(FlutterEngine) &engine_properties.switches[engine_properties.switches_count]); } + std::filesystem::path assets_path = + std::filesystem::u8path(engine_properties.assets_path); + std::filesystem::path icu_path = + std::filesystem::u8path(engine_properties.icu_data_path); + if (assets_path.is_relative() || icu_path.is_relative()) { + // Treat relative paths as relative to the directory of this executable. + std::filesystem::path executable_location = + flutter::GetExecutableDirectory(); + if (executable_location.empty()) { + std::cerr << "Unable to find executable location to resolve paths." + << std::endl; + return nullptr; + } + assets_path = std::filesystem::path(executable_location) / assets_path; + icu_path = std::filesystem::path(executable_location) / icu_path; + } + std::string assets_path_string = assets_path.u8string(); + std::string icu_path_string = assets_path.u8string(); + FlutterRendererConfig config = {}; if (window == nullptr) { config.type = kOpenGL; @@ -528,8 +549,8 @@ static FLUTTER_API_SYMBOL(FlutterEngine) } FlutterProjectArgs args = {}; args.struct_size = sizeof(FlutterProjectArgs); - args.assets_path = engine_properties.assets_path; - args.icu_data_path = engine_properties.icu_data_path; + args.assets_path = assets_path_string.c_str(); + args.icu_data_path = icu_path_string.c_str(); args.command_line_argc = static_cast(argv.size()); args.command_line_argv = &argv[0]; args.platform_message_callback = GLFWOnFlutterPlatformMessage; diff --git a/engine/src/flutter/shell/platform/glfw/public/flutter_glfw.h b/engine/src/flutter/shell/platform/glfw/public/flutter_glfw.h index 610c63ff79..f9921bf36c 100644 --- a/engine/src/flutter/shell/platform/glfw/public/flutter_glfw.h +++ b/engine/src/flutter/shell/platform/glfw/public/flutter_glfw.h @@ -38,8 +38,12 @@ typedef struct { // Properties for configuring a Flutter engine instance. typedef struct { // The path to the flutter_assets folder for the application to be run. + // This can either be an absolute path, or on Windows or Linux, a path + // relative to the directory containing the executable. const char* assets_path; // The path to the icudtl.dat file for the version of Flutter you are using. + // This can either be an absolute path, or on Windows or Linux, a path + // relative to the directory containing the executable. const char* icu_data_path; // The switches to pass to the Flutter engine. // diff --git a/engine/src/flutter/testing/run_tests.py b/engine/src/flutter/testing/run_tests.py index 658a982ff8..083117458d 100755 --- a/engine/src/flutter/testing/run_tests.py +++ b/engine/src/flutter/testing/run_tests.py @@ -108,6 +108,8 @@ def RunCCTests(build_dir, filter): RunEngineExecutable(build_dir, 'client_wrapper_glfw_unittests', filter, shuffle_flags) + RunEngineExecutable(build_dir, 'common_cpp_core_unittests', filter, shuffle_flags) + RunEngineExecutable(build_dir, 'client_wrapper_unittests', filter, shuffle_flags) # https://github.com/flutter/flutter/issues/36294