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