vendor_snapshot.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // Copyright 2020 The Android Open Source Project
  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. "encoding/json"
  17. "path/filepath"
  18. "strings"
  19. "android/soong/android"
  20. "android/soong/snapshot"
  21. )
  22. // This file defines how to capture cc modules into snapshot package.
  23. // Checks if the target image would contain VNDK
  24. func includeVndk(image snapshot.SnapshotImage) bool {
  25. if image.ImageName() == snapshot.VendorSnapshotImageName {
  26. return true
  27. }
  28. return false
  29. }
  30. // Check if the module is VNDK private
  31. func isPrivate(image snapshot.SnapshotImage, m LinkableInterface) bool {
  32. if image.ImageName() == snapshot.VendorSnapshotImageName && m.IsVndkPrivate() {
  33. return true
  34. }
  35. return false
  36. }
  37. // Checks if target image supports VNDK Ext
  38. func supportsVndkExt(image snapshot.SnapshotImage) bool {
  39. if image.ImageName() == snapshot.VendorSnapshotImageName {
  40. return true
  41. }
  42. return false
  43. }
  44. // Determines if the module is a candidate for snapshot.
  45. func isSnapshotAware(cfg android.DeviceConfig, m LinkableInterface, inProprietaryPath bool, apexInfo android.ApexInfo, image snapshot.SnapshotImage) bool {
  46. if !m.Enabled() || m.HiddenFromMake() {
  47. return false
  48. }
  49. // When android/prebuilt.go selects between source and prebuilt, it sets
  50. // HideFromMake on the other one to avoid duplicate install rules in make.
  51. if m.IsHideFromMake() {
  52. return false
  53. }
  54. // skip proprietary modules, but (for the vendor snapshot only)
  55. // include all VNDK (static)
  56. if inProprietaryPath && (!includeVndk(image) || !m.IsVndk()) {
  57. return false
  58. }
  59. // If the module would be included based on its path, check to see if
  60. // the module is marked to be excluded. If so, skip it.
  61. if image.ExcludeFromSnapshot(m) {
  62. return false
  63. }
  64. if m.Target().Os.Class != android.Device {
  65. return false
  66. }
  67. if m.Target().NativeBridge == android.NativeBridgeEnabled {
  68. return false
  69. }
  70. // the module must be installed in target image
  71. if !apexInfo.IsForPlatform() || m.IsSnapshotPrebuilt() || !image.InImage(m)() {
  72. return false
  73. }
  74. // skip kernel_headers which always depend on vendor
  75. if m.KernelHeadersDecorator() {
  76. return false
  77. }
  78. if m.IsLlndk() {
  79. return false
  80. }
  81. // Libraries
  82. if sanitizable, ok := m.(PlatformSanitizeable); ok && sanitizable.IsSnapshotLibrary() {
  83. if sanitizable.SanitizePropDefined() {
  84. // scs and hwasan export both sanitized and unsanitized variants for static and header
  85. // Always use unsanitized variants of them.
  86. for _, t := range []SanitizerType{scs, Hwasan} {
  87. if !sanitizable.Shared() && sanitizable.IsSanitizerEnabled(t) {
  88. return false
  89. }
  90. }
  91. // cfi also exports both variants. But for static, we capture both.
  92. // This is because cfi static libraries can't be linked from non-cfi modules,
  93. // and vice versa. This isn't the case for scs and hwasan sanitizers.
  94. if !sanitizable.Static() && !sanitizable.Shared() && sanitizable.IsSanitizerEnabled(cfi) {
  95. return false
  96. }
  97. }
  98. if sanitizable.Static() {
  99. return sanitizable.OutputFile().Valid() && !isPrivate(image, m)
  100. }
  101. if sanitizable.Shared() || sanitizable.Rlib() {
  102. if !sanitizable.OutputFile().Valid() {
  103. return false
  104. }
  105. if includeVndk(image) {
  106. if !sanitizable.IsVndk() {
  107. return true
  108. }
  109. return sanitizable.IsVndkExt()
  110. }
  111. }
  112. return true
  113. }
  114. // Binaries and Objects
  115. if m.Binary() || m.Object() {
  116. return m.OutputFile().Valid()
  117. }
  118. return false
  119. }
  120. // Extend the snapshot.SnapshotJsonFlags to include cc specific fields.
  121. type snapshotJsonFlags struct {
  122. snapshot.SnapshotJsonFlags
  123. // library flags
  124. ExportedDirs []string `json:",omitempty"`
  125. ExportedSystemDirs []string `json:",omitempty"`
  126. ExportedFlags []string `json:",omitempty"`
  127. Sanitize string `json:",omitempty"`
  128. SanitizeMinimalDep bool `json:",omitempty"`
  129. SanitizeUbsanDep bool `json:",omitempty"`
  130. // binary flags
  131. Symlinks []string `json:",omitempty"`
  132. StaticExecutable bool `json:",omitempty"`
  133. InstallInRoot bool `json:",omitempty"`
  134. // dependencies
  135. SharedLibs []string `json:",omitempty"`
  136. StaticLibs []string `json:",omitempty"`
  137. RuntimeLibs []string `json:",omitempty"`
  138. // extra config files
  139. InitRc []string `json:",omitempty"`
  140. VintfFragments []string `json:",omitempty"`
  141. }
  142. var ccSnapshotAction snapshot.GenerateSnapshotAction = func(s snapshot.SnapshotSingleton, ctx android.SingletonContext, snapshotArchDir string) android.Paths {
  143. /*
  144. Vendor snapshot zipped artifacts directory structure for cc modules:
  145. {SNAPSHOT_ARCH}/
  146. arch-{TARGET_ARCH}-{TARGET_ARCH_VARIANT}/
  147. shared/
  148. (.so shared libraries)
  149. static/
  150. (.a static libraries)
  151. header/
  152. (header only libraries)
  153. binary/
  154. (executable binaries)
  155. object/
  156. (.o object files)
  157. arch-{TARGET_2ND_ARCH}-{TARGET_2ND_ARCH_VARIANT}/
  158. shared/
  159. (.so shared libraries)
  160. static/
  161. (.a static libraries)
  162. header/
  163. (header only libraries)
  164. binary/
  165. (executable binaries)
  166. object/
  167. (.o object files)
  168. NOTICE_FILES/
  169. (notice files, e.g. libbase.txt)
  170. configs/
  171. (config files, e.g. init.rc files, vintf_fragments.xml files, etc.)
  172. include/
  173. (header files of same directory structure with source tree)
  174. */
  175. var snapshotOutputs android.Paths
  176. includeDir := filepath.Join(snapshotArchDir, "include")
  177. configsDir := filepath.Join(snapshotArchDir, "configs")
  178. noticeDir := filepath.Join(snapshotArchDir, "NOTICE_FILES")
  179. installedNotices := make(map[string]bool)
  180. installedConfigs := make(map[string]bool)
  181. var headers android.Paths
  182. copyFile := func(ctx android.SingletonContext, path android.Path, out string, fake bool) android.OutputPath {
  183. if fake {
  184. // All prebuilt binaries and headers are installed by copyFile function. This makes a fake
  185. // snapshot just touch prebuilts and headers, rather than installing real files.
  186. return snapshot.WriteStringToFileRule(ctx, "", out)
  187. } else {
  188. return snapshot.CopyFileRule(pctx, ctx, path, out)
  189. }
  190. }
  191. // installSnapshot function copies prebuilt file (.so, .a, or executable) and json flag file.
  192. // For executables, init_rc and vintf_fragments files are also copied.
  193. installSnapshot := func(m LinkableInterface, fake bool) android.Paths {
  194. targetArch := "arch-" + m.Target().Arch.ArchType.String()
  195. if m.Target().Arch.ArchVariant != "" {
  196. targetArch += "-" + m.Target().Arch.ArchVariant
  197. }
  198. var ret android.Paths
  199. prop := snapshotJsonFlags{}
  200. // Common properties among snapshots.
  201. prop.ModuleName = ctx.ModuleName(m)
  202. if supportsVndkExt(s.Image) && m.IsVndkExt() {
  203. // vndk exts are installed to /vendor/lib(64)?/vndk(-sp)?
  204. if m.IsVndkSp() {
  205. prop.RelativeInstallPath = "vndk-sp"
  206. } else {
  207. prop.RelativeInstallPath = "vndk"
  208. }
  209. } else {
  210. prop.RelativeInstallPath = m.RelativeInstallPath()
  211. }
  212. prop.RuntimeLibs = m.SnapshotRuntimeLibs()
  213. prop.Required = m.RequiredModuleNames()
  214. for _, path := range m.InitRc() {
  215. prop.InitRc = append(prop.InitRc, filepath.Join("configs", path.Base()))
  216. }
  217. for _, path := range m.VintfFragments() {
  218. prop.VintfFragments = append(prop.VintfFragments, filepath.Join("configs", path.Base()))
  219. }
  220. // install config files. ignores any duplicates.
  221. for _, path := range append(m.InitRc(), m.VintfFragments()...) {
  222. out := filepath.Join(configsDir, path.Base())
  223. if !installedConfigs[out] {
  224. installedConfigs[out] = true
  225. ret = append(ret, copyFile(ctx, path, out, fake))
  226. }
  227. }
  228. var propOut string
  229. if m.IsSnapshotLibrary() {
  230. exporterInfo := ctx.ModuleProvider(m.Module(), FlagExporterInfoProvider).(FlagExporterInfo)
  231. // library flags
  232. prop.ExportedFlags = exporterInfo.Flags
  233. for _, dir := range exporterInfo.IncludeDirs {
  234. prop.ExportedDirs = append(prop.ExportedDirs, filepath.Join("include", dir.String()))
  235. }
  236. for _, dir := range exporterInfo.SystemIncludeDirs {
  237. prop.ExportedSystemDirs = append(prop.ExportedSystemDirs, filepath.Join("include", dir.String()))
  238. }
  239. // shared libs dependencies aren't meaningful on static or header libs
  240. if m.Shared() {
  241. prop.SharedLibs = m.SnapshotSharedLibs()
  242. }
  243. // static libs dependencies are required to collect the NOTICE files.
  244. prop.StaticLibs = m.SnapshotStaticLibs()
  245. if sanitizable, ok := m.(PlatformSanitizeable); ok {
  246. if sanitizable.Static() && sanitizable.SanitizePropDefined() {
  247. prop.SanitizeMinimalDep = sanitizable.MinimalRuntimeDep() || sanitizable.MinimalRuntimeNeeded()
  248. prop.SanitizeUbsanDep = sanitizable.UbsanRuntimeDep() || sanitizable.UbsanRuntimeNeeded()
  249. }
  250. }
  251. var libType string
  252. if m.Static() {
  253. libType = "static"
  254. } else if m.Shared() {
  255. libType = "shared"
  256. } else if m.Rlib() {
  257. libType = "rlib"
  258. } else {
  259. libType = "header"
  260. }
  261. var stem string
  262. // install .a or .so
  263. if libType != "header" {
  264. libPath := m.OutputFile().Path()
  265. stem = libPath.Base()
  266. if sanitizable, ok := m.(PlatformSanitizeable); ok {
  267. if (sanitizable.Static() || sanitizable.Rlib()) && sanitizable.SanitizePropDefined() && sanitizable.IsSanitizerEnabled(cfi) {
  268. // both cfi and non-cfi variant for static libraries can exist.
  269. // attach .cfi to distinguish between cfi and non-cfi.
  270. // e.g. libbase.a -> libbase.cfi.a
  271. ext := filepath.Ext(stem)
  272. stem = strings.TrimSuffix(stem, ext) + ".cfi" + ext
  273. prop.Sanitize = "cfi"
  274. prop.ModuleName += ".cfi"
  275. }
  276. }
  277. snapshotLibOut := filepath.Join(snapshotArchDir, targetArch, libType, stem)
  278. ret = append(ret, copyFile(ctx, libPath, snapshotLibOut, fake))
  279. } else {
  280. stem = ctx.ModuleName(m)
  281. }
  282. propOut = filepath.Join(snapshotArchDir, targetArch, libType, stem+".json")
  283. } else if m.Binary() {
  284. // binary flags
  285. prop.Symlinks = m.Symlinks()
  286. prop.StaticExecutable = m.StaticExecutable()
  287. prop.InstallInRoot = m.InstallInRoot()
  288. prop.SharedLibs = m.SnapshotSharedLibs()
  289. // static libs dependencies are required to collect the NOTICE files.
  290. prop.StaticLibs = m.SnapshotStaticLibs()
  291. // install bin
  292. binPath := m.OutputFile().Path()
  293. snapshotBinOut := filepath.Join(snapshotArchDir, targetArch, "binary", binPath.Base())
  294. ret = append(ret, copyFile(ctx, binPath, snapshotBinOut, fake))
  295. propOut = snapshotBinOut + ".json"
  296. } else if m.Object() {
  297. // object files aren't installed to the device, so their names can conflict.
  298. // Use module name as stem.
  299. objPath := m.OutputFile().Path()
  300. snapshotObjOut := filepath.Join(snapshotArchDir, targetArch, "object",
  301. ctx.ModuleName(m)+filepath.Ext(objPath.Base()))
  302. ret = append(ret, copyFile(ctx, objPath, snapshotObjOut, fake))
  303. propOut = snapshotObjOut + ".json"
  304. } else {
  305. ctx.Errorf("unknown module %q in vendor snapshot", m.String())
  306. return nil
  307. }
  308. j, err := json.Marshal(prop)
  309. if err != nil {
  310. ctx.Errorf("json marshal to %q failed: %#v", propOut, err)
  311. return nil
  312. }
  313. ret = append(ret, snapshot.WriteStringToFileRule(ctx, string(j), propOut))
  314. return ret
  315. }
  316. ctx.VisitAllModules(func(module android.Module) {
  317. m, ok := module.(LinkableInterface)
  318. if !ok {
  319. return
  320. }
  321. moduleDir := ctx.ModuleDir(module)
  322. inProprietaryPath := s.Image.IsProprietaryPath(moduleDir, ctx.DeviceConfig())
  323. apexInfo := ctx.ModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo)
  324. if s.Image.ExcludeFromSnapshot(m) {
  325. if inProprietaryPath {
  326. // Error: exclude_from_vendor_snapshot applies
  327. // to framework-path modules only.
  328. ctx.Errorf("module %q in vendor proprietary path %q may not use \"exclude_from_vendor_snapshot: true\"", m.String(), moduleDir)
  329. return
  330. }
  331. }
  332. if !isSnapshotAware(ctx.DeviceConfig(), m, inProprietaryPath, apexInfo, s.Image) {
  333. return
  334. }
  335. // If we are using directed snapshot and a module is not included in the
  336. // list, we will still include the module as if it was a fake module.
  337. // The reason is that soong needs all the dependencies to be present, even
  338. // if they are not using during the build.
  339. installAsFake := s.Fake
  340. if s.Image.ExcludeFromDirectedSnapshot(ctx.DeviceConfig(), m.BaseModuleName()) {
  341. installAsFake = true
  342. }
  343. // installSnapshot installs prebuilts and json flag files
  344. snapshotOutputs = append(snapshotOutputs, installSnapshot(m, installAsFake)...)
  345. // just gather headers and notice files here, because they are to be deduplicated
  346. if m.IsSnapshotLibrary() {
  347. headers = append(headers, m.SnapshotHeaders()...)
  348. }
  349. if len(m.EffectiveLicenseFiles()) > 0 {
  350. noticeName := ctx.ModuleName(m) + ".txt"
  351. noticeOut := filepath.Join(noticeDir, noticeName)
  352. // skip already copied notice file
  353. if !installedNotices[noticeOut] {
  354. installedNotices[noticeOut] = true
  355. snapshotOutputs = append(snapshotOutputs, combineNoticesRule(ctx, m.EffectiveLicenseFiles(), noticeOut))
  356. }
  357. }
  358. })
  359. // install all headers after removing duplicates
  360. for _, header := range android.FirstUniquePaths(headers) {
  361. snapshotOutputs = append(snapshotOutputs, copyFile(ctx, header, filepath.Join(includeDir, header.String()), s.Fake))
  362. }
  363. return snapshotOutputs
  364. }
  365. func init() {
  366. snapshot.RegisterSnapshotAction(ccSnapshotAction)
  367. }