diff --git a/.bazelrc b/.bazelrc index e85840df..760a273d 100644 --- a/.bazelrc +++ b/.bazelrc @@ -18,8 +18,8 @@ build --workspace_status_command=$(pwd)/workspace_status.sh # To update these lines, execute # `bazel run @rules_bazel_integration_test//tools:update_deleted_packages` # docs: https://bazel.build/reference/command-line-reference#flag--deleted_packages -build --deleted_packages=e2e/workspace,e2e/workspace/bazel_builtin,e2e/workspace/c-sources,e2e/workspace/canonical-name-module,e2e/workspace/canonical-name-module/other,e2e/workspace/cc-dependencies,e2e/workspace/cc-dependencies/shared-library,e2e/workspace/cc-dependencies/static-library,e2e/workspace/cc-dependencies/static-library-cdeps,e2e/workspace/configure-mode,e2e/workspace/configure-target,e2e/workspace/configure-threaded,e2e/workspace/configure-use_cc_common_link,e2e/workspace/configure-use_cc_common_link/shared-library,e2e/workspace/configure-use_cc_common_link/static-library,e2e/workspace/configure-version,e2e/workspace/data-dependencies,e2e/workspace/embed-file,e2e/workspace/env-attr,e2e/workspace/import-name-attr,e2e/workspace/include-dependencies,e2e/workspace/include-dependencies/zig-include,e2e/workspace/include-dependencies/zig-include-define,e2e/workspace/include-dependencies/zig-include-isystem,e2e/workspace/include-dependencies/zig-std-include,e2e/workspace/link-dependencies,e2e/workspace/link-dependencies/shared-library,e2e/workspace/link-dependencies/static-library,e2e/workspace/linker-script,e2e/workspace/linkopts-attr,e2e/workspace/location-expansion,e2e/workspace/multiple-sources-and-packages-test,e2e/workspace/multiple-sources-binary,e2e/workspace/root-module-from-single-dependency,e2e/workspace/runfiles-library,e2e/workspace/runfiles-library/dependency,e2e/workspace/runfiles-library/dependency/transitive-dependency,e2e/workspace/simple-binary,e2e/workspace/simple-library,e2e/workspace/simple-shared-library,e2e/workspace/simple-test,e2e/workspace/test-runner-attr,e2e/workspace/transitive-zig-modules-binary,e2e/workspace/transitive-zig-modules-binary/hello-world,e2e/workspace/transitive-zig-modules-binary/hello-world/data,e2e/workspace/transitive-zig-modules-binary/hello-world/data/hello,e2e/workspace/transitive-zig-modules-binary/hello-world/data/world,e2e/workspace/transitive-zig-modules-binary/hello-world/io,e2e/workspace/translate-c/transitive-cc-library-zig-binary,e2e/workspace/zig-docs,e2e/workspace/zig-header,e2e/workspace/zig-module-binary,e2e/workspace/zig-module-binary/data,e2e/workspace/zig-module-binary/io,zig/tests/integration_tests/minimal,zig/tests/integration_tests/mirrors,zig/tests/integration_tests/workspace,zig/tests/integration_tests/workspace/custom_interpreter,zig/tests/integration_tests/workspace/env-attr,zig/tests/integration_tests/workspace/runfiles -query --deleted_packages=e2e/workspace,e2e/workspace/bazel_builtin,e2e/workspace/c-sources,e2e/workspace/canonical-name-module,e2e/workspace/canonical-name-module/other,e2e/workspace/cc-dependencies,e2e/workspace/cc-dependencies/shared-library,e2e/workspace/cc-dependencies/static-library,e2e/workspace/cc-dependencies/static-library-cdeps,e2e/workspace/configure-mode,e2e/workspace/configure-target,e2e/workspace/configure-threaded,e2e/workspace/configure-use_cc_common_link,e2e/workspace/configure-use_cc_common_link/shared-library,e2e/workspace/configure-use_cc_common_link/static-library,e2e/workspace/configure-version,e2e/workspace/data-dependencies,e2e/workspace/embed-file,e2e/workspace/env-attr,e2e/workspace/import-name-attr,e2e/workspace/include-dependencies,e2e/workspace/include-dependencies/zig-include,e2e/workspace/include-dependencies/zig-include-define,e2e/workspace/include-dependencies/zig-include-isystem,e2e/workspace/include-dependencies/zig-std-include,e2e/workspace/link-dependencies,e2e/workspace/link-dependencies/shared-library,e2e/workspace/link-dependencies/static-library,e2e/workspace/linker-script,e2e/workspace/linkopts-attr,e2e/workspace/location-expansion,e2e/workspace/multiple-sources-and-packages-test,e2e/workspace/multiple-sources-binary,e2e/workspace/root-module-from-single-dependency,e2e/workspace/runfiles-library,e2e/workspace/runfiles-library/dependency,e2e/workspace/runfiles-library/dependency/transitive-dependency,e2e/workspace/simple-binary,e2e/workspace/simple-library,e2e/workspace/simple-shared-library,e2e/workspace/simple-test,e2e/workspace/test-runner-attr,e2e/workspace/transitive-zig-modules-binary,e2e/workspace/transitive-zig-modules-binary/hello-world,e2e/workspace/transitive-zig-modules-binary/hello-world/data,e2e/workspace/transitive-zig-modules-binary/hello-world/data/hello,e2e/workspace/transitive-zig-modules-binary/hello-world/data/world,e2e/workspace/transitive-zig-modules-binary/hello-world/io,e2e/workspace/translate-c/transitive-cc-library-zig-binary,e2e/workspace/zig-docs,e2e/workspace/zig-header,e2e/workspace/zig-module-binary,e2e/workspace/zig-module-binary/data,e2e/workspace/zig-module-binary/io,zig/tests/integration_tests/minimal,zig/tests/integration_tests/mirrors,zig/tests/integration_tests/workspace,zig/tests/integration_tests/workspace/custom_interpreter,zig/tests/integration_tests/workspace/env-attr,zig/tests/integration_tests/workspace/runfiles +build --deleted_packages=e2e/workspace,e2e/workspace/bazel_builtin,e2e/workspace/c-sources,e2e/workspace/canonical-name-module,e2e/workspace/canonical-name-module/other,e2e/workspace/cc-dependencies,e2e/workspace/cc-dependencies/shared-library,e2e/workspace/cc-dependencies/static-library,e2e/workspace/cc-dependencies/static-library-cdeps,e2e/workspace/configure-mode,e2e/workspace/configure-target,e2e/workspace/configure-threaded,e2e/workspace/configure-use_cc_common_link,e2e/workspace/configure-use_cc_common_link/shared-library,e2e/workspace/configure-use_cc_common_link/static-library,e2e/workspace/configure-version,e2e/workspace/data-dependencies,e2e/workspace/embed-file,e2e/workspace/env-attr,e2e/workspace/import-name-attr,e2e/workspace/include-dependencies,e2e/workspace/include-dependencies/zig-include,e2e/workspace/include-dependencies/zig-include-define,e2e/workspace/include-dependencies/zig-include-isystem,e2e/workspace/include-dependencies/zig-std-include,e2e/workspace/link-dependencies,e2e/workspace/link-dependencies/shared-library,e2e/workspace/link-dependencies/static-library,e2e/workspace/linker-script,e2e/workspace/linkopts-attr,e2e/workspace/location-expansion,e2e/workspace/multiple-sources-and-packages-test,e2e/workspace/multiple-sources-binary,e2e/workspace/root-module-from-single-dependency,e2e/workspace/runfiles-library,e2e/workspace/runfiles-library/dependency,e2e/workspace/runfiles-library/dependency/transitive-dependency,e2e/workspace/simple-binary,e2e/workspace/simple-cmake-library,e2e/workspace/simple-library,e2e/workspace/simple-shared-library,e2e/workspace/simple-test,e2e/workspace/test-runner-attr,e2e/workspace/toolchain-glibc-version,e2e/workspace/transitive-zig-modules-binary,e2e/workspace/transitive-zig-modules-binary/hello-world,e2e/workspace/transitive-zig-modules-binary/hello-world/data,e2e/workspace/transitive-zig-modules-binary/hello-world/data/hello,e2e/workspace/transitive-zig-modules-binary/hello-world/data/world,e2e/workspace/transitive-zig-modules-binary/hello-world/io,e2e/workspace/translate-c/transitive-cc-library-zig-binary,e2e/workspace/zig-docs,e2e/workspace/zig-header,e2e/workspace/zig-module-binary,e2e/workspace/zig-module-binary/data,e2e/workspace/zig-module-binary/io,zig/tests/integration_tests/minimal,zig/tests/integration_tests/mirrors,zig/tests/integration_tests/workspace,zig/tests/integration_tests/workspace/custom_interpreter,zig/tests/integration_tests/workspace/env-attr,zig/tests/integration_tests/workspace/runfiles +query --deleted_packages=e2e/workspace,e2e/workspace/bazel_builtin,e2e/workspace/c-sources,e2e/workspace/canonical-name-module,e2e/workspace/canonical-name-module/other,e2e/workspace/cc-dependencies,e2e/workspace/cc-dependencies/shared-library,e2e/workspace/cc-dependencies/static-library,e2e/workspace/cc-dependencies/static-library-cdeps,e2e/workspace/configure-mode,e2e/workspace/configure-target,e2e/workspace/configure-threaded,e2e/workspace/configure-use_cc_common_link,e2e/workspace/configure-use_cc_common_link/shared-library,e2e/workspace/configure-use_cc_common_link/static-library,e2e/workspace/configure-version,e2e/workspace/data-dependencies,e2e/workspace/embed-file,e2e/workspace/env-attr,e2e/workspace/import-name-attr,e2e/workspace/include-dependencies,e2e/workspace/include-dependencies/zig-include,e2e/workspace/include-dependencies/zig-include-define,e2e/workspace/include-dependencies/zig-include-isystem,e2e/workspace/include-dependencies/zig-std-include,e2e/workspace/link-dependencies,e2e/workspace/link-dependencies/shared-library,e2e/workspace/link-dependencies/static-library,e2e/workspace/linker-script,e2e/workspace/linkopts-attr,e2e/workspace/location-expansion,e2e/workspace/multiple-sources-and-packages-test,e2e/workspace/multiple-sources-binary,e2e/workspace/root-module-from-single-dependency,e2e/workspace/runfiles-library,e2e/workspace/runfiles-library/dependency,e2e/workspace/runfiles-library/dependency/transitive-dependency,e2e/workspace/simple-binary,e2e/workspace/simple-cmake-library,e2e/workspace/simple-library,e2e/workspace/simple-shared-library,e2e/workspace/simple-test,e2e/workspace/test-runner-attr,e2e/workspace/toolchain-glibc-version,e2e/workspace/transitive-zig-modules-binary,e2e/workspace/transitive-zig-modules-binary/hello-world,e2e/workspace/transitive-zig-modules-binary/hello-world/data,e2e/workspace/transitive-zig-modules-binary/hello-world/data/hello,e2e/workspace/transitive-zig-modules-binary/hello-world/data/world,e2e/workspace/transitive-zig-modules-binary/hello-world/io,e2e/workspace/translate-c/transitive-cc-library-zig-binary,e2e/workspace/zig-docs,e2e/workspace/zig-header,e2e/workspace/zig-module-binary,e2e/workspace/zig-module-binary/data,e2e/workspace/zig-module-binary/io,zig/tests/integration_tests/minimal,zig/tests/integration_tests/mirrors,zig/tests/integration_tests/workspace,zig/tests/integration_tests/workspace/custom_interpreter,zig/tests/integration_tests/workspace/env-attr,zig/tests/integration_tests/workspace/runfiles # Load any settings specific to the current user. # Place settings that should affect the integration tests into `.bazelrc.ic.user`. diff --git a/e2e/workspace/MODULE.bazel b/e2e/workspace/MODULE.bazel index 27023e60..84341d1b 100644 --- a/e2e/workspace/MODULE.bazel +++ b/e2e/workspace/MODULE.bazel @@ -38,3 +38,9 @@ local_path_override( module_name = "runfiles_library_transitive_dependency", path = "runfiles-library/dependency/transitive-dependency", ) + +bazel_dep( + name = "rules_foreign_cc", + version = "0.15.0", + dev_dependency = True, +) diff --git a/e2e/workspace/simple-cmake-library/BUILD.bazel b/e2e/workspace/simple-cmake-library/BUILD.bazel new file mode 100644 index 00000000..81f50084 --- /dev/null +++ b/e2e/workspace/simple-cmake-library/BUILD.bazel @@ -0,0 +1,37 @@ +load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake") +load("@rules_zig//zig:defs.bzl", "zig_binary") + +# -------------------- +# external library using foreign_cc +filegroup( + name = "c-hello-srcs", + srcs = glob(["external_library/**"]), +) + +cmake( + name = "c-hello", + lib_source = ":c-hello-srcs", + out_include_dir = "hello", + out_shared_libs = select({ + "@platforms//os:macos": ["libhello.dylib"], + "//conditions:default": ["libhello.so"], + }), +) +# -------------------- +# equivalent implementation with cc_rules +# cc_library( +# name = "c-hello", +# srcs = ["external_library/hello.c"], +# hdrs = ["external_library/hello.h"], +# includes = ["external_library"], +# ) +# -------------------- + +zig_binary( + name = "main", + main = "main.zig", + deps = [ + ":c-hello", + "@rules_zig//zig/lib:libc", + ], +) diff --git a/e2e/workspace/simple-cmake-library/external_library/CMakeLists.txt b/e2e/workspace/simple-cmake-library/external_library/CMakeLists.txt new file mode 100644 index 00000000..c08a7156 --- /dev/null +++ b/e2e/workspace/simple-cmake-library/external_library/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.20) +project(hello) + +add_library(hello SHARED hello.c) +target_include_directories(hello PRIVATE ".") + + +install(TARGETS hello EXPORT helloTargets + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" +) + +install(FILES hello.h + DESTINATION "hello/" +) + diff --git a/e2e/workspace/simple-cmake-library/external_library/hello.c b/e2e/workspace/simple-cmake-library/external_library/hello.c new file mode 100644 index 00000000..475b6826 --- /dev/null +++ b/e2e/workspace/simple-cmake-library/external_library/hello.c @@ -0,0 +1,7 @@ +#include "hello.h" +#include "stdio.h" + +void hello() +{ + printf("Hello World from hello.c!\n"); +} diff --git a/e2e/workspace/simple-cmake-library/external_library/hello.h b/e2e/workspace/simple-cmake-library/external_library/hello.h new file mode 100644 index 00000000..af9bb7f2 --- /dev/null +++ b/e2e/workspace/simple-cmake-library/external_library/hello.h @@ -0,0 +1,3 @@ +#pragma once + +void hello(); diff --git a/e2e/workspace/simple-cmake-library/main.zig b/e2e/workspace/simple-cmake-library/main.zig new file mode 100644 index 00000000..ee966956 --- /dev/null +++ b/e2e/workspace/simple-cmake-library/main.zig @@ -0,0 +1,7 @@ +const c = @cImport({ + @cInclude("hello.h"); +}); + +pub fn main() !void { + c.hello(); +} diff --git a/e2e/workspace/toolchain-glibc-version/BUILD.bazel b/e2e/workspace/toolchain-glibc-version/BUILD.bazel new file mode 100644 index 00000000..ff29fa79 --- /dev/null +++ b/e2e/workspace/toolchain-glibc-version/BUILD.bazel @@ -0,0 +1,78 @@ +load("@bazel_skylib//rules:diff_test.bzl", "diff_test") +load("@rules_zig//zig:defs.bzl", "zig_binary", "zig_configure_binary", "zig_test") +load("@rules_zig//zig:toolchain.bzl", "zig_target_toolchain") + +platform( + name = "x86_64-linux-gnu-2.25", + constraint_values = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ":gnu-2.25", + ], +) + +constraint_value( + name = "gnu-2.25", + constraint_setting = "@rules_zig//zig/platforms/abi:abi", + visibility = ["//visibility:public"], +) + +zig_target_toolchain( + name = "glibc-2.25", + target = "x86_64-linux-gnu.2.25", +) + +toolchain( + name = "glibc_2.25_toolchain", + target_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ":gnu-2.25", + ], + toolchain = ":glibc-2.25", + toolchain_type = "@rules_zig//zig/target:toolchain_type", +) + +zig_binary( + name = "main-unconfigured", + main = "main.zig", + target_compatible_with = ["@platforms//os:linux"], + deps = [ + "@rules_zig//zig/lib:libc", + ], +) + +zig_configure_binary( + name = "main-2.25", + actual = ":main-unconfigured", + extra_toolchains = [":glibc_2.25_toolchain"], + target = ":x86_64-linux-gnu-2.25", + target_compatible_with = ["@platforms//os:linux"], +) + +zig_test( + name = "fallback_glibc.2.17", + size = "small", + main = "main.zig", + target_compatible_with = ["@platforms//os:linux"], + visibility = ["//zig/tests:__pkg__"], + deps = [ + "@rules_zig//zig/lib:libc", + ], +) + +genrule( + name = "main-2.25-glibc-version", + outs = ["main-2.25-glibc-version.actual"], + cmd = "$(execpath :main-2.25) |& grep compile-time > $(OUTS)", + target_compatible_with = ["@platforms//os:linux"], + tools = [":main-2.25"], +) + +diff_test( + name = "main-2.25-glibc-version-test", + size = "small", + file1 = ":main-2.25-glibc-version.expected", + file2 = ":main-2.25-glibc-version.actual", + target_compatible_with = ["@platforms//os:linux"], +) diff --git a/e2e/workspace/toolchain-glibc-version/README.md b/e2e/workspace/toolchain-glibc-version/README.md new file mode 100644 index 00000000..338c1aab --- /dev/null +++ b/e2e/workspace/toolchain-glibc-version/README.md @@ -0,0 +1,22 @@ +By default, `rules_zig` targets a "lowest common denominator" glibc version to ensure broad compatibility. You can find the discussion behind this choice in [this PR](https://github.com/aherrmann/rules_zig/pull/299) and see the implementation [here](https://github.com/aherrmann/rules_zig/blob/main/zig/target/BUILD.bazel#L56). + +This example project demonstrates how to manually configure and use a specific glibc version with `rules_zig`. + + +```bash +# navigate to the workspace directory: +cd e2e/workspace + +# run with the default glibc version +# "main-unconfigured" uses the default glibc version +# note that the runtime glibc version depends on the execution environment +bazel run toolchain-glibc-version:main-unconfigured + +# run with a custom glibc version +# "main-2.25" uses a custom toolchain using glibc 2.25 +bazel run toolchain-glibc-version:main-2.25 + +# verify the fallback behaviour +# this shows the "unconfigured" zig_test still uses the fallback version +bazel test toolchain-glibc-version:fallback_glibc.2.17 +``` diff --git a/e2e/workspace/toolchain-glibc-version/main-2.25-glibc-version.expected b/e2e/workspace/toolchain-glibc-version/main-2.25-glibc-version.expected new file mode 100644 index 00000000..7c6a445d --- /dev/null +++ b/e2e/workspace/toolchain-glibc-version/main-2.25-glibc-version.expected @@ -0,0 +1 @@ +glibc compile-time version: 2.25 diff --git a/e2e/workspace/toolchain-glibc-version/main.zig b/e2e/workspace/toolchain-glibc-version/main.zig new file mode 100644 index 00000000..595407e8 --- /dev/null +++ b/e2e/workspace/toolchain-glibc-version/main.zig @@ -0,0 +1,16 @@ +const std = @import("std"); +const c = @cImport({ + @cInclude("features.h"); +}); + +extern "c" fn gnu_get_libc_version() [*c]const u8; + +pub fn main() !void { + std.debug.print("glibc compile-time version: {d}.{d}\n", .{ c.__GLIBC__, c.__GLIBC_MINOR__ }); + std.debug.print("glibc runtime version: {s}\n", .{gnu_get_libc_version()}); +} + +test "test_fallback_glibc.2.17" { + try std.testing.expectEqual(2, c.__GLIBC__); + try std.testing.expectEqual(17, c.__GLIBC_MINOR__); +}