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