Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 1 | // Copyright 2016 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 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 17 | import ( |
Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 18 | "strings" |
| 19 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 20 | "android/soong/android" |
| 21 | ) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 22 | |
Thiébaud Weksteen | 588ed66 | 2020-11-19 16:47:41 +0100 | [diff] [blame] | 23 | // StripProperties defines the type of stripping applied to the module. |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 24 | type StripProperties struct { |
| 25 | Strip struct { |
Colin Cross | 2254cff | 2020-12-01 09:06:10 -0800 | [diff] [blame] | 26 | // none forces all stripping to be disabled. |
| 27 | // Device modules default to stripping enabled leaving mini debuginfo. |
| 28 | // Host modules default to stripping disabled, but can be enabled by setting any other |
| 29 | // strip boolean property. |
Thiébaud Weksteen | 588ed66 | 2020-11-19 16:47:41 +0100 | [diff] [blame] | 30 | None *bool `android:"arch_variant"` |
| 31 | |
Colin Cross | 2254cff | 2020-12-01 09:06:10 -0800 | [diff] [blame] | 32 | // all forces stripping everything, including the mini debug info. |
Thiébaud Weksteen | 588ed66 | 2020-11-19 16:47:41 +0100 | [diff] [blame] | 33 | All *bool `android:"arch_variant"` |
| 34 | |
Colin Cross | 2254cff | 2020-12-01 09:06:10 -0800 | [diff] [blame] | 35 | // keep_symbols enables stripping but keeps all symbols. |
Thiébaud Weksteen | 588ed66 | 2020-11-19 16:47:41 +0100 | [diff] [blame] | 36 | Keep_symbols *bool `android:"arch_variant"` |
| 37 | |
Colin Cross | 2254cff | 2020-12-01 09:06:10 -0800 | [diff] [blame] | 38 | // keep_symbols_list specifies a list of symbols to keep if keep_symbols is enabled. |
| 39 | // If it is unset then all symbols are kept. |
Thiébaud Weksteen | 588ed66 | 2020-11-19 16:47:41 +0100 | [diff] [blame] | 40 | Keep_symbols_list []string `android:"arch_variant"` |
| 41 | |
Colin Cross | 2254cff | 2020-12-01 09:06:10 -0800 | [diff] [blame] | 42 | // keep_symbols_and_debug_frame enables stripping but keeps all symbols and debug frames. |
Thiébaud Weksteen | 588ed66 | 2020-11-19 16:47:41 +0100 | [diff] [blame] | 43 | Keep_symbols_and_debug_frame *bool `android:"arch_variant"` |
Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 44 | } `android:"arch_variant"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Thiébaud Weksteen | 588ed66 | 2020-11-19 16:47:41 +0100 | [diff] [blame] | 47 | // Stripper defines the stripping actions and properties for a module. |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 48 | type Stripper struct { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 49 | StripProperties StripProperties |
| 50 | } |
| 51 | |
Thiébaud Weksteen | 588ed66 | 2020-11-19 16:47:41 +0100 | [diff] [blame] | 52 | // NeedsStrip determines if stripping is required for a module. |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 53 | func (stripper *Stripper) NeedsStrip(actx android.ModuleContext) bool { |
Colin Cross | 2254cff | 2020-12-01 09:06:10 -0800 | [diff] [blame] | 54 | forceDisable := Bool(stripper.StripProperties.Strip.None) |
| 55 | defaultEnable := (!actx.Config().KatiEnabled() || actx.Device()) |
| 56 | forceEnable := Bool(stripper.StripProperties.Strip.All) || |
| 57 | Bool(stripper.StripProperties.Strip.Keep_symbols) || |
| 58 | Bool(stripper.StripProperties.Strip.Keep_symbols_and_debug_frame) |
Elliott Hughes | 8cf3236 | 2022-11-11 00:59:14 +0000 | [diff] [blame] | 59 | return !forceDisable && (forceEnable || defaultEnable) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 62 | func (stripper *Stripper) strip(actx android.ModuleContext, in android.Path, out android.ModuleOutPath, |
| 63 | flags StripFlags, isStaticLib bool) { |
| 64 | if actx.Darwin() { |
Chris Parsons | bf4f55f | 2020-11-23 17:02:44 -0500 | [diff] [blame] | 65 | transformDarwinStrip(actx, in, out) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 66 | } else { |
Colin Cross | 9a959cd | 2018-09-05 14:21:15 -0700 | [diff] [blame] | 67 | if Bool(stripper.StripProperties.Strip.Keep_symbols) { |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 68 | flags.StripKeepSymbols = true |
Christopher Ferris | b43fe7a | 2019-05-17 16:39:54 -0700 | [diff] [blame] | 69 | } else if Bool(stripper.StripProperties.Strip.Keep_symbols_and_debug_frame) { |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 70 | flags.StripKeepSymbolsAndDebugFrame = true |
Yi Kong | acee27c | 2019-03-29 20:05:14 -0700 | [diff] [blame] | 71 | } else if len(stripper.StripProperties.Strip.Keep_symbols_list) > 0 { |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 72 | flags.StripKeepSymbolsList = strings.Join(stripper.StripProperties.Strip.Keep_symbols_list, ",") |
Colin Cross | 9a959cd | 2018-09-05 14:21:15 -0700 | [diff] [blame] | 73 | } else if !Bool(stripper.StripProperties.Strip.All) { |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 74 | flags.StripKeepMiniDebugInfo = true |
Colin Cross | 9a959cd | 2018-09-05 14:21:15 -0700 | [diff] [blame] | 75 | } |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 76 | if actx.Config().Debuggable() && !flags.StripKeepMiniDebugInfo && !isStaticLib { |
| 77 | flags.StripAddGnuDebuglink = true |
Colin Cross | ed064c0 | 2018-09-05 16:28:13 -0700 | [diff] [blame] | 78 | } |
Chris Parsons | bf4f55f | 2020-11-23 17:02:44 -0500 | [diff] [blame] | 79 | transformStrip(actx, in, out, flags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
Ryan Prichard | f979d73 | 2019-05-30 20:53:29 -0700 | [diff] [blame] | 82 | |
Thiébaud Weksteen | 588ed66 | 2020-11-19 16:47:41 +0100 | [diff] [blame] | 83 | // StripExecutableOrSharedLib strips a binary or shared library from its debug |
| 84 | // symbols and other debugging information. The helper function |
| 85 | // flagsToStripFlags may be used to generate the flags argument. |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 86 | func (stripper *Stripper) StripExecutableOrSharedLib(actx android.ModuleContext, in android.Path, |
| 87 | out android.ModuleOutPath, flags StripFlags) { |
| 88 | stripper.strip(actx, in, out, flags, false) |
Ryan Prichard | f979d73 | 2019-05-30 20:53:29 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Thiébaud Weksteen | 588ed66 | 2020-11-19 16:47:41 +0100 | [diff] [blame] | 91 | // StripStaticLib strips a static library from its debug symbols and other |
| 92 | // debugging information. The helper function flagsToStripFlags may be used to |
| 93 | // generate the flags argument. |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 94 | func (stripper *Stripper) StripStaticLib(actx android.ModuleContext, in android.Path, out android.ModuleOutPath, |
| 95 | flags StripFlags) { |
| 96 | stripper.strip(actx, in, out, flags, true) |
Ryan Prichard | f979d73 | 2019-05-30 20:53:29 -0700 | [diff] [blame] | 97 | } |