Clone of official tools

Revision:
43:2a7da56ebd24
Parent:
40:7d3fa6b99b2b
--- a/export/__init__.py	Mon Nov 06 13:17:14 2017 -0600
+++ b/export/__init__.py	Tue Sep 25 13:43:09 2018 -0500
@@ -15,53 +15,49 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from __future__ import print_function, division, absolute_import
+
 import sys
-from os.path import join, abspath, dirname, exists
+from os.path import join, abspath, dirname, exists, isfile
 from os.path import basename, relpath, normpath, splitext
 from os import makedirs, walk
 import copy
 from shutil import rmtree, copyfile
 import zipfile
-ROOT = abspath(join(dirname(__file__), ".."))
-sys.path.insert(0, ROOT)
 
-from tools.build_api import prepare_toolchain
-from tools.build_api import scan_resources
-from tools.toolchains import Resources
-from tools.export import lpcxpresso, ds5_5, iar, makefile
-from tools.export import embitz, coide, kds, simplicity, atmelstudio, mcuxpresso
-from tools.export import sw4stm32, e2studio, zip, cmsis, uvision, cdt, vscode
-from tools.export import gnuarmeclipse
-from tools.export import qtcreator
-from tools.targets import TARGET_NAMES
+from ..resources import Resources, FileType, FileRef
+from ..config import ALLOWED_FEATURES
+from ..build_api import prepare_toolchain
+from ..targets import TARGET_NAMES
+from . import (lpcxpresso, ds5_5, iar, makefile, embitz, coide, kds, simplicity,
+               atmelstudio, mcuxpresso, sw4stm32, e2studio, zip, cmsis, uvision,
+               cdt, vscode, gnuarmeclipse, qtcreator, cmake, nb, cces, codeblocks)
 
 EXPORTERS = {
-    'uvision5': uvision.Uvision,
-    'uvision': uvision.Uvision,
-    'lpcxpresso': lpcxpresso.LPCXpresso,
-    'gcc_arm': makefile.GccArm,
-    'make_gcc_arm': makefile.GccArm,
-    'make_armc5': makefile.Armc5,
-    'make_armc6': makefile.Armc6,
-    'make_iar': makefile.IAR,
-    'ds5_5': ds5_5.DS5_5,
-    'iar': iar.IAR,
-    'embitz' : embitz.EmBitz,
-    'coide' : coide.CoIDE,
-    'kds' : kds.KDS,
-    'simplicityv3' : simplicity.SimplicityV3,
-    'atmelstudio' : atmelstudio.AtmelStudio,
-    'sw4stm32'    : sw4stm32.Sw4STM32,
-    'e2studio' : e2studio.E2Studio,
-    'eclipse_gcc_arm'  : cdt.EclipseGcc,
-    'eclipse_iar'      : cdt.EclipseIAR,
-    'eclipse_armc5'    : cdt.EclipseArmc5,
-    'gnuarmeclipse': gnuarmeclipse.GNUARMEclipse,
-    'mcuxpresso': mcuxpresso.MCUXpresso,
-    'qtcreator': qtcreator.QtCreator,
-    'vscode_gcc_arm' : vscode.VSCodeGcc,
-    'vscode_iar' : vscode.VSCodeIAR,
-    'vscode_armc5' : vscode.VSCodeArmc5
+    u'uvision6': uvision.UvisionArmc6,
+    u'uvision5': uvision.UvisionArmc5,
+    u'make_gcc_arm': makefile.GccArm,
+    u'make_armc5': makefile.Armc5,
+    u'make_armc6': makefile.Armc6,
+    u'make_iar': makefile.IAR,
+    u'ds5_5': ds5_5.DS5_5,
+    u'iar': iar.IAR,
+    u'embitz' : embitz.EmBitz,
+    u'sw4stm32'    : sw4stm32.Sw4STM32,
+    u'e2studio' : e2studio.E2Studio,
+    u'eclipse_gcc_arm'  : cdt.EclipseGcc,
+    u'eclipse_iar'      : cdt.EclipseIAR,
+    u'eclipse_armc5'    : cdt.EclipseArmc5,
+    u'gnuarmeclipse': gnuarmeclipse.GNUARMEclipse,
+    u'mcuxpresso': mcuxpresso.MCUXpresso,
+    u'netbeans':     nb.GNUARMNetbeans,
+    u'qtcreator': qtcreator.QtCreator,
+    u'vscode_gcc_arm' : vscode.VSCodeGcc,
+    u'vscode_iar' : vscode.VSCodeIAR,
+    u'vscode_armc5' : vscode.VSCodeArmc5,
+    u'cmake_gcc_arm': cmake.GccArm,
+    u'cces' : cces.CCES,
+    u'codeblocks': codeblocks.CodeBlocks
 }
 
 ERROR_MESSAGE_UNSUPPORTED_TOOLCHAIN = """
@@ -142,48 +138,6 @@
     return EXPORTERS[ide], EXPORTERS[ide].TOOLCHAIN
 
 
-def rewrite_basepath(file_name, resources, export_path, loc):
-    """ Replace the basepath of filename with export_path
-
-    Positional arguments:
-    file_name - the absolute path to a file
-    resources - the resources object that the file came from
-    export_path - the final destination of the file after export
-    """
-    new_f = join(loc, relpath(file_name, resources.file_basepath[file_name]))
-    resources.file_basepath[new_f] = export_path
-    return new_f
-
-
-def subtract_basepath(resources, export_path, loc=""):
-    """ Rewrite all of the basepaths with the export_path
-
-    Positional arguments:
-    resources - the resource object to rewrite the basepaths of
-    export_path - the final destination of the resources with respect to the
-      generated project files
-    """
-    keys = ['s_sources', 'c_sources', 'cpp_sources', 'hex_files',
-            'objects', 'libraries', 'inc_dirs', 'headers', 'linker_script',
-            'lib_dirs']
-    for key in keys:
-        vals = getattr(resources, key)
-        if isinstance(vals, set):
-            vals = list(vals)
-        if isinstance(vals, list):
-            new_vals = []
-            for val in vals:
-                new_vals.append(rewrite_basepath(val, resources, export_path,
-                                                 loc))
-            if isinstance(getattr(resources, key), set):
-                setattr(resources, key, set(new_vals))
-            else:
-                setattr(resources, key, new_vals)
-        elif vals:
-            setattr(resources, key, rewrite_basepath(vals, resources,
-                                                     export_path, loc))
-
-
 def generate_project_files(resources, export_path, target, name, toolchain, ide,
                            macros=None):
     """Generate the project files for a project
