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.
C++: Implements Skylark cc_common.compile()/link().
Working towards bazelbuild#4570. RELNOTES:none PiperOrigin-RevId: 205274676
Showing
9 changed files
with
861 additions
and
6 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
107 changes: 107 additions & 0 deletions
107
src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcModule.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,107 @@ | ||
// 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.bazel.rules.cpp; | ||
|
||
import com.google.devtools.build.lib.actions.Artifact; | ||
import com.google.devtools.build.lib.analysis.skylark.SkylarkRuleContext; | ||
import com.google.devtools.build.lib.rules.cpp.CcCompilationHelper.CompilationInfo; | ||
import com.google.devtools.build.lib.rules.cpp.CcCompilationInfo; | ||
import com.google.devtools.build.lib.rules.cpp.CcCompilationOutputs; | ||
import com.google.devtools.build.lib.rules.cpp.CcLinkParams; | ||
import com.google.devtools.build.lib.rules.cpp.CcLinkingHelper.LinkingInfo; | ||
import com.google.devtools.build.lib.rules.cpp.CcLinkingInfo; | ||
import com.google.devtools.build.lib.rules.cpp.CcModule; | ||
import com.google.devtools.build.lib.rules.cpp.CcModule.CcSkylarkInfo; | ||
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.cpp.CcToolchainVariables; | ||
import com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink; | ||
import com.google.devtools.build.lib.skylarkbuildapi.cpp.BazelCcModuleApi; | ||
import com.google.devtools.build.lib.syntax.EvalException; | ||
import com.google.devtools.build.lib.syntax.Runtime; | ||
import com.google.devtools.build.lib.syntax.SkylarkList; | ||
|
||
/** | ||
* A module that contains Skylark utilities for C++ support. | ||
* | ||
* <p>This is a work in progress. The API is guarded behind --experimental_enable_cc_skylark_api. | ||
* The API is under development and unstable. | ||
*/ | ||
public class BazelCcModule extends CcModule | ||
implements BazelCcModuleApi< | ||
CcToolchainProvider, | ||
FeatureConfiguration, | ||
CompilationInfo, | ||
CcCompilationInfo, | ||
CcCompilationOutputs, | ||
LinkingInfo, | ||
CcLinkingInfo, | ||
CcToolchainVariables, | ||
LibraryToLink, | ||
CcLinkParams, | ||
CcSkylarkInfo> { | ||
|
||
@Override | ||
public CompilationInfo compile( | ||
SkylarkRuleContext skylarkRuleContext, | ||
FeatureConfiguration skylarkFeatureConfiguration, | ||
CcToolchainProvider skylarkCcToolchainProvider, | ||
SkylarkList<Artifact> sources, | ||
SkylarkList<Artifact> headers, | ||
Object skylarkIncludes, | ||
Object skylarkCopts, | ||
SkylarkList<CcCompilationInfo> ccCompilationInfos) | ||
throws EvalException { | ||
return BazelCcModule.compile( | ||
BazelCppSemantics.INSTANCE, | ||
skylarkRuleContext, | ||
skylarkFeatureConfiguration, | ||
skylarkCcToolchainProvider, | ||
sources, | ||
headers, | ||
skylarkIncludes, | ||
skylarkCopts, | ||
/* generateNoPicOutputs= */ "conditionally", | ||
/* generatePicOutputs= */ "conditionally", | ||
/* skylarkAdditionalCompilationInputs= */ Runtime.NONE, | ||
/* skylarkAdditionalIncludeScanningRoots= */ Runtime.NONE, | ||
ccCompilationInfos, | ||
/* purpose= */ Runtime.NONE); | ||
} | ||
|
||
@Override | ||
public LinkingInfo link( | ||
SkylarkRuleContext skylarkRuleContext, | ||
FeatureConfiguration skylarkFeatureConfiguration, | ||
CcToolchainProvider skylarkCcToolchainProvider, | ||
CcCompilationOutputs ccCompilationOutputs, | ||
Object skylarkLinkopts, | ||
Object dynamicLibrary, | ||
SkylarkList<CcLinkingInfo> skylarkCcLinkingInfos, | ||
boolean neverLink) | ||
throws InterruptedException, EvalException { | ||
return BazelCcModule.link( | ||
BazelCppSemantics.INSTANCE, | ||
skylarkRuleContext, | ||
skylarkFeatureConfiguration, | ||
skylarkCcToolchainProvider, | ||
ccCompilationOutputs, | ||
skylarkLinkopts, | ||
/* shouldCreateStaticLibraries= */ true, | ||
dynamicLibrary, | ||
skylarkCcLinkingInfos, | ||
neverLink); | ||
} | ||
} |
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
204 changes: 204 additions & 0 deletions
204
src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/BazelCcModuleApi.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,204 @@ | ||
// 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.skylarkbuildapi.cpp; | ||
|
||
import com.google.devtools.build.lib.actions.Artifact; | ||
import com.google.devtools.build.lib.analysis.skylark.SkylarkRuleContext; | ||
import com.google.devtools.build.lib.skylarkbuildapi.SkylarkRuleContextApi; | ||
import com.google.devtools.build.lib.skylarkinterface.Param; | ||
import com.google.devtools.build.lib.skylarkinterface.ParamType; | ||
import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; | ||
import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; | ||
import com.google.devtools.build.lib.syntax.EvalException; | ||
import com.google.devtools.build.lib.syntax.Runtime.NoneType; | ||
import com.google.devtools.build.lib.syntax.SkylarkList; | ||
import com.google.devtools.build.lib.syntax.SkylarkNestedSet; | ||
|
||
/** Utilites related to C++ support. */ | ||
@SkylarkModule( | ||
name = "cc_common", | ||
doc = "Utilities for C++ compilation, linking, and command line generation.") | ||
// TODO(b/111365281): Add experimental field once it's available. | ||
public interface BazelCcModuleApi< | ||
CcToolchainProviderT extends CcToolchainProviderApi, | ||
FeatureConfigurationT extends FeatureConfigurationApi, | ||
CompilationInfoT extends CompilationInfoApi, | ||
CcCompilationInfoT extends CcCompilationInfoApi, | ||
CcCompilationOutputsT extends CcCompilationOutputsApi, | ||
LinkingInfoT extends LinkingInfoApi, | ||
CcLinkingInfoT extends CcLinkingInfoApi, | ||
CcToolchainVariablesT extends CcToolchainVariablesApi, | ||
LibraryToLinkT extends LibraryToLinkApi, | ||
CcLinkParamsT extends CcLinkParamsApi, | ||
CcSkylarkInfoT extends CcSkylarkInfoApi> | ||
extends CcModuleApi< | ||
CcToolchainProviderT, | ||
FeatureConfigurationT, | ||
CcToolchainVariablesT, | ||
LibraryToLinkT, | ||
CcLinkParamsT, | ||
CcSkylarkInfoT> { | ||
|
||
@SkylarkCallable( | ||
name = "compile", | ||
documented = false, | ||
parameters = { | ||
@Param( | ||
name = "ctx", | ||
positional = false, | ||
named = true, | ||
type = SkylarkRuleContextApi.class, | ||
doc = "The rule context."), | ||
@Param( | ||
name = "feature_configuration", | ||
doc = "Feature configuration to be queried.", | ||
positional = false, | ||
named = true, | ||
type = FeatureConfigurationApi.class), | ||
@Param( | ||
name = "cc_toolchain", | ||
doc = "C++ toolchain provider to be used.", | ||
positional = false, | ||
named = true, | ||
type = CcToolchainProviderApi.class), | ||
@Param( | ||
name = "srcs", | ||
doc = "The list of source files to be compiled, see cc_library.srcs", | ||
positional = false, | ||
named = true, | ||
defaultValue = "[]", | ||
type = SkylarkList.class), | ||
@Param( | ||
name = "hdrs", | ||
doc = "The list of public headers to be provided to dependents, see cc_library.hdrs", | ||
positional = false, | ||
named = true, | ||
defaultValue = "[]", | ||
type = SkylarkList.class), | ||
@Param( | ||
name = "includes", | ||
doc = "Include directories", | ||
positional = false, | ||
named = true, | ||
noneable = true, | ||
defaultValue = "[]", | ||
allowedTypes = { | ||
@ParamType(type = SkylarkNestedSet.class), | ||
@ParamType(type = SkylarkList.class) | ||
}), | ||
@Param( | ||
name = "copts", | ||
doc = "Additional list of compiler options.", | ||
positional = false, | ||
named = true, | ||
noneable = true, | ||
defaultValue = "None", | ||
allowedTypes = { | ||
@ParamType(type = SkylarkNestedSet.class), | ||
@ParamType(type = NoneType.class) | ||
}), | ||
@Param( | ||
name = "cc_compilation_infos", | ||
doc = "cc_compilation_info instances affecting compilation, e.g. from dependencies", | ||
positional = false, | ||
named = true, | ||
defaultValue = "[]", | ||
type = SkylarkList.class) | ||
}) | ||
CompilationInfoT compile( | ||
SkylarkRuleContext skylarkRuleContext, | ||
FeatureConfigurationT skylarkFeatureConfiguration, | ||
CcToolchainProviderT skylarkCcToolchainProvider, | ||
SkylarkList<Artifact> sources, | ||
SkylarkList<Artifact> headers, | ||
Object skylarkIncludes, | ||
Object skylarkCopts, | ||
SkylarkList<CcCompilationInfoT> ccCompilationInfos) | ||
throws EvalException; | ||
|
||
@SkylarkCallable( | ||
name = "link", | ||
documented = false, | ||
parameters = { | ||
@Param( | ||
name = "ctx", | ||
positional = false, | ||
named = true, | ||
type = SkylarkRuleContextApi.class, | ||
doc = "The rule context."), | ||
@Param( | ||
name = "feature_configuration", | ||
doc = "Feature configuration to be queried.", | ||
positional = false, | ||
named = true, | ||
type = FeatureConfigurationApi.class), | ||
@Param( | ||
name = "cc_toolchain", | ||
doc = "C++ toolchain provider to be used.", | ||
positional = false, | ||
named = true, | ||
type = CcToolchainProviderApi.class), | ||
@Param( | ||
name = "cc_compilation_outputs", | ||
doc = "List of object files to be linked.", | ||
positional = false, | ||
named = true, | ||
defaultValue = "[]", | ||
type = CcCompilationOutputsApi.class), | ||
@Param( | ||
name = "linkopts", | ||
doc = "Additional list of linker options.", | ||
positional = false, | ||
named = true, | ||
defaultValue = "[]", | ||
noneable = true, | ||
allowedTypes = { | ||
@ParamType(type = SkylarkList.class), | ||
@ParamType(type = SkylarkNestedSet.class) | ||
}), | ||
@Param( | ||
name = "dynamic_library", | ||
doc = "Dynamic library artifact.", | ||
positional = false, | ||
named = true, | ||
defaultValue = "None", | ||
noneable = true, | ||
allowedTypes = {@ParamType(type = NoneType.class), @ParamType(type = Artifact.class)}), | ||
@Param( | ||
name = "cc_linking_infos", | ||
doc = "cc_linking_info instances affecting linking, e.g. from dependencies", | ||
positional = false, | ||
named = true, | ||
noneable = true, | ||
defaultValue = "[]", | ||
type = SkylarkList.class), | ||
@Param( | ||
name = "neverlink", | ||
doc = "True if this should never be linked against other libraries.", | ||
positional = false, | ||
named = true, | ||
defaultValue = "False"), | ||
}) | ||
LinkingInfoT link( | ||
SkylarkRuleContext skylarkRuleContext, | ||
FeatureConfigurationT skylarkFeatureConfiguration, | ||
CcToolchainProviderT skylarkCcToolchainProvider, | ||
CcCompilationOutputsT ccCompilationOutputs, | ||
Object skylarkLinkopts, | ||
Object dynamicLibrary, | ||
SkylarkList<CcLinkingInfoT> skylarkCcLinkingInfos, | ||
boolean neverLink) | ||
throws InterruptedException, EvalException; | ||
} |
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
123 changes: 123 additions & 0 deletions
123
src/test/java/com/google/devtools/build/lib/rules/cpp/SkylarkCcCommonTestHelper.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,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'],", | ||
")"); | ||
} | ||
} |