Clone of official tools
export/makefile/__init__.py@31:8ea194f6145b, 2017-01-04 (annotated)
- Committer:
- The Other Jimmy
- Date:
- Wed Jan 04 11:58:24 2017 -0600
- Revision:
- 31:8ea194f6145b
- Child:
- 35:da9c89f8be7d
Update tools to follow mbed-os tools release 5.3.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
The Other Jimmy |
31:8ea194f6145b | 1 | """ |
The Other Jimmy |
31:8ea194f6145b | 2 | mbed SDK |
The Other Jimmy |
31:8ea194f6145b | 3 | Copyright (c) 2011-2016 ARM Limited |
The Other Jimmy |
31:8ea194f6145b | 4 | |
The Other Jimmy |
31:8ea194f6145b | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
The Other Jimmy |
31:8ea194f6145b | 6 | you may not use this file except in compliance with the License. |
The Other Jimmy |
31:8ea194f6145b | 7 | You may obtain a copy of the License at |
The Other Jimmy |
31:8ea194f6145b | 8 | |
The Other Jimmy |
31:8ea194f6145b | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
The Other Jimmy |
31:8ea194f6145b | 10 | |
The Other Jimmy |
31:8ea194f6145b | 11 | Unless required by applicable law or agreed to in writing, software |
The Other Jimmy |
31:8ea194f6145b | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
The Other Jimmy |
31:8ea194f6145b | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
The Other Jimmy |
31:8ea194f6145b | 14 | See the License for the specific language governing permissions and |
The Other Jimmy |
31:8ea194f6145b | 15 | limitations under the License. |
The Other Jimmy |
31:8ea194f6145b | 16 | """ |
The Other Jimmy |
31:8ea194f6145b | 17 | from os.path import splitext, basename, relpath, join, abspath, dirname,\ |
The Other Jimmy |
31:8ea194f6145b | 18 | exists |
The Other Jimmy |
31:8ea194f6145b | 19 | from os import remove |
The Other Jimmy |
31:8ea194f6145b | 20 | import sys |
The Other Jimmy |
31:8ea194f6145b | 21 | from subprocess import check_output, CalledProcessError, Popen, PIPE |
The Other Jimmy |
31:8ea194f6145b | 22 | import shutil |
The Other Jimmy |
31:8ea194f6145b | 23 | from jinja2.exceptions import TemplateNotFound |
The Other Jimmy |
31:8ea194f6145b | 24 | from tools.export.exporters import Exporter |
The Other Jimmy |
31:8ea194f6145b | 25 | from tools.utils import NotSupportedException |
The Other Jimmy |
31:8ea194f6145b | 26 | from tools.targets import TARGET_MAP |
The Other Jimmy |
31:8ea194f6145b | 27 | |
The Other Jimmy |
31:8ea194f6145b | 28 | |
The Other Jimmy |
31:8ea194f6145b | 29 | class Makefile(Exporter): |
The Other Jimmy |
31:8ea194f6145b | 30 | """Generic Makefile template that mimics the behavior of the python build |
The Other Jimmy |
31:8ea194f6145b | 31 | system |
The Other Jimmy |
31:8ea194f6145b | 32 | """ |
The Other Jimmy |
31:8ea194f6145b | 33 | |
The Other Jimmy |
31:8ea194f6145b | 34 | DOT_IN_RELATIVE_PATH = True |
The Other Jimmy |
31:8ea194f6145b | 35 | |
The Other Jimmy |
31:8ea194f6145b | 36 | MBED_CONFIG_HEADER_SUPPORTED = True |
The Other Jimmy |
31:8ea194f6145b | 37 | |
The Other Jimmy |
31:8ea194f6145b | 38 | def generate(self): |
The Other Jimmy |
31:8ea194f6145b | 39 | """Generate the makefile |
The Other Jimmy |
31:8ea194f6145b | 40 | |
The Other Jimmy |
31:8ea194f6145b | 41 | Note: subclasses should not need to override this method |
The Other Jimmy |
31:8ea194f6145b | 42 | """ |
The Other Jimmy |
31:8ea194f6145b | 43 | self.resources.win_to_unix() |
The Other Jimmy |
31:8ea194f6145b | 44 | |
The Other Jimmy |
31:8ea194f6145b | 45 | to_be_compiled = [splitext(src)[0] + ".o" for src in |
The Other Jimmy |
31:8ea194f6145b | 46 | self.resources.s_sources + |
The Other Jimmy |
31:8ea194f6145b | 47 | self.resources.c_sources + |
The Other Jimmy |
31:8ea194f6145b | 48 | self.resources.cpp_sources] |
The Other Jimmy |
31:8ea194f6145b | 49 | |
The Other Jimmy |
31:8ea194f6145b | 50 | libraries = [self.prepare_lib(basename(lib)) for lib |
The Other Jimmy |
31:8ea194f6145b | 51 | in self.resources.libraries] |
The Other Jimmy |
31:8ea194f6145b | 52 | |
The Other Jimmy |
31:8ea194f6145b | 53 | ctx = { |
The Other Jimmy |
31:8ea194f6145b | 54 | 'name': self.project_name, |
The Other Jimmy |
31:8ea194f6145b | 55 | 'to_be_compiled': to_be_compiled, |
The Other Jimmy |
31:8ea194f6145b | 56 | 'object_files': self.resources.objects, |
The Other Jimmy |
31:8ea194f6145b | 57 | 'include_paths': list(set(self.resources.inc_dirs)), |
The Other Jimmy |
31:8ea194f6145b | 58 | 'library_paths': self.resources.lib_dirs, |
The Other Jimmy |
31:8ea194f6145b | 59 | 'linker_script': self.resources.linker_script, |
The Other Jimmy |
31:8ea194f6145b | 60 | 'libraries': libraries, |
The Other Jimmy |
31:8ea194f6145b | 61 | 'hex_files': self.resources.hex_files, |
The Other Jimmy |
31:8ea194f6145b | 62 | 'vpath': (["../../.."] |
The Other Jimmy |
31:8ea194f6145b | 63 | if (basename(dirname(dirname(self.export_dir))) |
The Other Jimmy |
31:8ea194f6145b | 64 | == "projectfiles") |
The Other Jimmy |
31:8ea194f6145b | 65 | else [".."]), |
The Other Jimmy |
31:8ea194f6145b | 66 | 'cc_cmd': " ".join(["\'" + part + "\'" for part |
The Other Jimmy |
31:8ea194f6145b | 67 | in ([basename(self.toolchain.cc[0])] + |
The Other Jimmy |
31:8ea194f6145b | 68 | self.toolchain.cc[1:])]), |
The Other Jimmy |
31:8ea194f6145b | 69 | 'cppc_cmd': " ".join(["\'" + part + "\'" for part |
The Other Jimmy |
31:8ea194f6145b | 70 | in ([basename(self.toolchain.cppc[0])] + |
The Other Jimmy |
31:8ea194f6145b | 71 | self.toolchain.cppc[1:])]), |
The Other Jimmy |
31:8ea194f6145b | 72 | 'asm_cmd': " ".join(["\'" + part + "\'" for part |
The Other Jimmy |
31:8ea194f6145b | 73 | in ([basename(self.toolchain.asm[0])] + |
The Other Jimmy |
31:8ea194f6145b | 74 | self.toolchain.asm[1:])]), |
The Other Jimmy |
31:8ea194f6145b | 75 | 'ld_cmd': " ".join(["\'" + part + "\'" for part |
The Other Jimmy |
31:8ea194f6145b | 76 | in ([basename(self.toolchain.ld[0])] + |
The Other Jimmy |
31:8ea194f6145b | 77 | self.toolchain.ld[1:])]), |
The Other Jimmy |
31:8ea194f6145b | 78 | 'elf2bin_cmd': "\'" + basename(self.toolchain.elf2bin) + "\'", |
The Other Jimmy |
31:8ea194f6145b | 79 | 'link_script_ext': self.toolchain.LINKER_EXT, |
The Other Jimmy |
31:8ea194f6145b | 80 | 'link_script_option': self.LINK_SCRIPT_OPTION, |
The Other Jimmy |
31:8ea194f6145b | 81 | 'user_library_flag': self.USER_LIBRARY_FLAG, |
The Other Jimmy |
31:8ea194f6145b | 82 | } |
The Other Jimmy |
31:8ea194f6145b | 83 | |
The Other Jimmy |
31:8ea194f6145b | 84 | for key in ['include_paths', 'library_paths', 'linker_script', |
The Other Jimmy |
31:8ea194f6145b | 85 | 'hex_files']: |
The Other Jimmy |
31:8ea194f6145b | 86 | if isinstance(ctx[key], list): |
The Other Jimmy |
31:8ea194f6145b | 87 | ctx[key] = [ctx['vpath'][0] + "/" + t for t in ctx[key]] |
The Other Jimmy |
31:8ea194f6145b | 88 | else: |
The Other Jimmy |
31:8ea194f6145b | 89 | ctx[key] = ctx['vpath'][0] + "/" + ctx[key] |
The Other Jimmy |
31:8ea194f6145b | 90 | if "../." not in ctx["include_paths"]: |
The Other Jimmy |
31:8ea194f6145b | 91 | ctx["include_paths"] += ['../.'] |
The Other Jimmy |
31:8ea194f6145b | 92 | for key in ['include_paths', 'library_paths', 'hex_files', |
The Other Jimmy |
31:8ea194f6145b | 93 | 'to_be_compiled']: |
The Other Jimmy |
31:8ea194f6145b | 94 | ctx[key] = sorted(ctx[key]) |
The Other Jimmy |
31:8ea194f6145b | 95 | ctx.update(self.flags) |
The Other Jimmy |
31:8ea194f6145b | 96 | |
The Other Jimmy |
31:8ea194f6145b | 97 | for templatefile in \ |
The Other Jimmy |
31:8ea194f6145b | 98 | ['makefile/%s_%s.tmpl' % (self.TEMPLATE, |
The Other Jimmy |
31:8ea194f6145b | 99 | self.target.lower())] + \ |
The Other Jimmy |
31:8ea194f6145b | 100 | ['makefile/%s_%s.tmpl' % (self.TEMPLATE, |
The Other Jimmy |
31:8ea194f6145b | 101 | label.lower()) for label |
The Other Jimmy |
31:8ea194f6145b | 102 | in self.toolchain.target.extra_labels] +\ |
The Other Jimmy |
31:8ea194f6145b | 103 | ['makefile/%s.tmpl' % self.TEMPLATE]: |
The Other Jimmy |
31:8ea194f6145b | 104 | try: |
The Other Jimmy |
31:8ea194f6145b | 105 | self.gen_file(templatefile, ctx, 'Makefile') |
The Other Jimmy |
31:8ea194f6145b | 106 | break |
The Other Jimmy |
31:8ea194f6145b | 107 | except TemplateNotFound: |
The Other Jimmy |
31:8ea194f6145b | 108 | pass |
The Other Jimmy |
31:8ea194f6145b | 109 | else: |
The Other Jimmy |
31:8ea194f6145b | 110 | raise NotSupportedException("This make tool is in development") |
The Other Jimmy |
31:8ea194f6145b | 111 | |
The Other Jimmy |
31:8ea194f6145b | 112 | @staticmethod |
The Other Jimmy |
31:8ea194f6145b | 113 | def build(project_name, log_name="build_log.txt", cleanup=True): |
The Other Jimmy |
31:8ea194f6145b | 114 | """ Build Make project """ |
The Other Jimmy |
31:8ea194f6145b | 115 | # > Make -j |
The Other Jimmy |
31:8ea194f6145b | 116 | cmd = ["make", "-j"] |
The Other Jimmy |
31:8ea194f6145b | 117 | |
The Other Jimmy |
31:8ea194f6145b | 118 | # Build the project |
The Other Jimmy |
31:8ea194f6145b | 119 | p = Popen(cmd, stdout=PIPE, stderr=PIPE) |
The Other Jimmy |
31:8ea194f6145b | 120 | out, err = p.communicate() |
The Other Jimmy |
31:8ea194f6145b | 121 | ret_code = p.returncode |
The Other Jimmy |
31:8ea194f6145b | 122 | |
The Other Jimmy |
31:8ea194f6145b | 123 | out_string = "=" * 10 + "STDOUT" + "=" * 10 + "\n" |
The Other Jimmy |
31:8ea194f6145b | 124 | out_string += out |
The Other Jimmy |
31:8ea194f6145b | 125 | out_string += "=" * 10 + "STDERR" + "=" * 10 + "\n" |
The Other Jimmy |
31:8ea194f6145b | 126 | out_string += err |
The Other Jimmy |
31:8ea194f6145b | 127 | |
The Other Jimmy |
31:8ea194f6145b | 128 | if ret_code == 0: |
The Other Jimmy |
31:8ea194f6145b | 129 | out_string += "SUCCESS" |
The Other Jimmy |
31:8ea194f6145b | 130 | else: |
The Other Jimmy |
31:8ea194f6145b | 131 | out_string += "FAILURE" |
The Other Jimmy |
31:8ea194f6145b | 132 | |
The Other Jimmy |
31:8ea194f6145b | 133 | print out_string |
The Other Jimmy |
31:8ea194f6145b | 134 | |
The Other Jimmy |
31:8ea194f6145b | 135 | if log_name: |
The Other Jimmy |
31:8ea194f6145b | 136 | # Write the output to the log file |
The Other Jimmy |
31:8ea194f6145b | 137 | with open(log_name, 'w+') as f: |
The Other Jimmy |
31:8ea194f6145b | 138 | f.write(out_string) |
The Other Jimmy |
31:8ea194f6145b | 139 | |
The Other Jimmy |
31:8ea194f6145b | 140 | # Cleanup the exported and built files |
The Other Jimmy |
31:8ea194f6145b | 141 | if cleanup: |
The Other Jimmy |
31:8ea194f6145b | 142 | remove("Makefile") |
The Other Jimmy |
31:8ea194f6145b | 143 | remove(log_name) |
The Other Jimmy |
31:8ea194f6145b | 144 | # legacy .build directory cleaned if exists |
The Other Jimmy |
31:8ea194f6145b | 145 | if exists('.build'): |
The Other Jimmy |
31:8ea194f6145b | 146 | shutil.rmtree('.build') |
The Other Jimmy |
31:8ea194f6145b | 147 | if exists('BUILD'): |
The Other Jimmy |
31:8ea194f6145b | 148 | shutil.rmtree('BUILD') |
The Other Jimmy |
31:8ea194f6145b | 149 | |
The Other Jimmy |
31:8ea194f6145b | 150 | if ret_code != 0: |
The Other Jimmy |
31:8ea194f6145b | 151 | # Seems like something went wrong. |
The Other Jimmy |
31:8ea194f6145b | 152 | return -1 |
The Other Jimmy |
31:8ea194f6145b | 153 | else: |
The Other Jimmy |
31:8ea194f6145b | 154 | return 0 |
The Other Jimmy |
31:8ea194f6145b | 155 | |
The Other Jimmy |
31:8ea194f6145b | 156 | |
The Other Jimmy |
31:8ea194f6145b | 157 | class GccArm(Makefile): |
The Other Jimmy |
31:8ea194f6145b | 158 | """GCC ARM specific makefile target""" |
The Other Jimmy |
31:8ea194f6145b | 159 | TARGETS = [target for target, obj in TARGET_MAP.iteritems() |
The Other Jimmy |
31:8ea194f6145b | 160 | if "GCC_ARM" in obj.supported_toolchains] |
The Other Jimmy |
31:8ea194f6145b | 161 | NAME = 'Make-GCC-ARM' |
The Other Jimmy |
31:8ea194f6145b | 162 | TEMPLATE = 'make-gcc-arm' |
The Other Jimmy |
31:8ea194f6145b | 163 | TOOLCHAIN = "GCC_ARM" |
The Other Jimmy |
31:8ea194f6145b | 164 | LINK_SCRIPT_OPTION = "-T" |
The Other Jimmy |
31:8ea194f6145b | 165 | USER_LIBRARY_FLAG = "-L" |
The Other Jimmy |
31:8ea194f6145b | 166 | |
The Other Jimmy |
31:8ea194f6145b | 167 | @staticmethod |
The Other Jimmy |
31:8ea194f6145b | 168 | def prepare_lib(libname): |
The Other Jimmy |
31:8ea194f6145b | 169 | return "-l:" + libname |
The Other Jimmy |
31:8ea194f6145b | 170 | |
The Other Jimmy |
31:8ea194f6145b | 171 | |
The Other Jimmy |
31:8ea194f6145b | 172 | class Armc5(Makefile): |
The Other Jimmy |
31:8ea194f6145b | 173 | """ARM Compiler 5 specific makefile target""" |
The Other Jimmy |
31:8ea194f6145b | 174 | TARGETS = [target for target, obj in TARGET_MAP.iteritems() |
The Other Jimmy |
31:8ea194f6145b | 175 | if "ARM" in obj.supported_toolchains] |
The Other Jimmy |
31:8ea194f6145b | 176 | NAME = 'Make-ARMc5' |
The Other Jimmy |
31:8ea194f6145b | 177 | TEMPLATE = 'make-armc5' |
The Other Jimmy |
31:8ea194f6145b | 178 | TOOLCHAIN = "ARM" |
The Other Jimmy |
31:8ea194f6145b | 179 | LINK_SCRIPT_OPTION = "--scatter" |
The Other Jimmy |
31:8ea194f6145b | 180 | USER_LIBRARY_FLAG = "--userlibpath " |
The Other Jimmy |
31:8ea194f6145b | 181 | |
The Other Jimmy |
31:8ea194f6145b | 182 | @staticmethod |
The Other Jimmy |
31:8ea194f6145b | 183 | def prepare_lib(libname): |
The Other Jimmy |
31:8ea194f6145b | 184 | return libname |
The Other Jimmy |
31:8ea194f6145b | 185 | |
The Other Jimmy |
31:8ea194f6145b | 186 | |
The Other Jimmy |
31:8ea194f6145b | 187 | class IAR(Makefile): |
The Other Jimmy |
31:8ea194f6145b | 188 | """IAR specific makefile target""" |
The Other Jimmy |
31:8ea194f6145b | 189 | TARGETS = [target for target, obj in TARGET_MAP.iteritems() |
The Other Jimmy |
31:8ea194f6145b | 190 | if "IAR" in obj.supported_toolchains] |
The Other Jimmy |
31:8ea194f6145b | 191 | NAME = 'Make-IAR' |
The Other Jimmy |
31:8ea194f6145b | 192 | TEMPLATE = 'make-iar' |
The Other Jimmy |
31:8ea194f6145b | 193 | TOOLCHAIN = "IAR" |
The Other Jimmy |
31:8ea194f6145b | 194 | LINK_SCRIPT_OPTION = "--config" |
The Other Jimmy |
31:8ea194f6145b | 195 | USER_LIBRARY_FLAG = "-L" |
The Other Jimmy |
31:8ea194f6145b | 196 | |
The Other Jimmy |
31:8ea194f6145b | 197 | @staticmethod |
The Other Jimmy |
31:8ea194f6145b | 198 | def prepare_lib(libname): |
The Other Jimmy |
31:8ea194f6145b | 199 | if "lib" == libname[:3]: |
The Other Jimmy |
31:8ea194f6145b | 200 | libname = libname[3:] |
The Other Jimmy |
31:8ea194f6145b | 201 | return "-l" + splitext(libname)[0] |