Skip to content

Commit 789b5c6

Browse files
aykevldeadprogram
authored andcommitted
compileopts: add library version to cached library path
This may be a bit of a weird place to put the library path, but otherwise it's difficult to get the pathname for the Config.CFlags function. So I've put it here. This should help to avoid stale library caches. The idea is to increment it every time something changes to a library that means it needs to be recompiled. It's a manual process.
1 parent dcf609d commit 789b5c6

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

builder/library.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import (
1515

1616
// Library is a container for information about a single C library, such as a
1717
// compiler runtime or libc.
18+
//
19+
// Note: whenever a library gets changed, the version in compileopts/config.go
20+
// probably also needs to be incremented.
1821
type Library struct {
1922
// The library name, such as compiler-rt or picolibc.
2023
name string
@@ -50,7 +53,7 @@ type Library struct {
5053
// As a side effect, this call creates the library header files if they didn't
5154
// exist yet.
5255
func (l *Library) load(config *compileopts.Config, tmpdir string) (job *compileJob, abortLock func(), err error) {
53-
outdir := config.LibcPath(l.name)
56+
outdir := config.LibraryPath(l.name)
5457
archiveFilePath := filepath.Join(outdir, "lib.a")
5558

5659
// Create a lock on the output (if supported).

compileopts/config.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,23 @@ import (
88
"os"
99
"path/filepath"
1010
"regexp"
11+
"strconv"
1112
"strings"
1213

1314
"github.com/google/shlex"
1415
"github.com/tinygo-org/tinygo/goenv"
1516
)
1617

18+
// Library versions. Whenever an existing library is changed, this number should
19+
// be added/increased so that existing caches are invalidated.
20+
//
21+
// (This is a bit of a layering violation, this should really be part of the
22+
// builder.Library struct but that's hard to do since we want to know the
23+
// library path in advance in several places).
24+
var libVersions = map[string]int{
25+
"musl": 2,
26+
}
27+
1728
// Config keeps all configuration affecting the build in a single struct.
1829
type Config struct {
1930
Options *Options
@@ -247,9 +258,9 @@ func MuslArchitecture(triple string) string {
247258
return CanonicalArchName(triple)
248259
}
249260

250-
// LibcPath returns the path to the libc directory. The libc path will be a libc
251-
// path in the cache directory (which might not yet be built).
252-
func (c *Config) LibcPath(name string) string {
261+
// LibraryPath returns the path to the library build directory. The path will be
262+
// a library path in the cache directory (which might not yet be built).
263+
func (c *Config) LibraryPath(name string) string {
253264
archname := c.Triple()
254265
if c.CPU() != "" {
255266
archname += "-" + c.CPU()
@@ -265,6 +276,11 @@ func (c *Config) LibcPath(name string) string {
265276
archname += "-" + c.Target.Libc
266277
}
267278

279+
// Append a version string, if this library has a version.
280+
if v, ok := libVersions[name]; ok {
281+
archname += "-v" + strconv.Itoa(v)
282+
}
283+
268284
// No precompiled library found. Determine the path name that will be used
269285
// in the build cache.
270286
return filepath.Join(goenv.Get("GOCACHE"), name+"-"+archname)
@@ -351,7 +367,7 @@ func (c *Config) LibcCFlags() []string {
351367
case "picolibc":
352368
root := goenv.Get("TINYGOROOT")
353369
picolibcDir := filepath.Join(root, "lib", "picolibc", "newlib", "libc")
354-
path := c.LibcPath("picolibc")
370+
path := c.LibraryPath("picolibc")
355371
return []string{
356372
"-nostdlibinc",
357373
"-isystem", filepath.Join(path, "include"),
@@ -361,7 +377,7 @@ func (c *Config) LibcCFlags() []string {
361377
}
362378
case "musl":
363379
root := goenv.Get("TINYGOROOT")
364-
path := c.LibcPath("musl")
380+
path := c.LibraryPath("musl")
365381
arch := MuslArchitecture(c.Triple())
366382
return []string{
367383
"-nostdlibinc",
@@ -371,7 +387,7 @@ func (c *Config) LibcCFlags() []string {
371387
"-isystem", filepath.Join(root, "lib", "musl", "include"),
372388
}
373389
case "wasi-libc":
374-
path := c.LibcPath("wasi-libc")
390+
path := c.LibraryPath("wasi-libc")
375391
return []string{
376392
"-nostdlibinc",
377393
"-isystem", filepath.Join(path, "include"),
@@ -381,7 +397,7 @@ func (c *Config) LibcCFlags() []string {
381397
return nil
382398
case "mingw-w64":
383399
root := goenv.Get("TINYGOROOT")
384-
path := c.LibcPath("mingw-w64")
400+
path := c.LibraryPath("mingw-w64")
385401
return []string{
386402
"-nostdlibinc",
387403
"-isystem", filepath.Join(path, "include"),

0 commit comments

Comments
 (0)