Commit 8b972f42 authored by Sergey Ulanov's avatar Sergey Ulanov Committed by Commit Bot

[Fuchsia] Update namespace in generated FIDL files.

Currently fidlgen uses the namespace that's specified in the .fidl file.
As result the generated code may collide with other code in
chromium. This CL implements a temporary workaround for this problem:
generated code is updated to use a namespaces nested in fuchsia::
namespace. The script won't be necessary after FIDL-160 is resolved.

Bug: 831384
Change-Id: I3648f89f44a8e0148b1cc6b58d6f3a8de82a684e
Reviewed-on: https://chromium-review.googlesource.com/1045257Reviewed-by: default avatarKevin Marshall <kmarshall@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Sergey Ulanov <sergeyu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#556882}
parent 3a17a2f6
......@@ -2120,6 +2120,7 @@ if (is_fuchsia) {
sources = [
"fuchsia/test.fidl",
]
namespace = "base"
}
}
......
......@@ -11,7 +11,7 @@
namespace net {
IPAddress NetAddressToIPAddress(const netstack::NetAddress& addr) {
IPAddress NetAddressToIPAddress(const fuchsia::netstack::NetAddress& addr) {
if (addr.ipv4) {
return IPAddress(addr.ipv4->addr.data(), addr.ipv4->addr.count());
}
......@@ -22,25 +22,25 @@ IPAddress NetAddressToIPAddress(const netstack::NetAddress& addr) {
}
bool GetNetworkList(NetworkInterfaceList* networks, int policy) {
netstack::NetstackSyncPtr netstack =
fuchsia::netstack::NetstackSyncPtr netstack =
base::fuchsia::ComponentContext::GetDefault()
->ConnectToServiceSync<netstack::Netstack>();
->ConnectToServiceSync<fuchsia::netstack::Netstack>();
fidl::VectorPtr<netstack::NetInterface> interfaces;
fidl::VectorPtr<fuchsia::netstack::NetInterface> interfaces;
if (!netstack->GetInterfaces(&interfaces))
return false;
for (auto& interface : interfaces.get()) {
// Check if the interface is up.
if (!(interface.flags & netstack::NetInterfaceFlagUp))
if (!(interface.flags & fuchsia::netstack::NetInterfaceFlagUp))
continue;
// Skip loopback.
if (interface.features & netstack::interfaceFeatureLoopback)
if (interface.features & fuchsia::netstack::interfaceFeatureLoopback)
continue;
NetworkChangeNotifier::ConnectionType connection_type =
(interface.features & netstack::interfaceFeatureWlan)
(interface.features & fuchsia::netstack::interfaceFeatureWlan)
? NetworkChangeNotifier::CONNECTION_WIFI
: NetworkChangeNotifier::CONNECTION_UNKNOWN;
......
......@@ -4,6 +4,18 @@
assert(is_fuchsia)
# Template for FIDL libraries. Following parameters can be passed when
# instantiating these templates:
# sources - List of .fidl files.
# name - (optional) Name of the library. Must match the name specified
# in the .fidl files. target_name is used if name is not specified
# explicitly.
# deps - (optional) List of other fild_library() targets that this FIDL
# library depends on.
# namespace - (optional) Namespace for generated code. All classes in the
# generated code are placed under <namespace>::<name>:: namespace.
# 'fuchsia' namespace is used by default.
#
template("fidl_library") {
pkg_name = target_name
......@@ -12,10 +24,16 @@ template("fidl_library") {
pkg_name = invoker.name
}
namespace = "fuchsia"
if (defined(invoker.namespace)) {
namespace = invoker.namespace
}
response_file = "$target_gen_dir/$target_name.rsp"
json_representation = "$target_gen_dir/$pkg_name.fidl.json"
output_gen_base = "$target_gen_dir/fidl"
output_gen_dir = "$output_gen_base/fuchsia/cpp"
output_gen_dir_no_ns = "$output_gen_base/no_ns/fuchsia/cpp"
tables_file = "$output_gen_base/$pkg_name.fidl-tables.cc"
action("${target_name}_response_file") {
......@@ -97,7 +115,7 @@ template("fidl_library") {
}
action("${target_name}_cpp_gen") {
visibility = [ ":${invoker.target_name}" ]
visibility = [ ":${invoker.target_name}_update_namespace" ]
deps = [
":${invoker.target_name}_compile",
......@@ -108,8 +126,8 @@ template("fidl_library") {
]
outputs = [
"$output_gen_dir/$pkg_name.h",
"$output_gen_dir/$pkg_name.cc",
"${output_gen_dir_no_ns}/${pkg_name}.h",
"${output_gen_dir_no_ns}/${pkg_name}.cc",
]
script = "//build/gn_run_binary.py"
......@@ -121,9 +139,37 @@ template("fidl_library") {
"-json",
rebase_path("$json_representation"),
"-include-base",
rebase_path("$output_gen_base"),
rebase_path("$output_gen_base/no_ns"),
"-output-base",
rebase_path("$output_gen_dir/$pkg_name"),
rebase_path("${output_gen_dir_no_ns}/${pkg_name}"),
]
}
# Move generated code to a different namespace to avoid conflicts with
# existing code in chromium.
# TODO(sergeyu): Remove this once FIDL-160 is resolved in Fuchsia.
action_foreach("${target_name}_update_namespace") {
visibility = [ ":${invoker.target_name}" ]
deps = [
":${invoker.target_name}_cpp_gen",
]
sources = [
"${output_gen_dir_no_ns}/${pkg_name}.cc",
"${output_gen_dir_no_ns}/${pkg_name}.h",
]
outputs = [
"${output_gen_dir}/{{source_file_part}}",
]
script = "//third_party/fuchsia-sdk/update_namespace.py"
args = [
namespace,
pkg_name,
"{{source}}",
rebase_path("${output_gen_dir}/{{source_file_part}}"),
]
}
......@@ -152,7 +198,7 @@ template("fidl_library") {
}
deps += [
":${invoker.target_name}_compile",
":${invoker.target_name}_cpp_gen",
":${invoker.target_name}_update_namespace",
]
if (!defined(public_deps)) {
......
#!/usr/bin/env python
# Copyright 2018 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.
#
# Helper script used to update namespace in C++ files generated by fidlgen. This
# is necessary to avoid naming conflicts with existing code in chromium. For
# example ui::Event FIDL interface collides with ui::Event class in chromium.
# This script moves it to fuchsia::ui::Event.
#
# TODO(sergeyu): Remove this script once FIDL files in fuchsia are updated to
# use fuchsia namespace.
import sys
import re
def main(namespace, libname, input_file, output_file):
with open(input_file) as f:
text = f.read()
text = re.sub(r'^namespace\ %s\ \{$' % libname,
r'namespace %s {\nnamespace %s {' %
(namespace, libname),
text, flags=re.MULTILINE)
text = re.sub(r'}\ \ // namespace\ %s$' % libname,
r'} // namespace %s\n} // namespace %s'
% (libname, namespace),
text, flags=re.MULTILINE)
text = re.sub(r'(?<![a-z0-9_])%s::' % libname,
r'%s::%s::' % (namespace, libname),
text)
with open(output_file, "w") as f:
f.write(text)
if __name__ == "__main__":
sys.exit(main(*sys.argv[1:]))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment