From d119d62c1d0b5341056f2a7fd246f57b76ae1a3b Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Thu, 23 Mar 2017 15:20:52 -0700 Subject: [PATCH] Add fml::paths::GetExecutableDirectoryPath. (#3508) --- fml/BUILD.gn | 4 ++++ fml/icu_util.cc | 12 +++++++++++- fml/paths.h | 19 +++++++++++++++++++ fml/platform/android/paths_android.cc | 15 +++++++++++++++ fml/platform/darwin/paths_darwin.mm | 20 ++++++++++++++++++++ fml/platform/linux/paths_linux.cc | 25 +++++++++++++++++++++++++ travis/licenses_golden/licenses_flutter | 4 ++++ 7 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 fml/paths.h create mode 100644 fml/platform/android/paths_android.cc create mode 100644 fml/platform/darwin/paths_darwin.mm create mode 100644 fml/platform/linux/paths_linux.cc diff --git a/fml/BUILD.gn b/fml/BUILD.gn index 221bd13c3..285fdd6e3 100644 --- a/fml/BUILD.gn +++ b/fml/BUILD.gn @@ -12,6 +12,7 @@ source_set("fml") { "message_loop.h", "message_loop_impl.cc", "message_loop_impl.h", + "paths.h", "task_runner.cc", "task_runner.h", "thread.cc", @@ -43,6 +44,7 @@ source_set("fml") { "platform/darwin/message_loop_darwin.mm", "platform/darwin/nsstring_utils.h", "platform/darwin/nsstring_utils.mm", + "platform/darwin/paths_darwin.mm", "platform/darwin/resource_mapping_darwin.h", "platform/darwin/resource_mapping_darwin.mm", "platform/darwin/scoped_block.h", @@ -62,6 +64,7 @@ source_set("fml") { "platform/android/jni_weak_ref.h", "platform/android/message_loop_android.cc", "platform/android/message_loop_android.h", + "platform/android/paths_android.cc", "platform/android/scoped_java_ref.cc", "platform/android/scoped_java_ref.h", ] @@ -83,6 +86,7 @@ source_set("fml") { sources += [ "platform/linux/message_loop_linux.cc", "platform/linux/message_loop_linux.h", + "platform/linux/paths_linux.cc", "platform/linux/timerfd.cc", "platform/linux/timerfd.h", ] diff --git a/fml/icu_util.cc b/fml/icu_util.cc index d7737b6e8..c2964d6a4 100644 --- a/fml/icu_util.cc +++ b/fml/icu_util.cc @@ -8,6 +8,7 @@ #include #include "flutter/fml/mapping.h" +#include "flutter/fml/paths.h" #include "lib/ftl/build_config.h" #include "lib/ftl/logging.h" #include "third_party/icu/source/common/unicode/udata.h" @@ -44,7 +45,16 @@ class ICUContext { // Check if the mapping can by directly accessed via a file path. In this // case, the data file needs to be next to the executable. - auto file = std::make_unique(kIcuDataFileName); + auto directory = fml::paths::GetExecutableDirectoryPath(); + + if (!directory.first) { + return false; + } + + // FIXME(chinmaygarde): There is no Path::Join in FTL. So a non-portable + // version is used here. Patch FTL and update. + auto file = std::make_unique(directory.second + "/" + + kIcuDataFileName); if (file->GetSize() != 0) { mapping_ = std::move(file); return true; diff --git a/fml/paths.h b/fml/paths.h new file mode 100644 index 000000000..8ec3b4b58 --- /dev/null +++ b/fml/paths.h @@ -0,0 +1,19 @@ +// Copyright 2017 The Chromium 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_FML_PATHS_H_ +#define FLUTTER_FML_PATHS_H_ + +#include +#include + +namespace fml { +namespace paths { + +std::pair GetExecutableDirectoryPath(); + +} // namespace paths +} // namespace fml + +#endif // FLUTTER_FML_PATHS_H_ diff --git a/fml/platform/android/paths_android.cc b/fml/platform/android/paths_android.cc new file mode 100644 index 000000000..535bab4c0 --- /dev/null +++ b/fml/platform/android/paths_android.cc @@ -0,0 +1,15 @@ +// Copyright 2017 The Chromium 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/fml/paths.h" + +namespace fml { +namespace paths { + +std::pair GetExecutableDirectoryPath() { + return {false, ""}; +} + +} // namespace paths +} // namespace fml diff --git a/fml/platform/darwin/paths_darwin.mm b/fml/platform/darwin/paths_darwin.mm new file mode 100644 index 000000000..de8c9102f --- /dev/null +++ b/fml/platform/darwin/paths_darwin.mm @@ -0,0 +1,20 @@ +// Copyright 2017 The Chromium 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/fml/paths.h" + +#include + +#include "lib/ftl/files/path.h" + +namespace fml { +namespace paths { + +std::pair GetExecutableDirectoryPath() { + return {true, files::GetDirectoryName( + [NSBundle mainBundle].executablePath.UTF8String)}; +} + +} // namespace paths +} // namespace fml diff --git a/fml/platform/linux/paths_linux.cc b/fml/platform/linux/paths_linux.cc new file mode 100644 index 000000000..4ee3f9aa5 --- /dev/null +++ b/fml/platform/linux/paths_linux.cc @@ -0,0 +1,25 @@ +// Copyright 2017 The Chromium 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/fml/paths.h" + +#include + +#include "lib/ftl/files/path.h" + +namespace fml { +namespace paths { + +std::pair GetExecutableDirectoryPath() { + const int path_size = 255; + char path[path_size] = {0}; + auto read_size = ::readlink("/proc/self/exe", path, path_size); + if (read_size == -1) { + return {false, ""}; + } + return {true, files::GetDirectoryName(std::string{path, read_size})}; +} + +} // namespace paths +} // namespace fml diff --git a/travis/licenses_golden/licenses_flutter b/travis/licenses_golden/licenses_flutter index fa6546151..7b6504194 100644 --- a/travis/licenses_golden/licenses_flutter +++ b/travis/licenses_golden/licenses_flutter @@ -1913,12 +1913,14 @@ FILE: ../../../flutter/fml/message_loop.h FILE: ../../../flutter/fml/message_loop_impl.cc FILE: ../../../flutter/fml/message_loop_impl.h FILE: ../../../flutter/fml/message_loop_unittests.cc +FILE: ../../../flutter/fml/paths.h FILE: ../../../flutter/fml/platform/android/jni_util.cc FILE: ../../../flutter/fml/platform/android/jni_util.h FILE: ../../../flutter/fml/platform/android/jni_weak_ref.cc FILE: ../../../flutter/fml/platform/android/jni_weak_ref.h FILE: ../../../flutter/fml/platform/android/message_loop_android.cc FILE: ../../../flutter/fml/platform/android/message_loop_android.h +FILE: ../../../flutter/fml/platform/android/paths_android.cc FILE: ../../../flutter/fml/platform/android/scoped_java_ref.cc FILE: ../../../flutter/fml/platform/android/scoped_java_ref.h FILE: ../../../flutter/fml/platform/darwin/cf_utils.cc @@ -1927,6 +1929,7 @@ FILE: ../../../flutter/fml/platform/darwin/message_loop_darwin.h FILE: ../../../flutter/fml/platform/darwin/message_loop_darwin.mm FILE: ../../../flutter/fml/platform/darwin/nsstring_utils.h FILE: ../../../flutter/fml/platform/darwin/nsstring_utils.mm +FILE: ../../../flutter/fml/platform/darwin/paths_darwin.mm FILE: ../../../flutter/fml/platform/darwin/resource_mapping_darwin.h FILE: ../../../flutter/fml/platform/darwin/resource_mapping_darwin.mm FILE: ../../../flutter/fml/platform/darwin/scoped_block.mm @@ -1934,6 +1937,7 @@ FILE: ../../../flutter/fml/platform/darwin/scoped_nsobject.h FILE: ../../../flutter/fml/platform/darwin/scoped_nsobject.mm FILE: ../../../flutter/fml/platform/linux/message_loop_linux.cc FILE: ../../../flutter/fml/platform/linux/message_loop_linux.h +FILE: ../../../flutter/fml/platform/linux/paths_linux.cc FILE: ../../../flutter/fml/platform/linux/timerfd.cc FILE: ../../../flutter/fml/platform/linux/timerfd.h FILE: ../../../flutter/fml/task_runner.cc -- GitLab