Skip to content

Code::Blocks project file exporter #6481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions tools/export/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from ..targets import TARGET_NAMES
from . import (lpcxpresso, ds5_5, iar, makefile, embitz, coide, kds, simplicity,
atmelstudio, mcuxpresso, sw4stm32, e2studio, zip, cmsis, uvision,
cdt, vscode, gnuarmeclipse, qtcreator, cmake, nb, cces)
cdt, vscode, gnuarmeclipse, qtcreator, cmake, nb, cces, codeblocks)

EXPORTERS = {
u'uvision5': uvision.Uvision,
Expand Down Expand Up @@ -61,7 +61,8 @@
u'vscode_iar' : vscode.VSCodeIAR,
u'vscode_armc5' : vscode.VSCodeArmc5,
u'cmake_gcc_arm': cmake.GccArm,
u'cces' : cces.CCES
u'cces' : cces.CCES,
u'codeblocks': codeblocks.CodeBlocks
}

ERROR_MESSAGE_UNSUPPORTED_TOOLCHAIN = """
Expand Down
153 changes: 153 additions & 0 deletions tools/export/codeblocks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
"""
mbed SDK
Copyright (c) 2014-2017 ARM Limited
Copyright (c) 2018 ON Semiconductor

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import copy
import stat
import os
from os.path import splitext, basename, dirname, abspath, isdir
from os import remove, mkdir
from shutil import rmtree, copyfile
from tools.targets import TARGET_MAP
from tools.export.exporters import Exporter
from tools.export.makefile import GccArm

class CodeBlocks(GccArm):
NAME = 'Code::Blocks'

DOT_IN_RELATIVE_PATH = True

MBED_CONFIG_HEADER_SUPPORTED = True

PREPROCESS_ASM = False

POST_BINARY_WHITELIST = set([
"NCS36510TargetCode.ncs36510_addfib"
])

@staticmethod
def filter_dot(str_in):
"""
Remove the './' prefix, if present.
This function assumes that resources.win_to_unix()
replaced all windows backslashes with slashes.
"""
if str_in is None:
return None
if str_in[:2] == './':
return str_in[2:]
return str_in

@staticmethod
def prepare_lib(libname):
if "lib" == libname[:3]:
libname = libname[3:-2]
return "-l" + libname

@staticmethod
def prepare_sys_lib(libname):
return "-l" + libname

def generate(self):
self.resources.win_to_unix()

comp_flags = []
debug_flags = []
release_flags = [ '-Os', '-g1' ]
next_is_include = False
for f in self.flags['c_flags'] + self.flags['cxx_flags'] + self.flags['common_flags']:
f = f.strip()
if f == "-include":
next_is_include = True
continue
if f == '-c':
continue
if next_is_include:
f = '-include ' + f
next_is_include = False
if f.startswith('-O') or f.startswith('-g'):
debug_flags.append(f)
else:
comp_flags.append(f)
comp_flags = sorted(list(set(comp_flags)))
inc_dirs = [self.filter_dot(s) for s in self.resources.inc_dirs];
inc_dirs = [x for x in inc_dirs if (x is not None and
x != '' and x != '.' and
not x.startswith('bin') and
not x.startswith('obj'))];

c_sources = sorted([self.filter_dot(s) for s in self.resources.c_sources])
libraries = [self.prepare_lib(basename(lib)) for lib
in self.resources.libraries]
sys_libs = [self.prepare_sys_lib(lib) for lib
in self.toolchain.sys_libs]

ctx = {
'project_name': self.project_name,
'debug_flags': debug_flags,
'release_flags': release_flags,
'comp_flags': comp_flags,
'ld_flags': self.flags['ld_flags'],
'headers': sorted(list(set([self.filter_dot(s) for s in self.resources.headers]))),
'c_sources': c_sources,
's_sources': sorted([self.filter_dot(s) for s in self.resources.s_sources]),
'cpp_sources': sorted([self.filter_dot(s) for s in self.resources.cpp_sources]),
'include_paths': inc_dirs,
'linker_script': self.filter_dot(self.resources.linker_script),
'libraries': libraries,
'sys_libs': sys_libs,
'openocdboard': ''
}

