@@ -14,6 +14,24 @@ func stagingDir(buildDir string) string {
1414 return filepath .Join (filepath .Dir (filepath .Dir (buildDir )), "staging" )
1515}
1616
17+ // openBuildLog opens a build log file and returns a multiwriter that writes to both
18+ // the log file and stdout. The returned cleanup function must be called when done.
19+ // If append is true, the log file is opened in append mode; otherwise it is truncated.
20+ func openBuildLog (buildDir string , append bool ) (io.Writer , func (), error ) {
21+ logFile := filepath .Join (buildDir , "build.log" )
22+ var logger * os.File
23+ var err error
24+ if append {
25+ logger , err = os .OpenFile (logFile , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0644 )
26+ } else {
27+ logger , err = os .Create (logFile )
28+ }
29+ if err != nil {
30+ return nil , nil , err
31+ }
32+ return io .MultiWriter (logger , os .Stdout ), func () { logger .Close () }, nil
33+ }
34+
1735// AutoconfBuild implements the BuildSystem interface for autoconf-based builds
1836type AutoconfBuild struct {}
1937
@@ -55,44 +73,38 @@ func (a *AutoconfBuild) Configure(lib *Library, srcPath, buildDir, installDir st
5573 return fmt .Errorf ("failed to make configure executable: %w" , err )
5674 }
5775
58- logFile := filepath .Join (buildDir , "build.log" )
59- logger , err := os .Create (logFile )
76+ output , cleanup , err := openBuildLog (buildDir , false )
6077 if err != nil {
6178 return err
6279 }
63- defer logger .Close ()
64-
65- multiWriter := io .MultiWriter (logger , os .Stdout )
80+ defer cleanup ()
6681
67- if err := runCommand (srcPath , multiWriter , installDir , configurePath , args ... ); err != nil {
82+ if err := runCommand (srcPath , output , installDir , configurePath , args ... ); err != nil {
6883 return err
6984 }
7085
7186 return nil
7287}
7388
7489func (a * AutoconfBuild ) Build (lib * Library , srcPath , buildDir string ) error {
75- logFile := filepath .Join (buildDir , "build.log" )
76- logger , err := os .OpenFile (logFile , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0644 )
90+ output , cleanup , err := openBuildLog (buildDir , true )
7791 if err != nil {
7892 return err
7993 }
80- defer logger .Close ()
81-
82- multiWriter := io .MultiWriter (logger , os .Stdout )
94+ defer cleanup ()
8395
8496 // Touch automake files to prevent regeneration
8597 touchAutomakeFiles (srcPath )
8698
8799 installDir := stagingDir (buildDir )
88100
89101 // make
90- if err := runCommand (srcPath , multiWriter , installDir , "make" , "-j" , fmt .Sprintf ("%d" , runtime .NumCPU ())); err != nil {
102+ if err := runCommand (srcPath , output , installDir , "make" , "-j" , fmt .Sprintf ("%d" , runtime .NumCPU ())); err != nil {
91103 return err
92104 }
93105
94106 // make install
95- if err := runCommand (srcPath , multiWriter , installDir , "make" , "install" ); err != nil {
107+ if err := runCommand (srcPath , output , installDir , "make" , "install" ); err != nil {
96108 return err
97109 }
98110
@@ -122,40 +134,34 @@ func (c *CMakeBuild) Configure(lib *Library, srcPath, buildDir, installDir strin
122134 args = append (args , lib .ConfigureArgs (runtime .GOOS )... )
123135 }
124136
125- logFile := filepath .Join (buildDir , "build.log" )
126- logger , err := os .Create (logFile )
137+ output , cleanup , err := openBuildLog (buildDir , false )
127138 if err != nil {
128139 return err
129140 }
130- defer logger .Close ()
131-
132- multiWriter := io .MultiWriter (logger , os .Stdout )
141+ defer cleanup ()
133142
134- if err := runCommand (buildDir , multiWriter , installDir , "cmake" , args ... ); err != nil {
143+ if err := runCommand (buildDir , output , installDir , "cmake" , args ... ); err != nil {
135144 return err
136145 }
137146
138147 return nil
139148}
140149
141150func (c * CMakeBuild ) Build (lib * Library , srcPath , buildDir string ) error {
142- logFile := filepath .Join (buildDir , "build.log" )
143- logger , err := os .OpenFile (logFile , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0644 )
151+ output , cleanup , err := openBuildLog (buildDir , true )
144152 if err != nil {
145153 return err
146154 }
147- defer logger .Close ()
148-
149- multiWriter := io .MultiWriter (logger , os .Stdout )
155+ defer cleanup ()
150156
151157 installDir := stagingDir (buildDir )
152158
153159 // cmake --build . --target install
154- if err := runCommand (buildDir , multiWriter , installDir , "cmake" , "--build" , "." , "--parallel" , fmt .Sprintf ("%d" , runtime .NumCPU ())); err != nil {
160+ if err := runCommand (buildDir , output , installDir , "cmake" , "--build" , "." , "--parallel" , fmt .Sprintf ("%d" , runtime .NumCPU ())); err != nil {
155161 return err
156162 }
157163
158- if err := runCommand (buildDir , multiWriter , installDir , "cmake" , "--build" , "." , "--target" , "install" ); err != nil {
164+ if err := runCommand (buildDir , output , installDir , "cmake" , "--build" , "." , "--target" , "install" ); err != nil {
159165 return err
160166 }
161167
@@ -180,41 +186,35 @@ func (m *MesonBuild) Configure(lib *Library, srcPath, buildDir, installDir strin
180186 args = append (args , lib .ConfigureArgs (runtime .GOOS )... )
181187 }
182188
183- logFile := filepath .Join (buildDir , "build.log" )
184- logger , err := os .Create (logFile )
189+ output , cleanup , err := openBuildLog (buildDir , false )
185190 if err != nil {
186191 return err
187192 }
188- defer logger . Close ()
193+ defer cleanup ()
189194
190- multiWriter := io .MultiWriter (logger , os .Stdout )
191-
192- if err := runCommand ("." , multiWriter , installDir , "meson" , args ... ); err != nil {
195+ if err := runCommand ("." , output , installDir , "meson" , args ... ); err != nil {
193196 return err
194197 }
195198
196199 return nil
197200}
198201
199202func (m * MesonBuild ) Build (lib * Library , srcPath , buildDir string ) error {
200- logFile := filepath .Join (buildDir , "build.log" )
201- logger , err := os .OpenFile (logFile , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0644 )
203+ output , cleanup , err := openBuildLog (buildDir , true )
202204 if err != nil {
203205 return err
204206 }
205- defer logger .Close ()
206-
207- multiWriter := io .MultiWriter (logger , os .Stdout )
207+ defer cleanup ()
208208
209209 installDir := stagingDir (buildDir )
210210
211211 // meson compile
212- if err := runCommand (buildDir , multiWriter , installDir , "meson" , "compile" ); err != nil {
212+ if err := runCommand (buildDir , output , installDir , "meson" , "compile" ); err != nil {
213213 return err
214214 }
215215
216216 // meson install
217- if err := runCommand (buildDir , multiWriter , installDir , "meson" , "install" ); err != nil {
217+ if err := runCommand (buildDir , output , installDir , "meson" , "install" ); err != nil {
218218 return err
219219 }
220220
@@ -253,20 +253,17 @@ func (m *MakefileBuild) Configure(lib *Library, srcPath, buildDir, installDir st
253253}
254254
255255func (m * MakefileBuild ) Build (lib * Library , srcPath , buildDir string ) error {
256- logFile := filepath .Join (buildDir , "build.log" )
257- logger , err := os .Create (logFile )
256+ output , cleanup , err := openBuildLog (buildDir , false )
258257 if err != nil {
259258 return err
260259 }
261- defer logger .Close ()
262-
263- multiWriter := io .MultiWriter (logger , os .Stdout )
260+ defer cleanup ()
264261
265262 installDir := stagingDir (buildDir )
266263
267264 // Build the targets
268265 args := append ([]string {"-j" , fmt .Sprintf ("%d" , runtime .NumCPU ())}, m .Targets ... )
269- if err := runCommand (srcPath , multiWriter , installDir , "make" , args ... ); err != nil {
266+ if err := runCommand (srcPath , output , installDir , "make" , args ... ); err != nil {
270267 return err
271268 }
272269
@@ -308,41 +305,35 @@ func (o *OpenSSLBuild) Configure(lib *Library, srcPath, buildDir, installDir str
308305 return fmt .Errorf ("failed to make Configure executable: %w" , err )
309306 }
310307
311- logFile := filepath .Join (buildDir , "build.log" )
312- logger , err := os .Create (logFile )
308+ output , cleanup , err := openBuildLog (buildDir , false )
313309 if err != nil {
314310 return err
315311 }
316- defer logger . Close ()
312+ defer cleanup ()
317313
318- multiWriter := io .MultiWriter (logger , os .Stdout )
319-
320- if err := runCommand (srcPath , multiWriter , installDir , configurePath , args ... ); err != nil {
314+ if err := runCommand (srcPath , output , installDir , configurePath , args ... ); err != nil {
321315 return err
322316 }
323317
324318 return nil
325319}
326320
327321func (o * OpenSSLBuild ) Build (lib * Library , srcPath , buildDir string ) error {
328- logFile := filepath .Join (buildDir , "build.log" )
329- logger , err := os .OpenFile (logFile , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0644 )
322+ output , cleanup , err := openBuildLog (buildDir , true )
330323 if err != nil {
331324 return err
332325 }
333- defer logger .Close ()
334-
335- multiWriter := io .MultiWriter (logger , os .Stdout )
326+ defer cleanup ()
336327
337328 installDir := stagingDir (buildDir )
338329
339330 // make
340- if err := runCommand (srcPath , multiWriter , installDir , "make" , "-j" , fmt .Sprintf ("%d" , runtime .NumCPU ())); err != nil {
331+ if err := runCommand (srcPath , output , installDir , "make" , "-j" , fmt .Sprintf ("%d" , runtime .NumCPU ())); err != nil {
341332 return err
342333 }
343334
344335 // make install_sw (install software only, skip docs)
345- if err := runCommand (srcPath , multiWriter , installDir , "make" , "install_sw" ); err != nil {
336+ if err := runCommand (srcPath , output , installDir , "make" , "install_sw" ); err != nil {
346337 return err
347338 }
348339
0 commit comments