Skip to content

Commit dc31af3

Browse files
committed
CMake: Fix compiler flag passing to GN
Sometimes we want to pass a group of compiler flags in CMake without risking them being mangled by deduplication. CMake does this by appending a "SHELL:" marker to the front of the group. Make sure these flag groups are correctly forwarded to GN. While we are here, fix an issue with variable quoting and make GN generation steps depend explicitly on cmake/QtBuildGnHelpers.cmake. Change-Id: I5827880236b9ae1e0cdc47a6f3bcdc3f8c298ea4 Pick-to: 6.9 Reviewed-by: Michal Klocek <[email protected]>
1 parent 4285358 commit dc31af3

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

cmake/Functions.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ function(add_gn_command)
447447
run_${arg_MODULE}_GnReady
448448
"${WEBENGINE_ROOT_SOURCE_DIR}/src/${arg_MODULE}/configure/BUILD.root.gn.in"
449449
"${WEBENGINE_ROOT_SOURCE_DIR}/cmake/QtGnGen.cmake"
450+
"${WEBENGINE_ROOT_SOURCE_DIR}/cmake/QtBuildGnHelpers.cmake"
450451
)
451452
add_custom_target(runGn_${arg_GN_TARGET}
452453
DEPENDS #TODO this is fixed in cmake 3.20 so we could simply use GN_TARGET and not create new one

cmake/QtBuildGnHelpers.cmake

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ function(recover_framework_build includeDirs compilerFlags)
4141
set(${compilerFlags} ${${compilerFlags}} ${frameworkDirs} PARENT_SCOPE)
4242
endfunction()
4343

44+
# CMake uses a "SHELL:" prefix to group options and avoid unwanted option de-duplication; we
45+
# need to strip these after manually de-duplicating, but before passing to GN.
46+
# See https://cmake.org/cmake/help/latest/command/target_compile_options.html#option-de-duplication
47+
function(transform_cmake_compile_options_for_gn out_var compile_options_var)
48+
get_property(flags_var DIRECTORY PROPERTY ${compile_options_var})
49+
50+
list(REMOVE_DUPLICATES flags_var)
51+
set(out_flags "")
52+
foreach(elem IN LISTS flags_var)
53+
if(elem MATCHES "^SHELL:(.*)")
54+
# Split on spaces and enclose each argument with quotes.
55+
string(REPLACE " " "\";\"" elem "${CMAKE_MATCH_1}")
56+
endif()
57+
list(APPEND out_flags "\"${elem}\"")
58+
endforeach()
59+
60+
set(${out_var} ${out_flags} PARENT_SCOPE)
61+
endfunction()
62+
4463
function(configure_gn_target source_dir in_file_path out_file_path path_mode)
4564

4665
# GN_SOURCES GN_HEADERS
@@ -76,18 +95,10 @@ function(configure_gn_target source_dir in_file_path out_file_path path_mode)
7695
set(GN_ARGS_MOC_BIN \"${moc_file_path}\")
7796

7897
# GN_CFLAGS_CC
79-
get_property(gn_cxx_compile_options DIRECTORY PROPERTY GN_CXX_COMPILE_OPTIONS)
80-
foreach(gn_cxx_compile_option ${gn_cxx_compile_options})
81-
list(APPEND GN_CFLAGS_CC \"${gn_cxx_compile_option}\")
82-
endforeach()
83-
list(REMOVE_DUPLICATES GN_CFLAGS_CC)
98+
transform_cmake_compile_options_for_gn(GN_CFLAGS_CC GN_CXX_COMPILE_OPTIONS)
8499

85100
# GN_CFLAGS_C
86-
get_property(gn_c_compile_options DIRECTORY PROPERTY GN_C_COMPILE_OPTIONS)
87-
foreach(gn_c_compile_option ${gn_c_compile_options})
88-
list(APPEND GN_CFLAGS_C \"${gn_c_compile_option}\")
89-
endforeach()
90-
list(REMOVE_DUPLICATES GN_CFLAGS_C)
101+
transform_cmake_compile_options_for_gn(GN_CFLAGS_C GN_C_COMPILE_OPTIONS)
91102

92103
# GN_SOURCE_ROOT
93104
get_filename_component(GN_SOURCE_ROOT "${source_dir}" ${path_mode})

cmake/QtGnConfigHelpers.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ endfunction()
6262
function(create_c_config cmake_target arch config_file_name)
6363
file(GENERATE
6464
OUTPUT $<CONFIG>/${arch}/${config_file_name}
65-
CONTENT "set(GN_C_COMPILE_OPTIONS $<TARGET_PROPERTY:COMPILE_OPTIONS>)"
65+
CONTENT "set(GN_C_COMPILE_OPTIONS \"$<TARGET_PROPERTY:COMPILE_OPTIONS>\")"
6666
CONDITION $<COMPILE_LANGUAGE:C>
6767
TARGET ${cmake_target})
6868
endfunction()

0 commit comments

Comments
 (0)