Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers __init__.py Source File

__init__.py

00001 """
00002 mbed SDK
00003 Copyright (c) 2014-2017 ARM Limited
00004 Copyright (c) 2018 ON Semiconductor
00005 
00006 Licensed under the Apache License, Version 2.0 (the "License");
00007 you may not use this file except in compliance with the License.
00008 You may obtain a copy of the License at
00009 
00010     http://www.apache.org/licenses/LICENSE-2.0
00011 
00012 Unless required by applicable law or agreed to in writing, software
00013 distributed under the License is distributed on an "AS IS" BASIS,
00014 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 See the License for the specific language governing permissions and
00016 limitations under the License.
00017 """
00018 import copy
00019 import stat
00020 import os
00021 from os.path import splitext, basename, dirname, abspath, isdir
00022 from os import remove, mkdir
00023 from shutil import rmtree, copyfile
00024 from tools.targets import TARGET_MAP
00025 from tools.export.exporters import Exporter
00026 from tools.export.makefile import GccArm
00027 
00028 class CodeBlocks(GccArm ):
00029     NAME = 'Code::Blocks'
00030 
00031     DOT_IN_RELATIVE_PATH = True
00032 
00033     MBED_CONFIG_HEADER_SUPPORTED = True
00034 
00035     PREPROCESS_ASM = False
00036 
00037     POST_BINARY_WHITELIST = set([
00038         "NCS36510TargetCode.ncs36510_addfib"
00039     ])
00040 
00041     @staticmethod
00042     def filter_dot(str_in):
00043         """
00044         Remove the './' prefix, if present.
00045         This function assumes that resources.win_to_unix()
00046         replaced all windows backslashes with slashes.
00047         """
00048         if str_in is None:
00049             return None
00050         if str_in[:2] == './':
00051             return str_in[2:]
00052         return str_in
00053 
00054     @staticmethod
00055     def prepare_lib(libname):
00056         if "lib" == libname[:3]:
00057             libname = libname[3:-2]
00058         return "-l" + libname
00059 
00060     @staticmethod
00061     def prepare_sys_lib(libname):
00062         return "-l" + libname
00063 
00064     def generate(self):
00065         self.resources.win_to_unix()
00066 
00067         comp_flags = []
00068         debug_flags = []
00069         release_flags = [ '-Os', '-g1' ]
00070         next_is_include = False
00071         for f in self.flags['c_flags'] + self.flags['cxx_flags'] + self.flags['common_flags']:
00072             f = f.strip()
00073             if f == "-include":
00074                 next_is_include = True
00075                 continue
00076             if f == '-c':
00077                 continue
00078             if next_is_include:
00079                 f = '-include ' + f
00080             next_is_include = False
00081             if f.startswith('-O') or f.startswith('-g'):
00082                 debug_flags.append(f)
00083             else:
00084                 comp_flags.append(f)
00085         comp_flags = sorted(list(set(comp_flags)))
00086         inc_dirs = [self.filter_dot(s) for s in self.resources.inc_dirs];
00087         inc_dirs = [x for x in inc_dirs if (x is not None and
00088                                             x != '' and x != '.' and
00089                                             not x.startswith('bin') and
00090                                             not x.startswith('obj'))];
00091 
00092         c_sources = sorted([self.filter_dot(s) for s in self.resources.c_sources])
00093         libraries = [self.prepare_lib(basename(lib)) for lib
00094                      in self.resources.libraries]
00095         sys_libs = [self.prepare_sys_lib(lib) for lib
00096                     in self.toolchain.sys_libs]
00097         ncs36510fib = (hasattr(self.toolchain.target, 'post_binary_hook') and
00098                        self.toolchain.target.post_binary_hook['function'] == 'NCS36510TargetCode.ncs36510_addfib')
00099         if ncs36510fib:
00100             c_sources.append('ncs36510fib.c')
00101             c_sources.append('ncs36510trim.c')
00102 
00103         ctx = {
00104             'project_name': self.project_name,
00105             'debug_flags': debug_flags,
00106             'release_flags': release_flags,
00107             'comp_flags': comp_flags,
00108             'ld_flags': self.flags['ld_flags'],
00109             'headers': sorted(list(set([self.filter_dot(s) for s in self.resources.headers]))),
00110             'c_sources': c_sources,
00111             's_sources': sorted([self.filter_dot(s) for s in self.resources.s_sources]),
00112             'cpp_sources': sorted([self.filter_dot(s) for s in self.resources.cpp_sources]),
00113             'include_paths': inc_dirs,
00114             'linker_script': self.filter_dot(self.resources.linker_script),
00115             'libraries': libraries,
00116             'sys_libs': sys_libs,
00117             'ncs36510addfib': ncs36510fib,
00118             'openocdboard': ''
00119             }
00120 
00121         openocd_board = {
00122             'NCS36510': 'board/ncs36510_axdbg.cfg',
00123             'DISCO_F429ZI': 'board/stm32f429discovery.cfg',
00124             'DISCO_F469NI': 'board/stm32f469discovery.cfg',
00125             'DISCO_L053C8': 'board/stm32l0discovery.cfg',
00126             'DISCO_L072CZ_LRWAN1': 'board/stm32l0discovery.cfg',
00127             'DISCO_F769NI': 'board/stm32f7discovery.cfg',
00128             'DISCO_L475VG_IOT01A': 'board/stm32l4discovery.cfg',
00129             'DISCO_L476VG': 'board/stm32l4discovery.cfg',
00130             'NRF51822': 'board/nordic_nrf51822_mkit.cfg',
00131             'NRF51822_BOOT': 'board/nordic_nrf51822_mkit.cfg',
00132             'NRF51822_OTA': 'board/nordic_nrf51822_mkit.cfg',
00133             'NRF51_DK_LEGACY': 'board/nordic_nrf51_dk.cfg',
00134             'NRF51_DK_BOOT': 'board/nordic_nrf51_dk.cfg',
00135             'NRF51_DK_OTA': 'board/nordic_nrf51_dk.cfg',
00136             'NRF51_DK': 'board/nordic_nrf51_dk.cfg'
00137             }
00138 
00139         if self.target in openocd_board:
00140             ctx['openocdboard'] = openocd_board[self.target]
00141 
00142         self.gen_file('codeblocks/cbp.tmpl', ctx, "%s.%s" % (self.project_name, 'cbp'))
00143         for f in [ 'obj', 'bin' ]:
00144             if not isdir(f):
00145                 mkdir(f)
00146             self.gen_file_nonoverwrite('codeblocks/mbedignore.tmpl',
00147                                        ctx, f + '/.mbedignore')
00148 
00149         if ncs36510fib:
00150             genaddfiles = [ 'ncs36510fib.c', 'ncs36510trim.c' ]
00151             for f in genaddfiles:
00152                 copyfile(os.path.join(dirname(abspath(__file__)), f),
00153                          self.gen_file_dest(f))
00154             ignorefiles = genaddfiles
00155             try:
00156                 with open(self.gen_file_dest('.mbedignore'), 'r') as f:
00157                     l = set(map(lambda x: x.strip(), f.readlines()))
00158                     ignorefiles = [x for x in genaddfiles if x not in l]
00159             except IOError as e:
00160                 pass
00161             except:
00162                 raise
00163             if ignorefiles:
00164                 with open(self.gen_file_dest('.mbedignore'), 'a') as f:
00165                     for fi in ignorefiles:
00166                         f.write("%s\n" % fi)                
00167 
00168         # finally, generate the project file
00169         super(CodeBlocks, self).generate()
00170 
00171     @staticmethod
00172     def clean(project_name):
00173         for ext in ['cbp', 'depend', 'layout']:
00174             remove("%s.%s" % (project_name, ext))
00175         for f in ['openocd.log', 'ncs36510fib.c', 'ncs36510trim.c']:
00176             remove(f)
00177         for d in ['bin', 'obj']:
00178             rmtree(d, ignore_errors=True)