Skip to content

Commit c0fc125

Browse files
author
Federico Fissore
committed
Removing stale libraries object files, resulting from previous compilations
Signed-off-by: Federico Fissore <[email protected]>
1 parent 72c4659 commit c0fc125

File tree

4 files changed

+189
-0
lines changed

4 files changed

+189
-0
lines changed

Diff for: src/arduino.cc/builder/builder.go

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func (s *Builder) Run(context map[string]interface{}) error {
9494
&RecipeByPrefixSuffixRunner{Prefix: constants.HOOKS_SKETCH_POSTBUILD, Suffix: constants.HOOKS_PATTERN_SUFFIX},
9595

9696
&RecipeByPrefixSuffixRunner{Prefix: constants.HOOKS_LIBRARIES_PREBUILD, Suffix: constants.HOOKS_PATTERN_SUFFIX},
97+
&UnusedCompiledLibrariesRemover{},
9798
&phases.LibrariesBuilder{},
9899
&RecipeByPrefixSuffixRunner{Prefix: constants.HOOKS_LIBRARIES_POSTBUILD, Suffix: constants.HOOKS_PATTERN_SUFFIX},
99100

Diff for: src/arduino.cc/builder/test/builder_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,32 @@ func TestBuilderSketchWithSubfolders(t *testing.T) {
341341
err := command.Run(context)
342342
NoError(t, err)
343343
}
344+
345+
func TestBuilderSketchBuildPathContainsUnusedPreviouslyCompiledLibrary(t *testing.T) {
346+
DownloadCoresAndToolsAndLibraries(t)
347+
348+
context := make(map[string]interface{})
349+
350+
buildPath := SetupBuildPath(t, context)
351+
defer os.RemoveAll(buildPath)
352+
353+
NoError(t, os.MkdirAll(filepath.Join(buildPath, constants.FOLDER_LIBRARIES, "SPI"), os.FileMode(0755)))
354+
355+
context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}
356+
context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"}
357+
context[constants.CTX_FQBN] = "arduino:avr:leonardo"
358+
context[constants.CTX_SKETCH_LOCATION] = filepath.Join("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino")
359+
context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"}
360+
context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"}
361+
context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600"
362+
363+
command := builder.Builder{}
364+
err := command.Run(context)
365+
NoError(t, err)
366+
367+
_, err = os.Stat(filepath.Join(buildPath, constants.FOLDER_LIBRARIES, "SPI"))
368+
require.Error(t, err)
369+
require.True(t, os.IsNotExist(err))
370+
_, err = os.Stat(filepath.Join(buildPath, constants.FOLDER_LIBRARIES, "Bridge"))
371+
NoError(t, err)
372+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* This file is part of Arduino Builder.
3+
*
4+
* Arduino Builder is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As a special exception, you may use this file as part of a free software
19+
* library without restriction. Specifically, if other files instantiate
20+
* templates or use macros or inline functions from this file, or you compile
21+
* this file and link it with other files to produce an executable, this
22+
* file does not by itself cause the resulting executable to be covered by
23+
* the GNU General Public License. This exception does not however
24+
* invalidate any other reasons why the executable file might be covered by
25+
* the GNU General Public License.
26+
*
27+
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
28+
*/
29+
30+
package test
31+
32+
import (
33+
"arduino.cc/builder"
34+
"arduino.cc/builder/constants"
35+
"arduino.cc/builder/types"
36+
"github.com/stretchr/testify/require"
37+
"io/ioutil"
38+
"os"
39+
"path/filepath"
40+
"testing"
41+
)
42+
43+
func TestUnusedCompiledLibrariesRemover(t *testing.T) {
44+
temp, err := ioutil.TempDir("", "test")
45+
NoError(t, err)
46+
defer os.RemoveAll(temp)
47+
48+
NoError(t, os.MkdirAll(filepath.Join(temp, "SPI"), os.FileMode(0755)))
49+
NoError(t, os.MkdirAll(filepath.Join(temp, "Bridge"), os.FileMode(0755)))
50+
NoError(t, ioutil.WriteFile(filepath.Join(temp, "dummy_file"), []byte{}, os.FileMode(0644)))
51+
52+
context := make(map[string]interface{})
53+
context[constants.CTX_LIBRARIES_BUILD_PATH] = temp
54+
context[constants.CTX_IMPORTED_LIBRARIES] = []*types.Library{&types.Library{Name: "Bridge"}}
55+
56+
cmd := builder.UnusedCompiledLibrariesRemover{}
57+
err = cmd.Run(context)
58+
NoError(t, err)
59+
60+
_, err = os.Stat(filepath.Join(temp, "SPI"))
61+
require.Error(t, err)
62+
require.True(t, os.IsNotExist(err))
63+
_, err = os.Stat(filepath.Join(temp, "Bridge"))
64+
NoError(t, err)
65+
_, err = os.Stat(filepath.Join(temp, "dummy_file"))
66+
NoError(t, err)
67+
}
68+
69+
func TestUnusedCompiledLibrariesRemoverLibDoesNotExist(t *testing.T) {
70+
context := make(map[string]interface{})
71+
context[constants.CTX_LIBRARIES_BUILD_PATH] = filepath.Join(os.TempDir(), "test")
72+
context[constants.CTX_IMPORTED_LIBRARIES] = []*types.Library{&types.Library{Name: "Bridge"}}
73+
74+
cmd := builder.UnusedCompiledLibrariesRemover{}
75+
err := cmd.Run(context)
76+
NoError(t, err)
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* This file is part of Arduino Builder.
3+
*
4+
* Arduino Builder is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As a special exception, you may use this file as part of a free software
19+
* library without restriction. Specifically, if other files instantiate
20+
* templates or use macros or inline functions from this file, or you compile
21+
* this file and link it with other files to produce an executable, this
22+
* file does not by itself cause the resulting executable to be covered by
23+
* the GNU General Public License. This exception does not however
24+
* invalidate any other reasons why the executable file might be covered by
25+
* the GNU General Public License.
26+
*
27+
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
28+
*/
29+
30+
package builder
31+
32+
import (
33+
"arduino.cc/builder/constants"
34+
"arduino.cc/builder/types"
35+
"arduino.cc/builder/utils"
36+
"io/ioutil"
37+
"os"
38+
"path/filepath"
39+
)
40+
41+
type UnusedCompiledLibrariesRemover struct{}
42+
43+
func (s *UnusedCompiledLibrariesRemover) Run(context map[string]interface{}) error {
44+
librariesBuildPath := context[constants.CTX_LIBRARIES_BUILD_PATH].(string)
45+
libraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library)
46+
47+
if len(libraries) == 0 {
48+
return nil
49+
}
50+
51+
_, err := os.Stat(librariesBuildPath)
52+
if err != nil && os.IsNotExist(err) {
53+
return nil
54+
}
55+
56+
libraryNames := toLibraryNames(libraries)
57+
58+
files, err := ioutil.ReadDir(librariesBuildPath)
59+
if err != nil {
60+
return utils.WrapError(err)
61+
}
62+
for _, file := range files {
63+
if file.IsDir() {
64+
if !utils.SliceContains(libraryNames, file.Name()) {
65+
err := os.RemoveAll(filepath.Join(librariesBuildPath, file.Name()))
66+
if err != nil {
67+
return utils.WrapError(err)
68+
}
69+
}
70+
}
71+
}
72+
73+
return nil
74+
}
75+
76+
func toLibraryNames(libraries []*types.Library) []string {
77+
libraryNames := []string{}
78+
for _, library := range libraries {
79+
libraryNames = append(libraryNames, library.Name)
80+
}
81+
return libraryNames
82+
}

0 commit comments

Comments
 (0)