1// Copyright (C) 2021 The Android Open Source Project
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 aidl
16
17import (
18	"android/soong/android"
19
20	"fmt"
21	"io"
22
23	"github.com/google/blueprint"
24)
25
26var (
27	aidlDumpMappingsRule = pctx.StaticRule("aidlDumpMappingsRule", blueprint.RuleParams{
28		Command: `rm -rf "${outDir}" && mkdir -p "${outDir}" && ` +
29			`${aidlCmd} --apimapping ${outDir}/intermediate.txt ${in} ${imports} && ` +
30			`${aidlToJniCmd} ${outDir}/intermediate.txt ${out}`,
31		CommandDeps: []string{"${aidlCmd}"},
32	}, "imports", "outDir")
33)
34
35func init() {
36	android.RegisterModuleType("aidl_mapping", aidlMappingFactory)
37}
38
39type aidlMappingProperties struct {
40	// Source file of this prebuilt.
41	Srcs   []string `android:"path"`
42	Output string
43}
44
45type aidlMapping struct {
46	android.ModuleBase
47	properties     aidlMappingProperties
48	outputFilePath android.WritablePath
49}
50
51func (s *aidlMapping) DepsMutator(ctx android.BottomUpMutatorContext) {
52}
53
54func (s *aidlMapping) GenerateAndroidBuildActions(ctx android.ModuleContext) {
55	srcs, imports := getPaths(ctx, s.properties.Srcs, "")
56
57	s.outputFilePath = android.PathForModuleOut(ctx, s.properties.Output)
58	outDir := android.PathForModuleGen(ctx)
59	ctx.Build(pctx, android.BuildParams{
60		Rule:   aidlDumpMappingsRule,
61		Inputs: srcs,
62		Output: s.outputFilePath,
63		Args: map[string]string{
64			"imports": android.JoinWithPrefix(imports, " -I"),
65			"outDir":  outDir.String(),
66		},
67	})
68}
69
70func InitAidlMappingModule(s *aidlMapping) {
71	s.AddProperties(&s.properties)
72}
73
74func aidlMappingFactory() android.Module {
75	module := &aidlMapping{}
76	InitAidlMappingModule(module)
77	android.InitAndroidModule(module)
78	return module
79}
80
81func (m *aidlMapping) AndroidMk() android.AndroidMkData {
82	return android.AndroidMkData{
83		Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
84			android.WriteAndroidMkData(w, data)
85			targetName := m.Name()
86			fmt.Fprintln(w, ".PHONY:", targetName)
87			fmt.Fprintln(w, targetName+":", m.outputFilePath.String())
88		},
89	}
90}
91