strip.go 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. package cc
  15. import (
  16. "strings"
  17. "android/soong/android"
  18. )
  19. // StripProperties defines the type of stripping applied to the module.
  20. type StripProperties struct {
  21. Strip struct {
  22. // none forces all stripping to be disabled.
  23. // Device modules default to stripping enabled leaving mini debuginfo.
  24. // Host modules default to stripping disabled, but can be enabled by setting any other
  25. // strip boolean property.
  26. None *bool `android:"arch_variant"`
  27. // all forces stripping everything, including the mini debug info.
  28. All *bool `android:"arch_variant"`
  29. // keep_symbols enables stripping but keeps all symbols.
  30. Keep_symbols *bool `android:"arch_variant"`
  31. // keep_symbols_list specifies a list of symbols to keep if keep_symbols is enabled.
  32. // If it is unset then all symbols are kept.
  33. Keep_symbols_list []string `android:"arch_variant"`
  34. // keep_symbols_and_debug_frame enables stripping but keeps all symbols and debug frames.
  35. Keep_symbols_and_debug_frame *bool `android:"arch_variant"`
  36. } `android:"arch_variant"`
  37. }
  38. // Stripper defines the stripping actions and properties for a module.
  39. type Stripper struct {
  40. StripProperties StripProperties
  41. }
  42. // NeedsStrip determines if stripping is required for a module.
  43. func (stripper *Stripper) NeedsStrip(actx android.ModuleContext) bool {
  44. forceDisable := Bool(stripper.StripProperties.Strip.None)
  45. defaultEnable := (!actx.Config().KatiEnabled() || actx.Device())
  46. forceEnable := Bool(stripper.StripProperties.Strip.All) ||
  47. Bool(stripper.StripProperties.Strip.Keep_symbols) ||
  48. Bool(stripper.StripProperties.Strip.Keep_symbols_and_debug_frame)
  49. return !forceDisable && (forceEnable || defaultEnable)
  50. }
  51. // Keep this consistent with //build/bazel/rules/stripped_shared_library.bzl.
  52. func (stripper *Stripper) strip(actx android.ModuleContext, in android.Path, out android.ModuleOutPath,
  53. flags StripFlags, isStaticLib bool) {
  54. if actx.Darwin() {
  55. transformDarwinStrip(actx, in, out)
  56. } else {
  57. if Bool(stripper.StripProperties.Strip.Keep_symbols) {
  58. flags.StripKeepSymbols = true
  59. } else if Bool(stripper.StripProperties.Strip.Keep_symbols_and_debug_frame) {
  60. flags.StripKeepSymbolsAndDebugFrame = true
  61. } else if len(stripper.StripProperties.Strip.Keep_symbols_list) > 0 {
  62. flags.StripKeepSymbolsList = strings.Join(stripper.StripProperties.Strip.Keep_symbols_list, ",")
  63. } else if !Bool(stripper.StripProperties.Strip.All) {
  64. flags.StripKeepMiniDebugInfo = true
  65. }
  66. if actx.Config().Debuggable() && !flags.StripKeepMiniDebugInfo && !isStaticLib {
  67. flags.StripAddGnuDebuglink = true
  68. }
  69. transformStrip(actx, in, out, flags)
  70. }
  71. }
  72. // StripExecutableOrSharedLib strips a binary or shared library from its debug
  73. // symbols and other debugging information. The helper function
  74. // flagsToStripFlags may be used to generate the flags argument.
  75. func (stripper *Stripper) StripExecutableOrSharedLib(actx android.ModuleContext, in android.Path,
  76. out android.ModuleOutPath, flags StripFlags) {
  77. stripper.strip(actx, in, out, flags, false)
  78. }
  79. // StripStaticLib strips a static library from its debug symbols and other
  80. // debugging information. The helper function flagsToStripFlags may be used to
  81. // generate the flags argument.
  82. func (stripper *Stripper) StripStaticLib(actx android.ModuleContext, in android.Path, out android.ModuleOutPath,
  83. flags StripFlags) {
  84. stripper.strip(actx, in, out, flags, true)
  85. }