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 __future__ import print_function, division, absolute_import
00018 
00019 from json import load
00020 from os.path import join, dirname
00021 from os import listdir
00022 from argparse import ArgumentParser, ArgumentTypeError
00023 
00024 from .toolchains import TOOLCHAINS
00025 from .targets import TARGET_NAMES, Target, update_target_data
00026 from .utils import (argparse_force_uppercase_type, argparse_deprecate,
00027                     argparse_lowercase_hyphen_type, argparse_many,
00028                     argparse_filestring_type, args_error,
00029                     argparse_profile_filestring_type)
00030 
00031 FLAGS_DEPRECATION_MESSAGE = "Please use the --profile argument instead.\n"\
00032                             "Documentation may be found in "\
00033                             "docs/Toolchain_Profiles.md"
00034 
00035 def get_default_options_parser (add_clean=True, add_options=True,
00036                                add_app_config=False):
00037     """Create a new options parser with the default compiler options added
00038 
00039     Keyword arguments:
00040     add_clean - add the clean argument?
00041     add_options - add the options argument?
00042     """
00043     parser = ArgumentParser()
00044 
00045     targetnames = TARGET_NAMES
00046     targetnames.sort()
00047     toolchainlist = list(TOOLCHAINS)
00048     toolchainlist.sort()
00049 
00050     parser.add_argument("-m", "--mcu",
00051                         help=("build for the given MCU (%s)" %
00052                               ', '.join(targetnames)),
00053                         metavar="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, fallback="develop"):
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     profiles = []
00112     filenames = options.profile or [join(dirname(__file__), "profiles",
00113                                          fallback + ".json")]
00114     for filename in filenames:
00115         contents = load(open(filename))
00116         if toolchain not in contents:
00117             args_error(parser, ("argument --profile: toolchain {} is not"
00118                                 " supported by profile {}").format(toolchain,
00119                                                                    filename))
00120         profiles.append(contents)
00121 
00122     return profiles
00123     
00124 def extract_mcus(parser, options):
00125     try:
00126         if options.source_dir:
00127             for source_dir in options.source_dir:
00128                 Target.add_extra_targets(source_dir)
00129             update_target_data()
00130     except KeyError:
00131         pass
00132     targetnames = TARGET_NAMES
00133     targetnames.sort()
00134     try:
00135         return argparse_many(argparse_force_uppercase_type(targetnames, "MCU"))(options.mcu)
00136     except ArgumentTypeError as exc:
00137         args_error(parser, "argument -m/--mcu: {}".format(str(exc)))
00138