Home | History | Annotate | Download | only in cc
      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 	"path/filepath"
     20 	"runtime"
     21 	"strings"
     22 
     23 	"github.com/google/blueprint"
     24 )
     25 
     26 func init() {
     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 	})
     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,
     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 
     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 
     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")
     73 	depFiles := make(android.WritablePaths, 0, len(rsFiles))
     74 	genFiles := make(android.WritablePaths, 0, 2*len(rsFiles))
     75 	for _, rsFile := range rsFiles {
     76 		depFiles = append(depFiles, rsGeneratedDepFile(ctx, rsFile))
     77 		genFiles = append(genFiles,
     78 			rsGeneratedCppFile(ctx, rsFile),
     79 			rsGeneratedHFile(ctx, rsFile))
     80 	}
     81 
     82 	ctx.Build(pctx, android.BuildParams{
     83 		Rule:            rsCpp,
     84 		Description:     "llvm-rs-cc",
     85 		Output:          stampFile,
     86 		ImplicitOutputs: genFiles,
     87 		Inputs:          rsFiles,
     88 		Args: map[string]string{
     89 			"rsFlags":  rsFlags,
     90 			"outDir":   android.PathForModuleGen(ctx, "rs").String(),
     91 			"depFiles": strings.Join(depFiles.Strings(), " "),
     92 		},
     93 	})
     94 
     95 	return android.Paths{stampFile}
     96 }
     97 
     98 func rsFlags(ctx ModuleContext, flags Flags, properties *BaseCompilerProperties) Flags {
     99 	targetApi := String(properties.Renderscript.Target_api)
    100 	if targetApi == "" && ctx.useSdk() {
    101 		switch ctx.sdkVersion() {
    102 		case "current", "system_current", "test_current":
    103 			// Nothing
    104 		default:
    105 			targetApi = android.GetNumericSdkVersion(ctx.sdkVersion())
    106 		}
    107 	}
    108 
    109 	if targetApi != "" {
    110 		flags.rsFlags = append(flags.rsFlags, "-target-api "+targetApi)
    111 	}
    112 
    113 	flags.rsFlags = append(flags.rsFlags, "-Wall", "-Werror")
    114 	flags.rsFlags = append(flags.rsFlags, properties.Renderscript.Flags...)
    115 	if ctx.Arch().ArchType.Multilib == "lib64" {
    116 		flags.rsFlags = append(flags.rsFlags, "-m64")
    117 	} else {
    118 		flags.rsFlags = append(flags.rsFlags, "-m32")
    119 	}
    120 	flags.rsFlags = append(flags.rsFlags, "${config.RsGlobalIncludes}")
    121 
    122 	rootRsIncludeDirs := android.PathsForSource(ctx, properties.Renderscript.Include_dirs)
    123 	flags.rsFlags = append(flags.rsFlags, includeDirsToFlags(rootRsIncludeDirs))
    124 
    125 	flags.GlobalFlags = append(flags.GlobalFlags,
    126 		"-I"+android.PathForModuleGen(ctx, "rs").String(),
    127 		"-Iframeworks/rs",
    128 		"-Iframeworks/rs/cpp",
    129 	)
    130 
    131 	return flags
    132 }
    133