Home | History | Annotate | Download | only in cc
      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 
     15 package cc
     16 
     17 // The platform needs to provide the following artifacts for the NDK:
     18 // 1. Bionic headers.
     19 // 2. Platform API headers.
     20 // 3. NDK stub shared libraries.
     21 // 4. Bionic static libraries.
     22 //
     23 // TODO(danalbert): All of the above need to include NOTICE files.
     24 //
     25 // Components 1 and 2: Headers
     26 // The bionic and platform API headers are generalized into a single
     27 // `ndk_headers` rule. This rule has a `from` property that indicates a base
     28 // directory from which headers are to be taken, and a `to` property that
     29 // indicates where in the sysroot they should reside relative to usr/include.
     30 // There is also a `srcs` property that is glob compatible for specifying which
     31 // headers to include.
     32 //
     33 // Component 3: Stub Libraries
     34 // The shared libraries in the NDK are not the actual shared libraries they
     35 // refer to (to prevent people from accidentally loading them), but stub
     36 // libraries with dummy implementations of everything for use at build time
     37 // only.
     38 //
     39 // Since we don't actually need to know anything about the stub libraries aside
     40 // from a list of functions and globals to be exposed, we can create these for
     41 // every platform level in the current tree. This is handled by the
     42 // ndk_library rule.
     43 //
     44 // Component 4: Static Libraries
     45 // The NDK only provides static libraries for bionic, not the platform APIs.
     46 // Since these need to be the actual implementation, we can't build old versions
     47 // in the current platform tree. As such, legacy versions are checked in
     48 // prebuilt to development/ndk, and a current version is built and archived as
     49 // part of the platform build. The platfrom already builds these libraries, our
     50 // NDK build rules only need to archive them for retrieval so they can be added
     51 // to the prebuilts.
     52 //
     53 // TODO(danalbert): Write `ndk_static_library` rule.
     54 
     55 import (
     56 	"github.com/google/blueprint"
     57 
     58 	"android/soong/android"
     59 )
     60 
     61 func init() {
     62 	android.RegisterModuleType("ndk_headers", ndkHeadersFactory)
     63 	android.RegisterModuleType("ndk_library", ndkLibraryFactory)
     64 	android.RegisterModuleType("preprocessed_ndk_headers", preprocessedNdkHeadersFactory)
     65 	android.RegisterSingletonType("ndk", NdkSingleton)
     66 
     67 	pctx.Import("android/soong/common")
     68 }
     69 
     70 func getNdkInstallBase(ctx android.PathContext) android.OutputPath {
     71 	return android.PathForOutput(ctx, "ndk")
     72 }
     73 
     74 // Returns the main install directory for the NDK sysroot. Usable with --sysroot.
     75 func getNdkSysrootBase(ctx android.PathContext) android.OutputPath {
     76 	return getNdkInstallBase(ctx).Join(ctx, "sysroot")
     77 }
     78 
     79 func getNdkSysrootTimestampFile(ctx android.PathContext) android.Path {
     80 	return android.PathForOutput(ctx, "ndk.timestamp")
     81 }
     82 
     83 func NdkSingleton() blueprint.Singleton {
     84 	return &ndkSingleton{}
     85 }
     86 
     87 type ndkSingleton struct{}
     88 
     89 func (n *ndkSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) {
     90 	installPaths := []string{}
     91 	licensePaths := []string{}
     92 	ctx.VisitAllModules(func(module blueprint.Module) {
     93 		if m, ok := module.(*headerModule); ok {
     94 			installPaths = append(installPaths, m.installPaths...)
     95 			licensePaths = append(licensePaths, m.licensePath.String())
     96 		}
     97 
     98 		if m, ok := module.(*preprocessedHeaderModule); ok {
     99 			installPaths = append(installPaths, m.installPaths...)
    100 			licensePaths = append(licensePaths, m.licensePath.String())
    101 		}
    102 
    103 		if m, ok := module.(*Module); ok {
    104 			if installer, ok := m.installer.(*stubDecorator); ok {
    105 				installPaths = append(installPaths, installer.installPath)
    106 			}
    107 		}
    108 	})
    109 
    110 	combinedLicense := getNdkInstallBase(ctx).Join(ctx, "NOTICE")
    111 	ctx.Build(pctx, blueprint.BuildParams{
    112 		Rule:     android.Cat,
    113 		Outputs:  []string{combinedLicense.String()},
    114 		Inputs:   licensePaths,
    115 		Optional: true,
    116 	})
    117 
    118 	depPaths := append(installPaths, combinedLicense.String())
    119 
    120 	// There's a dummy "ndk" rule defined in ndk/Android.mk that depends on
    121 	// this. `m ndk` will build the sysroots.
    122 	ctx.Build(pctx, blueprint.BuildParams{
    123 		Rule:      android.Touch,
    124 		Outputs:   []string{getNdkSysrootTimestampFile(ctx).String()},
    125 		Implicits: depPaths,
    126 		Optional:  true,
    127 	})
    128 }
    129