the other jimmy / mbed-sdk-tools

Fork of mbed-sdk-tools by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers options.py Source File

options.py

00001 """
00002 mbed SDK
00003 Copyright (c) 2011-2013 ARM Limited
00004 
00005 Licensed under the Apache License, Version 2.0 (the "License");
00006 you may not use this file except in compliance with the License.
00007 You may obtain a copy of the License at
00008 
00009     http://www.apache.org/licenses/LICENSE-2.0
00010 
00011 Unless required by applicable law or agreed to in writing, software
00012 distributed under the License is distributed on an "AS IS" BASIS,
00013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 See the License for the specific language governing permissions and
00015 limitations under the License.
00016 """
00017 from json import load
00018 from os.path import join, dirname
00019 from os import listdir
00020 from argparse import ArgumentParser
00021 from tools.toolchains import TOOLCHAINS
00022 from tools.targets import TARGET_NAMES
00023 from tools.utils import argparse_force_uppercase_type, \
00024     argparse_lowercase_hyphen_type, argparse_many, \
00025     argparse_filestring_type, args_error, argparse_profile_filestring_type,\
00026     argparse_deprecate
00027 
00028 FLAGS_DEPRECATION_MESSAGE = "Please use the --profile argument instead.\n"\
00029                             "Documentation may be found in "\
00030                             "docs/Toolchain_Profiles.md"
00031 
00032 def get_default_options_parser (add_clean=True, add_options=True,
00033                                add_app_config=False):
00034     """Create a new options parser with the default compiler options added
00035 
00036     Keyword arguments:
00037     add_clean - add the clean argument?
00038     add_options - add the options argument?
00039     """
00040     parser = ArgumentParser()
00041 
00042     targetnames = TARGET_NAMES
00043     targetnames.sort()
00044     toolchainlist = list(TOOLCHAINS)
00045     toolchainlist.sort()
00046 
00047     parser.add_argument("-m", "--mcu",
00048                         help=("build for the given MCU (%s)" %
00049                               ', '.join(targetnames)),
00050                         metavar="MCU",
00051                         type=argparse_many(
00052                             argparse_force_uppercase_type(
00053                                 targetnames, "MCU")))
00054 
00055     parser.add_argument("-t", "--tool",
00056                         help=("build using the given TOOLCHAIN (%s)" %
00057                               ', '.join(toolchainlist)),
00058                         metavar="TOOLCHAIN",
00059                         type=argparse_many(
00060                             argparse_force_uppercase_type(
00061                                 toolchainlist, "toolchain")))
00062 
00063     parser.add_argument("--color",
00064                         help="print Warnings, and Errors in color",
00065                         action="store_true", default=False)
00066 
00067     parser.add_argument("--cflags",
00068                         type=argparse_deprecate(FLAGS_DEPRECATION_MESSAGE),
00069                         help="Deprecated. " + FLAGS_DEPRECATION_MESSAGE)
00070 
00071     parser.add_argument("--asmflags",
00072                         type=argparse_deprecate(FLAGS_DEPRECATION_MESSAGE),
00073                         help="Deprecated. " + FLAGS_DEPRECATION_MESSAGE)
00074 
00075     parser.add_argument("--ldflags",
00076                         type=argparse_deprecate(FLAGS_DEPRECATION_MESSAGE),
00077                         help="Deprecated. " + FLAGS_DEPRECATION_MESSAGE)
00078 
00079     if add_clean:
00080         parser.add_argument("-c", "--clean", action="store_true", default=False,
00081                             help="clean the build directory")
00082 
00083     if add_options:
00084         parser.add_argument("--profile", dest="profile", action="append",
00085                             type=argparse_profile_filestring_type,
00086                             help="Build profile to use. Can be either path to json" \
00087                             "file or one of the default one ({})".format(", ".join(list_profiles())),
00088                             default=[])
00089     if add_app_config:
00090         parser.add_argument("--app-config", default=None, dest="app_config",
00091                             type=argparse_filestring_type,
00092                             help="Path of an app configuration file (Default is to look for 'mbed_app.json')")
00093 
00094     return parser
00095 
00096 def list_profiles ():
00097     """Lists available build profiles
00098 
00099     Checks default profile directory (mbed-os/tools/profiles/) for all the json files and return list of names only
00100     """
00101     return [fn.replace(".json", "") for fn in listdir(join(dirname(__file__), "profiles")) if fn.endswith(".json")]
00102 
00103 def extract_profile (parser, options, toolchain):
00104     """Extract a Toolchain profile from parsed options
00105 
00106     Positional arguments:
00107     parser - parser used to parse the command line arguments
00108     options - The parsed command line arguments
00109     toolchain - the toolchain that the profile should be extracted for
00110     """
00111     profile = {'c': [], 'cxx': [], 'ld': [], 'common': [], 'asm': []}
00112     filenames = options.profile or [join(dirname(__file__), "profiles",
00113                                          "default.json")]
00114     for filename in filenames:
00115         contents = load(open(filename))
00116         try:
00117             for key in profile.iterkeys():
00118                 profile[key] += contents[toolchain][key]
00119         except KeyError:
00120             args_error(parser, ("argument --profile: toolchain {} is not"
00121                                 " supported by profile {}").format(toolchain,
00122                                                                    filename))
00123     return profile