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