mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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