@@ -32,6 +32,17 @@ func openBuildLog(buildDir string, append bool) (io.Writer, func(), error) {
3232 return io .MultiWriter (logger , os .Stdout ), func () { logger .Close () }, nil
3333}
3434
35+ // withBuildLog opens a build log and executes the provided function with the logger.
36+ // This helper consolidates the repeated log setup/cleanup pattern across build systems.
37+ func withBuildLog (buildDir string , append bool , fn func (output io.Writer ) error ) error {
38+ output , cleanup , err := openBuildLog (buildDir , append )
39+ if err != nil {
40+ return err
41+ }
42+ defer cleanup ()
43+ return fn (output )
44+ }
45+
3546// AutoconfBuild implements the BuildSystem interface for autoconf-based builds
3647type AutoconfBuild struct {}
3748
@@ -73,42 +84,25 @@ func (a *AutoconfBuild) Configure(lib *Library, srcPath, buildDir, installDir st
7384 return fmt .Errorf ("failed to make configure executable: %w" , err )
7485 }
7586
76- output , cleanup , err := openBuildLog (buildDir , false )
77- if err != nil {
78- return err
79- }
80- defer cleanup ()
81-
82- if err := runCommand (srcPath , output , installDir , configurePath , args ... ); err != nil {
83- return err
84- }
85-
86- return nil
87+ return withBuildLog (buildDir , false , func (output io.Writer ) error {
88+ return runCommand (srcPath , output , installDir , configurePath , args ... )
89+ })
8790}
8891
8992func (a * AutoconfBuild ) Build (lib * Library , srcPath , buildDir string ) error {
90- output , cleanup , err := openBuildLog (buildDir , true )
91- if err != nil {
92- return err
93- }
94- defer cleanup ()
95-
9693 // Touch automake files to prevent regeneration
9794 touchAutomakeFiles (srcPath )
9895
9996 installDir := stagingDir (buildDir )
10097
101- // make
102- if err := runCommand (srcPath , output , installDir , "make" , "-j" , fmt .Sprintf ("%d" , runtime .NumCPU ())); err != nil {
103- return err
104- }
105-
106- // make install
107- if err := runCommand (srcPath , output , installDir , "make" , "install" ); err != nil {
108- return err
109- }
110-
111- return nil
98+ return withBuildLog (buildDir , true , func (output io.Writer ) error {
99+ // make
100+ if err := runCommand (srcPath , output , installDir , "make" , "-j" , fmt .Sprintf ("%d" , runtime .NumCPU ())); err != nil {
101+ return err
102+ }
103+ // make install
104+ return runCommand (srcPath , output , installDir , "make" , "install" )
105+ })
112106}
113107
114108// CMakeBuild implements the BuildSystem interface for CMake-based builds
@@ -134,38 +128,21 @@ func (c *CMakeBuild) Configure(lib *Library, srcPath, buildDir, installDir strin
134128 args = append (args , lib .ConfigureArgs (runtime .GOOS )... )
135129 }
136130
137- output , cleanup , err := openBuildLog (buildDir , false )
138- if err != nil {
139- return err
140- }
141- defer cleanup ()
142-
143- if err := runCommand (buildDir , output , installDir , "cmake" , args ... ); err != nil {
144- return err
145- }
146-
147- return nil
131+ return withBuildLog (buildDir , false , func (output io.Writer ) error {
132+ return runCommand (buildDir , output , installDir , "cmake" , args ... )
133+ })
148134}
149135
150136func (c * CMakeBuild ) Build (lib * Library , srcPath , buildDir string ) error {
151- output , cleanup , err := openBuildLog (buildDir , true )
152- if err != nil {
153- return err
154- }
155- defer cleanup ()
156-
157137 installDir := stagingDir (buildDir )
158138
159- // cmake --build . --target install
160- if err := runCommand (buildDir , output , installDir , "cmake" , "--build" , "." , "--parallel" , fmt .Sprintf ("%d" , runtime .NumCPU ())); err != nil {
161- return err
162- }
163-
164- if err := runCommand (buildDir , output , installDir , "cmake" , "--build" , "." , "--target" , "install" ); err != nil {
165- return err
166- }
167-
168- return nil
139+ return withBuildLog (buildDir , true , func (output io.Writer ) error {
140+ // cmake --build . --target install
141+ if err := runCommand (buildDir , output , installDir , "cmake" , "--build" , "." , "--parallel" , fmt .Sprintf ("%d" , runtime .NumCPU ())); err != nil {
142+ return err
143+ }
144+ return runCommand (buildDir , output , installDir , "cmake" , "--build" , "." , "--target" , "install" )
145+ })
169146}
170147
171148// MesonBuild implements the BuildSystem interface for Meson-based builds
@@ -186,39 +163,22 @@ func (m *MesonBuild) Configure(lib *Library, srcPath, buildDir, installDir strin
186163 args = append (args , lib .ConfigureArgs (runtime .GOOS )... )
187164 }
188165
189- output , cleanup , err := openBuildLog (buildDir , false )
190- if err != nil {
191- return err
192- }
193- defer cleanup ()
194-
195- if err := runCommand ("." , output , installDir , "meson" , args ... ); err != nil {
196- return err
197- }
198-
199- return nil
166+ return withBuildLog (buildDir , false , func (output io.Writer ) error {
167+ return runCommand ("." , output , installDir , "meson" , args ... )
168+ })
200169}
201170
202171func (m * MesonBuild ) Build (lib * Library , srcPath , buildDir string ) error {
203- output , cleanup , err := openBuildLog (buildDir , true )
204- if err != nil {
205- return err
206- }
207- defer cleanup ()
208-
209172 installDir := stagingDir (buildDir )
210173
211- // meson compile
212- if err := runCommand (buildDir , output , installDir , "meson" , "compile" ); err != nil {
213- return err
214- }
215-
216- // meson install
217- if err := runCommand (buildDir , output , installDir , "meson" , "install" ); err != nil {
218- return err
219- }
220-
221- return nil
174+ return withBuildLog (buildDir , true , func (output io.Writer ) error {
175+ // meson compile
176+ if err := runCommand (buildDir , output , installDir , "meson" , "compile" ); err != nil {
177+ return err
178+ }
179+ // meson install
180+ return runCommand (buildDir , output , installDir , "meson" , "install" )
181+ })
222182}
223183
224184// CargoBuild implements the BuildSystem interface for Cargo/Rust-based builds
@@ -253,26 +213,21 @@ func (m *MakefileBuild) Configure(lib *Library, srcPath, buildDir, installDir st
253213}
254214
255215func (m * MakefileBuild ) Build (lib * Library , srcPath , buildDir string ) error {
256- output , cleanup , err := openBuildLog (buildDir , false )
257- if err != nil {
258- return err
259- }
260- defer cleanup ()
261-
262216 installDir := stagingDir (buildDir )
263217
264- // Build the targets
265- args := append ([]string {"-j" , fmt .Sprintf ("%d" , runtime .NumCPU ())}, m .Targets ... )
266- if err := runCommand (srcPath , output , installDir , "make" , args ... ); err != nil {
267- return err
268- }
269-
270- // If a custom install function is provided, use it
271- if m .InstallFunc != nil {
272- return m .InstallFunc (srcPath , installDir )
273- }
274-
275- return nil
218+ return withBuildLog (buildDir , false , func (output io.Writer ) error {
219+ // Build the targets
220+ args := append ([]string {"-j" , fmt .Sprintf ("%d" , runtime .NumCPU ())}, m .Targets ... )
221+ if err := runCommand (srcPath , output , installDir , "make" , args ... ); err != nil {
222+ return err
223+ }
224+
225+ // If a custom install function is provided, use it
226+ if m .InstallFunc != nil {
227+ return m .InstallFunc (srcPath , installDir )
228+ }
229+ return nil
230+ })
276231}
277232
278233// OpenSSLBuild implements the BuildSystem interface for OpenSSL's Configure/make
@@ -305,37 +260,20 @@ func (o *OpenSSLBuild) Configure(lib *Library, srcPath, buildDir, installDir str
305260 return fmt .Errorf ("failed to make Configure executable: %w" , err )
306261 }
307262
308- output , cleanup , err := openBuildLog (buildDir , false )
309- if err != nil {
310- return err
311- }
312- defer cleanup ()
313-
314- if err := runCommand (srcPath , output , installDir , configurePath , args ... ); err != nil {
315- return err
316- }
317-
318- return nil
263+ return withBuildLog (buildDir , false , func (output io.Writer ) error {
264+ return runCommand (srcPath , output , installDir , configurePath , args ... )
265+ })
319266}
320267
321268func (o * OpenSSLBuild ) Build (lib * Library , srcPath , buildDir string ) error {
322- output , cleanup , err := openBuildLog (buildDir , true )
323- if err != nil {
324- return err
325- }
326- defer cleanup ()
327-
328269 installDir := stagingDir (buildDir )
329270
330- // make
331- if err := runCommand (srcPath , output , installDir , "make" , "-j" , fmt .Sprintf ("%d" , runtime .NumCPU ())); err != nil {
332- return err
333- }
334-
335- // make install_sw (install software only, skip docs)
336- if err := runCommand (srcPath , output , installDir , "make" , "install_sw" ); err != nil {
337- return err
338- }
339-
340- return nil
271+ return withBuildLog (buildDir , true , func (output io.Writer ) error {
272+ // make
273+ if err := runCommand (srcPath , output , installDir , "make" , "-j" , fmt .Sprintf ("%d" , runtime .NumCPU ())); err != nil {
274+ return err
275+ }
276+ // make install_sw (install software only, skip docs)
277+ return runCommand (srcPath , output , installDir , "make" , "install_sw" )
278+ })
341279}
0 commit comments