作者:cmagli
项目:arduino-builde
func (s *AdditionalSketchFilesCopier) Run(context map[string]interface{}) error {
sketch := context[constants.CTX_SKETCH].(*types.Sketch)
sketchBuildPath := context[constants.CTX_SKETCH_BUILD_PATH].(string)
err := utils.EnsureFolderExists(sketchBuildPath)
if err != nil {
return utils.WrapError(err)
}
sketchBasePath := filepath.Dir(sketch.MainFile.Name)
for _, file := range sketch.AdditionalFiles {
relativePath, err := filepath.Rel(sketchBasePath, file.Name)
if err != nil {
return utils.WrapError(err)
}
targetFilePath := filepath.Join(sketchBuildPath, relativePath)
err = utils.EnsureFolderExists(filepath.Dir(targetFilePath))
if err != nil {
return utils.WrapError(err)
}
bytes, err := ioutil.ReadFile(file.Name)
if err != nil {
return utils.WrapError(err)
}
utils.WriteFileBytes(targetFilePath, bytes)
}
return nil
}
作者:me-no-de
项目:arduino-builde
func downloadAndUnpackCore(core Core, url string, targetPath string) error {
alreadyDownloaded, err := coreAlreadyDownloadedAndUnpacked(targetPath, core)
if err != nil {
return i18n.WrapError(err)
}
if alreadyDownloaded {
return nil
}
targetPath, err = filepath.Abs(targetPath)
if err != nil {
return i18n.WrapError(err)
}
unpackFolder, files, err := downloadAndUnpack(url)
if err != nil {
return i18n.WrapError(err)
}
defer os.RemoveAll(unpackFolder)
_, err = os.Stat(filepath.Join(targetPath, core.Maintainer, core.Arch))
if err == nil {
err = os.RemoveAll(filepath.Join(targetPath, core.Maintainer, core.Arch))
if err != nil {
return i18n.WrapError(err)
}
}
if len(files) == 1 && files[0].IsDir() {
err = utils.EnsureFolderExists(filepath.Join(targetPath, core.Maintainer))
if err != nil {
return i18n.WrapError(err)
}
err = copyRecursive(filepath.Join(unpackFolder, files[0].Name()), filepath.Join(targetPath, core.Maintainer, core.Arch))
if err != nil {
return i18n.WrapError(err)
}
} else {
err = utils.EnsureFolderExists(filepath.Join(targetPath, core.Maintainer, core.Arch))
if err != nil {
return i18n.WrapError(err)
}
for _, file := range files {
err = copyRecursive(filepath.Join(unpackFolder, file.Name()), filepath.Join(targetPath, core.Maintainer, core.Arch, file.Name()))
if err != nil {
return i18n.WrapError(err)
}
}
}
return nil
}
作者:me-no-de
项目:arduino-builde
func downloadAndUnpackTool(tool Tool, url string, targetPath string, deleteIfMissing bool) error {
if toolAlreadyDownloadedAndUnpacked(targetPath, tool) {
return nil
}
targetPath, err := filepath.Abs(targetPath)
if err != nil {
return i18n.WrapError(err)
}
unpackFolder, files, err := downloadAndUnpack(url)
if err != nil {
return i18n.WrapError(err)
}
defer os.RemoveAll(unpackFolder)
if deleteIfMissing {
_, err = os.Stat(filepath.Join(targetPath, tool.Name))
if err == nil {
err = os.RemoveAll(filepath.Join(targetPath, tool.Name))
if err != nil {
return i18n.WrapError(err)
}
}
}
if len(files) == 1 && files[0].IsDir() {
err = utils.EnsureFolderExists(filepath.Join(targetPath, tool.Name))
if err != nil {
return i18n.WrapError(err)
}
err = copyRecursive(filepath.Join(unpackFolder, files[0].Name()), filepath.Join(targetPath, tool.Name, tool.Version))
if err != nil {
return i18n.WrapError(err)
}
} else {
err = utils.EnsureFolderExists(filepath.Join(targetPath, tool.Name, tool.Version))
if err != nil {
return i18n.WrapError(err)
}
for _, file := range files {
err = copyRecursive(filepath.Join(unpackFolder, file.Name()), filepath.Join(targetPath, tool.Name, tool.Version, file.Name()))
if err != nil {
return i18n.WrapError(err)
}
}
}
return nil
}
作者:facchin
项目:arduino-builde
func (s *SketchBuilder) Run(ctx *types.Context) error {
sketchBuildPath := ctx.SketchBuildPath
buildProperties := ctx.BuildProperties
includes := ctx.IncludeFolders
includes = utils.Map(includes, utils.WrapWithHyphenI)
verbose := ctx.Verbose
warningsLevel := ctx.WarningsLevel
logger := ctx.GetLogger()
err := utils.EnsureFolderExists(sketchBuildPath)
if err != nil {
return i18n.WrapError(err)
}
var objectFiles []string
objectFiles, err = builder_utils.CompileFiles(objectFiles, sketchBuildPath, false, sketchBuildPath, buildProperties, includes, verbose, warningsLevel, logger)
if err != nil {
return i18n.WrapError(err)
}
// The "src/" subdirectory of a sketch is compiled recursively
sketchSrcPath := filepath.Join(sketchBuildPath, constants.SKETCH_FOLDER_SRC)
if info, err := os.Stat(sketchSrcPath); err == nil && info.IsDir() {
objectFiles, err = builder_utils.CompileFiles(objectFiles, sketchSrcPath, true, sketchSrcPath, buildProperties, includes, verbose, warningsLevel, logger)
if err != nil {
return i18n.WrapError(err)
}
}
ctx.SketchObjectFiles = objectFiles
return nil
}
作者:spiderkey
项目:arduino-builde
func prepareGCCPreprocRecipeProperties(context map[string]interface{}, sourceFilePath string, targetFilePath string) (props.PropertiesMap, string, error) {
if targetFilePath != utils.NULLFile() {
preprocPath := context[constants.CTX_PREPROC_PATH].(string)
err := utils.EnsureFolderExists(preprocPath)
if err != nil {
return nil, "", utils.WrapError(err)
}
targetFilePath = filepath.Join(preprocPath, targetFilePath)
}
properties := make(props.PropertiesMap)
if p, ok := context[constants.CTX_BUILD_PROPERTIES]; ok {
properties = p.(props.PropertiesMap).Clone()
}
properties[constants.BUILD_PROPERTIES_SOURCE_FILE] = sourceFilePath
properties[constants.BUILD_PROPERTIES_PREPROCESSED_FILE_PATH] = targetFilePath
includes := context[constants.CTX_INCLUDE_FOLDERS].([]string)
includes = utils.Map(includes, utils.WrapWithHyphenI)
properties[constants.BUILD_PROPERTIES_INCLUDES] = strings.Join(includes, constants.SPACE)
builder_utils.RemoveHyphenMDDFlagFromGCCCommandLine(properties)
return properties, targetFilePath, nil
}
作者:me-no-de
项目:arduino-builde
func compileFileWithRecipe(sourcePath string, source string, buildPath string, buildProperties props.PropertiesMap, includes []string, recipe string, verbose bool, warningsLevel string, logger i18n.Logger) (string, error) {
properties := buildProperties.Clone()
properties[constants.BUILD_PROPERTIES_COMPILER_WARNING_FLAGS] = properties[constants.BUILD_PROPERTIES_COMPILER_WARNING_FLAGS+"."+warningsLevel]
properties[constants.BUILD_PROPERTIES_INCLUDES] = strings.Join(includes, constants.SPACE)
properties[constants.BUILD_PROPERTIES_SOURCE_FILE] = source
relativeSource, err := filepath.Rel(sourcePath, source)
if err != nil {
return "", i18n.WrapError(err)
}
properties[constants.BUILD_PROPERTIES_OBJECT_FILE] = filepath.Join(buildPath, relativeSource+".o")
err = utils.EnsureFolderExists(filepath.Dir(properties[constants.BUILD_PROPERTIES_OBJECT_FILE]))
if err != nil {
return "", i18n.WrapError(err)
}
objIsUpToDate, err := ObjFileIsUpToDate(properties[constants.BUILD_PROPERTIES_SOURCE_FILE], properties[constants.BUILD_PROPERTIES_OBJECT_FILE], filepath.Join(buildPath, relativeSource+".d"))
if err != nil {
return "", i18n.WrapError(err)
}
if !objIsUpToDate {
_, err = ExecRecipe(properties, recipe, false, verbose, verbose, logger)
if err != nil {
return "", i18n.WrapError(err)
}
} else if verbose {
logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_USING_PREVIOUS_COMPILED_FILE, properties[constants.BUILD_PROPERTIES_OBJECT_FILE])
}
return properties[constants.BUILD_PROPERTIES_OBJECT_FILE], nil
}
作者:me-no-de
项目:arduino-builde
func (s *EnsureBuildPathExists) Run(ctx *types.Context) error {
err := utils.EnsureFolderExists(ctx.BuildPath)
if err != nil {
return i18n.WrapError(err)
}
return nil
}
作者:me-no-de
项目:arduino-builde
func (s *GCCPreprocSourceSaver) Run(ctx *types.Context) error {
preprocPath := ctx.PreprocPath
err := utils.EnsureFolderExists(preprocPath)
if err != nil {
return i18n.WrapError(err)
}
err = utils.WriteFile(filepath.Join(preprocPath, constants.FILE_GCC_PREPROC_TARGET), ctx.Source)
return i18n.WrapError(err)
}
作者:andy52
项目:arduino-builde
func (s *EnsureBuildPathExists) Run(context map[string]interface{}) error {
buildPath := context[constants.CTX_BUILD_PATH].(string)
err := utils.EnsureFolderExists(buildPath)
if err != nil {
return utils.WrapError(err)
}
return nil
}
作者:me-no-de
项目:arduino-builde
func (s *SketchSaver) Run(ctx *types.Context) error {
sketch := ctx.Sketch
sketchBuildPath := ctx.SketchBuildPath
err := utils.EnsureFolderExists(sketchBuildPath)
if err != nil {
return i18n.WrapError(err)
}
err = utils.WriteFile(filepath.Join(sketchBuildPath, filepath.Base(sketch.MainFile.Name)+".cpp"), ctx.Source)
return i18n.WrapError(err)
}
作者:andy52
项目:arduino-builde
func (s *GCCPreprocSourceSaver) Run(context map[string]interface{}) error {
preprocPath := context[constants.CTX_PREPROC_PATH].(string)
err := utils.EnsureFolderExists(preprocPath)
if err != nil {
return utils.WrapError(err)
}
source := context[constants.CTX_SOURCE].(string)
err = utils.WriteFile(filepath.Join(preprocPath, constants.FILE_GCC_PREPROC_TARGET), source)
return utils.WrapError(err)
}
作者:me-no-de
项目:arduino-builde
func downloadAndUnpackBoardsManagerTool(tool Tool, url string, targetPath string) error {
if boardManagerToolAlreadyDownloadedAndUnpacked(targetPath, tool) {
return nil
}
targetPath, err := filepath.Abs(targetPath)
if err != nil {
return i18n.WrapError(err)
}
unpackFolder, files, err := downloadAndUnpack(url)
if err != nil {
return i18n.WrapError(err)
}
defer os.RemoveAll(unpackFolder)
if len(files) == 1 && files[0].IsDir() {
err = utils.EnsureFolderExists(filepath.Join(targetPath, tool.Package, constants.FOLDER_TOOLS, tool.Name))
if err != nil {
return i18n.WrapError(err)
}
err = copyRecursive(filepath.Join(unpackFolder, files[0].Name()), filepath.Join(targetPath, tool.Package, constants.FOLDER_TOOLS, tool.Name, tool.Version))
if err != nil {
return i18n.WrapError(err)
}
} else {
err = utils.EnsureFolderExists(filepath.Join(targetPath, tool.Package, constants.FOLDER_TOOLS, tool.Name, tool.Version))
if err != nil {
return i18n.WrapError(err)
}
for _, file := range files {
err = copyRecursive(filepath.Join(unpackFolder, file.Name()), filepath.Join(targetPath, tool.Package, constants.FOLDER_TOOLS, tool.Name, tool.Version, file.Name()))
if err != nil {
return i18n.WrapError(err)
}
}
}
return nil
}
作者:andy52
项目:arduino-builde
func (s *SketchSaver) Run(context map[string]interface{}) error {
sketch := context[constants.CTX_SKETCH].(*types.Sketch)
sketchBuildPath := context[constants.CTX_SKETCH_BUILD_PATH].(string)
source := context[constants.CTX_SOURCE].(string)
err := utils.EnsureFolderExists(sketchBuildPath)
if err != nil {
return utils.WrapError(err)
}
err = utils.WriteFile(filepath.Join(sketchBuildPath, filepath.Base(sketch.MainFile.Name)+".cpp"), source)
return utils.WrapError(err)
}
作者:me-no-de
项目:arduino-builde
func (s *AdditionalSketchFilesCopier) Run(ctx *types.Context) error {
sketch := ctx.Sketch
sketchBuildPath := ctx.SketchBuildPath
err := utils.EnsureFolderExists(sketchBuildPath)
if err != nil {
return i18n.WrapError(err)
}
sketchBasePath := filepath.Dir(sketch.MainFile.Name)
for _, file := range sketch.AdditionalFiles {
relativePath, err := filepath.Rel(sketchBasePath, file.Name)
if err != nil {
return i18n.WrapError(err)
}
targetFilePath := filepath.Join(sketchBuildPath, relativePath)
err = utils.EnsureFolderExists(filepath.Dir(targetFilePath))
if err != nil {
return i18n.WrapError(err)
}
bytes, err := ioutil.ReadFile(file.Name)
if err != nil {
return i18n.WrapError(err)
}
if targetFileChanged(bytes, targetFilePath) {
err := utils.WriteFileBytes(targetFilePath, bytes)
if err != nil {
return i18n.WrapError(err)
}
}
}
return nil
}
作者:facchin
项目:arduino-builde
func compileLibrary(library *types.Library, buildPath string, buildProperties properties.Map, includes []string, verbose bool, warningsLevel string, logger i18n.Logger) ([]string, error) {
if verbose {
logger.Println(constants.LOG_LEVEL_INFO, "Compiling library \"{0}\"", library.Name)
}
libraryBuildPath := filepath.Join(buildPath, library.Name)
err := utils.EnsureFolderExists(libraryBuildPath)
if err != nil {
return nil, i18n.WrapError(err)
}
objectFiles := []string{}
if library.Layout == types.LIBRARY_RECURSIVE {
objectFiles, err = builder_utils.CompileFilesRecursive(objectFiles, library.SrcFolder, libraryBuildPath, buildProperties, includes, verbose, warningsLevel, logger)
if err != nil {
return nil, i18n.WrapError(err)
}
if library.DotALinkage {
archiveFile, err := builder_utils.ArchiveCompiledFiles(libraryBuildPath, library.Name+".a", objectFiles, buildProperties, verbose, logger)
if err != nil {
return nil, i18n.WrapError(err)
}
objectFiles = []string{archiveFile}
}
} else {
if library.UtilityFolder != "" {
includes = append(includes, utils.WrapWithHyphenI(library.UtilityFolder))
}
objectFiles, err = builder_utils.CompileFiles(objectFiles, library.SrcFolder, false, libraryBuildPath, buildProperties, includes, verbose, warningsLevel, logger)
if err != nil {
return nil, i18n.WrapError(err)
}
if library.UtilityFolder != "" {
utilityBuildPath := filepath.Join(libraryBuildPath, constants.LIBRARY_FOLDER_UTILITY)
objectFiles, err = builder_utils.CompileFiles(objectFiles, library.UtilityFolder, false, utilityBuildPath, buildProperties, includes, verbose, warningsLevel, logger)
if err != nil {
return nil, i18n.WrapError(err)
}
}
}
return objectFiles, nil
}
作者:andy52
项目:arduino-builde
func compileLibrary(library *types.Library, buildPath string, buildProperties map[string]string, includes []string, verbose bool, warningsLevel string, logger i18n.Logger) ([]string, error) {
libraryBuildPath := filepath.Join(buildPath, library.Name)
err := utils.EnsureFolderExists(libraryBuildPath)
if err != nil {
return nil, utils.WrapError(err)
}
objectFiles := []string{}
if library.Layout == types.LIBRARY_RECURSIVE {
objectFiles, err = builder_utils.CompileFilesRecursive(objectFiles, library.SrcFolder, libraryBuildPath, buildProperties, includes, verbose, warningsLevel, logger)
if err != nil {
return nil, utils.WrapError(err)
}
if library.DotALinkage {
archiveFile, err := builder_utils.ArchiveCompiledFiles(libraryBuildPath, library.Name+".a", objectFiles, buildProperties, verbose, logger)
if err != nil {
return nil, utils.WrapError(err)
}
objectFiles = []string{archiveFile}
}
} else {
utilitySourcePath := filepath.Join(library.SrcFolder, constants.LIBRARY_FOLDER_UTILITY)
_, utilitySourcePathErr := os.Stat(utilitySourcePath)
if utilitySourcePathErr == nil {
includes = append(includes, utils.WrapWithHyphenI(utilitySourcePath))
}
objectFiles, err = builder_utils.CompileFiles(objectFiles, library.SrcFolder, false, libraryBuildPath, buildProperties, includes, verbose, warningsLevel, logger)
if err != nil {
return nil, utils.WrapError(err)
}
if utilitySourcePathErr == nil {
utilityBuildPath := filepath.Join(libraryBuildPath, constants.LIBRARY_FOLDER_UTILITY)
objectFiles, err = builder_utils.CompileFiles(objectFiles, utilitySourcePath, false, utilityBuildPath, buildProperties, includes, verbose, warningsLevel, logger)
if err != nil {
return nil, utils.WrapError(err)
}
}
}
return objectFiles, nil
}
作者:me-no-de
项目:arduino-builde
func (s *CTagsTargetFileSaver) Run(ctx *types.Context) error {
source := *s.Source
preprocPath := ctx.PreprocPath
err := utils.EnsureFolderExists(preprocPath)
if err != nil {
return i18n.WrapError(err)
}
ctagsTargetFilePath := filepath.Join(preprocPath, s.TargetFileName)
err = utils.WriteFile(ctagsTargetFilePath, source)
if err != nil {
return i18n.WrapError(err)
}
ctx.CTagsTargetFile = ctagsTargetFilePath
return nil
}
作者:andy52
项目:arduino-builde
func (s *CTagsTargetFileSaver) Run(context map[string]interface{}) error {
source := context[s.SourceField].(string)
preprocPath := context[constants.CTX_PREPROC_PATH].(string)
err := utils.EnsureFolderExists(preprocPath)
if err != nil {
return utils.WrapError(err)
}
ctagsTargetFilePath := filepath.Join(preprocPath, s.TargetFileName)
err = utils.WriteFile(ctagsTargetFilePath, source)
if err != nil {
return utils.WrapError(err)
}
context[constants.CTX_CTAGS_TEMP_FILE_PATH] = ctagsTargetFilePath
return nil
}
作者:spiderkey
项目:arduino-builde
func TestMergeSketchWithBootloaderSketchInBuildPath(t *testing.T) {
DownloadCoresAndToolsAndLibraries(t)
context := make(map[string]interface{})
buildPath := SetupBuildPath(t, context)
defer os.RemoveAll(buildPath)
context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}
context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"}
context[constants.CTX_FQBN] = "arduino:avr:uno"
context[constants.CTX_BUILT_IN_LIBRARIES_FOLDERS] = []string{"downloaded_libraries"}
context[constants.CTX_OTHER_LIBRARIES_FOLDERS] = []string{"libraries"}
context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch1", "sketch.ino")
context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600"
err := utils.EnsureFolderExists(filepath.Join(buildPath, "sketch"))
NoError(t, err)
fakeSketchHex := "row 1\n" +
"row 2\n"
err = utils.WriteFile(filepath.Join(buildPath, "sketch.ino.hex"), fakeSketchHex)
NoError(t, err)
commands := []types.Command{
&builder.SetupHumanLoggerIfMissing{},
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
&builder.MergeSketchWithBootloader{},
}
for _, command := range commands {
err := command.Run(context)
NoError(t, err)
}
bytes, err := ioutil.ReadFile(filepath.Join(buildPath, "sketch.ino.with_bootloader.hex"))
NoError(t, err)
mergedSketchHex := string(bytes)
require.True(t, strings.HasPrefix(mergedSketchHex, "row 1\n:107E0000112484B714BE81FFF0D085E080938100F7\n"))
require.True(t, strings.HasSuffix(mergedSketchHex, ":0400000300007E007B\n:00000001FF\n"))
}
作者:facchin
项目:arduino-builde
func prepareGCCPreprocRecipeProperties(ctx *types.Context, sourceFilePath string, targetFilePath string, includes []string) (properties.Map, string, error) {
if targetFilePath != utils.NULLFile() {
preprocPath := ctx.PreprocPath
err := utils.EnsureFolderExists(preprocPath)
if err != nil {
return nil, "", i18n.WrapError(err)
}
targetFilePath = filepath.Join(preprocPath, targetFilePath)
}
properties := ctx.BuildProperties.Clone()
properties[constants.BUILD_PROPERTIES_SOURCE_FILE] = sourceFilePath
properties[constants.BUILD_PROPERTIES_PREPROCESSED_FILE_PATH] = targetFilePath
includes = utils.Map(includes, utils.WrapWithHyphenI)
properties[constants.BUILD_PROPERTIES_INCLUDES] = strings.Join(includes, constants.SPACE)
builder_utils.RemoveHyphenMDDFlagFromGCCCommandLine(properties)
return properties, targetFilePath, nil
}