From b248827341fa420f300d098435706d19a8b092b7 Mon Sep 17 00:00:00 2001 From: Emilio Monti Date: Fri, 16 Aug 2013 16:39:30 +0100 Subject: [PATCH] Add script to export mbed SDK tests to different IDEs --- workspace_tools/build.py | 13 +----- workspace_tools/export/__init__.py | 36 ++++++++++++++- workspace_tools/export_test.py | 31 ++++--------- workspace_tools/libraries.py | 1 - workspace_tools/make.py | 6 +-- workspace_tools/paths.py | 6 +++ workspace_tools/project.py | 70 ++++++++++++++++++++++++++++++ workspace_tools/tests.py | 8 ---- workspace_tools/utils.py | 6 +++ 9 files changed, 129 insertions(+), 48 deletions(-) create mode 100644 workspace_tools/project.py diff --git a/workspace_tools/build.py b/workspace_tools/build.py index d65e26d4c3..d57b54c819 100644 --- a/workspace_tools/build.py +++ b/workspace_tools/build.py @@ -40,10 +40,6 @@ if __name__ == '__main__': # Extra libraries parser.add_option("-r", "--rtos", action="store_true", dest="rtos", default=False, help="Compile the rtos") - parser.add_option("-b", "--debug", action="store_true", dest="debug", - default=False, help="Compile the debugging library") - parser.add_option("-f", "--fatfs", action="store_true", dest="fatfs", - default=False, help="Compile the fatfs") parser.add_option("-e", "--eth", action="store_true", dest="eth", default=False, help="Compile the ethernet library") parser.add_option("-V", "--vodafone", action="store_true", dest="vodafone", @@ -56,7 +52,6 @@ if __name__ == '__main__': default=False, help="Compile the DSP library") parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="Verbose diagnostic output") - (options, args) = parser.parse_args() # Get target list @@ -77,18 +72,14 @@ if __name__ == '__main__': # Additional Libraries if options.rtos: libraries.extend(["rtx", "rtos"]) - if options.debug: - libraries.append("debug") - if options.fatfs: - libraries.append("fatfs") - if options.usb_host: - libraries.append("usb_host") if options.eth: libraries.append("eth") if options.vodafone: libraries.append("vodafone") if options.usb: libraries.append("usb") + if options.usb_host: + libraries.append("usb_host") if options.dsp: libraries.extend(["cmsis_dsp", "dsp"]) diff --git a/workspace_tools/export/__init__.py b/workspace_tools/export/__init__.py index 66ee0b2ba3..3a95f35f14 100644 --- a/workspace_tools/export/__init__.py +++ b/workspace_tools/export/__init__.py @@ -15,8 +15,11 @@ See the License for the specific language governing permissions and limitations under the License. """ import os, tempfile -from os.path import join +from os.path import join, exists, basename +from os import makedirs +from shutil import copytree, rmtree +from workspace_tools.utils import mkdir from workspace_tools.export import uvision4, codesourcery, codered, gccarm, ds5_5, iar from workspace_tools.export.exporters import zip_working_directory_and_clean_up, OldLibrariesException @@ -80,3 +83,34 @@ def export(project_path, project_name, ide, target, destination='/tmp/', tempdir zip_path = zip_working_directory_and_clean_up(tempdir, destination, project_name, clean) return zip_path, report + + +############################################################################### +# Generate project folders following the online conventions +############################################################################### +def copy_tree(src, dst, clean=True): + if exists(dst): + if clean: + rmtree(dst) + else: + return + + copytree(src, dst) + + +def setup_user_prj(user_dir, prj_path, lib_paths=None): + """ + Setup a project with the same directory structure of the mbed online IDE + """ + mkdir(user_dir) + + # Project Path + copy_tree(prj_path, join(user_dir, "src")) + + # Project Libraries + user_lib = join(user_dir, "lib") + mkdir(user_lib) + + if lib_paths is not None: + for lib_path in lib_paths: + copy_tree(lib_path, join(user_lib, basename(lib_path))) diff --git a/workspace_tools/export_test.py b/workspace_tools/export_test.py index 811b44b871..877f1aeb30 100644 --- a/workspace_tools/export_test.py +++ b/workspace_tools/export_test.py @@ -19,36 +19,24 @@ from os.path import join, abspath, dirname, exists ROOT = abspath(join(dirname(__file__), "..")) sys.path.append(ROOT) +from shutil import move + from workspace_tools.paths import * -from workspace_tools.utils import mkdir, cmd, copy_file -from workspace_tools.export import export +from workspace_tools.utils import mkdir, cmd +from workspace_tools.export import export, setup_user_prj -from shutil import copytree - -EXPORT_DIR = join(BUILD_DIR, "export_test") -USER_WORKSPACE = join(EXPORT_DIR, "user_workspace") USR_PRJ_NAME = "usr_prj" -USER_PRJ = join(USER_WORKSPACE, USR_PRJ_NAME) -USER_LIB = join(USER_PRJ, "lib") +USER_PRJ = join(EXPORT_WORKSPACE, USR_PRJ_NAME) USER_SRC = join(USER_PRJ, "src") -TEMP = join(USER_WORKSPACE, ".temp") - def setup_test_user_prj(): if exists(USER_PRJ): print 'Test user project already generated...' return - # Build project directory structure - for d in [USER_LIB, USER_SRC]: - mkdir(d) - - # Sources - print 'Copying sources...' - copy_file(join(TEST_DIR, "rtos", "mbed", "basic", "main.cpp"), join(USER_SRC, "main.cpp")) - copytree(join(LIB_DIR, "rtos"), join(USER_LIB, "rtos")) + setup_user_prj(USER_PRJ, join(TEST_DIR, "rtos", "mbed", "basic"), [join(LIB_DIR, "rtos")]) # FAKE BUILD URL open(join(USER_SRC, "mbed.bld"), 'w').write("http://mbed.org/users/mbed_official/code/mbed/builds/976df7c37ad5\n") @@ -61,17 +49,16 @@ def fake_build_url_resolver(url): def test_export(toolchain, target, expected_error=None): if toolchain is None and target is None: - base_dir = join(TEMP, "zip") + base_dir = join(EXPORT_TMP, "zip") else: - base_dir = join(TEMP, toolchain, target) + base_dir = join(EXPORT_TMP, toolchain, target) temp_dir = join(base_dir, "temp") mkdir(temp_dir) zip_path, report = export(USER_PRJ, USR_PRJ_NAME, toolchain, target, base_dir, temp_dir, False, fake_build_url_resolver) if report['success']: - export_name = join(EXPORT_DIR, "export_%s_%s.zip" % (toolchain, target)) - cmd(["mv", zip_path, export_name]) + move(zip_path, join(EXPORT_DIR, "export_%s_%s.zip" % (toolchain, target))) print "[OK]" else: if expected_error is None: diff --git a/workspace_tools/libraries.py b/workspace_tools/libraries.py index 453922a17e..915f80e531 100644 --- a/workspace_tools/libraries.py +++ b/workspace_tools/libraries.py @@ -71,7 +71,6 @@ LIBRARIES = [ "source_dir": [ETH_SOURCES, LWIP_SOURCES], "build_dir": ETH_LIBRARY, "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_SOURCES, LWIP_SOURCES], - # "supported": CORTEX_ARM_SUPPORT }, { diff --git a/workspace_tools/make.py b/workspace_tools/make.py index d9312df4e1..2abca9a22b 100644 --- a/workspace_tools/make.py +++ b/workspace_tools/make.py @@ -32,16 +32,12 @@ from workspace_tools.build_api import build_project from workspace_tools.tests import TESTS, Test, TEST_MAP from workspace_tools.paths import BUILD_DIR, RTOS_LIBRARIES from workspace_tools.targets import TARGET_MAP +from workspace_tools.utils import args_error try: import workspace_tools.private_settings as ps except: ps = object() -def args_error(parser, message): - print "\n\n%s\n\n" % message - parser.print_help() - sys.exit() - if __name__ == '__main__': # Parse Options diff --git a/workspace_tools/paths.py b/workspace_tools/paths.py index f8ffd65a8c..94341a9ba4 100644 --- a/workspace_tools/paths.py +++ b/workspace_tools/paths.py @@ -78,3 +78,9 @@ USB_LIBRARIES = join(BUILD_DIR, "usb") # USB Host USB_HOST = join(LIB_DIR, "USBHost") USB_HOST_LIBRARIES = join(BUILD_DIR, "usb_host") + +# Export +EXPORT_DIR = join(BUILD_DIR, "export") +EXPORT_WORKSPACE = join(EXPORT_DIR, "workspace") +EXPORT_TMP = join(EXPORT_DIR, ".temp") + diff --git a/workspace_tools/project.py b/workspace_tools/project.py new file mode 100644 index 0000000000..8296fd3ff2 --- /dev/null +++ b/workspace_tools/project.py @@ -0,0 +1,70 @@ +import sys +from os.path import join, abspath, dirname, exists +ROOT = abspath(join(dirname(__file__), "..")) +sys.path.append(ROOT) + +from shutil import move +from optparse import OptionParser + +from workspace_tools.paths import EXPORT_DIR, EXPORT_WORKSPACE, EXPORT_TMP +from workspace_tools.paths import MBED_BASE, MBED_LIBRARIES +from workspace_tools.export import export, setup_user_prj, EXPORTERS +from workspace_tools.utils import args_error +from workspace_tools.tests import TESTS, Test, TEST_MAP +from workspace_tools.targets import TARGET_NAMES + + +if __name__ == '__main__': + # Parse Options + parser = OptionParser() + + parser.add_option("-m", "--mcu", metavar="MCU", default='LPC1768', + help="generate project for the given MCU (%s)" % ', '.join(TARGET_NAMES)) + + parser.add_option("-p", type="int", dest="program", + help="The index of the desired test program: [0-%d]" % (len(TESTS)-1)) + + parser.add_option("-i", dest="ide", default='uvision', + help="The target IDE: %s" % str(EXPORTERS.keys())) + + parser.add_option("-b", dest="build", action="store_true", default=False, + help="Use the mbed library build, instead of the sources") + + (options, args) = parser.parse_args() + + # Target + if options.mcu is None : + args_error(parser, "[ERROR] You should specify an MCU") + mcu = options.mcu + + # IDE + if options.ide is None: + args_error(parser, "[ERROR] You should specify an IDE") + ide = options.ide + + # Project + if options.program is None or (options.program < 0) or (options.program > (len(TESTS)-1)): + message = "[ERROR] You have to specify one of the following tests:\n" + message += '\n'.join(map(str, sorted(TEST_MAP.values()))) + args_error(parser, message) + test = Test(options.program) + + if not options.build: + # Substitute the library builds with the sources + # TODO: Substitute also the other library build paths + if MBED_LIBRARIES in test.dependencies: + test.dependencies.remove(MBED_LIBRARIES) + test.dependencies.append(MBED_BASE) + + # Build the projectwith the same directory structure of the mbed online IDE + project_dir = join(EXPORT_WORKSPACE, test.id) + setup_user_prj(project_dir, test.source_dir, test.dependencies) + + # Export to selected toolchain + tmp_path, report = export(project_dir, test.id, ide, mcu, EXPORT_WORKSPACE, EXPORT_TMP) + if report['success']: + zip_path = join(EXPORT_DIR, "%s_%s_%s.zip" % (test.id, ide, mcu)) + move(tmp_path, zip_path) + print "[OK]" + else: + print '[ERRROR] %s' % report['errormsg'] diff --git a/workspace_tools/tests.py b/workspace_tools/tests.py index 319faf3c95..cd4faec329 100644 --- a/workspace_tools/tests.py +++ b/workspace_tools/tests.py @@ -503,49 +503,41 @@ TESTS = [ "id": "NET_1", "description": "TCP client hello world", "source_dir": join(TEST_DIR, "net", "helloworld", "tcpclient"), "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], - # "supported": CORTEX_ARM_SUPPORT, }, { "id": "NET_2", "description": "UDP client hello world", "source_dir": join(TEST_DIR, "net", "helloworld", "udpclient"), "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], - # "supported": CORTEX_ARM_SUPPORT, }, { "id": "NET_3", "description": "TCP echo server", "source_dir": join(TEST_DIR, "net", "echo", "tcp_server"), "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], - # "supported": CORTEX_ARM_SUPPORT, }, { "id": "NET_4", "description": "TCP echo client", "source_dir": join(TEST_DIR, "net", "echo", "tcp_client"), "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], - # "supported": CORTEX_ARM_SUPPORT, }, { "id": "NET_5", "description": "UDP echo server", "source_dir": join(TEST_DIR, "net", "echo", "udp_server"), "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], - # "supported": CORTEX_ARM_SUPPORT, }, { "id": "NET_6", "description": "UDP echo client", "source_dir": join(TEST_DIR, "net", "echo", "udp_client"), "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], - # "supported": CORTEX_ARM_SUPPORT, }, { "id": "NET_7", "description": "HTTP client", "source_dir": join(TEST_DIR, "net", "protocols", "HTTPClient_HelloWorld"), "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], - # "supported": CORTEX_ARM_SUPPORT, }, { "id": "NET_8", "description": "NTP client", "source_dir": join(TEST_DIR, "net", "protocols", "NTPClient_HelloWorld"), "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], - # "supported": CORTEX_ARM_SUPPORT, }, { "id": "NET_9", "description": "Multicast Send", diff --git a/workspace_tools/utils.py b/workspace_tools/utils.py index 27a601f5fb..ebcc36adb6 100644 --- a/workspace_tools/utils.py +++ b/workspace_tools/utils.py @@ -84,3 +84,9 @@ def split_path(path): base, file = split(path) name, ext = splitext(file) return base, name, ext + + +def args_error(parser, message): + print "\n\n%s\n\n" % message + parser.print_help() + sys.exit()