| Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 1 | // Copyright 2017 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 cc | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "android/soong/android" | 
| Colin Cross | 2548b44 | 2018-11-18 20:57:21 -0800 | [diff] [blame] | 19 | "path/filepath" | 
|  | 20 | "runtime" | 
| Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 21 | "strings" | 
|  | 22 |  | 
|  | 23 | "github.com/google/blueprint" | 
| Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 24 | ) | 
|  | 25 |  | 
|  | 26 | func init() { | 
| Colin Cross | 2548b44 | 2018-11-18 20:57:21 -0800 | [diff] [blame] | 27 | pctx.VariableFunc("rsCmd", func(ctx android.PackageVarContext) string { | 
|  | 28 | if ctx.Config().UnbundledBuild() { | 
|  | 29 | // Use RenderScript prebuilts for unbundled builds but not PDK builds | 
|  | 30 | return filepath.Join("prebuilts/sdk/tools", runtime.GOOS, "bin/llvm-rs-cc") | 
|  | 31 | } else { | 
|  | 32 | return pctx.HostBinToolPath(ctx, "llvm-rs-cc").String() | 
|  | 33 | } | 
|  | 34 | }) | 
| Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 35 | } | 
|  | 36 |  | 
|  | 37 | var rsCppCmdLine = strings.Replace(` | 
|  | 38 | ${rsCmd} -o ${outDir} -d ${outDir} -a ${out} -MD -reflect-c++ ${rsFlags} $in && | 
|  | 39 | (echo '${out}: \' && cat ${depFiles} | awk 'start { sub(/( \\)?$$/, " \\"); print } /:/ { start=1 }') > ${out}.d && | 
|  | 40 | touch $out | 
|  | 41 | `, "\n", "", -1) | 
|  | 42 |  | 
|  | 43 | var ( | 
|  | 44 | rsCpp = pctx.AndroidStaticRule("rsCpp", | 
|  | 45 | blueprint.RuleParams{ | 
|  | 46 | Command:     rsCppCmdLine, | 
|  | 47 | CommandDeps: []string{"$rsCmd"}, | 
|  | 48 | Depfile:     "${out}.d", | 
|  | 49 | Deps:        blueprint.DepsGCC, | 
| Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 50 | }, | 
|  | 51 | "depFiles", "outDir", "rsFlags", "stampFile") | 
|  | 52 | ) | 
|  | 53 |  | 
|  | 54 | // Takes a path to a .rs or .fs file, and returns a path to a generated ScriptC_*.cpp file | 
|  | 55 | // This has to match the logic in llvm-rs-cc in DetermineOutputFile. | 
|  | 56 | func rsGeneratedCppFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath { | 
|  | 57 | fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext()) | 
|  | 58 | return android.PathForModuleGen(ctx, "rs", "ScriptC_"+fileName+".cpp") | 
|  | 59 | } | 
|  | 60 |  | 
| Colin Cross | 80e6054 | 2018-03-19 22:44:29 -0700 | [diff] [blame] | 61 | func rsGeneratedHFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath { | 
|  | 62 | fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext()) | 
|  | 63 | return android.PathForModuleGen(ctx, "rs", "ScriptC_"+fileName+".h") | 
|  | 64 | } | 
|  | 65 |  | 
| Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 66 | func rsGeneratedDepFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath { | 
|  | 67 | fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext()) | 
|  | 68 | return android.PathForModuleGen(ctx, "rs", fileName+".d") | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | func rsGenerateCpp(ctx android.ModuleContext, rsFiles android.Paths, rsFlags string) android.Paths { | 
|  | 72 | stampFile := android.PathForModuleGen(ctx, "rs", "rs.stamp") | 
| Colin Cross | 80e6054 | 2018-03-19 22:44:29 -0700 | [diff] [blame] | 73 | depFiles := make(android.WritablePaths, 0, len(rsFiles)) | 
|  | 74 | genFiles := make(android.WritablePaths, 0, 2*len(rsFiles)) | 
| Dan Willemsen | b085b9b | 2019-06-13 04:55:09 +0000 | [diff] [blame] | 75 | headers := make(android.Paths, 0, len(rsFiles)) | 
| Colin Cross | 80e6054 | 2018-03-19 22:44:29 -0700 | [diff] [blame] | 76 | for _, rsFile := range rsFiles { | 
|  | 77 | depFiles = append(depFiles, rsGeneratedDepFile(ctx, rsFile)) | 
| Dan Willemsen | b085b9b | 2019-06-13 04:55:09 +0000 | [diff] [blame] | 78 | headerFile := rsGeneratedHFile(ctx, rsFile) | 
|  | 79 | genFiles = append(genFiles, rsGeneratedCppFile(ctx, rsFile), headerFile) | 
|  | 80 | headers = append(headers, headerFile) | 
| Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
| Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 83 | ctx.Build(pctx, android.BuildParams{ | 
| Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 84 | Rule:            rsCpp, | 
| Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 85 | Description:     "llvm-rs-cc", | 
| Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 86 | Output:          stampFile, | 
| Colin Cross | 80e6054 | 2018-03-19 22:44:29 -0700 | [diff] [blame] | 87 | ImplicitOutputs: genFiles, | 
| Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 88 | Inputs:          rsFiles, | 
|  | 89 | Args: map[string]string{ | 
|  | 90 | "rsFlags":  rsFlags, | 
|  | 91 | "outDir":   android.PathForModuleGen(ctx, "rs").String(), | 
|  | 92 | "depFiles": strings.Join(depFiles.Strings(), " "), | 
|  | 93 | }, | 
|  | 94 | }) | 
|  | 95 |  | 
| Dan Willemsen | b085b9b | 2019-06-13 04:55:09 +0000 | [diff] [blame] | 96 | return headers | 
| Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 97 | } | 
|  | 98 |  | 
|  | 99 | func rsFlags(ctx ModuleContext, flags Flags, properties *BaseCompilerProperties) Flags { | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 100 | targetApi := String(properties.Renderscript.Target_api) | 
| Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 101 | if targetApi == "" && ctx.useSdk() { | 
| Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 102 | switch ctx.sdkVersion() { | 
|  | 103 | case "current", "system_current", "test_current": | 
|  | 104 | // Nothing | 
|  | 105 | default: | 
| Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 106 | targetApi = android.GetNumericSdkVersion(ctx.sdkVersion()) | 
| Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 107 | } | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | if targetApi != "" { | 
|  | 111 | flags.rsFlags = append(flags.rsFlags, "-target-api "+targetApi) | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | flags.rsFlags = append(flags.rsFlags, "-Wall", "-Werror") | 
|  | 115 | flags.rsFlags = append(flags.rsFlags, properties.Renderscript.Flags...) | 
|  | 116 | if ctx.Arch().ArchType.Multilib == "lib64" { | 
|  | 117 | flags.rsFlags = append(flags.rsFlags, "-m64") | 
|  | 118 | } else { | 
|  | 119 | flags.rsFlags = append(flags.rsFlags, "-m32") | 
|  | 120 | } | 
|  | 121 | flags.rsFlags = append(flags.rsFlags, "${config.RsGlobalIncludes}") | 
|  | 122 |  | 
|  | 123 | rootRsIncludeDirs := android.PathsForSource(ctx, properties.Renderscript.Include_dirs) | 
|  | 124 | flags.rsFlags = append(flags.rsFlags, includeDirsToFlags(rootRsIncludeDirs)) | 
|  | 125 |  | 
|  | 126 | flags.GlobalFlags = append(flags.GlobalFlags, | 
| Colin Cross | 2101f4a | 2017-05-08 09:16:34 -0700 | [diff] [blame] | 127 | "-I"+android.PathForModuleGen(ctx, "rs").String(), | 
|  | 128 | "-Iframeworks/rs", | 
|  | 129 | "-Iframeworks/rs/cpp", | 
|  | 130 | ) | 
| Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 131 |  | 
|  | 132 | return flags | 
|  | 133 | } |