Anders Blomdell / mbed-sdk-tools
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 in self.libraries]
00094         sys_libs = [self.prepare_sys_lib(lib) for lib
00095                     in self.toolchain.sys_libs]
00096         ncs36510fib = (hasattr(self.toolchain.target, 'post_binary_hook') and
00097                        self.toolchain.target.post_binary_hook['function'] == 'NCS36510TargetCode.ncs36510_addfib')
00098         if ncs36510fib:
00099             c_sources.append('ncs36510fib.c')
00100             c_sources.append('ncs36510trim.c')
00101 
00102         ctx = {
00103             'project_name': self.project_name,
00104             'debug_flags': debug_flags,
00105             'release_flags': release_flags,
00106             'comp_flags': comp_flags,
00107             'ld_flags': self.flags['ld_flags'],
00108             'headers': sorted(list(set([self.filter_dot(s) for s in self.resources.headers]))),
00109             'c_sources': c_sources,
00110             's_sources': sorted([self.filter_dot(s) for s in self.resources.s_sources]),
00111             'cpp_sources': sorted([self.filter_dot(s) for s in self.resources.cpp_sources]),
00112             'include_paths': inc_dirs,
00113             'linker_script': self.filter_dot(self.resources.linker_script),
00114             'libraries': libraries,
00115             'sys_libs': sys_libs,
00116             'ncs36510addfib': ncs36510fib,
00117             'openocdboard': ''
00118             }
00119 
00120         openocd_board = {
00121             'NCS36510': 'board/ncs36510_axdbg.cfg',
00122             'DISCO_F429ZI': 'board/stm32f429discovery.cfg',
00123             'DISCO_F469NI': 'board/stm32f469discovery.cfg',
00124             'DISCO_L053C8': 'board/stm32l0discovery.cfg',
00125             'DISCO_L072CZ_LRWAN1': 'board/stm32l0discovery.cfg',
00126             'DISCO_F769NI': 'board/stm32f7discovery.cfg',
00127             'DISCO_L475VG_IOT01A': 'board/stm32l4discovery.cfg',
00128             'DISCO_L476VG': 'board/stm32l4discovery.cfg',
00129             'NRF51822': 'board/nordic_nrf51822_mkit.cfg',
00130             'NRF51822_BOOT': 'board/nordic_nrf51822_mkit.cfg',
00131             'NRF51822_OTA': 'board/nordic_nrf51822_mkit.cfg',
00132             'NRF51_DK_LEGACY': 'board/nordic_nrf51_dk.cfg',
00133             'NRF51_DK_BOOT': 'board/nordic_nrf51_dk.cfg',
00134             'NRF51_DK_OTA': 'board/nordic_nrf51_dk.cfg',
00135             'NRF51_DK': 'board/nordic_nrf51_dk.cfg'
00136             }
00137 
00138         if self.target in openocd_board:
00139             ctx['openocdboard'] = openocd_board[self.target]
00140 
00141         self.gen_file('codeblocks/cbp.tmpl', ctx, "%s.%s" % (self.project_name, 'cbp'))
00142         for f in [ 'obj', 'bin' ]:
00143             if not isdir(f):
00144                 mkdir(f)
00145             self.gen_file_nonoverwrite('codeblocks/mbedignore.tmpl',
00146                                        ctx, f + '/.mbedignore')
00147 
00148         if ncs36510fib:
00149             genaddfiles = [ 'ncs36510fib.c', 'ncs36510trim.c' ]
00150             for f in genaddfiles:
00151                 copyfile(os.path.join(dirname(abspath(__file__)), f),
00152                          self.gen_file_dest(f))
00153             ignorefiles = genaddfiles
00154             try:
00155                 with open(self.gen_file_dest('.mbedignore'), 'r') as f:
00156                     l = set(map(lambda x: x.strip(), f.readlines()))
00157                     ignorefiles = [x for x in genaddfiles if x not in l]
00158             except IOError as e:
00159                 pass
00160             except:
00161                 raise
00162             if ignorefiles:
00163                 with open(self.gen_file_dest('.mbedignore'), 'a') as f:
00164                     for fi in ignorefiles:
00165                         f.write("%s\n" % fi)                
00166 
00167         # finally, generate the project file
00168         super(CodeBlocks, self).generate()
00169 
00170     @staticmethod
00171     def clean(project_name):
00172         for ext in ['cbp', 'depend', 'layout']:
00173             remove("%s.%s" % (project_name, ext))
00174         for f in ['openocd.log', 'ncs36510fib.c', 'ncs36510trim.c']:
00175             remove(f)
00176         for d in ['bin', 'obj']:
00177             rmtree(d, ignore_errors=True)