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" |
| 26 | ) |
| 27 | |
| 28 | // Creates a stub shared library based on the provided version file. |
| 29 | // |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 30 | // Example: |
| 31 | // |
| 32 | // llndk_library { |
Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 33 | // name: "libfoo", |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 34 | // symbol_file: "libfoo.map.txt", |
| 35 | // export_include_dirs: ["include_vndk"], |
| 36 | // } |
| 37 | // |
| 38 | type llndkLibraryProperties struct { |
| 39 | // Relative path to the symbol map. |
| 40 | // An example file can be seen here: TODO(danalbert): Make an example. |
| 41 | Symbol_file string |
| 42 | |
| 43 | // Whether to export any headers as -isystem instead of -I. Mainly for use by |
| 44 | // bionic/libc. |
| 45 | Export_headers_as_system bool |
| 46 | |
| 47 | // Which headers to process with versioner. This really only handles |
| 48 | // bionic/libc/include right now. |
| 49 | Export_preprocessed_headers []string |
| 50 | |
| 51 | // Whether the system library uses symbol versions. |
| 52 | Unversioned bool |
| 53 | } |
| 54 | |
| 55 | type llndkStubDecorator struct { |
| 56 | *libraryDecorator |
| 57 | |
| 58 | Properties llndkLibraryProperties |
| 59 | |
| 60 | exportHeadersTimestamp android.OptionalPath |
| 61 | versionScriptPath android.ModuleGenPath |
| 62 | } |
| 63 | |
| 64 | func (stub *llndkStubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects { |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 65 | objs, versionScript := compileStubLibrary(ctx, flags, stub.Properties.Symbol_file, "current", "--vndk") |
| 66 | stub.versionScriptPath = versionScript |
| 67 | return objs |
| 68 | } |
| 69 | |
| 70 | func (stub *llndkStubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { |
| 71 | return Deps{} |
| 72 | } |
| 73 | |
Dan Willemsen | 01a9059 | 2017-04-07 15:21:13 -0700 | [diff] [blame] | 74 | func (stub *llndkStubDecorator) Name(name string) string { |
| 75 | return name + llndkLibrarySuffix |
| 76 | } |
| 77 | |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 78 | func (stub *llndkStubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 79 | stub.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), |
| 80 | llndkLibrarySuffix) |
| 81 | return stub.libraryDecorator.linkerFlags(ctx, flags) |
| 82 | } |
| 83 | |
| 84 | func (stub *llndkStubDecorator) processHeaders(ctx ModuleContext, srcHeaderDir string, outDir android.ModuleGenPath) android.Path { |
| 85 | srcDir := android.PathForModuleSrc(ctx, srcHeaderDir) |
| 86 | srcFiles := ctx.Glob(filepath.Join(srcDir.String(), "**/*.h"), nil) |
| 87 | |
| 88 | var installPaths []android.WritablePath |
| 89 | for _, header := range srcFiles { |
| 90 | headerDir := filepath.Dir(header.String()) |
| 91 | relHeaderDir, err := filepath.Rel(srcDir.String(), headerDir) |
| 92 | if err != nil { |
| 93 | ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", |
| 94 | srcDir.String(), headerDir, err) |
| 95 | continue |
| 96 | } |
| 97 | |
| 98 | installPaths = append(installPaths, outDir.Join(ctx, relHeaderDir, header.Base())) |
| 99 | } |
| 100 | |
| 101 | return processHeadersWithVersioner(ctx, srcDir, outDir, srcFiles, installPaths) |
| 102 | } |
| 103 | |
| 104 | func (stub *llndkStubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, |
| 105 | objs Objects) android.Path { |
| 106 | |
| 107 | if !stub.Properties.Unversioned { |
| 108 | linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String() |
| 109 | flags.LdFlags = append(flags.LdFlags, linkerScriptFlag) |
| 110 | } |
| 111 | |
| 112 | if len(stub.Properties.Export_preprocessed_headers) > 0 { |
| 113 | genHeaderOutDir := android.PathForModuleGen(ctx, "include") |
| 114 | |
| 115 | var timestampFiles android.Paths |
| 116 | for _, dir := range stub.Properties.Export_preprocessed_headers { |
| 117 | timestampFiles = append(timestampFiles, stub.processHeaders(ctx, dir, genHeaderOutDir)) |
| 118 | } |
| 119 | |
| 120 | includePrefix := "-I " |
| 121 | if stub.Properties.Export_headers_as_system { |
| 122 | includePrefix = "-isystem " |
| 123 | } |
| 124 | |
| 125 | stub.reexportFlags([]string{includePrefix + " " + genHeaderOutDir.String()}) |
| 126 | stub.reexportDeps(timestampFiles) |
| 127 | } |
| 128 | |
| 129 | if stub.Properties.Export_headers_as_system { |
| 130 | stub.exportIncludes(ctx, "-isystem") |
| 131 | stub.libraryDecorator.flagExporter.Properties.Export_include_dirs = []string{} |
| 132 | } |
| 133 | |
| 134 | return stub.libraryDecorator.link(ctx, flags, deps, objs) |
| 135 | } |
| 136 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 137 | func newLLndkStubLibrary() *Module { |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 138 | module, library := NewLibrary(android.DeviceSupported) |
| 139 | library.BuildOnlyShared() |
| 140 | module.stl = nil |
| 141 | module.sanitize = nil |
| 142 | library.StripProperties.Strip.None = true |
| 143 | |
| 144 | stub := &llndkStubDecorator{ |
| 145 | libraryDecorator: library, |
| 146 | } |
| 147 | module.compiler = stub |
| 148 | module.linker = stub |
| 149 | module.installer = nil |
| 150 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 151 | module.AddProperties( |
| 152 | &stub.Properties, |
| 153 | &library.MutatedProperties, |
| 154 | &library.flagExporter.Properties) |
| 155 | |
| 156 | return module |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 159 | func llndkLibraryFactory() android.Module { |
| 160 | module := newLLndkStubLibrary() |
| 161 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth) |
| 162 | return module |
Dan Willemsen | b916b80 | 2017-03-19 13:44:32 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | func init() { |
| 166 | android.RegisterModuleType("llndk_library", llndkLibraryFactory) |
| 167 | } |