Clone of official tools

Revision:
7:5af61d55adbe
Parent:
2:07730b0f452c
Child:
8:a8ac6ed29081
diff -r 744106007ff3 -r 5af61d55adbe build_api.py
--- a/build_api.py	Sat May 21 20:17:44 2016 +0100
+++ b/build_api.py	Tue Jun 07 11:21:44 2016 +0100
@@ -22,19 +22,18 @@
 
 from types import ListType
 from shutil import rmtree
-from os.path import join, exists, basename, abspath
-from os import getcwd, walk
+from os.path import join, exists, basename, abspath, normpath
+from os import getcwd
 from time import time
-from copy import copy
 
-from tools.utils import mkdir, run_cmd, run_cmd_ext, NotSupportedException
+from tools.utils import mkdir, run_cmd, run_cmd_ext, NotSupportedException, ToolException
 from tools.paths import MBED_TARGETS_PATH, MBED_LIBRARIES, MBED_API, MBED_HAL, MBED_COMMON
 from tools.targets import TARGET_NAMES, TARGET_MAP
 from tools.libraries import Library
-from tools.toolchains import TOOLCHAIN_CLASSES, TOOLCHAIN_PROFILES
+from tools.toolchains import TOOLCHAIN_CLASSES
 from jinja2 import FileSystemLoader
 from jinja2.environment import Environment
-
+from tools.config import Config
 
 def prep_report(report, target_name, toolchain_name, id_name):
     # Setup report keys
@@ -77,24 +76,68 @@
     result_wrap = { 0: result }
     report[target][toolchain][id_name].append(result_wrap)
 
-def build_project(src_path, build_path, target, toolchain_name,
-        libraries_paths=None, options=None, linker_script=None,
-        clean=False, notify=None, verbose=False, name=None, macros=None, inc_dirs=None,
-        jobs=1, silent=False, report=None, properties=None, project_id=None, project_description=None, extra_verbose=False):
-    """ This function builds project. Project can be for example one test / UT
-    """
+def get_config(src_path, target, toolchain_name):
+    # Convert src_path to a list if needed
     src_paths = [src_path] if type(src_path) != ListType else src_path
     # We need to remove all paths which are repeated to avoid
     # multiple compilations and linking with the same objects
     src_paths = [src_paths[0]] + list(set(src_paths[1:]))
-    project_name = basename(abspath(src_paths[0] if src_paths[0] != "." and src_paths[0] != "./" else getcwd()))
+
+    # Create configuration object
+    config = Config(target, src_paths)
 
-    for path in src_paths:
-        profile = get_build_profile(path)
+    # If the 'target' argument is a string, convert it to a target instance
+    if isinstance(target, str):
+        try:
+            target = TARGET_MAP[target]
+        except KeyError:
+            raise KeyError("Target '%s' not found" % target)
 
     # Toolchain instance
     try:
-        toolchain = TOOLCHAIN_CLASSES[toolchain_name](target, options, notify, macros, silent, extra_verbose=extra_verbose, profile=profile)
+        toolchain = TOOLCHAIN_CLASSES[toolchain_name](target, options=None, notify=None, macros=None, silent=True, extra_verbose=False)
+    except KeyError as e:
+        raise KeyError("Toolchain %s not supported" % toolchain_name)
+
+    # Scan src_path for config files
+    resources = toolchain.scan_resources(src_paths[0])
+    for path in src_paths[1:]:
+        resources.add(toolchain.scan_resources(path))
+
+    config.add_config_files(resources.json_files)
+    return config.get_config_data()
+
+def build_project(src_path, build_path, target, toolchain_name,
+        libraries_paths=None, options=None, linker_script=None,
+        clean=False, notify=None, verbose=False, name=None, macros=None, inc_dirs=None,
+        jobs=1, silent=False, report=None, properties=None, project_id=None, project_description=None,
+        extra_verbose=False, config=None):
+    """ This function builds project. Project can be for example one test / UT
+    """
+
+    # Convert src_path to a list if needed
+    src_paths = [src_path] if type(src_path) != ListType else src_path
+
+    # We need to remove all paths which are repeated to avoid
+    # multiple compilations and linking with the same objects
+    src_paths = [src_paths[0]] + list(set(src_paths[1:]))
+    first_src_path = src_paths[0] if src_paths[0] != "." and src_paths[0] != "./" else getcwd()
+    abs_path = abspath(first_src_path)
+    project_name = basename(normpath(abs_path))
+
+    # If the configuration object was not yet created, create it now
+    config = config or Config(target, src_paths)
+
+    # If the 'target' argument is a string, convert it to a target instance
+    if isinstance(target, str):
+        try:
+            target = TARGET_MAP[target]
+        except KeyError:
+            raise KeyError("Target '%s' not found" % target)
+
+    # Toolchain instance
+    try:
+        toolchain = TOOLCHAIN_CLASSES[toolchain_name](target, options, notify, macros, silent, extra_verbose=extra_verbose)
     except KeyError as e:
         raise KeyError("Toolchain %s not supported" % toolchain_name)
 
