1// 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
17import (
18	"strings"
19
20	"android/soong/android"
21)
22
23// StripProperties defines the type of stripping applied to the module.
24type StripProperties struct {
25	Strip struct {
26		// 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.
30		None *bool `android:"arch_variant"`
31
32		// all forces stripping everything, including the mini debug info.
33		All *bool `android:"arch_variant"`
34
35		// keep_symbols enables stripping but keeps all symbols.
36		Keep_symbols *bool `android:"arch_variant"`
37
38		// 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.
40		Keep_symbols_list []string `android:"arch_variant"`
41
42		// keep_symbols_and_debug_frame enables stripping but keeps all symbols and debug frames.
43		Keep_symbols_and_debug_frame *bool `android:"arch_variant"`
44	} `android:"arch_variant"`
45}
46
47// Stripper defines the stripping actions and properties for a module.
48type Stripper struct {
49	StripProperties StripProperties
50}
51
52// NeedsStrip determines if stripping is required for a module.
53func (stripper *Stripper) NeedsStrip(actx android.ModuleContext) bool {
54	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)
60}
61
62func (stripper *Stripper) strip(actx android.ModuleContext, in android.Path, out android.ModuleOutPath,
63	flags StripFlags, isStaticLib bool) {
64	if actx.Darwin() {
65		transformDarwinStrip(actx, in, out)
66	} else {
67		if Bool(stripper.StripProperties.Strip.Keep_symbols) {
68			flags.StripKeepSymbols = true
69		} else if Bool(stripper.StripProperties.Strip.Keep_symbols_and_debug_frame) {
70			flags.StripKeepSymbolsAndDebugFrame = true
71		} else if len(stripper.StripProperties.Strip.Keep_symbols_list) > 0 {
72			flags.StripKeepSymbolsList = strings.Join(stripper.StripProperties.Strip.Keep_symbols_list, ",")
73		} else if !Bool(stripper.StripProperties.Strip.All) {
74			flags.StripKeepMiniDebugInfo = true
75		}
76		if actx.Config().Debuggable() && !flags.StripKeepMiniDebugInfo && !isStaticLib {
77			flags.StripAddGnuDebuglink = true
78		}
79		transformStrip(actx, in, out, flags)
80	}
81}
82
83// 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.
86func (stripper *Stripper) StripExecutableOrSharedLib(actx android.ModuleContext, in android.Path,
87	out android.ModuleOutPath, flags StripFlags) {
88	stripper.strip(actx, in, out, flags, false)
89}
90
91// 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.
94func (stripper *Stripper) StripStaticLib(actx android.ModuleContext, in android.Path, out android.ModuleOutPath,
95	flags StripFlags) {
96	stripper.strip(actx, in, out, flags, true)
97}
98