mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 """
elessair 0:f269e3021894 2 mbed SDK
elessair 0:f269e3021894 3 Copyright (c) 2011-2013 ARM Limited
elessair 0:f269e3021894 4
elessair 0:f269e3021894 5 Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:f269e3021894 6 you may not use this file except in compliance with the License.
elessair 0:f269e3021894 7 You may obtain a copy of the License at
elessair 0:f269e3021894 8
elessair 0:f269e3021894 9 http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 10
elessair 0:f269e3021894 11 Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 12 distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:f269e3021894 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 14 See the License for the specific language governing permissions and
elessair 0:f269e3021894 15 limitations under the License.
elessair 0:f269e3021894 16 """
elessair 0:f269e3021894 17 from json import load
elessair 0:f269e3021894 18 from os.path import join, dirname
elessair 0:f269e3021894 19 from os import listdir
elessair 0:f269e3021894 20 from argparse import ArgumentParser
elessair 0:f269e3021894 21 from tools.toolchains import TOOLCHAINS
elessair 0:f269e3021894 22 from tools.targets import TARGET_NAMES
elessair 0:f269e3021894 23 from tools.utils import argparse_force_uppercase_type, \
elessair 0:f269e3021894 24 argparse_lowercase_hyphen_type, argparse_many, \
elessair 0:f269e3021894 25 argparse_filestring_type, args_error, argparse_profile_filestring_type
elessair 0:f269e3021894 26
elessair 0:f269e3021894 27 def get_default_options_parser(add_clean=True, add_options=True,
elessair 0:f269e3021894 28 add_app_config=False):
elessair 0:f269e3021894 29 """Create a new options parser with the default compiler options added
elessair 0:f269e3021894 30
elessair 0:f269e3021894 31 Keyword arguments:
elessair 0:f269e3021894 32 add_clean - add the clean argument?
elessair 0:f269e3021894 33 add_options - add the options argument?
elessair 0:f269e3021894 34 """
elessair 0:f269e3021894 35 parser = ArgumentParser()
elessair 0:f269e3021894 36
elessair 0:f269e3021894 37 targetnames = TARGET_NAMES
elessair 0:f269e3021894 38 targetnames.sort()
elessair 0:f269e3021894 39 toolchainlist = list(TOOLCHAINS)
elessair 0:f269e3021894 40 toolchainlist.sort()
elessair 0:f269e3021894 41
elessair 0:f269e3021894 42 parser.add_argument("-m", "--mcu",
elessair 0:f269e3021894 43 help=("build for the given MCU (%s)" %
elessair 0:f269e3021894 44 ', '.join(targetnames)),
elessair 0:f269e3021894 45 metavar="MCU",
elessair 0:f269e3021894 46 type=argparse_many(
elessair 0:f269e3021894 47 argparse_force_uppercase_type(
elessair 0:f269e3021894 48 targetnames, "MCU")))
elessair 0:f269e3021894 49
elessair 0:f269e3021894 50 parser.add_argument("-t", "--tool",
elessair 0:f269e3021894 51 help=("build using the given TOOLCHAIN (%s)" %
elessair 0:f269e3021894 52 ', '.join(toolchainlist)),
elessair 0:f269e3021894 53 metavar="TOOLCHAIN",
elessair 0:f269e3021894 54 type=argparse_many(
elessair 0:f269e3021894 55 argparse_force_uppercase_type(
elessair 0:f269e3021894 56 toolchainlist, "toolchain")))
elessair 0:f269e3021894 57
elessair 0:f269e3021894 58 parser.add_argument("--color",
elessair 0:f269e3021894 59 help="print Warnings, and Errors in color",
elessair 0:f269e3021894 60 action="store_true", default=False)
elessair 0:f269e3021894 61
elessair 0:f269e3021894 62 parser.add_argument("--cflags", default=[], action="append",
elessair 0:f269e3021894 63 help="Extra flags to provide to the C compiler")
elessair 0:f269e3021894 64
elessair 0:f269e3021894 65 parser.add_argument("--asmflags", default=[], action="append",
elessair 0:f269e3021894 66 help="Extra flags to provide to the assembler")
elessair 0:f269e3021894 67
elessair 0:f269e3021894 68 parser.add_argument("--ldflags", default=[], action="append",
elessair 0:f269e3021894 69 help="Extra flags to provide to the linker")
elessair 0:f269e3021894 70
elessair 0:f269e3021894 71 if add_clean:
elessair 0:f269e3021894 72 parser.add_argument("-c", "--clean", action="store_true", default=False,
elessair 0:f269e3021894 73 help="clean the build directory")
elessair 0:f269e3021894 74
elessair 0:f269e3021894 75 if add_options:
elessair 0:f269e3021894 76 parser.add_argument("--profile", dest="profile", action="append",
elessair 0:f269e3021894 77 type=argparse_profile_filestring_type,
elessair 0:f269e3021894 78 help="Build profile to use. Can be either path to json" \
elessair 0:f269e3021894 79 "file or one of the default one ({})".format(", ".join(list_profiles())),
elessair 0:f269e3021894 80 default=[])
elessair 0:f269e3021894 81 if add_app_config:
elessair 0:f269e3021894 82 parser.add_argument("--app-config", default=None, dest="app_config",
elessair 0:f269e3021894 83 type=argparse_filestring_type,
elessair 0:f269e3021894 84 help="Path of an app configuration file (Default is to look for 'mbed_app.json')")
elessair 0:f269e3021894 85
elessair 0:f269e3021894 86 return parser
elessair 0:f269e3021894 87
elessair 0:f269e3021894 88 def list_profiles():
elessair 0:f269e3021894 89 """Lists available build profiles
elessair 0:f269e3021894 90
elessair 0:f269e3021894 91 Checks default profile directory (mbed-os/tools/profiles/) for all the json files and return list of names only
elessair 0:f269e3021894 92 """
elessair 0:f269e3021894 93 return [fn.replace(".json", "") for fn in listdir(join(dirname(__file__), "profiles")) if fn.endswith(".json")]
elessair 0:f269e3021894 94
elessair 0:f269e3021894 95 def extract_profile(parser, options, toolchain):
elessair 0:f269e3021894 96 """Extract a Toolchain profile from parsed options
elessair 0:f269e3021894 97
elessair 0:f269e3021894 98 Positional arguments:
elessair 0:f269e3021894 99 parser - parser used to parse the command line arguments
elessair 0:f269e3021894 100 options - The parsed command line arguments
elessair 0:f269e3021894 101 toolchain - the toolchain that the profile should be extracted for
elessair 0:f269e3021894 102 """
elessair 0:f269e3021894 103 profile = {'c': [], 'cxx': [], 'ld': [], 'common': [], 'asm': []}
elessair 0:f269e3021894 104 filenames = options.profile or [join(dirname(__file__), "profiles",
elessair 0:f269e3021894 105 "default.json")]
elessair 0:f269e3021894 106 for filename in filenames:
elessair 0:f269e3021894 107 contents = load(open(filename))
elessair 0:f269e3021894 108 try:
elessair 0:f269e3021894 109 for key in profile.iterkeys():
elessair 0:f269e3021894 110 profile[key] += contents[toolchain][key]
elessair 0:f269e3021894 111 except KeyError:
elessair 0:f269e3021894 112 args_error(parser, ("argument --profile: toolchain {} is not"
elessair 0:f269e3021894 113 " supported by profile {}").format(toolchain,
elessair 0:f269e3021894 114 filename))
elessair 0:f269e3021894 115 return profile