Skip to content

Commit 2108e45

Browse files
committed
builder: key library caches by inputs
Compute target library cache directories from a content-derived description of the library build instead of relying on manual version bumps. Use that same description to drive compilation, so the cache key covers the inputs consumed by the build. The key includes source hashes, generated headers, header inputs, per-file flags, the compiler argument template, relevant target options, and the clang/LLVM identity. This avoids stale lib.a and crt1.o files after libc, compiler-rt, header, or compiler flag changes.
1 parent 30355ce commit 2108e45

5 files changed

Lines changed: 357 additions & 121 deletions

File tree

builder/build.go

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -146,53 +146,33 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
146146
// As a side effect, this also creates the headers for the given libc, if
147147
// the libc needs them.
148148
root := goenv.Get("TINYGOROOT")
149+
libraries, err := configuredLibraries(config)
150+
if err != nil {
151+
return BuildResult{}, err
152+
}
153+
libraryKeys, err := makeLibraryCacheKeys(config, libraries)
154+
if err != nil {
155+
return BuildResult{}, err
156+
}
157+
config.LibraryKeys = libraryKeys
149158
var libcDependencies []*compileJob
150-
switch config.Target.Libc {
151-
case "darwin-libSystem":
159+
if config.Target.Libc == "darwin-libSystem" {
152160
libcJob := makeDarwinLibSystemJob(config, tmpdir)
153161
libcDependencies = append(libcDependencies, libcJob)
154-
case "musl":
155-
var unlock func()
156-
libcJob, unlock, err := libMusl.load(config, tmpdir)
157-
if err != nil {
158-
return BuildResult{}, err
159-
}
160-
defer unlock()
161-
libcDependencies = append(libcDependencies, dummyCompileJob(filepath.Join(filepath.Dir(libcJob.result), "crt1.o")))
162-
libcDependencies = append(libcDependencies, libcJob)
163-
case "picolibc":
164-
libcJob, unlock, err := libPicolibc.load(config, tmpdir)
165-
if err != nil {
166-
return BuildResult{}, err
167-
}
168-
defer unlock()
169-
libcDependencies = append(libcDependencies, libcJob)
170-
case "wasi-libc":
171-
libcJob, unlock, err := libWasiLibc.load(config, tmpdir)
172-
if err != nil {
173-
return BuildResult{}, err
174-
}
175-
defer unlock()
176-
libcDependencies = append(libcDependencies, libcJob)
177-
case "wasmbuiltins":
178-
libcJob, unlock, err := libWasmBuiltins.load(config, tmpdir)
162+
}
163+
if libraries.libc != nil {
164+
libcJob, unlock, err := libraries.libc.load(config, tmpdir)
179165
if err != nil {
180166
return BuildResult{}, err
181167
}
182168
defer unlock()
183-
libcDependencies = append(libcDependencies, libcJob)
184-
case "mingw-w64":
185-
libcJob, unlock, err := libMinGW.load(config, tmpdir)
186-
if err != nil {
187-
return BuildResult{}, err
169+
if libraries.libc.crt1Source != "" {
170+
libcDependencies = append(libcDependencies, dummyCompileJob(filepath.Join(filepath.Dir(libcJob.result), "crt1.o")))
188171
}
189-
defer unlock()
190172
libcDependencies = append(libcDependencies, libcJob)
173+
}
174+
if config.Target.Libc == "mingw-w64" {
191175
libcDependencies = append(libcDependencies, makeMinGWExtraLibs(tmpdir, config.GOARCH())...)
192-
case "":
193-
// no library specified, so nothing to do
194-
default:
195-
return BuildResult{}, fmt.Errorf("unknown libc: %s", config.Target.Libc)
196176
}
197177

198178
optLevel, speedLevel, sizeLevel := config.OptLevel()
@@ -734,27 +714,16 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
734714
}
735715
}
736716

737-
// Add compiler-rt dependency if needed. Usually this is a simple load from
738-
// a cache.
739-
if config.Target.RTLib == "compiler-rt" {
740-
job, unlock, err := libCompilerRT.load(config, tmpdir)
717+
// Add library dependencies needed by the linker, usually from the cache.
718+
for _, library := range libraries.linker {
719+
job, unlock, err := library.load(config, tmpdir)
741720
if err != nil {
742721
return result, err
743722
}
744723
defer unlock()
745724
linkerDependencies = append(linkerDependencies, job)
746725
}
747726

748-
// The Boehm collector is stored in a separate C library.
749-
if config.GC() == "boehm" {
750-
job, unlock, err := BoehmGC.load(config, tmpdir)
751-
if err != nil {
752-
return BuildResult{}, err
753-
}
754-
defer unlock()
755-
linkerDependencies = append(linkerDependencies, job)
756-
}
757-
758727
// Add jobs to compile extra files. These files are in C or assembly and
759728
// contain things like the interrupt vector table and low level operations
760729
// such as stack switching.

0 commit comments

Comments
 (0)