blob: 5c32d8b040227530b747b9918532c2d57cb11556 [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)
Colin Cross1deeade2022-10-11 11:49:43 -070059 // create_minidebuginfo doesn't work for riscv64 yet, disable stripping for now
60 riscv64 := actx.Arch().ArchType == android.Riscv64
61 return !forceDisable && (forceEnable || defaultEnable) && !riscv64
Colin Cross4d9c2d12016-07-29 12:48:20 -070062}
63
Jingwen Chen3d383bb2021-06-09 07:18:37 +000064// Keep this consistent with //build/bazel/rules/stripped_shared_library.bzl.
Thiébaud Weksteend4587452020-08-19 14:53:01 +020065func (stripper *Stripper) strip(actx android.ModuleContext, in android.Path, out android.ModuleOutPath,
66 flags StripFlags, isStaticLib bool) {
67 if actx.Darwin() {
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050068 transformDarwinStrip(actx, in, out)
Colin Cross4d9c2d12016-07-29 12:48:20 -070069 } else {
Colin Cross9a959cd2018-09-05 14:21:15 -070070 if Bool(stripper.StripProperties.Strip.Keep_symbols) {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020071 flags.StripKeepSymbols = true
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -070072 } else if Bool(stripper.StripProperties.Strip.Keep_symbols_and_debug_frame) {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020073 flags.StripKeepSymbolsAndDebugFrame = true
Yi Kongacee27c2019-03-29 20:05:14 -070074 } else if len(stripper.StripProperties.Strip.Keep_symbols_list) > 0 {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020075 flags.StripKeepSymbolsList = strings.Join(stripper.StripProperties.Strip.Keep_symbols_list, ",")
Colin Cross9a959cd2018-09-05 14:21:15 -070076 } else if !Bool(stripper.StripProperties.Strip.All) {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020077 flags.StripKeepMiniDebugInfo = true
Colin Cross9a959cd2018-09-05 14:21:15 -070078 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +020079 if actx.Config().Debuggable() && !flags.StripKeepMiniDebugInfo && !isStaticLib {
80 flags.StripAddGnuDebuglink = true
Colin Crossed064c02018-09-05 16:28:13 -070081 }
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050082 transformStrip(actx, in, out, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -070083 }
84}
Ryan Prichardf979d732019-05-30 20:53:29 -070085
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010086// StripExecutableOrSharedLib strips a binary or shared library from its debug
87// symbols and other debugging information. The helper function
88// flagsToStripFlags may be used to generate the flags argument.
Thiébaud Weksteend4587452020-08-19 14:53:01 +020089func (stripper *Stripper) StripExecutableOrSharedLib(actx android.ModuleContext, in android.Path,
90 out android.ModuleOutPath, flags StripFlags) {
91 stripper.strip(actx, in, out, flags, false)
Ryan Prichardf979d732019-05-30 20:53:29 -070092}
93
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010094// StripStaticLib strips a static library from its debug symbols and other
95// debugging information. The helper function flagsToStripFlags may be used to
96// generate the flags argument.
Thiébaud Weksteend4587452020-08-19 14:53:01 +020097func (stripper *Stripper) StripStaticLib(actx android.ModuleContext, in android.Path, out android.ModuleOutPath,
98 flags StripFlags) {
99 stripper.strip(actx, in, out, flags, true)
Ryan Prichardf979d732019-05-30 20:53:29 -0700100}