diff --git a/test/cgo/cgo.go b/test/cgo/cgo.go index 7534209..2519b21 100644 --- a/test/cgo/cgo.go +++ b/test/cgo/cgo.go @@ -4,8 +4,9 @@ package main // #include // #include // #include +// #include // char* hello() { return "hello, world"; } -// void phello() { printf("%s, your lucky number is %p\n", hello(), fcntl); } +// void phello() { printf("%s, your lucky numbers are %p and %p\n", hello(), fcntl, res_search); } import "C" func main() { diff --git a/toolchain/BUILD.sdk.bazel b/toolchain/BUILD.sdk.bazel index 6fd4495..bea4447 100644 --- a/toolchain/BUILD.sdk.bazel +++ b/toolchain/BUILD.sdk.bazel @@ -11,7 +11,9 @@ declare_files( exports_files([ "glibc-hacks/fcntl.map", - "glibc-hacks/glibchack-fcntl.h", + "glibc-hacks/fcntl.h", + "glibc-hacks/res_search.map", + "glibc-hacks/res_search.h", ]) declare_cc_toolchains( diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl index db97916..08e1fc5 100644 --- a/toolchain/defs.bzl +++ b/toolchain/defs.bzl @@ -10,19 +10,6 @@ _DEFAULT_INCLUDE_DIRECTORIES = [ "libunwind/include", ] -_fcntl_map = """ -GLIBC_2.2.5 { - fcntl; -}; -""" -_fcntl_h = """ -#ifdef __ASSEMBLER__ -.symver fcntl64, fcntl@GLIBC_2.2.5 -#else -__asm__(".symver fcntl64, fcntl@GLIBC_2.2.5"); -#endif -""" - # Official recommended version. Should use this when we have a usable release. URL_FORMAT_RELEASE = "https://ziglang.org/download/{version}/zig-{host_platform}-{version}.{_ext}" @@ -82,6 +69,36 @@ _ZIG_TOOLS = [ "ar", ] +_template_mapfile = """ +%s { + %s; +}; +""" + +_template_linker = """ +#ifdef __ASSEMBLER__ +.symver {from_function}, {to_function_abi} +#else +__asm__(".symver {from_function}, {to_function_abi}"); +#endif +""" + +def _glibc_hack(from_function, to_function_abi): + # Cannot use .format(...) here, because starlark thinks + # that the byte 3 (the opening brace on the first line) + # is a nested { ... }, returning an error: + # Error in format: Nested replacement fields are not supported + to_function, to_abi = to_function_abi.split("@") + mapfile = _template_mapfile % (to_abi, to_function) + header = _template_linker.format( + from_function = from_function, + to_function_abi = to_function_abi, + ) + return struct( + mapfile = mapfile, + header = header, + ) + def _quote(s): return "'" + s.replace("'", "'\\''") + "'" @@ -188,14 +205,12 @@ def _zig_repository_impl(repository_ctx): ) repository_ctx.symlink("tools/launcher{}".format(exe), tool_path) - repository_ctx.file( - "glibc-hacks/fcntl.map", - content = _fcntl_map, - ) - repository_ctx.file( - "glibc-hacks/glibchack-fcntl.h", - content = _fcntl_h, - ) + fcntl_hack = _glibc_hack("fcntl64", "fcntl@GLIBC_2.2.5") + repository_ctx.file("glibc-hacks/fcntl.map", content = fcntl_hack.mapfile) + repository_ctx.file("glibc-hacks/fcntl.h", content = fcntl_hack.header) + res_search_hack = _glibc_hack("res_search", "__res_search@GLIBC_2.2.5") + repository_ctx.file("glibc-hacks/res_search.map", content = res_search_hack.mapfile) + repository_ctx.file("glibc-hacks/res_search.h", content = res_search_hack.header) zig_repository = repository_rule( attrs = { diff --git a/toolchain/private/cc_toolchains.bzl b/toolchain/private/cc_toolchains.bzl index a935ef5..a9c0fba 100644 --- a/toolchain/private/cc_toolchains.bzl +++ b/toolchain/private/cc_toolchains.bzl @@ -17,9 +17,6 @@ def declare_cc_toolchains(os, zig_sdk_path): zigtarget = target_config.zigtarget cxx_builtin_include_directories = [] - for d in getattr(target_config, "toplevel_include", []): - cxx_builtin_include_directories.append(zig_sdk_path + "/" + d) - absolute_tool_paths = {} for name, path in target_config.tool_paths.items() + DEFAULT_TOOL_PATHS: if path[0] == "/": diff --git a/toolchain/private/defs.bzl b/toolchain/private/defs.bzl index 635dde9..cf1caca 100644 --- a/toolchain/private/defs.bzl +++ b/toolchain/private/defs.bzl @@ -121,9 +121,15 @@ def _target_windows(gocpu, zigcpu): def _target_linux_gnu(gocpu, zigcpu, glibc_version): glibc_suffix = "gnu.{}".format(glibc_version) - # https://github.com/ziglang/zig/issues/5882#issuecomment-888250676 - # fcntl_hack is only required for glibc 2.27 or less. - fcntl_hack = glibc_version < "2.28" + compiler_extra_includes = [] + linker_version_scripts = [] + if glibc_version < "2.28": + # https://github.com/ziglang/zig/issues/5882#issuecomment-888250676 + compiler_extra_includes.append("glibc-hacks/fcntl.h") + linker_version_scripts.append("glibc-hacks/fcntl.map") + if glibc_version < "2.34": + compiler_extra_includes.append("glibc-hacks/res_search.h") + linker_version_scripts.append("glibc-hacks/res_search.map") return struct( gotarget = "linux_{}_{}".format(gocpu, glibc_suffix), @@ -137,9 +143,8 @@ def _target_linux_gnu(gocpu, zigcpu, glibc_version): (["libc/include/{}-linux-any".format(zigcpu)] if zigcpu != "x86_64" else []) + [ "libc/include/any-linux-any", ] + _INCLUDE_TAIL, - toplevel_include = ["glibc-hacks"] if fcntl_hack else [], - compiler_extra_includes = ["glibc-hacks/glibchack-fcntl.h"] if fcntl_hack else [], - linker_version_scripts = ["glibc-hacks/fcntl.map"] if fcntl_hack else [], + compiler_extra_includes = compiler_extra_includes, + linker_version_scripts = linker_version_scripts, dynamic_library_linkopts = [], copts = [], libc = "glibc",