diff options
Diffstat (limited to 'src/test/java/com/google/devtools')
4 files changed, 297 insertions, 1 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/BUILD b/src/test/java/com/google/devtools/build/lib/rules/cpp/BUILD index 8fa0c86c49..e15d4584cd 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/BUILD +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/BUILD @@ -14,7 +14,10 @@ java_test( name = "cpp-rules-tests", srcs = glob( ["*.java"], - exclude = ["CcImportBaseConfiguredTargetTest.java"], + exclude = [ + "CcImportBaseConfiguredTargetTest.java", + "SkylarkCcCommonTestHelper.java", + ], ) + ["proto/CcProtoLibraryTest.java"], resources = [ "//tools/build_defs/cc:action_names.bzl", @@ -25,6 +28,7 @@ java_test( test_class = "com.google.devtools.build.lib.AllTests", deps = [ ":CcImportBaseConfiguredTargetTest", + ":SkylarkCcCommonTestHelper", "//src/main/java/com/google/devtools/build/lib:bazel-main", "//src/main/java/com/google/devtools/build/lib:bazel-rules", "//src/main/java/com/google/devtools/build/lib:build-base", @@ -43,6 +47,7 @@ java_test( "//src/main/java/com/google/devtools/build/lib/skyframe/serialization", "//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec", "//src/main/java/com/google/devtools/build/lib/skyframe/serialization/testutils", + "//src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp", "//src/main/java/com/google/devtools/build/lib/vfs", "//src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs", "//src/main/java/com/google/devtools/common/options", @@ -102,6 +107,15 @@ java_library( ], ) +java_library( + name = "SkylarkCcCommonTestHelper", + srcs = ["SkylarkCcCommonTestHelper.java"], + deps = [ + "//src/test/java/com/google/devtools/build/lib:analysis_testutil", + "//src/test/java/com/google/devtools/build/lib:testutil", + ], +) + test_suite( name = "windows_tests", tags = [ diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/LinkBuildVariablesTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/LinkBuildVariablesTest.java index cabbb2da82..cffef0494c 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/LinkBuildVariablesTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/LinkBuildVariablesTest.java @@ -16,6 +16,7 @@ package com.google.devtools.build.lib.rules.cpp; import static com.google.common.truth.Truth.assertThat; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.devtools.build.lib.actions.Action; import com.google.devtools.build.lib.actions.Artifact; @@ -367,4 +368,51 @@ public class LinkBuildVariablesTest extends LinkBuildVariablesTestCase { } return null; } + + @Test + public void testUserLinkFlags() throws Exception { + useConfiguration("--linkopt=-bar", "--noexperimental_linkopts_in_user_link_flags"); + + scratch.file("x/BUILD", "cc_binary(name = 'foo', srcs = ['a.cc'], linkopts = ['-foo'])"); + scratch.file("x/a.cc"); + + ConfiguredTarget testTarget = getConfiguredTarget("//x:foo"); + CcToolchainVariables testVariables = + getLinkBuildVariables(testTarget, LinkTargetType.EXECUTABLE); + + ImmutableList<String> userLinkFlags = + CcToolchainVariables.toStringList( + testVariables, LinkBuildVariables.USER_LINK_FLAGS.getVariableName()); + assertThat(userLinkFlags).contains("-foo"); + assertThat(userLinkFlags).doesNotContain("-bar"); + + ImmutableList<String> legacyLinkFlags = + CcToolchainVariables.toStringList( + testVariables, LinkBuildVariables.LEGACY_LINK_FLAGS.getVariableName()); + assertThat(legacyLinkFlags).doesNotContain("-foo"); + assertThat(legacyLinkFlags).contains("-bar"); + } + + @Test + public void testUserLinkFlagsWithLinkoptOption() throws Exception { + useConfiguration("--linkopt=-bar", "--experimental_linkopts_in_user_link_flags"); + + scratch.file("x/BUILD", "cc_binary(name = 'foo', srcs = ['a.cc'], linkopts = ['-foo'])"); + scratch.file("x/a.cc"); + + ConfiguredTarget testTarget = getConfiguredTarget("//x:foo"); + CcToolchainVariables testVariables = + getLinkBuildVariables(testTarget, LinkTargetType.EXECUTABLE); + + ImmutableList<String> userLinkFlags = + CcToolchainVariables.toStringList( + testVariables, LinkBuildVariables.USER_LINK_FLAGS.getVariableName()); + assertThat(userLinkFlags).containsAllOf("-foo", "-bar").inOrder(); + + ImmutableList<String> legacyLinkFlags = + CcToolchainVariables.toStringList( + testVariables, LinkBuildVariables.LEGACY_LINK_FLAGS.getVariableName()); + assertThat(legacyLinkFlags).doesNotContain("-foo"); + assertThat(legacyLinkFlags).doesNotContain("-bar"); + } } diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/SkylarkCcCommonTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/SkylarkCcCommonTest.java index 86d914f073..cbb84be9ef 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/SkylarkCcCommonTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/SkylarkCcCommonTest.java @@ -28,6 +28,7 @@ import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; import com.google.devtools.build.lib.packages.util.ResourceLoader; import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration; import com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink; +import com.google.devtools.build.lib.skylarkbuildapi.cpp.LibraryToLinkApi; import com.google.devtools.build.lib.syntax.SkylarkDict; import com.google.devtools.build.lib.syntax.SkylarkList; import com.google.devtools.build.lib.syntax.SkylarkNestedSet; @@ -1245,4 +1246,114 @@ public class SkylarkCcCommonTest extends BuildViewTestCase { " fragments = ['cpp'],", ");"); } + + @Test + public void testCcNativeRuleDependingOnSkylarkDefinedRule() throws Exception { + SkylarkCcCommonTestHelper.createFiles(scratch, "tools/build_defs/cc"); + assertThat(getConfiguredTarget("//foo:bin")).isNotNull(); + } + + @Test + public void testWhitelist() throws Exception { + SkylarkCcCommonTestHelper.createFiles(scratch, "foo/bar"); + AssertionError e = assertThrows(AssertionError.class, () -> getConfiguredTarget("//foo:bin")); + assertThat(e).hasMessageThat().contains("C++ Skylark API is for the time being"); + } + + @Test + public void testCopts() throws Exception { + SkylarkCcCommonTestHelper.createFilesForTestingCompilation( + scratch, "tools/build_defs/foo", "copts=depset(['-COMPILATION_OPTION'])"); + assertThat(getConfiguredTarget("//foo:bin")).isNotNull(); + ConfiguredTarget target = getConfiguredTarget("//foo:skylark_lib"); + CppCompileAction action = + (CppCompileAction) getGeneratingAction(artifactByPath(getFilesToBuild(target), ".o")); + assertThat(action.getArguments()).contains("-COMPILATION_OPTION"); + } + + @Test + public void testIncludeDirs() throws Exception { + SkylarkCcCommonTestHelper.createFilesForTestingCompilation( + scratch, "tools/build_defs/foo", "includes=depset(['foo/bar', 'baz/qux'])"); + ConfiguredTarget target = getConfiguredTarget("//foo:skylark_lib"); + assertThat(target).isNotNull(); + CppCompileAction action = + (CppCompileAction) getGeneratingAction(artifactByPath(getFilesToBuild(target), ".o")); + assertThat(action.getArguments()).containsAllOf("-Ifoo/bar", "-Ibaz/qux"); + } + + @Test + public void testLinkingOutputs() throws Exception { + SkylarkCcCommonTestHelper.createFiles(scratch, "tools/build_defs/foo"); + ConfiguredTarget target = getConfiguredTarget("//foo:skylark_lib"); + assertThat(target).isNotNull(); + @SuppressWarnings("unchecked") + SkylarkList<LibraryToLinkApi> libraries = + (SkylarkList<LibraryToLinkApi>) target.get("libraries"); + assertThat( + libraries + .stream() + .map(x -> x.getOriginalLibraryArtifact().getFilename()) + .collect(ImmutableList.toImmutableList())) + .contains("libskylark_lib.so"); + } + + @Test + public void testLinkopts() throws Exception { + SkylarkCcCommonTestHelper.createFilesForTestingLinking( + scratch, "tools/build_defs/foo", "linkopts=depset(['-LINKING_OPTION'])"); + ConfiguredTarget target = getConfiguredTarget("//foo:skylark_lib"); + assertThat(target).isNotNull(); + CppLinkAction action = + (CppLinkAction) getGeneratingAction(artifactByPath(getFilesToBuild(target), ".so")); + assertThat(action.getArguments()).contains("-LINKING_OPTION"); + } + + @Test + public void testSettingDynamicLibraryArtifact() throws Exception { + SkylarkCcCommonTestHelper.createFilesForTestingLinking( + scratch, + "tools/build_defs/foo", + "dynamic_library=ctx.actions.declare_file('dynamic_lib_artifact.so')"); + assertThat(getConfiguredTarget("//foo:skylark_lib")).isNotNull(); + ConfiguredTarget target = getConfiguredTarget("//foo:skylark_lib"); + assertThat( + getFilesToBuild(target) + .toCollection() + .stream() + .map(x -> x.getFilename()) + .collect(ImmutableList.toImmutableList())) + .contains("dynamic_lib_artifact.so"); + } + + @Test + public void testCcLinkingInfos() throws Exception { + SkylarkCcCommonTestHelper.createFilesForTestingLinking( + scratch, "tools/build_defs/foo", "cc_linking_infos=dep_cc_linking_infos"); + assertThat(getConfiguredTarget("//foo:bin")).isNotNull(); + ConfiguredTarget target = getConfiguredTarget("//foo:bin"); + CppLinkAction action = + (CppLinkAction) getGeneratingAction(artifactByPath(getFilesToBuild(target), "bin")); + assertThat(action.getArguments()).containsAllOf("-DEP1_LINKOPT", "-DEP2_LINKOPT"); + } + + @Test + public void testNeverlinkTrue() throws Exception { + assertThat(setUpNeverlinkTest("True").getArguments()).doesNotContain("-NEVERLINK_OPTION"); + } + + @Test + public void testNeverlinkFalse() throws Exception { + assertThat(setUpNeverlinkTest("False").getArguments()).contains("-NEVERLINK_OPTION"); + } + + private CppLinkAction setUpNeverlinkTest(String value) throws Exception { + SkylarkCcCommonTestHelper.createFilesForTestingLinking( + scratch, + "tools/build_defs/foo", + String.join(",", "linkopts=depset(['-NEVERLINK_OPTION'])", "neverlink=" + value)); + assertThat(getConfiguredTarget("//foo:bin")).isNotNull(); + ConfiguredTarget target = getConfiguredTarget("//foo:bin"); + return (CppLinkAction) getGeneratingAction(artifactByPath(getFilesToBuild(target), "bin")); + } } diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/SkylarkCcCommonTestHelper.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/SkylarkCcCommonTestHelper.java new file mode 100644 index 0000000000..1aafab17b2 --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/SkylarkCcCommonTestHelper.java @@ -0,0 +1,123 @@ +// Copyright 2018 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. +package com.google.devtools.build.lib.rules.cpp; + +import com.google.devtools.build.lib.analysis.util.AnalysisMock; +import com.google.devtools.build.lib.testutil.Scratch; + +/** Methods useful for tests testing the C++ Skylark API. */ +public final class SkylarkCcCommonTestHelper { + + public static void createFilesForTestingCompilation( + Scratch scratch, String bzlFilePath, String compileProviderLines) throws Exception { + createFiles(scratch, bzlFilePath, compileProviderLines, ""); + } + + public static void createFilesForTestingLinking( + Scratch scratch, String bzlFilePath, String linkProviderLines) throws Exception { + createFiles(scratch, bzlFilePath, "", linkProviderLines); + } + + public static void createFiles(Scratch scratch, String bzlFilePath) throws Exception { + createFiles(scratch, bzlFilePath, "", ""); + } + + public static void createFiles( + Scratch scratch, String bzlFilePath, String compileProviderLines, String linkProviderLines) + throws Exception { + String fragments = " fragments = ['google_cpp', 'cpp'],"; + if (AnalysisMock.get().isThisBazel()) { + fragments = " fragments = ['cpp'],"; + } + scratch.file(bzlFilePath + "/BUILD"); + scratch.file( + bzlFilePath + "/extension.bzl", + "def _cc_skylark_library_impl(ctx):", + " dep_cc_linking_infos = []", + " for dep in ctx.attr._deps:", + " dep_cc_linking_infos.append(dep[CcLinkingInfo])", + " toolchain = ctx.attr._cc_toolchain[cc_common.CcToolchainInfo]", + " feature_configuration = cc_common.configure_features(", + " cc_toolchain=toolchain,", + " requested_features = ctx.features,", + " unsupported_features = ctx.disabled_features)", + " compilation_info = cc_common.compile(", + " ctx=ctx,", + " feature_configuration=feature_configuration,", + " cc_toolchain=toolchain,", + " srcs=ctx.files.srcs,", + " hdrs=ctx.files.hdrs" + (compileProviderLines.isEmpty() ? "" : ","), + " " + compileProviderLines, + " )", + " linking_info = cc_common.link(", + " ctx=ctx,", + " feature_configuration=feature_configuration,", + " cc_compilation_outputs=compilation_info.cc_compilation_outputs(),", + " cc_toolchain=toolchain" + (linkProviderLines.isEmpty() ? "" : ","), + " " + linkProviderLines, + " )", + " files_to_build = []", + " files_to_build.extend(compilation_info.cc_compilation_outputs()", + " .object_files(use_pic=True))", + " files_to_build.extend(compilation_info.cc_compilation_outputs()", + " .object_files(use_pic=False))", + " static_libs = linking_info.cc_linking_outputs().pic_static_libraries()", + " for static_lib in static_libs:", + " files_to_build.append(static_lib.original_artifact())", + " dynamic_libs = linking_info.cc_linking_outputs().dynamic_libraries_for_linking()", + " for dynamic_lib in dynamic_libs:", + " files_to_build.append(dynamic_lib.original_artifact())", + " return struct(", + " libraries=dynamic_libs,", + " providers=[DefaultInfo(files=depset(files_to_build)),", + " cc_common.create_cc_skylark_info(ctx=ctx),", + " compilation_info.cc_compilation_info(),", + " linking_info.cc_linking_info()])", + "cc_skylark_library = rule(", + " implementation = _cc_skylark_library_impl,", + " attrs = {", + " 'srcs': attr.label_list(allow_files=True),", + " 'hdrs': attr.label_list(allow_files=True),", + " '_deps': attr.label_list(default=['//foo:dep1', '//foo:dep2']),", + " '_cc_toolchain': attr.label(default =", + " configuration_field(fragment = 'cpp', name = 'cc_toolchain'))", + " },", + fragments, + ")"); + scratch.file( + "foo/BUILD", + "load('//" + bzlFilePath + ":extension.bzl', 'cc_skylark_library')", + "cc_library(", + " name = 'dep1',", + " srcs = ['dep1.cc'],", + " hdrs = ['dep1.h'],", + " linkopts = ['-DEP1_LINKOPT'],", + ")", + "cc_library(", + " name = 'dep2',", + " srcs = ['dep2.cc'],", + " hdrs = ['dep2.h'],", + " linkopts = ['-DEP2_LINKOPT'],", + ")", + "cc_skylark_library(", + " name = 'skylark_lib',", + " srcs = ['skylark_lib.cc'],", + " hdrs = ['skylark_lib.h'],", + ")", + "cc_binary(", + " name = 'bin',", + " deps = ['skylark_lib'],", + ")"); + } +} |