Rizky Ardi Maulana / mbed-os
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 
00027 def get_default_options_parser (add_clean=True, add_options=True,
00028                                add_app_config=False):
00029     """Create a new options parser with the default compiler options added
00030 
00031     Keyword arguments:
00032     add_clean - add the clean argument?
00033     add_options - add the options argument?
00034     """
00035     parser = ArgumentParser()
00036 
00037     targetnames = TARGET_NAMES
00038     targetnames.sort()
00039     toolchainlist = list(TOOLCHAINS)
00040     toolchainlist.sort()
00041 
00042     parser.add_argument("-m", "--mcu",
00043                         help=("build for the given MCU (%s)" %
00044                               ', '.join(targetnames)),
00045                         metavar="MCU",
00046                         type=argparse_many(
00047                             argparse_force_uppercase_type(
00048                                 targetnames, "MCU")))
00049 
00050     parser.add_argument("-t", "--tool",
00051                         help=("build using the given TOOLCHAIN (%s)" %
00052                               ', '.join(toolchainlist)),
00053                         metavar="TOOLCHAIN",
00054                         type=argparse_many(
00055                             argparse_force_uppercase_type(
00056                                 toolchainlist, "toolchain")))
00057 
00058     parser.add_argument("--color",
00059                         help="print Warnings, and Errors in color",
00060                         action="store_true", default=False)
00061 
00062     parser.add_argument("--cflags", default=[], action="append",
00063                         help="Extra flags to provide to the C compiler")
00064 
00065     parser.add_argument("--asmflags", default=[], action="append",
00066                         help="Extra flags to provide to the assembler")
00067 
00068     parser.add_argument("--ldflags", default=[], action="append",
00069                         help="Extra flags to provide to the linker")
00070 
00071     if add_clean:
00072         parser.add_argument("-c", "--clean", action="store_true", default=False,
00073                             help="clean the build directory")
00074 
00075     if add_options:
00076         parser.add_argument("--profile", dest="profile", action="append",
00077                             type=argparse_profile_filestring_type,
00078                             help="Build profile to use. Can be either path to json" \
00079                             "file or one of the default one ({})".format(", ".join(list_profiles())),
00080                             default=[])
00081     if add_app_config:
00082         parser.add_argument("--app-config", default=None, dest="app_config",
00083                             type=argparse_filestring_type,
00084                             help="Path of an app configuration file (Default is to look for 'mbed_app.json')")
00085 
00086     return parser
00087 
00088 def list_profiles ():
00089     """Lists available build profiles
00090 
00091     Checks default profile directory (mbed-os/tools/profiles/) for all the json files and return list of names only
00092     """
00093     return [fn.replace(".json", "") for fn in listdir(join(dirname(__file__), "profiles")) if fn.endswith(".json")]
00094 
00095 def extract_profile (parser, options, toolchain):
00096     """Extract a Toolchain profile from parsed options
00097 
00098     Positional arguments:
00099     parser - parser used to parse the command line arguments
00100     options - The parsed command line arguments
00101     toolchain - the toolchain that the profile should be extracted for
00102     """
00103     profile = {'c': [], 'cxx': [], 'ld': [], 'common': [], 'asm': []}
00104     filenames = options.profile or [join(dirname(__file__), "profiles",
00105                                          "default.json")]
00106     for filename in filenames:
00107         contents = load(open(filename))
00108         try:
00109             for key in profile.iterkeys():
00110                 profile[key] += contents[toolchain][key]
00111         except KeyError:
00112             args_error(parser, ("argument --profile: toolchain {} is not"
00113                                 " supported by profile {}").format(toolchain,
00114                                                                    filename))
00115     return profile