blob: ad2b786e2cc685e101dee0cdc4656ad02ff18bc3 [file] [log] [blame]
Nan Zhangb8fa1972017-12-22 16:12:00 -08001// 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 python
16
17import (
18 "android/soong/android"
Nan Zhangb8fa1972017-12-22 16:12:00 -080019)
20
Cole Faustcaf766b2022-10-21 16:07:56 -070021func genProto(ctx android.ModuleContext, protoFile android.Path, flags android.ProtoFlags) android.Path {
Trevor Radcliffed3243d52023-08-15 15:07:22 +000022 // Using protoFile.Base() would generate duplicate source errors in some cases, so we use Rel() instead
23 srcsZipFile := android.PathForModuleGen(ctx, protoFile.Rel()+".srcszip")
Nan Zhangb8fa1972017-12-22 16:12:00 -080024
Colin Cross19878da2019-03-28 14:45:07 -070025 outDir := srcsZipFile.ReplaceExtension(ctx, "tmp")
26 depFile := srcsZipFile.ReplaceExtension(ctx, "srcszip.d")
Nan Zhangb8fa1972017-12-22 16:12:00 -080027
Colin Crossf1a035e2020-11-16 17:32:30 -080028 rule := android.NewRuleBuilder(pctx, ctx)
Nan Zhangb8fa1972017-12-22 16:12:00 -080029
Colin Cross19878da2019-03-28 14:45:07 -070030 rule.Command().Text("rm -rf").Flag(outDir.String())
31 rule.Command().Text("mkdir -p").Flag(outDir.String())
Nan Zhangb8fa1972017-12-22 16:12:00 -080032
Colin Crossf1a035e2020-11-16 17:32:30 -080033 android.ProtoRule(rule, protoFile, flags, flags.Deps, outDir, depFile, nil)
Nan Zhangb8fa1972017-12-22 16:12:00 -080034
Colin Cross19878da2019-03-28 14:45:07 -070035 // Proto generated python files have an unknown package name in the path, so package the entire output directory
36 // into a srcszip.
37 zipCmd := rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -080038 BuiltTool("soong_zip").
Colin Cross09364fd2019-04-08 13:00:40 -070039 FlagWithOutput("-o ", srcsZipFile)
Colin Cross09364fd2019-04-08 13:00:40 -070040 zipCmd.FlagWithArg("-C ", outDir.String()).
41 FlagWithArg("-D ", outDir.String())
Nan Zhangb8fa1972017-12-22 16:12:00 -080042
Colin Cross19878da2019-03-28 14:45:07 -070043 rule.Command().Text("rm -rf").Flag(outDir.String())
44
Colin Crossf1a035e2020-11-16 17:32:30 -080045 rule.Build("protoc_"+protoFile.Rel(), "protoc "+protoFile.Rel())
Colin Cross19878da2019-03-28 14:45:07 -070046
47 return srcsZipFile
Nan Zhangb8fa1972017-12-22 16:12:00 -080048}