aboutsummaryrefslogtreecommitdiffstats
path: root/cc/ndk_headers.go
blob: 5d70b8916ee2b9385a64dec3eef8c481bd32c4db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// 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 (
	"path/filepath"

	"github.com/google/blueprint"

	"android/soong/android"
)

// 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.
	From string

	// Install path within the sysroot. This is relative to usr/include.
	To string

	// List of headers to install. Glob compatible. Common case is "include/**/*.h".
	Srcs []string
}

type headerModule struct {
	android.ModuleBase

	properties headerProperies

	installPaths []string
}

func (m *headerModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
	srcFiles := ctx.ExpandSources(m.properties.Srcs, nil)
	for _, header := range srcFiles {
		// 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, m.properties.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, m.properties.To, strippedHeaderDir)

		// full/platform/path/to/sysroot/usr/include/bar/woodly/doodly.h
		installPath := ctx.InstallFile(installDir, header)
		m.installPaths = append(m.installPaths, installPath.String())
	}

	if len(m.installPaths) == 0 {
		ctx.ModuleErrorf("srcs %q matched zero files", m.properties.Srcs)
	}
}

func ndkHeadersFactory() (blueprint.Module, []interface{}) {
	module := &headerModule{}
	return android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst,
		&module.properties)
}