blob: 53ebb5895018ab22ec46bf9004658e1074c3cbfc [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
Colin Cross19878da2019-03-28 14:45:07 -070021func genProto(ctx android.ModuleContext, protoFile android.Path, flags android.ProtoFlags, pkgPath string) android.Path {
22 srcsZipFile := android.PathForModuleGen(ctx, protoFile.Base()+".srcszip")
Nan Zhangb8fa1972017-12-22 16:12:00 -080023
Colin Cross19878da2019-03-28 14:45:07 -070024 outDir := srcsZipFile.ReplaceExtension(ctx, "tmp")
25 depFile := srcsZipFile.ReplaceExtension(ctx, "srcszip.d")
Nan Zhangb8fa1972017-12-22 16:12:00 -080026
Colin Crossf1a035e2020-11-16 17:32:30 -080027 rule := android.NewRuleBuilder(pctx, ctx)
Nan Zhangb8fa1972017-12-22 16:12:00 -080028
Colin Cross19878da2019-03-28 14:45:07 -070029 rule.Command().Text("rm -rf").Flag(outDir.String())
30 rule.Command().Text("mkdir -p").Flag(outDir.String())
Nan Zhangb8fa1972017-12-22 16:12:00 -080031
Colin Crossf1a035e2020-11-16 17:32:30 -080032 android.ProtoRule(rule, protoFile, flags, flags.Deps, outDir, depFile, nil)
Nan Zhangb8fa1972017-12-22 16:12:00 -080033
Colin Cross19878da2019-03-28 14:45:07 -070034 // 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 Crossf1a035e2020-11-16 17:32:30 -080037 BuiltTool("soong_zip").
Colin Cross09364fd2019-04-08 13:00:40 -070038 FlagWithOutput("-o ", srcsZipFile)
Nan Zhangf0c4e432018-05-22 14:50:18 -070039 if pkgPath != "" {
Colin Cross19878da2019-03-28 14:45:07 -070040 zipCmd.FlagWithArg("-P ", pkgPath)
Nan Zhangf0c4e432018-05-22 14:50:18 -070041 }
Colin Cross09364fd2019-04-08 13:00:40 -070042 zipCmd.FlagWithArg("-C ", outDir.String()).
43 FlagWithArg("-D ", outDir.String())
Nan Zhangb8fa1972017-12-22 16:12:00 -080044
Colin Cross19878da2019-03-28 14:45:07 -070045 rule.Command().Text("rm -rf").Flag(outDir.String())
46
Colin Crossf1a035e2020-11-16 17:32:30 -080047 rule.Build("protoc_"+protoFile.Rel(), "protoc "+protoFile.Rel())
Colin Cross19878da2019-03-28 14:45:07 -070048
49 return srcsZipFile
Nan Zhangb8fa1972017-12-22 16:12:00 -080050}