diff options
Diffstat (limited to 'tests/legacy/examples/cgo')
20 files changed, 320 insertions, 0 deletions
diff --git a/tests/legacy/examples/cgo/BUILD.bazel b/tests/legacy/examples/cgo/BUILD.bazel new file mode 100644 index 00000000..901f6d8d --- /dev/null +++ b/tests/legacy/examples/cgo/BUILD.bazel @@ -0,0 +1,51 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +package( + default_visibility = ["//visibility:public"], +) + +go_library( + name = "go_default_library", + srcs = [ + "export_example.go", + "generated.go", + "import_example.go", + "pure_go.go", + "use_exported.c", + "use_exported.h", + ], + cdeps = ["//tests/legacy/examples/cgo/cc_dependency:version"], + cgo = True, + clinkopts = ["-lm"], + copts = ["-DDEFINED_IN_COPTS=1"], + importpath = "github.com/bazelbuild/rules_go/examples/cgo", + visibility = ["//visibility:public"], + deps = [":sub"], +) + +# TODO(#746) exclude because gazelle does not recognise the rule below +# gazelle:exclude sub + +go_library( + name = "sub", + srcs = ["sub/floor.go"], + cgo = True, + clinkopts = ["-lm"], + importpath = "github.com/bazelbuild/rules_go/examples/cgo/sub", + visibility = ["//visibility:private"], +) + +go_test( + name = "cgo_lib_test", + size = "small", + srcs = ["cgo_lib_test.go"], + embed = [":go_default_library"], +) + +genrule( + name = "generate_go_src", + srcs = ["generated.go.tpl"], + outs = ["generated.go"], + cmd = "cp -f $< $@", + visibility = ["//visibility:private"], +) diff --git a/tests/legacy/examples/cgo/cc_dependency/BUILD.bazel b/tests/legacy/examples/cgo/cc_dependency/BUILD.bazel new file mode 100644 index 00000000..f0eee7dd --- /dev/null +++ b/tests/legacy/examples/cgo/cc_dependency/BUILD.bazel @@ -0,0 +1,29 @@ +cc_library( + name = "version", + srcs = ["cxx_version.cc"], + hdrs = ["version.h"], + linkopts = ["-ldl"], + visibility = ["//tests/legacy/examples/cgo:__pkg__"], + # TODO(yugui) Support darwin too and remove this workaround. + # See also comments in cxx_version.cc. + deps = select({ + "@platforms//os:macos": [], + "//conditions:default": [":c_version_import"], + }), +) + +cc_binary( + name = "c_version_so", + srcs = [ + "c_version.c", + "c_version.h", + ], + linkshared = True, +) + +cc_import( + name = "c_version_import", + hdrs = ["c_version.h"], + shared_library = ":c_version_so", + tags = ["manual"], +) diff --git a/tests/legacy/examples/cgo/cc_dependency/c_version.c b/tests/legacy/examples/cgo/cc_dependency/c_version.c new file mode 100644 index 00000000..5672898e --- /dev/null +++ b/tests/legacy/examples/cgo/cc_dependency/c_version.c @@ -0,0 +1,15 @@ +#include <stdio.h> + +#include "tests/legacy/examples/cgo/cc_dependency/c_version.h" + +void PrintCVersion() { +#ifdef __STDC__ +# ifdef __STDC_VERSION__ + printf("C version: %ld\n", __STDC_VERSION__); +# else + printf("C version: C89\n"); +# endif +#else + printf("C version: maybe K&R\n"); +#endif +} diff --git a/tests/legacy/examples/cgo/cc_dependency/c_version.h b/tests/legacy/examples/cgo/cc_dependency/c_version.h new file mode 100644 index 00000000..5ff655cf --- /dev/null +++ b/tests/legacy/examples/cgo/cc_dependency/c_version.h @@ -0,0 +1,5 @@ +#ifdef __cplusplus +extern "C" void PrintCVersion(); +#else +void PrintCVersion(); +#endif diff --git a/tests/legacy/examples/cgo/cc_dependency/cxx_version.cc b/tests/legacy/examples/cgo/cc_dependency/cxx_version.cc new file mode 100644 index 00000000..43fb427e --- /dev/null +++ b/tests/legacy/examples/cgo/cc_dependency/cxx_version.cc @@ -0,0 +1,30 @@ +#include <dlfcn.h> +#include <iostream> + +#include "tests/legacy/examples/cgo/cc_dependency/version.h" + +// TODO(yugui) Support Darwin too once Bazel allows it. +// +// Bazel passes two or more -Wl,-rpath to $(CC) when it links a binary with +// shared libraries prebuilt outside of Bazel (i.e. when "srcs" attribute of +// the dependency cc_library contains ".so" files). +// Unfortunately tools/cpp/osx_cc_wrapper.sh, which is $(CC) on Darwin, expects +// only one -Wl,-rpath. So the binary fails to resolve the shared libraries +// at runtime. +#ifndef __APPLE_CC__ +# include "tests/legacy/examples/cgo/cc_dependency/c_version.h" +#endif + +extern "C" void PrintCXXVersion() { +#ifndef __APPLE_CC__ + PrintCVersion(); +#endif + void* ptr = dlsym(RTLD_DEFAULT, "PrintCXXVersion"); + if (ptr) { + std::cout + << "function ptr: " << std::hex << ptr << std::dec << std::endl; + } else { + std::cout << dlerror() << std::endl; + } + std::cout << "C++ version: " << __cplusplus << std::endl; +} diff --git a/tests/legacy/examples/cgo/cc_dependency/version.h b/tests/legacy/examples/cgo/cc_dependency/version.h new file mode 100644 index 00000000..025656b3 --- /dev/null +++ b/tests/legacy/examples/cgo/cc_dependency/version.h @@ -0,0 +1,10 @@ +#ifndef BAZEL_RULES_GO_EXAMPLES_CGO_CC_DEPENDENCY_VERSION_H_ +#define BAZEL_RULES_GO_EXAMPLES_CGO_CC_DEPENDENCY_VERSION_H_ + +#ifdef __cplusplus +extern "C" void PrintCXXVersion(); +#else +void PrintCXXVersion(); +#endif + +#endif // BAZEL_RULES_GO_EXAMPLES_CGO_CC_DEPENDENCY_VERSION_H_ diff --git a/tests/legacy/examples/cgo/cgo_lib_test.go b/tests/legacy/examples/cgo/cgo_lib_test.go new file mode 100644 index 00000000..a486da6a --- /dev/null +++ b/tests/legacy/examples/cgo/cgo_lib_test.go @@ -0,0 +1,15 @@ +package cgo + +import ( + "math" + "testing" +) + +func TestNsqrt(t *testing.T) { + for _, n := range []int{1, 2, 10, 100, 1000} { + got, want := Nsqrt(n), int(math.Floor(math.Sqrt(float64(n)))) + if got != want { + t.Errorf("Nsqrt(n) = %d; want %d", got, want) + } + } +} diff --git a/tests/legacy/examples/cgo/example_command/BUILD.bazel b/tests/legacy/examples/cgo/example_command/BUILD.bazel new file mode 100644 index 00000000..db480ab7 --- /dev/null +++ b/tests/legacy/examples/cgo/example_command/BUILD.bazel @@ -0,0 +1,26 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_binary") +load(":generate_test.bzl", "generate_script") + +package( + default_visibility = ["//visibility:public"], +) + +go_binary( + name = "example_command", + srcs = ["main.go"], + deps = [ + "//tests/legacy/examples/cgo:go_default_library", + ], +) + +generate_script( + name = "example_command_script", + binary = ":example_command", +) + +sh_test( + name = "example_command_test", + size = "small", + srcs = [":example_command_script"], + data = [":example_command"], +) diff --git a/tests/legacy/examples/cgo/example_command/generate_test.bzl b/tests/legacy/examples/cgo/example_command/generate_test.bzl new file mode 100644 index 00000000..c3c991df --- /dev/null +++ b/tests/legacy/examples/cgo/example_command/generate_test.bzl @@ -0,0 +1,17 @@ +def _generate_script_impl(ctx): + script_file = ctx.actions.declare_file(ctx.label.name + ".bash") + ctx.actions.write(output = script_file, is_executable = True, content = """ +{0} +""".format(ctx.file.binary.short_path)) + return struct( + files = depset([script_file]), + ) + +generate_script = rule( + _generate_script_impl, + attrs = { + "binary": attr.label( + allow_single_file = True, + ), + }, +) diff --git a/tests/legacy/examples/cgo/example_command/main.go b/tests/legacy/examples/cgo/example_command/main.go new file mode 100644 index 00000000..8f0bfd6a --- /dev/null +++ b/tests/legacy/examples/cgo/example_command/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" + + "github.com/bazelbuild/rules_go/examples/cgo" +) + +func main() { + fmt.Println("floor(sqrt(10)) = ", cgo.Nsqrt(10)) + cgo.PrintGoVersion() + cgo.PrintCXXVersion() +} diff --git a/tests/legacy/examples/cgo/export_example.go b/tests/legacy/examples/cgo/export_example.go new file mode 100644 index 00000000..874340c7 --- /dev/null +++ b/tests/legacy/examples/cgo/export_example.go @@ -0,0 +1,11 @@ +package cgo + +import ( + "C" + "runtime" +) + +//export goVersion +func goVersion() string { + return runtime.Version() +} diff --git a/tests/legacy/examples/cgo/generated.go.tpl b/tests/legacy/examples/cgo/generated.go.tpl new file mode 100644 index 00000000..ba80e040 --- /dev/null +++ b/tests/legacy/examples/cgo/generated.go.tpl @@ -0,0 +1,13 @@ +package cgo + +import ( + //#cgo LDFLAGS: -lm + //#include <math.h> + "C" + "math" +) + +// Ncbrt returns the cube root of n. +func Ncbrt(n int) int { + return int(math.Floor(float64(C.cbrt(C.double(n))))) +} diff --git a/tests/legacy/examples/cgo/import_example.go b/tests/legacy/examples/cgo/import_example.go new file mode 100644 index 00000000..a25a6971 --- /dev/null +++ b/tests/legacy/examples/cgo/import_example.go @@ -0,0 +1,29 @@ +package cgo + +import ( + //#cgo LDFLAGS: -lm -lversion -lc_version -L${SRCDIR}/cc_dependency + //#cgo CPPFLAGS: -I${SRCDIR}/../.. + //#include <math.h> + //#include "use_exported.h" + //#include "cc_dependency/version.h" + "C" + + "github.com/bazelbuild/rules_go/examples/cgo/sub" +) + +// Nsqrt returns the square root of n. +func Nsqrt(n int) int { + return int(sub.Floor(float64(C.sqrt(C.double(n))))) +} + +func PrintGoVersion() { + C.PrintGoVersion() +} + +func printCXXVersion() { + C.PrintCXXVersion() +} + +func ReturnDefined() int { + return int(C.DEFINED_IN_COPTS) +} diff --git a/tests/legacy/examples/cgo/pure_go.go b/tests/legacy/examples/cgo/pure_go.go new file mode 100644 index 00000000..531517dd --- /dev/null +++ b/tests/legacy/examples/cgo/pure_go.go @@ -0,0 +1,5 @@ +package cgo + +func PrintCXXVersion() { + printCXXVersion() +} diff --git a/tests/legacy/examples/cgo/skip_go_library/BUILD.bazel b/tests/legacy/examples/cgo/skip_go_library/BUILD.bazel new file mode 100644 index 00000000..69a99596 --- /dev/null +++ b/tests/legacy/examples/cgo/skip_go_library/BUILD.bazel @@ -0,0 +1,13 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +package(default_visibility = ["//visibility:public"]) + +go_library( + name = "go_default_library", + srcs = [ + "cgo_foo.go", + "types.go", + ], + cgo = True, + importpath = "github.com/bazelbuild/rules_go/examples/cgo/skip_go_library", +) diff --git a/tests/legacy/examples/cgo/skip_go_library/cgo_foo.go b/tests/legacy/examples/cgo/skip_go_library/cgo_foo.go new file mode 100644 index 00000000..42b1dbaa --- /dev/null +++ b/tests/legacy/examples/cgo/skip_go_library/cgo_foo.go @@ -0,0 +1,8 @@ +package skip_go_library + +/* +#include <stdio.h> +*/ +import "C" + +var _ = Type{} diff --git a/tests/legacy/examples/cgo/skip_go_library/types.go b/tests/legacy/examples/cgo/skip_go_library/types.go new file mode 100644 index 00000000..1f3510ec --- /dev/null +++ b/tests/legacy/examples/cgo/skip_go_library/types.go @@ -0,0 +1,3 @@ +package skip_go_library + +type Type struct{} diff --git a/tests/legacy/examples/cgo/sub/floor.go b/tests/legacy/examples/cgo/sub/floor.go new file mode 100644 index 00000000..315b5c8f --- /dev/null +++ b/tests/legacy/examples/cgo/sub/floor.go @@ -0,0 +1,13 @@ +package sub + +import ( + //#cgo LDFLAGS: -lm + //#include <math.h> + "C" +) + +// Floor calculates floor of the given number +// with the implementation in the standard C library. +func Floor(f float64) float64 { + return float64(C.floor(C.double(f))) +} diff --git a/tests/legacy/examples/cgo/use_exported.c b/tests/legacy/examples/cgo/use_exported.c new file mode 100644 index 00000000..0a6d01e7 --- /dev/null +++ b/tests/legacy/examples/cgo/use_exported.c @@ -0,0 +1,8 @@ +#include <stdio.h> + +#include "_cgo_export.h" + +void PrintGoVersion() { + GoString version = goVersion(); + printf("Go version: %.*s\n", (int)version.n, version.p); +} diff --git a/tests/legacy/examples/cgo/use_exported.h b/tests/legacy/examples/cgo/use_exported.h new file mode 100644 index 00000000..3c8f8414 --- /dev/null +++ b/tests/legacy/examples/cgo/use_exported.h @@ -0,0 +1,6 @@ +#ifndef BAZEL_RULES_GO_EXAMPLES_CGO_USE_EXPORTED_H_ +#define BAZEL_RULES_GO_EXAMPLES_CGO_USE_EXPORTED_H_ + +void PrintGoVersion(); + +#endif // BAZEL_RULES_GO_EXAMPLES_CGO_USE_EXPORTED_H_ |