Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/bazel" |
| 20 | ) |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 21 | |
| 22 | func init() { |
| 23 | RegisterLibraryHeadersBuildComponents(android.InitRegistrationContext) |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 24 | |
| 25 | // Register sdk member types. |
| 26 | android.RegisterSdkMemberType(headersLibrarySdkMemberType) |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 27 | |
| 28 | android.RegisterBp2BuildMutator("cc_library_headers", CcLibraryHeadersBp2Build) |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | var headersLibrarySdkMemberType = &librarySdkMemberType{ |
| 32 | SdkMemberTypeBase: android.SdkMemberTypeBase{ |
Martin Stjernholm | caa47d7 | 2020-07-11 04:52:24 +0100 | [diff] [blame] | 33 | PropertyName: "native_header_libs", |
| 34 | SupportsSdk: true, |
| 35 | HostOsDependent: true, |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 36 | }, |
| 37 | prebuiltModuleType: "cc_prebuilt_library_headers", |
Martin Stjernholm | cd07bce | 2020-03-10 22:37:59 +0000 | [diff] [blame] | 38 | noOutputFiles: true, |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | func RegisterLibraryHeadersBuildComponents(ctx android.RegistrationContext) { |
| 42 | ctx.RegisterModuleType("cc_library_headers", LibraryHeaderFactory) |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 43 | ctx.RegisterModuleType("cc_prebuilt_library_headers", prebuiltLibraryHeaderFactory) |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 44 | } |
| 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. |
| 50 | func LibraryHeaderFactory() android.Module { |
| 51 | module, library := NewLibrary(android.HostAndDeviceSupported) |
| 52 | library.HeaderOnly() |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 53 | module.sdkMemberTypes = []android.SdkMemberType{headersLibrarySdkMemberType} |
Paul Duffin | 1c6c1c7 | 2020-02-21 10:28:43 +0000 | [diff] [blame] | 54 | return module.Init() |
| 55 | } |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 56 | |
| 57 | // cc_prebuilt_library_headers is a prebuilt version of cc_library_headers |
| 58 | func prebuiltLibraryHeaderFactory() android.Module { |
| 59 | module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported) |
| 60 | library.HeaderOnly() |
| 61 | return module.Init() |
| 62 | } |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 63 | |
| 64 | type bazelCcLibraryHeadersAttributes struct { |
| 65 | Hdrs bazel.LabelList |
| 66 | Includes bazel.LabelList |
| 67 | Deps bazel.LabelList |
| 68 | } |
| 69 | |
| 70 | type bazelCcLibraryHeaders struct { |
| 71 | android.BazelTargetModuleBase |
| 72 | bazelCcLibraryHeadersAttributes |
| 73 | } |
| 74 | |
| 75 | func BazelCcLibraryHeadersFactory() android.Module { |
| 76 | module := &bazelCcLibraryHeaders{} |
| 77 | module.AddProperties(&module.bazelCcLibraryHeadersAttributes) |
| 78 | android.InitBazelTargetModule(module) |
| 79 | return module |
| 80 | } |
| 81 | |
| 82 | func CcLibraryHeadersBp2Build(ctx android.TopDownMutatorContext) { |
| 83 | module, ok := ctx.Module().(*Module) |
| 84 | if !ok { |
| 85 | // Not a cc module |
| 86 | return |
| 87 | } |
| 88 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame^] | 89 | if !module.ConvertWithBp2build(ctx) { |
Jingwen Chen | 8c1b97e | 2021-02-18 03:21:34 -0500 | [diff] [blame] | 90 | return |
| 91 | } |
| 92 | |
Jingwen Chen | c7846f3 | 2021-03-18 05:16:10 -0400 | [diff] [blame] | 93 | if ctx.ModuleType() != "cc_library_headers" { |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 94 | return |
| 95 | } |
Jingwen Chen | c7846f3 | 2021-03-18 05:16:10 -0400 | [diff] [blame] | 96 | |
| 97 | lib, _ := module.linker.(*libraryDecorator) |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 98 | |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 99 | // list of directories that will be added to the include path (using -I) for this |
| 100 | // module and any module that links against this module. |
| 101 | includeDirs := lib.flagExporter.Properties.Export_system_include_dirs |
| 102 | includeDirs = append(includeDirs, lib.flagExporter.Properties.Export_include_dirs...) |
| 103 | includeDirLabels := android.BazelLabelForModuleSrc(ctx, includeDirs) |
| 104 | |
| 105 | var includeDirGlobs []string |
| 106 | for _, includeDir := range includeDirs { |
| 107 | includeDirGlobs = append(includeDirGlobs, includeDir+"/**/*.h") |
| 108 | } |
| 109 | |
| 110 | headerLabels := android.BazelLabelForModuleSrc(ctx, includeDirGlobs) |
| 111 | |
| 112 | // list of modules that should only provide headers for this module. |
| 113 | var headerLibs []string |
| 114 | for _, linkerProps := range lib.linkerProps() { |
| 115 | if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok { |
| 116 | headerLibs = baseLinkerProps.Export_header_lib_headers |
| 117 | break |
| 118 | } |
| 119 | } |
| 120 | headerLibLabels := android.BazelLabelForModuleDeps(ctx, headerLibs) |
| 121 | |
| 122 | attrs := &bazelCcLibraryHeadersAttributes{ |
| 123 | Includes: includeDirLabels, |
| 124 | Hdrs: headerLabels, |
| 125 | Deps: headerLibLabels, |
| 126 | } |
| 127 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 128 | props := bazel.BazelTargetModuleProperties{ |
| 129 | Rule_class: "cc_library_headers", |
| 130 | Bzl_load_location: "//build/bazel/rules:cc_library_headers.bzl", |
| 131 | } |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 132 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 133 | ctx.CreateBazelTargetModule(BazelCcLibraryHeadersFactory, module.Name(), props, attrs) |
Rupert Shuttleworth | 54e7841 | 2021-02-15 11:04:32 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | func (m *bazelCcLibraryHeaders) Name() string { |
| 137 | return m.BaseModuleName() |
| 138 | } |
| 139 | |
| 140 | func (m *bazelCcLibraryHeaders) GenerateAndroidBuildActions(ctx android.ModuleContext) {} |