@@ -65,6 +65,25 @@ func sourcePatchesDir(sOpt dalec.SourceOpts, base llb.State, dir, name string, s
65
65
return append (states , series ), nil
66
66
}
67
67
68
+ type SourcePkgConfig struct {
69
+ // PrependPath is a list of paths to be prepended to the $PATH var in build
70
+ // scripts.
71
+ PrependPath []string
72
+ // APpendPath is a list of paths to be appended to the $PATH var in build
73
+ // scripts.
74
+ AppendPath []string
75
+ }
76
+
77
+ // Addpath creates a SourcePkgConfig where the first argument is sets
78
+ // [SourcePkgConfig.PrependPath] and the 2nd argument sets
79
+ // [SourcePkgConfig.AppendPath]
80
+ func AddPath (pre , post []string ) SourcePkgConfig {
81
+ return SourcePkgConfig {
82
+ PrependPath : pre ,
83
+ AppendPath : post ,
84
+ }
85
+ }
86
+
68
87
// Debroot creates a debian root directory suitable for use with debbuild.
69
88
// This does not include sources in case you want to mount sources (instead of copying them) later.
70
89
//
@@ -75,7 +94,7 @@ func sourcePatchesDir(sOpt dalec.SourceOpts, base llb.State, dir, name string, s
75
94
// an upgrade even if it is technically the same underlying source.
76
95
// It may be left blank but is highly recommended to set this.
77
96
// Use [ReadDistroVersionID] to get a suitable value.
78
- func Debroot (ctx context.Context , sOpt dalec.SourceOpts , spec * dalec.Spec , worker , in llb.State , target , dir , distroVersionID string , opts ... llb.ConstraintsOpt ) (llb.State , error ) {
97
+ func Debroot (ctx context.Context , sOpt dalec.SourceOpts , spec * dalec.Spec , worker , in llb.State , target , dir , distroVersionID string , cfg SourcePkgConfig , opts ... llb.ConstraintsOpt ) (llb.State , error ) {
79
98
control , err := controlFile (spec , in , target , dir )
80
99
if err != nil {
81
100
return llb .Scratch (), errors .Wrap (err , "error generating control file" )
@@ -122,15 +141,10 @@ func Debroot(ctx context.Context, sOpt dalec.SourceOpts, spec *dalec.Spec, worke
122
141
dalecDir := base .
123
142
File (llb .Mkdir (filepath .Join (dir , "dalec" ), 0o755 ), opts ... )
124
143
125
- pathVar , _ , err := worker .GetEnv (ctx , "PATH" , opts ... )
126
- if err != nil {
127
- return in , fmt .Errorf ("error looking up $PATH in worker image: %w" , err )
128
- }
129
-
130
- states = append (states , dalecDir .File (llb .Mkfile (filepath .Join (dir , "dalec/build.sh" ), 0o700 , createBuildScript (spec , pathVar )), opts ... ))
131
- states = append (states , dalecDir .File (llb .Mkfile (filepath .Join (dir , "dalec/patch.sh" ), 0o700 , createPatchScript (spec , pathVar )), opts ... ))
132
- states = append (states , dalecDir .File (llb .Mkfile (filepath .Join (dir , "dalec/fix_sources.sh" ), 0o700 , fixupSources (spec , pathVar )), opts ... ))
133
- states = append (states , dalecDir .File (llb .Mkfile (filepath .Join (dir , "dalec/fix_perms.sh" ), 0o700 , fixupArtifactPerms (spec , pathVar )), opts ... ))
144
+ states = append (states , dalecDir .File (llb .Mkfile (filepath .Join (dir , "dalec/build.sh" ), 0o700 , createBuildScript (spec , & cfg )), opts ... ))
145
+ states = append (states , dalecDir .File (llb .Mkfile (filepath .Join (dir , "dalec/patch.sh" ), 0o700 , createPatchScript (spec , & cfg )), opts ... ))
146
+ states = append (states , dalecDir .File (llb .Mkfile (filepath .Join (dir , "dalec/fix_sources.sh" ), 0o700 , fixupSources (spec , & cfg )), opts ... ))
147
+ states = append (states , dalecDir .File (llb .Mkfile (filepath .Join (dir , "dalec/fix_perms.sh" ), 0o700 , fixupArtifactPerms (spec , & cfg )), opts ... ))
134
148
135
149
customEnable , err := customDHInstallSystemdPostinst (spec )
136
150
if err != nil {
@@ -166,9 +180,9 @@ func Debroot(ctx context.Context, sOpt dalec.SourceOpts, spec *dalec.Spec, worke
166
180
return dalec .MergeAtPath (in , states , "/" ), nil
167
181
}
168
182
169
- func fixupArtifactPerms (spec * dalec.Spec , pathVar string ) []byte {
183
+ func fixupArtifactPerms (spec * dalec.Spec , cfg * SourcePkgConfig ) []byte {
170
184
buf := bytes .NewBuffer (nil )
171
- writeScriptHeader (buf , pathVar )
185
+ writeScriptHeader (buf , cfg )
172
186
173
187
basePath := filepath .Join ("debian" , spec .Name )
174
188
@@ -203,9 +217,9 @@ func fixupArtifactPerms(spec *dalec.Spec, pathVar string) []byte {
203
217
// to bring those back.
204
218
//
205
219
// This is called from `debian/rules` after the source tarball has been extracted.
206
- func fixupSources (spec * dalec.Spec , pathVar string ) []byte {
220
+ func fixupSources (spec * dalec.Spec , cfg * SourcePkgConfig ) []byte {
207
221
buf := bytes .NewBuffer (nil )
208
- writeScriptHeader (buf , pathVar )
222
+ writeScriptHeader (buf , cfg )
209
223
210
224
// now, we need to find all the sources that are file-backed and fix them up
211
225
for name , src := range spec .Sources {
@@ -229,29 +243,41 @@ func fixupSources(spec *dalec.Spec, pathVar string) []byte {
229
243
// Older go versions did not have support for the `GOMODCACHE` var
230
244
// This is a hack to try and make the build work by linking the go modules
231
245
// we've already fetched into to module dir under $GOPATH
232
- fmt .Fprintf (buf , `test -n "$(go env GOMODCACHE)" || (GOPATH="$(go env GOPATH)"; mkdir -p "${GOPATH}/pkg" && ln -s "$(pwd)/%s" "${GOPATH}/pkg/mod")` , gomodsName )
246
+ fmt .Fprintf (buf , `env | grep PATH; test -n "$(go env GOMODCACHE)" || (GOPATH="$(go env GOPATH)"; mkdir -p "${GOPATH}/pkg" && ln -s "$(pwd)/%s" "${GOPATH}/pkg/mod")` , gomodsName )
233
247
// Above command does not have a newline due to quoting issues, so add that here.
234
248
fmt .Fprint (buf , "\n " )
235
249
}
236
250
237
251
return buf .Bytes ()
238
252
}
239
253
240
- func writeScriptHeader (buf io.Writer , pathVar string ) {
254
+ func setupPathVar (pre , post []string ) string {
255
+ if len (pre ) == 0 && len (post ) == 0 {
256
+ return ""
257
+ }
258
+
259
+ full := append (pre , "$PATH" )
260
+ full = append (full , post ... )
261
+ return strings .Join (full , ":" )
262
+ }
263
+
264
+ func writeScriptHeader (buf io.Writer , cfg * SourcePkgConfig ) {
241
265
fmt .Fprintln (buf , "#!/usr/bin/env sh" )
242
266
fmt .Fprintln (buf )
243
267
244
268
fmt .Fprintln (buf , "set -ex" )
245
269
246
- if pathVar != "" {
247
- fmt .Fprintln (buf , `export PATH="` + pathVar + `"` )
270
+ if cfg != nil {
271
+ if pathVar := setupPathVar (cfg .PrependPath , cfg .AppendPath ); pathVar != "" {
272
+ fmt .Fprintln (buf , "export PATH=" + pathVar )
273
+ }
248
274
}
249
275
}
250
276
251
- func createPatchScript (spec * dalec.Spec , pathVar string ) []byte {
277
+ func createPatchScript (spec * dalec.Spec , cfg * SourcePkgConfig ) []byte {
252
278
buf := bytes .NewBuffer (nil )
253
279
254
- writeScriptHeader (buf , pathVar )
280
+ writeScriptHeader (buf , cfg )
255
281
256
282
for name , patches := range spec .Patches {
257
283
for _ , patch := range patches {
@@ -263,9 +289,9 @@ func createPatchScript(spec *dalec.Spec, pathVar string) []byte {
263
289
return buf .Bytes ()
264
290
}
265
291
266
- func createBuildScript (spec * dalec.Spec , pathVar string ) []byte {
292
+ func createBuildScript (spec * dalec.Spec , cfg * SourcePkgConfig ) []byte {
267
293
buf := bytes .NewBuffer (nil )
268
- writeScriptHeader (buf , pathVar )
294
+ writeScriptHeader (buf , cfg )
269
295
270
296
sorted := dalec .SortMapKeys (spec .Build .Env )
271
297
for _ , k := range sorted {
0 commit comments