blob: 5d70b8916ee2b9385a64dec3eef8c481bd32c4db [file] [log] [blame]
Dan Albert914449f2016-06-17 16:45:24 -07001// 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 "path/filepath"
19
20 "github.com/google/blueprint"
21
22 "android/soong/android"
23)
24
25// Returns the NDK base include path for use with sdk_version current. Usable with -I.
26func getCurrentIncludePath(ctx android.ModuleContext) android.OutputPath {
27 return getNdkSysrootBase(ctx).Join(ctx, "usr/include")
28}
29
30type headerProperies struct {
31 // Base directory of the headers being installed. As an example:
32 //
33 // ndk_headers {
34 // name: "foo",
35 // from: "include",
36 // to: "",
37 // srcs: ["include/foo/bar/baz.h"],
38 // }
39 //
40 // Will install $SYSROOT/usr/include/foo/bar/baz.h. If `from` were instead
41 // "include/foo", it would have installed $SYSROOT/usr/include/bar/baz.h.
42 From string
43
44 // Install path within the sysroot. This is relative to usr/include.
45 To string
46
47 // List of headers to install. Glob compatible. Common case is "include/**/*.h".
48 Srcs []string
49}
50
51type headerModule struct {
52 android.ModuleBase
53
54 properties headerProperies
55
56 installPaths []string
57}
58
59func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
60 srcFiles := ctx.ExpandSources(m.properties.Srcs, nil)
61 for _, header := range srcFiles {
62 // Output path is the sysroot base + "usr/include" + to directory + directory component
63 // of the file without the leading from directory stripped.
64 //
65 // Given:
66 // sysroot base = "ndk/sysroot"
67 // from = "include/foo"
68 // to = "bar"
69 // header = "include/foo/woodly/doodly.h"
70 // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
71
72 // full/platform/path/to/include/foo
73 fullFromPath := android.PathForModuleSrc(ctx, m.properties.From)
74
75 // full/platform/path/to/include/foo/woodly
76 headerDir := filepath.Dir(header.String())
77
78 // woodly
79 strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir)
80 if err != nil {
81 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir,
82 fullFromPath.String(), err)
83 }
84
85 // full/platform/path/to/sysroot/usr/include/bar/woodly
86 installDir := getCurrentIncludePath(ctx).Join(ctx, m.properties.To, strippedHeaderDir)
87
88 // full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h
89 installPath := ctx.InstallFile(installDir, header)
90 m.installPaths = append(m.installPaths, installPath.String())
91 }
92
93 if len(m.installPaths) == 0 {
94 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
95 }
96}
97
98func ndkHeadersFactory() (blueprint.Module, []interface{}) {
99 module := &headerModule{}
100 return android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst,
101 &module.properties)
102}