blob: a950df89883740e67cc244587ac5a4a04b0a00a4 [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 {
Spandan Dasb5890df2025-02-05 23:31:19 +000026 // Device and host modules default to stripping enabled leaving mini debuginfo.
27 // This can be disabled by setting none to true.
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010028 None *bool `android:"arch_variant"`
29
Colin Cross2254cff2020-12-01 09:06:10 -080030 // all forces stripping everything, including the mini debug info.
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010031 All *bool `android:"arch_variant"`
32
Colin Cross2254cff2020-12-01 09:06:10 -080033 // keep_symbols enables stripping but keeps all symbols.
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010034 Keep_symbols *bool `android:"arch_variant"`
35
Colin Cross2254cff2020-12-01 09:06:10 -080036 // keep_symbols_list specifies a list of symbols to keep if keep_symbols is enabled.
37 // If it is unset then all symbols are kept.
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010038 Keep_symbols_list []string `android:"arch_variant"`
39
Colin Cross2254cff2020-12-01 09:06:10 -080040 // keep_symbols_and_debug_frame enables stripping but keeps all symbols and debug frames.
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010041 Keep_symbols_and_debug_frame *bool `android:"arch_variant"`
Yi Kongacee27c2019-03-29 20:05:14 -070042 } `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070043}
44
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010045// Stripper defines the stripping actions and properties for a module.
Thiébaud Weksteend4587452020-08-19 14:53:01 +020046type Stripper struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070047 StripProperties StripProperties
48}
49
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010050// NeedsStrip determines if stripping is required for a module.
Thiébaud Weksteend4587452020-08-19 14:53:01 +020051func (stripper *Stripper) NeedsStrip(actx android.ModuleContext) bool {
Colin Cross2254cff2020-12-01 09:06:10 -080052 forceDisable := Bool(stripper.StripProperties.Strip.None)
Spandan Das5910b2a2025-01-21 23:57:08 +000053 return !forceDisable
Colin Cross4d9c2d12016-07-29 12:48:20 -070054}
55
Thiébaud Weksteend4587452020-08-19 14:53:01 +020056func (stripper *Stripper) strip(actx android.ModuleContext, in android.Path, out android.ModuleOutPath,
57 flags StripFlags, isStaticLib bool) {
58 if actx.Darwin() {
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050059 transformDarwinStrip(actx, in, out)
Colin Cross4d9c2d12016-07-29 12:48:20 -070060 } else {
Colin Cross9a959cd2018-09-05 14:21:15 -070061 if Bool(stripper.StripProperties.Strip.Keep_symbols) {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020062 flags.StripKeepSymbols = true
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -070063 } else if Bool(stripper.StripProperties.Strip.Keep_symbols_and_debug_frame) {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020064 flags.StripKeepSymbolsAndDebugFrame = true
Yi Kongacee27c2019-03-29 20:05:14 -070065 } else if len(stripper.StripProperties.Strip.Keep_symbols_list) > 0 {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020066 flags.StripKeepSymbolsList = strings.Join(stripper.StripProperties.Strip.Keep_symbols_list, ",")
Colin Cross9a959cd2018-09-05 14:21:15 -070067 } else if !Bool(stripper.StripProperties.Strip.All) {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020068 flags.StripKeepMiniDebugInfo = true
Colin Cross9a959cd2018-09-05 14:21:15 -070069 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +020070 if actx.Config().Debuggable() && !flags.StripKeepMiniDebugInfo && !isStaticLib {
71 flags.StripAddGnuDebuglink = true
Colin Crossed064c02018-09-05 16:28:13 -070072 }
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050073 transformStrip(actx, in, out, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -070074 }
75}
Ryan Prichardf979d732019-05-30 20:53:29 -070076
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010077// StripExecutableOrSharedLib strips a binary or shared library from its debug
78// symbols and other debugging information. The helper function
79// flagsToStripFlags may be used to generate the flags argument.
Thiébaud Weksteend4587452020-08-19 14:53:01 +020080func (stripper *Stripper) StripExecutableOrSharedLib(actx android.ModuleContext, in android.Path,
81 out android.ModuleOutPath, flags StripFlags) {
82 stripper.strip(actx, in, out, flags, false)
Ryan Prichardf979d732019-05-30 20:53:29 -070083}
84
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010085// StripStaticLib strips a static library from its debug symbols and other
86// debugging information. The helper function flagsToStripFlags may be used to
87// generate the flags argument.
Thiébaud Weksteend4587452020-08-19 14:53:01 +020088func (stripper *Stripper) StripStaticLib(actx android.ModuleContext, in android.Path, out android.ModuleOutPath,
89 flags StripFlags) {
90 stripper.strip(actx, in, out, flags, true)
Ryan Prichardf979d732019-05-30 20:53:29 -070091}