Home | History | Annotate | Download | only in build
      1 // Copyright (C) 2018 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 
     15 package vintf
     16 
     17 import (
     18 	"fmt"
     19 	"io"
     20 	"strings"
     21 
     22 	"github.com/google/blueprint"
     23 	"github.com/google/blueprint/proptools"
     24 
     25 	"android/soong/android"
     26 	"android/soong/kernel/configs"
     27 )
     28 
     29 type dependencyTag struct {
     30 	blueprint.BaseDependencyTag
     31 	name string
     32 }
     33 
     34 var (
     35 	pctx = android.NewPackageContext("android/vintf")
     36 
     37 	assembleVintfRule = pctx.AndroidStaticRule("assemble_vintf", blueprint.RuleParams{
     38 		Command:     `${assembleVintfCmd} -i ${inputs} -o ${out}`,
     39 		CommandDeps: []string{"${assembleVintfCmd}"},
     40 		Description: "assemble_vintf -i ${inputs}",
     41 	}, "inputs")
     42 
     43 	kernelConfigTag = dependencyTag{name: "kernel-config"}
     44 )
     45 
     46 const (
     47 	relpath = "vintf"
     48 )
     49 
     50 type vintfCompatibilityMatrixProperties struct {
     51 	// set the name of the output
     52 	Stem *string
     53 
     54 	// list of source compatibility matrix XML files
     55 	Srcs []string
     56 
     57 	// list of kernel_config modules to be combined to final output
     58 	Kernel_configs []string
     59 }
     60 
     61 type vintfCompatibilityMatrixRule struct {
     62 	android.ModuleBase
     63 	properties vintfCompatibilityMatrixProperties
     64 
     65 	genFile android.WritablePath
     66 }
     67 
     68 func init() {
     69 	pctx.HostBinToolVariable("assembleVintfCmd", "assemble_vintf")
     70 	android.RegisterModuleType("vintf_compatibility_matrix", vintfCompatibilityMatrixFactory)
     71 }
     72 
     73 func vintfCompatibilityMatrixFactory() android.Module {
     74 	g := &vintfCompatibilityMatrixRule{}
     75 	g.AddProperties(&g.properties)
     76 	android.InitAndroidArchModule(g, android.DeviceSupported, android.MultilibCommon)
     77 	return g
     78 }
     79 
     80 var _ android.AndroidMkDataProvider = (*vintfCompatibilityMatrixRule)(nil)
     81 
     82 func (g *vintfCompatibilityMatrixRule) DepsMutator(ctx android.BottomUpMutatorContext) {
     83 	android.ExtractSourcesDeps(ctx, g.properties.Srcs)
     84 	ctx.AddDependency(ctx.Module(), kernelConfigTag, g.properties.Kernel_configs...)
     85 }
     86 
     87 func (g *vintfCompatibilityMatrixRule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
     88 
     89 	outputFilename := proptools.String(g.properties.Stem)
     90 	if outputFilename == "" {
     91 		outputFilename = g.Name()
     92 	}
     93 
     94 	inputPaths := android.PathsForModuleSrc(ctx, g.properties.Srcs)
     95 	ctx.VisitDirectDepsWithTag(kernelConfigTag, func(m android.Module) {
     96 		if k, ok := m.(*configs.KernelConfigRule); ok {
     97 			inputPaths = append(inputPaths, k.OutputPath())
     98 		} else {
     99 			ctx.PropertyErrorf("kernel_config",
    100 				"module %q is not a kernel_config", ctx.OtherModuleName(m))
    101 		}
    102 	})
    103 
    104 	g.genFile = android.PathForModuleGen(ctx, outputFilename)
    105 
    106 	ctx.Build(pctx, android.BuildParams{
    107 		Rule:        assembleVintfRule,
    108 		Description: "Framework Compatibility Matrix",
    109 		Implicits:   inputPaths,
    110 		Output:      g.genFile,
    111 		Args: map[string]string{
    112 			"inputs": strings.Join(inputPaths.Strings(), ":"),
    113 		},
    114 	})
    115 
    116 	ctx.InstallFile(android.PathForModuleInstall(ctx, "etc", relpath), outputFilename, g.genFile)
    117 }
    118 
    119 func (g *vintfCompatibilityMatrixRule) AndroidMk() android.AndroidMkData {
    120 	return android.AndroidMkData{
    121 		Class:      "ETC",
    122 		OutputFile: android.OptionalPathForPath(g.genFile),
    123 		Extra: []android.AndroidMkExtraFunc{
    124 			func(w io.Writer, outputFile android.Path) {
    125 				fmt.Fprintln(w, "LOCAL_MODULE_RELATIVE_PATH :=", relpath)
    126 				if proptools.String(g.properties.Stem) != "" {
    127 					fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", proptools.String(g.properties.Stem))
    128 				}
    129 			},
    130 		},
    131 	}
    132 }
    133