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 	"strings"
     20 
     21 	"github.com/google/blueprint"
     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,
     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 
     52 func rsGeneratedDepFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath {
     53 	fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext())
     54 	return android.PathForModuleGen(ctx, "rs", fileName+".d")
     55 }
     56 
     57 func rsGenerateCpp(ctx android.ModuleContext, rsFiles android.Paths, rsFlags string) android.Paths {
     58 	stampFile := android.PathForModuleGen(ctx, "rs", "rs.stamp")
     59 	depFiles := make(android.WritablePaths, len(rsFiles))
     60 	cppFiles := make(android.WritablePaths, len(rsFiles))
     61 	for i, rsFile := range rsFiles {
     62 		depFiles[i] = rsGeneratedDepFile(ctx, rsFile)
     63 		cppFiles[i] = rsGeneratedCppFile(ctx, rsFile)
     64 	}
     65 
     66 	ctx.Build(pctx, android.BuildParams{
     67 		Rule:            rsCpp,
     68 		Description:     "llvm-rs-cc",
     69 		Output:          stampFile,
     70 		ImplicitOutputs: cppFiles,
     71 		Inputs:          rsFiles,
     72 		Args: map[string]string{
     73 			"rsFlags":  rsFlags,
     74 			"outDir":   android.PathForModuleGen(ctx, "rs").String(),
     75 			"depFiles": strings.Join(depFiles.Strings(), " "),
     76 		},
     77 	})
     78 
     79 	return android.Paths{stampFile}
     80 }
     81 
     82 func rsFlags(ctx ModuleContext, flags Flags, properties *BaseCompilerProperties) Flags {
     83 	targetApi := String(properties.Renderscript.Target_api)
     84 	if targetApi == "" && ctx.useSdk() {
     85 		switch ctx.sdkVersion() {
     86 		case "current", "system_current", "test_current":
     87 			// Nothing
     88 		default:
     89 			targetApi = android.GetNumericSdkVersion(ctx.sdkVersion())
     90 		}
     91 	}
     92 
     93 	if targetApi != "" {
     94 		flags.rsFlags = append(flags.rsFlags, "-target-api "+targetApi)
     95 	}
     96 
     97 	flags.rsFlags = append(flags.rsFlags, "-Wall", "-Werror")
     98 	flags.rsFlags = append(flags.rsFlags, properties.Renderscript.Flags...)
     99 	if ctx.Arch().ArchType.Multilib == "lib64" {
    100 		flags.rsFlags = append(flags.rsFlags, "-m64")
    101 	} else {
    102 		flags.rsFlags = append(flags.rsFlags, "-m32")
    103 	}
    104 	flags.rsFlags = append(flags.rsFlags, "${config.RsGlobalIncludes}")
    105 
    106 	rootRsIncludeDirs := android.PathsForSource(ctx, properties.Renderscript.Include_dirs)
    107 	flags.rsFlags = append(flags.rsFlags, includeDirsToFlags(rootRsIncludeDirs))
    108 
    109 	flags.GlobalFlags = append(flags.GlobalFlags,
    110 		"-I"+android.PathForModuleGen(ctx, "rs").String(),
    111 		"-Iframeworks/rs",
    112 		"-Iframeworks/rs/cpp",
    113 	)
    114 
    115 	return flags
    116 }
    117