2016-06-18 07:45:24 +08:00
|
|
|
// Copyright 2016 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cc
|
|
|
|
|
|
|
|
import (
|
2017-02-16 09:31:33 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-06-18 07:45:24 +08:00
|
|
|
"path/filepath"
|
2017-11-24 23:38:47 +08:00
|
|
|
"strings"
|
2016-06-18 07:45:24 +08:00
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
)
|
|
|
|
|
2017-02-16 09:31:33 +08:00
|
|
|
var (
|
|
|
|
preprocessBionicHeaders = pctx.AndroidStaticRule("preprocessBionicHeaders",
|
|
|
|
blueprint.RuleParams{
|
2017-03-30 09:33:28 +08:00
|
|
|
// The `&& touch $out` isn't really necessary, but Blueprint won't
|
|
|
|
// let us have only implicit outputs.
|
|
|
|
Command: "$versionerCmd -o $outDir $srcDir $depsPath && touch $out",
|
2017-02-16 09:31:33 +08:00
|
|
|
CommandDeps: []string{"$versionerCmd"},
|
|
|
|
},
|
2017-03-30 09:33:28 +08:00
|
|
|
"depsPath", "srcDir", "outDir")
|
2017-02-16 09:31:33 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
pctx.HostBinToolVariable("versionerCmd", "versioner")
|
|
|
|
}
|
|
|
|
|
2016-06-18 07:45:24 +08:00
|
|
|
// Returns the NDK base include path for use with sdk_version current. Usable with -I.
|
|
|
|
func getCurrentIncludePath(ctx android.ModuleContext) android.OutputPath {
|
|
|
|
return getNdkSysrootBase(ctx).Join(ctx, "usr/include")
|
|
|
|
}
|
|
|
|
|
|
|
|
type headerProperies struct {
|
|
|
|
// Base directory of the headers being installed. As an example:
|
|
|
|
//
|
|
|
|
// ndk_headers {
|
|
|
|
// name: "foo",
|
|
|
|
// from: "include",
|
|
|
|
// to: "",
|
|
|
|
// srcs: ["include/foo/bar/baz.h"],
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
|
|
|
|
// "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
|
2017-11-08 02:57:05 +08:00
|
|
|
From *string
|
2016-06-18 07:45:24 +08:00
|
|
|
|
|
|
|
// Install path within the sysroot. This is relative to usr/include.
|
2017-11-08 02:57:05 +08:00
|
|
|
To *string
|
2016-06-18 07:45:24 +08:00
|
|
|
|
|
|
|
// List of headers to install. Glob compatible. Common case is "include/**/*.h".
|
|
|
|
Srcs []string
|
2016-10-20 16:36:11 +08:00
|
|
|
|
|
|
|
// Path to the NOTICE file associated with the headers.
|
2017-11-08 02:57:05 +08:00
|
|
|
License *string
|
2016-06-18 07:45:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type headerModule struct {
|
|
|
|
android.ModuleBase
|
|
|
|
|
|
|
|
properties headerProperies
|
|
|
|
|
2017-11-29 09:34:01 +08:00
|
|
|
installPaths android.Paths
|
2016-10-20 16:36:11 +08:00
|
|
|
licensePath android.ModuleSrcPath
|
2016-06-18 07:45:24 +08:00
|
|
|
}
|
|
|
|
|
2016-10-13 05:38:15 +08:00
|
|
|
func (m *headerModule) DepsMutator(ctx android.BottomUpMutatorContext) {
|
|
|
|
}
|
|
|
|
|
2017-02-16 09:31:33 +08:00
|
|
|
func getHeaderInstallDir(ctx android.ModuleContext, header android.Path, from string,
|
|
|
|
to string) android.OutputPath {
|
|
|
|
// Output path is the sysroot base + "usr/include" + to directory + directory component
|
|
|
|
// of the file without the leading from directory stripped.
|
|
|
|
//
|
|
|
|
// Given:
|
|
|
|
// sysroot base = "ndk/sysroot"
|
|
|
|
// from = "include/foo"
|
|
|
|
// to = "bar"
|
|
|
|
// header = "include/foo/woodly/doodly.h"
|
|
|
|
// output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
|
|
|
|
|
|
|
|
// full/platform/path/to/include/foo
|
|
|
|
fullFromPath := android.PathForModuleSrc(ctx, from)
|
|
|
|
|
|
|
|
// full/platform/path/to/include/foo/woodly
|
|
|
|
headerDir := filepath.Dir(header.String())
|
|
|
|
|
|
|
|
// woodly
|
|
|
|
strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir,
|
|
|
|
fullFromPath.String(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// full/platform/path/to/sysroot/usr/include/bar/woodly
|
|
|
|
installDir := getCurrentIncludePath(ctx).Join(ctx, to, strippedHeaderDir)
|
|
|
|
|
|
|
|
// full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h
|
|
|
|
return installDir
|
|
|
|
}
|
|
|
|
|
2016-06-18 07:45:24 +08:00
|
|
|
func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
2017-11-08 02:57:05 +08:00
|
|
|
if String(m.properties.License) == "" {
|
2016-10-20 16:36:11 +08:00
|
|
|
ctx.PropertyErrorf("license", "field is required")
|
|
|
|
}
|
|
|
|
|
2017-11-08 02:57:05 +08:00
|
|
|
m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
|
2016-10-20 16:36:11 +08:00
|
|
|
|
2017-11-24 23:38:47 +08:00
|
|
|
// When generating NDK prebuilts, skip installing MIPS headers,
|
|
|
|
// but keep them when doing regular platform build.
|
|
|
|
// Ndk_abis property is only set to true with build/soong/scripts/build-ndk-prebuilts.sh
|
|
|
|
// TODO: Revert this once MIPS is supported in NDK again.
|
|
|
|
if Bool(ctx.AConfig().Ndk_abis) && strings.Contains(ctx.ModuleName(), "mips") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-18 07:45:24 +08:00
|
|
|
srcFiles := ctx.ExpandSources(m.properties.Srcs, nil)
|
|
|
|
for _, header := range srcFiles {
|
2017-11-08 02:57:05 +08:00
|
|
|
installDir := getHeaderInstallDir(ctx, header, String(m.properties.From),
|
|
|
|
String(m.properties.To))
|
2017-09-01 03:29:17 +08:00
|
|
|
installedPath := ctx.InstallFile(installDir, header.Base(), header)
|
2017-02-16 09:31:33 +08:00
|
|
|
installPath := installDir.Join(ctx, header.Base())
|
|
|
|
if installPath != installedPath {
|
|
|
|
panic(fmt.Sprintf(
|
|
|
|
"expected header install path (%q) not equal to actual install path %q",
|
|
|
|
installPath, installedPath))
|
2016-06-18 07:45:24 +08:00
|
|
|
}
|
2017-11-29 09:34:01 +08:00
|
|
|
m.installPaths = append(m.installPaths, installPath)
|
2016-06-18 07:45:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(m.installPaths) == 0 {
|
|
|
|
ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-24 06:06:31 +08:00
|
|
|
func ndkHeadersFactory() android.Module {
|
2016-06-18 07:45:24 +08:00
|
|
|
module := &headerModule{}
|
2017-06-24 06:06:31 +08:00
|
|
|
module.AddProperties(&module.properties)
|
|
|
|
android.InitAndroidModule(module)
|
|
|
|
return module
|
2016-06-18 07:45:24 +08:00
|
|
|
}
|
2017-02-16 09:31:33 +08:00
|
|
|
|
|
|
|
type preprocessedHeaderProperies struct {
|
|
|
|
// Base directory of the headers being installed. As an example:
|
|
|
|
//
|
|
|
|
// preprocessed_ndk_headers {
|
|
|
|
// name: "foo",
|
|
|
|
// from: "include",
|
|
|
|
// to: "",
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
|
|
|
|
// "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
|
2017-11-10 14:42:32 +08:00
|
|
|
From *string
|
2017-02-16 09:31:33 +08:00
|
|
|
|
|
|
|
// Install path within the sysroot. This is relative to usr/include.
|
2017-11-10 14:42:32 +08:00
|
|
|
To *string
|
2017-02-16 09:31:33 +08:00
|
|
|
|
|
|
|
// Path to the NOTICE file associated with the headers.
|
2017-11-10 14:42:32 +08:00
|
|
|
License *string
|
2017-02-16 09:31:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Like ndk_headers, but preprocesses the headers with the bionic versioner:
|
|
|
|
// https://android.googlesource.com/platform/bionic/+/master/tools/versioner/README.md.
|
|
|
|
//
|
|
|
|
// Unlike ndk_headers, we don't operate on a list of sources but rather a whole directory, the
|
|
|
|
// module does not have the srcs property, and operates on a full directory (the `from` property).
|
|
|
|
//
|
|
|
|
// Note that this is really only built to handle bionic/libc/include.
|
|
|
|
type preprocessedHeaderModule struct {
|
|
|
|
android.ModuleBase
|
|
|
|
|
|
|
|
properties preprocessedHeaderProperies
|
|
|
|
|
2017-11-29 09:34:01 +08:00
|
|
|
installPaths android.Paths
|
2017-02-16 09:31:33 +08:00
|
|
|
licensePath android.ModuleSrcPath
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *preprocessedHeaderModule) DepsMutator(ctx android.BottomUpMutatorContext) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *preprocessedHeaderModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
2017-11-10 14:42:32 +08:00
|
|
|
if String(m.properties.License) == "" {
|
2017-02-16 09:31:33 +08:00
|
|
|
ctx.PropertyErrorf("license", "field is required")
|
|
|
|
}
|
|
|
|
|
2017-11-10 14:42:32 +08:00
|
|
|
m.licensePath = android.PathForModuleSrc(ctx, String(m.properties.License))
|
2017-02-16 09:31:33 +08:00
|
|
|
|
2017-11-10 14:42:32 +08:00
|
|
|
fromSrcPath := android.PathForModuleSrc(ctx, String(m.properties.From))
|
|
|
|
toOutputPath := getCurrentIncludePath(ctx).Join(ctx, String(m.properties.To))
|
2017-02-16 09:31:33 +08:00
|
|
|
srcFiles := ctx.Glob(filepath.Join(fromSrcPath.String(), "**/*.h"), nil)
|
|
|
|
var installPaths []android.WritablePath
|
|
|
|
for _, header := range srcFiles {
|
2017-11-10 14:42:32 +08:00
|
|
|
installDir := getHeaderInstallDir(ctx, header, String(m.properties.From), String(m.properties.To))
|
2017-02-16 09:31:33 +08:00
|
|
|
installPath := installDir.Join(ctx, header.Base())
|
|
|
|
installPaths = append(installPaths, installPath)
|
2017-11-29 09:34:01 +08:00
|
|
|
m.installPaths = append(m.installPaths, installPath)
|
2017-02-16 09:31:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(m.installPaths) == 0 {
|
2017-11-10 14:42:32 +08:00
|
|
|
ctx.ModuleErrorf("glob %q matched zero files", String(m.properties.From))
|
2017-02-16 09:31:33 +08:00
|
|
|
}
|
|
|
|
|
2017-03-20 04:44:32 +08:00
|
|
|
processHeadersWithVersioner(ctx, fromSrcPath, toOutputPath, srcFiles, installPaths)
|
|
|
|
}
|
|
|
|
|
|
|
|
func processHeadersWithVersioner(ctx android.ModuleContext, srcDir, outDir android.Path, srcFiles android.Paths, installPaths []android.WritablePath) android.Path {
|
2017-02-16 09:31:33 +08:00
|
|
|
// The versioner depends on a dependencies directory to simplify determining include paths
|
|
|
|
// when parsing headers. This directory contains architecture specific directories as well
|
|
|
|
// as a common directory, each of which contains symlinks to the actually directories to
|
|
|
|
// be included.
|
|
|
|
//
|
|
|
|
// ctx.Glob doesn't follow symlinks, so we need to do this ourselves so we correctly
|
|
|
|
// depend on these headers.
|
|
|
|
// TODO(http://b/35673191): Update the versioner to use a --sysroot.
|
|
|
|
depsPath := android.PathForSource(ctx, "bionic/libc/versioner-dependencies")
|
|
|
|
depsGlob := ctx.Glob(filepath.Join(depsPath.String(), "**/*"), nil)
|
|
|
|
for i, path := range depsGlob {
|
|
|
|
fileInfo, err := os.Lstat(path.String())
|
|
|
|
if err != nil {
|
|
|
|
ctx.ModuleErrorf("os.Lstat(%q) failed: %s", path.String, err)
|
|
|
|
}
|
|
|
|
if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
|
|
|
|
dest, err := os.Readlink(path.String())
|
|
|
|
if err != nil {
|
|
|
|
ctx.ModuleErrorf("os.Readlink(%q) failed: %s",
|
|
|
|
path.String, err)
|
|
|
|
}
|
|
|
|
// Additional .. to account for the symlink itself.
|
|
|
|
depsGlob[i] = android.PathForSource(
|
|
|
|
ctx, filepath.Clean(filepath.Join(path.String(), "..", dest)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 09:33:28 +08:00
|
|
|
timestampFile := android.PathForModuleOut(ctx, "versioner.timestamp")
|
2017-10-24 08:16:14 +08:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2017-02-16 09:31:33 +08:00
|
|
|
Rule: preprocessBionicHeaders,
|
2017-05-10 04:45:28 +08:00
|
|
|
Description: "versioner preprocess " + srcDir.Rel(),
|
2017-03-30 09:33:28 +08:00
|
|
|
Output: timestampFile,
|
2017-02-16 09:31:33 +08:00
|
|
|
Implicits: append(srcFiles, depsGlob...),
|
|
|
|
ImplicitOutputs: installPaths,
|
|
|
|
Args: map[string]string{
|
|
|
|
"depsPath": depsPath.String(),
|
2017-03-20 04:44:32 +08:00
|
|
|
"srcDir": srcDir.String(),
|
|
|
|
"outDir": outDir.String(),
|
2017-02-16 09:31:33 +08:00
|
|
|
},
|
|
|
|
})
|
2017-03-20 04:44:32 +08:00
|
|
|
|
|
|
|
return timestampFile
|
2017-02-16 09:31:33 +08:00
|
|
|
}
|
|
|
|
|
2017-06-24 06:06:31 +08:00
|
|
|
func preprocessedNdkHeadersFactory() android.Module {
|
2017-02-16 09:31:33 +08:00
|
|
|
module := &preprocessedHeaderModule{}
|
2017-06-24 06:06:31 +08:00
|
|
|
|
|
|
|
module.AddProperties(&module.properties)
|
|
|
|
|
2017-02-16 09:31:33 +08:00
|
|
|
// Host module rather than device module because device module install steps
|
|
|
|
// do not get run when embedded in make. We're not any of the existing
|
|
|
|
// module types that can be exposed via the Android.mk exporter, so just use
|
|
|
|
// a host module.
|
2017-06-24 06:06:31 +08:00
|
|
|
android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibFirst)
|
|
|
|
|
|
|
|
return module
|
2017-02-16 09:31:33 +08:00
|
|
|
}
|