Skip to content

Commit

Permalink
Opensource more cpp rules tests.
Browse files Browse the repository at this point in the history
--
MOS_MIGRATED_REVID=108806251
  • Loading branch information
dslomov authored and kchodorow committed Nov 30, 2015
1 parent 6072b2b commit ece87c2
Show file tree
Hide file tree
Showing 8 changed files with 876 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/test/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,10 @@ java_test(
":testutil",
"//src/main/java/com/google/devtools/build/lib:analysis-exec-rules-skyframe",
"//src/main/java/com/google/devtools/build/lib:bazel-core",
"//src/main/java/com/google/devtools/build/lib:util",
"//src/main/java/com/google/devtools/build/lib:vfs",
"//src/main/java/com/google/devtools/build/lib/actions",
"//src/main/java/com/google/devtools/common/options",
"//src/main/protobuf:crosstool_config_proto",
"//third_party:guava",
"//third_party:guava-testlib",
Expand All @@ -863,8 +866,8 @@ java_test(
":test_runner",
"//src/main/java/com/google/devtools/build/lib:bazel-core",
"//src/main/java/com/google/devtools/build/lib:events",
"//src/main/java/com/google/devtools/build/lib:util",
"//src/main/java/com/google/devtools/build/lib:vfs",
"//src/main/java/com/google/dtesevtools/build/lib:util",
"//src/main/protobuf:crosstool_config_proto",
"//third_party:guava",
"//third_party:junit4",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2015 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.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.util.CompileOnlyTestCase;

/**
* Unit tests that validate --compile_only behavior.
*/
public class CcCompileOnlyTest extends CompileOnlyTestCase {
public void testCcCompileOnly() throws Exception {
scratch.file("package/BUILD",
"cc_binary(name='foo', srcs=['foo.cc', ':bar'], deps = [':foolib'])",
"cc_library(name='foolib', srcs=['foolib.cc'])",
"genrule(name='bar', outs=['bar.h', 'bar.cc'], cmd='touch $(OUTS)')");
scratch.file("package/foo.cc",
"#include <stdio.h>",
"int main() {",
" printf(\"Hello, world!\\n\");",
" return 0;",
"}");
scratch.file("package/foolib.cc",
"#include <stdio.h>",
"int printHeader() {",
" printf(\"Hello, library!\\n\");",
" return 0;",
"}");

ConfiguredTarget target = getConfiguredTarget("//package:foo");

assertNotNull(getArtifactByExecPathSuffix(target, "/foo.pic.o"));
assertNotNull(getArtifactByExecPathSuffix(target, "/bar.pic.o"));
// Check that deps are not built
assertNull(getArtifactByExecPathSuffix(target, "/foolib.pic.o"));
// Check that linking is not executed
assertNull(getArtifactByExecPathSuffix(target, "/foo"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright 2015 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 static com.google.common.truth.Truth.assertThat;

import com.google.devtools.build.lib.actions.util.ActionsTestUtil;
import com.google.devtools.build.lib.analysis.RuleConfiguredTarget;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import com.google.devtools.build.lib.testutil.TestConstants;

/**
* Tests for Skylark providers for cpp rules.
*/
public class CcSkylarkApiProviderTest extends BuildViewTestCase {
private CcSkylarkApiProvider getApi(String label) throws Exception {
RuleConfiguredTarget rule = (RuleConfiguredTarget) getConfiguredTarget(label);
return (CcSkylarkApiProvider) rule.get(CcSkylarkApiProvider.NAME);
}

public void testTransitiveHeaders() throws Exception {
scratch.file(
"pkg/BUILD",
"cc_binary(",
" name = 'check',",
" srcs = ['bin.cc', 'bin.h'],",
" deps = [':check_lib'],",
")",
"cc_library(",
" name = 'check_lib',",
" srcs = ['lib.cc', 'lib.h'],",
")");
assertThat(ActionsTestUtil.baseArtifactNames(getApi("//pkg:check").getTransitiveHeaders()))
.containsAllOf("lib.h", "bin.h");
assertThat(ActionsTestUtil.baseArtifactNames(getApi("//pkg:check_lib").getTransitiveHeaders()))
.contains("lib.h");
}

public void testLinkFlags() throws Exception {
scratch.file(
"pkg/BUILD",
"cc_binary(",
" name = 'check',",
" srcs = ['bin.cc', 'bin.h'],",
" linkopts = ['-lm'],",
" deps = [':dependent_lib'],",
")",
"cc_binary(",
" name = 'check_no_srcs',",
" linkopts = ['-lm'],",
" deps = [':dependent_lib'],",
")",
"cc_library(",
" name = 'dependent_lib',",
" linkopts = ['-lz'],",
" deps = [':check_lib'],",
")",
"cc_library(",
" name = 'check_lib',",
" defines = ['foo'],",
" linkopts = ['-Wl,-M'],",
")");
assertThat(getApi("//pkg:check_lib").getLinkopts())
.contains("-Wl,-M");
assertThat(getApi("//pkg:dependent_lib").getLinkopts())
.containsAllOf("-lz", "-Wl,-M")
.inOrder();
assertThat(getApi("//pkg:check").getLinkopts())
.isEmpty();
assertThat(getApi("//pkg:check_no_srcs").getLinkopts())
.isEmpty();
}

public void testLibraries() throws Exception {
scratch.file(
"pkg/BUILD",
"cc_binary(",
" name = 'check',",
" srcs = ['bin.cc', 'bin.h'],",
" deps = [':check_lib'],",
")",
"cc_binary(",
" name = 'check_no_srcs',",
" deps = [':check_lib'],",
")",
"cc_library(",
" name = 'check_lib',",
" srcs = ['lib.cc', 'lib.h'],",
")");
assertThat(ActionsTestUtil.baseArtifactNames(getApi("//pkg:check_lib").getLibraries()))
.containsExactly("libcheck_lib.a");
assertThat(ActionsTestUtil.baseArtifactNames(getApi("//pkg:check").getLibraries()))
.isEmpty();
assertThat(ActionsTestUtil.baseArtifactNames(getApi("//pkg:check_no_srcs").getLibraries()))
.isEmpty();
}

public void testCcFlags() throws Exception {
scratch.file(
"pkg/BUILD",
"cc_binary(",
" name = 'check',",
" srcs = ['bin.cc', 'bin.h'],",
" deps = [':check_lib'],",
")",
"cc_library(",
" name = 'check_lib',",
" defines = ['foo'],",
")");
// The particular values for include directories are slightly
// fragile because the build system changes. But check for at
// least one normal include, one system include, and one define.
assertThat(getApi("//pkg:check").getCcFlags())
.containsAllOf("-iquote .", "-isystem " + TestConstants.GCC_INCLUDE_PATH, "-Dfoo");
}
}
Loading

0 comments on commit ece87c2

Please sign in to comment.