Clone of official tools
options.py@43:2a7da56ebd24, 2018-09-25 (annotated)
- Committer:
- theotherjimmy
- Date:
- Tue Sep 25 13:43:09 2018 -0500
- Revision:
- 43:2a7da56ebd24
- Parent:
- 38:399953da035d
Release 5.10.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 0:66f3b5499f7f | 1 | """ |
screamer | 0:66f3b5499f7f | 2 | mbed SDK |
screamer | 0:66f3b5499f7f | 3 | Copyright (c) 2011-2013 ARM Limited |
screamer | 0:66f3b5499f7f | 4 | |
screamer | 0:66f3b5499f7f | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
screamer | 0:66f3b5499f7f | 6 | you may not use this file except in compliance with the License. |
screamer | 0:66f3b5499f7f | 7 | You may obtain a copy of the License at |
screamer | 0:66f3b5499f7f | 8 | |
screamer | 0:66f3b5499f7f | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
screamer | 0:66f3b5499f7f | 10 | |
screamer | 0:66f3b5499f7f | 11 | Unless required by applicable law or agreed to in writing, software |
screamer | 0:66f3b5499f7f | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
screamer | 0:66f3b5499f7f | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
screamer | 0:66f3b5499f7f | 14 | See the License for the specific language governing permissions and |
screamer | 0:66f3b5499f7f | 15 | limitations under the License. |
screamer | 0:66f3b5499f7f | 16 | """ |
theotherjimmy |
43:2a7da56ebd24 | 17 | from __future__ import print_function, division, absolute_import |
theotherjimmy |
43:2a7da56ebd24 | 18 | |
The Other Jimmy |
31:8ea194f6145b | 19 | from json import load |
The Other Jimmy |
31:8ea194f6145b | 20 | from os.path import join, dirname |
The Other Jimmy |
31:8ea194f6145b | 21 | from os import listdir |
The Other Jimmy |
38:399953da035d | 22 | from argparse import ArgumentParser, ArgumentTypeError |
theotherjimmy |
43:2a7da56ebd24 | 23 | |
theotherjimmy |
43:2a7da56ebd24 | 24 | from .toolchains import TOOLCHAINS |
theotherjimmy |
43:2a7da56ebd24 | 25 | from .targets import TARGET_NAMES, Target, update_target_data |
theotherjimmy |
43:2a7da56ebd24 | 26 | from .utils import (argparse_force_uppercase_type, argparse_deprecate, |
theotherjimmy |
43:2a7da56ebd24 | 27 | argparse_lowercase_hyphen_type, argparse_many, |
theotherjimmy |
43:2a7da56ebd24 | 28 | argparse_filestring_type, args_error, |
theotherjimmy |
43:2a7da56ebd24 | 29 | argparse_profile_filestring_type) |
screamer | 0:66f3b5499f7f | 30 | |
The Other Jimmy |
31:8ea194f6145b | 31 | FLAGS_DEPRECATION_MESSAGE = "Please use the --profile argument instead.\n"\ |
The Other Jimmy |
31:8ea194f6145b | 32 | "Documentation may be found in "\ |
The Other Jimmy |
31:8ea194f6145b | 33 | "docs/Toolchain_Profiles.md" |
The Other Jimmy |
31:8ea194f6145b | 34 | |
The Other Jimmy |
31:8ea194f6145b | 35 | def get_default_options_parser(add_clean=True, add_options=True, |
The Other Jimmy |
31:8ea194f6145b | 36 | add_app_config=False): |
screamer | 29:1210849dba19 | 37 | """Create a new options parser with the default compiler options added |
screamer | 29:1210849dba19 | 38 | |
screamer | 29:1210849dba19 | 39 | Keyword arguments: |
screamer | 29:1210849dba19 | 40 | add_clean - add the clean argument? |
screamer | 29:1210849dba19 | 41 | add_options - add the options argument? |
screamer | 29:1210849dba19 | 42 | """ |
screamer | 22:9e85236d8716 | 43 | parser = ArgumentParser() |
screamer | 0:66f3b5499f7f | 44 | |
screamer | 0:66f3b5499f7f | 45 | targetnames = TARGET_NAMES |
screamer | 0:66f3b5499f7f | 46 | targetnames.sort() |
screamer | 0:66f3b5499f7f | 47 | toolchainlist = list(TOOLCHAINS) |
screamer | 0:66f3b5499f7f | 48 | toolchainlist.sort() |
screamer | 0:66f3b5499f7f | 49 | |
screamer | 22:9e85236d8716 | 50 | parser.add_argument("-m", "--mcu", |
screamer | 29:1210849dba19 | 51 | help=("build for the given MCU (%s)" % |
screamer | 29:1210849dba19 | 52 | ', '.join(targetnames)), |
The Other Jimmy |
38:399953da035d | 53 | metavar="MCU") |
screamer | 0:66f3b5499f7f | 54 | |
screamer | 22:9e85236d8716 | 55 | parser.add_argument("-t", "--tool", |
screamer | 29:1210849dba19 | 56 | help=("build using the given TOOLCHAIN (%s)" % |
screamer | 29:1210849dba19 | 57 | ', '.join(toolchainlist)), |
screamer | 29:1210849dba19 | 58 | metavar="TOOLCHAIN", |
screamer | 29:1210849dba19 | 59 | type=argparse_many( |
screamer | 29:1210849dba19 | 60 | argparse_force_uppercase_type( |
screamer | 29:1210849dba19 | 61 | toolchainlist, "toolchain"))) |
screamer | 22:9e85236d8716 | 62 | |
screamer | 22:9e85236d8716 | 63 | parser.add_argument("--color", |
screamer | 22:9e85236d8716 | 64 | help="print Warnings, and Errors in color", |
screamer | 22:9e85236d8716 | 65 | action="store_true", default=False) |
screamer | 0:66f3b5499f7f | 66 | |
The Other Jimmy |
31:8ea194f6145b | 67 | parser.add_argument("--cflags", |
The Other Jimmy |
31:8ea194f6145b | 68 | type=argparse_deprecate(FLAGS_DEPRECATION_MESSAGE), |
The Other Jimmy |
31:8ea194f6145b | 69 | help="Deprecated. " + FLAGS_DEPRECATION_MESSAGE) |
screamer | 29:1210849dba19 | 70 | |
The Other Jimmy |
31:8ea194f6145b | 71 | parser.add_argument("--asmflags", |
The Other Jimmy |
31:8ea194f6145b | 72 | type=argparse_deprecate(FLAGS_DEPRECATION_MESSAGE), |
The Other Jimmy |
31:8ea194f6145b | 73 | help="Deprecated. " + FLAGS_DEPRECATION_MESSAGE) |
screamer | 29:1210849dba19 | 74 | |
The Other Jimmy |
31:8ea194f6145b | 75 | parser.add_argument("--ldflags", |
The Other Jimmy |
31:8ea194f6145b | 76 | type=argparse_deprecate(FLAGS_DEPRECATION_MESSAGE), |
The Other Jimmy |
31:8ea194f6145b | 77 | help="Deprecated. " + FLAGS_DEPRECATION_MESSAGE) |
screamer | 29:1210849dba19 | 78 | |
screamer | 13:ab47a20b66f0 | 79 | if add_clean: |
screamer | 22:9e85236d8716 | 80 | parser.add_argument("-c", "--clean", action="store_true", default=False, |
screamer | 29:1210849dba19 | 81 | help="clean the build directory") |
screamer | 0:66f3b5499f7f | 82 | |
screamer | 13:ab47a20b66f0 | 83 | if add_options: |
The Other Jimmy |
31:8ea194f6145b | 84 | parser.add_argument("--profile", dest="profile", action="append", |
The Other Jimmy |
31:8ea194f6145b | 85 | type=argparse_profile_filestring_type, |
The Other Jimmy |
31:8ea194f6145b | 86 | help="Build profile to use. Can be either path to json" \ |
The Other Jimmy |
31:8ea194f6145b | 87 | "file or one of the default one ({})".format(", ".join(list_profiles())), |
The Other Jimmy |
31:8ea194f6145b | 88 | default=[]) |
The Other Jimmy |
31:8ea194f6145b | 89 | if add_app_config: |
The Other Jimmy |
31:8ea194f6145b | 90 | parser.add_argument("--app-config", default=None, dest="app_config", |
The Other Jimmy |
31:8ea194f6145b | 91 | type=argparse_filestring_type, |
The Other Jimmy |
31:8ea194f6145b | 92 | help="Path of an app configuration file (Default is to look for 'mbed_app.json')") |
screamer | 0:66f3b5499f7f | 93 | |
screamer | 0:66f3b5499f7f | 94 | return parser |
The Other Jimmy |
31:8ea194f6145b | 95 | |
The Other Jimmy |
31:8ea194f6145b | 96 | def list_profiles(): |
The Other Jimmy |
31:8ea194f6145b | 97 | """Lists available build profiles |
The Other Jimmy |
31:8ea194f6145b | 98 | |
The Other Jimmy |
31:8ea194f6145b | 99 | Checks default profile directory (mbed-os/tools/profiles/) for all the json files and return list of names only |
The Other Jimmy |
31:8ea194f6145b | 100 | """ |
The Other Jimmy |
31:8ea194f6145b | 101 | return [fn.replace(".json", "") for fn in listdir(join(dirname(__file__), "profiles")) if fn.endswith(".json")] |
The Other Jimmy |
31:8ea194f6145b | 102 | |
The Other Jimmy |
36:96847d42f010 | 103 | def extract_profile(parser, options, toolchain, fallback="develop"): |
The Other Jimmy |
31:8ea194f6145b | 104 | """Extract a Toolchain profile from parsed options |
The Other Jimmy |
31:8ea194f6145b | 105 | |
The Other Jimmy |
31:8ea194f6145b | 106 | Positional arguments: |
The Other Jimmy |
31:8ea194f6145b | 107 | parser - parser used to parse the command line arguments |
The Other Jimmy |
31:8ea194f6145b | 108 | options - The parsed command line arguments |
The Other Jimmy |
31:8ea194f6145b | 109 | toolchain - the toolchain that the profile should be extracted for |
The Other Jimmy |
31:8ea194f6145b | 110 | """ |
The Other Jimmy |
36:96847d42f010 | 111 | profiles = [] |
The Other Jimmy |
31:8ea194f6145b | 112 | filenames = options.profile or [join(dirname(__file__), "profiles", |
The Other Jimmy |
35:da9c89f8be7d | 113 | fallback + ".json")] |
The Other Jimmy |
31:8ea194f6145b | 114 | for filename in filenames: |
The Other Jimmy |
31:8ea194f6145b | 115 | contents = load(open(filename)) |
The Other Jimmy |
36:96847d42f010 | 116 | if toolchain not in contents: |
The Other Jimmy |
31:8ea194f6145b | 117 | args_error(parser, ("argument --profile: toolchain {} is not" |
The Other Jimmy |
31:8ea194f6145b | 118 | " supported by profile {}").format(toolchain, |
The Other Jimmy |
31:8ea194f6145b | 119 | filename)) |
The Other Jimmy |
36:96847d42f010 | 120 | profiles.append(contents) |
The Other Jimmy |
36:96847d42f010 | 121 | |
The Other Jimmy |
36:96847d42f010 | 122 | return profiles |
The Other Jimmy |
38:399953da035d | 123 | |
The Other Jimmy |
38:399953da035d | 124 | def extract_mcus(parser, options): |
The Other Jimmy |
38:399953da035d | 125 | try: |
The Other Jimmy |
38:399953da035d | 126 | if options.source_dir: |
The Other Jimmy |
38:399953da035d | 127 | for source_dir in options.source_dir: |
The Other Jimmy |
38:399953da035d | 128 | Target.add_extra_targets(source_dir) |
The Other Jimmy |
38:399953da035d | 129 | update_target_data() |
The Other Jimmy |
38:399953da035d | 130 | except KeyError: |
The Other Jimmy |
38:399953da035d | 131 | pass |
The Other Jimmy |
38:399953da035d | 132 | targetnames = TARGET_NAMES |
The Other Jimmy |
38:399953da035d | 133 | targetnames.sort() |
The Other Jimmy |
38:399953da035d | 134 | try: |
The Other Jimmy |
38:399953da035d | 135 | return argparse_many(argparse_force_uppercase_type(targetnames, "MCU"))(options.mcu) |
The Other Jimmy |
38:399953da035d | 136 | except ArgumentTypeError as exc: |
The Other Jimmy |
38:399953da035d | 137 | args_error(parser, "argument -m/--mcu: {}".format(str(exc))) |
The Other Jimmy |
38:399953da035d | 138 |