@@ -209,7 +163,25 @@
     return files, exporter
 
 
-def zip_export(file_name, prefix, resources, project_files, inc_repos):
+def _inner_zip_export(resources, prj_files, inc_repos):
+    to_zip = sum((resources.get_file_refs(ftype) for ftype
+                  in Resources.ALL_FILE_TYPES),
+                 [])
+    to_zip.extend(FileRef(basename(pfile), pfile) for pfile in prj_files)
+    for dest, source in resources.get_file_refs(FileType.BLD_REF):
+        target_dir, _ = splitext(dest)
+        dest = join(target_dir, ".bld", "bldrc")
+        to_zip.append(FileRef(dest, source))
+    if inc_repos:
+        for dest, source in resources.get_file_refs(FileType.REPO_DIRS):
+            for root, _, files in walk(source):
+                for repo_file in files:
+                    file_source = join(root, repo_file)
+                    file_dest = join(dest, relpath(file_source, source))
+                    to_zip.append(FileRef(file_dest, file_source))
+    return to_zip
+
+def zip_export(file_name, prefix, resources, project_files, inc_repos, notify):
     """Create a zip file from an exported project.
 
     Positional Parameters:
@@ -219,43 +191,26 @@
     project_files - a list of extra files to be added to the root of the prefix
       directory
     """
+    to_zip_list = sorted(set(_inner_zip_export(
+        resources, project_files, inc_repos)))
+    total_files = len(to_zip_list)
+    zipped = 0
     with zipfile.ZipFile(file_name, "w") as zip_file:
