Clone of official tools
Diff: options.py
- Revision:
- 31:8ea194f6145b
- Parent:
- 30:f12ce67666d0
- Child:
- 35:da9c89f8be7d
diff -r f12ce67666d0 -r 8ea194f6145b options.py --- a/options.py Mon Aug 29 11:56:59 2016 +0100 +++ b/options.py Wed Jan 04 11:58:24 2017 -0600 @@ -14,13 +14,23 @@ See the License for the specific language governing permissions and limitations under the License. """ +from json import load +from os.path import join, dirname +from os import listdir from argparse import ArgumentParser from tools.toolchains import TOOLCHAINS from tools.targets import TARGET_NAMES from tools.utils import argparse_force_uppercase_type, \ - argparse_lowercase_hyphen_type, argparse_many + argparse_lowercase_hyphen_type, argparse_many, \ + argparse_filestring_type, args_error, argparse_profile_filestring_type,\ + argparse_deprecate -def get_default_options_parser(add_clean=True, add_options=True): +FLAGS_DEPRECATION_MESSAGE = "Please use the --profile argument instead.\n"\ + "Documentation may be found in "\ + "docs/Toolchain_Profiles.md" + +def get_default_options_parser(add_clean=True, add_options=True, + add_app_config=False): """Create a new options parser with the default compiler options added Keyword arguments: @@ -54,28 +64,60 @@ help="print Warnings, and Errors in color", action="store_true", default=False) - parser.add_argument("--cflags", default=[], action="append", - help="Extra flags to provide to the C compiler") + parser.add_argument("--cflags", + type=argparse_deprecate(FLAGS_DEPRECATION_MESSAGE), + help="Deprecated. " + FLAGS_DEPRECATION_MESSAGE) - parser.add_argument("--asmflags", default=[], action="append", - help="Extra flags to provide to the assembler") + parser.add_argument("--asmflags", + type=argparse_deprecate(FLAGS_DEPRECATION_MESSAGE), + help="Deprecated. " + FLAGS_DEPRECATION_MESSAGE) - parser.add_argument("--ldflags", default=[], action="append", - help="Extra flags to provide to the linker") + parser.add_argument("--ldflags", + type=argparse_deprecate(FLAGS_DEPRECATION_MESSAGE), + help="Deprecated. " + FLAGS_DEPRECATION_MESSAGE) if add_clean: parser.add_argument("-c", "--clean", action="store_true", default=False, help="clean the build directory") if add_options: - parser.add_argument("-o", "--options", action="append", - help=('Add a build argument ("save-asm": save the ' - 'asm generated by the compiler, "debug-info":' - ' generate debugging information, "analyze": ' - 'run Goanna static code analyzer")'), - type=argparse_lowercase_hyphen_type(['save-asm', - 'debug-info', - 'analyze'], - "build option")) + parser.add_argument("--profile", dest="profile", action="append", + type=argparse_profile_filestring_type, + help="Build profile to use. Can be either path to json" \ + "file or one of the default one ({})".format(", ".join(list_profiles())), + default=[]) + if add_app_config: + parser.add_argument("--app-config", default=None, dest="app_config", + type=argparse_filestring_type, + help="Path of an app configuration file (Default is to look for 'mbed_app.json')") return parser + +def list_profiles(): + """Lists available build profiles + + Checks default profile directory (mbed-os/tools/profiles/) for all the json files and return list of names only + """ + return [fn.replace(".json", "") for fn in listdir(join(dirname(__file__), "profiles")) if fn.endswith(".json")] + +def extract_profile(parser, options, toolchain): + """Extract a Toolchain profile from parsed options + + Positional arguments: + parser - parser used to parse the command line arguments + options - The parsed command line arguments + toolchain - the toolchain that the profile should be extracted for + """ + profile = {'c': [], 'cxx': [], 'ld': [], 'common': [], 'asm': []} + filenames = options.profile or [join(dirname(__file__), "profiles", + "default.json")] + for filename in filenames: + contents = load(open(filename)) + try: + for key in profile.iterkeys(): + profile[key] += contents[toolchain][key] + except KeyError: + args_error(parser, ("argument --profile: toolchain {} is not" + " supported by profile {}").format(toolchain, + filename)) + return profile