diff options
author | Yun Peng <pcloudy@google.com> | 2017-11-17 09:01:57 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2017-11-17 09:03:56 -0800 |
commit | 8731dc06915563b208a5c774b59bdb917c6ebbb0 (patch) | |
tree | b749f9919d9bc9c34f7fcc9bf56e1dc24bcc185e /src | |
parent | 41584252323b8f4410b6aecb29db68cb7b6a2de8 (diff) |
Add cc_import rule definition
Designed by
https://docs.google.com/document/d/1hK2mWl3TYNL9oJYX_S020TKkXZvBw1aBoYERvTHVyfg/edit#
Change-Id: I025adf555a9827c55a90acc3f254cbd105e224c6
PiperOrigin-RevId: 176114968
Diffstat (limited to 'src')
7 files changed, 272 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java index 4dcd5b6d95..93c90f69ae 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java @@ -37,6 +37,7 @@ import com.google.devtools.build.lib.bazel.rules.android.BazelAndroidRuleClasses import com.google.devtools.build.lib.bazel.rules.android.BazelAndroidSemantics; import com.google.devtools.build.lib.bazel.rules.common.BazelFilegroupRule; import com.google.devtools.build.lib.bazel.rules.cpp.BazelCcBinaryRule; +import com.google.devtools.build.lib.bazel.rules.cpp.BazelCcImportRule; import com.google.devtools.build.lib.bazel.rules.cpp.BazelCcIncLibraryRule; import com.google.devtools.build.lib.bazel.rules.cpp.BazelCcLibraryRule; import com.google.devtools.build.lib.bazel.rules.cpp.BazelCcTestRule; @@ -99,6 +100,7 @@ import com.google.devtools.build.lib.rules.config.ConfigFeatureFlagConfiguration import com.google.devtools.build.lib.rules.config.ConfigRuleClasses; import com.google.devtools.build.lib.rules.config.ConfigSkylarkCommon; import com.google.devtools.build.lib.rules.core.CoreRules; +import com.google.devtools.build.lib.rules.cpp.CcImportRule; import com.google.devtools.build.lib.rules.cpp.CcIncLibraryRule; import com.google.devtools.build.lib.rules.cpp.CcToolchainAlias; import com.google.devtools.build.lib.rules.cpp.CcToolchainRule; @@ -372,6 +374,7 @@ public class BazelRuleClassProvider { builder.addRuleDefinition(new CcToolchainSuiteRule()); builder.addRuleDefinition(new CcToolchainAlias.CcToolchainAliasRule()); builder.addRuleDefinition(new CcIncLibraryRule()); + builder.addRuleDefinition(new CcImportRule()); builder.addRuleDefinition(new BazelCppRuleClasses.CcLinkingRule()); builder.addRuleDefinition(new BazelCppRuleClasses.CcDeclRule()); builder.addRuleDefinition(new BazelCppRuleClasses.CcBaseRule()); @@ -382,6 +385,7 @@ public class BazelRuleClassProvider { builder.addRuleDefinition(new BazelCppRuleClasses.CcLibraryBaseRule()); builder.addRuleDefinition(new BazelCcLibraryRule()); builder.addRuleDefinition(new BazelCcIncLibraryRule()); + builder.addRuleDefinition(new BazelCcImportRule()); } @Override diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcImport.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcImport.java new file mode 100644 index 0000000000..0b74aef0f4 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcImport.java @@ -0,0 +1,24 @@ +// Copyright 2017 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.bazel.rules.cpp; + +import com.google.devtools.build.lib.rules.cpp.CcImport; + +/** {@code cc_import} rule with Bazel semantics. */ +public class BazelCcImport extends CcImport { + public BazelCcImport() { + super(BazelCppSemantics.INSTANCE); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcImportRule.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcImportRule.java new file mode 100644 index 0000000000..de582d01d4 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcImportRule.java @@ -0,0 +1,54 @@ +// Copyright 2017 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.bazel.rules.cpp; + +import static com.google.devtools.build.lib.packages.Attribute.attr; +import static com.google.devtools.build.lib.packages.BuildType.LABEL; + +import com.google.devtools.build.lib.analysis.BaseRuleClasses; +import com.google.devtools.build.lib.analysis.RuleDefinition; +import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment; +import com.google.devtools.build.lib.packages.RuleClass; +import com.google.devtools.build.lib.packages.RuleClass.Builder; +import com.google.devtools.build.lib.rules.cpp.CcImportRule; +import com.google.devtools.build.lib.rules.cpp.CcToolchain; +import com.google.devtools.build.lib.rules.cpp.CppConfiguration; +import com.google.devtools.build.lib.rules.cpp.CppRuleClasses; + +/** Rule definition for the cc_import rule. */ +public final class BazelCcImportRule implements RuleDefinition { + @Override + public RuleClass build(Builder builder, RuleDefinitionEnvironment env) { + return builder + .requiresConfigurationFragments(CppConfiguration.class) + .add( + attr(CcToolchain.CC_TOOLCHAIN_DEFAULT_ATTRIBUTE_NAME, LABEL) + .value(CppRuleClasses.ccToolchainAttribute(env))) + .add( + attr(CcToolchain.CC_TOOLCHAIN_TYPE_ATTRIBUTE_NAME, LABEL) + .value(CppRuleClasses.ccToolchainTypeAttribute(env))) + .add(attr(":stl", LABEL).value(BazelCppRuleClasses.STL)) + .build(); + } + + @Override + public Metadata getMetadata() { + return RuleDefinition.Metadata.builder() + .name("cc_import") + .ancestors(BaseRuleClasses.BaseRule.class, CcImportRule.class) + .factoryClass(BazelCcImport.class) + .build(); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcImport.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcImport.java new file mode 100644 index 0000000000..abfe81ce26 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcImport.java @@ -0,0 +1,43 @@ +// Copyright 2017 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.RuleConfiguredTargetBuilder; +import com.google.devtools.build.lib.analysis.RuleConfiguredTargetFactory; +import com.google.devtools.build.lib.analysis.RuleContext; +import com.google.devtools.build.lib.analysis.RunfilesProvider; +import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; +import com.google.devtools.build.lib.collect.nestedset.Order; + +/** + * A ConfiguredTarget for <code>cc_import</code> rule. + */ +public abstract class CcImport implements RuleConfiguredTargetFactory { + private final CppSemantics semantics; + + protected CcImport(CppSemantics semantics) { + this.semantics = semantics; + } + + @Override + public ConfiguredTarget create(RuleContext ruleContext) + throws RuleErrorException, InterruptedException { + return new RuleConfiguredTargetBuilder(ruleContext) + .setFilesToBuild(NestedSetBuilder.emptySet(Order.STABLE_ORDER)) + .addProvider(RunfilesProvider.class, RunfilesProvider.EMPTY) + .build(); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcImportRule.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcImportRule.java new file mode 100644 index 0000000000..5701549bea --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcImportRule.java @@ -0,0 +1,88 @@ +// Copyright 2017 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.devtools.build.lib.packages.Attribute.ConfigurationTransition.DATA; +import static com.google.devtools.build.lib.packages.Attribute.attr; +import static com.google.devtools.build.lib.packages.BuildType.LABEL; +import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST; +import static com.google.devtools.build.lib.syntax.Type.BOOLEAN; + +import com.google.devtools.build.lib.analysis.BaseRuleClasses; +import com.google.devtools.build.lib.analysis.RuleDefinition; +import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment; +import com.google.devtools.build.lib.packages.RuleClass; +import com.google.devtools.build.lib.packages.RuleClass.Builder; +import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType; +import com.google.devtools.build.lib.util.FileTypeSet; + +/** Rule definition for the cc_import rule. */ +public final class CcImportRule implements RuleDefinition { + @Override + public RuleClass build(Builder builder, RuleDefinitionEnvironment env) { + return builder + /*<!-- #BLAZE_RULE($cc_import).ATTRIBUTE(static_library) --> + A single precompiled static library + <!-- #END_BLAZE_RULE.ATTRIBUTE -->*/ + .add( + attr("static_library", LABEL) + .allowedFileTypes(CppFileTypes.ARCHIVE) + ) + /*<!-- #BLAZE_RULE($cc_import).ATTRIBUTE(shared_library) --> + A single precompiled shared library + <!-- #END_BLAZE_RULE.ATTRIBUTE -->*/ + .add(attr("shared_library", LABEL) + .allowedFileTypes(CppFileTypes.SHARED_LIBRARY) + ) + /*<!-- #BLAZE_RULE($cc_import).ATTRIBUTE(shared_library) --> + A single interface library for linking the shared library + <!-- #END_BLAZE_RULE.ATTRIBUTE -->*/ + .add(attr("interface_library", LABEL) + .allowedFileTypes(CppFileTypes.INTERFACE_SHARED_LIBRARY)) + /*<!-- #BLAZE_RULE($cc_import).ATTRIBUTE(hdrs) --> + The list of header files published by + this precompiled library to be directly included by sources in dependent rules. + <!-- #END_BLAZE_RULE.ATTRIBUTE -->*/ + .add( + attr("hdrs", LABEL_LIST) + .orderIndependent() + .direct_compile_time_input() + .allowedFileTypes(CppFileTypes.CPP_HEADER)) + /*<!-- #BLAZE_RULE(cc_import).ATTRIBUTE(alwayslink) --> + If 1, any binary that depends (directly or indirectly) on this C++ + precompiled library will link in all the object files archived in the static library, + even if some contain no symbols referenced by the binary. + This is useful if your code isn't explicitly called by code in + the binary, e.g., if your code registers to receive some callback + provided by some service. + <!-- #END_BLAZE_RULE.ATTRIBUTE -->*/ + .add( + attr("alwayslink", BOOLEAN) + .nonconfigurable("value is referenced in an ImplicitOutputsFunction")) + .add(attr("data", LABEL_LIST).cfg(DATA) + .allowedFileTypes(FileTypeSet.ANY_FILE) + .dontCheckConstraints()) + .build(); + } + + @Override + public Metadata getMetadata() { + return RuleDefinition.Metadata.builder() + .name("$cc_import") + .ancestors(BaseRuleClasses.BaseRule.class) + .type(RuleClassType.ABSTRACT) + .build(); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcImportConfiguredTargetTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcImportConfiguredTargetTest.java new file mode 100644 index 0000000000..e7fa1657cd --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcImportConfiguredTargetTest.java @@ -0,0 +1,43 @@ +// 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. +// Copyright 2006 Google Inc. All rights reserved. + +package com.google.devtools.build.lib.rules.cpp; + +import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * "White-box" unit test of cc_import rule. + */ +@RunWith(JUnit4.class) +public class CcImportConfiguredTargetTest extends BuildViewTestCase { + + @Test + public void testCcImportRule() throws Exception { + scratch.file("third_party/BUILD", + "cc_import(", + " name = 'a_import',", + " static_library = 'A.a',", + " shared_library = 'A.so',", + " interface_library = 'A.ifso',", + " hdrs = ['a.h'],", + " alwayslink = 1,", + ")"); + getConfiguredTarget("//third_party:a_import"); + } +} diff --git a/src/test/py/bazel/bazel_windows_cpp_test.py b/src/test/py/bazel/bazel_windows_cpp_test.py index cb842f005d..71d8231dc2 100644 --- a/src/test/py/bazel/bazel_windows_cpp_test.py +++ b/src/test/py/bazel/bazel_windows_cpp_test.py @@ -478,6 +478,22 @@ class BazelWindowsCppTest(test_base.TestBase): self.AssertExitCode(exit_code, 0, stderr) self.assertIn('/DEF:my_lib.def', ''.join(stderr)) + def testCcImportRule(self): + self.ScratchFile('WORKSPACE') + self.ScratchFile('BUILD', [ + 'cc_import(', + ' name = "a_import",', + ' static_library = "A.a",', + ' shared_library = "A.so",', + ' interface_library = "A.ifso",', + ' hdrs = ["a.h"],', + ' alwayslink = 1,', + ')', + ]) + exit_code, _, stderr = self.RunBazel([ + 'build', '//:a_import', + ]) + self.AssertExitCode(exit_code, 0, stderr) if __name__ == '__main__': unittest.main() |