blob: d92dcf9197a491662d3a415fd6d1bd1375e279e6 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 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 common
16
17import (
Colin Cross6e18ca42015-07-14 18:55:36 -070018 "fmt"
Colin Crossf2298272015-05-12 11:36:53 -070019 "os"
Colin Cross6a745c62015-06-16 16:38:10 -070020 "path/filepath"
Colin Cross3f40fa42015-01-30 17:27:36 -080021)
22
Colin Cross3f40fa42015-01-30 17:27:36 -080023// ModuleOutDir returns the path to the module-specific output directory.
24func ModuleOutDir(ctx AndroidModuleContext) string {
Colin Cross1332b002015-04-07 17:11:30 -070025 return filepath.Join(ctx.AConfig().IntermediatesDir(),
26 ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir())
Colin Cross3f40fa42015-01-30 17:27:36 -080027}
28
29// ModuleSrcDir returns the path of the directory that all source file paths are
30// specified relative to.
Colin Cross1332b002015-04-07 17:11:30 -070031func ModuleSrcDir(ctx AndroidModuleContext) string {
32 return filepath.Join(ctx.AConfig().SrcDir(), ctx.ModuleDir())
Colin Cross3f40fa42015-01-30 17:27:36 -080033}
34
35// ModuleBinDir returns the path to the module- and architecture-specific binary
36// output directory.
37func ModuleBinDir(ctx AndroidModuleContext) string {
38 return filepath.Join(ModuleOutDir(ctx), "bin")
39}
40
41// ModuleLibDir returns the path to the module- and architecture-specific
42// library output directory.
43func ModuleLibDir(ctx AndroidModuleContext) string {
44 return filepath.Join(ModuleOutDir(ctx), "lib")
45}
46
47// ModuleGenDir returns the module directory for generated files
48// path.
49func ModuleGenDir(ctx AndroidModuleContext) string {
50 return filepath.Join(ModuleOutDir(ctx), "gen")
51}
52
53// ModuleObjDir returns the module- and architecture-specific object directory
54// path.
55func ModuleObjDir(ctx AndroidModuleContext) string {
56 return filepath.Join(ModuleOutDir(ctx), "obj")
57}
58
59// ModuleGoPackageDir returns the module-specific package root directory path.
60// This directory is where the final package .a files are output and where
61// dependent modules search for this package via -I arguments.
62func ModuleGoPackageDir(ctx AndroidModuleContext) string {
63 return filepath.Join(ModuleOutDir(ctx), "pkg")
64}
65
66// ModuleIncludeDir returns the module-specific public include directory path.
67func ModuleIncludeDir(ctx AndroidModuleContext) string {
68 return filepath.Join(ModuleOutDir(ctx), "include")
69}
70
71// ModuleProtoDir returns the module-specific public proto include directory path.
72func ModuleProtoDir(ctx AndroidModuleContext) string {
73 return filepath.Join(ModuleOutDir(ctx), "proto")
74}
75
76func ModuleJSCompiledDir(ctx AndroidModuleContext) string {
77 return filepath.Join(ModuleOutDir(ctx), "js")
78}
Colin Crossf2298272015-05-12 11:36:53 -070079
80// CheckModuleSrcDirsExist logs an error on a property if any of the directories relative to the
81// Blueprints file don't exist.
82func CheckModuleSrcDirsExist(ctx AndroidModuleContext, dirs []string, prop string) {
83 for _, dir := range dirs {
84 fullDir := filepath.Join(ModuleSrcDir(ctx), dir)
85 if _, err := os.Stat(fullDir); err != nil {
86 if os.IsNotExist(err) {
87 ctx.PropertyErrorf(prop, "module source directory %q does not exist", dir)
88 } else {
89 ctx.PropertyErrorf(prop, "%s", err.Error())
90 }
91 }
92 }
93}
94
95// CheckModuleSrcDirsExist logs an error on a property if any of the directories relative to the
96// top of the source tree don't exist.
97func CheckSrcDirsExist(ctx AndroidModuleContext, dirs []string, prop string) {
98 for _, dir := range dirs {
99 fullDir := filepath.Join(ctx.AConfig().SrcDir(), dir)
100 if _, err := os.Stat(fullDir); err != nil {
101 if os.IsNotExist(err) {
102 ctx.PropertyErrorf(prop, "top-level source directory %q does not exist", dir)
103 } else {
104 ctx.PropertyErrorf(prop, "%s", err.Error())
105 }
106 }
107 }
108}
Colin Cross6e18ca42015-07-14 18:55:36 -0700109
110// Returns a path relative to the top level source directory. Panics if path is not inside the
111// top level source directory.
112func SrcDirRelPath(ctx AndroidModuleContext, path string) string {
113 srcDir := ctx.AConfig().SrcDir()
114 relPath, err := filepath.Rel(srcDir, path)
115 if err != nil {
116 panic(fmt.Errorf("%q is not inside %q: %s", path, srcDir, err.Error()))
117 }
118
119 return relPath
120}