blob: dd2f06c21250eab28631d4daa9987a0b6e647807 [file] [log] [blame]
Paul Duffin1c6c1c72020-02-21 10:28:43 +00001// Copyright 2020 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000017import (
18 "android/soong/android"
19 "android/soong/bazel"
Chris Parsonsf874e462022-05-10 13:50:12 -040020 "android/soong/bazel/cquery"
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000021)
Paul Duffin1c6c1c72020-02-21 10:28:43 +000022
23func init() {
24 RegisterLibraryHeadersBuildComponents(android.InitRegistrationContext)
Paul Duffin91756d22020-02-21 16:29:57 +000025
26 // Register sdk member types.
27 android.RegisterSdkMemberType(headersLibrarySdkMemberType)
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000028
Paul Duffin91756d22020-02-21 16:29:57 +000029}
30
31var headersLibrarySdkMemberType = &librarySdkMemberType{
32 SdkMemberTypeBase: android.SdkMemberTypeBase{
Martin Stjernholmcaa47d72020-07-11 04:52:24 +010033 PropertyName: "native_header_libs",
34 SupportsSdk: true,
35 HostOsDependent: true,
Paul Duffin93b750e2019-11-19 19:44:10 +000036 Traits: []android.SdkMemberTrait{
37 nativeBridgeSdkTrait,
Paul Duffin12a0a312021-09-15 17:25:10 +010038 ramdiskImageRequiredSdkTrait,
Paul Duffin63696222021-09-06 10:28:34 +010039 recoveryImageRequiredSdkTrait,
Paul Duffin93b750e2019-11-19 19:44:10 +000040 },
Paul Duffin91756d22020-02-21 16:29:57 +000041 },
42 prebuiltModuleType: "cc_prebuilt_library_headers",
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000043 noOutputFiles: true,
Paul Duffin1c6c1c72020-02-21 10:28:43 +000044}
45
46func RegisterLibraryHeadersBuildComponents(ctx android.RegistrationContext) {
47 ctx.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Paul Duffinf5ea9e12020-02-21 10:57:00 +000048 ctx.RegisterModuleType("cc_prebuilt_library_headers", prebuiltLibraryHeaderFactory)
Paul Duffin1c6c1c72020-02-21 10:28:43 +000049}
50
Chris Parsonsf874e462022-05-10 13:50:12 -040051type libraryHeaderBazelHandler struct {
Liz Kammerb6a55bf2021-04-12 15:42:51 -040052 module *Module
53 library *libraryDecorator
54}
55
Chris Parsons6ce2cf92022-05-20 10:54:17 -040056var _ BazelHandler = (*libraryHeaderBazelHandler)(nil)
57
Chris Parsonsf874e462022-05-10 13:50:12 -040058func (handler *libraryHeaderBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
Liz Kammerb6a55bf2021-04-12 15:42:51 -040059 bazelCtx := ctx.Config().BazelContext
Chris Parsonsf874e462022-05-10 13:50:12 -040060 bazelCtx.QueueBazelRequest(label, cquery.GetCcInfo, android.GetConfigKey(ctx))
61}
62
63func (h *libraryHeaderBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
64 bazelCtx := ctx.Config().BazelContext
65 ccInfo, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
Liz Kammerb6a55bf2021-04-12 15:42:51 -040066 if err != nil {
Chris Parsonsf874e462022-05-10 13:50:12 -040067 ctx.ModuleErrorf(err.Error())
68 return
Liz Kammerb6a55bf2021-04-12 15:42:51 -040069 }
70
71 outputPaths := ccInfo.OutputFiles
72 if len(outputPaths) != 1 {
73 ctx.ModuleErrorf("expected exactly one output file for %q, but got %q", label, outputPaths)
Chris Parsonsf874e462022-05-10 13:50:12 -040074 return
Liz Kammerb6a55bf2021-04-12 15:42:51 -040075 }
76
77 outputPath := android.PathForBazelOut(ctx, outputPaths[0])
78 h.module.outputFile = android.OptionalPathForPath(outputPath)
79
80 // HeaderLibraryInfo is an empty struct to indicate to dependencies that this is a header library
81 ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{})
82
Chris Parsons94a0bba2021-06-04 15:03:47 -040083 h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo)
Liz Kammerb6a55bf2021-04-12 15:42:51 -040084
85 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
86 // validation will fail. For now, set this to an empty list.
87 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
88 h.library.collectedSnapshotHeaders = android.Paths{}
Liz Kammerb6a55bf2021-04-12 15:42:51 -040089}
90
Paul Duffin1c6c1c72020-02-21 10:28:43 +000091// cc_library_headers contains a set of c/c++ headers which are imported by
92// other soong cc modules using the header_libs property. For best practices,
93// use export_include_dirs property or LOCAL_EXPORT_C_INCLUDE_DIRS for
94// Make.
95func LibraryHeaderFactory() android.Module {
96 module, library := NewLibrary(android.HostAndDeviceSupported)
97 library.HeaderOnly()
Paul Duffin91756d22020-02-21 16:29:57 +000098 module.sdkMemberTypes = []android.SdkMemberType{headersLibrarySdkMemberType}
Liz Kammerbe46fcc2021-11-01 15:32:43 -040099 module.bazelable = true
Chris Parsonsf874e462022-05-10 13:50:12 -0400100 module.bazelHandler = &libraryHeaderBazelHandler{module: module, library: library}
Paul Duffin1c6c1c72020-02-21 10:28:43 +0000101 return module.Init()
102}
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000103
104// cc_prebuilt_library_headers is a prebuilt version of cc_library_headers
105func prebuiltLibraryHeaderFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000106 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "")
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000107 library.HeaderOnly()
Liz Kammer3d3b35c2022-01-11 16:02:50 +0000108 module.bazelable = true
109 module.bazelHandler = &ccLibraryBazelHandler{module: module}
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000110 return module.Init()
111}
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000112
113type bazelCcLibraryHeadersAttributes struct {
Liz Kammer1263d9b2021-12-10 14:28:20 -0500114 Hdrs bazel.LabelListAttribute
115 Export_includes bazel.StringListAttribute
116 Export_absolute_includes bazel.StringListAttribute
117 Export_system_includes bazel.StringListAttribute
118 Deps bazel.LabelListAttribute
119 Implementation_deps bazel.LabelListAttribute
120 System_dynamic_deps bazel.LabelListAttribute
Yu Liufc603162022-03-01 15:44:08 -0800121 sdkAttributes
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000122}
123
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400124func libraryHeadersBp2Build(ctx android.TopDownMutatorContext, module *Module) {
Liz Kammere6583482021-10-19 13:56:10 -0400125 baseAttributes := bp2BuildParseBaseProps(ctx, module)
Liz Kammer1263d9b2021-12-10 14:28:20 -0500126 exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, baseAttributes.includes)
Liz Kammere6583482021-10-19 13:56:10 -0400127 linkerAttrs := baseAttributes.linkerAttributes
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000128
129 attrs := &bazelCcLibraryHeadersAttributes{
Liz Kammer1263d9b2021-12-10 14:28:20 -0500130 Export_includes: exportedIncludes.Includes,
131 Export_absolute_includes: exportedIncludes.AbsoluteIncludes,
132 Export_system_includes: exportedIncludes.SystemIncludes,
133 Implementation_deps: linkerAttrs.implementationDeps,
134 Deps: linkerAttrs.deps,
135 System_dynamic_deps: linkerAttrs.systemDynamicDeps,
136 Hdrs: baseAttributes.hdrs,
Yu Liufc603162022-03-01 15:44:08 -0800137 sdkAttributes: bp2BuildParseSdkAttributes(module),
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000138 }
139
Liz Kammerfc46bc12021-02-19 11:06:17 -0500140 props := bazel.BazelTargetModuleProperties{
141 Rule_class: "cc_library_headers",
Liz Kammer2b376bc2022-01-12 12:00:49 -0500142 Bzl_load_location: "//build/bazel/rules/cc:cc_library_headers.bzl",
Liz Kammerfc46bc12021-02-19 11:06:17 -0500143 }
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000144
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000145 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, attrs)
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000146}