File: buildpathprefix.go

package info (click to toggle)
golang-1.12 1.12.7-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, experimental, sid
  • size: 113,080 kB
  • sloc: asm: 97,488; ansic: 8,245; perl: 2,243; sh: 1,895; xml: 623; python: 349; makefile: 127; cpp: 22; f90: 8; awk: 7
file content (56 lines) | stat: -rw-r--r-- 1,433 bytes parent folder | download | duplicates (27)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package ld

import (
	"log"
	"os"
	"strings"
	"sync"
)

func decode(s string) string {
	s = strings.Replace(s, "%.", ":", -1)
	s = strings.Replace(s, "%+", "=", -1)
	s = strings.Replace(s, "%#", "%", -1)
	return s
}

type prefixMapEntry struct {
	target string
	source string
}

var (
	buildPathPrefixMap     []prefixMapEntry
	buildPathPrefixMapOnce sync.Once
)

// See https://reproducible-builds.org/specs/build-path-prefix-map/
func applyBuildPathPrefixMap(dir string) string {
	// Parse the BUILD_PATH_PREFIX_MAP only once; this function gets called for
	// every compiled file.
	buildPathPrefixMapOnce.Do(func() {
		for _, item := range strings.Split(os.Getenv("BUILD_PATH_PREFIX_MAP"), ":") {
			if strings.TrimSpace(item) == "" {
				continue
			}
			parts := strings.Split(item, "=")
			if got, want := len(parts), 2; got != want {
				log.Fatalf("parsing BUILD_PATH_PREFIX_MAP: incorrect number of = separators in item %q: got %d, want %d", item, got-1, want-1)
			}
			buildPathPrefixMap = append(buildPathPrefixMap, prefixMapEntry{
				target: decode(parts[0]),
				source: decode(parts[1]),
			})
		}
	})
	for _, e := range buildPathPrefixMap {
		if strings.HasPrefix(dir, e.source) {
			return e.target + strings.TrimPrefix(dir, e.source)
		}
	}
	return dir
}