作者:me-no-de
项目:arduino-builde
func (s *ToolsLoader) Run(ctx *types.Context) error {
folders := ctx.ToolsFolders
tools := []*types.Tool{}
for _, folder := range folders {
builtinToolsVersionsFile, err := findBuiltinToolsVersionsFile(folder)
if err != nil {
return i18n.WrapError(err)
}
if builtinToolsVersionsFile != constants.EMPTY_STRING {
err = loadToolsFrom(&tools, builtinToolsVersionsFile)
if err != nil {
return i18n.WrapError(err)
}
} else {
subfolders, err := collectAllToolsFolders(folder)
if err != nil {
return i18n.WrapError(err)
}
for _, subfolder := range subfolders {
err = loadToolsFromFolderStructure(&tools, subfolder)
if err != nil {
return i18n.WrapError(err)
}
}
}
}
ctx.Tools = tools
return nil
}
作者:me-no-de
项目:arduino-builde
func loadToolsFrom(tools *[]*types.Tool, builtinToolsVersionsFilePath string) error {
rows, err := utils.ReadFileToRows(builtinToolsVersionsFilePath)
if err != nil {
return i18n.WrapError(err)
}
folder, err := filepath.Abs(filepath.Dir(builtinToolsVersionsFilePath))
if err != nil {
return i18n.WrapError(err)
}
for _, row := range rows {
row = strings.TrimSpace(row)
if row != constants.EMPTY_STRING {
rowParts := strings.Split(row, "=")
toolName := strings.Split(rowParts[0], ".")[1]
toolVersion := rowParts[1]
if !toolsSliceContains(tools, toolName, toolVersion) {
*tools = append(*tools, &types.Tool{Name: toolName, Version: toolVersion, Folder: folder})
}
}
}
return nil
}
作者:me-no-de
项目:arduino-builde
func collectAllToolsFolders(from string) ([]string, error) {
folders := []string{}
walkFunc := func(currentPath string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if !info.IsDir() {
return nil
}
rel, err := filepath.Rel(from, currentPath)
if err != nil {
return i18n.WrapError(err)
}
depth := len(strings.Split(rel, string(os.PathSeparator)))
if info.Name() == constants.FOLDER_TOOLS && depth == 2 {
folders = append(folders, currentPath)
} else if depth > 2 {
return filepath.SkipDir
}
return nil
}
err := gohasissues.Walk(from, walkFunc)
if len(folders) == 0 {
folders = append(folders, from)
}
return folders, i18n.WrapError(err)
}
作者: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
}
作者:me-no-de
项目:arduino-builde
func findFilesInFolder(sourcePath string, extension string, recurse bool) ([]string, error) {
files, err := utils.ReadDirFiltered(sourcePath, utils.FilterFilesWithExtension(extension))
if err != nil {
return nil, i18n.WrapError(err)
}
var sources []string
for _, file := range files {
sources = append(sources, filepath.Join(sourcePath, file.Name()))
}
if recurse {
folders, err := utils.ReadDirFiltered(sourcePath, utils.FilterDirs)
if err != nil {
return nil, i18n.WrapError(err)
}
for _, folder := range folders {
otherSources, err := findFilesInFolder(filepath.Join(sourcePath, folder.Name()), extension, recurse)
if err != nil {
return nil, i18n.WrapError(err)
}
sources = append(sources, otherSources...)
}
}
return sources, nil
}
作者:me-no-de
项目:arduino-builde
func findIncludesUntilDone(ctx *types.Context, sourceFilePath string) error {
targetFilePath := utils.NULLFile()
importedLibraries := ctx.ImportedLibraries
done := false
for !done {
commands := []types.Command{
&GCCPreprocRunnerForDiscoveringIncludes{SourceFilePath: sourceFilePath, TargetFilePath: targetFilePath},
&IncludesFinderWithRegExp{Source: &ctx.SourceGccMinusE},
&IncludesToIncludeFolders{},
}
for _, command := range commands {
err := runCommand(ctx, command)
if err != nil {
return i18n.WrapError(err)
}
}
if len(ctx.IncludesJustFound) == 0 {
done = true
} else if len(ctx.ImportedLibraries) == len(importedLibraries) {
err := runCommand(ctx, &GCCPreprocRunner{TargetFileName: constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E})
return i18n.WrapError(err)
}
importedLibraries = ctx.ImportedLibraries
ctx.IncludesJustFound = []string{}
}
return nil
}
作者:facchin
项目:arduino-builde
func (s *Linker) Run(ctx *types.Context) error {
objectFilesSketch := ctx.SketchObjectFiles
objectFilesLibraries := ctx.LibrariesObjectFiles
objectFilesCore := ctx.CoreObjectsFiles
var objectFiles []string
objectFiles = append(objectFiles, objectFilesSketch...)
objectFiles = append(objectFiles, objectFilesLibraries...)
objectFiles = append(objectFiles, objectFilesCore...)
coreArchiveFilePath := ctx.CoreArchiveFilePath
buildPath := ctx.BuildPath
coreDotARelPath, err := filepath.Rel(buildPath, coreArchiveFilePath)
if err != nil {
return i18n.WrapError(err)
}
buildProperties := ctx.BuildProperties
verbose := ctx.Verbose
warningsLevel := ctx.WarningsLevel
logger := ctx.GetLogger()
err = link(objectFiles, coreDotARelPath, coreArchiveFilePath, buildProperties, verbose, warningsLevel, logger)
if err != nil {
return i18n.WrapError(err)
}
return nil
}
作者:me-no-de
项目:arduino-builde
func (s *UnusedCompiledLibrariesRemover) Run(ctx *types.Context) error {
librariesBuildPath := ctx.LibrariesBuildPath
_, err := os.Stat(librariesBuildPath)
if err != nil && os.IsNotExist(err) {
return nil
}
libraryNames := toLibraryNames(ctx.ImportedLibraries)
files, err := ioutil.ReadDir(librariesBuildPath)
if err != nil {
return i18n.WrapError(err)
}
for _, file := range files {
if file.IsDir() {
if !utils.SliceContains(libraryNames, file.Name()) {
err := os.RemoveAll(filepath.Join(librariesBuildPath, file.Name()))
if err != nil {
return i18n.WrapError(err)
}
}
}
}
return nil
}
作者:me-no-de
项目:arduino-builde
func (s *CTagsRunner) Run(ctx *types.Context) error {
buildProperties := ctx.BuildProperties
ctagsTargetFilePath := ctx.CTagsTargetFile
logger := ctx.GetLogger()
properties := buildProperties.Clone()
properties.Merge(buildProperties.SubTree(constants.BUILD_PROPERTIES_TOOLS_KEY).SubTree(constants.CTAGS))
properties[constants.BUILD_PROPERTIES_SOURCE_FILE] = ctagsTargetFilePath
pattern := properties[constants.BUILD_PROPERTIES_PATTERN]
if pattern == constants.EMPTY_STRING {
return i18n.ErrorfWithLogger(logger, constants.MSG_PATTERN_MISSING, constants.CTAGS)
}
commandLine := properties.ExpandPropsInString(pattern)
command, err := utils.PrepareCommand(commandLine, logger)
if err != nil {
return i18n.WrapError(err)
}
verbose := ctx.Verbose
if verbose {
fmt.Println(commandLine)
}
sourceBytes, err := command.Output()
if err != nil {
return i18n.WrapError(err)
}
ctx.CTagsOutput = string(sourceBytes)
return nil
}
作者:me-no-de
项目:arduino-builde
func compileCore(buildPath string, buildProperties props.PropertiesMap, verbose bool, warningsLevel string, logger i18n.Logger) (string, []string, error) {
coreFolder := buildProperties[constants.BUILD_PROPERTIES_BUILD_CORE_PATH]
variantFolder := buildProperties[constants.BUILD_PROPERTIES_BUILD_VARIANT_PATH]
includes := []string{}
includes = append(includes, coreFolder)
if variantFolder != constants.EMPTY_STRING {
includes = append(includes, variantFolder)
}
includes = utils.Map(includes, utils.WrapWithHyphenI)
var err error
variantObjectFiles := []string{}
if variantFolder != constants.EMPTY_STRING {
variantObjectFiles, err = builder_utils.CompileFiles(variantObjectFiles, variantFolder, true, buildPath, buildProperties, includes, verbose, warningsLevel, logger)
if err != nil {
return "", nil, i18n.WrapError(err)
}
}
coreObjectFiles, err := builder_utils.CompileFiles([]string{}, coreFolder, true, buildPath, buildProperties, includes, verbose, warningsLevel, logger)
if err != nil {
return "", nil, i18n.WrapError(err)
}
archiveFile, err := builder_utils.ArchiveCompiledFiles(buildPath, "core.a", coreObjectFiles, buildProperties, verbose, logger)
if err != nil {
return "", nil, i18n.WrapError(err)
}
return archiveFile, variantObjectFiles, nil
}
作者:me-no-de
项目:arduino-builde
func loadBoards(boards map[string]*types.Board, packageId string, platformId string, folder string, logger i18n.Logger) error {
properties, err := props.Load(filepath.Join(folder, constants.FILE_BOARDS_TXT), logger)
if err != nil {
return i18n.WrapError(err)
}
localProperties, err := props.SafeLoad(filepath.Join(folder, constants.FILE_BOARDS_LOCAL_TXT), logger)
if err != nil {
return i18n.WrapError(err)
}
properties = properties.Merge(localProperties)
propertiesByBoardId := properties.FirstLevelOf()
delete(propertiesByBoardId, constants.BOARD_PROPERTIES_MENU)
for boardId, properties := range propertiesByBoardId {
properties[constants.ID] = boardId
board := getOrCreateBoard(boards, boardId)
board.Properties.Merge(properties)
boards[boardId] = board
}
return 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 PrepareCommandForRecipe(properties props.PropertiesMap, recipe string, removeUnsetProperties bool, echoCommandLine bool, echoOutput bool, logger i18n.Logger) (*exec.Cmd, error) {
pattern := properties[recipe]
if pattern == constants.EMPTY_STRING {
return nil, i18n.ErrorfWithLogger(logger, constants.MSG_PATTERN_MISSING, recipe)
}
var err error
commandLine := properties.ExpandPropsInString(pattern)
if removeUnsetProperties {
commandLine, err = props.DeleteUnexpandedPropsFromString(commandLine)
if err != nil {
return nil, i18n.WrapError(err)
}
}
command, err := utils.PrepareCommand(commandLine, logger)
if err != nil {
return nil, i18n.WrapError(err)
}
if echoCommandLine {
fmt.Println(commandLine)
}
return command, nil
}
作者:me-no-de
项目:arduino-builde
func downloadAndUnpackLibrary(library Library, url string, targetPath string) error {
if libraryAlreadyDownloadedAndUnpacked(targetPath, library) {
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, strings.Replace(library.Name, " ", "_", -1)))
if err == nil {
err = os.RemoveAll(filepath.Join(targetPath, strings.Replace(library.Name, " ", "_", -1)))
if err != nil {
return i18n.WrapError(err)
}
}
err = copyRecursive(filepath.Join(unpackFolder, files[0].Name()), filepath.Join(targetPath, strings.Replace(library.Name, " ", "_", -1)))
if err != nil {
return i18n.WrapError(err)
}
return nil
}
作者:me-no-de
项目:arduino-builde
func downloadToolsMultipleVersions(tools []Tool, index map[string]interface{}) error {
host := translateGOOSGOARCHToPackageIndexValue()
for _, tool := range tools {
if !toolAlreadyDownloadedAndUnpacked(TOOLS_FOLDER, tool) {
_, err := os.Stat(filepath.Join(TOOLS_FOLDER, tool.Name))
if err == nil {
err = os.RemoveAll(filepath.Join(TOOLS_FOLDER, tool.Name))
if err != nil {
return i18n.WrapError(err)
}
}
}
}
for _, tool := range tools {
url, err := findToolUrl(index, tool, host)
if err != nil {
return i18n.WrapError(err)
}
err = downloadAndUnpackTool(tool, url, TOOLS_FOLDER, false)
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)
}
作者:me-no-de
项目:arduino-builde
func (s *HardwareLoader) Run(ctx *types.Context) error {
logger := ctx.GetLogger()
packages := &types.Packages{}
packages.Packages = make(map[string]*types.Package)
packages.Properties = make(map[string]string)
folders := ctx.HardwareFolders
folders, err := utils.AbsolutizePaths(folders)
if err != nil {
return i18n.WrapError(err)
}
for _, folder := range folders {
stat, err := os.Stat(folder)
if err != nil {
return i18n.WrapError(err)
}
if !stat.IsDir() {
return i18n.ErrorfWithLogger(logger, constants.MSG_MUST_BE_A_FOLDER, folder)
}
hardwarePlatformTxt, err := props.SafeLoad(filepath.Join(folder, constants.FILE_PLATFORM_TXT), logger)
if err != nil {
return i18n.WrapError(err)
}
packages.Properties.Merge(hardwarePlatformTxt)
subfolders, err := utils.ReadDirFiltered(folder, utils.FilterDirs)
if err != nil {
return i18n.WrapError(err)
}
subfolders = utils.FilterOutFoldersByNames(subfolders, constants.FOLDER_TOOLS)
for _, subfolder := range subfolders {
subfolderPath := filepath.Join(folder, subfolder.Name())
packageId := subfolder.Name()
if _, err := os.Stat(filepath.Join(subfolderPath, constants.FOLDER_HARDWARE)); err == nil {
subfolderPath = filepath.Join(subfolderPath, constants.FOLDER_HARDWARE)
}
targetPackage := getOrCreatePackage(packages, packageId)
err = loadPackage(targetPackage, subfolderPath, logger)
if err != nil {
return i18n.WrapError(err)
}
packages.Packages[packageId] = targetPackage
}
}
ctx.Hardware = packages
return nil
}
作者:facchin
项目:arduino-builde
func collectAllSketchFiles(from string) ([]string, error) {
filePaths := []string{}
// Source files in the root are compiled, non-recursively. This
// is the only place where .ino files can be present.
rootExtensions := func(ext string) bool { return MAIN_FILE_VALID_EXTENSIONS[ext] || ADDITIONAL_FILE_VALID_EXTENSIONS[ext] }
err := utils.FindFilesInFolder(&filePaths, from, rootExtensions, true /* recurse */)
if err != nil {
return nil, i18n.WrapError(err)
}
return filePaths, i18n.WrapError(err)
}
作者: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)
}
作者:me-no-de
项目:arduino-builde
func downloadBoardManagerCores(cores []Core, index map[string]interface{}) error {
for _, core := range cores {
url, err := findCoreUrl(index, core)
if err != nil {
return i18n.WrapError(err)
}
err = downloadAndUnpackBoardManagerCore(core, url, BOARD_MANAGER_FOLDER)
if err != nil {
return i18n.WrapError(err)
}
}
return nil
}