blob: 0c3d089f4fd8ac640074af38f8646ebefc230a31 [file] [log] [blame]
Colin Cross581c1892015-04-07 16:50:10 -07001// Copyright 2015 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
Colin Cross581c1892015-04-07 16:50:10 -070017import (
Inseob Kim21f26902018-09-06 00:55:20 +090018 "path/filepath"
19
Colin Cross581c1892015-04-07 16:50:10 -070020 "github.com/google/blueprint"
Colin Cross581c1892015-04-07 16:50:10 -070021
Colin Cross635c3b02016-05-18 15:37:25 -070022 "android/soong/android"
Colin Cross581c1892015-04-07 16:50:10 -070023)
24
25func init() {
Dan Willemsenb7adae82018-05-24 15:45:21 -070026 pctx.SourcePathVariable("lexCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/flex")
Dan Willemsen6f46a382018-01-08 17:26:13 -080027 pctx.SourcePathVariable("yaccCmd", "prebuilts/build-tools/${config.HostPrebuiltTag}/bin/bison")
28 pctx.SourcePathVariable("yaccDataDir", "prebuilts/build-tools/common/bison")
Dan Willemsene1240db2016-11-03 14:28:51 -070029
30 pctx.HostBinToolVariable("aidlCmd", "aidl-cpp")
Inseob Kim21f26902018-09-06 00:55:20 +090031 pctx.HostBinToolVariable("syspropCmd", "sysprop_cpp")
Colin Cross581c1892015-04-07 16:50:10 -070032}
33
34var (
Colin Cross9d45bb72016-08-29 16:14:13 -070035 yacc = pctx.AndroidStaticRule("yacc",
Colin Cross581c1892015-04-07 16:50:10 -070036 blueprint.RuleParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -070037 Command: "BISON_PKGDATADIR=$yaccDataDir $yaccCmd -d $yaccFlags --defines=$hFile -o $out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080038 CommandDeps: []string{"$yaccCmd"},
Colin Cross581c1892015-04-07 16:50:10 -070039 },
Dan Willemsen9f3c5742016-11-03 14:28:31 -070040 "yaccFlags", "hFile")
Colin Cross581c1892015-04-07 16:50:10 -070041
Colin Cross9d45bb72016-08-29 16:14:13 -070042 lex = pctx.AndroidStaticRule("lex",
Colin Cross581c1892015-04-07 16:50:10 -070043 blueprint.RuleParams{
44 Command: "$lexCmd -o$out $in",
Dan Willemsenc94a7682015-11-17 15:27:28 -080045 CommandDeps: []string{"$lexCmd"},
Colin Cross581c1892015-04-07 16:50:10 -070046 })
Dan Willemsene1240db2016-11-03 14:28:51 -070047
48 aidl = pctx.AndroidStaticRule("aidl",
49 blueprint.RuleParams{
Steven Morelandb468bca2018-07-06 11:36:32 -070050 Command: "$aidlCmd -d${out}.d --ninja $aidlFlags $in $outDir $out",
Dan Willemsene1240db2016-11-03 14:28:51 -070051 CommandDeps: []string{"$aidlCmd"},
52 Depfile: "${out}.d",
53 Deps: blueprint.DepsGCC,
Dan Willemsene1240db2016-11-03 14:28:51 -070054 },
55 "aidlFlags", "outDir")
Dan Willemsen4f1c3d42017-09-09 01:15:26 -070056
Inseob Kim21f26902018-09-06 00:55:20 +090057 sysprop = pctx.AndroidStaticRule("sysprop",
58 blueprint.RuleParams{
Inseob Kimc0907f12019-02-08 21:00:45 +090059 Command: "$syspropCmd --header-dir=$headerOutDir --system-header-dir=$systemOutDir " +
60 "--source-dir=$srcOutDir --include-name=$includeName $in",
Inseob Kim21f26902018-09-06 00:55:20 +090061 CommandDeps: []string{"$syspropCmd"},
62 },
Inseob Kimc0907f12019-02-08 21:00:45 +090063 "headerOutDir", "systemOutDir", "srcOutDir", "includeName")
Inseob Kim21f26902018-09-06 00:55:20 +090064
Dan Willemsen4f1c3d42017-09-09 01:15:26 -070065 windmc = pctx.AndroidStaticRule("windmc",
66 blueprint.RuleParams{
67 Command: "$windmcCmd -r$$(dirname $out) -h$$(dirname $out) $in",
68 CommandDeps: []string{"$windmcCmd"},
69 },
70 "windmcCmd")
Colin Cross581c1892015-04-07 16:50:10 -070071)
72
Colin Cross635c3b02016-05-18 15:37:25 -070073func genYacc(ctx android.ModuleContext, yaccFile android.Path, outFile android.ModuleGenPath, yaccFlags string) (headerFile android.ModuleGenPath) {
Dan Willemsen21ec4902016-11-02 20:43:13 -070074 headerFile = android.GenPathWithExt(ctx, "yacc", yaccFile, "h")
Colin Cross581c1892015-04-07 16:50:10 -070075
Colin Crossae887032017-10-23 17:16:14 -070076 ctx.Build(pctx, android.BuildParams{
Dan Willemsen9f3c5742016-11-03 14:28:31 -070077 Rule: yacc,
Colin Cross67a5c132017-05-09 13:45:28 -070078 Description: "yacc " + yaccFile.Rel(),
Dan Willemsen9f3c5742016-11-03 14:28:31 -070079 Output: outFile,
80 ImplicitOutput: headerFile,
81 Input: yaccFile,
Colin Cross581c1892015-04-07 16:50:10 -070082 Args: map[string]string{
83 "yaccFlags": yaccFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -070084 "hFile": headerFile.String(),
Colin Cross581c1892015-04-07 16:50:10 -070085 },
86 })
87
Dan Willemsenf0c73e02016-03-01 15:15:26 -080088 return headerFile
Colin Cross581c1892015-04-07 16:50:10 -070089}
90
Dan Willemsene1240db2016-11-03 14:28:51 -070091func genAidl(ctx android.ModuleContext, aidlFile android.Path, outFile android.ModuleGenPath, aidlFlags string) android.Paths {
Colin Crossae887032017-10-23 17:16:14 -070092 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -070093 Rule: aidl,
94 Description: "aidl " + aidlFile.Rel(),
95 Output: outFile,
96 Input: aidlFile,
Dan Willemsene1240db2016-11-03 14:28:51 -070097 Args: map[string]string{
98 "aidlFlags": aidlFlags,
99 "outDir": android.PathForModuleGen(ctx, "aidl").String(),
100 },
101 })
102
103 // TODO: This should return the generated headers, not the source file.
104 return android.Paths{outFile}
105}
106
Colin Cross635c3b02016-05-18 15:37:25 -0700107func genLex(ctx android.ModuleContext, lexFile android.Path, outFile android.ModuleGenPath) {
Colin Crossae887032017-10-23 17:16:14 -0700108 ctx.Build(pctx, android.BuildParams{
Colin Cross67a5c132017-05-09 13:45:28 -0700109 Rule: lex,
110 Description: "lex " + lexFile.Rel(),
111 Output: outFile,
112 Input: lexFile,
Colin Cross581c1892015-04-07 16:50:10 -0700113 })
Colin Cross581c1892015-04-07 16:50:10 -0700114}
115
Inseob Kim21f26902018-09-06 00:55:20 +0900116func genSysprop(ctx android.ModuleContext, syspropFile android.Path) (android.Path, android.Path) {
117 headerFile := android.PathForModuleGen(ctx, "sysprop", "include", syspropFile.Rel()+".h")
Inseob Kimc0907f12019-02-08 21:00:45 +0900118 systemHeaderFile := android.PathForModuleGen(ctx, "sysprop/system", "include", syspropFile.Rel()+".h")
Inseob Kim21f26902018-09-06 00:55:20 +0900119 cppFile := android.PathForModuleGen(ctx, "sysprop", syspropFile.Rel()+".cpp")
120
121 ctx.Build(pctx, android.BuildParams{
122 Rule: sysprop,
123 Description: "sysprop " + syspropFile.Rel(),
124 Output: cppFile,
125 ImplicitOutput: headerFile,
126 Input: syspropFile,
127 Args: map[string]string{
128 "headerOutDir": filepath.Dir(headerFile.String()),
Inseob Kimc0907f12019-02-08 21:00:45 +0900129 "systemOutDir": filepath.Dir(systemHeaderFile.String()),
Inseob Kim21f26902018-09-06 00:55:20 +0900130 "srcOutDir": filepath.Dir(cppFile.String()),
131 "includeName": syspropFile.Rel() + ".h",
132 },
133 })
134
135 return cppFile, headerFile
136}
137
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700138func genWinMsg(ctx android.ModuleContext, srcFile android.Path, flags builderFlags) (android.Path, android.Path) {
139 headerFile := android.GenPathWithExt(ctx, "windmc", srcFile, "h")
140 rcFile := android.GenPathWithExt(ctx, "windmc", srcFile, "rc")
141
142 windmcCmd := gccCmd(flags.toolchain, "windmc")
143
Colin Crossae887032017-10-23 17:16:14 -0700144 ctx.Build(pctx, android.BuildParams{
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700145 Rule: windmc,
146 Description: "windmc " + srcFile.Rel(),
147 Output: rcFile,
148 ImplicitOutput: headerFile,
149 Input: srcFile,
150 Args: map[string]string{
151 "windmcCmd": windmcCmd,
152 },
153 })
154
155 return rcFile, headerFile
156}
157
Colin Cross635c3b02016-05-18 15:37:25 -0700158func genSources(ctx android.ModuleContext, srcFiles android.Paths,
159 buildFlags builderFlags) (android.Paths, android.Paths) {
Colin Cross581c1892015-04-07 16:50:10 -0700160
Colin Cross635c3b02016-05-18 15:37:25 -0700161 var deps android.Paths
Colin Cross581c1892015-04-07 16:50:10 -0700162
Colin Cross2a252be2017-05-01 17:37:24 -0700163 var rsFiles android.Paths
164
Colin Cross581c1892015-04-07 16:50:10 -0700165 for i, srcFile := range srcFiles {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700166 switch srcFile.Ext() {
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800167 case ".y":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700168 cFile := android.GenPathWithExt(ctx, "yacc", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800169 srcFiles[i] = cFile
170 deps = append(deps, genYacc(ctx, srcFile, cFile, buildFlags.yaccFlags))
171 case ".yy":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700172 cppFile := android.GenPathWithExt(ctx, "yacc", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700173 srcFiles[i] = cppFile
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800174 deps = append(deps, genYacc(ctx, srcFile, cppFile, buildFlags.yaccFlags))
175 case ".l":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700176 cFile := android.GenPathWithExt(ctx, "lex", srcFile, "c")
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800177 srcFiles[i] = cFile
178 genLex(ctx, srcFile, cFile)
179 case ".ll":
Dan Willemsen21ec4902016-11-02 20:43:13 -0700180 cppFile := android.GenPathWithExt(ctx, "lex", srcFile, "cpp")
Colin Cross581c1892015-04-07 16:50:10 -0700181 srcFiles[i] = cppFile
Dan Willemsenf0c73e02016-03-01 15:15:26 -0800182 genLex(ctx, srcFile, cppFile)
Colin Cross0c461f12016-10-20 16:11:43 -0700183 case ".proto":
Dan Willemsen60e62f02018-11-16 21:05:32 -0800184 ccFile, headerFile := genProto(ctx, srcFile, buildFlags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700185 srcFiles[i] = ccFile
186 deps = append(deps, headerFile)
Dan Willemsene1240db2016-11-03 14:28:51 -0700187 case ".aidl":
188 cppFile := android.GenPathWithExt(ctx, "aidl", srcFile, "cpp")
189 srcFiles[i] = cppFile
190 deps = append(deps, genAidl(ctx, srcFile, cppFile, buildFlags.aidlFlags)...)
Colin Cross2a252be2017-05-01 17:37:24 -0700191 case ".rs", ".fs":
192 cppFile := rsGeneratedCppFile(ctx, srcFile)
193 rsFiles = append(rsFiles, srcFiles[i])
194 srcFiles[i] = cppFile
Dan Willemsen4f1c3d42017-09-09 01:15:26 -0700195 case ".mc":
196 rcFile, headerFile := genWinMsg(ctx, srcFile, buildFlags)
197 srcFiles[i] = rcFile
198 deps = append(deps, headerFile)
Inseob Kim21f26902018-09-06 00:55:20 +0900199 case ".sysprop":
200 cppFile, headerFile := genSysprop(ctx, srcFile)
201 srcFiles[i] = cppFile
202 deps = append(deps, headerFile)
Colin Cross581c1892015-04-07 16:50:10 -0700203 }
204 }
205
Colin Cross2a252be2017-05-01 17:37:24 -0700206 if len(rsFiles) > 0 {
207 deps = append(deps, rsGenerateCpp(ctx, rsFiles, buildFlags.rsFlags)...)
208 }
209
Colin Cross581c1892015-04-07 16:50:10 -0700210 return srcFiles, deps
211}