xref: /aosp_15_r20/external/spdx-tools/builder/build.go (revision ba677afa8f67bb56cbc794f4d0e378e0da058e16)
1// Package builder is used to create tools-golang data structures for a given
2// directory path's contents, with hashes, etc. filled in and with empty
3// license data.
4// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
5package builder
6
7import (
8	"fmt"
9
10	"github.com/spdx/tools-golang/builder/builder2v1"
11	"github.com/spdx/tools-golang/builder/builder2v2"
12	"github.com/spdx/tools-golang/builder/builder2v3"
13	"github.com/spdx/tools-golang/spdx/common"
14	"github.com/spdx/tools-golang/spdx/v2_1"
15	"github.com/spdx/tools-golang/spdx/v2_2"
16	"github.com/spdx/tools-golang/spdx/v2_3"
17)
18
19// ===== 2.1 builder =====
20
21// Config2_1 is a collection of configuration settings for builder
22// (for version 2.1 SPDX Documents). A few mandatory fields are set here
23// so that they can be repeatedly reused in multiple calls to Build2_1.
24type Config2_1 struct {
25	// NamespacePrefix should be a URI representing a prefix for the
26	// namespace with which the SPDX Document will be associated.
27	// It will be used in the DocumentNamespace field in the CreationInfo
28	// section, followed by the per-Document package name and a random UUID.
29	NamespacePrefix string
30
31	// CreatorType should be one of "Person", "Organization" or "Tool".
32	// If not one of those strings, it will be interpreted as "Person".
33	CreatorType string
34
35	// Creator will be filled in for the given CreatorType.
36	Creator string
37
38	// PathsIgnored lists certain paths to be omitted from the built document.
39	// Each string should be a path, relative to the package's dirRoot,
40	// to a specific file or (for all files in a directory) ending in a slash.
41	// Prefix the string with "**" to omit all instances of that file /
42	// directory, regardless of where it is in the file tree.
43	PathsIgnored []string
44
45	// TestValues is used to pass fixed values for testing purposes
46	// only, and should be set to nil for production use. It is only
47	// exported so that it will be accessible within builder2v1.
48	TestValues map[string]string
49}
50
51// Build2_1 creates an SPDX Document (version 2.1), returning that document or
52// error if any is encountered. Arguments:
53//   - packageName: name of package / directory
54//   - dirRoot: path to directory to be analyzed
55//   - config: Config object
56func Build2_1(packageName string, dirRoot string, config *Config2_1) (*v2_1.Document, error) {
57	// build Package section first -- will include Files and make the
58	// package verification code available
59	pkg, err := builder2v1.BuildPackageSection2_1(packageName, dirRoot, config.PathsIgnored)
60	if err != nil {
61		return nil, err
62	}
63
64	ci, err := builder2v1.BuildCreationInfoSection2_1(config.CreatorType, config.Creator, config.TestValues)
65	if err != nil {
66		return nil, err
67	}
68
69	rln, err := builder2v1.BuildRelationshipSection2_1(packageName)
70	if err != nil {
71		return nil, err
72	}
73
74	doc := &v2_1.Document{
75		SPDXVersion:       "SPDX-2.1",
76		DataLicense:       "CC0-1.0",
77		SPDXIdentifier:    common.ElementID("DOCUMENT"),
78		DocumentName:      packageName,
79		DocumentNamespace: fmt.Sprintf("%s%s-%s", config.NamespacePrefix, packageName, pkg.PackageVerificationCode),
80		CreationInfo:      ci,
81		Packages:          []*v2_1.Package{pkg},
82		Relationships:     []*v2_1.Relationship{rln},
83	}
84
85	return doc, nil
86}
87
88// ===== 2.2 builder =====
89
90// Config2_2 is a collection of configuration settings for builder
91// (for version 2.2 SPDX Documents). A few mandatory fields are set here
92// so that they can be repeatedly reused in multiple calls to Build2_2.
93type Config2_2 struct {
94	// NamespacePrefix should be a URI representing a prefix for the
95	// namespace with which the SPDX Document will be associated.
96	// It will be used in the DocumentNamespace field in the CreationInfo
97	// section, followed by the per-Document package name and a random UUID.
98	NamespacePrefix string
99
100	// CreatorType should be one of "Person", "Organization" or "Tool".
101	// If not one of those strings, it will be interpreted as "Person".
102	CreatorType string
103
104	// Creator will be filled in for the given CreatorType.
105	Creator string
106
107	// PathsIgnored lists certain paths to be omitted from the built document.
108	// Each string should be a path, relative to the package's dirRoot,
109	// to a specific file or (for all files in a directory) ending in a slash.
110	// Prefix the string with "**" to omit all instances of that file /
111	// directory, regardless of where it is in the file tree.
112	PathsIgnored []string
113
114	// TestValues is used to pass fixed values for testing purposes
115	// only, and should be set to nil for production use. It is only
116	// exported so that it will be accessible within builder2v2.
117	TestValues map[string]string
118}
119
120// Build2_2 creates an SPDX Document (version 2.2), returning that document or
121// error if any is encountered. Arguments:
122//   - packageName: name of package / directory
123//   - dirRoot: path to directory to be analyzed
124//   - config: Config object
125func Build2_2(packageName string, dirRoot string, config *Config2_2) (*v2_2.Document, error) {
126	// build Package section first -- will include Files and make the
127	// package verification code available
128	pkg, err := builder2v2.BuildPackageSection2_2(packageName, dirRoot, config.PathsIgnored)
129	if err != nil {
130		return nil, err
131	}
132
133	ci, err := builder2v2.BuildCreationInfoSection2_2(config.CreatorType, config.Creator, config.TestValues)
134	if err != nil {
135		return nil, err
136	}
137
138	rln, err := builder2v2.BuildRelationshipSection2_2(packageName)
139	if err != nil {
140		return nil, err
141	}
142
143	doc := &v2_2.Document{
144		SPDXVersion:       "SPDX-2.2",
145		DataLicense:       "CC0-1.0",
146		SPDXIdentifier:    common.ElementID("DOCUMENT"),
147		DocumentName:      packageName,
148		DocumentNamespace: fmt.Sprintf("%s%s-%s", config.NamespacePrefix, packageName, pkg.PackageVerificationCode),
149		CreationInfo:      ci,
150		Packages:          []*v2_2.Package{pkg},
151		Relationships:     []*v2_2.Relationship{rln},
152	}
153
154	return doc, nil
155}
156
157// ===== 2.3 builder =====
158
159// Config2_3 is a collection of configuration settings for builder
160// (for version 2.3 SPDX Documents). A few mandatory fields are set here
161// so that they can be repeatedly reused in multiple calls to Build2_3.
162type Config2_3 struct {
163	// NamespacePrefix should be a URI representing a prefix for the
164	// namespace with which the SPDX Document will be associated.
165	// It will be used in the DocumentNamespace field in the CreationInfo
166	// section, followed by the per-Document package name and a random UUID.
167	NamespacePrefix string
168
169	// CreatorType should be one of "Person", "Organization" or "Tool".
170	// If not one of those strings, it will be interpreted as "Person".
171	CreatorType string
172
173	// Creator will be filled in for the given CreatorType.
174	Creator string
175
176	// PathsIgnored lists certain paths to be omitted from the built document.
177	// Each string should be a path, relative to the package's dirRoot,
178	// to a specific file or (for all files in a directory) ending in a slash.
179	// Prefix the string with "**" to omit all instances of that file /
180	// directory, regardless of where it is in the file tree.
181	PathsIgnored []string
182
183	// TestValues is used to pass fixed values for testing purposes
184	// only, and should be set to nil for production use. It is only
185	// exported so that it will be accessible within builder2v3.
186	TestValues map[string]string
187}
188
189// Build2_3 creates an SPDX Document (version 2.3), returning that document or
190// error if any is encountered. Arguments:
191//   - packageName: name of package / directory
192//   - dirRoot: path to directory to be analyzed
193//   - config: Config object
194func Build2_3(packageName string, dirRoot string, config *Config2_3) (*v2_3.Document, error) {
195	// build Package section first -- will include Files and make the
196	// package verification code available
197	pkg, err := builder2v3.BuildPackageSection2_3(packageName, dirRoot, config.PathsIgnored)
198	if err != nil {
199		return nil, err
200	}
201
202	ci, err := builder2v3.BuildCreationInfoSection2_3(config.CreatorType, config.Creator, config.TestValues)
203	if err != nil {
204		return nil, err
205	}
206
207	rln, err := builder2v3.BuildRelationshipSection2_3(packageName)
208	if err != nil {
209		return nil, err
210	}
211
212	doc := &v2_3.Document{
213		SPDXVersion:       "SPDX-2.3",
214		DataLicense:       "CC0-1.0",
215		SPDXIdentifier:    common.ElementID("DOCUMENT"),
216		DocumentName:      packageName,
217		DocumentNamespace: fmt.Sprintf("%s%s-%s", config.NamespacePrefix, packageName, pkg.PackageVerificationCode),
218		CreationInfo:      ci,
219		Packages:          []*v2_3.Package{pkg},
220		Relationships:     []*v2_3.Relationship{rln},
221	}
222
223	return doc, nil
224}
225