2
2
# SPDX-License-Identifier: Apache-2.0.
3
3
4
4
import os
5
+ import pprint
5
6
import re
6
7
import shutil
8
+ from collections import defaultdict
7
9
from functools import lru_cache , partial
8
10
from pathlib import Path
9
11
@@ -100,6 +102,28 @@ def _project_dirs(env, project):
100
102
return source_dir , build_dir , install_dir
101
103
102
104
105
+ def _merge_cmake_lang_flags (cmake_args ):
106
+ print ("=== _merge_cmake_lang_flags: cmake args" )
107
+ pprint .pprint (cmake_args , indent = 4 , depth = 4 )
108
+ pattern = re .compile (r'''-D(CMAKE_C(?:XX)?_FLAGS)=["']?([^"']+)''' )
109
+
110
+ new_cmake_args = []
111
+
112
+ cmake_lang_flags = defaultdict (list )
113
+ for arg in cmake_args :
114
+ m = pattern .match (arg )
115
+ if m :
116
+ cmake_lang_flags [m .group (1 )].append (m .group (2 ))
117
+ else :
118
+ new_cmake_args .append (arg )
119
+
120
+ pprint .pprint (cmake_lang_flags , indent = 4 , depth = 4 )
121
+
122
+ for (k , v ) in cmake_lang_flags .items ():
123
+ new_cmake_args .append ('-D{}={}' .format (k , ' ' .join (v )))
124
+
125
+ return new_cmake_args
126
+
103
127
def _build_project (env , project , cmake_extra , build_tests = False , args_transformer = None , coverage = False ):
104
128
sh = env .shell
105
129
config = project .get_config (env .spec )
@@ -135,6 +159,7 @@ def _build_project(env, project, cmake_extra, build_tests=False, args_transforme
135
159
136
160
# Set compiler flags
137
161
compiler_flags = []
162
+ c_path = None
138
163
if toolchain .compiler != 'default' and toolchain .compiler != 'msvc' and not toolchain .cross_compile :
139
164
c_path = toolchain .compiler_path ()
140
165
cxx_path = toolchain .cxx_compiler_path ()
@@ -177,6 +202,13 @@ def _build_project(env, project, cmake_extra, build_tests=False, args_transforme
177
202
else :
178
203
raise Exception ('--coverage only support GCC as compiler. Current compiler is: {}' .format (c_path ))
179
204
205
+ # If there are multiple of the same -DCMAKE_<LANG>_FLAGS arguments, CMake takes only the last one.
206
+ # Since -DCMAKE_<LANG>_FLAGS can be set in multiple places (e.g. in a default config for a specific platform or
207
+ # compiler, in a user project's config, in this Python module, etc.), we should merge language flags into one per
208
+ # language.
209
+ cmake_args = _merge_cmake_lang_flags (cmake_args )
210
+ print ("=== _build_project: cmake_args: {}" .format (cmake_args ))
211
+
180
212
# Allow caller to programmatically tweak the cmake_args,
181
213
# as a last resort in case data merging wasn't working out
182
214
if args_transformer :
@@ -199,7 +231,7 @@ def _build_project(env, project, cmake_extra, build_tests=False, args_transforme
199
231
200
232
# build & install
201
233
sh .exec (* toolchain .shell_env , cmake , "--build" , project_build_dir , "--config" ,
202
- build_config , "--target" , "install" , working_dir = working_dir , check = True )
234
+ build_config , "--verbose" , "-- target" , "install" , working_dir = working_dir , check = True )
203
235
204
236
205
237
class CMakeBuild (Action ):
0 commit comments