Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bryantaylor 0:eafc3fd41f75 1 """ The new way of doing exports """
bryantaylor 0:eafc3fd41f75 2 import sys
bryantaylor 0:eafc3fd41f75 3 from os.path import join, abspath, dirname, exists
bryantaylor 0:eafc3fd41f75 4 from os.path import basename, relpath, normpath
bryantaylor 0:eafc3fd41f75 5 from os import makedirs, walk
bryantaylor 0:eafc3fd41f75 6 ROOT = abspath(join(dirname(__file__), ".."))
bryantaylor 0:eafc3fd41f75 7 sys.path.insert(0, ROOT)
bryantaylor 0:eafc3fd41f75 8 import copy
bryantaylor 0:eafc3fd41f75 9 from shutil import rmtree
bryantaylor 0:eafc3fd41f75 10 import zipfile
bryantaylor 0:eafc3fd41f75 11
bryantaylor 0:eafc3fd41f75 12 from tools.build_api import prepare_toolchain
bryantaylor 0:eafc3fd41f75 13 from tools.build_api import scan_resources
bryantaylor 0:eafc3fd41f75 14 from tools.export import EXPORTERS
bryantaylor 0:eafc3fd41f75 15 from tools.toolchains import Resources
bryantaylor 0:eafc3fd41f75 16
bryantaylor 0:eafc3fd41f75 17
bryantaylor 0:eafc3fd41f75 18 def get_exporter_toolchain(ide):
bryantaylor 0:eafc3fd41f75 19 """ Return the exporter class and the toolchain string as a tuple
bryantaylor 0:eafc3fd41f75 20
bryantaylor 0:eafc3fd41f75 21 Positional arguments:
bryantaylor 0:eafc3fd41f75 22 ide - the ide name of an exporter
bryantaylor 0:eafc3fd41f75 23 """
bryantaylor 0:eafc3fd41f75 24 return EXPORTERS[ide], EXPORTERS[ide].TOOLCHAIN
bryantaylor 0:eafc3fd41f75 25
bryantaylor 0:eafc3fd41f75 26
bryantaylor 0:eafc3fd41f75 27 def rewrite_basepath(file_name, resources, export_path, loc):
bryantaylor 0:eafc3fd41f75 28 """ Replace the basepath of filename with export_path
bryantaylor 0:eafc3fd41f75 29
bryantaylor 0:eafc3fd41f75 30 Positional arguments:
bryantaylor 0:eafc3fd41f75 31 file_name - the absolute path to a file
bryantaylor 0:eafc3fd41f75 32 resources - the resources object that the file came from
bryantaylor 0:eafc3fd41f75 33 export_path - the final destination of the file after export
bryantaylor 0:eafc3fd41f75 34 """
bryantaylor 0:eafc3fd41f75 35 new_f = join(loc, relpath(file_name, resources.file_basepath[file_name]))
bryantaylor 0:eafc3fd41f75 36 resources.file_basepath[join(export_path, new_f)] = export_path
bryantaylor 0:eafc3fd41f75 37 return new_f
bryantaylor 0:eafc3fd41f75 38
bryantaylor 0:eafc3fd41f75 39
bryantaylor 0:eafc3fd41f75 40 def subtract_basepath(resources, export_path, loc=""):
bryantaylor 0:eafc3fd41f75 41 """ Rewrite all of the basepaths with the export_path
bryantaylor 0:eafc3fd41f75 42
bryantaylor 0:eafc3fd41f75 43 Positional arguments:
bryantaylor 0:eafc3fd41f75 44 resources - the resource object to rewrite the basepaths of
bryantaylor 0:eafc3fd41f75 45 export_path - the final destination of the resources with respect to the
bryantaylor 0:eafc3fd41f75 46 generated project files
bryantaylor 0:eafc3fd41f75 47 """
bryantaylor 0:eafc3fd41f75 48 keys = ['s_sources', 'c_sources', 'cpp_sources', 'hex_files',
bryantaylor 0:eafc3fd41f75 49 'objects', 'libraries', 'inc_dirs', 'headers', 'linker_script',
bryantaylor 0:eafc3fd41f75 50 'lib_dirs']
bryantaylor 0:eafc3fd41f75 51 for key in keys:
bryantaylor 0:eafc3fd41f75 52 vals = getattr(resources, key)
bryantaylor 0:eafc3fd41f75 53 if isinstance(vals, set):
bryantaylor 0:eafc3fd41f75 54 vals = list(vals)
bryantaylor 0:eafc3fd41f75 55 if isinstance(vals, list):
bryantaylor 0:eafc3fd41f75 56 new_vals = []
bryantaylor 0:eafc3fd41f75 57 for val in vals:
bryantaylor 0:eafc3fd41f75 58 new_vals.append(rewrite_basepath(val, resources, export_path,
bryantaylor 0:eafc3fd41f75 59 loc))
bryantaylor 0:eafc3fd41f75 60 if isinstance(getattr(resources, key), set):
bryantaylor 0:eafc3fd41f75 61 setattr(resources, key, set(new_vals))
bryantaylor 0:eafc3fd41f75 62 else:
bryantaylor 0:eafc3fd41f75 63 setattr(resources, key, new_vals)
bryantaylor 0:eafc3fd41f75 64 elif vals:
bryantaylor 0:eafc3fd41f75 65 setattr(resources, key, rewrite_basepath(vals, resources,
bryantaylor 0:eafc3fd41f75 66 export_path, loc))
bryantaylor 0:eafc3fd41f75 67
bryantaylor 0:eafc3fd41f75 68
bryantaylor 0:eafc3fd41f75 69 def generate_project_files(resources, export_path, target, name, toolchain, ide,
bryantaylor 0:eafc3fd41f75 70 macros=None):
bryantaylor 0:eafc3fd41f75 71 """Generate the project files for a project
bryantaylor 0:eafc3fd41f75 72
bryantaylor 0:eafc3fd41f75 73 Positional arguments:
bryantaylor 0:eafc3fd41f75 74 resources - a Resources object containing all of the files needed to build
bryantaylor 0:eafc3fd41f75 75 this project
bryantaylor 0:eafc3fd41f75 76 export_path - location to place project files
bryantaylor 0:eafc3fd41f75 77 name - name of the project
bryantaylor 0:eafc3fd41f75 78 toolchain - a toolchain class that corresponds to the toolchain used by the
bryantaylor 0:eafc3fd41f75 79 IDE or makefile
bryantaylor 0:eafc3fd41f75 80 ide - IDE name to export to
bryantaylor 0:eafc3fd41f75 81
bryantaylor 0:eafc3fd41f75 82 Optional arguments:
bryantaylor 0:eafc3fd41f75 83 macros - additional macros that should be defined within the exported
bryantaylor 0:eafc3fd41f75 84 project
bryantaylor 0:eafc3fd41f75 85 """
bryantaylor 0:eafc3fd41f75 86 exporter_cls, _ = get_exporter_toolchain(ide)
bryantaylor 0:eafc3fd41f75 87 exporter = exporter_cls(target, export_path, name, toolchain,
bryantaylor 0:eafc3fd41f75 88 extra_symbols=macros, resources=resources)
bryantaylor 0:eafc3fd41f75 89 exporter.generate()
bryantaylor 0:eafc3fd41f75 90 files = exporter.generated_files
bryantaylor 0:eafc3fd41f75 91 return files, exporter
bryantaylor 0:eafc3fd41f75 92
bryantaylor 0:eafc3fd41f75 93
bryantaylor 0:eafc3fd41f75 94 def zip_export(file_name, prefix, resources, project_files):
bryantaylor 0:eafc3fd41f75 95 """Create a zip file from an exported project.
bryantaylor 0:eafc3fd41f75 96
bryantaylor 0:eafc3fd41f75 97 Positional Parameters:
bryantaylor 0:eafc3fd41f75 98 file_name - the file name of the resulting zip file
bryantaylor 0:eafc3fd41f75 99 prefix - a directory name that will prefix the entire zip file's contents
bryantaylor 0:eafc3fd41f75 100 resources - a resources object with files that must be included in the zip
bryantaylor 0:eafc3fd41f75 101 project_files - a list of extra files to be added to the root of the prefix
bryantaylor 0:eafc3fd41f75 102 directory
bryantaylor 0:eafc3fd41f75 103 """
bryantaylor 0:eafc3fd41f75 104 with zipfile.ZipFile(file_name, "w") as zip_file:
bryantaylor 0:eafc3fd41f75 105 for prj_file in project_files:
bryantaylor 0:eafc3fd41f75 106 zip_file.write(prj_file, join(prefix, basename(prj_file)))
bryantaylor 0:eafc3fd41f75 107 for loc, resource in resources.iteritems():
bryantaylor 0:eafc3fd41f75 108 print resource.features
bryantaylor 0:eafc3fd41f75 109 for res in [resource] + resource.features.values():
bryantaylor 0:eafc3fd41f75 110 extras = []
bryantaylor 0:eafc3fd41f75 111 for directory in res.repo_dirs:
bryantaylor 0:eafc3fd41f75 112 for root, _, files in walk(directory):
bryantaylor 0:eafc3fd41f75 113 for repo_file in files:
bryantaylor 0:eafc3fd41f75 114 source = join(root, repo_file)
bryantaylor 0:eafc3fd41f75 115 extras.append(source)
bryantaylor 0:eafc3fd41f75 116 res.file_basepath[source] = res.base_path
bryantaylor 0:eafc3fd41f75 117 for source in \
bryantaylor 0:eafc3fd41f75 118 res.headers + res.s_sources + res.c_sources +\
bryantaylor 0:eafc3fd41f75 119 res.cpp_sources + res.libraries + res.hex_files + \
bryantaylor 0:eafc3fd41f75 120 [res.linker_script] + res.bin_files + res.objects + \
bryantaylor 0:eafc3fd41f75 121 res.json_files + res.lib_refs + res.lib_builds + \
bryantaylor 0:eafc3fd41f75 122 res.repo_files + extras:
bryantaylor 0:eafc3fd41f75 123 if source:
bryantaylor 0:eafc3fd41f75 124 zip_file.write(
bryantaylor 0:eafc3fd41f75 125 source,
bryantaylor 0:eafc3fd41f75 126 join(prefix, loc,
bryantaylor 0:eafc3fd41f75 127 relpath(source, res.file_basepath[source])))
bryantaylor 0:eafc3fd41f75 128
bryantaylor 0:eafc3fd41f75 129
bryantaylor 0:eafc3fd41f75 130
bryantaylor 0:eafc3fd41f75 131 def export_project(src_paths, export_path, target, ide,
bryantaylor 0:eafc3fd41f75 132 libraries_paths=None, options=None, linker_script=None,
bryantaylor 0:eafc3fd41f75 133 clean=False, notify=None, verbose=False, name=None,
bryantaylor 0:eafc3fd41f75 134 inc_dirs=None, jobs=1, silent=False, extra_verbose=False,
bryantaylor 0:eafc3fd41f75 135 config=None, macros=None, zip_proj=None):
bryantaylor 0:eafc3fd41f75 136 """Generates a project file and creates a zip archive if specified
bryantaylor 0:eafc3fd41f75 137
bryantaylor 0:eafc3fd41f75 138 Positional Arguments:
bryantaylor 0:eafc3fd41f75 139 src_paths - a list of paths from which to find source files
bryantaylor 0:eafc3fd41f75 140 export_path - a path specifying the location of generated project files
bryantaylor 0:eafc3fd41f75 141 target - the mbed board/mcu for which to generate the executable
bryantaylor 0:eafc3fd41f75 142 ide - the ide for which to generate the project fields
bryantaylor 0:eafc3fd41f75 143
bryantaylor 0:eafc3fd41f75 144 Keyword Arguments:
bryantaylor 0:eafc3fd41f75 145 libraries_paths - paths to additional libraries
bryantaylor 0:eafc3fd41f75 146 options - build options passed by -o flag
bryantaylor 0:eafc3fd41f75 147 linker_script - path to the linker script for the specified target
bryantaylor 0:eafc3fd41f75 148 clean - removes the export_path if it exists
bryantaylor 0:eafc3fd41f75 149 notify - function is passed all events, and expected to handle notification
bryantaylor 0:eafc3fd41f75 150 of the user, emit the events to a log, etc.
bryantaylor 0:eafc3fd41f75 151 verbose - assigns the notify function to toolchains print_notify_verbose
bryantaylor 0:eafc3fd41f75 152 name - project name
bryantaylor 0:eafc3fd41f75 153 inc_dirs - additional include directories
bryantaylor 0:eafc3fd41f75 154 jobs - number of threads
bryantaylor 0:eafc3fd41f75 155 silent - silent build - no output
bryantaylor 0:eafc3fd41f75 156 extra_verbose - assigns the notify function to toolchains
bryantaylor 0:eafc3fd41f75 157 print_notify_verbose
bryantaylor 0:eafc3fd41f75 158 config - toolchain's config object
bryantaylor 0:eafc3fd41f75 159 macros - User-defined macros
bryantaylor 0:eafc3fd41f75 160 zip_proj - string name of the zip archive you wish to creat (exclude arg
bryantaylor 0:eafc3fd41f75 161 if you do not wish to create an archive
bryantaylor 0:eafc3fd41f75 162 """
bryantaylor 0:eafc3fd41f75 163
bryantaylor 0:eafc3fd41f75 164 # Convert src_path to a list if needed
bryantaylor 0:eafc3fd41f75 165 if isinstance(src_paths, dict):
bryantaylor 0:eafc3fd41f75 166 paths = sum(src_paths.values(), [])
bryantaylor 0:eafc3fd41f75 167 elif isinstance(src_paths, list):
bryantaylor 0:eafc3fd41f75 168 paths = src_paths[:]
bryantaylor 0:eafc3fd41f75 169 else:
bryantaylor 0:eafc3fd41f75 170 paths = [src_paths]
bryantaylor 0:eafc3fd41f75 171
bryantaylor 0:eafc3fd41f75 172 # Extend src_paths wit libraries_paths
bryantaylor 0:eafc3fd41f75 173 if libraries_paths is not None:
bryantaylor 0:eafc3fd41f75 174 paths.extend(libraries_paths)
bryantaylor 0:eafc3fd41f75 175
bryantaylor 0:eafc3fd41f75 176 if not isinstance(src_paths, dict):
bryantaylor 0:eafc3fd41f75 177 src_paths = {"": paths}
bryantaylor 0:eafc3fd41f75 178
bryantaylor 0:eafc3fd41f75 179 # Export Directory
bryantaylor 0:eafc3fd41f75 180 if exists(export_path) and clean:
bryantaylor 0:eafc3fd41f75 181 rmtree(export_path)
bryantaylor 0:eafc3fd41f75 182 if not exists(export_path):
bryantaylor 0:eafc3fd41f75 183 makedirs(export_path)
bryantaylor 0:eafc3fd41f75 184
bryantaylor 0:eafc3fd41f75 185 _, toolchain_name = get_exporter_toolchain(ide)
bryantaylor 0:eafc3fd41f75 186
bryantaylor 0:eafc3fd41f75 187 # Pass all params to the unified prepare_resources()
bryantaylor 0:eafc3fd41f75 188 toolchain = prepare_toolchain(paths, target, toolchain_name,
bryantaylor 0:eafc3fd41f75 189 macros=macros, options=options, clean=clean,
bryantaylor 0:eafc3fd41f75 190 jobs=jobs, notify=notify, silent=silent,
bryantaylor 0:eafc3fd41f75 191 verbose=verbose, extra_verbose=extra_verbose,
bryantaylor 0:eafc3fd41f75 192 config=config)
bryantaylor 0:eafc3fd41f75 193 # The first path will give the name to the library
bryantaylor 0:eafc3fd41f75 194 if name is None:
bryantaylor 0:eafc3fd41f75 195 name = basename(normpath(abspath(src_paths[0])))
bryantaylor 0:eafc3fd41f75 196
bryantaylor 0:eafc3fd41f75 197 # Call unified scan_resources
bryantaylor 0:eafc3fd41f75 198 resource_dict = {loc: scan_resources(path, toolchain, inc_dirs=inc_dirs)
bryantaylor 0:eafc3fd41f75 199 for loc, path in src_paths.iteritems()}
bryantaylor 0:eafc3fd41f75 200 resources = Resources()
bryantaylor 0:eafc3fd41f75 201 toolchain.build_dir = export_path
bryantaylor 0:eafc3fd41f75 202 config_header = toolchain.get_config_header()
bryantaylor 0:eafc3fd41f75 203 resources.headers.append(config_header)
bryantaylor 0:eafc3fd41f75 204 resources.file_basepath[config_header] = dirname(config_header)
bryantaylor 0:eafc3fd41f75 205
bryantaylor 0:eafc3fd41f75 206 if zip_proj:
bryantaylor 0:eafc3fd41f75 207 subtract_basepath(resources, export_path)
bryantaylor 0:eafc3fd41f75 208 for loc, res in resource_dict.iteritems():
bryantaylor 0:eafc3fd41f75 209 temp = copy.deepcopy(res)
bryantaylor 0:eafc3fd41f75 210 subtract_basepath(temp, export_path, loc)
bryantaylor 0:eafc3fd41f75 211 resources.add(temp)
bryantaylor 0:eafc3fd41f75 212 else:
bryantaylor 0:eafc3fd41f75 213 for _, res in resource_dict.iteritems():
bryantaylor 0:eafc3fd41f75 214 resources.add(res)
bryantaylor 0:eafc3fd41f75 215
bryantaylor 0:eafc3fd41f75 216 # Change linker script if specified
bryantaylor 0:eafc3fd41f75 217 if linker_script is not None:
bryantaylor 0:eafc3fd41f75 218 resources.linker_script = linker_script
bryantaylor 0:eafc3fd41f75 219
bryantaylor 0:eafc3fd41f75 220 files, exporter = generate_project_files(resources, export_path,
bryantaylor 0:eafc3fd41f75 221 target, name, toolchain, ide,
bryantaylor 0:eafc3fd41f75 222 macros=macros)
bryantaylor 0:eafc3fd41f75 223 files.append(config_header)
bryantaylor 0:eafc3fd41f75 224 if zip_proj:
bryantaylor 0:eafc3fd41f75 225 if isinstance(zip_proj, basestring):
bryantaylor 0:eafc3fd41f75 226 zip_export(join(export_path, zip_proj), name, resource_dict, files)
bryantaylor 0:eafc3fd41f75 227 else:
bryantaylor 0:eafc3fd41f75 228 zip_export(zip_proj, name, resource_dict, files)
bryantaylor 0:eafc3fd41f75 229
bryantaylor 0:eafc3fd41f75 230 return exporter
bryantaylor 0:eafc3fd41f75 231
bryantaylor 0:eafc3fd41f75 232
bryantaylor 0:eafc3fd41f75 233 def print_results(successes, failures, skips=None):
bryantaylor 0:eafc3fd41f75 234 """ Print out the results of an export process
bryantaylor 0:eafc3fd41f75 235
bryantaylor 0:eafc3fd41f75 236 Positional arguments:
bryantaylor 0:eafc3fd41f75 237 successes - The list of exports that succeeded
bryantaylor 0:eafc3fd41f75 238 failures - The list of exports that failed
bryantaylor 0:eafc3fd41f75 239
bryantaylor 0:eafc3fd41f75 240 Keyword arguments:
bryantaylor 0:eafc3fd41f75 241 skips - The list of exports that were skipped
bryantaylor 0:eafc3fd41f75 242 """
bryantaylor 0:eafc3fd41f75 243 print
bryantaylor 0:eafc3fd41f75 244 if successes:
bryantaylor 0:eafc3fd41f75 245 print "Successful: "
bryantaylor 0:eafc3fd41f75 246 for success in successes:
bryantaylor 0:eafc3fd41f75 247 print " * %s" % success
bryantaylor 0:eafc3fd41f75 248 if failures:
bryantaylor 0:eafc3fd41f75 249 print "Failed: "
bryantaylor 0:eafc3fd41f75 250 for failure in failures:
bryantaylor 0:eafc3fd41f75 251 print " * %s" % failure
bryantaylor 0:eafc3fd41f75 252 if skips:
bryantaylor 0:eafc3fd41f75 253 print "Skipped: "
bryantaylor 0:eafc3fd41f75 254 for skip in skips:
bryantaylor 0:eafc3fd41f75 255 print " * %s" % skip
bryantaylor 0:eafc3fd41f75 256