openocd_board = {
'NCS36510': 'board/ncs36510_axdbg.cfg',
'DISCO_F429ZI': 'board/stm32f429discovery.cfg',
'DISCO_F469NI': 'board/stm32f469discovery.cfg',
'DISCO_L053C8': 'board/stm32l0discovery.cfg',
'DISCO_L072CZ_LRWAN1': 'board/stm32l0discovery.cfg',
'DISCO_F769NI': 'board/stm32f7discovery.cfg',
'DISCO_L475VG_IOT01A': 'board/stm32l4discovery.cfg',
'DISCO_L476VG': 'board/stm32l4discovery.cfg',
'NRF51822': 'board/nordic_nrf51822_mkit.cfg',
'NRF51822_BOOT': 'board/nordic_nrf51822_mkit.cfg',
'NRF51822_OTA': 'board/nordic_nrf51822_mkit.cfg',
'NRF51_DK_LEGACY': 'board/nordic_nrf51_dk.cfg',
'NRF51_DK_BOOT': 'board/nordic_nrf51_dk.cfg',
'NRF51_DK_OTA': 'board/nordic_nrf51_dk.cfg',
'NRF51_DK': 'board/nordic_nrf51_dk.cfg'
}

if self.target in openocd_board:
ctx['openocdboard'] = openocd_board[self.target]

self.gen_file('codeblocks/cbp.tmpl', ctx, "%s.%s" % (self.project_name, 'cbp'))
for f in [ 'obj', 'bin' ]:
if not isdir(f):
mkdir(f)
self.gen_file_nonoverwrite('codeblocks/mbedignore.tmpl',
ctx, f + '/.mbedignore')

# finally, generate the project file
super(CodeBlocks, self).generate()

@staticmethod
def clean(project_name):
for ext in ['cbp', 'depend', 'layout']:
remove("%s.%s" % (project_name, ext))
for f in ['openocd.log']:
remove(f)
for d in ['bin', 'obj']:
rmtree(d, ignore_errors=True)
93 changes: 93 additions & 0 deletions tools/export/codeblocks/cbp.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="{{project_name}}" />
<Option pch_mode="2" />
<Option compiler="arm-elf-gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/{{project_name}}.elf" prefix_auto="1" extension_auto="0" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="arm-elf-gcc" />
<Option use_console_runner="0" />
<Compiler>
{% for f in debug_flags -%}
<Add option="{{f}}" />
{% endfor -%}
</Compiler>
<Linker>
<Add option='-Wl,-Map,&quot;bin/Debug/{{project_name}}.map&quot;' />
</Linker>
</Target>
<Target title="Release">
<Option output="bin/Release/{{project_name}}.elf" prefix_auto="1" extension_auto="0" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="arm-elf-gcc" />
<Option use_console_runner="0" />
<Compiler>
<Add option="-DNDEBUG" />
{% for f in release_flags -%}
<Add option="{{f}}" />
{% endfor -%}
</Compiler>
<Linker>
<Add option='-Wl,-Map,&quot;bin/Release/{{project_name}}.map&quot;' />
</Linker>
</Target>
</Build>
<Compiler>
{% for f in comp_flags -%}
<Add option="{{f}}" />
{% endfor -%}
{% for f in include_paths -%}
<Add directory="{{f}}" />
{% endfor -%}
</Compiler>
<Linker>
{% for f in ld_flags -%}
<Add option="{{f}}" />
{% endfor -%}
<Add option="-T {{linker_script}}" />
<Add option="-Wl,--start-group {{sys_libs|join(" ")}} {{libraries|join(" ")}} -Wl,--end-group" />
{% for f in sys_libs -%}
<Add option="{{f}}" />
{% endfor -%}
{% for f in libraries -%}
<Add option="{{f}}" />
{% endfor -%}
</Linker>
{% for f in headers -%}
<Unit filename="{{f}}"/>
{% endfor -%}
{% for f in c_sources -%}
<Unit filename="{{f}}">
<Option compilerVar="CC" />
</Unit>
{% endfor -%}
{% for f in s_sources -%}
<Unit filename="{{f}}">
<Option compilerVar="CPP" />
</Unit>
{% endfor -%}
{% for f in cpp_sources -%}
<Unit filename="{{f}}">
<Option compilerVar="CPP" />
</Unit>
{% endfor -%}
<Extensions>
{% if openocdboard != '' -%}
<debugger>
<remote_debugging target="Release">
<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" />
</remote_debugging>
<remote_debugging target="Debug">
<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" />
</remote_debugging>
</debugger>
{% endif -%}
</Extensions>
</Project>
</CodeBlocks_project_file>
1 change: 1 addition & 0 deletions tools/export/codeblocks/mbedignore.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*