blob: e3115d020bbbad049bfc039bee080711b1078bc9 [file] [log] [blame]
# Copyright 2020 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//build/go/go_binary.gni")
import("//build/go/go_library.gni")
import("//build/go/toolchain.gni")
import("//build/host.gni")
import("//build/testing/golden_files.gni")
import("//tools/fidl/fidlc/testdata/info.gni")
import("//tools/fidl/lib/fidlgentest/fidlgentest_go_test.gni")
if (is_host) {
go_library("codegen") {
source_dir = "codegen"
deps = [ "//tools/fidl/lib/fidlgen" ]
sources = [
"bits.tmpl",
"codegen.go",
"enum.tmpl",
"ir.go",
"ir_test.go",
"library.tmpl",
"protocol.tmpl",
"struct.tmpl",
"table.tmpl",
"union.tmpl",
]
}
go_library("main") {
deps = [
":codegen",
"//tools/fidl/lib/fidlgen",
]
sources = [ "main.go" ]
}
go_binary("fidlgen_go") {
library = ":main"
sdk_category = "partner"
}
fidlgentest_go_test("fidlgen_go_lib_tests") {
library = ":codegen"
}
golden_files("fidlgen_go_golden_tests") {
testonly = true
formatter = {
script = "//prebuilt/third_party/go/$host_platform/bin/gofmt"
}
deps = []
comparisons = []
foreach(info, fidl_testdata_info) {
if (info.denylist + [ "fidlgen_go" ] - [ "fidlgen_go" ] ==
info.denylist) {
deps += [ "${info.target}_go_generate($go_toolchain)" ]
comparisons += [
{
golden = "goldens/${info.name}.go.golden"
candidate = "${info.fidl_gen_dir}/${info.target_name}/go/${info.library}.fidl/impl.go"
},
]
}
}
}
}
install_host_tools("host") {
deps = [ ":fidlgen_go" ]
outputs = [ "fidlgen_go" ]
}
# Test that the generated Go library compiles.
#
# Go libraries can only be compiled inside the context of some binary.
# The following is modeled from a similar template in
# /zircon/tools/zither/BUILD.gn.
# We create a simple main file that imports the generated package, which
# will feed into our compilation check.
template("fidlgen_go_golden_compilation_test") {
info = invoker.info
compilation_check_target = target_name
output_dir = "${target_gen_dir}/${target_name}"
create_main_target = "${compilation_check_target}.create_main"
main_file = "$output_dir/main.go"
# Something like "fidl/test/foobar"
go_package_under_test = "fidl/" + string_replace(info.library, ".", "/")
generated_file(create_main_target) {
visibility = [ ":*" ]
testonly = true
contents = [
"package main",
"",
"import (",
# It is necessary to import the generated bindings library.
# Otherwise, the tree-shaking build optimization from Go will
# avoid actually compiling the library.
# `_` to avoid usage complaints.
" _ \"${go_package_under_test}\"",
# Without this import, there are undefined symbol errors at link time.
" _ \"syscall/zx\"",
")",
"",
"func main() {}",
"",
]
outputs = [ main_file ]
}
# The generated main file now needs to be declared in a package/library.
main_library_target = "${compilation_check_target}.main_library"
main_pkg_name = rebase_path(output_dir, root_build_dir)
go_library(main_library_target) {
visibility = [ ":*" ]
testonly = true
name = main_pkg_name
source_dir = output_dir
sources = [ rebase_path(main_file, source_dir) ]
deps = [ info.target + "_go($go_toolchain)" ]
non_go_deps = [ ":$create_main_target" ]
}
go_binary(compilation_check_target) {
library = ":$main_library_target"
visibility = [ ":*" ]
testonly = true
sdk_category = "excluded"
}
}
golden_compilation_test_deps = []
if (is_fuchsia) {
foreach(testdata_info, fidl_testdata_info) {
if (testdata_info.build_denylist + [ "fidlgen_go" ] - [ "fidlgen_go" ] ==
testdata_info.build_denylist) {
compilation_check_target = "${testdata_info.name}_go_compilation_test"
fidlgen_go_golden_compilation_test(compilation_check_target) {
info = testdata_info
}
golden_compilation_test_deps += [ ":${compilation_check_target}" ]
}
}
}
group("goldens") {
testonly = true
deps = golden_compilation_test_deps
}
group("tests") {
testonly = true
deps = [
":fidlgen_go_golden_tests($host_toolchain)",
":fidlgen_go_lib_tests($host_toolchain)",
":goldens",
]
}