22
22
23
23
from jinja2 .exceptions import TemplateNotFound
24
24
25
- from tools .export .exporters import Exporter
25
+ from tools .export .exporters import Exporter , apply_supported_whitelist
26
+ from tools .targets import TARGET_MAP
26
27
from tools .utils import NotSupportedException
27
28
28
29
@@ -37,35 +38,55 @@ class CMake(Exporter):
37
38
38
39
PREPROCESS_ASM = False
39
40
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
+
40
48
@classmethod
41
49
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 )
43
53
44
54
def generate (self ):
45
55
"""Generate the CMakefiles.txt
46
56
"""
47
57
self .resources .win_to_unix ()
48
58
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 ]
63
84
64
85
ctx = {
65
86
'name' : self .project_name ,
66
87
'target' : self .target ,
67
88
'sources' : srcs ,
68
- 'libs ' : libs ,
89
+ 'dependencies ' : depSources ,
69
90
'libraries' : libraries ,
70
91
'ld_sys_libs' : sys_libs ,
71
92
'include_paths' : sorted (list (set (self .resources .inc_dirs ))),
@@ -74,14 +95,15 @@ def generate(self):
74
95
'hex_files' : self .resources .hex_files ,
75
96
'ar' : basename (self .toolchain .ar ),
76
97
'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" ),
78
99
'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" ),
80
101
'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 () ),
83
104
'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 :])),
85
107
'elf2bin' : basename (self .toolchain .elf2bin ),
86
108
'link_script_ext' : self .toolchain .LINKER_EXT ,
87
109
'link_script_option' : self .LINK_SCRIPT_OPTION ,
@@ -90,21 +112,17 @@ def generate(self):
90
112
}
91
113
92
114
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 :])
97
118
else :
98
119
ctx ['pp' ] = None
120
+ ctx ['pp_flags' ] = None
99
121
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
108
126
109
127
@staticmethod
110
128
def build (project_name , log_name = "build_log.txt" , cleanup = True ):
@@ -168,7 +186,6 @@ def prepare_lib(libname):
168
186
def prepare_sys_lib (libname ):
169
187
return "-l" + libname
170
188
171
-
172
189
# class Arm(CMake):
173
190
# """ARM Compiler generic cmake target"""
174
191
# LINK_SCRIPT_OPTION = "--scatter"
0 commit comments