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