From 5356fedd4b6079851b51db27077bf84c7bab16a4 Mon Sep 17 00:00:00 2001 From: oquenchil <23365806+oquenchil@users.noreply.github.com> Date: Thu, 10 Feb 2022 14:48:54 +0100 Subject: [PATCH] Cherrypicks for experimental cc_shared_library (#14773) * Cherrypicks for experimental cc_shared_library * Adds cc_shared_library builtin test sources to //:srcs --- .bazelci/presubmit.yml | 6 + BUILD | 1 + .../bazel/rules/cpp/BazelCcLibraryRule.java | 1 + .../lib/bazel/rules/cpp/BazelCcModule.java | 4 + .../build/lib/rules/cpp/CcBinary.java | 52 ++- .../build/lib/rules/cpp/CcModule.java | 10 + .../cpp/BazelCcModuleApi.java | 27 +- .../builtins_bzl/common/cc/cc_helper.bzl | 130 +++++- .../cc/experimental_cc_shared_library.bzl | 160 ++++++-- .../builtins_bzl/common/cc/semantics.bzl | 81 ++-- src/main/starlark/tests/builtins_bzl/BUILD | 31 ++ .../tests/builtins_bzl/builtin_test_setup.sh | 50 +++ .../test_cc_shared_library/BUILD.builtin_test | 383 ++++++++++++++++++ .../test_cc_shared_library/a_suffix.cc | 16 + .../test_cc_shared_library/a_suffix.h | 19 + .../additional_script.txt | 0 .../test_cc_shared_library/bar.cc | 16 + .../test_cc_shared_library/bar.h | 19 + .../test_cc_shared_library/bar.lds | 5 + .../test_cc_shared_library/bar2.cc | 16 + .../test_cc_shared_library/bar2.h | 19 + .../test_cc_shared_library/bar3.cc | 16 + .../test_cc_shared_library/bar3.h | 19 + .../test_cc_shared_library/barX.cc | 16 + .../test_cc_shared_library/barX.h | 19 + .../test_cc_shared_library/baz.cc | 16 + .../test_cc_shared_library/baz.h | 19 + .../cc_shared_library_integration_test.sh | 82 ++++ .../direct_so_file_cc_lib.cc | 16 + .../direct_so_file_cc_lib.h | 20 + .../direct_so_file_cc_lib2.cc | 16 + .../direct_so_file_cc_lib2.h | 20 + .../failing_targets/BUILD.builtin_test | 114 ++++++ .../failing_targets/a.cc | 13 + .../failing_targets/b.cc | 13 + .../failing_targets/c.cc | 13 + .../failing_targets/main.cc | 13 + .../test_cc_shared_library/foo.cc | 31 ++ .../test_cc_shared_library/foo.h | 19 + .../test_cc_shared_library/foo.lds | 6 + .../test_cc_shared_library/main.cc | 21 + .../test_cc_shared_library/not_depended_on.cc | 19 + .../test_cc_shared_library/preloaded_dep.cc | 16 + .../test_cc_shared_library/preloaded_dep.h | 19 + .../test_cc_shared_library/quux.cc | 14 + .../test_cc_shared_library/qux.cc | 16 + .../test_cc_shared_library/qux.h | 19 + .../test_cc_shared_library/qux2.cc | 16 + .../test_cc_shared_library/qux2.h | 19 + .../test_cc_shared_library/starlark_tests.bzl | 148 +++++++ .../test_cc_shared_library/testenv.sh | 17 + .../BUILD.builtin_test | 8 + .../test_cc_shared_library2/WORKSPACE | 1 + .../test_cc_shared_library2/bar.cc | 13 + .../test_cc_shared_library2/bar.h | 19 + .../BUILD.builtin_test | 16 + .../test_cc_shared_library3/bar.cc | 13 + .../test_cc_shared_library3/bar.h | 19 + .../tests/builtins_bzl/cc_builtin_tests.sh | 78 ++++ src/test/shell/bazel/BUILD | 1 + 60 files changed, 1940 insertions(+), 79 deletions(-) create mode 100644 src/main/starlark/tests/builtins_bzl/BUILD create mode 100644 src/main/starlark/tests/builtins_bzl/builtin_test_setup.sh create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/BUILD.builtin_test create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/a_suffix.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/a_suffix.h create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/additional_script.txt create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar.h create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar.lds create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar2.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar2.h create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar3.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar3.h create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/barX.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/barX.h create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/baz.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/baz.h create mode 100755 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/cc_shared_library_integration_test.sh create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib.h create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib2.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib2.h create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/BUILD.builtin_test create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/a.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/b.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/c.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/main.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/foo.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/foo.h create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/foo.lds create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/main.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/not_depended_on.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/preloaded_dep.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/preloaded_dep.h create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/quux.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux.h create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux2.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux2.h create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/starlark_tests.bzl create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/testenv.sh create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library2/BUILD.builtin_test create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library2/WORKSPACE create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library2/bar.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library2/bar.h create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3/BUILD.builtin_test create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3/bar.cc create mode 100644 src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3/bar.h create mode 100755 src/main/starlark/tests/builtins_bzl/cc_builtin_tests.sh diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 90e2bdd14faecd..845c7b53f69871 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -26,6 +26,7 @@ tasks: test_targets: - "//scripts/..." - "//src/java_tools/..." + - "//src/main/starlark/tests/builtins_bzl/..." - "//src/test/..." - "//src/tools/execlog/..." - "//src/tools/singlejar/..." @@ -68,6 +69,7 @@ tasks: test_targets: - "//scripts/..." - "//src/java_tools/..." + - "//src/main/starlark/tests/builtins_bzl/..." - "//src/test/..." - "//src/tools/execlog/..." - "//src/tools/singlejar/..." @@ -130,6 +132,7 @@ tasks: test_targets: - "//scripts/..." - "//src/java_tools/..." + - "//src/main/starlark/tests/builtins_bzl/..." - "//src/test/..." - "//src/tools/execlog/..." - "//src/tools/singlejar/..." @@ -166,6 +169,7 @@ tasks: - "--test_env=REMOTE_NETWORK_ADDRESS=bazel.build:80" test_targets: - "//scripts/..." + - "//src/main/starlark/tests/builtins_bzl/..." - "//src/test/..." - "//src/tools/execlog/..." - "//src/tools/singlejar/..." @@ -203,6 +207,7 @@ tasks: - "--test_env=TEST_REPOSITORY_HOME=$OUTPUT_BASE/external" test_targets: - "//src:embedded_tools_size_test" + - "//src/main/starlark/tests/builtins_bzl/..." - "//src/test/cpp/..." - "//src/test/java/com/google/devtools/build/android/..." - "//src/test/java/com/google/devtools/build/lib/..." @@ -269,6 +274,7 @@ tasks: test_targets: - "//scripts/..." - "//src/java_tools/..." + - "//src/main/starlark/tests/builtins_bzl/..." - "//src/test/..." - "//src/tools/execlog/..." - "//src/tools/singlejar/..." diff --git a/BUILD b/BUILD index a8eb1e7d40bcd3..8bc5528476a8d3 100644 --- a/BUILD +++ b/BUILD @@ -27,6 +27,7 @@ filegroup( "//src:srcs", "//tools:srcs", "//third_party:srcs", + "//src/main/starlark/tests/builtins_bzl:srcs", ] + glob([".bazelci/*"]) + [".bazelrc"], visibility = ["//src/test/shell/bazel:__pkg__"], ) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcLibraryRule.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcLibraryRule.java index a7c68e248693fa..a26da04ec94b9c 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcLibraryRule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcLibraryRule.java @@ -63,6 +63,7 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) attr("implementation_deps", LABEL_LIST) .allowedFileTypes(FileTypeSet.NO_FILE) .mandatoryProviders(CcInfo.PROVIDER.id())) + .advertiseStarlarkProvider(CcInfo.PROVIDER.id()) .build(); } diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcModule.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcModule.java index 1ff392b1bd0ab1..746c3e35eb9a0e 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcModule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcModule.java @@ -182,7 +182,9 @@ public CcLinkingOutputs link( Object wholeArchive, Object additionalLinkstampDefines, Object onlyForDynamicLibs, + Object mainOutput, Object linkerOutputs, + Object winDefFile, StarlarkThread thread) throws InterruptedException, EvalException { return super.link( @@ -208,7 +210,9 @@ public CcLinkingOutputs link( wholeArchive, additionalLinkstampDefines, onlyForDynamicLibs, + mainOutput, linkerOutputs, + winDefFile, thread); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java index b96470b0a14faf..06925611e977a9 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java @@ -259,6 +259,11 @@ private static RuntimeFiles collectRunfiles( .getAllArtifacts() .toList()); } + for (TransitiveInfoCollection transitiveInfoCollection : + ruleContext.getPrerequisites("dynamic_deps")) { + builder.merge( + transitiveInfoCollection.getProvider(RunfilesProvider.class).getDefaultRunfiles()); + } // Add the C++ runtime libraries if linking them dynamically. if (linkingMode == Link.LinkingMode.DYNAMIC) { try { @@ -557,6 +562,7 @@ public static void init( Pair ccLinkingOutputsAndCcLinkingInfo = createTransitiveLinkingActions( ruleContext, + ruleBuilder, ccToolchain, featureConfiguration, fdoContext, @@ -741,8 +747,9 @@ public static void init( .addNativeDeclaredProvider(ccLauncherInfo); } - public static Pair createTransitiveLinkingActions( + private static Pair createTransitiveLinkingActions( RuleContext ruleContext, + RuleConfiguredTargetBuilder ruleBuilder, CcToolchainProvider ccToolchain, FeatureConfiguration featureConfiguration, FdoContext fdoContext, @@ -869,7 +876,7 @@ public static Pair createTransitiveLinkingActi CcLinkingContext ccLinkingContext = ruleContext.attributes().isAttributeValueExplicitlySpecified("dynamic_deps") ? filterLibrariesThatAreLinkedDynamically( - ruleContext, ccInfo.getCcLinkingContext(), cppSemantics) + ruleContext, ruleBuilder, ccInfo.getCcLinkingContext(), cppSemantics) : ccInfo.getCcLinkingContext(); if (ruleContext.hasErrors()) { return null; @@ -1426,8 +1433,11 @@ private static ImmutableList filterInputs( graphStructureAspectNodes.add(nodeInfo); } } - graphStructureAspectNodes.add( - CppHelper.mallocForTarget(ruleContext).getProvider(GraphNodeInfo.class)); + GraphNodeInfo mallocNodeInfo = + CppHelper.mallocForTarget(ruleContext).getProvider(GraphNodeInfo.class); + if (mallocNodeInfo != null) { + graphStructureAspectNodes.add(mallocNodeInfo); + } Set canBeLinkedDynamically = new HashSet<>(); for (CcLinkingContext.LinkerInput linkerInput : linkerInputs) { @@ -1533,7 +1543,8 @@ public static ImmutableMap buildLinkOnceStaticLibsMap( } private static CcLinkingContext filterLibrariesThatAreLinkedDynamically( - RuleContext ruleContext, CcLinkingContext ccLinkingContext, CppSemantics cppSemantics) { + RuleContext ruleContext, RuleConfiguredTargetBuilder ruleBuilder, + CcLinkingContext ccLinkingContext, CppSemantics cppSemantics) { ImmutableList mergedCcSharedLibraryInfos = mergeCcSharedLibraryInfos(ruleContext, cppSemantics); ImmutableList preloadedDeps = @@ -1548,6 +1559,37 @@ private static CcLinkingContext filterLibrariesThatAreLinkedDynamically( buildExportsMapFromOnlyDynamicDeps(ruleContext, mergedCcSharedLibraryInfos); ImmutableList staticLinkerInputs = filterInputs(ruleContext, ccLinkingContext, exportsMap, linkOnceStaticLibsMap); + if (ruleContext + .getConfiguration() + .getFragment(CppConfiguration.class) + .experimentalCcSharedLibraryDebug()) { + ImmutableList.Builder debugLinkerInputsFile = ImmutableList.builder(); + debugLinkerInputsFile.add("Owner: " + ruleContext.getLabel()); + for (CcLinkingContext.LinkerInput linkerInput : + Iterables.concat(staticLinkerInputs, preloadedDeps)) { + debugLinkerInputsFile.add(linkerInput.getOwner().toString()); + } + Artifact linkOnceStaticLibsDebugFile = + ruleContext.getBinArtifact( + ruleContext.getLabel().getName() + "_link_once_static_libs.txt"); + ruleContext.registerAction( + FileWriteAction.create( + ruleContext, + linkOnceStaticLibsDebugFile, + Joiner.on("\n").join(debugLinkerInputsFile.build()), + false)); + NestedSetBuilder transitiveDebugFiles = NestedSetBuilder.stableOrder(); + for (TransitiveInfoCollection dep : ruleContext.getPrerequisites("dynamic_deps")) { + transitiveDebugFiles.addTransitive( + dep.get(OutputGroupInfo.STARLARK_CONSTRUCTOR).getOutputGroup("rule_impl_debug_files")); + } + ruleBuilder.addOutputGroup( + "rule_impl_debug_files", + NestedSetBuilder.stableOrder() + .add(linkOnceStaticLibsDebugFile) + .addTransitive(transitiveDebugFiles.build()) + .build()); + } return createLinkingContextWithDynamicDependencies( staticLinkerInputs, preloadedDeps, exportsMap.values()); diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java index 3ea6a3b321fa53..6d546f7d5de8ef 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java @@ -2300,12 +2300,17 @@ protected CcLinkingOutputs link( Object wholeArchiveObject, Object additionalLinkstampDefines, Object onlyForDynamicLibsObject, + Object mainOutputObject, Object linkerOutputsObject, + Object winDefFile, StarlarkThread thread) throws InterruptedException, EvalException { // TODO(bazel-team): Rename always_link to alwayslink before delisting. Also it looks like the // suffix parameter can be removed since we can use `name` for the same thing. if (checkObjectsBound( + // TODO(b/205690414): Keep linkedArtifactNameSuffixObject protected. Use cases that are + // passing the suffix should be migrated to using mainOutput instead where the suffix is + // taken into account. Then this parameter should be removed. linkedArtifactNameSuffixObject, neverLinkObject, alwaysLinkObject, @@ -2313,6 +2318,8 @@ protected CcLinkingOutputs link( nativeDepsObject, wholeArchiveObject, additionalLinkstampDefines, + mainOutputObject, + winDefFile, onlyForDynamicLibsObject)) { checkPrivateStarlarkificationAllowlist(thread); } @@ -2327,6 +2334,7 @@ protected CcLinkingOutputs link( convertFromNoneable(starlarkCcToolchainProvider, null); FeatureConfigurationForStarlark featureConfiguration = convertFromNoneable(starlarkFeatureConfiguration, null); + Artifact mainOutput = convertFromNoneable(mainOutputObject, null); Label label = getCallerLabel(actions, name); FdoContext fdoContext = ccToolchainProvider.getFdoContext(); LinkTargetType dynamicLinkTargetType = null; @@ -2406,6 +2414,8 @@ protected CcLinkingOutputs link( && actualFeatureConfiguration.isEnabled(CppRuleClasses.TARGETS_WINDOWS) && CppHelper.useInterfaceSharedLibraries( cppConfiguration, ccToolchainProvider, actualFeatureConfiguration)) + .setDefFile(convertFromNoneable(winDefFile, null)) + .setLinkerOutputArtifact(convertFromNoneable(mainOutput, null)) .addLinkerOutputs(linkerOutputs); if (staticLinkTargetType != null) { helper.setShouldCreateDynamicLibrary(false).setStaticLinkType(staticLinkTargetType); diff --git a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/BazelCcModuleApi.java b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/BazelCcModuleApi.java index 81403da6b3ed52..6d4abf0576ca84 100644 --- a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/BazelCcModuleApi.java +++ b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/BazelCcModuleApi.java @@ -243,7 +243,7 @@ public interface BazelCcModuleApi< name = "name", doc = "This is used for naming the output artifacts of actions created by this " - + "method.", + + "method. See also the `main_output` arg.", positional = false, named = true), @Param( @@ -546,13 +546,34 @@ Tuple compile( documented = false, allowedTypes = {@ParamType(type = Boolean.class)}, defaultValue = "unbound"), + @Param( + name = "main_output", + doc = + "Name of the main output artifact that will be produced by the linker. " + + "Only set this if the default name generation does not match you needs " + + "For output_type=executable, this is the final executable filename. " + + "For output_type=dynamic_library, this is the shared library filename. " + + "If not specified, then one will be computed based on `name` and " + + "`output_type`", + positional = false, + named = true, + documented = false, + defaultValue = "unbound", + allowedTypes = {@ParamType(type = FileApi.class), @ParamType(type = NoneType.class)}), @Param( name = "additional_outputs", doc = "For additional outputs to the linking action, e.g.: map files.", positional = false, named = true, allowedTypes = {@ParamType(type = Sequence.class)}, - defaultValue = "unbound") + defaultValue = "unbound"), + @Param( + name = "win_def_file", + documented = false, + positional = false, + named = true, + defaultValue = "unbound"), + }) LinkingOutputsT link( StarlarkActionFactoryT starlarkActionFactoryApi, @@ -577,7 +598,9 @@ LinkingOutputsT link( Object wholeArchive, Object additionalLinkstampDefines, Object onlyForDynamicLibs, + Object mainOutput, Object linkerOutputs, + Object winDefFile, StarlarkThread thread) throws InterruptedException, EvalException; diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl index 918a41ff6b3bb6..0e4037cc9649c5 100644 --- a/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl @@ -20,11 +20,34 @@ CcInfo = _builtins.toplevel.CcInfo cc_common = _builtins.toplevel.cc_common cc_internal = _builtins.internal.cc_internal +def _check_src_extension(file, allowed_src_files): + extension = "." + file.extension + if _matches_extension(extension, allowed_src_files) or _is_shared_library_extension_valid(file.path): + return True + return False + +def _check_srcs_extensions(ctx, allowed_src_files, rule_name): + for src in ctx.attr.srcs: + if DefaultInfo in src: + files = src[DefaultInfo].files.to_list() + if len(files) == 1 and files[0].is_source: + if not _check_src_extension(files[0], allowed_src_files) and not files[0].is_directory: + fail("in srcs attribute of {} rule {}: source file '{}' is misplaced here".format(rule_name, ctx.label, str(src.label))) + else: + at_least_one_good = False + for file in files: + if _check_src_extension(file, allowed_src_files) or file.is_directory: + at_least_one_good = True + break + if not at_least_one_good: + fail("'{}' does not produce any {} srcs files".format(str(src.label), rule_name), attr = "srcs") + def _merge_cc_debug_contexts(compilation_outputs, dep_cc_infos): debug_context = cc_common.create_debug_context(compilation_outputs) - debug_contexts = [debug_context] + debug_contexts = [] for dep_cc_info in dep_cc_infos: debug_contexts.append(dep_cc_info.debug_context()) + debug_contexts.append(debug_context) return cc_common.merge_debug_context(debug_contexts) @@ -124,6 +147,48 @@ def _build_output_groups_for_emitting_compile_providers( return output_groups_builder +def _dll_hash_suffix(ctx, feature_configuration): + return cc_internal.dll_hash_suffix(ctx = ctx, feature_configuration = feature_configuration) + +def _gen_empty_def_file(ctx): + trivial_def_file = ctx.actions.declare_file(ctx.label.name + ".gen.empty.def") + ctx.actions.write(trivial_def_file, "", False) + return trivial_def_file + +def _get_windows_def_file_for_linking(ctx, custom_def_file, generated_def_file, feature_configuration): + # 1. If a custom DEF file is specified in win_def_file attribute, use it. + # 2. If a generated DEF file is available and should be used, use it. + # 3. Otherwise, we use an empty DEF file to ensure the import library will be generated. + if custom_def_file != None: + return custom_def_file + elif generated_def_file != None and _should_generate_def_file(ctx, feature_configuration) == True: + return generated_def_file + else: + return _gen_empty_def_file(ctx) + +def _should_generate_def_file(ctx, feature_configuration): + windows_export_all_symbols_enabled = cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "windows_export_all_symbols") + no_windows_export_all_symbols_enabled = cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "no_windows_export_all_symbols") + return windows_export_all_symbols_enabled and (not no_windows_export_all_symbols_enabled) and (ctx.attr.win_def_file == None) + +def _generate_def_file(ctx, def_parser, object_files, dll_name): + def_file = ctx.actions.declare_file(ctx.label.name + ".gen.def") + argv = ctx.actions.args() + argv.add(def_file) + argv.add(dll_name) + for object_file in object_files: + argv.add(object_file.path) + + ctx.actions.run( + mnemonic = "DefParser", + executable = def_parser, + arguments = [argv], + inputs = object_files, + outputs = [def_file], + use_default_shell_env = True, + ) + return def_file + CC_SOURCE = [".cc", ".cpp", ".cxx", ".c++", ".C", ".cu", ".cl"] C_SOURCE = [".c"] OBJC_SOURCE = [".m"] @@ -140,13 +205,13 @@ CC_AND_OBJC.extend(CLIF_OUTPUT_PROTO) CC_HEADER = [".h", ".hh", ".hpp", ".ipp", ".hxx", ".h++", ".inc", ".inl", ".tlh", ".tli", ".H", ".tcc"] ASSESMBLER_WITH_C_PREPROCESSOR = [".S"] -ASSEMBLER = [".s"] +ASSEMBLER = [".s", ".asm"] ARCHIVE = [".a", ".lib"] PIC_ARCHIVE = [".pic.a"] ALWAYSLINK_LIBRARY = [".lo"] ALWAYSLINK_PIC_LIBRARY = [".pic.lo"] SHARED_LIBRARY = [".so", ".dylib", ".dll"] -OBJECT_FILE = [".o"] +OBJECT_FILE = [".o", ".obj"] PIC_OBJECT_FILE = [".pic.o"] extensions = struct( @@ -191,7 +256,8 @@ def _collect_library_hidden_top_level_artifacts( if hasattr(ctx.attr, "deps"): for dep in ctx.attr.deps: if OutputGroupInfo in dep: - artifacts_to_force_builder.append(dep[OutputGroupInfo]["_hidden_top_level_INTERNAL_"]) + if "_hidden_top_level_INTERNAL_" in dep[OutputGroupInfo]: + artifacts_to_force_builder.append(dep[OutputGroupInfo]["_hidden_top_level_INTERNAL_"]) return depset(transitive = artifacts_to_force_builder) @@ -308,21 +374,21 @@ def _is_shared_library_extension_valid(shared_library_name): shared_library_name.endswith(".dylib")): return True - # Validate against the regex "^.+\.so(\.\d\w*)+$" for versioned .so files - parts = shared_library_name.split(".") - if len(parts) == 1: - return False - extension = parts[1] - if extension != "so": - return False - version_parts = parts[2:] - for part in version_parts: - if not part[0].isdigit(): - return False - for c in part[1:].elems(): - if not (c.isalnum() or c == "_"): - return False - return True + # validate agains the regex "^.+\\.((so)|(dylib))(\\.\\d\\w*)+$", + # must match VERSIONED_SHARED_LIBRARY. + for ext in (".so.", ".dylib."): + name, _, version = shared_library_name.rpartition(ext) + if name and version: + version_parts = version.split(".") + for part in version_parts: + if not part[0].isdigit(): + return False + for c in part[1:].elems(): + if not (c.isalnum() or c == "_"): + return False + return True + + return False def _get_providers(deps, provider): providers = [] @@ -347,6 +413,25 @@ def _get_static_mode_params_for_dynamic_library_libraries(libs): linker_inputs.append(lib.dynamic_library) return linker_inputs +def _should_create_per_object_debug_info(feature_configuration, cpp_configuration): + return cpp_configuration.fission_active_for_current_compilation_mode() and \ + cc_common.is_enabled( + feature_configuration = feature_configuration, + feature_name = "per_object_debug_info", + ) + +def _libraries_from_linking_context(linking_context): + libraries = [] + for linker_input in linking_context.linker_inputs.to_list(): + libraries.extend(linker_input.libraries) + return depset(libraries, order = "topological") + +def _additional_inputs_from_linking_context(linking_context): + inputs = [] + for linker_input in linking_context.linker_inputs.to_list(): + inputs.extend(linker_input.additional_inputs) + return depset(inputs, order = "topological") + cc_helper = struct( merge_cc_debug_contexts = _merge_cc_debug_contexts, is_code_coverage_enabled = _is_code_coverage_enabled, @@ -367,4 +452,11 @@ cc_helper = struct( is_compilation_outputs_empty = _is_compilation_outputs_empty, matches_extension = _matches_extension, get_static_mode_params_for_dynamic_library_libraries = _get_static_mode_params_for_dynamic_library_libraries, + should_create_per_object_debug_info = _should_create_per_object_debug_info, + check_srcs_extensions = _check_srcs_extensions, + libraries_from_linking_context = _libraries_from_linking_context, + additional_inputs_from_linking_context = _additional_inputs_from_linking_context, + dll_hash_suffix = _dll_hash_suffix, + get_windows_def_file_for_linking = _get_windows_def_file_for_linking, + generate_def_file = _generate_def_file, ) diff --git a/src/main/starlark/builtins_bzl/common/cc/experimental_cc_shared_library.bzl b/src/main/starlark/builtins_bzl/common/cc/experimental_cc_shared_library.bzl index 0dbf099b9ab5eb..7a8c59d4eaa44a 100644 --- a/src/main/starlark/builtins_bzl/common/cc/experimental_cc_shared_library.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/experimental_cc_shared_library.bzl @@ -20,7 +20,7 @@ rely on this. It requires bazel >1.2 and passing the flag """ load(":common/cc/cc_helper.bzl", "cc_helper") -load(":common/objc/semantics.bzl", "semantics") +load(":common/cc/semantics.bzl", "semantics") CcInfo = _builtins.toplevel.CcInfo cc_common = _builtins.toplevel.cc_common @@ -103,7 +103,8 @@ def _merge_cc_shared_library_infos(ctx): dynamic_deps = [] transitive_dynamic_deps = [] for dep in ctx.attr.dynamic_deps: - if dep[CcSharedLibraryInfo].preloaded_deps != None: + # This error is not relevant for cc_binary. + if not hasattr(ctx.attr, "_cc_binary") and dep[CcSharedLibraryInfo].preloaded_deps != None: fail("{} can only be a direct dependency of a " + " cc_binary because it has " + "preloaded_deps".format(str(dep.label))) @@ -145,18 +146,25 @@ def _build_link_once_static_libs_map(merged_shared_library_infos): link_once_static_libs_map[static_lib] = str(linker_input.owner) return link_once_static_libs_map +def _is_dynamic_only(library_to_link): + if library_to_link.static_library == None and library_to_link.pic_static_library == None: + return True + return False + def _wrap_static_library_with_alwayslink(ctx, feature_configuration, cc_toolchain, linker_input): new_libraries_to_link = [] for old_library_to_link in linker_input.libraries: - # TODO(#5200): This will lose the object files from a library to link. - # Not too bad for the prototype but as soon as the library_to_link - # constructor has object parameters this should be changed. + if _is_dynamic_only(old_library_to_link): + new_libraries_to_link.append(old_library_to_link) + continue new_library_to_link = cc_common.create_library_to_link( actions = ctx.actions, feature_configuration = feature_configuration, cc_toolchain = cc_toolchain, static_library = old_library_to_link.static_library, + objects = old_library_to_link.objects, pic_static_library = old_library_to_link.pic_static_library, + pic_objects = old_library_to_link.pic_objects, alwayslink = True, ) new_libraries_to_link.append(new_library_to_link) @@ -252,6 +260,8 @@ def _filter_inputs( preloaded_deps_direct_labels, ) + precompiled_only_dynamic_libraries = [] + unaccounted_for_libs = [] exports = {} owners_seen = {} for linker_input in dependency_linker_inputs: @@ -267,7 +277,28 @@ def _filter_inputs( fail(owner + " is already linked statically in " + link_once_static_libs_map[owner] + " but not exported") - if owner in direct_exports: + is_direct_export = owner in direct_exports + + dynamic_only_libraries = [] + static_libraries = [] + for library in linker_input.libraries: + if _is_dynamic_only(library): + dynamic_only_libraries.append(library) + else: + static_libraries.append(library) + + if len(dynamic_only_libraries): + if not len(static_libraries): + if is_direct_export: + fail("Do not place libraries which only contain a precompiled dynamic library in roots.") + + precompiled_only_dynamic_libraries.extend(dynamic_only_libraries) + + if not len(static_libraries): + linker_inputs.append(linker_input) + continue + + if is_direct_export: wrapped_library = _wrap_static_library_with_alwayslink( ctx, feature_configuration, @@ -301,10 +332,32 @@ def _filter_inputs( link_once_static_libs.append(owner) linker_inputs.append(linker_input) else: - fail("We can't link " + - str(owner) + " either statically or dynamically") + unaccounted_for_libs.append(linker_input.owner) + + _throw_error_if_unaccounted_libs(unaccounted_for_libs) + return (exports, linker_inputs, link_once_static_libs, precompiled_only_dynamic_libraries) - return (exports, linker_inputs, link_once_static_libs) +def _throw_error_if_unaccounted_libs(unaccounted_for_libs): + if not unaccounted_for_libs: + return + libs_message = [] + different_repos = {} + for unaccounted_lib in unaccounted_for_libs: + different_repos[unaccounted_lib.workspace_name] = True + if len(libs_message) < 10: + libs_message.append(str(unaccounted_lib)) + + if len(unaccounted_for_libs) > 10: + libs_message = "(and " + str(len(unaccounted_for_libs) - 10) + " others)\n" + + static_deps_message = [] + for repo in different_repos: + static_deps_message.append(" \"@" + repo + "//:__subpackages__\",") + + fail("The following libraries cannot be linked either statically or dynamically:\n" + + "\n".join(libs_message) + "\nTo ignore which libraries get linked" + + " statically for now, add the following to 'static_deps':\n" + + "\n".join(static_deps_message)) def _same_package_or_above(label_a, label_b): if label_a.workspace_name != label_b.workspace_name: @@ -332,7 +385,10 @@ def _cc_shared_library_impl(ctx): feature_configuration = cc_common.configure_features( ctx = ctx, cc_toolchain = cc_toolchain, - requested_features = ctx.features, + # This features enables behavior which creates a def file automatically + # for exporting all the symbols in a shared libary on Windows. If a + # custom def file is passed, this behavior doesn't apply. + requested_features = ctx.features + ["windows_export_all_symbols"], unsupported_features = ctx.disabled_features, ) @@ -357,7 +413,7 @@ def _cc_shared_library_impl(ctx): link_once_static_libs_map = _build_link_once_static_libs_map(merged_cc_shared_library_info) - (exports, linker_inputs, link_once_static_libs) = _filter_inputs( + (exports, linker_inputs, link_once_static_libs, precompiled_only_dynamic_libraries) = _filter_inputs( ctx, feature_configuration, cc_toolchain, @@ -372,52 +428,95 @@ def _cc_shared_library_impl(ctx): for user_link_flag in ctx.attr.user_link_flags: user_link_flags.append(ctx.expand_location(user_link_flag, targets = ctx.attr.additional_linker_inputs)) + main_output = None + if ctx.attr.shared_lib_name: + main_output = ctx.actions.declare_file(ctx.attr.shared_lib_name) + + debug_files = [] + exports_debug_file = ctx.actions.declare_file(ctx.label.name + "_exports.txt") + ctx.actions.write(content = "\n".join(["Owner:" + str(ctx.label)] + exports.keys()), output = exports_debug_file) + + link_once_static_libs_debug_file = ctx.actions.declare_file(ctx.label.name + "_link_once_static_libs.txt") + ctx.actions.write(content = "\n".join(["Owner:" + str(ctx.label)] + link_once_static_libs), output = link_once_static_libs_debug_file) + + debug_files.append(exports_debug_file) + debug_files.append(link_once_static_libs_debug_file) + + win_def_file = None + if cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "targets_windows"): + object_files = [] + for linker_input in linking_context.linker_inputs.to_list(): + for library in linker_input.libraries: + if library.pic_static_library != None: + if library.pic_objects != None: + object_files.extend(library.pic_objects) + elif library.static_library != None: + if library.objects != None: + object_files.extend(library.objects) + + def_parser = ctx.file._def_parser + + generated_def_file = None + if def_parser != None: + generated_def_file = cc_helper.generate_def_file(ctx, def_parser, object_files, ctx.label.name) + custom_win_def_file = ctx.file.win_def_file + win_def_file = cc_helper.get_windows_def_file_for_linking(ctx, custom_win_def_file, generated_def_file, feature_configuration) + + additional_inputs = [] + additional_inputs.extend(ctx.files.additional_linker_inputs) linking_outputs = cc_common.link( actions = ctx.actions, feature_configuration = feature_configuration, cc_toolchain = cc_toolchain, linking_contexts = [linking_context], user_link_flags = user_link_flags, - additional_inputs = ctx.files.additional_linker_inputs, + additional_inputs = additional_inputs, name = ctx.label.name, output_type = "dynamic_library", + main_output = main_output, + win_def_file = win_def_file, ) + runfiles_files = [] + if linking_outputs.library_to_link.resolved_symlink_dynamic_library != None: + runfiles_files.append(linking_outputs.library_to_link.resolved_symlink_dynamic_library) + runfiles_files.append(linking_outputs.library_to_link.dynamic_library) runfiles = ctx.runfiles( - files = [linking_outputs.library_to_link.resolved_symlink_dynamic_library], + files = runfiles_files, ) + transitive_debug_files = [] for dep in ctx.attr.dynamic_deps: runfiles = runfiles.merge(dep[DefaultInfo].data_runfiles) + transitive_debug_files.append(dep[OutputGroupInfo].rule_impl_debug_files) for export in ctx.attr.roots: exports[str(export.label)] = True - debug_files = [] - if ctx.fragments.cpp.experimental_cc_shared_library_debug(): - exports_debug_file = ctx.actions.declare_file(ctx.label.name + "_exports.txt") - ctx.actions.write(content = "\n".join(exports.keys()), output = exports_debug_file) - - link_once_static_libs_debug_file = ctx.actions.declare_file(ctx.label.name + "_link_once_static_libs.txt") - ctx.actions.write(content = "\n".join(link_once_static_libs), output = link_once_static_libs_debug_file) - - debug_files.append(exports_debug_file) - debug_files.append(link_once_static_libs_debug_file) - if not ctx.fragments.cpp.experimental_link_static_libraries_once(): link_once_static_libs = [] + library = [] + if linking_outputs.library_to_link.resolved_symlink_dynamic_library != None: + library.append(linking_outputs.library_to_link.resolved_symlink_dynamic_library) + else: + library.append(linking_outputs.library_to_link.dynamic_library) + return [ DefaultInfo( - files = depset([linking_outputs.library_to_link.resolved_symlink_dynamic_library] + debug_files), + files = depset(library), runfiles = runfiles, ), + OutputGroupInfo( + main_shared_library_output = depset(library), + rule_impl_debug_files = depset(direct = debug_files, transitive = transitive_debug_files), + ), CcSharedLibraryInfo( dynamic_deps = merged_cc_shared_library_info, exports = exports.keys(), link_once_static_libs = link_once_static_libs, linker_input = cc_common.create_linker_input( owner = ctx.label, - libraries = depset([linking_outputs.library_to_link]), + libraries = depset([linking_outputs.library_to_link] + precompiled_only_dynamic_libraries), ), preloaded_deps = preloaded_dep_merged_cc_info, ), @@ -426,7 +525,8 @@ def _cc_shared_library_impl(ctx): def _graph_structure_aspect_impl(target, ctx): children = [] - if hasattr(ctx.rule.attr, "deps"): + # For now ignore cases when deps is of type label instead of label_list. + if hasattr(ctx.rule.attr, "deps") and type(ctx.rule.attr.deps) != "Target": for dep in ctx.rule.attr.deps: if GraphNodeInfo in dep: children.append(dep[GraphNodeInfo]) @@ -474,13 +574,16 @@ cc_shared_library = rule( implementation = _cc_shared_library_impl, attrs = { "additional_linker_inputs": attr.label_list(allow_files = True), + "shared_lib_name": attr.string(), "dynamic_deps": attr.label_list(providers = [CcSharedLibraryInfo]), "exports_filter": attr.string_list(), "permissions": attr.label_list(providers = [CcSharedLibraryPermissionsInfo]), "preloaded_deps": attr.label_list(providers = [CcInfo]), + "win_def_file": attr.label(allow_single_file = [".def"]), "roots": attr.label_list(providers = [CcInfo], aspects = [graph_structure_aspect]), "static_deps": attr.string_list(), "user_link_flags": attr.string_list(), + "_def_parser": semantics.get_def_parser(), "_cc_toolchain": attr.label(default = "@" + semantics.get_repo() + "//tools/cpp:current_cc_toolchain"), }, toolchains = ["@" + semantics.get_repo() + "//tools/cpp:toolchain_type"], # copybara-use-repo-external-label @@ -489,3 +592,6 @@ cc_shared_library = rule( ) for_testing_dont_use_check_if_target_under_path = _check_if_target_under_path +merge_cc_shared_library_infos = _merge_cc_shared_library_infos +build_link_once_static_libs_map = _build_link_once_static_libs_map +build_exports_map_from_only_dynamic_deps = _build_exports_map_from_only_dynamic_deps diff --git a/src/main/starlark/builtins_bzl/common/cc/semantics.bzl b/src/main/starlark/builtins_bzl/common/cc/semantics.bzl index f23891a8e6756c..7f1224cb035082 100644 --- a/src/main/starlark/builtins_bzl/common/cc/semantics.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/semantics.bzl @@ -12,47 +12,76 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Semantics for Google cc rules""" +"""Semantics for Bazel cc rules""" + +def _should_create_empty_archive(): + return False + +def _validate_deps(ctx): + pass + +def _validate_attributes(ctx): + pass + +def _determine_headers_checking_mode(ctx): + return "strict" def _get_semantics(): - return _builtins.internal.google_cc_internal.semantics + return _builtins.internal.bazel_cc_internal.semantics + +def _get_stl(): + return attr.label() def _get_repo(): - return "" + return "bazel_tools" def _additional_fragments(): - return ["google_cpp"] - -def _get_licenses_attr(): - if hasattr(attr, "license"): - # TODO(b/182226065): Change to applicable_licenses - return { - # buildifier: disable=attr-license - "licenses": attr.license(), - } - else: - return { - "licenses": attr.string_list(), - } + return [] def _get_distribs_attr(): - return { - "distribs": attr.string_list(), - } + return {} + +def _get_licenses_attr(): + # TODO(b/182226065): Change to applicable_licenses + return {} def _get_loose_mode_in_hdrs_check_allowed_attr(): - return { - "_allowlist_loose_mode_in_hdrs_check_allowed": attr.label( - default = "@//tools/build_defs/cc/whitelists/hdrs_check:loose_mode_in_hdrs_check_allowed", - providers = [_builtins.internal.cc_internal.PackageGroupInfo], - ), - } + return {} + +def _get_def_parser(): + return attr.label( + default = "@bazel_tools//tools/def_parser:def_parser", + allow_single_file = True, + cfg = "exec", + ) + +def _get_grep_includes(): + return attr.label() semantics = struct( + ALLOWED_RULES_IN_DEPS = [ + "cc_library", + "objc_library", + "cc_proto_library", + "cc_import", + ], + ALLOWED_FILES_IN_DEPS = [ + ".ld", + ".lds", + ".ldscript", + ], + ALLOWED_RULES_WITH_WARNINGS_IN_DEPS = [], + validate_deps = _validate_deps, + validate_attributes = _validate_attributes, + determine_headers_checking_mode = _determine_headers_checking_mode, get_semantics = _get_semantics, get_repo = _get_repo, additional_fragments = _additional_fragments, - get_licenses_attr = _get_licenses_attr, get_distribs_attr = _get_distribs_attr, + get_licenses_attr = _get_licenses_attr, get_loose_mode_in_hdrs_check_allowed_attr = _get_loose_mode_in_hdrs_check_allowed_attr, + get_def_parser = _get_def_parser, + get_stl = _get_stl, + should_create_empty_archive = _should_create_empty_archive, + get_grep_includes = _get_grep_includes, ) diff --git a/src/main/starlark/tests/builtins_bzl/BUILD b/src/main/starlark/tests/builtins_bzl/BUILD new file mode 100644 index 00000000000000..f5a6115ed11033 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/BUILD @@ -0,0 +1,31 @@ +licenses(["notice"]) + +filegroup( + name = "srcs", + srcs = glob(["**/**"]), + visibility = [ + "//visibility:public", + ], +) + +filegroup( + name = "cc_builtin_test_files", + srcs = glob(["cc/**/**"]), +) + +sh_test( + name = "cc_builtin_tests", + size = "medium", + srcs = ["cc_builtin_tests.sh"], + data = [ + ":builtin_test_setup", + ":cc_builtin_test_files", + "//src/test/shell/bazel:test-deps", + "@bazel_tools//tools/bash/runfiles", + ], +) + +sh_library( + name = "builtin_test_setup", + srcs = ["builtin_test_setup.sh"], +) diff --git a/src/main/starlark/tests/builtins_bzl/builtin_test_setup.sh b/src/main/starlark/tests/builtins_bzl/builtin_test_setup.sh new file mode 100644 index 00000000000000..7aa98ddd5ae667 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/builtin_test_setup.sh @@ -0,0 +1,50 @@ +#!/bin/bash -eu +# +# Copyright 2016 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This integration test exists so that we can run our Starlark tests +# for cc_import with Bazel built from head. Once the Stararlark +# implementation can rely on release Bazel, we can add the tests directly. + +function setup_tests() { + setup_skylib_support + src=$(get_runfiles_dir $1) + dest="${2:-$1}" + if [ ! -e "$src" ]; then + echo "copy_tests() - $src does not exist" 1>&2; exit 1 + fi + + if [ ! -e "$dest" ]; then + mkdir -p "$(dirname "$dest")" + cp -r "$src" "$dest" + fi + chmod -R ugo+rwx "$dest" + + for file in $(find $dest/* -name *.builtin_test); do + mv $file ${file%%.builtin_test} + done +} + +function get_runfiles_dir() { + src="$TEST_SRCDIR/io_bazel/$1" + #echo $TEST_SRCDIR/io_bazel + #echo $1 + #echo "hi" + if [ -e "$src" ]; then + echo $src + elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then + echo $(grep -m1 "io_bazel/$1" "${RUNFILES_MANIFEST_FILE}" | cut -d' ' -f2 | sed "s|$1.*|$1|") + fi +} diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/BUILD.builtin_test b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/BUILD.builtin_test new file mode 100644 index 00000000000000..8d873edc1e1d40 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/BUILD.builtin_test @@ -0,0 +1,383 @@ +load(":starlark_tests.bzl", "additional_inputs_test", "build_failure_test", "debug_files_test", "linking_suffix_test", "paths_test") + +LINKABLE_MORE_THAN_ONCE = "LINKABLE_MORE_THAN_ONCE" + +licenses(["notice"]) + +package( + default_visibility = ["//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:__subpackages__"], +) + +cc_test( + name = "cc_test", + srcs = ["main.cc"], + dynamic_deps = ["foo_so"], + features = ["-dynamic_link_test_srcs"], + deps = ["foo"], +) + +cc_binary( + name = "binary", + srcs = ["main.cc"], + dynamic_deps = ["foo_so"], + deps = ["foo"], +) + +cc_binary( + name = "binary_with_bar_so_twice", + srcs = ["main.cc"], + dynamic_deps = [ + "foo_so", + "bar_so", + ], + deps = ["foo"], +) + +cc_shared_library( + name = "a_so", + roots = [":a_suffix"], +) + +cc_shared_library( + name = "diamond_so", + dynamic_deps = [":a_so"], + roots = [":qux"], +) + +cc_shared_library( + name = "diamond2_so", + dynamic_deps = [":a_so"], + roots = [":qux2"], +) + +cc_binary( + name = "diamond_inheritance", + srcs = ["main.cc"], + dynamic_deps = [ + ":diamond_so", + ":diamond2_so", + ], + deps = [ + ":a_suffix", + ":foo", + ], +) + +cc_shared_library( + name = "foo_so", + additional_linker_inputs = select({ + "//src/conditions:linux": [ + ":foo.lds", + ":additional_script.txt", + ], + "//conditions:default": []}), + dynamic_deps = ["bar_so"], + preloaded_deps = ["preloaded_dep"], + roots = [ + "baz", + "foo", + "a_suffix", + ], + static_deps = [ + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:qux", + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:qux2", + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:prebuilt", + ], + user_link_flags = select({ + "//src/conditions:linux": [ + "-Wl,-rpath,kittens", + "-Wl,--version-script=$(location :foo.lds)", + "-Wl,--script=$(location :additional_script.txt)", + ], + "//conditions:default": []}), +) + +cc_library( + name = "preloaded_dep", + srcs = ["preloaded_dep.cc"], + hdrs = ["preloaded_dep.h"], +) + +cc_library( + name = "foo", + srcs = ["foo.cc", "direct_so_file_cc_lib2.h"] + + select({ + "//src/conditions:linux": [":renamed_so_file_copy.so"], + "//conditions:default": [], + }), + hdrs = ["foo.h"], + defines = select({ + "//src/conditions:linux": ["IS_LINUX"], + "//conditions:default": [], + }), + deps = [ + "preloaded_dep", + "bar", + "baz", + # Not exported. + "qux", + "qux2", + "prebuilt", + ], +) + +cc_library( + name = "a_suffix", + srcs = ["a_suffix.cc"], + hdrs = ["a_suffix.h"], +) + +cc_library( + name = "baz", + srcs = ["baz.cc"], + hdrs = ["baz.h"], + deps = ["bar3"], +) + +cc_library( + name = "qux", + srcs = ["qux.cc"], + hdrs = ["qux.h"], +) + +cc_library( + name = "qux2", + srcs = ["qux2.cc"], + hdrs = ["qux2.h"], + tags = [LINKABLE_MORE_THAN_ONCE], +) + +config_setting( + name = "is_bazel", + define_values = { + "is_bazel": "true", + }, +) + +cc_shared_library( + name = "bar_so", + additional_linker_inputs = select({ + "//src/conditions:linux": [":bar.lds",], + "//conditions:default": []}), + exports_filter = [ + "bar3", # Exported transitive dependency + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3:bar", + ], + permissions = [ + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3:permissions", + ], + roots = [ + "bar", + "bar2", + ] + select({ + ":is_bazel": ["@test_repo//:bar"], + "//conditions:default": [], + }), + static_deps = [ + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:barX", + "@test_repo//:bar", + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:qux2", + ], + user_link_flags = select({ + "//src/conditions:linux": [ + "-Wl,--version-script=$(location :bar.lds)", + ], + "//conditions:default": [], + }), +) + +cc_library( + name = "barX", + srcs = ["barX.cc"], + hdrs = ["barX.h"], + deps = select({ + ":is_bazel": ["@test_repo//:bar"], + "//conditions:default": [], + }), +) + +cc_library( + name = "bar", + srcs = ["bar.cc"], + hdrs = ["bar.h"], + deps = [ + "barX", + "qux2", + ], +) + +cc_library( + name = "bar2", + srcs = ["bar2.cc"], + hdrs = ["bar2.h"], + deps = ["bar3"], +) + +cc_library( + name = "bar3", + srcs = ["bar3.cc"], + hdrs = ["bar3.h"], + deps = [ + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3:bar", + ], +) + +sh_test( + name = "cc_shared_library_integration_test", + srcs = [ + "cc_shared_library_integration_test.sh", + ], + args = select({ + "//src/conditions:linux": ["linux"], + "//conditions:default": ["non_linux"], + }), + data = [ + "testenv.sh", + ":bar_so", + ":binary", + ":cc_test", + ":debug_files", + ":foo_so", + ] + select({ + ":is_bazel": [ + "@bazel_tools//tools/bash/runfiles", + ], + "//conditions:default": [ + ],}) +) + +filegroup( + name = "debug_files", + srcs = ["binary"], + output_group = "rule_impl_debug_files", +) + +linking_suffix_test( + name = "linking_action_test", + target_under_test = ":foo_so", + # TODO(bazel-team): Support this test on Windows and Mac. + is_linux = select({ + "//src/conditions:linux": True, + "//conditions:default": False}), +) + +additional_inputs_test( + name = "additional_inputs_test", + target_under_test = ":foo_so", + # TODO(bazel-team): Support this test on Windows and Mac. + is_linux = select({ + "//src/conditions:linux": True, + "//conditions:default": False}), +) + +build_failure_test( + name = "link_once_repeated_test", + message = "already linked statically in " + + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:foo_so but not exported.", + target_under_test = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:should_fail_binary", +) + +paths_test( + name = "path_matching_test", +) + +build_failure_test( + name = "export_without_permissions_test", + message = "doesn't have the necessary permissions", + target_under_test = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:permissions_fail_so", +) + +build_failure_test( + name = "forbidden_target_permissions_test", + message = "can only list targets that are in the same package or a sub-package", + target_under_test = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:permissions_fail", +) + +cc_library( + name = "prebuilt", + srcs = [":just_main_output", "direct_so_file_cc_lib.h"], +) + +filegroup( + name = "just_main_output", + srcs = ["direct_so_file"], + output_group = "main_shared_library_output", +) + +cc_shared_library( + name = "direct_so_file", + roots = [ + ":direct_so_file_cc_lib", + ], +) + +genrule( + name = "check_file_named_correctly", + srcs = ["just_main_output_renamed"], + outs = ["renamed_so_file_copy.so"], + cmd = "cp $$(dirname $(location :just_main_output_renamed))/renamed_so_file.so $@", +) + +filegroup( + name = "just_main_output_renamed", + srcs = ["renamed_so_file"], + output_group = "main_shared_library_output", +) + +cc_shared_library( + name = "renamed_so_file", + roots = [ + ":direct_so_file_cc_lib2", + ], + shared_lib_name = "renamed_so_file.so", +) + +cc_library( + name = "direct_so_file_cc_lib", + srcs = ["direct_so_file_cc_lib.cc", "direct_so_file_cc_lib.h"], +) + +cc_library( + name = "direct_so_file_cc_lib2", + srcs = ["direct_so_file_cc_lib2.cc", "direct_so_file_cc_lib2.h"], +) + +cc_shared_library_permissions( + name = "permissions", + targets = [ + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:a_suffix", + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:qux", + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:qux2", + ], +) + +build_failure_test( + name = "two_dynamic_deps_same_export_in_so_test", + message = "Two shared libraries in dependencies export the same symbols", + target_under_test = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:two_dynamic_deps_same_export_in_so", +) + +build_failure_test( + name = "two_dynamic_deps_same_export_in_binary_test", + message = "Two shared libraries in dependencies link the same library statically", + target_under_test = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:two_dynamic_deps_same_export_in_binary", +) + +debug_files_test( + name = "debug_files_test", + target_under_test = ":binary", +) + +build_failure_test( + name = "static_deps_error_test", + messages = select({ + ":is_bazel": [ + "@//:__subpackages__", + "@test_repo//:__subpackages__", + ], + "//conditions:default": [ + "@//:__subpackages__", + ], + }), + target_under_test = "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets:unaccounted_for_libs_so", +) diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/a_suffix.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/a_suffix.cc new file mode 100644 index 00000000000000..063a1336fda8c7 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/a_suffix.cc @@ -0,0 +1,16 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/a_suffix.h" + +int a_suffix() { return 42; } diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/a_suffix.h b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/a_suffix.h new file mode 100644 index 00000000000000..84b4a976a218f7 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/a_suffix.h @@ -0,0 +1,19 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY_A_SUFFIX_H_ +#define EXAMPLES_TEST_CC_SHARED_LIBRARY_A_SUFFIX_H_ + +int a_suffix(); + +#endif // EXAMPLES_TEST_CC_SHARED_LIBRARY_A_SUFFIX_H_ diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/additional_script.txt b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/additional_script.txt new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar.cc new file mode 100644 index 00000000000000..785f3ea5bda292 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar.cc @@ -0,0 +1,16 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar.h" + +int bar() { return 42; } diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar.h b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar.h new file mode 100644 index 00000000000000..b116ab15423208 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar.h @@ -0,0 +1,19 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_H_ +#define EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_H_ + +int bar(); + +#endif // EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_H_ diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar.lds b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar.lds new file mode 100644 index 00000000000000..cfe541ca19674f --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar.lds @@ -0,0 +1,5 @@ +VERS_1.1 { + global: + _Z3barv; + local: *; +}; diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar2.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar2.cc new file mode 100644 index 00000000000000..89470613b7213a --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar2.cc @@ -0,0 +1,16 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar2.h" + +int bar2() { return 42; } diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar2.h b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar2.h new file mode 100644 index 00000000000000..1d9697b8f04e88 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar2.h @@ -0,0 +1,19 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_2_H_ +#define EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_2_H_ + +int bar2(); + +#endif // EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_2_H_ diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar3.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar3.cc new file mode 100644 index 00000000000000..050254110b850c --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar3.cc @@ -0,0 +1,16 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar3.h" + +int bar3() { return 42; } diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar3.h b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar3.h new file mode 100644 index 00000000000000..2d19e93c3182c8 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar3.h @@ -0,0 +1,19 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_3_H_ +#define EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_3_H_ + +int bar3(); + +#endif // EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_3_H_ diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/barX.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/barX.cc new file mode 100644 index 00000000000000..e5b1ae1318c26b --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/barX.cc @@ -0,0 +1,16 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/barX.h" + +int barX() { return 42; } diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/barX.h b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/barX.h new file mode 100644 index 00000000000000..0daf84ca1a00ff --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/barX.h @@ -0,0 +1,19 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY_BARX_H_ +#define EXAMPLES_TEST_CC_SHARED_LIBRARY_BARX_H_ + +int barX(); + +#endif // EXAMPLES_TEST_CC_SHARED_LIBRARY_BARX_H_ diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/baz.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/baz.cc new file mode 100644 index 00000000000000..aef2ed5cf797c3 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/baz.cc @@ -0,0 +1,16 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/baz.h" + +int baz() { return 42; } diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/baz.h b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/baz.h new file mode 100644 index 00000000000000..1e7dc7606250e7 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/baz.h @@ -0,0 +1,19 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY_BAZ_H_ +#define EXAMPLES_TEST_CC_SHARED_LIBRARY_BAZ_H_ + +int baz(); + +#endif // EXAMPLES_TEST_CC_SHARED_LIBRARY_BAZ_H_ diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/cc_shared_library_integration_test.sh b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/cc_shared_library_integration_test.sh new file mode 100755 index 00000000000000..a6b480bff2d95d --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/cc_shared_library_integration_test.sh @@ -0,0 +1,82 @@ +#!/bin/bash + +# Copyright 2019 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -euo pipefail + +if [[ "$1" = "non_linux" ]]; then + exit 0 +fi + +CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source ${CURRENT_DIR}/testenv.sh + +function check_symbol_present() { + message="Should have seen '$2' but didn't." + echo "$1" | (grep -q "$2" || (echo "$message" && exit 1)) +} + +function check_symbol_absent() { + message="Shouldn't have seen '$2' but did." + if [ "$(echo $1 | grep -c $2)" -gt 0 ]; then + echo "$message" + exit 1 + fi +} + +function test_shared_library_symbols() { + foo_so=$(find . -name libfoo_so.so) + symbols=$(nm -D $foo_so) + check_symbol_present "$symbols" "U _Z3barv" + check_symbol_present "$symbols" "T _Z3bazv" + check_symbol_present "$symbols" "T _Z3foov" + # Check that the preloaded dep symbol is not present + check_symbol_present "$symbols" "U _Z13preloaded_depv" + + check_symbol_absent "$symbols" "_Z3quxv" + check_symbol_absent "$symbols" "_Z4bar3v" + check_symbol_absent "$symbols" "_Z4bar4v" +} + +function test_shared_library_user_link_flags() { + foo_so=$(find . -name libfoo_so.so) + objdump -x $foo_so | grep $RPATH | grep "kittens" > /dev/null \ + || (echo "Expected to have RUNPATH contain 'kittens' (set by user_link_flags)" \ + && exit 1) +} + +function do_test_binary() { + symbols=$(nm -D $1) + check_symbol_present "$symbols" "U _Z3foov" + $1 | (grep -q "hello 42" || (echo "Expected 'hello 42'" && exit 1)) +} + +function test_binary() { + binary=$(find . -name binary) + do_test_binary $binary + check_symbol_present "$symbols" "T _Z13preloaded_depv" +} + +function test_cc_test() { + cc_test=$(find . -name cc_test) + do_test_binary $cc_test + check_symbol_absent "$symbols" "_Z13preloaded_depv" + $LDD_BINARY $cc_test | (grep -q "preloaded_Udep.so" || (echo "Expected '"preloaded_Udep.so"'" && exit 1)) +} + +test_shared_library_user_link_flags +test_shared_library_symbols +test_binary +test_cc_test diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib.cc new file mode 100644 index 00000000000000..c1875415cbf31c --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib.cc @@ -0,0 +1,16 @@ +// Copyright 2021 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib.h" +int direct_so_file_cc_lib() { return 0; } diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib.h b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib.h new file mode 100644 index 00000000000000..62a7b26915ff42 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib.h @@ -0,0 +1,20 @@ +// Copyright 2022 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_BAZEL_SRC_MAIN_STARLARK_TESTS_BUILTINS_BZL_CC_CC_SHARED_LIBRARY_TEST_CC_SHARED_LIBRARY_DIRECT_SO_FILE_CC_LIB_H_ +#define THIRD_PARTY_BAZEL_SRC_MAIN_STARLARK_TESTS_BUILTINS_BZL_CC_CC_SHARED_LIBRARY_TEST_CC_SHARED_LIBRARY_DIRECT_SO_FILE_CC_LIB_H_ + +int direct_so_file_cc_lib(); + +#endif // THIRD_PARTY_BAZEL_SRC_MAIN_STARLARK_TESTS_BUILTINS_BZL_CC_CC_SHARED_LIBRARY_TEST_CC_SHARED_LIBRARY_DIRECT_SO_FILE_CC_LIB_H_ diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib2.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib2.cc new file mode 100644 index 00000000000000..fe878c1ee2029d --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib2.cc @@ -0,0 +1,16 @@ +// Copyright 2022 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib2.h" +int direct_so_file_cc_lib2() { return 0; } diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib2.h b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib2.h new file mode 100644 index 00000000000000..49fb6316e0a717 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib2.h @@ -0,0 +1,20 @@ +// Copyright 2022 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_BAZEL_SRC_MAIN_STARLARK_TESTS_BUILTINS_BZL_CC_CC_SHARED_LIBRARY_TEST_CC_SHARED_LIBRARY_DIRECT_SO_FILE_CC_LIB2_H_ +#define THIRD_PARTY_BAZEL_SRC_MAIN_STARLARK_TESTS_BUILTINS_BZL_CC_CC_SHARED_LIBRARY_TEST_CC_SHARED_LIBRARY_DIRECT_SO_FILE_CC_LIB2_H_ + +int direct_so_file_cc_lib2(); + +#endif // THIRD_PARTY_BAZEL_SRC_MAIN_STARLARK_TESTS_BUILTINS_BZL_CC_CC_SHARED_LIBRARY_TEST_CC_SHARED_LIBRARY_DIRECT_SO_FILE_CC_LIB2_H_ diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/BUILD.builtin_test b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/BUILD.builtin_test new file mode 100644 index 00000000000000..72354b7a340ba4 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/BUILD.builtin_test @@ -0,0 +1,114 @@ +licenses(["notice"]) + +package(default_visibility = ["//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library:__subpackages__"]) + +TAGS = [ + "manual", + "nobuilder", +] + +cc_binary( + name = "should_fail_binary", + dynamic_deps = ["//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:foo_so"], + tags = TAGS, + deps = [ + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:foo", + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:qux", + ], +) + +cc_shared_library( + name = "permissions_fail_so", + roots = [ + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3:bar", + ], + tags = TAGS, +) + +cc_shared_library_permissions( + name = "permissions_fail", + tags = TAGS, + targets = [ + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:foo", + ], +) + +cc_library( + name = "a", + srcs = ["a.cc"], +) + +cc_library( + name = "b", + srcs = ["b.cc"], +) + +cc_library( + name = "c", + srcs = ["c.cc"], +) + +cc_shared_library( + name = "two_dynamic_deps_same_export_in_so", + dynamic_deps = [ + ":b_so", + ":b2_so", + ], + roots = [ + ":a", + ], + tags = TAGS, +) + +cc_binary( + name = "two_dynamic_deps_same_export_in_binary", + srcs = ["main.cc"], + dynamic_deps = [ + ":b_so", + ":b2_so", + ], + tags = TAGS, +) + +cc_shared_library( + name = "b_so", + roots = [ + ":b", + ], + tags = TAGS, +) + +cc_shared_library( + name = "b2_so", + roots = [ + ":b", + ], + tags = TAGS, +) + +cc_shared_library( + name = "unaccounted_for_libs_so", + roots = [ + ":d1", + ], + tags = TAGS, +) + +cc_library( + name = "d1", + deps = ["d2"], +) + +cc_library( + name = "d2", + deps = [ + "d3", + ] + select({ + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library:is_bazel": ["@test_repo//:bar"], + "//conditions:default": [], + }), +) + +cc_library( + name = "d3", +) diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/a.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/a.cc new file mode 100644 index 00000000000000..e774b99a2a7ebe --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/a.cc @@ -0,0 +1,13 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/b.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/b.cc new file mode 100644 index 00000000000000..e774b99a2a7ebe --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/b.cc @@ -0,0 +1,13 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/c.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/c.cc new file mode 100644 index 00000000000000..e774b99a2a7ebe --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/c.cc @@ -0,0 +1,13 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/main.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/main.cc new file mode 100644 index 00000000000000..e774b99a2a7ebe --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/failing_targets/main.cc @@ -0,0 +1,13 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/foo.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/foo.cc new file mode 100644 index 00000000000000..cc9c8da410e740 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/foo.cc @@ -0,0 +1,31 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar.h" +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/baz.h" +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib.h" +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/direct_so_file_cc_lib2.h" +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/preloaded_dep.h" +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux.h" + +int foo() { + bar(); + baz(); + qux(); +#ifdef IS_LINUX + direct_so_file_cc_lib(); + direct_so_file_cc_lib2(); + preloaded_dep(); +#endif + return 42; +} diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/foo.h b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/foo.h new file mode 100644 index 00000000000000..d180b31116c63e --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/foo.h @@ -0,0 +1,19 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY_FOO_H_ +#define EXAMPLES_TEST_CC_SHARED_LIBRARY_FOO_H_ + +int foo(); + +#endif // EXAMPLES_TEST_CC_SHARED_LIBRARY_FOO_H_ diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/foo.lds b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/foo.lds new file mode 100644 index 00000000000000..54dbe17e89acbb --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/foo.lds @@ -0,0 +1,6 @@ +VERS_1.1 { + global: + _Z3foov; + _Z3bazv; + local: *; +}; diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/main.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/main.cc new file mode 100644 index 00000000000000..d7c47e9ad8f681 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/main.cc @@ -0,0 +1,21 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include + +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/foo.h" + +int main() { + std::cout << "hello " << foo() << std::endl; + return 0; +} diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/not_depended_on.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/not_depended_on.cc new file mode 100644 index 00000000000000..aecaa0c403e982 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/not_depended_on.cc @@ -0,0 +1,19 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/bar.h" + +int foo() { + bar(); + return 42; +} diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/preloaded_dep.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/preloaded_dep.cc new file mode 100644 index 00000000000000..bfa422deb17eb2 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/preloaded_dep.cc @@ -0,0 +1,16 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/preloaded_dep.h" + +int preloaded_dep() { return 42; } diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/preloaded_dep.h b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/preloaded_dep.h new file mode 100644 index 00000000000000..d295eda86d8fea --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/preloaded_dep.h @@ -0,0 +1,19 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY_PRELOADED_DEP_H_ +#define EXAMPLES_TEST_CC_SHARED_LIBRARY_PRELOADED_DEP_H_ + +int preloaded_dep(); + +#endif // EXAMPLES_TEST_CC_SHARED_LIBRARY_PRELOADED_DEP_H_ diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/quux.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/quux.cc new file mode 100644 index 00000000000000..f43354bf10cca7 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/quux.cc @@ -0,0 +1,14 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +int quux() { return 42; } diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux.cc new file mode 100644 index 00000000000000..1463ba696ed419 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux.cc @@ -0,0 +1,16 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux.h" + +int qux() { return 42; } diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux.h b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux.h new file mode 100644 index 00000000000000..e72e5bb6b14f06 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux.h @@ -0,0 +1,19 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY_QUX_H_ +#define EXAMPLES_TEST_CC_SHARED_LIBRARY_QUX_H_ + +int qux(); + +#endif // EXAMPLES_TEST_CC_SHARED_LIBRARY_QUX_H_ diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux2.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux2.cc new file mode 100644 index 00000000000000..95872ce7eef213 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux2.cc @@ -0,0 +1,16 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux2.h" + +int qux2() { return 42; } diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux2.h b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux2.h new file mode 100644 index 00000000000000..f64354cc910446 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/qux2.h @@ -0,0 +1,19 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY_QUX2_H_ +#define EXAMPLES_TEST_CC_SHARED_LIBRARY_QUX2_H_ + +int qux2(); + +#endif // EXAMPLES_TEST_CC_SHARED_LIBRARY_QUX2_H_ diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/starlark_tests.bzl b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/starlark_tests.bzl new file mode 100644 index 00000000000000..9e099061b80b0c --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/starlark_tests.bzl @@ -0,0 +1,148 @@ +# Copyright 2021 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Starlark tests for cc_shared_library""" + +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts", "unittest") + +def _same_package_or_above(label_a, label_b): + if label_a.workspace_name != label_b.workspace_name: + return False + package_a_tokenized = label_a.package.split("/") + package_b_tokenized = label_b.package.split("/") + if len(package_b_tokenized) < len(package_a_tokenized): + return False + + if package_a_tokenized[0] != "": + for i in range(len(package_a_tokenized)): + if package_a_tokenized[i] != package_b_tokenized[i]: + return False + + return True + +def _check_if_target_under_path(value, pattern): + if pattern.workspace_name != value.workspace_name: + return False + if pattern.name == "__pkg__": + return pattern.package == value.package + if pattern.name == "__subpackages__": + return _same_package_or_above(pattern, value) + + return pattern.package == value.package and pattern.name == value.name + +def _linking_suffix_test_impl(ctx): + env = analysistest.begin(ctx) + + if ctx.attr.is_linux: + target_under_test = analysistest.target_under_test(env) + actions = analysistest.target_actions(env) + + args = actions[2].content.split("\n") + user_libs = [] + for arg in args: + if arg.endswith(".o"): + user_libs.append(arg) + asserts.true(env, user_libs[-1].endswith("a_suffix.pic.o"), "liba_suffix.pic.o should be the last user library linked") + + return analysistest.end(env) + +linking_suffix_test = analysistest.make( + _linking_suffix_test_impl, + attrs = { + "is_linux": attr.bool(), + }, +) + +def _additional_inputs_test_impl(ctx): + env = analysistest.begin(ctx) + + if ctx.attr.is_linux: + target_under_test = analysistest.target_under_test(env) + actions = analysistest.target_actions(env) + + found = False + for arg in actions[3].argv: + if arg.find("-Wl,--script=") != -1: + asserts.equals(env, "src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/additional_script.txt", arg[13:]) + found = True + break + asserts.true(env, found, "Should have seen option --script=") + + return analysistest.end(env) + +additional_inputs_test = analysistest.make( + _additional_inputs_test_impl, + attrs = { + "is_linux": attr.bool(), + }, +) + +def _build_failure_test_impl(ctx): + env = analysistest.begin(ctx) + + if ctx.attr.message: + asserts.expect_failure(env, ctx.attr.message) + + if ctx.attr.messages: + for message in ctx.attr.messages: + asserts.expect_failure(env, message) + + return analysistest.end(env) + +build_failure_test = analysistest.make( + _build_failure_test_impl, + expect_failure = True, + attrs = { + "message": attr.string(), + "messages": attr.string_list(), + }, +) + +def _paths_test_impl(ctx): + env = unittest.begin(ctx) + + asserts.false(env, _check_if_target_under_path(Label("//foo"), Label("//bar"))) + asserts.false(env, _check_if_target_under_path(Label("@foo//foo"), Label("@bar//bar"))) + asserts.false(env, _check_if_target_under_path(Label("//bar"), Label("@foo//bar"))) + asserts.true(env, _check_if_target_under_path(Label("@foo//bar"), Label("@foo//bar"))) + asserts.true(env, _check_if_target_under_path(Label("@foo//bar:bar"), Label("@foo//bar"))) + asserts.true(env, _check_if_target_under_path(Label("//bar:bar"), Label("//bar"))) + + asserts.false(env, _check_if_target_under_path(Label("@foo//bar/baz"), Label("@foo//bar"))) + asserts.false(env, _check_if_target_under_path(Label("@foo//bar/baz"), Label("@foo//bar:__pkg__"))) + asserts.true(env, _check_if_target_under_path(Label("@foo//bar/baz"), Label("@foo//bar:__subpackages__"))) + asserts.true(env, _check_if_target_under_path(Label("@foo//bar:qux"), Label("@foo//bar:__pkg__"))) + + asserts.false(env, _check_if_target_under_path(Label("@foo//bar"), Label("@foo//bar/baz:__subpackages__"))) + asserts.false(env, _check_if_target_under_path(Label("//bar"), Label("//bar/baz:__pkg__"))) + + asserts.true(env, _check_if_target_under_path(Label("//foo/bar:baz"), Label("//:__subpackages__"))) + + return unittest.end(env) + +paths_test = unittest.make(_paths_test_impl) + +def _debug_files_test_impl(ctx): + env = analysistest.begin(ctx) + + target_under_test = analysistest.target_under_test(env) + actual_files = [] + for debug_file in target_under_test[OutputGroupInfo].rule_impl_debug_files.to_list(): + actual_files.append(debug_file.basename) + expected_files = ["bar_so_exports.txt", "bar_so_link_once_static_libs.txt", "foo_so_exports.txt", "foo_so_link_once_static_libs.txt", "binary_link_once_static_libs.txt"] + asserts.equals(env, expected_files, actual_files) + + return analysistest.end(env) + +debug_files_test = analysistest.make(_debug_files_test_impl) diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/testenv.sh b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/testenv.sh new file mode 100644 index 00000000000000..5e946c88ca2052 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library/testenv.sh @@ -0,0 +1,17 @@ +#!/bin/bash +# Copyright 2021 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +LDD_BINARY="ldd" +RPATH="RUNPATH" diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library2/BUILD.builtin_test b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library2/BUILD.builtin_test new file mode 100644 index 00000000000000..689b37da968c06 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library2/BUILD.builtin_test @@ -0,0 +1,8 @@ +licenses(["notice"]) + +cc_library( + name = "bar", + srcs = ["bar.cc"], + hdrs = ["bar.h"], + visibility = ["//visibility:public"], +) diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library2/WORKSPACE b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library2/WORKSPACE new file mode 100644 index 00000000000000..838ce120624db3 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library2/WORKSPACE @@ -0,0 +1 @@ +workspace(name = "test_repo") diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library2/bar.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library2/bar.cc new file mode 100644 index 00000000000000..e774b99a2a7ebe --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library2/bar.cc @@ -0,0 +1,13 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library2/bar.h b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library2/bar.h new file mode 100644 index 00000000000000..878b23d3c77ace --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library2/bar.h @@ -0,0 +1,19 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef EXAMPLES_TEST_CC_SHARED2_LIBRARY_BAR_H_ +#define EXAMPLES_TEST_CC_SHARED2_LIBRARY_BAR_H_ + +int bar(); + +#endif // EXAMPLES_TEST_CC_SHARED2_LIBRARY_BAR_H_ diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3/BUILD.builtin_test b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3/BUILD.builtin_test new file mode 100644 index 00000000000000..6a5b93da89d103 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3/BUILD.builtin_test @@ -0,0 +1,16 @@ +package(default_visibility = ["//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library:__subpackages__"]) + +licenses(["notice"]) + +cc_library( + name = "bar", + srcs = ["bar.cc"], + hdrs = ["bar.h"], +) + +cc_shared_library_permissions( + name = "permissions", + targets = [ + "//src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3:bar", + ], +) diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3/bar.cc b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3/bar.cc new file mode 100644 index 00000000000000..e774b99a2a7ebe --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3/bar.cc @@ -0,0 +1,13 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. diff --git a/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3/bar.h b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3/bar.h new file mode 100644 index 00000000000000..8665c8f9b7fdb9 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc/cc_shared_library/test_cc_shared_library3/bar.h @@ -0,0 +1,19 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY3_BAR_H_ +#define EXAMPLES_TEST_CC_SHARED_LIBRARY3_BAR_H_ + +int bar(); + +#endif // EXAMPLES_TEST_CC_SHARED_LIBRARY3_BAR_H_ diff --git a/src/main/starlark/tests/builtins_bzl/cc_builtin_tests.sh b/src/main/starlark/tests/builtins_bzl/cc_builtin_tests.sh new file mode 100755 index 00000000000000..ce07b08f6a9c23 --- /dev/null +++ b/src/main/starlark/tests/builtins_bzl/cc_builtin_tests.sh @@ -0,0 +1,78 @@ +#!/bin/bash -eu +# +# Copyright 2016 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# --- begin runfiles.bash initialization --- +set -euo pipefail +if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then + if [[ -f "$0.runfiles_manifest" ]]; then + export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest" + elif [[ -f "$0.runfiles/MANIFEST" ]]; then + export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST" + elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then + export RUNFILES_DIR="$0.runfiles" + fi +fi +if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then + source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" +elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then + source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \ + "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)" +else + echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" + exit 1 +fi +# --- end runfiles.bash initialization --- + +source "$(rlocation "io_bazel/src/test/shell/integration_test_setup.sh")" \ + || { echo "integration_test_setup.sh not found!" >&2; exit 1; } +source "$(rlocation "io_bazel/src/main/starlark/tests/builtins_bzl/builtin_test_setup.sh")" \ + || { echo "builtin_test_setup.sh not found!" >&2; exit 1; } + +case "$(uname -s | tr [:upper:] [:lower:])" in +msys*) + # As of 2019-01-15, Bazel on Windows only supports MSYS Bash. + declare -r is_windows=true + ;; +*) + declare -r is_windows=false + ;; +esac + +function test_starlark_cc() { + setup_tests src/main/starlark/tests/builtins_bzl/cc + mkdir -p "src/conditions" + cp "$(rlocation "io_bazel/src/conditions/BUILD")" "src/conditions/BUILD" + + cat >> WORKSPACE<