blob: 9cc3417087744326bc5f3111b38cbc8490a34dff [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
Dan Albertc6345fb2016-10-20 01:36:11 -070049
50 // Path to the NOTICE file associated with the headers.
51 License string
Dan Albert914449f2016-06-17 16:45:24 -070052}
53
54type headerModule struct {
55 android.ModuleBase
56
57 properties headerProperies
58
59 installPaths []string
Dan Albertc6345fb2016-10-20 01:36:11 -070060 licensePath android.ModuleSrcPath
Dan Albert914449f2016-06-17 16:45:24 -070061}
62
Colin Cross1e676be2016-10-12 14:38:15 -070063func (m *headerModule) DepsMutator(ctx android.BottomUpMutatorContext) {
64}
65
Dan Albert914449f2016-06-17 16:45:24 -070066func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Dan Albertc6345fb2016-10-20 01:36:11 -070067 if m.properties.License == "" {
68 ctx.PropertyErrorf("license", "field is required")
69 }
70
71 m.licensePath = android.PathForModuleSrc(ctx, m.properties.License)
72
Dan Albert914449f2016-06-17 16:45:24 -070073 srcFiles := ctx.ExpandSources(m.properties.Srcs, nil)
74 for _, header := range srcFiles {
75 // Output path is the sysroot base + "usr/include" + to directory + directory component
76 // of the file without the leading from directory stripped.
77 //
78 // Given:
79 // sysroot base = "ndk/sysroot"
80 // from = "include/foo"
81 // to = "bar"
82 // header = "include/foo/woodly/doodly.h"
83 // output path = "ndk/sysroot/usr/include/bar/woodly/doodly.h"
84
85 // full/platform/path/to/include/foo
86 fullFromPath := android.PathForModuleSrc(ctx, m.properties.From)
87
88 // full/platform/path/to/include/foo/woodly
89 headerDir := filepath.Dir(header.String())
90
91 // woodly
92 strippedHeaderDir, err := filepath.Rel(fullFromPath.String(), headerDir)
93 if err != nil {
94 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", headerDir,
95 fullFromPath.String(), err)
96 }
97
98 // full/platform/path/to/sysroot/usr/include/bar/woodly
99 installDir := getCurrentIncludePath(ctx).Join(ctx, m.properties.To, strippedHeaderDir)
100
101 // full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h
102 installPath := ctx.InstallFile(installDir, header)
103 m.installPaths = append(m.installPaths, installPath.String())
104 }
105
106 if len(m.installPaths) == 0 {
107 ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
108 }
109}
110
111func ndkHeadersFactory() (blueprint.Module, []interface{}) {
112 module := &headerModule{}
Dan Albertc6345fb2016-10-20 01:36:11 -0700113 // Host module rather than device module because device module install steps
114 // do not get run when embedded in make. We're not any of the existing
115 // module types that can be exposed via the Android.mk exporter, so just use
116 // a host module.
117 return android.InitAndroidArchModule(module, android.HostSupportedNoCross,
118 android.MultilibFirst, &module.properties)
Dan Albert914449f2016-06-17 16:45:24 -0700119}