blob: e5a555729cd4e667323f24413a3ece0874625680 [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 Duffin91756d22020-02-21 16:29:57 +000036 },
37 prebuiltModuleType: "cc_prebuilt_library_headers",
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000038 noOutputFiles: true,
Paul Duffin1c6c1c72020-02-21 10:28:43 +000039}
40
41func RegisterLibraryHeadersBuildComponents(ctx android.RegistrationContext) {
42 ctx.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Paul Duffinf5ea9e12020-02-21 10:57:00 +000043 ctx.RegisterModuleType("cc_prebuilt_library_headers", prebuiltLibraryHeaderFactory)
Paul Duffin1c6c1c72020-02-21 10:28:43 +000044}
45
46// cc_library_headers contains a set of c/c++ headers which are imported by
47// other soong cc modules using the header_libs property. For best practices,
48// use export_include_dirs property or LOCAL_EXPORT_C_INCLUDE_DIRS for
49// Make.
50func LibraryHeaderFactory() android.Module {
51 module, library := NewLibrary(android.HostAndDeviceSupported)
52 library.HeaderOnly()
Paul Duffin91756d22020-02-21 16:29:57 +000053 module.sdkMemberTypes = []android.SdkMemberType{headersLibrarySdkMemberType}
Paul Duffin1c6c1c72020-02-21 10:28:43 +000054 return module.Init()
55}
Paul Duffinf5ea9e12020-02-21 10:57:00 +000056
57// cc_prebuilt_library_headers is a prebuilt version of cc_library_headers
58func prebuiltLibraryHeaderFactory() android.Module {
59 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported)
60 library.HeaderOnly()
61 return module.Init()
62}
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000063
64type bazelCcLibraryHeadersAttributes struct {
65 Hdrs bazel.LabelList
66 Includes bazel.LabelList
67 Deps bazel.LabelList
68}
69
70type bazelCcLibraryHeaders struct {
71 android.BazelTargetModuleBase
72 bazelCcLibraryHeadersAttributes
73}
74
75func BazelCcLibraryHeadersFactory() android.Module {
76 module := &bazelCcLibraryHeaders{}
77 module.AddProperties(&module.bazelCcLibraryHeadersAttributes)
78 android.InitBazelTargetModule(module)
79 return module
80}
81
82func CcLibraryHeadersBp2Build(ctx android.TopDownMutatorContext) {
83 module, ok := ctx.Module().(*Module)
84 if !ok {
85 // Not a cc module
86 return
87 }
88
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050089 if !module.Properties.Bazel_module.Bp2build_available {
90 return
91 }
92
Rupert Shuttleworth54e78412021-02-15 11:04:32 +000093 lib, ok := module.linker.(*libraryDecorator)
94 if !ok {
95 // Not a cc_library module
96 return
97 }
98 if !lib.header() {
99 // Not a cc_library_headers module
100 return
101 }
102
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000103 // list of directories that will be added to the include path (using -I) for this
104 // module and any module that links against this module.
105 includeDirs := lib.flagExporter.Properties.Export_system_include_dirs
106 includeDirs = append(includeDirs, lib.flagExporter.Properties.Export_include_dirs...)
107 includeDirLabels := android.BazelLabelForModuleSrc(ctx, includeDirs)
108
109 var includeDirGlobs []string
110 for _, includeDir := range includeDirs {
111 includeDirGlobs = append(includeDirGlobs, includeDir+"/**/*.h")
112 }
113
114 headerLabels := android.BazelLabelForModuleSrc(ctx, includeDirGlobs)
115
116 // list of modules that should only provide headers for this module.
117 var headerLibs []string
118 for _, linkerProps := range lib.linkerProps() {
119 if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
120 headerLibs = baseLinkerProps.Export_header_lib_headers
121 break
122 }
123 }
124 headerLibLabels := android.BazelLabelForModuleDeps(ctx, headerLibs)
125
126 attrs := &bazelCcLibraryHeadersAttributes{
127 Includes: includeDirLabels,
128 Hdrs: headerLabels,
129 Deps: headerLibLabels,
130 }
131
132 props := bazel.NewBazelTargetModuleProperties(
133 module.Name(),
134 "cc_library_headers",
135 "//build/bazel/rules:cc_library_headers.bzl",
136 )
137
138 ctx.CreateBazelTargetModule(BazelCcLibraryHeadersFactory, props, attrs)
139}
140
141func (m *bazelCcLibraryHeaders) Name() string {
142 return m.BaseModuleName()
143}
144
145func (m *bazelCcLibraryHeaders) GenerateAndroidBuildActions(ctx android.ModuleContext) {}