1package ca_certificates
2
3import (
4	"path"
5	"path/filepath"
6
7	"github.com/google/blueprint/proptools"
8
9	"android/soong/android"
10	"android/soong/etc"
11	"android/soong/phony"
12)
13
14func init() {
15	android.RegisterModuleType("ca_certificates", caCertificatesFactory)
16	android.RegisterModuleType("ca_certificates_host", caCertificatesHostFactory)
17}
18
19type caCertificatesProperties struct {
20	Src_dir            *string
21	Dest_dir           *string
22	Module_name_prefix *string
23}
24
25func caCertificatesLoadHook(
26	ctx android.LoadHookContext, factory android.ModuleFactory, c *caCertificatesProperties) {
27	// Find all files in src_dir.
28	srcs, err := ctx.GlobWithDeps(path.Join(ctx.ModuleDir(), *c.Src_dir, "*"), nil)
29	if err != nil || len(srcs) == 0 {
30		ctx.PropertyErrorf("src_dir", "cannot find files to install")
31		return
32	}
33
34	// Scan through the found files to create a prebuilt_etc module for each of them.
35	requiredModuleNames := make([]string, len(srcs))
36	for i, src := range srcs {
37		etcProps := struct {
38			Name     *string
39			Src      *string
40			Sub_dir  *string
41			Filename *string
42		}{}
43		filename := filepath.Base(src)
44		moduleName := *c.Module_name_prefix + filename
45		etcProps.Name = proptools.StringPtr(moduleName)
46		etcProps.Src = proptools.StringPtr(path.Join(*c.Src_dir, filename))
47		etcProps.Sub_dir = c.Dest_dir
48		etcProps.Filename = proptools.StringPtr(filename)
49		ctx.CreateModule(factory, &etcProps)
50
51		// Add it to the required module list of the parent phony rule.
52		requiredModuleNames[i] = moduleName
53	}
54
55	phonyProps := struct {
56		Required []string
57	}{}
58	phonyProps.Required = requiredModuleNames
59	ctx.AppendProperties(&phonyProps)
60}
61
62func caCertificatesFactory() android.Module {
63	p := phony.PhonyFactory()
64	c := &caCertificatesProperties{}
65	android.AddLoadHook(p, func(ctx android.LoadHookContext) {
66		caCertificatesLoadHook(ctx, etc.PrebuiltEtcFactory, c)
67	})
68	p.AddProperties(c)
69
70	return p
71}
72
73func caCertificatesHostFactory() android.Module {
74	p := phony.PhonyFactory()
75	c := &caCertificatesProperties{}
76	android.AddLoadHook(p, func(ctx android.LoadHookContext) {
77		caCertificatesLoadHook(ctx, etc.PrebuiltEtcHostFactory, c)
78	})
79	p.AddProperties(c)
80
81	return p
82}
83