blob: dab012d043abf2d2fe7a3c3d5a3adaa6a5121ef9 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// 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
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross70b40592015-03-23 12:57:34 -070018 "github.com/google/blueprint"
Colin Crossc20dc852020-11-10 12:27:45 -080019 "github.com/google/blueprint/bootstrap"
Colin Cross3f40fa42015-01-30 17:27:36 -080020)
21
22var (
Sam Delmerico46d08b42022-11-15 15:51:04 -050023 pctx = NewPackageContext("android/soong/android")
24 exportedVars = NewExportedVariables(pctx)
Colin Cross3f40fa42015-01-30 17:27:36 -080025
26 cpPreserveSymlinks = pctx.VariableConfigMethod("cpPreserveSymlinks",
27 Config.CpPreserveSymlinksFlags)
28
Colin Cross3f40fa42015-01-30 17:27:36 -080029 // A phony rule that is not the built-in Ninja phony rule. The built-in
30 // phony rule has special behavior that is sometimes not desired. See the
31 // Ninja docs for more details.
Colin Cross9d45bb72016-08-29 16:14:13 -070032 Phony = pctx.AndroidStaticRule("Phony",
Colin Cross3f40fa42015-01-30 17:27:36 -080033 blueprint.RuleParams{
34 Command: "# phony $out",
35 Description: "phony $out",
36 })
37
38 // GeneratedFile is a rule for indicating that a given file was generated
39 // while running soong. This allows the file to be cleaned up if it ever
40 // stops being generated by soong.
Colin Cross9d45bb72016-08-29 16:14:13 -070041 GeneratedFile = pctx.AndroidStaticRule("GeneratedFile",
Colin Cross3f40fa42015-01-30 17:27:36 -080042 blueprint.RuleParams{
43 Command: "# generated $out",
44 Description: "generated $out",
45 Generator: true,
46 })
47
48 // A copy rule.
Colin Cross9d45bb72016-08-29 16:14:13 -070049 Cp = pctx.AndroidStaticRule("Cp",
Colin Cross3f40fa42015-01-30 17:27:36 -080050 blueprint.RuleParams{
Colin Cross50ed1f92021-11-12 17:41:02 -080051 Command: "rm -f $out && cp $cpPreserveSymlinks $cpFlags $in $out$extraCmds",
Colin Cross3f40fa42015-01-30 17:27:36 -080052 Description: "cp $out",
53 },
Colin Cross50ed1f92021-11-12 17:41:02 -080054 "cpFlags", "extraCmds")
Colin Cross3f40fa42015-01-30 17:27:36 -080055
Sam Delmerico4ed95e22023-02-03 18:12:15 -050056 // A copy rule that doesn't preserve symlinks.
57 CpNoPreserveSymlink = pctx.AndroidStaticRule("CpNoPreserveSymlink",
58 blueprint.RuleParams{
59 Command: "rm -f $out && cp $cpFlags $in $out$extraCmds",
60 Description: "cp $out",
61 },
62 "cpFlags", "extraCmds")
63
Colin Cross00d93b12021-03-04 10:00:09 -080064 // A copy rule that only updates the output if it changed.
65 CpIfChanged = pctx.AndroidStaticRule("CpIfChanged",
66 blueprint.RuleParams{
67 Command: "if ! cmp -s $in $out; then cp $in $out; fi",
68 Description: "cp if changed $out",
69 Restat: true,
Colin Cross31a67452023-11-02 16:57:08 -070070 })
Colin Cross00d93b12021-03-04 10:00:09 -080071
Colin Cross5c517922017-08-31 12:29:17 -070072 CpExecutable = pctx.AndroidStaticRule("CpExecutable",
73 blueprint.RuleParams{
Chih-Hung Hsieh1048a732022-08-10 20:51:37 -070074 Command: "rm -f $out && cp $cpFlags $in $out && chmod +x $out$extraCmds",
Colin Cross5c517922017-08-31 12:29:17 -070075 Description: "cp $out",
76 },
Colin Cross50ed1f92021-11-12 17:41:02 -080077 "cpFlags", "extraCmds")
Colin Cross5c517922017-08-31 12:29:17 -070078
Dan Albert5d723ab2016-07-18 22:29:52 -070079 // A timestamp touch rule.
Colin Cross9d45bb72016-08-29 16:14:13 -070080 Touch = pctx.AndroidStaticRule("Touch",
Dan Albert5d723ab2016-07-18 22:29:52 -070081 blueprint.RuleParams{
82 Command: "touch $out",
83 Description: "touch $out",
84 })
85
Colin Cross3f40fa42015-01-30 17:27:36 -080086 // A symlink rule.
Colin Cross9d45bb72016-08-29 16:14:13 -070087 Symlink = pctx.AndroidStaticRule("Symlink",
Colin Cross3f40fa42015-01-30 17:27:36 -080088 blueprint.RuleParams{
Cole Faust9a346f62024-01-18 20:12:02 +000089 Command: "rm -f $out && ln -f -s $fromPath $out",
90 Description: "symlink $out",
Colin Cross3f40fa42015-01-30 17:27:36 -080091 },
92 "fromPath")
Colin Cross6ff51382015-12-17 16:39:19 -080093
Colin Cross9d45bb72016-08-29 16:14:13 -070094 ErrorRule = pctx.AndroidStaticRule("Error",
Colin Cross6ff51382015-12-17 16:39:19 -080095 blueprint.RuleParams{
96 Command: `echo "$error" && false`,
97 Description: "error building $out",
98 },
99 "error")
Colin Cross9d45bb72016-08-29 16:14:13 -0700100
Dan Albertc6345fb2016-10-20 01:36:11 -0700101 Cat = pctx.AndroidStaticRule("Cat",
102 blueprint.RuleParams{
Cole Faust20f20302023-08-31 11:00:25 -0700103 Command: "rm -f $out && cat $in > $out",
104 Description: "concatenate files to $out",
Dan Albertc6345fb2016-10-20 01:36:11 -0700105 })
106
Nan Zhang27005112017-05-12 14:02:13 -0700107 // ubuntu 14.04 offcially use dash for /bin/sh, and its builtin echo command
108 // doesn't support -e option. Therefore we force to use /bin/bash when writing out
109 // content to file.
Colin Crosscf371cc2020-11-13 11:48:42 -0800110 writeFile = pctx.AndroidStaticRule("writeFile",
Dan Albert30c9d6e2017-03-28 14:54:55 -0700111 blueprint.RuleParams{
Cole Faust20f20302023-08-31 11:00:25 -0700112 Command: `rm -f $out && /bin/bash -c 'echo -e -n "$$0" > $out' $content`,
Dan Albert30c9d6e2017-03-28 14:54:55 -0700113 Description: "writing file $out",
114 },
115 "content")
116
Colin Cross9d45bb72016-08-29 16:14:13 -0700117 // Used only when USE_GOMA=true is set, to restrict non-goma jobs to the local parallelism value
118 localPool = blueprint.NewBuiltinPool("local_pool")
Colin Cross8b8bec32019-11-15 13:18:43 -0800119
Ramy Medhat944839a2020-03-31 22:14:52 -0400120 // Used only by RuleBuilder to identify remoteable rules. Does not actually get created in ninja.
121 remotePool = blueprint.NewBuiltinPool("remote_pool")
122
Colin Cross8b8bec32019-11-15 13:18:43 -0800123 // Used for processes that need significant RAM to ensure there are not too many running in parallel.
124 highmemPool = blueprint.NewBuiltinPool("highmem_pool")
Colin Cross3f40fa42015-01-30 17:27:36 -0800125)
Dan Willemsen24f2f8d2015-07-15 14:34:02 -0700126
127func init() {
128 pctx.Import("github.com/google/blueprint/bootstrap")
Colin Cross77cdcfd2021-03-12 11:28:25 -0800129
130 pctx.VariableFunc("RBEWrapper", func(ctx PackageVarContext) string {
131 return ctx.Config().RBEWrapper()
132 })
Sam Delmerico46d08b42022-11-15 15:51:04 -0500133
134 exportedVars.ExportStringList("NeverAllowNotInIncludeDir", neverallowNotInIncludeDir)
135 exportedVars.ExportStringList("NeverAllowNoUseIncludeDir", neverallowNoUseIncludeDir)
136}
137
Colin Crossc20dc852020-11-10 12:27:45 -0800138// GlobToListFileRule creates a rule that writes a list of files matching a pattern to a file.
139func GlobToListFileRule(ctx ModuleContext, pattern string, excludes []string, file WritablePath) {
140 bootstrap.GlobFile(ctx.blueprintModuleContext(), pattern, excludes, file.String())
141}