Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 1 | // Copyright 2017 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 | |
| 17 | import ( |
| 18 | "path/filepath" |
| 19 | "strings" |
| 20 | |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 21 | "android/soong/android" |
| 22 | ) |
| 23 | |
| 24 | var ( |
| 25 | llndkLibrarySuffix = ".llndk" |
Jiyong Park | 2a45412 | 2017-10-19 15:59:33 +0900 | [diff] [blame] | 26 | llndkHeadersSuffix = ".llndk" |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | // Creates a stub shared library based on the provided version file. |
| 30 | // |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 31 | // Example: |
| 32 | // |
| 33 | // llndk_library { |
Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 34 | // name: "libfoo", |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 35 | // symbol_file: "libfoo.map.txt", |
| 36 | // export_include_dirs: ["include_vndk"], |
| 37 | // } |
| 38 | // |
| 39 | type llndkLibraryProperties struct { |
| 40 | // Relative path to the symbol map. |
| 41 | // An example file can be seen here: TODO(danalbert): Make an example. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame^] | 42 | Symbol_file *string |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 43 | |
| 44 | // Whether to export any headers as -isystem instead of -I. Mainly for use by |
| 45 | // bionic/libc. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame^] | 46 | Export_headers_as_system *bool |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 47 | |
| 48 | // Which headers to process with versioner. This really only handles |
| 49 | // bionic/libc/include right now. |
| 50 | Export_preprocessed_headers []string |
| 51 | |
| 52 | // Whether the system library uses symbol versions. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame^] | 53 | Unversioned *bool |
Jiyong Park | 82e2bf3 | 2017-08-16 14:05:54 +0900 | [diff] [blame] | 54 | |
| 55 | // whether this module can be directly depended upon by libs that are installed to /vendor. |
| 56 | // When set to false, this module can only be depended on by VNDK libraries, not vendor |
| 57 | // libraries. This effectively hides this module from vendors. Default value is true. |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame^] | 58 | Vendor_available *bool |
Jiyong Park | 2a45412 | 2017-10-19 15:59:33 +0900 | [diff] [blame] | 59 | |
| 60 | // list of llndk headers to re-export include directories from. |
| 61 | Export_llndk_headers []string `android:"arch_variant"` |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | type llndkStubDecorator struct { |
| 65 | *libraryDecorator |
| 66 | |
| 67 | Properties llndkLibraryProperties |
| 68 | |
| 69 | exportHeadersTimestamp android.OptionalPath |
| 70 | versionScriptPath android.ModuleGenPath |
| 71 | } |
| 72 | |
George Burgess IV | f5310e3 | 2017-07-19 11:39:53 -0700 | [diff] [blame] | 73 | func (stub *llndkStubDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { |
| 74 | flags = stub.baseCompiler.compilerFlags(ctx, flags) |
| 75 | return addStubLibraryCompilerFlags(flags) |
| 76 | } |
| 77 | |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 78 | func (stub *llndkStubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame^] | 79 | objs, versionScript := compileStubLibrary(ctx, flags, String(stub.Properties.Symbol_file), "current", "--vndk") |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 80 | stub.versionScriptPath = versionScript |
| 81 | return objs |
| 82 | } |
| 83 | |
| 84 | func (stub *llndkStubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Jiyong Park | 2a45412 | 2017-10-19 15:59:33 +0900 | [diff] [blame] | 85 | headers := addSuffix(stub.Properties.Export_llndk_headers, llndkHeadersSuffix) |
| 86 | deps.HeaderLibs = append(deps.HeaderLibs, headers...) |
| 87 | deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, headers...) |
| 88 | return deps |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 91 | func (stub *llndkStubDecorator) Name(name string) string { |
| 92 | return name + llndkLibrarySuffix |
| 93 | } |
| 94 | |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 95 | func (stub *llndkStubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 96 | stub.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), |
| 97 | llndkLibrarySuffix) |
| 98 | return stub.libraryDecorator.linkerFlags(ctx, flags) |
| 99 | } |
| 100 | |
| 101 | func (stub *llndkStubDecorator) processHeaders(ctx ModuleContext, srcHeaderDir string, outDir android.ModuleGenPath) android.Path { |
| 102 | srcDir := android.PathForModuleSrc(ctx, srcHeaderDir) |
| 103 | srcFiles := ctx.Glob(filepath.Join(srcDir.String(), "**/*.h"), nil) |
| 104 | |
| 105 | var installPaths []android.WritablePath |
| 106 | for _, header := range srcFiles { |
| 107 | headerDir := filepath.Dir(header.String()) |
| 108 | relHeaderDir, err := filepath.Rel(srcDir.String(), headerDir) |
| 109 | if err != nil { |
| 110 | ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", |
| 111 | srcDir.String(), headerDir, err) |
| 112 | continue |
| 113 | } |
| 114 | |
| 115 | installPaths = append(installPaths, outDir.Join(ctx, relHeaderDir, header.Base())) |
| 116 | } |
| 117 | |
| 118 | return processHeadersWithVersioner(ctx, srcDir, outDir, srcFiles, installPaths) |
| 119 | } |
| 120 | |
| 121 | func (stub *llndkStubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, |
| 122 | objs Objects) android.Path { |
| 123 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame^] | 124 | if !Bool(stub.Properties.Unversioned) { |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 125 | linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String() |
| 126 | flags.LdFlags = append(flags.LdFlags, linkerScriptFlag) |
| 127 | } |
| 128 | |
| 129 | if len(stub.Properties.Export_preprocessed_headers) > 0 { |
| 130 | genHeaderOutDir := android.PathForModuleGen(ctx, "include") |
| 131 | |
| 132 | var timestampFiles android.Paths |
| 133 | for _, dir := range stub.Properties.Export_preprocessed_headers { |
| 134 | timestampFiles = append(timestampFiles, stub.processHeaders(ctx, dir, genHeaderOutDir)) |
| 135 | } |
| 136 | |
| 137 | includePrefix := "-I " |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame^] | 138 | if Bool(stub.Properties.Export_headers_as_system) { |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 139 | includePrefix = "-isystem " |
| 140 | } |
| 141 | |
| 142 | stub.reexportFlags([]string{includePrefix + " " + genHeaderOutDir.String()}) |
| 143 | stub.reexportDeps(timestampFiles) |
| 144 | } |
| 145 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame^] | 146 | if Bool(stub.Properties.Export_headers_as_system) { |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 147 | stub.exportIncludes(ctx, "-isystem") |
| 148 | stub.libraryDecorator.flagExporter.Properties.Export_include_dirs = []string{} |
| 149 | } |
| 150 | |
| 151 | return stub.libraryDecorator.link(ctx, flags, deps, objs) |
| 152 | } |
| 153 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 154 | func newLLndkStubLibrary() *Module { |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 155 | module, library := NewLibrary(android.DeviceSupported) |
| 156 | library.BuildOnlyShared() |
| 157 | module.stl = nil |
| 158 | module.sanitize = nil |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame^] | 159 | library.StripProperties.Strip.None = BoolPtr(true) |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 160 | |
| 161 | stub := &llndkStubDecorator{ |
| 162 | libraryDecorator: library, |
| 163 | } |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame^] | 164 | stub.Properties.Vendor_available = BoolPtr(true) |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 165 | module.compiler = stub |
| 166 | module.linker = stub |
| 167 | module.installer = nil |
| 168 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 169 | module.AddProperties( |
| 170 | &stub.Properties, |
| 171 | &library.MutatedProperties, |
| 172 | &library.flagExporter.Properties) |
| 173 | |
| 174 | return module |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 177 | func llndkLibraryFactory() android.Module { |
| 178 | module := newLLndkStubLibrary() |
| 179 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth) |
| 180 | return module |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Jiyong Park | 2a45412 | 2017-10-19 15:59:33 +0900 | [diff] [blame] | 183 | type llndkHeadersDecorator struct { |
| 184 | *libraryDecorator |
| 185 | } |
| 186 | |
| 187 | func (headers *llndkHeadersDecorator) Name(name string) string { |
| 188 | return name + llndkHeadersSuffix |
| 189 | } |
| 190 | |
| 191 | func llndkHeadersFactory() android.Module { |
| 192 | module, library := NewLibrary(android.DeviceSupported) |
| 193 | library.HeaderOnly() |
| 194 | library.setStatic() |
| 195 | |
| 196 | decorator := &llndkHeadersDecorator{ |
| 197 | libraryDecorator: library, |
| 198 | } |
| 199 | |
| 200 | module.compiler = nil |
| 201 | module.linker = decorator |
| 202 | module.installer = nil |
| 203 | |
| 204 | module.AddProperties(&library.MutatedProperties, &library.flagExporter.Properties) |
| 205 | |
| 206 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth) |
| 207 | |
| 208 | return module |
| 209 | } |
| 210 | |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 211 | func init() { |
| 212 | android.RegisterModuleType("llndk_library", llndkLibraryFactory) |
Jiyong Park | 2a45412 | 2017-10-19 15:59:33 +0900 | [diff] [blame] | 213 | android.RegisterModuleType("llndk_headers", llndkHeadersFactory) |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 214 | } |