@@ -113,8 +156,10 @@
 
     if report != None:
         start = time()
-        id_name = project_id.upper()
-        description = project_description
+        
+        # If project_id is specified, use that over the default name
+        id_name = project_id.upper() if project_id else name.upper()
+        description = project_description if project_description else name
         vendor_label = target.extra_labels[0]
         cur_result = None
         prep_report(report, target.name, toolchain_name, id_name)
@@ -148,13 +193,18 @@
                 resources.inc_dirs.extend(inc_dirs)
             else:
                 resources.inc_dirs.append(inc_dirs)
+
+        # Update the configuration with any .json files found while scanning
+        config.add_config_files(resources.json_files)
+        # And add the configuration macros to the toolchain
+        toolchain.add_macros(config.get_config_data_macros())
+
         # Compile Sources
         for path in src_paths:
             src = toolchain.scan_resources(path)
             objects = toolchain.compile_sources(src, build_path, resources.inc_dirs)
             resources.objects.extend(objects)
 
-
         # Link Program
         res, needed_update = toolchain.link_program(resources, build_path, name)
 
@@ -190,11 +240,11 @@
         # Let Exception propagate
         raise e
 
-
 def build_library(src_paths, build_path, target, toolchain_name,
          dependencies_paths=None, options=None, name=None, clean=False, archive=True,
          notify=None, verbose=False, macros=None, inc_dirs=None, inc_dirs_ext=None,
-         jobs=1, silent=False, report=None, properties=None, extra_verbose=False):
+         jobs=1, silent=False, report=None, properties=None, extra_verbose=False,
+         project_id=None):
     """ src_path: the path of the source directory
     build_path: the path of the build directory
     target: ['LPC1768', 'LPC11U24', 'LPC2368']
@@ -210,14 +260,16 @@
         src_paths = [src_paths]
 
     # The first path will give the name to the library
-    project_name = basename(abspath(absrc_paths[0] if src_paths[0] != "." and src_paths[0] != "./" else getcwd()))
+    project_name = basename(src_paths[0] if src_paths[0] != "." and src_paths[0] != "./" else getcwd())
     if name is None:
         # We will use default project name based on project folder name
         name = project_name
 
     if report != None:
         start = time()
-        id_name = name.upper()
+        
+        # If project_id is specified, use that over the default name
+        id_name = project_id.upper() if project_id else name.upper()
         description = name
         vendor_label = target.extra_labels[0]
         cur_result = None
@@ -276,6 +328,9 @@
         else:
             tmp_path = build_path
 
+        # Handle configuration
+        config = Config(target)
+
         # Copy headers, objects and static libraries
         for resource in resources:
             toolchain.copy_files(resource.headers, build_path, rel_path=resource.base_path)
@@ -283,6 +338,9 @@
             toolchain.copy_files(resource.libraries, build_path, rel_path=resource.base_path)
             if resource.linker_script:
                 toolchain.copy_files(resource.linker_script, build_path, rel_path=resource.base_path)
+            config.add_config_files(resource.json_files)
+
+        toolchain.add_macros(config.get_config_data_macros())
 
         # Compile Sources
         objects = []
@@ -305,7 +363,12 @@
     except Exception, e:
         if report != None:
             end = time()
-            cur_result["result"] = "FAIL"
+            
+            if isinstance(e, ToolException):
+                cur_result["result"] = "FAIL"
+            elif isinstance(e, NotSupportedException):
+                cur_result["result"] = "NOT_SUPPORTED"
+            
             cur_result["elapsed_time"] = end - start
 
             toolchain_output = toolchain.get_output()
@@ -901,4 +964,4 @@
     '969fc1867111': 2,
     '6b7f447ca868': 1,
     '82220227f4fa': 0,
-}
+}
\ No newline at end of file