Skip to content

Commit 6f667e5

Browse files
committed
explain code, fix naming, work on linking
1 parent 07b21ab commit 6f667e5

File tree

2 files changed

+77
-57
lines changed

2 files changed

+77
-57
lines changed

tools/export/cmake/CMakeLists.txt.tmpl

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,27 @@ SET(CMAKE_CROSSCOMPILING TRUE)
1111
SET(CMAKE_C_COMPILER_WORKS TRUE)
1212
SET(CMAKE_CXX_COMPILER_WORKS TRUE)
1313

14-
SET(CMAKE_ASM_COMPILER_INIT "{{asm}}")
15-
SET(CMAKE_C_COMPILER_INIT "{{cc}}")
16-
SET(CMAKE_CXX_COMPILER_INIT "{{cxx}}")
17-
SET(CMAKE_CXX_LINK_EXECUTABLE "{{ld}}")
18-
19-
{% if pp -%}
20-
SET(PREPROC "{{pp}}")
21-
{%- endif %}
14+
SET(CMAKE_ASM_COMPILER_INIT "{{asm}}")
15+
SET(CMAKE_C_COMPILER_INIT "{{cc}}")
16+
SET(CMAKE_CXX_COMPILER_INIT "{{cxx}}")
17+
SET(ELF2BIN "{{elf2bin}}")
2218
{% if hex_files %}
2319
SET(SREC_CAT "srec_cat")
2420
{%- endif %}
2521

2622
# here starts the project
27-
PROJECT({{name}} C CXX ASM)
23+
PROJECT(cmake-{{name}} C CXX ASM)
2824
#SET(CMAKE_VERBOSE_MAKEFILE ON)
2925

26+
SET(LD_SYS_LIBS "{%- block sys_libs -%} -Wl,--start-group {{ld_sys_libs|join(" ")}} {{libraries|join(" ")}} -Wl,--end-group {%- endblock -%}")
27+
3028
SET(CMAKE_C_FLAGS "{{cc_flags}} -include mbed_config.h")
3129
SET(CMAKE_CXX_FLAGS "{{cxx_flags}} -include mbed_config.h")
3230
SET(CMAKE_ASM_FLAGS "{{asm_flags}} -include mbed_config.h")
33-
SET(CMAKE_LINKER_FLAGS "{{ld_flags}}")
34-
35-
SET(LD_SYS_LIBS "{{ld_sys_libs|join(" ")}}")
36-
SET(ELF2BIN {{elf2bin}})
37-
38-
SET(CMAKE_EXE_LINKER_FLAGS "{{ld_flags}} {{link_script_option}} ${LD_SYS_LIBS}")
31+
SET(CMAKE_CXX_LINK_FLAGS "{{ld_flags}}")
32+
{% if pp -%}
33+
SET(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_INK_FLAGS} {{link_script_option}} ${CMAKE_BINARY_DIR}/{{name}}.link_script.ld ${LD_SYS_LIBS}")
34+
{%- endif %}
3935

