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 &&
39(echo '${out}: \' && cat ${depFiles} | awk 'start { sub(/( \\)?$$/, " \\"); print } /:/ { start=1 }') > ${out}.d &&
40touch $out
41`, "\n", "", -1)
42
43var (
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 .rscript 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.
56func 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
61func 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
66func 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
71func 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	headers := make(android.Paths, 0, len(rsFiles))
76	for _, rsFile := range rsFiles {
77		depFiles = append(depFiles, rsGeneratedDepFile(ctx, rsFile))
78		headerFile := rsGeneratedHFile(ctx, rsFile)
79		genFiles = append(genFiles, rsGeneratedCppFile(ctx, rsFile), headerFile)
80		headers = append(headers, headerFile)
81	}
82
83	ctx.Build(pctx, android.BuildParams{
84		Rule:            rsCpp,
85		Description:     "llvm-rs-cc",
86		Output:          stampFile,
87		ImplicitOutputs: genFiles,
88		Inputs:          rsFiles,
89		Args: map[string]string{
90			"rsFlags":  rsFlags,
91			"outDir":   android.PathForModuleGen(ctx, "rs").String(),
92			"depFiles": strings.Join(depFiles.Strings(), " "),
93		},
94	})
95
96	return headers
97}
98
99func rsFlags(ctx ModuleContext, flags Flags, properties *BaseCompilerProperties) Flags {
100	targetApi := String(properties.Renderscript.Target_api)
101	if targetApi == "" && ctx.useSdk() {
102		switch ctx.sdkVersion() {
103		case "current", "system_current", "test_current":
104			// Nothing
105		default:
106			targetApi = android.GetNumericSdkVersion(ctx.sdkVersion())
107		}
108	}
109
110	if targetApi != "" {
111		flags.rsFlags = append(flags.rsFlags, "-target-api "+targetApi)
112	}
113
114	flags.rsFlags = append(flags.rsFlags, "-Wall", "-Werror")
115	flags.rsFlags = append(flags.rsFlags, properties.Renderscript.Flags...)
116	if ctx.Arch().ArchType.Multilib == "lib64" {
117		flags.rsFlags = append(flags.rsFlags, "-m64")
118	} else {
119		flags.rsFlags = append(flags.rsFlags, "-m32")
120	}
121	flags.rsFlags = append(flags.rsFlags, "${config.RsGlobalIncludes}")
122
123	rootRsIncludeDirs := android.PathsForSource(ctx, properties.Renderscript.Include_dirs)
124	flags.rsFlags = append(flags.rsFlags, includeDirsToFlags(rootRsIncludeDirs))
125
126	flags.Local.CommonFlags = append(flags.Local.CommonFlags,
127		"-I"+android.PathForModuleGen(ctx, "rs").String(),
128		"-Iframeworks/rs",
129		"-Iframeworks/rs/cpp",
130	)
131
132	return flags
133}
134