Skip to content

Commit 35bdf40

Browse files
author
Federico Fissore
committed
Added build_utils.ArchiveCompiledFiles and changed core_builder
Moved some string literals into constants Added a patch to AVR core (it's temporary: master AVR core is already patched but not released) Signed-off-by: Federico Fissore <[email protected]>
1 parent 00364c0 commit 35bdf40

File tree

15 files changed

+208
-88
lines changed

15 files changed

+208
-88
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,29 @@ func objFileIsUpToDateWithSourceFile(sourceFileStat, objectFileStat os.FileInfo)
158158
return objectFileStat != nil && sourceFileStat.ModTime().Before(objectFileStat.ModTime())
159159
}
160160

161+
func ArchiveCompiledFiles(buildPath string, archiveFile string, objectFiles []string, buildProperties map[string]string, verbose bool, logger i18n.Logger) (string, error) {
162+
archiveFilePath := filepath.Join(buildPath, archiveFile)
163+
if _, err := os.Stat(archiveFilePath); err == nil {
164+
err = os.Remove(archiveFilePath)
165+
if err != nil {
166+
return "", utils.WrapError(err)
167+
}
168+
}
169+
170+
for _, objectFile := range objectFiles {
171+
properties := utils.MergeMapsOfStrings(make(map[string]string), buildProperties)
172+
properties[constants.BUILD_PROPERTIES_ARCHIVE_FILE] = filepath.Base(archiveFilePath)
173+
properties[constants.BUILD_PROPERTIES_OBJECT_FILE] = objectFile
174+
175+
_, err := ExecRecipe(properties, constants.RECIPE_AR_PATTERN, false, verbose, verbose, logger)
176+
if err != nil {
177+
return "", utils.WrapError(err)
178+
}
179+
}
180+
181+
return archiveFilePath, nil
182+
}
183+
161184
func ExecRecipe(properties map[string]string, recipe string, removeUnsetProperties bool, echoCommandLine bool, echoOutput bool, logger i18n.Logger) ([]byte, error) {
162185
pattern := properties[recipe]
163186
if pattern == constants.EMPTY_STRING {

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ package constants
3232
const BOARD_PROPERTIES_MENU = "menu"
3333
const BUILD_OPTIONS_FILE = "build.options.json"
3434
const BUILD_PROPERTIES_ARCHIVE_FILE = "archive_file"
35+
const BUILD_PROPERTIES_ARCHIVE_FILE_PATH = "archive_file_path"
3536
const BUILD_PROPERTIES_ARCH_OVERRIDE_CHECK = "architecture.override_check"
3637
const BUILD_PROPERTIES_BOOTLOADER_FILE = "bootloader.file"
3738
const BUILD_PROPERTIES_BOOTLOADER_NOBLINK = "bootloader.noblink"
@@ -45,6 +46,7 @@ const BUILD_PROPERTIES_BUILD_PROJECT_NAME = "build.project_name"
4546
const BUILD_PROPERTIES_BUILD_SYSTEM_PATH = "build.system.path"
4647
const BUILD_PROPERTIES_BUILD_VARIANT = "build.variant"
4748
const BUILD_PROPERTIES_BUILD_VARIANT_PATH = "build.variant.path"
49+
const BUILD_PROPERTIES_COMPILER_C_ELF_FLAGS = "compiler.c.elf.flags"
4850
const BUILD_PROPERTIES_COMPILER_CPP_FLAGS = "compiler.cpp.flags"
4951
const BUILD_PROPERTIES_COMPILER_PATH = "compiler.path"
5052
const BUILD_PROPERTIES_COMPILER_WARNING_FLAGS = "compiler.warning_flags"
@@ -70,6 +72,7 @@ const BUILD_PROPERTIES_VID = "vid"
7072
const COAN = "coan"
7173
const CTAGS = "ctags"
7274
const CTX_ACTUAL_PLATFORM = "actualPlatform"
75+
const CTX_ARCHIVE_FILE_PATH_CORE = "archiveFileCore"
7376
const CTX_BUILD_CORE = "buildCore"
7477
const CTX_BUILD_OPTIONS = "buildOptions"
7578
const CTX_BUILD_OPTIONS_JSON = "buildOptionsJson"
@@ -99,7 +102,6 @@ const CTX_LIBRARIES = "libraries"
99102
const CTX_LIBRARY_DISCOVERY_RECURSION_DEPTH = "libraryDiscoveryRecursionDepth"
100103
const CTX_LINE_OFFSET = "lineOffset"
101104
const CTX_LOGGER = "logger"
102-
const CTX_OBJECT_FILES_CORE = "objectFilesCore"
103105
const CTX_OBJECT_FILES_LIBRARIES = "objectFilesLibraries"
104106
const CTX_OBJECT_FILES_SKETCH = "objectFilesSketch"
105107
const CTX_PLATFORM_KEYS_REWRITE = "platformKeysRewrite"
@@ -173,6 +175,7 @@ const LIBRARY_ALL_ARCHS = "*"
173175
const LIBRARY_ARCHITECTURES = "architectures"
174176
const LIBRARY_AUTHOR = "author"
175177
const LIBRARY_CATEGORY = "category"
178+
const LIBRARY_DOT_A_LINKAGE = "dot_a_linkage"
176179
const LIBRARY_EMAIL = "email"
177180
const LIBRARY_FOLDER_ARCH = "arch"
178181
const LIBRARY_FOLDER_SRC = "src"
@@ -230,6 +233,8 @@ const PLATFORM_REWRITE_OLD = "old"
230233
const PLATFORM_URL = "url"
231234
const PLATFORM_VERSION = "version"
232235
const PROGRAMMER_NAME = "name"
236+
const RECIPE_AR_PATTERN = "recipe.ar.pattern"
237+
const RECIPE_C_COMBINE_PATTERN = "recipe.c.combine.pattern"
233238
const RECIPE_C_PATTERN = "recipe.c.o.pattern"
234239
const RECIPE_CPP_PATTERN = "recipe.cpp.o.pattern"
235240
const RECIPE_PREPROC_INCLUDES = "recipe.preproc.includes"

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (s *IncludesFinderWithGCC) Run(context map[string]interface{}) error {
4848
verbose := context[constants.CTX_VERBOSE].(bool)
4949
logger := context[constants.CTX_LOGGER].(i18n.Logger)
5050

51-
includesParams := ""
51+
includesParams := constants.EMPTY_STRING
5252
if utils.MapHas(context, constants.CTX_INCLUDE_FOLDERS) {
5353
includes := context[constants.CTX_INCLUDE_FOLDERS].([]string)
5454
includes = utils.Map(includes, utils.WrapWithHyphenI)

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

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ func makeNewLibrary(libraryFolder string, debugLevel int, logger i18n.Logger) (*
191191
library.Paragraph = strings.TrimSpace(properties[constants.LIBRARY_PARAGRAPH])
192192
library.URL = strings.TrimSpace(properties[constants.LIBRARY_URL])
193193
library.IsLegacy = false
194+
library.DotALinkage = strings.TrimSpace(properties[constants.LIBRARY_DOT_A_LINKAGE]) == "true"
194195

195196
return library, nil
196197
}

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

+9-26
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ import (
3535
"arduino.cc/builder/i18n"
3636
"arduino.cc/builder/utils"
3737
"os"
38-
"path/filepath"
39-
"strings"
4038
)
4139

4240
type CoreBuilder struct{}
@@ -53,17 +51,17 @@ func (s *CoreBuilder) Run(context map[string]interface{}) error {
5351
return utils.WrapError(err)
5452
}
5553

56-
objectFiles, err := compileCore(coreBuildPath, buildProperties, verbose, warningsLevel, logger)
54+
archiveFile, err := compileCore(coreBuildPath, buildProperties, verbose, warningsLevel, logger)
5755
if err != nil {
5856
return utils.WrapError(err)
5957
}
6058

61-
context[constants.CTX_OBJECT_FILES_CORE] = objectFiles
59+
context[constants.CTX_ARCHIVE_FILE_PATH_CORE] = archiveFile
6260

6361
return nil
6462
}
6563

66-
func compileCore(buildPath string, buildProperties map[string]string, verbose bool, warningsLevel string, logger i18n.Logger) ([]string, error) {
64+
func compileCore(buildPath string, buildProperties map[string]string, verbose bool, warningsLevel string, logger i18n.Logger) (string, error) {
6765
var objectFiles []string
6866
coreFolder := buildProperties[constants.BUILD_PROPERTIES_BUILD_CORE_PATH]
6967
variantFolder := buildProperties[constants.BUILD_PROPERTIES_BUILD_VARIANT_PATH]
@@ -80,34 +78,19 @@ func compileCore(buildPath string, buildProperties map[string]string, verbose bo
8078
if variantFolder != constants.EMPTY_STRING {
8179
objectFiles, err = builder_utils.CompileFiles(objectFiles, variantFolder, true, buildPath, buildProperties, includes, verbose, warningsLevel, logger)
8280
if err != nil {
83-
return nil, utils.WrapError(err)
81+
return "", utils.WrapError(err)
8482
}
8583
}
8684

8785
coreObjectFiles, err := builder_utils.CompileFiles([]string{}, coreFolder, true, buildPath, buildProperties, includes, verbose, warningsLevel, logger)
8886
if err != nil {
89-
return nil, utils.WrapError(err)
87+
return "", utils.WrapError(err)
9088
}
9189

92-
coreArchiveFilePath := filepath.Join(buildPath, "core.a")
93-
if _, err := os.Stat(coreArchiveFilePath); err == nil {
94-
err = os.Remove(coreArchiveFilePath)
95-
if err != nil {
96-
return nil, utils.WrapError(err)
97-
}
98-
}
99-
100-
for _, coreObjectFile := range coreObjectFiles {
101-
properties := utils.MergeMapsOfStrings(make(map[string]string), buildProperties)
102-
properties[constants.BUILD_PROPERTIES_INCLUDES] = strings.Join(includes, constants.SPACE)
103-
properties[constants.BUILD_PROPERTIES_ARCHIVE_FILE] = filepath.Base(coreArchiveFilePath)
104-
properties[constants.BUILD_PROPERTIES_OBJECT_FILE] = coreObjectFile
105-
106-
_, err := builder_utils.ExecRecipe(properties, "recipe.ar.pattern", false, verbose, verbose, logger)
107-
if err != nil {
108-
return nil, utils.WrapError(err)
109-
}
90+
archiveFile, err := builder_utils.ArchiveCompiledFiles(buildPath, "core.a", coreObjectFiles, buildProperties, verbose, logger)
91+
if err != nil {
92+
return "", utils.WrapError(err)
11093
}
11194

112-
return objectFiles, nil
95+
return archiveFile, nil
11396
}

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

+18-12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"arduino.cc/builder/constants"
3535
"arduino.cc/builder/i18n"
3636
"arduino.cc/builder/utils"
37+
"path/filepath"
3738
"strings"
3839
)
3940

@@ -42,45 +43,50 @@ type Linker struct{}
4243
func (s *Linker) Run(context map[string]interface{}) error {
4344
objectFilesSketch := context[constants.CTX_OBJECT_FILES_SKETCH].([]string)
4445
objectFilesLibraries := context[constants.CTX_OBJECT_FILES_LIBRARIES].([]string)
45-
objectFilesCore := context[constants.CTX_OBJECT_FILES_CORE].([]string)
4646

4747
var objectFiles []string
4848
objectFiles = append(objectFiles, objectFilesSketch...)
4949
objectFiles = append(objectFiles, objectFilesLibraries...)
50-
objectFiles = append(objectFiles, objectFilesCore...)
50+
51+
coreArchiveFilePath := context[constants.CTX_ARCHIVE_FILE_PATH_CORE].(string)
5152

5253
buildProperties := context[constants.CTX_BUILD_PROPERTIES].(map[string]string)
5354
verbose := context[constants.CTX_VERBOSE].(bool)
5455
warningsLevel := context[constants.CTX_WARNINGS_LEVEL].(string)
5556
logger := context[constants.CTX_LOGGER].(i18n.Logger)
5657

57-
err := link(objectFiles, buildProperties, verbose, warningsLevel, logger)
58+
err := link(objectFiles, coreArchiveFilePath, buildProperties, verbose, warningsLevel, logger)
5859
if err != nil {
5960
return utils.WrapError(err)
6061
}
6162

6263
return nil
6364
}
6465

65-
func link(objectFiles []string, buildProperties map[string]string, verbose bool, warningsLevel string, logger i18n.Logger) error {
66-
optRelax := constants.EMPTY_STRING
67-
if buildProperties[constants.BUILD_PROPERTIES_BUILD_MCU] == "atmega2560" {
68-
optRelax = ",--relax"
69-
}
66+
func link(objectFiles []string, coreArchiveFilePath string, buildProperties map[string]string, verbose bool, warningsLevel string, logger i18n.Logger) error {
67+
optRelax := addRelaxTrickIfATMEGA2560(buildProperties)
7068

7169
objectFiles = utils.Map(objectFiles, wrapWithDoubleQuotes)
7270
objectFileList := strings.Join(objectFiles, constants.SPACE)
7371

7472
properties := utils.MergeMapsOfStrings(make(map[string]string), buildProperties)
75-
properties["compiler.c.elf.flags"] = properties["compiler.c.elf.flags"] + optRelax
76-
properties["compiler.warning_flags"] = properties["compiler.warning_flags."+warningsLevel]
77-
properties[constants.BUILD_PROPERTIES_ARCHIVE_FILE] = "core.a"
73+
properties[constants.BUILD_PROPERTIES_COMPILER_C_ELF_FLAGS] = properties[constants.BUILD_PROPERTIES_COMPILER_C_ELF_FLAGS] + optRelax
74+
properties[constants.BUILD_PROPERTIES_COMPILER_WARNING_FLAGS] = properties[constants.BUILD_PROPERTIES_COMPILER_WARNING_FLAGS+"."+warningsLevel]
75+
properties[constants.BUILD_PROPERTIES_ARCHIVE_FILE] = filepath.Base(coreArchiveFilePath)
76+
properties[constants.BUILD_PROPERTIES_ARCHIVE_FILE_PATH] = coreArchiveFilePath
7877
properties[constants.BUILD_PROPERTIES_OBJECT_FILES] = objectFileList
7978

80-
_, err := builder_utils.ExecRecipe(properties, "recipe.c.combine.pattern", false, verbose, verbose, logger)
79+
_, err := builder_utils.ExecRecipe(properties, constants.RECIPE_C_COMBINE_PATTERN, false, verbose, verbose, logger)
8180
return err
8281
}
8382

8483
func wrapWithDoubleQuotes(value string) string {
8584
return "\"" + value + "\""
8685
}
86+
87+
func addRelaxTrickIfATMEGA2560(buildProperties map[string]string) string {
88+
if buildProperties[constants.BUILD_PROPERTIES_BUILD_MCU] == "atmega2560" {
89+
return ",--relax"
90+
}
91+
return constants.EMPTY_STRING
92+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func loadSingleLine(properties map[string]string, line string) {
9191
key := strings.TrimSpace(lineParts[0])
9292
value := strings.TrimSpace(lineParts[1])
9393

94-
key = strings.Replace(key, "."+OSNAME, "", 1)
94+
key = strings.Replace(key, "."+OSNAME, constants.EMPTY_STRING, 1)
9595
properties[key] = value
9696
}
9797
}

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

+23
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,26 @@ func TestBuilderSketchNoFunctions(t *testing.T) {
264264
err := command.Run(context)
265265
require.Error(t, err)
266266
}
267+
268+
/*
269+
func TestBuilderALinkage(t *testing.T) {
270+
DownloadCoresAndToolsAndLibraries(t)
271+
272+
context := make(map[string]interface{})
273+
274+
buildPath := SetupBuildPath(t, context)
275+
defer os.RemoveAll(buildPath)
276+
277+
context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "downloaded_board_manager_stuff"}
278+
context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools", "downloaded_board_manager_stuff"}
279+
context[constants.CTX_LIBRARIES_FOLDERS] = []string{"libraries", "downloaded_libraries"}
280+
context[constants.CTX_FQBN] = "arduino:avr:leonardo"
281+
context[constants.CTX_SKETCH_LOCATION] = filepath.Join("downloaded_libraries", "HID", "examples", "NKROKeyboard", "NKROKeyboard.ino")
282+
context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600"
283+
context[constants.CTX_VERBOSE] = true
284+
285+
command := builder.Builder{}
286+
err := command.Run(context)
287+
NoError(t, err)
288+
}
289+
*/
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
A*
22
B*
33
C*
4+
H*
45
R*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--- downloaded_hardware/arduino/avr/platform.txt 2015-09-10 17:14:15.137942087 +0200
2+
+++ downloaded_hardware/arduino/avr/platform.txt 2015-09-23 10:27:19.522085331 +0200
3+
@@ -77,6 +77,12 @@
4+
recipe.size.regex.data=^(?:\.data|\.bss|\.noinit)\s+([0-9]+).*
5+
recipe.size.regex.eeprom=^(?:\.eeprom)\s+([0-9]+).*
6+
7+
+## Preprocessor
8+
+preproc.includes.flags=-w -x c++ -M -MG -MP
9+
+recipe.preproc.includes="{compiler.path}{compiler.cpp.cmd}" {compiler.cpp.flags} {preproc.includes.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.cpp.extra_flags} {build.extra_flags} {includes} "{source_file}"
10+
+
11+
+preproc.macros.flags=-w -x c++ -E -CC
12+
+recipe.preproc.macros="{compiler.path}{compiler.cpp.cmd}" {compiler.cpp.flags} {preproc.macros.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.cpp.extra_flags} {build.extra_flags} {includes} "{source_file}"
13+
14+
# AVR Uploader/Programmers tools
15+
# ------------------------------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--- downloaded_libraries/HID/library.properties 2015-09-23 09:49:20.470004391 +0200
2+
+++ downloaded_libraries/HID/library.properties.new 2015-09-23 11:04:13.206163949 +0200
3+
@@ -7,4 +7,4 @@
4+
category=Device Control
5+
url=https://github.com/NicoHood/HID
6+
architectures=*
7+
-alinkage=true
8+
+dot_a_linkage=true

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

+27
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const HARDWARE_FOLDER = "downloaded_hardware"
5151
const BOARD_MANAGER_FOLDER = "downloaded_board_manager_stuff"
5252
const TOOLS_FOLDER = "downloaded_tools"
5353
const LIBRARIES_FOLDER = "downloaded_libraries"
54+
const PATCHES_FOLDER = "downloaded_stuff_patches"
5455

5556
type Tool struct {
5657
Name string
@@ -67,6 +68,7 @@ type OsUrl struct {
6768
type Library struct {
6869
Name string
6970
Version string
71+
Url string
7072
}
7173

7274
type Core struct {
@@ -125,8 +127,30 @@ func DownloadCoresAndToolsAndLibraries(t *testing.T) {
125127
Library{Name: "Bridge", Version: "1.0.7"},
126128
Library{Name: "CapacitiveSensor", Version: "0.5.0"},
127129
Library{Name: "Robot IR Remote", Version: "1.0.2"},
130+
//Library{Name: "HID", Version: "0.0.0", Url: "https://github.com/NicoHood/HID/archive/dev_2_4.zip"},
128131
}
129132

133+
download(t, cores, boardsManagerCores, boardsManagerRedBearCores, tools, boardsManagerTools, boardsManagerRFduinoTools, libraries)
134+
135+
patch(t)
136+
}
137+
138+
// FIXME: once patched cores are released, patching them will be unnecessary
139+
func patch(t *testing.T) {
140+
files, err := ioutil.ReadDir(PATCHES_FOLDER)
141+
NoError(t, err)
142+
143+
for _, file := range files {
144+
if filepath.Ext(file.Name()) == ".patch" {
145+
cmd := exec.Command("patch", "-N", "-p0", "-r", "-", "-i", filepath.Join(PATCHES_FOLDER, file.Name()))
146+
cmd.CombinedOutput()
147+
//output, _ := cmd.CombinedOutput()
148+
//fmt.Println(string(output))
149+
}
150+
}
151+
}
152+
153+
func download(t *testing.T, cores, boardsManagerCores, boardsManagerRedBearCores []Core, tools, boardsManagerTools, boardsManagerRFduinoTools []Tool, libraries []Library) {
130154
if allCoresAlreadyDownloadedAndUnpacked(HARDWARE_FOLDER, cores) &&
131155
allBoardsManagerCoresAlreadyDownloadedAndUnpacked(BOARD_MANAGER_FOLDER, boardsManagerCores) &&
132156
allBoardsManagerCoresAlreadyDownloadedAndUnpacked(BOARD_MANAGER_FOLDER, boardsManagerRedBearCores) &&
@@ -626,6 +650,9 @@ func downloadLibraries(libraries []Library, index map[string]interface{}) error
626650
}
627651

628652
func findLibraryUrl(index map[string]interface{}, library Library) (string, error) {
653+
if library.Url != "" {
654+
return library.Url, nil
655+
}
629656
libs := index["libraries"].([]interface{})
630657
for _, l := range libs {
631658
lib := l.(map[string]interface{})

0 commit comments

Comments
 (0)