| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1 | // 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 |  | 
 | 15 | package common | 
 | 16 |  | 
 | 17 | import ( | 
| Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 18 | 	"fmt" | 
| Colin Cross | f229827 | 2015-05-12 11:36:53 -0700 | [diff] [blame] | 19 | 	"os" | 
| Colin Cross | 6a745c6 | 2015-06-16 16:38:10 -0700 | [diff] [blame] | 20 | 	"path/filepath" | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 21 | ) | 
 | 22 |  | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 23 | // ModuleOutDir returns the path to the module-specific output directory. | 
 | 24 | func ModuleOutDir(ctx AndroidModuleContext) string { | 
| Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 25 | 	return filepath.Join(ctx.AConfig().IntermediatesDir(), | 
 | 26 | 		ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir()) | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 27 | } | 
 | 28 |  | 
 | 29 | // ModuleSrcDir returns the path of the directory that all source file paths are | 
 | 30 | // specified relative to. | 
| Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 31 | func ModuleSrcDir(ctx AndroidModuleContext) string { | 
 | 32 | 	return filepath.Join(ctx.AConfig().SrcDir(), ctx.ModuleDir()) | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 33 | } | 
 | 34 |  | 
 | 35 | // ModuleBinDir returns the path to the module- and architecture-specific binary | 
 | 36 | // output directory. | 
 | 37 | func 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. | 
 | 43 | func ModuleLibDir(ctx AndroidModuleContext) string { | 
 | 44 | 	return filepath.Join(ModuleOutDir(ctx), "lib") | 
 | 45 | } | 
 | 46 |  | 
 | 47 | // ModuleGenDir returns the module directory for generated files | 
 | 48 | // path. | 
 | 49 | func 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. | 
 | 55 | func 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. | 
 | 62 | func ModuleGoPackageDir(ctx AndroidModuleContext) string { | 
 | 63 | 	return filepath.Join(ModuleOutDir(ctx), "pkg") | 
 | 64 | } | 
 | 65 |  | 
 | 66 | // ModuleIncludeDir returns the module-specific public include directory path. | 
 | 67 | func ModuleIncludeDir(ctx AndroidModuleContext) string { | 
 | 68 | 	return filepath.Join(ModuleOutDir(ctx), "include") | 
 | 69 | } | 
 | 70 |  | 
 | 71 | // ModuleProtoDir returns the module-specific public proto include directory path. | 
 | 72 | func ModuleProtoDir(ctx AndroidModuleContext) string { | 
 | 73 | 	return filepath.Join(ModuleOutDir(ctx), "proto") | 
 | 74 | } | 
 | 75 |  | 
 | 76 | func ModuleJSCompiledDir(ctx AndroidModuleContext) string { | 
 | 77 | 	return filepath.Join(ModuleOutDir(ctx), "js") | 
 | 78 | } | 
| Colin Cross | f229827 | 2015-05-12 11:36:53 -0700 | [diff] [blame] | 79 |  | 
 | 80 | // CheckModuleSrcDirsExist logs an error on a property if any of the directories relative to the | 
 | 81 | // Blueprints file don't exist. | 
 | 82 | func 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. | 
 | 97 | func 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 Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 109 |  | 
 | 110 | // Returns a path relative to the top level source directory.  Panics if path is not inside the | 
 | 111 | // top level source directory. | 
 | 112 | func 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 | } |