Clone of official tools
export/makefile/__init__.py@36:96847d42f010, 2017-06-22 (annotated)
- Committer:
- The Other Jimmy
- Date:
- Thu Jun 22 11:12:28 2017 -0500
- Revision:
- 36:96847d42f010
- Parent:
- 35:da9c89f8be7d
- Child:
- 40:7d3fa6b99b2b
Tools release 5.5.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 |
36:96847d42f010 | 24 | from tools.export.exporters import Exporter, filter_supported |
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 |
36:96847d42f010 | 38 | POST_BINARY_WHITELIST = set([ |
The Other Jimmy |
36:96847d42f010 | 39 | "MCU_NRF51Code.binary_hook", |
The Other Jimmy |
36:96847d42f010 | 40 | "TEENSY3_1Code.binary_hook", |
The Other Jimmy |
36:96847d42f010 | 41 | "LPCTargetCode.lpc_patch", |
The Other Jimmy |
36:96847d42f010 | 42 | "LPC4088Code.binary_hook" |
The Other Jimmy |
36:96847d42f010 | 43 | ]) |
The Other Jimmy |
36:96847d42f010 | 44 | |
The Other Jimmy |
31:8ea194f6145b | 45 | def generate(self): |
The Other Jimmy |
31:8ea194f6145b | 46 | """Generate the makefile |
The Other Jimmy |
31:8ea194f6145b | 47 | |
The Other Jimmy |
31:8ea194f6145b | 48 | Note: subclasses should not need to override this method |
The Other Jimmy |
31:8ea194f6145b | 49 | """ |
The Other Jimmy |
35:da9c89f8be7d | 50 | if not self.resources.linker_script: |
The Other Jimmy |
35:da9c89f8be7d | 51 | raise NotSupportedException("No linker script found.") |
The Other Jimmy |
35:da9c89f8be7d | 52 | |
The Other Jimmy |
31:8ea194f6145b | 53 | self.resources.win_to_unix() |
The Other Jimmy |
31:8ea194f6145b | 54 | |
The Other Jimmy |
31:8ea194f6145b | 55 | to_be_compiled = [splitext(src)[0] + ".o" for src in |
The Other Jimmy |
31:8ea194f6145b | 56 | self.resources.s_sources + |
The Other Jimmy |
31:8ea194f6145b | 57 | self.resources.c_sources + |
The Other Jimmy |
31:8ea194f6145b | 58 | self.resources.cpp_sources] |
The Other Jimmy |
31:8ea194f6145b | 59 | |
The Other Jimmy |
31:8ea194f6145b | 60 | libraries = [self.prepare_lib(basename(lib)) for lib |
The Other Jimmy |
31:8ea194f6145b | 61 | in self.resources.libraries] |
The Other Jimmy |
35:da9c89f8be7d | 62 | sys_libs = [self.prepare_sys_lib(lib) for lib |
The Other Jimmy |
35:da9c89f8be7d | 63 | in self.toolchain.sys_libs] |
The Other Jimmy |
31:8ea194f6145b | 64 | |
The Other Jimmy |
31:8ea194f6145b | 65 | ctx = { |
The Other Jimmy |
31:8ea194f6145b | 66 | 'name': self.project_name, |
The Other Jimmy |
31:8ea194f6145b | 67 | 'to_be_compiled': to_be_compiled, |
The Other Jimmy |
31:8ea194f6145b | 68 | 'object_files': self.resources.objects, |
The Other Jimmy |
31:8ea194f6145b | 69 | 'include_paths': list(set(self.resources.inc_dirs)), |
The Other Jimmy |
31:8ea194f6145b | 70 | 'library_paths': self.resources.lib_dirs, |
The Other Jimmy |
31:8ea194f6145b | 71 | 'linker_script': self.resources.linker_script, |
The Other Jimmy |
31:8ea194f6145b | 72 | 'libraries': libraries, |
The Other Jimmy |
35:da9c89f8be7d | 73 | 'ld_sys_libs': sys_libs, |
The Other Jimmy |
31:8ea194f6145b | 74 | 'hex_files': self.resources.hex_files, |
The Other Jimmy |
31:8ea194f6145b | 75 | 'vpath': (["../../.."] |
The Other Jimmy |
31:8ea194f6145b | 76 | if (basename(dirname(dirname(self.export_dir))) |
The Other Jimmy |
31:8ea194f6145b | 77 | == "projectfiles") |
The Other Jimmy |
31:8ea194f6145b | 78 | else [".."]), |
The Other Jimmy |
31:8ea194f6145b | 79 | 'cc_cmd': " ".join(["\'" + part + "\'" for part |
The Other Jimmy |
31:8ea194f6145b | 80 | in ([basename(self.toolchain.cc[0])] + |
The Other Jimmy |
31:8ea194f6145b | 81 | self.toolchain.cc[1:])]), |
The Other Jimmy |
31:8ea194f6145b | 82 | 'cppc_cmd': " ".join(["\'" + part + "\'" for part |
The Other Jimmy |
31:8ea194f6145b | 83 | in ([basename(self.toolchain.cppc[0])] + |
The Other Jimmy |
31:8ea194f6145b | 84 | self.toolchain.cppc[1:])]), |
The Other Jimmy |
31:8ea194f6145b | 85 | 'asm_cmd': " ".join(["\'" + part + "\'" for part |
The Other Jimmy |
31:8ea194f6145b | 86 | in ([basename(self.toolchain.asm[0])] + |
The Other Jimmy |
31:8ea194f6145b | 87 | self.toolchain.asm[1:])]), |
The Other Jimmy |
36:96847d42f010 | 88 | 'ld_cmd': "\'" + basename(self.toolchain.ld[0]) + "\'", |
The Other Jimmy |
31:8ea194f6145b | 89 | 'elf2bin_cmd': "\'" + basename(self.toolchain.elf2bin) + "\'", |
The Other Jimmy |
31:8ea194f6145b | 90 | 'link_script_ext': self.toolchain.LINKER_EXT, |
The Other Jimmy |
31:8ea194f6145b | 91 | 'link_script_option': self.LINK_SCRIPT_OPTION, |
The Other Jimmy |
31:8ea194f6145b | 92 | 'user_library_flag': self.USER_LIBRARY_FLAG, |
The Other Jimmy |
31:8ea194f6145b | 93 | } |
The Other Jimmy |
31:8ea194f6145b | 94 | |
The Other Jimmy |
36:96847d42f010 | 95 | if hasattr(self.toolchain, "preproc"): |
The Other Jimmy |
36:96847d42f010 | 96 | ctx['pp_cmd'] = " ".join(["\'" + part + "\'" for part |
The Other Jimmy |
36:96847d42f010 | 97 | in ([basename(self.toolchain.preproc[0])] + |
The Other Jimmy |
36:96847d42f010 | 98 | self.toolchain.preproc[1:] + |
The Other Jimmy |
36:96847d42f010 | 99 | self.toolchain.ld[1:])]) |
The Other Jimmy |
36:96847d42f010 | 100 | else: |
The Other Jimmy |
36:96847d42f010 | 101 | ctx['pp_cmd'] = None |
The Other Jimmy |
36:96847d42f010 | 102 | |
The Other Jimmy |
31:8ea194f6145b | 103 | for key in ['include_paths', 'library_paths', 'linker_script', |
The Other Jimmy |
31:8ea194f6145b | 104 | 'hex_files']: |
The Other Jimmy |
31:8ea194f6145b | 105 | if isinstance(ctx[key], list): |
The Other Jimmy |
31:8ea194f6145b | 106 | ctx[key] = [ctx['vpath'][0] + "/" + t for t in ctx[key]] |
The Other Jimmy |
31:8ea194f6145b | 107 | else: |
The Other Jimmy |
31:8ea194f6145b | 108 | ctx[key] = ctx['vpath'][0] + "/" + ctx[key] |
The Other Jimmy |
31:8ea194f6145b | 109 | if "../." not in ctx["include_paths"]: |
The Other Jimmy |
31:8ea194f6145b | 110 | ctx["include_paths"] += ['../.'] |
The Other Jimmy |
31:8ea194f6145b | 111 | for key in ['include_paths', 'library_paths', 'hex_files', |
The Other Jimmy |
31:8ea194f6145b | 112 | 'to_be_compiled']: |
The Other Jimmy |
31:8ea194f6145b | 113 | ctx[key] = sorted(ctx[key]) |
The Other Jimmy |
36:96847d42f010 | 114 | ctx.update(self.format_flags()) |
The Other Jimmy |
31:8ea194f6145b | 115 | |
The Other Jimmy |
31:8ea194f6145b | 116 | for templatefile in \ |
The Other Jimmy |
31:8ea194f6145b | 117 | ['makefile/%s_%s.tmpl' % (self.TEMPLATE, |
The Other Jimmy |
31:8ea194f6145b | 118 | self.target.lower())] + \ |
The Other Jimmy |
31:8ea194f6145b | 119 | ['makefile/%s_%s.tmpl' % (self.TEMPLATE, |
The Other Jimmy |
31:8ea194f6145b | 120 | label.lower()) for label |
The Other Jimmy |
31:8ea194f6145b | 121 | in self.toolchain.target.extra_labels] +\ |
The Other Jimmy |
31:8ea194f6145b | 122 | ['makefile/%s.tmpl' % self.TEMPLATE]: |
The Other Jimmy |
31:8ea194f6145b | 123 | try: |
The Other Jimmy |
31:8ea194f6145b | 124 | self.gen_file(templatefile, ctx, 'Makefile') |
The Other Jimmy |
31:8ea194f6145b | 125 | break |
The Other Jimmy |
31:8ea194f6145b | 126 | except TemplateNotFound: |
The Other Jimmy |
31:8ea194f6145b | 127 | pass |
The Other Jimmy |
31:8ea194f6145b | 128 | else: |
The Other Jimmy |
31:8ea194f6145b | 129 | raise NotSupportedException("This make tool is in development") |
The Other Jimmy |
31:8ea194f6145b | 130 | |
The Other Jimmy |
36:96847d42f010 | 131 | def format_flags(self): |
The Other Jimmy |
36:96847d42f010 | 132 | """Format toolchain flags for Makefile""" |
The Other Jimmy |
36:96847d42f010 | 133 | flags = {} |
The Other Jimmy |
36:96847d42f010 | 134 | for k, v in self.flags.iteritems(): |
The Other Jimmy |
36:96847d42f010 | 135 | if k in ['asm_flags', 'c_flags', 'cxx_flags']: |
The Other Jimmy |
36:96847d42f010 | 136 | flags[k] = map(lambda x: x.replace('"', '\\"'), v) |
The Other Jimmy |
36:96847d42f010 | 137 | else: |
The Other Jimmy |
36:96847d42f010 | 138 | flags[k] = v |
The Other Jimmy |
36:96847d42f010 | 139 | |
The Other Jimmy |
36:96847d42f010 | 140 | return flags |
The Other Jimmy |
36:96847d42f010 | 141 | |
The Other Jimmy |
31:8ea194f6145b | 142 | @staticmethod |
The Other Jimmy |
31:8ea194f6145b | 143 | def build(project_name, log_name="build_log.txt", cleanup=True): |
The Other Jimmy |
31:8ea194f6145b | 144 | """ Build Make project """ |
The Other Jimmy |
31:8ea194f6145b | 145 | # > Make -j |
The Other Jimmy |
31:8ea194f6145b | 146 | cmd = ["make", "-j"] |
The Other Jimmy |
31:8ea194f6145b | 147 | |
The Other Jimmy |
31:8ea194f6145b | 148 | # Build the project |
The Other Jimmy |
31:8ea194f6145b | 149 | p = Popen(cmd, stdout=PIPE, stderr=PIPE) |
The Other Jimmy |
31:8ea194f6145b | 150 | out, err = p.communicate() |
The Other Jimmy |
31:8ea194f6145b | 151 | ret_code = p.returncode |
The Other Jimmy |
31:8ea194f6145b | 152 | |
The Other Jimmy |
31:8ea194f6145b | 153 | out_string = "=" * 10 + "STDOUT" + "=" * 10 + "\n" |
The Other Jimmy |
31:8ea194f6145b | 154 | out_string += out |
The Other Jimmy |
31:8ea194f6145b | 155 | out_string += "=" * 10 + "STDERR" + "=" * 10 + "\n" |
The Other Jimmy |
31:8ea194f6145b | 156 | out_string += err |
The Other Jimmy |
31:8ea194f6145b | 157 | |
The Other Jimmy |
31:8ea194f6145b | 158 | if ret_code == 0: |
The Other Jimmy |
31:8ea194f6145b | 159 | out_string += "SUCCESS" |
The Other Jimmy |
31:8ea194f6145b | 160 | else: |
The Other Jimmy |
31:8ea194f6145b | 161 | out_string += "FAILURE" |
The Other Jimmy |
31:8ea194f6145b | 162 | |
The Other Jimmy |
31:8ea194f6145b | 163 | print out_string |
The Other Jimmy |
31:8ea194f6145b | 164 | |
The Other Jimmy |
31:8ea194f6145b | 165 | if log_name: |
The Other Jimmy |
31:8ea194f6145b | 166 | # Write the output to the log file |
The Other Jimmy |
31:8ea194f6145b | 167 | with open(log_name, 'w+') as f: |
The Other Jimmy |
31:8ea194f6145b | 168 | f.write(out_string) |
The Other Jimmy |
31:8ea194f6145b | 169 | |
The Other Jimmy |
31:8ea194f6145b | 170 | # Cleanup the exported and built files |
The Other Jimmy |
31:8ea194f6145b | 171 | if cleanup: |
The Other Jimmy |
31:8ea194f6145b | 172 | remove("Makefile") |
The Other Jimmy |
31:8ea194f6145b | 173 | remove(log_name) |
The Other Jimmy |
31:8ea194f6145b | 174 | # legacy .build directory cleaned if exists |
The Other Jimmy |
31:8ea194f6145b | 175 | if exists('.build'): |
The Other Jimmy |
31:8ea194f6145b | 176 | shutil.rmtree('.build') |
The Other Jimmy |
31:8ea194f6145b | 177 | if exists('BUILD'): |
The Other Jimmy |
31:8ea194f6145b | 178 | shutil.rmtree('BUILD') |
The Other Jimmy |
31:8ea194f6145b | 179 | |
The Other Jimmy |
31:8ea194f6145b | 180 | if ret_code != 0: |
The Other Jimmy |
31:8ea194f6145b | 181 | # Seems like something went wrong. |
The Other Jimmy |
31:8ea194f6145b | 182 | return -1 |
The Other Jimmy |
31:8ea194f6145b | 183 | else: |
The Other Jimmy |
31:8ea194f6145b | 184 | return 0 |
The Other Jimmy |
31:8ea194f6145b | 185 | |
The Other Jimmy |
31:8ea194f6145b | 186 | |
The Other Jimmy |
31:8ea194f6145b | 187 | class GccArm(Makefile): |
The Other Jimmy |
31:8ea194f6145b | 188 | """GCC ARM specific makefile target""" |
The Other Jimmy |
36:96847d42f010 | 189 | TARGETS = filter_supported("GCC_ARM", Makefile.POST_BINARY_WHITELIST) |
The Other Jimmy |
31:8ea194f6145b | 190 | NAME = 'Make-GCC-ARM' |
The Other Jimmy |
31:8ea194f6145b | 191 | TEMPLATE = 'make-gcc-arm' |
The Other Jimmy |
31:8ea194f6145b | 192 | TOOLCHAIN = "GCC_ARM" |
The Other Jimmy |
31:8ea194f6145b | 193 | LINK_SCRIPT_OPTION = "-T" |
The Other Jimmy |
31:8ea194f6145b | 194 | USER_LIBRARY_FLAG = "-L" |
The Other Jimmy |
31:8ea194f6145b | 195 | |
The Other Jimmy |
31:8ea194f6145b | 196 | @staticmethod |
The Other Jimmy |
31:8ea194f6145b | 197 | def prepare_lib(libname): |
The Other Jimmy |
31:8ea194f6145b | 198 | return "-l:" + libname |
The Other Jimmy |
31:8ea194f6145b | 199 | |
The Other Jimmy |
35:da9c89f8be7d | 200 | @staticmethod |
The Other Jimmy |
35:da9c89f8be7d | 201 | def prepare_sys_lib(libname): |
The Other Jimmy |
35:da9c89f8be7d | 202 | return "-l" + libname |
The Other Jimmy |
35:da9c89f8be7d | 203 | |
The Other Jimmy |
31:8ea194f6145b | 204 | |
The Other Jimmy |
31:8ea194f6145b | 205 | class Armc5(Makefile): |
The Other Jimmy |
31:8ea194f6145b | 206 | """ARM Compiler 5 specific makefile target""" |
The Other Jimmy |
36:96847d42f010 | 207 | TARGETS = filter_supported("ARM", Makefile.POST_BINARY_WHITELIST) |
The Other Jimmy |
31:8ea194f6145b | 208 | NAME = 'Make-ARMc5' |
The Other Jimmy |
31:8ea194f6145b | 209 | TEMPLATE = 'make-armc5' |
The Other Jimmy |
31:8ea194f6145b | 210 | TOOLCHAIN = "ARM" |
The Other Jimmy |
31:8ea194f6145b | 211 | LINK_SCRIPT_OPTION = "--scatter" |
The Other Jimmy |
31:8ea194f6145b | 212 | USER_LIBRARY_FLAG = "--userlibpath " |
The Other Jimmy |
31:8ea194f6145b | 213 | |
The Other Jimmy |
31:8ea194f6145b | 214 | @staticmethod |
The Other Jimmy |
31:8ea194f6145b | 215 | def prepare_lib(libname): |
The Other Jimmy |
31:8ea194f6145b | 216 | return libname |
The Other Jimmy |
31:8ea194f6145b | 217 | |
The Other Jimmy |
35:da9c89f8be7d | 218 | @staticmethod |
The Other Jimmy |
35:da9c89f8be7d | 219 | def prepare_sys_lib(libname): |
The Other Jimmy |
35:da9c89f8be7d | 220 | return libname |
The Other Jimmy |
35:da9c89f8be7d | 221 | |
The Other Jimmy |
31:8ea194f6145b | 222 | |
The Other Jimmy |
31:8ea194f6145b | 223 | class IAR(Makefile): |
The Other Jimmy |
31:8ea194f6145b | 224 | """IAR specific makefile target""" |
The Other Jimmy |
36:96847d42f010 | 225 | TARGETS = filter_supported("IAR", Makefile.POST_BINARY_WHITELIST) |
The Other Jimmy |
31:8ea194f6145b | 226 | NAME = 'Make-IAR' |
The Other Jimmy |
31:8ea194f6145b | 227 | TEMPLATE = 'make-iar' |
The Other Jimmy |
31:8ea194f6145b | 228 | TOOLCHAIN = "IAR" |
The Other Jimmy |
31:8ea194f6145b | 229 | LINK_SCRIPT_OPTION = "--config" |
The Other Jimmy |
31:8ea194f6145b | 230 | USER_LIBRARY_FLAG = "-L" |
The Other Jimmy |
31:8ea194f6145b | 231 | |
The Other Jimmy |
31:8ea194f6145b | 232 | @staticmethod |
The Other Jimmy |
31:8ea194f6145b | 233 | def prepare_lib(libname): |
The Other Jimmy |
31:8ea194f6145b | 234 | if "lib" == libname[:3]: |
The Other Jimmy |
31:8ea194f6145b | 235 | libname = libname[3:] |
The Other Jimmy |
31:8ea194f6145b | 236 | return "-l" + splitext(libname)[0] |
The Other Jimmy |
35:da9c89f8be7d | 237 | |
The Other Jimmy |
35:da9c89f8be7d | 238 | @staticmethod |
The Other Jimmy |
35:da9c89f8be7d | 239 | def prepare_sys_lib(libname): |
The Other Jimmy |
35:da9c89f8be7d | 240 | if "lib" == libname[:3]: |
The Other Jimmy |
35:da9c89f8be7d | 241 | libname = libname[3:] |
The Other Jimmy |
35:da9c89f8be7d | 242 | return "-l" + splitext(libname)[0] |