Skip to content

Commit 854e436

Browse files
Merge pull request #6481 from tsailer/exporter-codeblocks
Code::Blocks project file exporter
2 parents 96084a3 + 696a7f5 commit 854e436

File tree

4 files changed

+250
-2
lines changed

4 files changed

+250
-2
lines changed

tools/export/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from ..targets import TARGET_NAMES
3131
from . import (lpcxpresso, ds5_5, iar, makefile, embitz, coide, kds, simplicity,
3232
atmelstudio, mcuxpresso, sw4stm32, e2studio, zip, cmsis, uvision,
33-
cdt, vscode, gnuarmeclipse, qtcreator, cmake, nb, cces)
33+
cdt, vscode, gnuarmeclipse, qtcreator, cmake, nb, cces, codeblocks)
3434

3535
EXPORTERS = {
3636
u'uvision5': uvision.Uvision,
@@ -61,7 +61,8 @@
6161
u'vscode_iar' : vscode.VSCodeIAR,
6262
u'vscode_armc5' : vscode.VSCodeArmc5,
6363
u'cmake_gcc_arm': cmake.GccArm,
64-
u'cces' : cces.CCES
64+
u'cces' : cces.CCES,
65+
u'codeblocks': codeblocks.CodeBlocks
6566
}
6667

