Skip to content

Commit

Permalink
Changes to assets/ build/ flow/ runtime/ and shell/ to allow for comp…
Browse files Browse the repository at this point in the history
…ilation on Windows (flutter#4407)

Made changes to assets/ build/ flow/ runtime/ and shell/ to allow for
compilation on Windows.
  • Loading branch information
bkonyi authored Dec 1, 2017
1 parent da43e7c commit 74a2d90
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 8 deletions.
4 changes: 4 additions & 0 deletions assets/directory_asset_bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
// found in the LICENSE file.

#include "flutter/assets/directory_asset_bundle.h"
#include "lib/fxl/build_config.h"

#include <fcntl.h>

#if !defined(OS_WIN)
#include <unistd.h>
#endif

#include <utility>

Expand Down
4 changes: 4 additions & 0 deletions assets/zip_asset_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
// found in the LICENSE file.

#include "flutter/assets/zip_asset_store.h"
#include "lib/fxl/build_config.h"

#include <fcntl.h>

#if !defined(OS_WIN)
#include <unistd.h>
#endif

#include <string>
#include <utility>
Expand Down
6 changes: 5 additions & 1 deletion build/dart/tools/dart_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ def list_files(from_root, filter_func=None):


def remove_broken_symlink(path):
if not USE_LINKS:
return
try:
link_path = os.readlink(path)
except OSError as e:
Expand All @@ -131,6 +133,8 @@ def remove_broken_symlink(path):


def remove_broken_symlinks(root_dir):
if not USE_LINKS:
return
for current_dir, _, child_files in os.walk(root_dir):
for filename in child_files:
path = os.path.join(current_dir, filename)
Expand Down Expand Up @@ -268,7 +272,7 @@ def main():

# Symlink packages/
package_path = os.path.join(args.package_root, args.package_name)
link(lib_path, package_path)
copy_or_link(lib_path, package_path)

# Link dart-pkg/$package/packages to dart-pkg/packages
link_if_possible(args.package_root, target_packages_dir)
Expand Down
7 changes: 7 additions & 0 deletions flow/matrix_decomposition_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "lib/fxl/build_config.h"

#if defined(OS_WIN)
#define _USE_MATH_DEFINES
#endif
#include <cmath>

#include "flutter/flow/matrix_decomposition.h"
#include "third_party/gtest/include/gtest/gtest.h"

Expand Down
41 changes: 40 additions & 1 deletion runtime/dart_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
// found in the LICENSE file.

#include "flutter/runtime/dart_controller.h"
#include "lib/fxl/build_config.h"

#if defined(OS_WIN)
#include <windows.h>
#undef GetCurrentDirectory
#endif

#include <utility>

Expand Down Expand Up @@ -33,6 +39,37 @@ using tonic::ToDart;

namespace blink {
namespace {
#if defined(OS_WIN)

std::string FindAndReplace(const std::string& str,
const std::string& findStr,
const std::string& replaceStr) {
std::string rStr = str;
size_t pos = 0;
while ((pos = rStr.find(findStr, pos)) != std::string::npos) {
rStr.replace(pos, findStr.length(), replaceStr);
pos += replaceStr.length();
}
return rStr;
}

std::string SanitizePath(const std::string& path) {
return FindAndReplace(path, "\\\\", "/");
}

std::string ResolvePath(std::string path) {
std::string sanitized = SanitizePath(path);
if ((sanitized.length() > 2) && (sanitized[1] == ':')) {
return sanitized;
}
return files::SimplifyPath(files::GetCurrentDirectory() + "/" + sanitized);
}

#else // defined(OS_WIN)

std::string SanitizePath(const std::string& path) {
return path;
}

// TODO(abarth): Consider adding this to //garnet/public/lib/fxl.
std::string ResolvePath(std::string path) {
Expand All @@ -41,6 +78,8 @@ std::string ResolvePath(std::string path) {
return files::SimplifyPath(files::GetCurrentDirectory() + "/" + path);
}

#endif

} // namespace

DartController::DartController() : ui_dart_state_(nullptr) {}
Expand Down Expand Up @@ -169,7 +208,7 @@ tonic::DartErrorHandleType DartController::RunFromSource(
tonic::FileLoader& loader = dart_state()->file_loader();
if (!packages.empty() && !loader.LoadPackagesMap(ResolvePath(packages)))
FXL_LOG(WARNING) << "Failed to load package map: " << packages;
Dart_Handle result = loader.LoadScript(main);
Dart_Handle result = loader.LoadScript(SanitizePath(main));
LogIfError(result);
error = tonic::GetErrorHandleType(result);
}
Expand Down
7 changes: 7 additions & 0 deletions runtime/dart_init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
// found in the LICENSE file.

#include "flutter/runtime/dart_init.h"
#include "flutter/sky/engine/wtf/OperatingSystem.h"

#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

#if defined(OS_WIN)
#include <windows.h>
#undef ERROR
#else
#include <unistd.h>
#endif

#include <memory>
#include <string>
Expand Down
94 changes: 90 additions & 4 deletions shell/common/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,28 @@

#include "flutter/shell/common/engine.h"

#if OS(WIN)
#include <io.h>
#include <windows.h>
#define access _access
#define R_OK 0x4

#ifndef S_ISDIR
#define S_ISDIR(mode) (((mode)&S_IFMT) == S_IFDIR)
#endif

#ifndef S_ISREG
#define S_ISREG(mode) (((mode)&S_IFMT) == S_IFREG)
#endif

#else
#include <dlfcn.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#endif // OS(WIN)

#include <fcntl.h>
#include <sys/stat.h>
#include <memory>
#include <utility>

Expand Down Expand Up @@ -46,6 +63,30 @@ constexpr char kNavigationChannel[] = "flutter/navigation";
constexpr char kLocalizationChannel[] = "flutter/localization";
constexpr char kSettingsChannel[] = "flutter/settings";

void FindAndReplaceInPlace(std::string& str,
const std::string& findStr,
const std::string& replaceStr) {
size_t pos = 0;
while ((pos = str.find(findStr, pos)) != std::string::npos) {
str.replace(pos, findStr.length(), replaceStr);
pos += replaceStr.length();
}
}

std::string SanitizePath(const std::string& path) {
#if OS(WIN)
std::string sanitized = path;
FindAndReplaceInPlace(sanitized, "\\\\", "/");
if ((sanitized.length() > 2) && (sanitized[1] == ':')) {
// Path begins with a drive letter.
sanitized = '/' + sanitized;
}
return sanitized;
#else
return path;
#endif
}

bool PathExists(const std::string& path) {
return access(path.c_str(), R_OK) == 0;
}
Expand All @@ -63,7 +104,7 @@ std::string FindPackagesPath(const std::string& main_dart) {
}

std::string GetScriptUriFromPath(const std::string& path) {
return "file://" + path;
return "file://" + SanitizePath(path);
}

} // namespace
Expand Down Expand Up @@ -93,6 +134,7 @@ fml::WeakPtr<Engine> Engine::GetWeakPtr() {
#if !FLUTTER_AOT
#elif OS(IOS)
#elif OS(ANDROID)
// TODO(bkonyi): do we even get here for Windows?
static const uint8_t* MemMapSnapshot(const std::string& aot_snapshot_path,
const std::string& default_file_name,
const std::string& settings_file_name,
Expand All @@ -104,6 +146,49 @@ static const uint8_t* MemMapSnapshot(const std::string& aot_snapshot_path,
asset_path = aot_snapshot_path + "/" + settings_file_name;
}

#if OS(WIN)
HANDLE file_handle_ =
CreateFileA(reinterpret_cast<LPCSTR>(path.c_str()), GENERIC_READ,
FILE_SHARE_READ, nullptr, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, nullptr);

if (file_handle_ == INVALID_HANDLE_VALUE) {
return;
}

size_ = GetFileSize(file_handle_, nullptr);
if (size_ == INVALID_FILE_SIZE) {
size_ = 0;
return;
}

int mapping_flags = executable ? PAGE_EXECUTE_READ : PAGE_READONLY;
mapping_handle_ = CreateFileMapping(file_handle_, nullptr, mapping_flags, 0,
size_, nullptr);

CloseHandle(file_handle_);

if (mapping_handle_ == INVALID_HANDLE_VALUE) {
return;
}

int access_flags = FILE_MAP_READ;
if (executable) {
access_flags |= FILE_MAP_EXECUTE;
}
auto mapping = MapViewOfFile(mapping_handle_, access_flags, 0, 0, size_);

if (mapping == INVALID_HANDLE_VALUE) {
CloseHandle(mapping_handle_);
mapping_handle_ = INVALID_HANDLE_VALUE;
return;
}

void* symbol = static_cast<void*>(mapping);
if (symbol == NULL) {
return nullptr;
}
#else
struct stat info;
if (stat(asset_path.c_str(), &info) < 0) {
return nullptr;
Expand All @@ -123,6 +208,7 @@ static const uint8_t* MemMapSnapshot(const std::string& aot_snapshot_path,
if (symbol == MAP_FAILED) {
return nullptr;
}
#endif
return reinterpret_cast<const uint8_t*>(symbol);
}
#endif
Expand Down Expand Up @@ -161,7 +247,7 @@ void Engine::Init(const std::string& bundle_path) {
dlsym(library_handle, "kDartIsolateSnapshotData"));
default_isolate_snapshot_instr = reinterpret_cast<const uint8_t*>(
dlsym(library_handle, "kDartIsolateSnapshotInstructions"));
#elif OS(ANDROID)
#elif OS(ANDROID) || OS(WIN)
const blink::Settings& settings = blink::Settings::Get();
const std::string& aot_shared_library_path = settings.aot_shared_library_path;
const std::string& aot_snapshot_path = settings.aot_snapshot_path;
Expand Down
3 changes: 2 additions & 1 deletion shell/common/switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <sstream>
Expand All @@ -27,7 +28,7 @@ struct SwitchDesc {
// clang-format off
#define DEF_SWITCHES_START static const struct SwitchDesc gSwitchDescs[] = {
#define DEF_SWITCH(p_swtch, p_flag, p_help) \
{ .sw = shell::Switch:: p_swtch, .flag = p_flag, .help = p_help },
{ shell::Switch:: p_swtch, p_flag, p_help },
#define DEF_SWITCHES_END };
// clang-format on

Expand Down
4 changes: 3 additions & 1 deletion shell/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ group("platform") {
"embedder",
]
} else if (is_win) {
print("Shell currently not supported on Windows.")
deps = [
"win"
]
} else {
assert(false, "Unknown/Unsupported platform.")
}
Expand Down
30 changes: 30 additions & 0 deletions shell/platform/win/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2016 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.

executable("win") {
output_name = "flutter_tester"

sources = [
"main_win.cc",
]

deps = [
"//flutter/common",
"//flutter/flow",
"//flutter/fml",
"//flutter/shell/common",
"//flutter/shell/testing",
"//flutter/sky/engine/wtf",
"//garnet/public/lib/fxl",
"//third_party/dart/runtime/bin:embedded_dart_io",
"//third_party/dart/runtime:libdart_jit",
"//third_party/skia",
"//topaz/lib/tonic",
]

libs = [
"iphlpapi.lib",
"Rpcrt4.lib"
]
}
Loading

0 comments on commit 74a2d90

Please sign in to comment.