blob: b1f34bb89fc272e1a7c8cd430dbfeeb769d0fb6d [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// 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
15package cc
16
Nan Zhang0007d812017-11-07 10:57:05 -080017import (
Yi Kongacee27c2019-03-29 20:05:14 -070018 "strings"
19
Nan Zhang0007d812017-11-07 10:57:05 -080020 "android/soong/android"
21)
Colin Cross4d9c2d12016-07-29 12:48:20 -070022
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010023// StripProperties defines the type of stripping applied to the module.
Colin Cross4d9c2d12016-07-29 12:48:20 -070024type StripProperties struct {
25 Strip struct {
Colin Cross2254cff2020-12-01 09:06:10 -080026 // 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 Weksteen588ed662020-11-19 16:47:41 +010030 None *bool `android:"arch_variant"`
31
Colin Cross2254cff2020-12-01 09:06:10 -080032 // all forces stripping everything, including the mini debug info.
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010033 All *bool `android:"arch_variant"`
34
Colin Cross2254cff2020-12-01 09:06:10 -080035 // keep_symbols enables stripping but keeps all symbols.
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010036 Keep_symbols *bool `android:"arch_variant"`
37
Colin Cross2254cff2020-12-01 09:06:10 -080038 // 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 Weksteen588ed662020-11-19 16:47:41 +010040 Keep_symbols_list []string `android:"arch_variant"`
41
Colin Cross2254cff2020-12-01 09:06:10 -080042 // keep_symbols_and_debug_frame enables stripping but keeps all symbols and debug frames.
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010043 Keep_symbols_and_debug_frame *bool `android:"arch_variant"`
Yi Kongacee27c2019-03-29 20:05:14 -070044 } `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070045}
46
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010047// Stripper defines the stripping actions and properties for a module.
Thiébaud Weksteend4587452020-08-19 14:53:01 +020048type Stripper struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070049 StripProperties StripProperties
50}
51
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010052// NeedsStrip determines if stripping is required for a module.
Thiébaud Weksteend4587452020-08-19 14:53:01 +020053func (stripper *Stripper) NeedsStrip(actx android.ModuleContext) bool {
Colin Cross2254cff2020-12-01 09:06:10 -080054 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 Hughes8cf32362022-11-11 00:59:14 +000059 return !forceDisable && (forceEnable || defaultEnable)
Colin Cross4d9c2d12016-07-29 12:48:20 -070060}
61
Thiébaud Weksteend4587452020-08-19 14:53:01 +020062func (stripper *Stripper) strip(actx android.ModuleContext, in android.Path, out android.ModuleOutPath,
63 flags StripFlags, isStaticLib bool) {
64 if actx.Darwin() {
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050065 transformDarwinStrip(actx, in, out)
Colin Cross4d9c2d12016-07-29 12:48:20 -070066 } else {
Colin Cross9a959cd2018-09-05 14:21:15 -070067 if Bool(stripper.StripProperties.Strip.Keep_symbols) {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020068 flags.StripKeepSymbols = true
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -070069 } else if Bool(stripper.StripProperties.Strip.Keep_symbols_and_debug_frame) {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020070 flags.StripKeepSymbolsAndDebugFrame = true
Yi Kongacee27c2019-03-29 20:05:14 -070071 } else if len(stripper.StripProperties.Strip.Keep_symbols_list) > 0 {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020072 flags.StripKeepSymbolsList = strings.Join(stripper.StripProperties.Strip.Keep_symbols_list, ",")
Colin Cross9a959cd2018-09-05 14:21:15 -070073 } else if !Bool(stripper.StripProperties.Strip.All) {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020074 flags.StripKeepMiniDebugInfo = true
Colin Cross9a959cd2018-09-05 14:21:15 -070075 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +020076 if actx.Config().Debuggable() && !flags.StripKeepMiniDebugInfo && !isStaticLib {
77 flags.StripAddGnuDebuglink = true
Colin Crossed064c02018-09-05 16:28:13 -070078 }
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050079 transformStrip(actx, in, out, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -070080 }
81}
Ryan Prichardf979d732019-05-30 20:53:29 -070082
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010083// 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 Weksteend4587452020-08-19 14:53:01 +020086func (stripper *Stripper) StripExecutableOrSharedLib(actx android.ModuleContext, in android.Path,
87 out android.ModuleOutPath, flags StripFlags) {
88 stripper.strip(actx, in, out, flags, false)
Ryan Prichardf979d732019-05-30 20:53:29 -070089}
90
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010091// 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 Weksteend4587452020-08-19 14:53:01 +020094func (stripper *Stripper) StripStaticLib(actx android.ModuleContext, in android.Path, out android.ModuleOutPath,
95 flags StripFlags) {
96 stripper.strip(actx, in, out, flags, true)
Ryan Prichardf979d732019-05-30 20:53:29 -070097}