| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 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 python | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "android/soong/android" | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 19 | ) | 
|  | 20 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 21 | func genProto(ctx android.ModuleContext, protoFile android.Path, flags android.ProtoFlags, pkgPath string) android.Path { | 
|  | 22 | srcsZipFile := android.PathForModuleGen(ctx, protoFile.Base()+".srcszip") | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 23 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 24 | outDir := srcsZipFile.ReplaceExtension(ctx, "tmp") | 
|  | 25 | depFile := srcsZipFile.ReplaceExtension(ctx, "srcszip.d") | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 26 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 27 | rule := android.NewRuleBuilder() | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 28 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 29 | rule.Command().Text("rm -rf").Flag(outDir.String()) | 
|  | 30 | rule.Command().Text("mkdir -p").Flag(outDir.String()) | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 31 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 32 | android.ProtoRule(ctx, rule, protoFile, flags, flags.Deps, outDir, depFile, nil) | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 33 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 34 | // Proto generated python files have an unknown package name in the path, so package the entire output directory | 
|  | 35 | // into a srcszip. | 
|  | 36 | zipCmd := rule.Command(). | 
| Colin Cross | ee94d6a | 2019-07-08 17:08:34 -0700 | [diff] [blame] | 37 | BuiltTool(ctx, "soong_zip"). | 
| Colin Cross | 09364fd | 2019-04-08 13:00:40 -0700 | [diff] [blame] | 38 | FlagWithOutput("-o ", srcsZipFile) | 
| Nan Zhang | f0c4e43 | 2018-05-22 14:50:18 -0700 | [diff] [blame] | 39 | if pkgPath != "" { | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 40 | zipCmd.FlagWithArg("-P ", pkgPath) | 
| Nan Zhang | f0c4e43 | 2018-05-22 14:50:18 -0700 | [diff] [blame] | 41 | } | 
| Colin Cross | 09364fd | 2019-04-08 13:00:40 -0700 | [diff] [blame] | 42 | zipCmd.FlagWithArg("-C ", outDir.String()). | 
|  | 43 | FlagWithArg("-D ", outDir.String()) | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 44 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 45 | rule.Command().Text("rm -rf").Flag(outDir.String()) | 
|  | 46 |  | 
|  | 47 | rule.Build(pctx, ctx, "protoc_"+protoFile.Rel(), "protoc "+protoFile.Rel()) | 
|  | 48 |  | 
|  | 49 | return srcsZipFile | 
| Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 50 | } |