6768
ERROR_MESSAGE_UNSUPPORTED_TOOLCHAIN = """

tools/export/codeblocks/__init__.py

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
"""
2+
mbed SDK
3+
Copyright (c) 2014-2017 ARM Limited
4+
Copyright (c) 2018 ON Semiconductor
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
"""
18+
import copy
19+
import stat
20+
import os
21+
from os.path import splitext, basename, dirname, abspath, isdir
22+
from os import remove, mkdir
23+
from shutil import rmtree, copyfile
24+
from tools.targets import TARGET_MAP
25+
from tools.export.exporters import Exporter
26+
from tools.export.makefile import GccArm
27+
28+
class CodeBlocks(GccArm):
29+
NAME = 'Code::Blocks'
30+
31+
DOT_IN_RELATIVE_PATH = True
32+
33+
MBED_CONFIG_HEADER_SUPPORTED = True
34+
35+
PREPROCESS_ASM = False
36+
37+
POST_BINARY_WHITELIST = set([
38+
"NCS36510TargetCode.ncs36510_addfib"
39+
])
40+
41+
@staticmethod
42+
def filter_dot(str_in):
43+
"""
44+
Remove the './' prefix, if present.
45+
This function assumes that resources.win_to_unix()
46+
replaced all windows backslashes with slashes.
47+
"""
48+
if str_in is None:
49+
return None
50+
if str_in[:2] == './':
51+
return str_in[2:]
52+
return str_in
53+
54+
@staticmethod
55+
def prepare_lib(libname):
56+
if "lib" == libname[:3]:
57+
libname = libname[3:-2]
58+
return "-l" + libname
59+
60+
@staticmethod
61+
def prepare_sys_lib(libname):
62+
return "-l" + libname
63+
64+
def generate(self):
65+
self.resources.win_to_unix()
66+
67+
comp_flags = []
68+
debug_flags = []
69+
release_flags = [ '-Os', '-g1' ]
70+
next_is_include = False
71+
for f in self.flags['c_flags'] + self.flags['cxx_flags'] + self.flags['common_flags']:
72+
f = f.strip()
73+
if f == "-include":
74+
next_is_include = True
75+
continue
76+
if f == '-c':
77+
continue
78+
if next_is_include:
79+
f = '-include ' + f
80+
next_is_include = False
81+
if f.startswith('-O') or f.startswith('-g'):
82+
debug_flags.append(f)
83+
else:
84+
comp_flags.append(f)
85+
comp_flags = sorted(list(set(comp_flags)))
86+
inc_dirs = [self.filter_dot(s) for s in self.resources.inc_dirs];
87+
inc_dirs = [x for x in inc_dirs if (x is not None and
88+
x != '' and x != '.' and
89+
not x.startswith('bin') and
90+
not x.startswith('obj'))];
91+
92+
c_sources = sorted([self.filter_dot(s) for s in self.resources.c_sources])
93+
libraries = [self.prepare_lib(basename(lib)) for lib
94+
in self.resources.libraries]
95+
sys_libs = [self.prepare_sys_lib(lib) for lib
96+
in self.toolchain.sys_libs]
97+
98+
ctx = {
99+
'project_name': self.project_name,
100+
'debug_flags': debug_flags,
101+
'release_flags': release_flags,
102+
'comp_flags': comp_flags,
103+
'ld_flags': self.flags['ld_flags'],
104+
'headers': sorted(list(set([self.filter_dot(s) for s in self.resources.headers]))),
105+
'c_sources': c_sources,
106+
's_sources': sorted([self.filter_dot(s) for s in self.resources.s_sources]),
107+
'cpp_sources': sorted([self.filter_dot(s) for s in self.resources.cpp_sources]),
108+
'include_paths': inc_dirs,
109+
'linker_script': self.filter_dot(self.resources.linker_script),
110+
'libraries': libraries,
111+
'sys_libs': sys_libs,
112+
'openocdboard': ''
113+
}
114+
115+
openocd_board = {
116+
'NCS36510': 'board/ncs36510_axdbg.cfg',
117+
'DISCO_F429ZI': 'board/stm32f429discovery.cfg',
118+
'DISCO_F469NI': 'board/stm32f469discovery.cfg',
119+
'DISCO_L053C8': 'board/stm32l0discovery.cfg',
120+
'DISCO_L072CZ_LRWAN1': 'board/stm32l0discovery.cfg',
121+
'DISCO_F769NI': 'board/stm32f7discovery.cfg',
122+
'DISCO_L475VG_IOT01A': 'board/stm32l4discovery.cfg',
123+
'DISCO_L476VG': 'board/stm32l4discovery.cfg',
124+
'NRF51822': 'board/nordic_nrf51822_mkit.cfg',
125+
'NRF51822_BOOT': 'board/nordic_nrf51822_mkit.cfg',
126+
'NRF51822_OTA': 'board/nordic_nrf51822_mkit.cfg',
127+
'NRF51_DK_LEGACY': 'board/nordic_nrf51_dk.cfg',
128+
'NRF51_DK_BOOT': 'board/nordic_nrf51_dk.cfg',
129+
'NRF51_DK_OTA': 'board/nordic_nrf51_dk.cfg',
130+
'NRF51_DK': 'board/nordic_nrf51_dk.cfg'
131+
}
132+
133+
if self.target in openocd_board:
134+
ctx['openocdboard'] = openocd_board[self.target]
135+
136+
self.gen_file('codeblocks/cbp.tmpl', ctx, "%s.%s" % (self.project_name, 'cbp'))
137+
for f in [ 'obj', 'bin' ]:
138+
if not isdir(f):
139+
mkdir(f)
140+
self.gen_file_nonoverwrite('codeblocks/mbedignore.tmpl',
141+
ctx, f + '/.mbedignore')
142+
143+
# finally, generate the project file
144+
super(CodeBlocks, self).generate()
145+
146+
@staticmethod
147+
def clean(project_name):
148+
for ext in ['cbp', 'depend', 'layout']:
149+
remove("%s.%s" % (project_name, ext))
150+
for f in ['openocd.log']:
151+
remove(f)
152+
for d in ['bin', 'obj']:
153+
rmtree(d, ignore_errors=True)

tools/export/codeblocks/cbp.tmpl

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2+
<CodeBlocks_project_file>
3+
<FileVersion major="1" minor="6" />
4+
<Project>
5+
<Option title="{{project_name}}" />
6+
<Option pch_mode="2" />
7+
<Option compiler="arm-elf-gcc" />
8+
<Build>
9+
<Target title="Debug">
10+
<Option output="bin/Debug/{{project_name}}.elf" prefix_auto="1" extension_auto="0" />
11+
<Option object_output="obj/Debug/" />
12+
<Option type="1" />
13+
<Option compiler="arm-elf-gcc" />
14+
<Option use_console_runner="0" />
15+
<Compiler>
16+
{% for f in debug_flags -%}
17+
<Add option="{{f}}" />
18+
{% endfor -%}
19+
</Compiler>
20+
<Linker>
21+
<Add option='-Wl,-Map,&quot;bin/Debug/{{project_name}}.map&quot;' />
22+
</Linker>
23+
</Target>
24+
<Target title="Release">
25+
<Option output="bin/Release/{{project_name}}.elf" prefix_auto="1" extension_auto="0" />
26+
<Option object_output="obj/Release/" />
27+
<Option type="1" />
28+
<Option compiler="arm-elf-gcc" />
29+
<Option use_console_runner="0" />
30+
<Compiler>
31+
<Add option="-DNDEBUG" />
32+
{% for f in release_flags -%}
33+
<Add option="{{f}}" />
34+
{% endfor -%}
35+
</Compiler>
36+
<Linker>
37+
<Add option='-Wl,-Map,&quot;bin/Release/{{project_name}}.map&quot;' />
38+
</Linker>
39+
</Target>
40+
</Build>
41+
<Compiler>
42+
{% for f in comp_flags -%}
43+
<Add option="{{f}}" />
44+
{% endfor -%}
45+
{% for f in include_paths -%}
46+
<Add directory="{{f}}" />
47+
{% endfor -%}
48+
</Compiler>
49+
<Linker>
50+
{% for f in ld_flags -%}
51+
<Add option="{{f}}" />
52+
{% endfor -%}
53+
<Add option="-T {{linker_script}}" />
54+
<Add option="-Wl,--start-group {{sys_libs|join(" ")}} {{libraries|join(" ")}} -Wl,--end-group" />
55+
{% for f in sys_libs -%}
56+
<Add option="{{f}}" />
57+
{% endfor -%}
58+
{% for f in libraries -%}
59+
<Add option="{{f}}" />
60+
{% endfor -%}
61+
</Linker>
62+
{% for f in headers -%}
63+
<Unit filename="{{f}}"/>
64+
{% endfor -%}
65+
{% for f in c_sources -%}
66+
<Unit filename="{{f}}">
67+
<Option compilerVar="CC" />
68+
</Unit>
69+
{% endfor -%}
70+
{% for f in s_sources -%}
71+
<Unit filename="{{f}}">
72+
<Option compilerVar="CPP" />
73+
</Unit>
74+
{% endfor -%}
75+
{% for f in cpp_sources -%}
76+
<Unit filename="{{f}}">
77+
<Option compilerVar="CPP" />
78+
</Unit>
79+
{% endfor -%}
80+
<Extensions>
81+
{% if openocdboard != '' -%}
82+
<debugger>
83+
<remote_debugging target="Release">
84+
<options conn_type="3" serial_baud="115200" pipe_command="openocd -p -l openocd.log -f {{openocdboard}}" additional_cmds='monitor reset halt&#x0A;monitor flash write_image erase &quot;bin/Release/{{project_name}}.elf&quot;&#x0A;file &quot;bin/Release/{{project_name}}.elf&quot;&#x0A;monitor reset halt&#x0A;' extended_remote="1" />
85+
</remote_debugging>
86+
<remote_debugging target="Debug">
87+
<options conn_type="3" serial_baud="115200" pipe_command="openocd -p -l openocd.log -f {{openocdboard}}" additional_cmds='monitor reset halt&#x0A;monitor flash write_image erase &quot;bin/Debug/{{project_name}}.elf&quot;&#x0A;file &quot;bin/Debug/{{project_name}}.elf&quot;&#x0A;monitor reset halt&#x0A;' extended_remote="1" />
88+
</remote_debugging>
89+
</debugger>
90+
{% endif -%}
91+
</Extensions>
92+
</Project>
93+
</CodeBlocks_project_file>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*

0 commit comments

Comments
 (0)