Skip to content

Commit eb85640

Browse files
author
Binu Jose Philip
authored
feat: enable macro override for component definitions in srpm build (#356)
1 parent a32da22 commit eb85640

5 files changed

Lines changed: 63 additions & 24 deletions

File tree

defaultconfigs/content/distros/azl/azl.distro.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ spec = { type = "upstream", upstream-distro = { name = "fedora", version = "42"
2525
dist = ".azl4"
2626
vendor = "Microsoft Corporation"
2727
distribution = "Azure Linux"
28+
azl = "4"

internal/app/azldev/core/componentbuilder/componentbuilder.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/microsoft/azldev/internal/app/azldev/core/workdir"
1818
"github.com/microsoft/azldev/internal/buildenv"
1919
"github.com/microsoft/azldev/internal/global/opctx"
20+
"github.com/microsoft/azldev/internal/rpm/mock"
2021
"github.com/microsoft/azldev/internal/utils/fileutils"
2122
)
2223

@@ -144,7 +145,16 @@ func (b *Builder) buildSRPMFromPreparedSources(
144145

145146
defer srpmEvent.End()
146147

147-
srpmOptions := buildenv.SRPMBuildOptions{}
148+
componentConfig := component.GetConfig()
149+
150+
srpmOptions := buildenv.SRPMBuildOptions{
151+
CommonBuildOptions: mock.CommonBuildOptions{
152+
With: componentConfig.Build.With,
153+
Without: componentConfig.Build.Without,
154+
Defines: componentConfig.Build.Defines,
155+
LocalRepoPaths: []string{},
156+
},
157+
}
148158

149159
err = buildEnv.BuildSRPM(ctx, preparedSpecPath, preparedSourcesDir, srpmOutputDir, srpmOptions)
150160
if err != nil {
@@ -205,10 +215,14 @@ func (b *Builder) buildRPM(
205215

206216
// Compose RPM build options, based on our parameters and the component's default configuration.
207217
rpmOptions := buildenv.RPMBuildOptions{
208-
RunChecks: runChecks,
209-
With: componentConfig.Build.With,
210-
Without: componentConfig.Build.Without,
211-
Defines: componentConfig.Build.Defines,
218+
CommonBuildOptions: mock.CommonBuildOptions{
219+
With: componentConfig.Build.With,
220+
Without: componentConfig.Build.Without,
221+
Defines: componentConfig.Build.Defines,
222+
LocalRepoPaths: []string{},
223+
},
224+
RunChecks: runChecks,
225+
ForceRebuild: false, // Could be added to config if needed
212226
}
213227

214228
err = buildEnv.BuildRPM(ctx, srpmPath, tempRPMDir, rpmOptions)

internal/buildenv/buildenv.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ type BuildEnv interface {
3131
Destroy(ctx opctx.Ctx) error
3232
}
3333

34-
// SRPMBuildOptions encapsulates options that may be specified when building a source RPM package
34+
// SRPMBuildOptions encapsulates options that may be specified when building source RPM packages
3535
// using an [RPMAwareBuildEnv].
3636
type SRPMBuildOptions = mock.SRPMBuildOptions
3737

38-
// SRPMBuildOptions encapsulates options that may be specified when building binary RPM packages
38+
// RPMBuildOptions encapsulates options that may be specified when building binary RPM packages
3939
// using an [RPMAwareBuildEnv].
4040
type RPMBuildOptions = mock.RPMBuildOptions
4141

internal/rpm/mock/mock.go

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,30 @@ func (r *Runner) GetRootPath(ctx context.Context) (string, error) {
232232
return strings.TrimSpace(rootPath), nil
233233
}
234234

235-
// Encapsulates the options for building an SRPM using mock.
236-
type SRPMBuildOptions struct {
235+
// CommonBuildOptions encapsulates options that are shared between SRPM and RPM builds.
236+
type CommonBuildOptions struct {
237+
With []string
238+
Without []string
239+
Defines map[string]string
240+
237241
LocalRepoPaths []string
238242
}
239243

244+
// SRPMBuildOptions encapsulates options for building source RPMs using mock.
245+
type SRPMBuildOptions struct {
246+
CommonBuildOptions
247+
// Add any SRPM-specific options here in the future if needed
248+
}
249+
250+
// RPMBuildOptions encapsulates options for building binary RPMs using mock.
251+
type RPMBuildOptions struct {
252+
CommonBuildOptions
253+
254+
// Binary RPM specific options
255+
RunChecks bool
256+
ForceRebuild bool
257+
}
258+
240259
// Builds a Source RPM (SRPM) using mock.
241260
func (r *Runner) BuildSRPM(
242261
ctx context.Context, specPath, sourceDirPath, outputDirPath string, options SRPMBuildOptions,
@@ -255,6 +274,21 @@ func (r *Runner) BuildSRPM(
255274
"--resultdir", outputDirPath,
256275
)
257276

277+
// Add 'with'-style enabled conditionals.
278+
for _, with := range options.With {
279+
args = append(args, "--with", with)
280+
}
281+
282+
// Add 'without'-style disabled conditionals.
283+
for _, without := range options.Without {
284+
args = append(args, "--without", without)
285+
}
286+
287+
// Add override macros.
288+
for macro, value := range options.Defines {
289+
args = append(args, "--define", macro+" "+value)
290+
}
291+
258292
// If provided, pass along extra local repo dirs.
259293
for _, localRepoPath := range options.LocalRepoPaths {
260294
args = append(args, "--addrepo", localRepoPath)
@@ -287,18 +321,6 @@ func (r *Runner) BuildSRPM(
287321
return nil
288322
}
289323

290-
// Encapsulates the options for building a binary RPM using mock.
291-
type RPMBuildOptions struct {
292-
RunChecks bool
293-
ForceRebuild bool
294-
295-
With []string
296-
Without []string
297-
Defines map[string]string
298-
299-
LocalRepoPaths []string
300-
}
301-
302324
// Given a path to a Source RPM (SRPM), invokes mock to build a binary RPM.
303325
func (r *Runner) BuildRPM(ctx context.Context, srpmPath, outputDirPath string, options RPMBuildOptions) error {
304326
slog.Debug("Building RPM", "SRPM", srpmPath)

internal/rpm/mock/mock_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,12 @@ func TestBuildRPM(t *testing.T) {
173173

174174
runner := mock.NewRunner(ctx, testMockConfigPath)
175175
mockOptions := mock.RPMBuildOptions{
176+
CommonBuildOptions: mock.CommonBuildOptions{
177+
Without: []string{withoutFeature},
178+
With: []string{withFeature},
179+
Defines: map[string]string{macroName: macroValue},
180+
},
176181
RunChecks: false,
177-
Without: []string{withoutFeature},
178-
With: []string{withFeature},
179-
Defines: map[string]string{macroName: macroValue},
180182
}
181183

182184
err := runner.BuildRPM(ctx, testSRPMPath, testOutputDirPath, mockOptions)

0 commit comments

Comments
 (0)