Skip to content

Commit 4ce18ab

Browse files
liamappelbecommit-bot@chromium.org
authored andcommitted
Add an --os=fuchsia option to build.py:
tools/build.py --os=fuchsia runtime create_sdk This is analogous to --os=android. It cross compiles from Linux x64 to Fuchsia. A lot of the build rules are just slightly different between the existing Fuchsia build rules used by Flutter, and the ones added by GN SDK. For example "$fuchsia_sdk_root/pkg:fdio" is now "$fuchsia_sdk_root/pkg/fdio". So to support this I had to add a new variable, using_fuchsia_gn_sdk, analogous to using_fuchsia_sdk. Flutter will need to set this to false. Change-Id: Ief275d65f30a42a801607de93cf2d27a1fe825dd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/150689 Reviewed-by: Ryan Macnak <[email protected]> Commit-Queue: Liam Appelbe <[email protected]>
1 parent d2543e3 commit 4ce18ab

File tree

16 files changed

+309
-32
lines changed

16 files changed

+309
-32
lines changed

DEPS

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,17 @@ deps = {
470470
"dep_type": "cipd",
471471
},
472472

473+
Var("dart_root") + "/third_party/fuchsia/sdk/linux": {
474+
"packages": [
475+
{
476+
"package": "fuchsia/sdk/gn/linux-amd64",
477+
"version": "git_revision:8d5242d4f6ff8b7634b492700e60b0fd09abefa3"
478+
}
479+
],
480+
"condition": 'host_os == "linux" and host_cpu == "x64"',
481+
"dep_type": "cipd",
482+
},
483+
473484
Var("dart_root") + "/pkg/front_end/test/fasta/types/benchmark_data": {
474485
"packages": [
475486
{

build/config/BUILDCONFIG.gn

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,16 @@ if (current_os == "win") {
196196
is_nacl = false
197197
is_posix = true
198198
is_win = false
199+
} else if (current_os == "fuchsia") {
200+
is_android = false
201+
is_chromeos = false
202+
is_fuchsia = true
203+
is_ios = false
204+
is_linux = false
205+
is_mac = false
206+
is_nacl = false
207+
is_posix = true
208+
is_win = false
199209
}
200210

201211
# =============================================================================
@@ -384,6 +394,11 @@ if (is_win) {
384394
} else if (is_mac) {
385395
host_toolchain = "//build/toolchain/mac:clang_x64"
386396
set_default_toolchain(host_toolchain)
397+
} else if (is_fuchsia) {
398+
assert(host_os == "linux")
399+
assert(host_cpu == "x64")
400+
host_toolchain = "//build/toolchain/linux:clang_$host_cpu"
401+
set_default_toolchain("//build/toolchain/fuchsia")
387402
}
388403

389404
# ==============================================================================

build/config/compiler/BUILD.gn

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ config("compiler") {
243243
# 3. When using the sanitizers.
244244
# Otherwise there is a performance hit, in particular on ia32.
245245
if (is_android || is_asan || is_lsan || is_msan || is_tsan || is_ubsan ||
246-
(is_linux && current_cpu != "x86")) {
246+
(is_linux && current_cpu != "x86") || is_fuchsia) {
247247
cflags += [ "-fPIC" ]
248248
ldflags += [ "-fPIC" ]
249249
}
@@ -276,6 +276,8 @@ config("compiler") {
276276
if (is_win) {
277277
# Up-to-date toolchain MSVC doesn't support c++11 flag any longer.
278278
cc_std = [ "/std:c++14" ]
279+
} else if (is_fuchsia) {
280+
cc_std = [ "-std=c++17" ]
279281
} else {
280282
cc_std = [ "-std=c++11" ]
281283
}

build/fuchsia/sdk.gni

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
# for details. All rights reserved. Use of this source code is governed by a
3+
# BSD-style license that can be found in the LICENSE file.
4+
5+
declare_args() {
6+
using_fuchsia_gn_sdk = true
7+
fuchsia_sdk_root = "//third_party/fuchsia/sdk/$host_os"
8+
fuchsia_sdk_path = "//third_party/fuchsia/sdk/$host_os"
9+
fuchsia_toolchain_path = "//third_party/fuchsia/toolchain/$host_os"
10+
}

build/toolchain/fuchsia/BUILD.gn

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
# for details. All rights reserved. Use of this source code is governed by a
3+
# BSD-style license that can be found in the LICENSE file.
4+
5+
import("//build/config/sysroot.gni")
6+
import("//build/toolchain/ccache.gni")
7+
import("//build/toolchain/gcc_toolchain.gni")
8+
import("//build/toolchain/goma.gni")
9+
10+
if (use_goma) {
11+
assert(!use_ccache, "Goma and ccache can't be used together.")
12+
compiler_prefix = "$goma_dir/gomacc "
13+
} else if (use_ccache) {
14+
compiler_prefix = "ccache "
15+
} else {
16+
compiler_prefix = ""
17+
}
18+
19+
toolchain("fuchsia") {
20+
assert(target_cpu == "x64", "We currently only support 'x64' for fuchsia.")
21+
toolchain_bin =
22+
rebase_path("//buildtools/$host_os-$target_cpu/clang/bin", root_out_dir)
23+
fuchsia_sdk = rebase_path("//third_party/fuchsia/sdk/$host_os", root_out_dir)
24+
25+
# We can't do string interpolation ($ in strings) on things with dots in
26+
# them. To allow us to use $cc below, for example, we create copies of
27+
# these values in our scope.
28+
cc = "${toolchain_bin}/clang"
29+
cxx = "${toolchain_bin}/clang++"
30+
ar = "${toolchain_bin}/llvm-ar"
31+
ld = "${toolchain_bin}/clang++"
32+
readelf = "${toolchain_bin}/llvm-readelf"
33+
nm = "${toolchain_bin}/llvm-nm"
34+
strip = "${toolchain_bin}/llvm-strip"
35+
36+
target_triple_flags = "--target=x86_64-fuchsia"
37+
sysroot_flags = "--sysroot ${fuchsia_sdk}/arch/${target_cpu}/sysroot"
38+
lto_flags = ""
39+
40+
# These library switches can apply to all tools below.
41+
lib_switch = "-l"
42+
lib_dir_switch = "-L"
43+
44+
tool("cc") {
45+
depfile = "{{output}}.d"
46+
command = "$compiler_prefix $cc -MD -MF $depfile $target_triple_flags $sysroot_flags $lto_flags {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
47+
depsformat = "gcc"
48+
description = "CC {{output}}"
49+
outputs =
50+
[ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]
51+
}
52+
53+
tool("cxx") {
54+
depfile = "{{output}}.d"
55+
command = "$compiler_prefix $cxx -MD -MF $depfile $target_triple_flags $sysroot_flags $lto_flags {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
56+
depsformat = "gcc"
57+
description = "CXX {{output}}"
58+
outputs =
59+
[ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]
60+
}
61+
62+
tool("asm") {
63+
depfile = "{{output}}.d"
64+
command = "$compiler_prefix $cc -MD -MF $depfile $target_triple_flags $sysroot_flags $lto_flags {{defines}} {{include_dirs}} {{asmflags}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
65+
depsformat = "gcc"
66+
description = "ASM {{output}}"
67+
outputs =
68+
[ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]
69+
}
70+
71+
tool("alink") {
72+
rspfile = "{{output}}.rsp"
73+
command = "rm -f {{output}} && $ar rcs {{output}} @$rspfile"
74+
description = "AR {{output}}"
75+
rspfile_content = "{{inputs}}"
76+
outputs =
77+
[ "{{target_out_dir}}/{{target_output_name}}{{output_extension}}" ]
78+
default_output_extension = ".a"
79+
output_prefix = "lib"
80+
}
81+
82+
tool("solink") {
83+
soname = "{{target_output_name}}{{output_extension}}" # e.g. "libfoo.so".
84+
sofile = "{{root_out_dir}}/$soname" # Possibly including toolchain dir.
85+
unstripped_sofile =
86+
"{{root_out_dir}}/so.unstripped/$soname" # Possibly including toolchain
87+
# dir.
88+
rspfile = sofile + ".rsp"
89+
90+
# These variables are not built into GN but are helpers that implement
91+
# (1) linking to produce a .so, (2) extracting the symbols from that file
92+
# to a temporary file, (3) if the temporary file has differences from the
93+
# existing .TOC file, overwrite it, otherwise, don't change it.
94+
tocfile = sofile + ".TOC"
95+
temporary_tocname = sofile + ".tmp"
96+
link_command = "$compiler_prefix $ld $target_triple_flags $sysroot_flags $lto_flags -shared {{ldflags}} -o $unstripped_sofile -Wl,--build-id -Wl,-soname=$soname @$rspfile"
97+
toc_command = "{ $readelf -d $unstripped_sofile | grep SONAME ; $nm -gD -f posix $unstripped_sofile | cut -f1-2 -d' '; } > $temporary_tocname"
98+
replace_command = "if ! cmp -s $temporary_tocname $tocfile; then mv $temporary_tocname $tocfile; fi"
99+
strip_command = "$strip -o $sofile $unstripped_sofile"
100+
101+
command =
102+
"$link_command && $toc_command && $replace_command && $strip_command"
103+
rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive {{libs}}"
104+
105+
description = "SOLINK $sofile"
106+
107+
default_output_extension = ".so"
108+
109+
output_prefix = "lib"
110+
111+
# Since the above commands only updates the .TOC file when it changes, ask
112+
# Ninja to check if the timestamp actually changed to know if downstream
113+
# dependencies should be recompiled.
114+
restat = true
115+
116+
# Tell GN about the output files. It will link to the sofile but use the
117+
# tocfile for dependency management.
118+
outputs = [
119+
sofile,
120+
unstripped_sofile,
121+
tocfile,
122+
]
123+
124+
link_output = sofile
125+
depend_output = tocfile
126+
}
127+
128+
tool("link") {
129+
exename = "{{target_output_name}}{{output_extension}}"
130+
outfile = "{{root_out_dir}}/$exename"
131+
rspfile = "$outfile.rsp"
132+
unstripped_outfile = "{{root_out_dir}}/exe.stripped/$exename"
133+
command = "$compiler_prefix $ld $target_triple_flags $sysroot_flags $lto_flags {{ldflags}} -o $unstripped_outfile -Wl,--build-id -Wl,--start-group @$rspfile {{solibs}} -Wl,--end-group {{libs}} && ${strip} -o $outfile $unstripped_outfile"
134+
description = "LINK $outfile"
135+
rspfile_content = "{{inputs}}"
136+
outputs = [
137+
unstripped_outfile,
138+
outfile,
139+
]
140+
}
141+
142+
tool("stamp") {
143+
command = "touch {{output}}"
144+
description = "STAMP {{output}}"
145+
}
146+
147+
tool("copy") {
148+
command = "ln -f {{source}} {{output}} 2>/dev/null || (rm -rf {{output}} && cp -af {{source}} {{output}})"
149+
description = "COPY {{source}} {{output}}"
150+
}
151+
152+
# When invoking this toolchain not as the default one, these args will be
153+
# passed to the build. They are ignored when this is the default toolchain.
154+
toolchain_args = {
155+
current_cpu = target_cpu
156+
current_os = target_os
157+
158+
# These values need to be passed through unchanged.
159+
target_os = target_os
160+
target_cpu = target_cpu
161+
162+
is_clang = true
163+
}
164+
}

runtime/BUILD.gn

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ config("dart_config") {
136136
}
137137

138138
if (is_fuchsia) {
139-
if (using_fuchsia_sdk) {
139+
if (using_fuchsia_gn_sdk) {
140+
lib_dirs = [ "../out/DebugFuchsiaX64/lib" ]
141+
}
142+
if (using_fuchsia_gn_sdk || using_fuchsia_sdk) {
140143
# TODO(chinmaygarde): Currenty these targets need to be build in the
141144
# Fuchsia tree as well as outside it using the SDK. However, not all
142145
# Fuchsia features are available in the SDK. As these features are added,
@@ -223,7 +226,12 @@ library_for_all_configs("libdart") {
223226
defines = [ "DART_ENABLE_WASM" ]
224227
}
225228
if (is_fuchsia) {
226-
if (using_fuchsia_sdk) {
229+
if (using_fuchsia_gn_sdk) {
230+
extra_deps += [
231+
"$fuchsia_sdk_root/pkg/fdio",
232+
"$fuchsia_sdk_root/pkg/trace-engine",
233+
]
234+
} else if (using_fuchsia_sdk) {
227235
extra_deps += [
228236
"$fuchsia_sdk_root/pkg:fdio",
229237
"$fuchsia_sdk_root/pkg:trace-engine",

runtime/bin/BUILD.gn

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ template("build_libdart_builtin") {
4242
public_configs = [ ":libdart_builtin_config" ]
4343
deps = []
4444
if (is_fuchsia) {
45-
if (using_fuchsia_sdk) {
45+
if (using_fuchsia_gn_sdk) {
46+
public_deps = [ "$fuchsia_sdk_root/pkg/fdio" ]
47+
} else if (using_fuchsia_sdk) {
4648
public_deps = [ "$fuchsia_sdk_root/pkg:fdio" ]
4749
} else {
4850
public_deps = [ "//zircon/public/lib/fdio" ]
@@ -318,7 +320,13 @@ template("build_gen_snapshot_dart_io") {
318320
deps = []
319321

320322
if (is_fuchsia) {
321-
if (using_fuchsia_sdk) {
323+
if (using_fuchsia_gn_sdk) {
324+
deps += [
325+
"$fuchsia_sdk_root/fidl/fuchsia.netstack",
326+
"$fuchsia_sdk_root/pkg/sys_cpp",
327+
]
328+
public_deps = [ "$fuchsia_sdk_root/pkg/fdio" ]
329+
} else if (using_fuchsia_sdk) {
322330
deps += [
323331
"$fuchsia_sdk_root/fidl:fuchsia.netstack",
324332
"$fuchsia_sdk_root/pkg:sys_cpp",
@@ -454,7 +462,13 @@ template("dart_io") {
454462
deps += [ "//third_party/boringssl" ]
455463

456464
if (is_fuchsia) {
457-
if (using_fuchsia_sdk) {
465+
if (using_fuchsia_gn_sdk) {
466+
deps += [
467+
"$fuchsia_sdk_root/fidl/fuchsia.netstack",
468+
"$fuchsia_sdk_root/pkg/sys_cpp",
469+
]
470+
public_deps = [ "$fuchsia_sdk_root/pkg/fdio" ]
471+
} else if (using_fuchsia_sdk) {
458472
deps += [
459473
"$fuchsia_sdk_root/fidl:fuchsia.netstack",
460474
"$fuchsia_sdk_root/pkg:sys_cpp",
@@ -788,7 +802,9 @@ template("dart_executable") {
788802
# have them. They are needed for running Fuchsia binaries built for the
789803
# host.
790804
if (is_linux) {
791-
configs += [ "../../build/config/gcc:executable_ldconfig" ]
805+
# TODO(liama): Commenting this line out because it causes problems for
806+
# --os=fuchsia. If no one complains, remove it.
807+
# configs += [ "../../build/config/gcc:executable_ldconfig" ]
792808
} else if (is_mac) {
793809
configs += [ "../../build/config/mac:mac_dynamic_flags" ]
794810
}
@@ -1012,7 +1028,11 @@ executable("run_vm_tests") {
10121028
}
10131029

10141030
if (is_fuchsia) {
1015-
if (!using_fuchsia_sdk) {
1031+
if (using_fuchsia_gn_sdk) {
1032+
include_dirs += [ "$fuchsia_sdk_path/pkg/trace-engine/include" ]
1033+
libs = [ "zircon" ]
1034+
}
1035+
if (!using_fuchsia_gn_sdk && !using_fuchsia_sdk) {
10161036
deps += [ "//zircon/system/ulib/trace" ]
10171037
}
10181038
}

runtime/bin/process_fuchsia.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -744,16 +744,20 @@ class ProcessStarter {
744744
}
745745
actions[3] = {
746746
.action = FDIO_SPAWN_ACTION_SET_NAME,
747-
.name.data = program_arguments_[0],
747+
.name = {
748+
.data = program_arguments_[0],
749+
},
748750
};
749751

750752
// Then fill in the namespace actions.
751753
if (ns != nullptr) {
752754
for (size_t i = 0; i < flat_ns->count; i++) {
753755
actions[fixed_actions_cnt + i] = {
754756
.action = FDIO_SPAWN_ACTION_ADD_NS_ENTRY,
755-
.ns.prefix = flat_ns->path[i],
756-
.ns.handle = flat_ns->handle[i],
757+
.ns = {
758+
.prefix = flat_ns->path[i],
759+
.handle = flat_ns->handle[i],
760+
},
757761
};
758762
}
759763
free(flat_ns);

runtime/bin/snapshot_utils.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ static AppSnapshot* TryReadAppSnapshotElf(
201201
*isolate_data_buffer = nullptr,
202202
*isolate_instructions_buffer = nullptr;
203203
Dart_LoadedElf* handle = nullptr;
204+
#if !defined(HOST_OS_FUCHSIA)
204205
if (force_load_elf_from_memory) {
206+
#endif
205207
File* const file =
206208
File::Open(/*namespc=*/nullptr, script_name, File::kRead);
207209
if (file == nullptr) return nullptr;
@@ -216,11 +218,13 @@ static AppSnapshot* TryReadAppSnapshotElf(
216218
&isolate_data_buffer, &isolate_instructions_buffer);
217219
delete memory;
218220
file->Release();
221+
#if !defined(HOST_OS_FUCHSIA)
219222
} else {
220223
handle = Dart_LoadELF(script_name, file_offset, &error, &vm_data_buffer,
221224
&vm_instructions_buffer, &isolate_data_buffer,
222225
&isolate_instructions_buffer);
223226
}
227+
#endif
224228
if (handle == nullptr) {
225229
Syslog::PrintErr("Loading failed: %s\n", error);
226230
return nullptr;

runtime/platform/BUILD.gn

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ library_for_all_configs("libdart_platform") {
1717
extra_deps = []
1818

1919
if (is_fuchsia) {
20-
if (using_fuchsia_sdk) {
20+
if (using_fuchsia_gn_sdk) {
21+
extra_deps += [ "$fuchsia_sdk_root/pkg/sys_cpp" ]
22+
} else if (using_fuchsia_sdk) {
2123
extra_deps += [ "$fuchsia_sdk_root/pkg:sys_cpp" ]
2224
} else {
2325
extra_deps += [ "//sdk/lib/sys/cpp" ]

0 commit comments

Comments
 (0)