-        for prj_file in project_files:
-            zip_file.write(prj_file, join(prefix, basename(prj_file)))
-        for loc, res in resources.iteritems():
-            to_zip = (
-                res.headers + res.s_sources + res.c_sources +\
-                res.cpp_sources + res.libraries + res.hex_files + \
-                [res.linker_script] + res.bin_files + res.objects + \
-                res.json_files + res.lib_refs + res.lib_builds)
-            if inc_repos:
-                for directory in res.repo_dirs:
-                    for root, _, files in walk(directory):
-                        for repo_file in files:
-                            source = join(root, repo_file)
-                            to_zip.append(source)
-                            res.file_basepath[source] = res.base_path
-                to_zip += res.repo_files
-            for source in to_zip:
-                if source:
-                    zip_file.write(
-                        source,
-                        join(prefix, loc,
-                             relpath(source, res.file_basepath[source])))
-            for source in res.lib_builds:
-                target_dir, _ = splitext(source)
-                dest = join(prefix, loc,
-                            relpath(target_dir, res.file_basepath[source]),
-                            ".bld", "bldrc")
-                zip_file.write(source, dest)
-
+        for dest, source in to_zip_list:
+            if source and isfile(source):
+                zip_file.write(source, join(prefix, dest))
+                zipped += 1
+                notify.progress("Zipping", source,
+                                100 * (zipped / total_files))
+            else:
+                zipped += 1
 
 
 def export_project(src_paths, export_path, target, ide, libraries_paths=None,
-                   linker_script=None, notify=None, verbose=False, name=None,
-                   inc_dirs=None, jobs=1, silent=False, extra_verbose=False,
-                   config=None, macros=None, zip_proj=None, inc_repos=False,
-                   build_profile=None, app_config=None):
+                   linker_script=None, notify=None, name=None, inc_dirs=None,
+                   jobs=1, config=None, macros=None, zip_proj=None,
+                   inc_repos=False, build_profile=None, app_config=None,
+                   ignore=None):
     """Generates a project file and creates a zip archive if specified
 
     Positional Arguments:
@@ -269,17 +224,14 @@
     linker_script - path to the linker script for the specified target
     notify - function is passed all events, and expected to handle notification
       of the user, emit the events to a log, etc.
-    verbose - assigns the notify function to toolchains print_notify_verbose
     name - project name
     inc_dirs - additional include directories
     jobs - number of threads
-    silent - silent build - no output
-    extra_verbose - assigns the notify function to toolchains
-      print_notify_verbose
     config - toolchain's config object
     macros - User-defined macros
     zip_proj - string name of the zip archive you wish to creat (exclude arg
      if you do not wish to create an archive
+    ignore - list of paths to add to mbedignore
     """
 
     # Convert src_path to a list if needed
@@ -306,32 +258,23 @@
     # Pass all params to the unified prepare_resources()
     toolchain = prepare_toolchain(
         paths, "", target, toolchain_name, macros=macros, jobs=jobs,
-        notify=notify, silent=silent, verbose=verbose,
-        extra_verbose=extra_verbose, config=config, build_profile=build_profile,
-        app_config=app_config)
-    # The first path will give the name to the library
+        notify=notify, config=config, build_profile=build_profile,
+        app_config=app_config, ignore=ignore)
+
     toolchain.RESPONSE_FILES = False
     if name is None:
         name = basename(normpath(abspath(src_paths[0])))
 
-    # Call unified scan_resources
-    resource_dict = {loc: scan_resources(path, toolchain, inc_dirs=inc_dirs, collect_ignores=True)
-                     for loc, path in src_paths.iteritems()}
-    resources = Resources()
+    resources = Resources(notify, collect_ignores=True)
+    resources.add_toolchain_labels(toolchain)
+    for loc, path in src_paths.items():
+        for p in path:
+            resources.add_directory(p, into_path=loc)
     toolchain.build_dir = export_path
+    toolchain.config.load_resources(resources)
+    toolchain.set_config_data(toolchain.config.get_config_data())
     config_header = toolchain.get_config_header()
-    resources.headers.append(config_header)
-    resources.file_basepath[config_header] = dirname(config_header)
-
-    if zip_proj:
-        subtract_basepath(resources, ".")
-        for loc, res in resource_dict.iteritems():
-            temp = copy.deepcopy(res)
-            subtract_basepath(temp, ".", loc)
-            resources.add(temp)
-    else:
-        for _, res in resource_dict.iteritems():
-            resources.add(res)
+    resources.add_file_ref(FileType.HEADER, basename(config_header), config_header)
 
     # Change linker script if specified
     if linker_script is not None:
@@ -340,18 +283,14 @@
     files, exporter = generate_project_files(resources, export_path,
                                              target, name, toolchain, ide,
                                              macros=macros)
-    files.append(config_header)
     if zip_proj:
-        for resource in resource_dict.values():
-            for label, res in resource.features.iteritems():
-                if label not in toolchain.target.features:
-                    resource.add(res)
+        resources.add_features(ALLOWED_FEATURES)
         if isinstance(zip_proj, basestring):
-            zip_export(join(export_path, zip_proj), name, resource_dict,
-                       files + list(exporter.static_files), inc_repos)
+            zip_export(join(export_path, zip_proj), name, resources,
+                       files + list(exporter.static_files), inc_repos, notify)
         else:
-            zip_export(zip_proj, name, resource_dict,
-                       files + list(exporter.static_files), inc_repos)
+            zip_export(zip_proj, name, resources,
+                       files + list(exporter.static_files), inc_repos, notify)
     else:
         for static_file in exporter.static_files:
             if not exists(join(export_path, basename(static_file))):