blob: f88b801ef677833bbdb193b4dcb5faf02ec4e1fd [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"
20)
Paul Duffin1c6c1c72020-02-21 10:28:43 +000021
22func init() {
23 RegisterLibraryHeadersBuildComponents(android.InitRegistrationContext)
Paul Duffin91756d22020-02-21 16:29:57 +000024
25 // Register sdk member types.
26 android.RegisterSdkMemberType(headersLibrarySdkMemberType)
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000027
28 android.RegisterBp2BuildMutator("cc_library_headers", CcLibraryHeadersBp2Build)
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,
38 },
Paul Duffin91756d22020-02-21 16:29:57 +000039 },
40 prebuiltModuleType: "cc_prebuilt_library_headers",
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000041 noOutputFiles: true,
Paul Duffin1c6c1c72020-02-21 10:28:43 +000042}
43
44func RegisterLibraryHeadersBuildComponents(ctx android.RegistrationContext) {
45 ctx.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Paul Duffinf5ea9e12020-02-21 10:57:00 +000046 ctx.RegisterModuleType("cc_prebuilt_library_headers", prebuiltLibraryHeaderFactory)
Paul Duffin1c6c1c72020-02-21 10:28:43 +000047}
48
Liz Kammerb6a55bf2021-04-12 15:42:51 -040049type libraryHeaderBazelHander struct {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000050 android.BazelHandler
Liz Kammerb6a55bf2021-04-12 15:42:51 -040051
52 module *Module
53 library *libraryDecorator
54}
55
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000056func (h *libraryHeaderBazelHander) GenerateBazelBuildActions(ctx android.ModuleContext, label string) bool {
Liz Kammerb6a55bf2021-04-12 15:42:51 -040057 bazelCtx := ctx.Config().BazelContext
58 ccInfo, ok, err := bazelCtx.GetCcInfo(label, ctx.Arch().ArchType)
59 if err != nil {
60 ctx.ModuleErrorf("Error getting Bazel CcInfo: %s", err)
61 return false
62 }
63 if !ok {
64 return false
65 }
66
67 outputPaths := ccInfo.OutputFiles
68 if len(outputPaths) != 1 {
69 ctx.ModuleErrorf("expected exactly one output file for %q, but got %q", label, outputPaths)
70 return false
71 }
72
73 outputPath := android.PathForBazelOut(ctx, outputPaths[0])
74 h.module.outputFile = android.OptionalPathForPath(outputPath)
75
76 // HeaderLibraryInfo is an empty struct to indicate to dependencies that this is a header library
77 ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{})
78
Chris Parsons94a0bba2021-06-04 15:03:47 -040079 h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo)
Liz Kammerb6a55bf2021-04-12 15:42:51 -040080
81 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
82 // validation will fail. For now, set this to an empty list.
83 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
84 h.library.collectedSnapshotHeaders = android.Paths{}
85
86 return true
87}
88
Paul Duffin1c6c1c72020-02-21 10:28:43 +000089// cc_library_headers contains a set of c/c++ headers which are imported by
90// other soong cc modules using the header_libs property. For best practices,
91// use export_include_dirs property or LOCAL_EXPORT_C_INCLUDE_DIRS for
92// Make.
93func LibraryHeaderFactory() android.Module {
94 module, library := NewLibrary(android.HostAndDeviceSupported)
95 library.HeaderOnly()
Paul Duffin91756d22020-02-21 16:29:57 +000096 module.sdkMemberTypes = []android.SdkMemberType{headersLibrarySdkMemberType}
Liz Kammerb6a55bf2021-04-12 15:42:51 -040097 module.bazelHandler = &libraryHeaderBazelHander{module: module, library: library}
Paul Duffin1c6c1c72020-02-21 10:28:43 +000098 return module.Init()
99}
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000100
101// cc_prebuilt_library_headers is a prebuilt version of cc_library_headers
102func prebuiltLibraryHeaderFactory() android.Module {
103 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported)
104 library.HeaderOnly()
105 return module.Init()
106}
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000107
108type bazelCcLibraryHeadersAttributes struct {
Liz Kammer5fad5012021-09-09 14:08:21 -0400109 Hdrs bazel.LabelListAttribute
110 Export_includes bazel.StringListAttribute
111 Export_system_includes bazel.StringListAttribute
112 Deps bazel.LabelListAttribute
113 Implementation_deps bazel.LabelListAttribute
114 System_dynamic_deps bazel.LabelListAttribute
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000115}
116
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000117func CcLibraryHeadersBp2Build(ctx android.TopDownMutatorContext) {
118 module, ok := ctx.Module().(*Module)
119 if !ok {
120 // Not a cc module
121 return
122 }
123
Jingwen Chen12b4c272021-03-10 02:05:59 -0500124 if !module.ConvertWithBp2build(ctx) {
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500125 return
126 }
127
Jingwen Chenc7846f32021-03-18 05:16:10 -0400128 if ctx.ModuleType() != "cc_library_headers" {
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000129 return
130 }
Jingwen Chenc7846f32021-03-18 05:16:10 -0400131
Jingwen Chen882bcc12021-04-27 05:54:20 +0000132 exportedIncludes := bp2BuildParseExportedIncludes(ctx, module)
Jingwen Chen107c0de2021-04-09 10:43:12 +0000133 linkerAttrs := bp2BuildParseLinkerProps(ctx, module)
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000134
135 attrs := &bazelCcLibraryHeadersAttributes{
Liz Kammer5fad5012021-09-09 14:08:21 -0400136 Export_includes: exportedIncludes.Includes,
137 Export_system_includes: exportedIncludes.SystemIncludes,
Liz Kammer7a210ac2021-09-22 15:52:58 -0400138 Implementation_deps: linkerAttrs.implementationDeps,
139 Deps: linkerAttrs.deps,
Liz Kammer5fad5012021-09-09 14:08:21 -0400140 System_dynamic_deps: linkerAttrs.systemDynamicDeps,
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000141 }
142
Liz Kammerfc46bc12021-02-19 11:06:17 -0500143 props := bazel.BazelTargetModuleProperties{
144 Rule_class: "cc_library_headers",
145 Bzl_load_location: "//build/bazel/rules:cc_library_headers.bzl",
146 }
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000147
Liz Kammer2ada09a2021-08-11 00:17:36 -0400148 ctx.CreateBazelTargetModule(module.Name(), props, attrs)
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000149}