作者:stevemarpl
项目:arduino-builde
func (s *ContainerFindIncludes) Run(context map[string]interface{}) error {
err := runCommand(context, &IncludesToIncludeFolders{})
if err != nil {
return utils.WrapError(err)
}
sketch := context[constants.CTX_SKETCH].(*types.Sketch)
sketchBuildPath := context[constants.CTX_SKETCH_BUILD_PATH].(string)
wheelSpins := context[constants.CTX_LIBRARY_DISCOVERY_RECURSION_DEPTH].(int)
for i := 0; i < wheelSpins; i++ {
commands := []types.Command{
&IncludesFinderWithGCC{SourceFile: filepath.Join(sketchBuildPath, filepath.Base(sketch.MainFile.Name)+".cpp")},
&GCCMinusMOutputParser{},
&IncludesToIncludeFolders{},
}
for _, command := range commands {
err := runCommand(context, command)
if err != nil {
return utils.WrapError(err)
}
}
}
foldersWithSources := context[constants.CTX_FOLDERS_WITH_SOURCES_QUEUE].(*types.UniqueSourceFolderQueue)
foldersWithSources.Push(types.SourceFolder{Folder: context[constants.CTX_SKETCH_BUILD_PATH].(string), Recurse: true})
if utils.MapHas(context, constants.CTX_IMPORTED_LIBRARIES) {
for _, library := range context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library) {
sourceFolders := utils.LibraryToSourceFolder(library)
for _, sourceFolder := range sourceFolders {
foldersWithSources.Push(sourceFolder)
}
}
}
err = runCommand(context, &CollectAllSourceFilesFromFoldersWithSources{})
if err != nil {
return utils.WrapError(err)
}
sourceFiles := context[constants.CTX_COLLECTED_SOURCE_FILES_QUEUE].(*types.UniqueStringQueue)
for !sourceFiles.Empty() {
commands := []types.Command{
&IncludesFinderWithGCC{SourceFile: sourceFiles.Pop().(string)},
&GCCMinusMOutputParser{},
&IncludesToIncludeFolders{},
&CollectAllSourceFilesFromFoldersWithSources{},
}
for _, command := range commands {
err := runCommand(context, command)
if err != nil {
return utils.WrapError(err)
}
}
}
return nil
}
作者:stevemarpl
项目: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 utils.WrapError(err)
}
unpackFolder, files, err := downloadAndUnpack(url)
if err != nil {
return utils.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 utils.WrapError(err)
}
}
err = copyRecursive(filepath.Join(unpackFolder, files[0].Name()), filepath.Join(targetPath, strings.Replace(library.Name, " ", "_", -1)))
if err != nil {
return utils.WrapError(err)
}
return nil
}
作者:stevemarpl
项目:arduino-builde
func compileCore(buildPath string, buildProperties map[string]string, verbose bool, warningsLevel string, logger i18n.Logger) (string, error) {
coreFolder := buildProperties[constants.BUILD_PROPERTIES_BUILD_CORE_PATH]
variantFolder := buildProperties[constants.BUILD_PROPERTIES_BUILD_VARIANT_PATH]
var includes []string
includes = append(includes, coreFolder)
if variantFolder != constants.EMPTY_STRING {
includes = append(includes, variantFolder)
}
includes = utils.Map(includes, utils.WrapWithHyphenI)
var err error
var 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 "", utils.WrapError(err)
}
}
coreObjectFiles, err := builder_utils.CompileFiles([]string{}, coreFolder, true, buildPath, buildProperties, includes, verbose, warningsLevel, logger)
if err != nil {
return "", utils.WrapError(err)
}
objectFiles := append(coreObjectFiles, variantObjectFiles...)
archiveFile, err := builder_utils.ArchiveCompiledFiles(buildPath, "core.a", objectFiles, buildProperties, verbose, logger)
if err != nil {
return "", utils.WrapError(err)
}
return archiveFile, nil
}
作者:andy52
项目:arduino-builde
func (s *ToolsLoader) Run(context map[string]interface{}) error {
folders := context[constants.CTX_TOOLS_FOLDERS].([]string)
tools := []*types.Tool{}
for _, folder := range folders {
builtinToolsVersionsFile, err := findBuiltinToolsVersionsFile(folder)
if err != nil {
return utils.WrapError(err)
}
if builtinToolsVersionsFile != constants.EMPTY_STRING {
err = loadToolsFrom(&tools, builtinToolsVersionsFile)
if err != nil {
return utils.WrapError(err)
}
} else {
subfolders, err := collectAllToolsFolders(folder)
if err != nil {
return utils.WrapError(err)
}
for _, subfolder := range subfolders {
err = loadToolsFromFolderStructure(&tools, subfolder)
if err != nil {
return utils.WrapError(err)
}
}
}
}
context[constants.CTX_TOOLS] = tools
return nil
}
作者:andy52
项目: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 utils.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, utils.WrapError(err)
}
作者:andy52
项目:arduino-builde
func loadToolsFrom(tools *[]*types.Tool, builtinToolsVersionsFilePath string) error {
rows, err := utils.ReadFileToRows(builtinToolsVersionsFilePath)
if err != nil {
return utils.WrapError(err)
}
folder, err := filepath.Abs(filepath.Dir(builtinToolsVersionsFilePath))
if err != nil {
return utils.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
}
作者:stevemarpl
项目: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 utils.WrapError(err)
}
}
}
}
for _, tool := range tools {
url, err := findToolUrl(index, tool, host)
if err != nil {
return utils.WrapError(err)
}
err = downloadAndUnpackTool(tool, url, TOOLS_FOLDER, false)
if err != nil {
return utils.WrapError(err)
}
}
return nil
}
作者:andy52
项目:arduino-builde
func (s *UnusedCompiledLibrariesRemover) Run(context map[string]interface{}) error {
librariesBuildPath := context[constants.CTX_LIBRARIES_BUILD_PATH].(string)
libraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library)
_, err := os.Stat(librariesBuildPath)
if err != nil && os.IsNotExist(err) {
return nil
}
libraryNames := toLibraryNames(libraries)
files, err := ioutil.ReadDir(librariesBuildPath)
if err != nil {
return utils.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 utils.WrapError(err)
}
}
}
}
return nil
}
作者:andy52
项目:arduino-builde
func compileFileWithRecipe(sourcePath string, source string, buildPath string, buildProperties map[string]string, includes []string, recipe string, verbose bool, warningsLevel string, logger i18n.Logger) (string, error) {
properties := utils.MergeMapsOfStrings(make(map[string]string), buildProperties)
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 "", utils.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 "", utils.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 "", utils.WrapError(err)
}
if !objIsUpToDate {
_, err = ExecRecipe(properties, recipe, false, verbose, verbose, logger)
if err != nil {
return "", utils.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
}
作者:andy52
项目:arduino-builde
func PrepareCommandForRecipe(properties map[string]string, recipe string, removeUnsetProperties bool, echoCommandLine bool, echoOutput bool, logger i18n.Logger) (*exec.Cmd, error) {
pattern := properties[recipe]
if pattern == constants.EMPTY_STRING {
return nil, utils.ErrorfWithLogger(logger, constants.MSG_PATTERN_MISSING, recipe)
}
var err error
commandLine := props.ExpandPropsInString(properties, pattern)
if removeUnsetProperties {
commandLine, err = props.DeleteUnexpandedPropsFromString(commandLine)
if err != nil {
return nil, utils.WrapError(err)
}
}
command, err := utils.PrepareCommand(commandLine, logger)
if err != nil {
return nil, utils.WrapError(err)
}
if echoCommandLine {
fmt.Println(commandLine)
}
return command, nil
}
作者:andy52
项目: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 utils.WrapError(err)
}
localProperties, err := props.SafeLoad(filepath.Join(folder, constants.FILE_BOARDS_LOCAL_TXT), logger)
if err != nil {
return utils.WrapError(err)
}
properties = utils.MergeMapsOfStrings(properties, localProperties)
propertiesByBoardId := props.FirstLevelOf(properties)
delete(propertiesByBoardId, constants.BOARD_PROPERTIES_MENU)
for boardId, properties := range propertiesByBoardId {
properties[constants.ID] = boardId
board := getOrCreateBoard(boards, boardId)
board.Properties = utils.MergeMapsOfStrings(board.Properties, properties)
boards[boardId] = board
}
return nil
}
作者:ricklo
项目:arduino-builde
func (s *CTagsRunner) Run(context map[string]interface{}) error {
buildProperties := context[constants.CTX_BUILD_PROPERTIES].(map[string]string)
ctagsTargetFileName := context[constants.CTX_CTAGS_TEMP_FILE_NAME].(string)
logger := context[constants.CTX_LOGGER].(i18n.Logger)
properties := utils.MergeMapsOfStrings(make(map[string]string), buildProperties, props.SubTree(props.SubTree(buildProperties, constants.BUILD_PROPERTIES_TOOLS_KEY), constants.CTAGS))
properties[constants.BUILD_PROPERTIES_SOURCE_FILE] = ctagsTargetFileName
pattern := properties[constants.BUILD_PROPERTIES_PATTERN]
if pattern == constants.EMPTY_STRING {
return utils.Errorf(context, constants.MSG_PATTERN_MISSING, constants.CTAGS)
}
commandLine := props.ExpandPropsInString(properties, pattern)
command, err := utils.PrepareCommand(commandLine, logger)
if err != nil {
return utils.WrapError(err)
}
verbose := context[constants.CTX_VERBOSE].(bool)
if verbose {
fmt.Println(commandLine)
}
sourceBytes, err := command.Output()
if err != nil {
return utils.WrapError(err)
}
context[constants.CTX_CTAGS_OUTPUT] = string(sourceBytes)
return nil
}
作者:spiderkey
项目:arduino-builde
func findIncludesUntilDone(context map[string]interface{}, sourceFilePath string) error {
targetFilePath := utils.NULLFile()
importedLibraries := context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library)
done := false
for !done {
commands := []types.Command{
&GCCPreprocRunnerForDiscoveringIncludes{SourceFilePath: sourceFilePath, TargetFilePath: targetFilePath},
&IncludesFinderWithRegExp{ContextField: constants.CTX_GCC_MINUS_E_SOURCE},
&IncludesToIncludeFolders{},
}
for _, command := range commands {
err := runCommand(context, command)
if err != nil {
return utils.WrapError(err)
}
}
if len(context[constants.CTX_INCLUDES_JUST_FOUND].([]string)) == 0 {
done = true
} else if len(context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library)) == len(importedLibraries) {
err := runCommand(context, &GCCPreprocRunner{TargetFileName: constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E})
return utils.WrapError(err)
}
importedLibraries = context[constants.CTX_IMPORTED_LIBRARIES].([]*types.Library)
context[constants.CTX_INCLUDES_JUST_FOUND] = []string{}
}
return nil
}
作者: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
}
作者:andy52
项目:arduino-builde
func (s *Linker) Run(context map[string]interface{}) error {
objectFilesSketch := context[constants.CTX_OBJECT_FILES_SKETCH].([]string)
objectFilesLibraries := context[constants.CTX_OBJECT_FILES_LIBRARIES].([]string)
objectFilesCore := context[constants.CTX_OBJECT_FILES_CORE].([]string)
var objectFiles []string
objectFiles = append(objectFiles, objectFilesSketch...)
objectFiles = append(objectFiles, objectFilesLibraries...)
objectFiles = append(objectFiles, objectFilesCore...)
coreArchiveFilePath := context[constants.CTX_ARCHIVE_FILE_PATH_CORE].(string)
buildPath := context[constants.CTX_BUILD_PATH].(string)
coreDotARelPath, err := filepath.Rel(buildPath, coreArchiveFilePath)
if err != nil {
return utils.WrapError(err)
}
buildProperties := context[constants.CTX_BUILD_PROPERTIES].(map[string]string)
verbose := context[constants.CTX_VERBOSE].(bool)
warningsLevel := context[constants.CTX_WARNINGS_LEVEL].(string)
logger := context[constants.CTX_LOGGER].(i18n.Logger)
err = link(objectFiles, coreDotARelPath, coreArchiveFilePath, buildProperties, verbose, warningsLevel, logger)
if err != nil {
return utils.WrapError(err)
}
return nil
}
作者:andy52
项目: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, utils.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, utils.WrapError(err)
}
for _, folder := range folders {
otherSources, err := findFilesInFolder(filepath.Join(sourcePath, folder.Name()), extension, recurse)
if err != nil {
return nil, utils.WrapError(err)
}
sources = append(sources, otherSources...)
}
}
return sources, nil
}
作者:ricklo
项目: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 := os.MkdirAll(sketchBuildPath, os.FileMode(0755))
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)
os.MkdirAll(filepath.Dir(targetFilePath), os.FileMode(0755))
bytes, err := ioutil.ReadFile(file.Name)
if err != nil {
return utils.WrapError(err)
}
ioutil.WriteFile(targetFilePath, bytes, os.FileMode(0644))
}
return nil
}
作者:ricklo
项目:arduino-builde
func compileLibrary(objectFiles []string, 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 := os.MkdirAll(libraryBuildPath, os.FileMode(0755))
if err != nil {
return nil, utils.WrapError(err)
}
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)
}
} 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
}
作者:andy52
项目:arduino-builde
func (s *HardwareLoader) Run(context map[string]interface{}) error {
logger := context[constants.CTX_LOGGER].(i18n.Logger)
packages := &types.Packages{}
packages.Packages = make(map[string]*types.Package)
packages.Properties = make(map[string]string)
folders := context[constants.CTX_HARDWARE_FOLDERS].([]string)
folders, err := utils.AbsolutizePaths(folders)
if err != nil {
return utils.WrapError(err)
}
for _, folder := range folders {
stat, err := os.Stat(folder)
if err != nil {
return utils.WrapError(err)
}
if !stat.IsDir() {
return utils.Errorf(context, constants.MSG_MUST_BE_A_FOLDER, folder)
}
hardwarePlatformTxt, err := props.SafeLoad(filepath.Join(folder, constants.FILE_PLATFORM_TXT), logger)
if err != nil {
return utils.WrapError(err)
}
packages.Properties = utils.MergeMapsOfStrings(packages.Properties, hardwarePlatformTxt)
subfolders, err := utils.ReadDirFiltered(folder, utils.FilterDirs)
if err != nil {
return utils.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 utils.WrapError(err)
}
packages.Packages[packageId] = targetPackage
}
}
context[constants.CTX_HARDWARE] = packages
return nil
}
作者: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)
}