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