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