blob: c60e135304fc363ece96b7019dfe0761a97c8ea5 [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)
59 return !forceDisable && (forceEnable || defaultEnable)
Colin Cross4d9c2d12016-07-29 12:48:20 -070060}
61
Jingwen Chen3d383bb2021-06-09 07:18:37 +000062// Keep this consistent with //build/bazel/rules/stripped_shared_library.bzl.
Thiébaud Weksteend4587452020-08-19 14:53:01 +020063func (stripper *Stripper) strip(actx android.ModuleContext, in android.Path, out android.ModuleOutPath,
64 flags StripFlags, isStaticLib bool) {
65 if actx.Darwin() {
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050066 transformDarwinStrip(actx, in, out)
Colin Cross4d9c2d12016-07-29 12:48:20 -070067 } else {
Colin Cross9a959cd2018-09-05 14:21:15 -070068 if Bool(stripper.StripProperties.Strip.Keep_symbols) {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020069 flags.StripKeepSymbols = true
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -070070 } else if Bool(stripper.StripProperties.Strip.Keep_symbols_and_debug_frame) {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020071 flags.StripKeepSymbolsAndDebugFrame = true
Yi Kongacee27c2019-03-29 20:05:14 -070072 } else if len(stripper.StripProperties.Strip.Keep_symbols_list) > 0 {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020073 flags.StripKeepSymbolsList = strings.Join(stripper.StripProperties.Strip.Keep_symbols_list, ",")
Colin Cross9a959cd2018-09-05 14:21:15 -070074 } else if !Bool(stripper.StripProperties.Strip.All) {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020075 flags.StripKeepMiniDebugInfo = true
Colin Cross9a959cd2018-09-05 14:21:15 -070076 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +020077 if actx.Config().Debuggable() && !flags.StripKeepMiniDebugInfo && !isStaticLib {
78 flags.StripAddGnuDebuglink = true
Colin Crossed064c02018-09-05 16:28:13 -070079 }
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050080 transformStrip(actx, in, out, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -070081 }
82}
Ryan Prichardf979d732019-05-30 20:53:29 -070083
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010084// StripExecutableOrSharedLib strips a binary or shared library from its debug
85// symbols and other debugging information. The helper function
86// flagsToStripFlags may be used to generate the flags argument.
Thiébaud Weksteend4587452020-08-19 14:53:01 +020087func (stripper *Stripper) StripExecutableOrSharedLib(actx android.ModuleContext, in android.Path,
88 out android.ModuleOutPath, flags StripFlags) {
89 stripper.strip(actx, in, out, flags, false)
Ryan Prichardf979d732019-05-30 20:53:29 -070090}
91
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010092// StripStaticLib strips a static library from its debug symbols and other
93// debugging information. The helper function flagsToStripFlags may be used to
94// generate the flags argument.
Thiébaud Weksteend4587452020-08-19 14:53:01 +020095func (stripper *Stripper) StripStaticLib(actx android.ModuleContext, in android.Path, out android.ModuleOutPath,
96 flags StripFlags) {
97 stripper.strip(actx, in, out, flags, true)
Ryan Prichardf979d732019-05-30 20:53:29 -070098}