4036
ADD_DEFINITIONS(
4137
{% for d in symbols %}-D{{d}}
@@ -44,21 +40,29 @@ INCLUDE_DIRECTORIES(
4440
{% for p in include_paths %}{{p}}
4541
{% endfor %})
4642

47-
{% for libname,libsrcs in libs.items() %}
43+
{% for libname,libsrcs in dependencies.items() %}
4844
# target for library "{{libname}}"
49-
ADD_LIBRARY({{libname}} STATIC EXCLUDE_FROM_ALL
45+
ADD_LIBRARY({{libname}} STATIC
5046
{% for libsrc in libsrcs %}{{libsrc}}
5147
{% endfor %})
5248
{% endfor %}
5349

5450
# executable {{name}}
55-
ADD_EXECUTABLE({{name}} EXCLUDE_FROM_ALL
51+
ADD_EXECUTABLE({{name}}.elf
5652
{% for src in sources %}{{src}}
5753
{% endfor %})
58-
TARGET_LINK_LIBRARIES({{name}}
59-
{% for libname in libs %}{{libname}}
54+
TARGET_LINK_LIBRARIES({{name}}.elf
55+
{% for libname in dependencies %}{{libname}}
6056
{% endfor %})
6157

58+
{% if pp -%}
59+
add_custom_command(TARGET {{name}}.elf PRE_LINK
60+
COMMAND "{{pp}}" {{pp_flags}} {{linker_script}} -o ${CMAKE_CURRENT_BINARY_DIR}/{{name}}.link_script.ld
61+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
62+
BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/{{name}}.link_script.ld"
63+
)
64+
{%- endif %}
65+
6266
##########################################################################
6367
# mbed-cli specific targets
6468
##########################################################################
@@ -80,8 +84,7 @@ ELSE()
8084
ENDIF()
8185

8286
# optional custom target to build via mbed-cli
83-
MESSAGE(STATUS "Creating target 01-{{name}} for mbed compilation...")
84-
ADD_CUSTOM_TARGET(01-{{name}} ALL
87+
ADD_CUSTOM_TARGET(mbed-cli-build EXCLUDE_FROM_ALL
8588
COMMAND ${CMAKE_COMMAND} -E echo "mbed compile --build BUILD/${CMAKE_BUILD_TYPE} ${MBED_BUILD_PROFILE}"
8689
COMMAND mbed compile --build BUILD/${CMAKE_BUILD_TYPE} --profile ${MBED_BUILD_PROFILE}
8790
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}

tools/export/cmake/__init__.py

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
from jinja2.exceptions import TemplateNotFound
2424

25-
from tools.export.exporters import Exporter
25+
from tools.export.exporters import Exporter, apply_supported_whitelist
26+
from tools.targets import TARGET_MAP
2627
from tools.utils import NotSupportedException
2728

2829

@@ -37,35 +38,55 @@ class CMake(Exporter):
3738

3839
PREPROCESS_ASM = False
3940

41+
POST_BINARY_WHITELIST = set([
42+
"MCU_NRF51Code.binary_hook",
43+
"TEENSY3_1Code.binary_hook",
44+
"LPCTargetCode.lpc_patch",
45+
"LPC4088Code.binary_hook"
46+
])
47+
4048
@classmethod
4149
def is_target_supported(cls, target_name):
42-
return True
50+
target = TARGET_MAP[target_name]
51+
return apply_supported_whitelist(
52+
cls.TOOLCHAIN, cls.POST_BINARY_WHITELIST, target)
4353

4454
def generate(self):
4555
"""Generate the CMakefiles.txt
4656
"""
4757
self.resources.win_to_unix()
4858

49-
sources = set(self.resources.c_sources + \
50-
self.resources.cpp_sources + \
51-
self.resources.s_sources + \
52-
self.resources.headers)
53-
54-
libnames = [l[:-4] for l in self.resources.lib_refs]
55-
libs = {re.sub(r'^[.]/', '', l): sorted([f for f in sources if f.startswith(l)]) for l in libnames}
56-
libs = {k: v for k, v in libs.items() if len(v) != 0}
57-
srcs = sorted([f for f in sources if f not in [item for sublist in libs.values() for item in sublist]])
58-
59-
libraries = [self.prepare_lib(basename(lib)) for lib
60-
in self.resources.libraries]
61-
sys_libs = [self.prepare_sys_lib(lib) for lib
62-
in self.toolchain.sys_libs]
59+
# get all source files including headers, adding headers allows IDEs to detect which files
60+
# belong to the project, otherwise headers may be greyed out and not work with inspection
61+
# (that is true for CLion and definitely for Visual Code)
62+
allSourceFiles = set(self.resources.c_sources +
63+
self.resources.cpp_sources +
64+
self.resources.s_sources +
65+
self.resources.headers)
66+
67+
# create a list of dependencies (mbed add ...)
68+
dependencies = [l[:-4] for l in self.resources.lib_refs]
69+
# separate the individual dependency source files into a map with the dep name as key and an array if files
70+
depSources = {re.sub(r'^[.]/', '', l):
71+
sorted([f for f in allSourceFiles if f.startswith(l)]) for l in dependencies}
72+
# delete dependencies that have no source files (may happen if a sub-dependency is ignored by .mbedignore)
73+
depSources = {k: v for k, v in depSources.items() if len(v) != 0}
74+
75+
# remove all source files that ended up being part of one of the dependencies
76+
# we flatten the list of source files from all dependencies and
77+
# then only add file to srcs if its not in that list
78+
# (basically srcs = allSourcefiles - flatten(depSources.values())
79+
srcs = [f for f in allSourceFiles if f not in [item for sublist in depSources.values() for item in sublist]]
80+
81+
# additional libraries
82+
libraries = [self.prepare_lib(basename(lib)) for lib in self.resources.libraries]
83+
sys_libs = [self.prepare_sys_lib(lib) for lib in self.toolchain.sys_libs]
6384

6485
ctx = {
6586
'name': self.project_name,
6687
'target': self.target,
6788
'sources': srcs,
68-
'libs': libs,
89+
'dependencies': depSources,
6990
'libraries': libraries,
7091
'ld_sys_libs': sys_libs,
7192
'include_paths': sorted(list(set(self.resources.inc_dirs))),
@@ -74,14 +95,15 @@ def generate(self):
7495
'hex_files': self.resources.hex_files,
7596
'ar': basename(self.toolchain.ar),
7697
'cc': basename(self.toolchain.cc[0]),
77-
'cc_flags': " ".join(self.toolchain.cc[1:]),
98+
'cc_flags': " ".join(flag for flag in self.toolchain.cc[1:] if not flag == "-c"),
7899
'cxx': basename(self.toolchain.cppc[0]),
79-
'cxx_flags': " ".join(self.toolchain.cppc[1:]),
100+
'cxx_flags': " ".join(flag for flag in self.toolchain.cppc[1:] if not flag == "-c"),
80101
'asm': basename(self.toolchain.asm[0]),
81-
'asm_flags': " ".join(self.toolchain.asm[1:]),
82-
'symbols': self.toolchain.get_symbols(),
102+
'asm_flags': " ".join(flag for flag in self.toolchain.asm[1:] if not flag == "-c"),
103+
'symbols': sorted(self.toolchain.get_symbols()),
83104
'ld': basename(self.toolchain.ld[0]),
84-
'ld_flags': " ".join(self.toolchain.ld[1:]),
105+
# fix the missing underscore '_' (see
106+
'ld_flags': re.sub("--wrap,_(?!_)", "--wrap,__", " ".join(self.toolchain.ld[1:])),
85107
'elf2bin': basename(self.toolchain.elf2bin),
86108
'link_script_ext': self.toolchain.LINKER_EXT,
87109
'link_script_option': self.LINK_SCRIPT_OPTION,
@@ -90,21 +112,17 @@ def generate(self):
90112
}
91113

92114
if hasattr(self.toolchain, "preproc"):
93-
ctx['pp'] = " ".join(["\'" + part + "\'" for part
94-
in ([basename(self.toolchain.preproc[0])] +
95-
self.toolchain.preproc[1:] +
96-
self.toolchain.ld[1:])])
115+
ctx['pp'] = basename(self.toolchain.preproc[0])
116+
ctx['pp_flags'] = " ".join(self.toolchain.preproc[1:] +
117+
self.toolchain.ld[1:])
97118
else:
98119
ctx['pp'] = None
120+
ctx['pp_flags'] = None
99121

100-
for templatefile in ['cmake/%s.tmpl' % self.TEMPLATE]:
101-
try:
102-
self.gen_file(templatefile, ctx, 'CMakeLists.txt')
103-
break
104-
except TemplateNotFound:
105-
pass
106-
else:
107-
raise NotSupportedException("This make tool is in development")
122+
try:
123+
self.gen_file('cmake/%s.tmpl' % self.TEMPLATE, ctx, 'CMakeLists.txt')
124+
except TemplateNotFound:
125+
pass
108126

109127
@staticmethod
110128
def build(project_name, log_name="build_log.txt", cleanup=True):
@@ -168,7 +186,6 @@ def prepare_lib(libname):
168186
def prepare_sys_lib(libname):
169187
return "-l" + libname
170188

171-
172189
# class Arm(CMake):
173190
# """ARM Compiler generic cmake target"""
174191
# LINK_SCRIPT_OPTION = "--scatter"

0 commit comments

Comments
 (0)