forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces experimental_objc_library. This rule builds objc code with…
… the c++ rule implementation backend and an OSX crosstool. -- MOS_MIGRATED_REVID=119660101
- Loading branch information
Showing
8 changed files
with
307 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/main/java/com/google/devtools/build/lib/rules/objc/ExperimentalObjcLibrary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// 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. | ||
|
||
package com.google.devtools.build.lib.rules.objc; | ||
|
||
import com.google.common.collect.Sets; | ||
import com.google.devtools.build.lib.actions.Artifact; | ||
import com.google.devtools.build.lib.analysis.ConfiguredTarget; | ||
import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode; | ||
import com.google.devtools.build.lib.analysis.RuleContext; | ||
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; | ||
import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory; | ||
import com.google.devtools.build.lib.rules.cpp.CcLibraryHelper; | ||
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration; | ||
import com.google.devtools.build.lib.rules.cpp.CcToolchainProvider; | ||
import com.google.devtools.build.lib.rules.objc.ObjcCommon.CompilationAttributes; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* Implementation for experimental_objc_library. | ||
*/ | ||
public class ExperimentalObjcLibrary implements RuleConfiguredTargetFactory { | ||
|
||
@Override | ||
public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException { | ||
|
||
CompilationArtifacts compilationArtifacts = | ||
CompilationSupport.compilationArtifacts(ruleContext); | ||
CompilationAttributes compilationAttributes = new CompilationAttributes(ruleContext); | ||
|
||
ObjcCommon common = common(ruleContext, compilationAttributes, compilationArtifacts); | ||
|
||
CcToolchainProvider toolchain = | ||
ruleContext | ||
.getPrerequisite(":cc_toolchain", Mode.TARGET) | ||
.getProvider(CcToolchainProvider.class); | ||
FeatureConfiguration featureConfiguration = toolchain.getFeatures().getFeatureConfiguration(); | ||
|
||
Collection<Artifact> sources = Sets.newHashSet(compilationArtifacts.getSrcs()); | ||
Collection<Artifact> privateHdrs = Sets.newHashSet(compilationArtifacts.getPrivateHdrs()); | ||
Collection<Artifact> publicHdrs = Sets.newHashSet(compilationAttributes.hdrs()); | ||
|
||
CcLibraryHelper helper = | ||
new CcLibraryHelper(ruleContext, ObjcCppSemantics.INSTANCE, featureConfiguration) | ||
.addSources(sources) | ||
.addSources(privateHdrs) | ||
.enableCompileProviders() | ||
.addPublicHeaders(publicHdrs) | ||
.addDeps(ruleContext.getPrerequisites("deps", Mode.TARGET)); | ||
|
||
CcLibraryHelper.Info info = helper.build(); | ||
|
||
NestedSetBuilder<Artifact> filesToBuild = | ||
NestedSetBuilder.<Artifact>stableOrder().addAll(common.getCompiledArchive().asSet()); | ||
|
||
return ObjcRuleClasses.ruleConfiguredTarget(ruleContext, filesToBuild.build()) | ||
.addProviders(info.getProviders()) | ||
.build(); | ||
} | ||
|
||
private ObjcCommon common( | ||
RuleContext ruleContext, | ||
CompilationAttributes compilationAttributes, | ||
CompilationArtifacts compilationArtifacts) { | ||
return new ObjcCommon.Builder(ruleContext) | ||
.setCompilationAttributes(compilationAttributes) | ||
.setCompilationArtifacts(compilationArtifacts) | ||
.addDepObjcProviders(ruleContext.getPrerequisites("deps", Mode.TARGET, ObjcProvider.class)) | ||
.build(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/google/devtools/build/lib/rules/objc/ExperimentalObjcLibraryRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// 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. | ||
|
||
package com.google.devtools.build.lib.rules.objc; | ||
|
||
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.PackageNameConstraint; | ||
import com.google.devtools.build.lib.rules.apple.AppleConfiguration; | ||
import com.google.devtools.build.lib.rules.cpp.CppConfiguration; | ||
|
||
/** | ||
* Rule definition for experimental_objc_library. | ||
*/ | ||
public class ExperimentalObjcLibraryRule implements RuleDefinition { | ||
@Override | ||
public RuleClass build(Builder builder, RuleDefinitionEnvironment env) { | ||
return builder | ||
.requiresConfigurationFragments( | ||
ObjcConfiguration.class, AppleConfiguration.class, CppConfiguration.class) | ||
// experimental_objc_library should only occur in bazel test code. We use the /objc | ||
// directory for tests. | ||
.setValidityPredicate(new PackageNameConstraint(1, "objc")) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public Metadata getMetadata() { | ||
return RuleDefinition.Metadata.builder() | ||
.name("experimental_objc_library") | ||
.factoryClass(ExperimentalObjcLibrary.class) | ||
.ancestors( | ||
BaseRuleClasses.BaseRule.class, | ||
ObjcRuleClasses.LinkingRule.class, | ||
ObjcRuleClasses.AlwaysLinkRule.class, | ||
ObjcRuleClasses.CrosstoolRule.class) | ||
.build(); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/com/google/devtools/build/lib/rules/objc/ObjcCppSemantics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// 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. | ||
|
||
package com.google.devtools.build.lib.rules.objc; | ||
|
||
import com.google.devtools.build.lib.actions.Artifact; | ||
import com.google.devtools.build.lib.actions.Root; | ||
import com.google.devtools.build.lib.analysis.RuleContext; | ||
import com.google.devtools.build.lib.rules.cpp.CppCompilationContext.Builder; | ||
import com.google.devtools.build.lib.rules.cpp.CppCompileActionBuilder; | ||
import com.google.devtools.build.lib.rules.cpp.CppCompileActionContext; | ||
import com.google.devtools.build.lib.rules.cpp.CppConfiguration; | ||
import com.google.devtools.build.lib.rules.cpp.CppConfiguration.HeadersCheckingMode; | ||
import com.google.devtools.build.lib.rules.cpp.CppHelper; | ||
import com.google.devtools.build.lib.rules.cpp.CppSemantics; | ||
import com.google.devtools.build.lib.vfs.PathFragment; | ||
|
||
/** | ||
* CppSemantics for objc builds. | ||
*/ | ||
public class ObjcCppSemantics implements CppSemantics { | ||
|
||
// We make CppSemantics a singleton object for efficiency and consistency, since we expect | ||
// any instance to be identical. | ||
public static final CppSemantics INSTANCE = new ObjcCppSemantics(); | ||
|
||
@Override | ||
public PathFragment getEffectiveSourcePath(Artifact source) { | ||
return source.getRootRelativePath(); | ||
} | ||
|
||
@Override | ||
public void finalizeCompileActionBuilder( | ||
RuleContext ruleContext, CppCompileActionBuilder actionBuilder) { | ||
actionBuilder.setCppConfiguration(ruleContext.getFragment(CppConfiguration.class)); | ||
actionBuilder.setActionContext(CppCompileActionContext.class); | ||
// Because Bazel does not support include scanning, we need the entire crosstool filegroup, | ||
// including header files, as opposed to just the "compile" filegroup. | ||
actionBuilder.addTransitiveMandatoryInputs(CppHelper.getToolchain(ruleContext).getCrosstool()); | ||
actionBuilder.setShouldScanIncludes(false); | ||
} | ||
|
||
|
||
@Override | ||
public void setupCompilationContext(RuleContext ruleContext, Builder contextBuilder) { | ||
// For objc builds, no extra setup is required. | ||
} | ||
|
||
@Override | ||
public HeadersCheckingMode determineHeadersCheckingMode(RuleContext ruleContext) { | ||
// Currently, objc builds do not enforce strict deps. To begin enforcing strict deps in objc, | ||
// switch this flag to STRICT. | ||
return HeadersCheckingMode.WARN; | ||
} | ||
|
||
@Override | ||
public boolean needsIncludeScanning(RuleContext ruleContext) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Root getGreppedIncludesDirectory(RuleContext ruleContext) { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters