blob: 1f10a745c60b2d4466647cfd9b1b0bdc83ba2344 [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 {
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010026 // whether to disable all stripping.
27 None *bool `android:"arch_variant"`
28
29 // whether to strip everything, including the mini debug info.
30 All *bool `android:"arch_variant"`
31
32 // whether to keep the symbols.
33 Keep_symbols *bool `android:"arch_variant"`
34
35 // keeps only the symbols defined here.
36 Keep_symbols_list []string `android:"arch_variant"`
37
38 // whether to keep the symbols and the debug frames.
39 Keep_symbols_and_debug_frame *bool `android:"arch_variant"`
Yi Kongacee27c2019-03-29 20:05:14 -070040 } `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070041}
42
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010043// Stripper defines the stripping actions and properties for a module.
Thiébaud Weksteend4587452020-08-19 14:53:01 +020044type Stripper struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070045 StripProperties StripProperties
46}
47
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010048// NeedsStrip determines if stripping is required for a module.
Thiébaud Weksteend4587452020-08-19 14:53:01 +020049func (stripper *Stripper) NeedsStrip(actx android.ModuleContext) bool {
Jingwen Chencda22c92020-11-23 00:22:30 -050050 // TODO(ccross): enable host stripping when Kati is enabled? Make never had support for stripping host binaries.
51 return (!actx.Config().KatiEnabled() || actx.Device()) && !Bool(stripper.StripProperties.Strip.None)
Colin Cross4d9c2d12016-07-29 12:48:20 -070052}
53
Thiébaud Weksteend4587452020-08-19 14:53:01 +020054func (stripper *Stripper) strip(actx android.ModuleContext, in android.Path, out android.ModuleOutPath,
55 flags StripFlags, isStaticLib bool) {
56 if actx.Darwin() {
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050057 transformDarwinStrip(actx, in, out)
Colin Cross4d9c2d12016-07-29 12:48:20 -070058 } else {
Colin Cross9a959cd2018-09-05 14:21:15 -070059 if Bool(stripper.StripProperties.Strip.Keep_symbols) {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020060 flags.StripKeepSymbols = true
Christopher Ferrisb43fe7a2019-05-17 16:39:54 -070061 } else if Bool(stripper.StripProperties.Strip.Keep_symbols_and_debug_frame) {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020062 flags.StripKeepSymbolsAndDebugFrame = true
Yi Kongacee27c2019-03-29 20:05:14 -070063 } else if len(stripper.StripProperties.Strip.Keep_symbols_list) > 0 {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020064 flags.StripKeepSymbolsList = strings.Join(stripper.StripProperties.Strip.Keep_symbols_list, ",")
Colin Cross9a959cd2018-09-05 14:21:15 -070065 } else if !Bool(stripper.StripProperties.Strip.All) {
Thiébaud Weksteend4587452020-08-19 14:53:01 +020066 flags.StripKeepMiniDebugInfo = true
Colin Cross9a959cd2018-09-05 14:21:15 -070067 }
Thiébaud Weksteend4587452020-08-19 14:53:01 +020068 if actx.Config().Debuggable() && !flags.StripKeepMiniDebugInfo && !isStaticLib {
69 flags.StripAddGnuDebuglink = true
Colin Crossed064c02018-09-05 16:28:13 -070070 }
Chris Parsonsbf4f55f2020-11-23 17:02:44 -050071 transformStrip(actx, in, out, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -070072 }
73}
Ryan Prichardf979d732019-05-30 20:53:29 -070074
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010075// StripExecutableOrSharedLib strips a binary or shared library from its debug
76// symbols and other debugging information. The helper function
77// flagsToStripFlags may be used to generate the flags argument.
Thiébaud Weksteend4587452020-08-19 14:53:01 +020078func (stripper *Stripper) StripExecutableOrSharedLib(actx android.ModuleContext, in android.Path,
79 out android.ModuleOutPath, flags StripFlags) {
80 stripper.strip(actx, in, out, flags, false)
Ryan Prichardf979d732019-05-30 20:53:29 -070081}
82
Thiébaud Weksteen588ed662020-11-19 16:47:41 +010083// StripStaticLib strips a static library from its debug symbols and other
84// debugging information. The helper function flagsToStripFlags may be used to
85// generate the flags argument.
Thiébaud Weksteend4587452020-08-19 14:53:01 +020086func (stripper *Stripper) StripStaticLib(actx android.ModuleContext, in android.Path, out android.ModuleOutPath,
87 flags StripFlags) {
88 stripper.strip(actx, in, out, flags, true)
Ryan Prichardf979d732019-05-30 20:53:29 -070089}