Clone of official tools

Committer:
The Other Jimmy
Date:
Wed Feb 15 13:53:18 2017 -0600
Revision:
35:da9c89f8be7d
Parent:
31:8ea194f6145b
Update tools to mbed-os 5.3.5

Who changed what in which revision?

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