File: platform.go

package info (click to toggle)
singularity-container 4.1.5%2Bds4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 43,876 kB
  • sloc: asm: 14,840; sh: 3,190; ansic: 1,751; awk: 414; makefile: 413; python: 99
file content (58 lines) | stat: -rw-r--r-- 1,566 bytes parent folder | download
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
57
58
package dockerfile2llb

import (
	"github.com/containerd/platforms"
	"github.com/moby/buildkit/frontend/dockerfile/instructions"
	ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
)

type platformOpt struct {
	targetPlatform ocispecs.Platform
	buildPlatforms []ocispecs.Platform
	implicitTarget bool
}

func buildPlatformOpt(opt *ConvertOpt) *platformOpt {
	buildPlatforms := opt.BuildPlatforms
	targetPlatform := opt.TargetPlatform
	implicitTargetPlatform := false

	if opt.TargetPlatform != nil && opt.BuildPlatforms == nil {
		buildPlatforms = []ocispecs.Platform{*opt.TargetPlatform}
	}
	if len(buildPlatforms) == 0 {
		buildPlatforms = []ocispecs.Platform{platforms.DefaultSpec()}
	}

	if opt.TargetPlatform == nil {
		implicitTargetPlatform = true
		targetPlatform = &buildPlatforms[0]
	}

	return &platformOpt{
		targetPlatform: *targetPlatform,
		buildPlatforms: buildPlatforms,
		implicitTarget: implicitTargetPlatform,
	}
}

func getPlatformArgs(po *platformOpt) []instructions.KeyValuePairOptional {
	bp := po.buildPlatforms[0]
	tp := po.targetPlatform
	m := map[string]string{
		"BUILDPLATFORM":  platforms.Format(bp),
		"BUILDOS":        bp.OS,
		"BUILDARCH":      bp.Architecture,
		"BUILDVARIANT":   bp.Variant,
		"TARGETPLATFORM": platforms.Format(tp),
		"TARGETOS":       tp.OS,
		"TARGETARCH":     tp.Architecture,
		"TARGETVARIANT":  tp.Variant,
	}
	opts := make([]instructions.KeyValuePairOptional, 0, len(m))
	for k, v := range m {
		s := v
		opts = append(opts, instructions.KeyValuePairOptional{Key: k, Value: &s})
	}
	return opts
}