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