Skip to content

Commit 4373862

Browse files
authored
refactor embed storage and packaging for bazel (#50)
1 parent f636cfe commit 4373862

File tree

13 files changed

+56
-75
lines changed

13 files changed

+56
-75
lines changed

_tools/libddwaf-updater/update.go

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ var (
3030
currentVersion string
3131
)
3232

33+
const (
34+
goVersionUnsupported = "go1.22"
35+
)
36+
3337
func main() {
3438
force := os.Args[1] == "--force"
3539

@@ -63,21 +67,7 @@ func main() {
6367
wg := sync.WaitGroup{}
6468
wg.Add(len(targets))
6569
for _, tgt := range targets {
66-
embedDir := path.Join(libDir, fmt.Sprintf("%s-%s", tgt.os, tgt.arch))
67-
created := false
68-
if _, err = os.Stat(embedDir); errors.Is(err, os.ErrNotExist) {
69-
if err = os.MkdirAll(embedDir, 0755); err != nil {
70-
panic(err)
71-
}
72-
created = true
73-
}
74-
if created || force {
75-
if err := os.WriteFile(path.Join(embedDir, "vendor.go"), []byte(vendorMarkerFile), 0644); err != nil {
76-
panic(err)
77-
}
78-
createEmbedSource(tgt)
79-
}
80-
go handleTarget(&wg, version, tgt, embedDir, assets)
70+
go handleTarget(&wg, version, tgt, assets, force)
8171
}
8272

8373
wg.Wait()
@@ -98,6 +88,9 @@ func main() {
9888
fmt.Println("All done! Don't forget to check in changes to include/ and internal/lib/, check the libddwaf upgrade guide to update bindings!")
9989
}
10090

91+
// createEmbedSource creates the embed source file for the given target.
92+
// The go:embed directive MUST be in the same package as the target of the directive.
93+
// See bazelbuild/bazel-gazelle#1316 for more details.
10194
func createEmbedSource(tgt target) {
10295
gosource := strings.Join(
10396
[]string{
@@ -106,25 +99,28 @@ func createEmbedSource(tgt target) {
10699
"// This product includes software developed at Datadog (https://www.datadoghq.com/).",
107100
"// Copyright 2016-present Datadog, Inc.",
108101
"",
109-
fmt.Sprintf("//go:build %s && %s && !go1.22", tgt.os, tgt.arch),
102+
tgt.buildConstraintDirective(),
103+
"",
110104
"package lib",
111105
"",
112-
`import _ "embed" // Needed for go:embed`,
106+
"// THIS FILE IS AUTOGENERATED. DO NOT EDIT.",
113107
"",
114-
fmt.Sprintf("//go:embed %s-%s/%s.%s", tgt.os, tgt.arch, tgt.base, tgt.ext),
108+
"import _ \"embed\" // Needed for go:embed",
109+
"",
110+
tgt.embedSourceDirective(),
115111
"var libddwaf []byte",
116112
"",
117-
fmt.Sprintf(`const embedNamePattern = "%s-*.%s"`, tgt.base, tgt.ext),
113+
tgt.tempFilePatternStatement(),
118114
"", // Trailing new line...
119115
},
120116
"\n",
121117
)
122-
if err := os.WriteFile(path.Join(libDir, fmt.Sprintf("lib_%s_%s.go", tgt.os, tgt.arch)), []byte(gosource), 0644); err != nil {
118+
if err := os.WriteFile(path.Join(libDir, tgt.embedSourceFilename()), []byte(gosource), 0644); err != nil {
123119
panic(err)
124120
}
125121
}
126122

127-
func handleTarget(wg *sync.WaitGroup, version string, tgt target, embedDir string, assets map[string]*github.ReleaseAsset) {
123+
func handleTarget(wg *sync.WaitGroup, version string, tgt target, assets map[string]*github.ReleaseAsset, force bool) {
128124
defer wg.Done()
129125

130126
tarName := fmt.Sprintf("libddwaf-%s-%s.tar.gz", version, tgt.assetLabel)
@@ -187,15 +183,14 @@ func handleTarget(wg *sync.WaitGroup, version string, tgt target, embedDir strin
187183
var destPath string
188184
switch name := header.FileInfo().Name(); name {
189185
case "libddwaf.so", "libddwaf.dylib", "ddwaf.dll":
190-
destPath = path.Join(embedDir, name)
186+
destPath = path.Join(libDir, tgt.binaryLibName())
191187
foundLib = true
192188
case "ddwaf.h":
193-
if tgt.primary {
194-
destPath = path.Join(rootDir, "include", name)
195-
foundHdr = true
196-
} else {
189+
if !tgt.primary {
197190
continue
198191
}
192+
destPath = path.Join(rootDir, "include", name)
193+
foundHdr = true
199194
default:
200195
continue
201196
}
@@ -217,11 +212,16 @@ func handleTarget(wg *sync.WaitGroup, version string, tgt target, embedDir strin
217212
}
218213

219214
if !foundLib {
220-
panic(fmt.Errorf("could not find libddwaf.so/libddwaf.dylib in %s", tarUrl))
215+
panic(fmt.Errorf("could not find libddwaf.%s in %s", tgt.ext, tarUrl))
221216
}
222217
if tgt.primary && !foundHdr {
223218
panic(fmt.Errorf("could not find ddwaf.h in %s", tarUrl))
224219
}
220+
221+
// If the embed source file doesn't exist, or if --force is set, create it.
222+
if _, err = os.Stat(path.Join(libDir, tgt.embedSourceFilename())); errors.Is(err, os.ErrNotExist) || force {
223+
createEmbedSource(tgt)
224+
}
225225
}
226226

227227
type target struct {
@@ -273,16 +273,25 @@ var targets = []target{
273273
// {os: "linux", arch: "i386", ext: "so", assetLabel: "i386-linux-musl"},
274274
}
275275

276-
const vendorMarkerFile = `// Unless explicitly stated otherwise all files in this repository are licensed
277-
// under the Apache License Version 2.0.
278-
// This product includes software developed at Datadog (https://www.datadoghq.com/).
279-
// Copyright 2016-present Datadog, Inc.
276+
func (t target) binaryLibName() string {
277+
return fmt.Sprintf("%s-%s-%s.%s", t.base, t.os, t.arch, t.ext)
278+
}
280279

281-
package vendor
280+
func (t target) embedSourceFilename() string {
281+
return fmt.Sprintf("lib_%s_%s.go", t.os, t.arch)
282+
}
282283

283-
// This file is there to make sure "go mod vendor" does not drop those directories, as it otherwise removes
284-
// any directiory that contains no ".go" files.
285-
`
284+
func (t target) buildConstraintDirective() string {
285+
return fmt.Sprintf("//go:build %s && %s && !%s && !datadog.no_waf", t.os, t.arch, goVersionUnsupported)
286+
}
287+
288+
func (t target) tempFilePatternStatement() string {
289+
return fmt.Sprintf("const embedNamePattern = \"%s-*.%s\"", t.base, t.ext)
290+
}
291+
292+
func (t target) embedSourceDirective() string {
293+
return fmt.Sprintf("//go:embed %s", t.binaryLibName())
294+
}
286295

287296
func init() {
288297
_, filename, _, _ := runtime.Caller(0)

internal/lib/darwin-amd64/vendor.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

internal/lib/darwin-arm64/vendor.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

internal/lib/lib_darwin_amd64.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
package lib
99

10+
// THIS FILE IS AUTOGENERATED. DO NOT EDIT.
11+
1012
import _ "embed" // Needed for go:embed
1113

12-
//go:embed darwin-amd64/libddwaf.dylib
14+
//go:embed libddwaf-darwin-amd64.dylib
1315
var libddwaf []byte
1416

1517
const embedNamePattern = "libddwaf-*.dylib"

internal/lib/lib_darwin_arm64.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
package lib
99

10+
// THIS FILE IS AUTOGENERATED. DO NOT EDIT.
11+
1012
import _ "embed" // Needed for go:embed
1113

12-
//go:embed darwin-arm64/libddwaf.dylib
14+
//go:embed libddwaf-darwin-arm64.dylib
1315
var libddwaf []byte
1416

1517
const embedNamePattern = "libddwaf-*.dylib"

internal/lib/lib_linux_amd64.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
package lib
99

10+
// THIS FILE IS AUTOGENERATED. DO NOT EDIT.
11+
1012
import _ "embed" // Needed for go:embed
1113

12-
//go:embed linux-amd64/libddwaf.so
14+
//go:embed libddwaf-linux-amd64.so
1315
var libddwaf []byte
1416

1517
const embedNamePattern = "libddwaf-*.so"

internal/lib/lib_linux_arm64.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
package lib
99

10+
// THIS FILE IS AUTOGENERATED. DO NOT EDIT.
11+
1012
import _ "embed" // Needed for go:embed
1113

12-
//go:embed linux-arm64/libddwaf.so
14+
//go:embed libddwaf-linux-arm64.so
1315
var libddwaf []byte
1416

1517
const embedNamePattern = "libddwaf-*.so"
File renamed without changes.

0 commit comments

Comments
 (0)