Skip to content

Commit 123c2b0

Browse files
committed
build: convert output variables to target
Convert the out parameters for `_add_host_variant_c_compile_flags` to use the target as an in-parameter. Doing so allows us to set the properties on the target directly rather than providing them as out parameters which then get set subsequently.
1 parent 9873280 commit 123c2b0

File tree

2 files changed

+69
-74
lines changed

2 files changed

+69
-74
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 68 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -159,41 +159,34 @@ function(_add_host_variant_c_compile_link_flags)
159159
endfunction()
160160

161161

162-
function(_add_host_variant_c_compile_flags)
163-
set(oneValueArgs RESULT_VAR_NAME)
164-
cmake_parse_arguments(CFLAGS
165-
""
166-
"${oneValueArgs}"
167-
""
168-
${ARGN})
169-
170-
set(result ${${CFLAGS_RESULT_VAR_NAME}})
171-
162+
function(_add_host_variant_c_compile_flags target)
172163
_add_host_variant_c_compile_link_flags(
173164
ANALYZE_CODE_COVERAGE FALSE
174165
RESULT_VAR_NAME result)
166+
target_compile_options(${target} PRIVATE
167+
${result})
175168

176169
is_build_type_optimized("${CMAKE_BUILD_TYPE}" optimized)
177170
if(optimized)
178-
list(APPEND result "-O2")
171+
target_compile_options(${target} PRIVATE -O2)
179172

180173
# Omit leaf frame pointers on x86 production builds (optimized, no debug
181174
# info, and no asserts).
182175
is_build_type_with_debuginfo("${CMAKE_BUILD_TYPE}" debug)
183176
if(NOT debug AND NOT LLVM_ENABLE_ASSERTIONS)
184177
if(SWIFT_HOST_VARIANT_ARCH MATCHES "i?86")
185178
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
186-
list(APPEND result "-momit-leaf-frame-pointer")
179+
target_compile_options(${target} PRIVATE -momit-leaf-frame-pointer)
187180
else()
188-
list(APPEND result "/Oy")
181+
target_compile_options(${target} PRIVATE /Oy)
189182
endif()
190183
endif()
191184
endif()
192185
else()
193186
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
194-
list(APPEND result "-O0")
187+
target_compile_options(${target} PRIVATE -O0)
195188
else()
196-
list(APPEND result "/Od")
189+
target_compile_options(${target} PRIVATE /Od)
197190
endif()
198191
endif()
199192

@@ -203,64 +196,73 @@ function(_add_host_variant_c_compile_flags)
203196
if(debuginfo)
204197
_compute_lto_flag("${SWIFT_TOOLS_ENABLE_LTO}" _lto_flag_out)
205198
if(_lto_flag_out)
206-
list(APPEND result "-gline-tables-only")
199+
target_compile_options(${target} PRIVATE -gline-tables-only)
207200
else()
208-
list(APPEND result "-g")
201+
target_compile_options(${target} PRIVATE -g)
209202
endif()
210203
else()
211-
list(APPEND result "-g0")
204+
target_compile_options(${target} PRIVATE -g0)
212205
endif()
213206
endif()
214207

215208
if(SWIFT_HOST_VARIANT_SDK STREQUAL WINDOWS)
216209
# MSVC/clang-cl don't support -fno-pic or -fms-compatibility-version.
217210
if(NOT SWIFT_COMPILER_IS_MSVC_LIKE)
218-
list(APPEND result -fno-pic)
219-
list(APPEND result "-fms-compatibility-version=1900")
211+
target_compile_options(${target} PRIVATE
212+
-fms-compatibility-version=1900
213+
-fno-pic)
220214
endif()
221215

222-
list(APPEND result "-DLLVM_ON_WIN32")
223-
list(APPEND result "-D_CRT_SECURE_NO_WARNINGS")
224-
list(APPEND result "-D_CRT_NONSTDC_NO_WARNINGS")
216+
target_compile_definitions(${target} PRIVATE
217+
LLVM_ON_WIN32
218+
_CRT_SECURE_NO_WARNINGS
219+
_CRT_NONSTDC_NO_WARNINGS)
225220
if(NOT "${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
226-
list(APPEND result "-D_CRT_USE_BUILTIN_OFFSETOF")
221+
target_compile_definitions(${target} PRIVATE
222+
_CRT_USE_BUILTIN_OFFSETOF)
227223
endif()
228224
# TODO(compnerd) permit building for different families
229-
list(APPEND result "-D_CRT_USE_WINAPI_FAMILY_DESKTOP_APP")
225+
target_compile_definitions(${target} PRIVATE
226+
_CRT_USE_WINAPI_FAMILY_DESKTOP_APP)
230227
if(SWIFT_HOST_VARIANT_ARCH MATCHES arm)
231-
list(APPEND result "-D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE")
228+
target_compile_definitions(${target} PRIVATE
229+
_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE)
232230
endif()
233-
list(APPEND result "-D_MT")
234-
# TODO(compnerd) handle /MT
235-
list(APPEND result "-D_DLL")
236-
# NOTE: We assume that we are using VS 2015 U2+
237-
list(APPEND result "-D_ENABLE_ATOMIC_ALIGNMENT_FIX")
238-
# NOTE: We use over-aligned values for the RefCount side-table
239-
# (see revision d913eefcc93f8c80d6d1a6de4ea898a2838d8b6f)
240-
# This is required to build with VS2017 15.8+
241-
list(APPEND result "-D_ENABLE_EXTENDED_ALIGNED_STORAGE=1")
231+
target_compile_definitions(${target} PRIVATE
232+
# TODO(compnerd) handle /MT
233+
_MD
234+
_DLL
235+
# NOTE: We assume that we are using VS 2015 U2+
236+
_ENABLE_ATOMIC_ALIGNMENT_FIX
237+
# NOTE: We use over-aligned values for the RefCount side-table
238+
# (see revision d913eefcc93f8c80d6d1a6de4ea898a2838d8b6f)
239+
# This is required to build with VS2017 15.8+
240+
_ENABLE_EXTENDED_ALIGNED_STORAGE=1)
242241

243242
# msvcprt's std::function requires RTTI, but we do not want RTTI data.
244243
# Emulate /GR-.
245244
# TODO(compnerd) when moving up to VS 2017 15.3 and newer, we can disable
246245
# RTTI again
247246
if(SWIFT_COMPILER_IS_MSVC_LIKE)
248-
list(APPEND result /GR-)
247+
target_compile_options(${target} PRIVATE /GR-)
249248
else()
250-
list(APPEND result -frtti)
251-
list(APPEND result -Xclang;-fno-rtti-data)
249+
target_compile_options(${target} PRIVATE
250+
-frtti
251+
"SHELL:-Xclang -fno-rtti-data")
252252
endif()
253253

254254
# NOTE: VS 2017 15.3 introduced this to disable the static components of
255255
# RTTI as well. This requires a newer SDK though and we do not have
256256
# guarantees on the SDK version currently.
257-
list(APPEND result "-D_HAS_STATIC_RTTI=0")
257+
target_compile_definitions(${target} PRIVATE
258+
_HAS_STATIC_RTTI=0)
258259

259260
# NOTE(compnerd) workaround LLVM invoking `add_definitions(-D_DEBUG)` which
260261
# causes failures for the runtime library when cross-compiling due to
261262
# undefined symbols from the standard library.
262263
if(NOT CMAKE_BUILD_TYPE STREQUAL Debug)
263-
list(APPEND result "-U_DEBUG")
264+
target_compile_options(${target} PRIVATE
265+
-U_DEBUG)
264266
endif()
265267
endif()
266268

@@ -274,52 +276,54 @@ function(_add_host_variant_c_compile_flags)
274276
# the build.
275277
if(CMAKE_C_COMPILER_ID MATCHES Clang AND CMAKE_C_COMPILER_VERSION
276278
VERSION_LESS 9.0.0)
277-
list(APPEND result -mcx16)
279+
target_compile_options(${target} PRIVATE -mcx16)
278280
endif()
279281
endif()
280282
endif()
281283

282284
if(LLVM_ENABLE_ASSERTIONS)
283-
list(APPEND result "-UNDEBUG")
285+
target_compile_options(${target} PRIVATE -UNDEBUG)
284286
else()
285-
list(APPEND result "-DNDEBUG")
287+
target_compile_definitions(${target} PRIVATE -DNDEBUG)
286288
endif()
287-
289+
288290
if(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS)
289-
list(APPEND result "-DSWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS")
291+
target_compile_definitions(${target} PRIVATE
292+
SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS)
290293
endif()
291294

292295
if(SWIFT_ANALYZE_CODE_COVERAGE)
293-
list(APPEND result "-fprofile-instr-generate"
294-
"-fcoverage-mapping")
296+
target_compile_options(${target} PRIVATE
297+
-fprofile-instr-generate
298+
-fcoverage-mapping)
295299
endif()
296300

297301
if((SWIFT_HOST_VARIANT_ARCH STREQUAL armv7 OR
298302
SWIFT_HOST_VARIANT_ARCH STREQUAL aarch64) AND
299303
(SWIFT_HOST_VARIANT_SDK STREQUAL LINUX OR
300304
SWIFT_HOST_VARIANT_SDK STREQUAL ANDROID))
301-
list(APPEND result -funwind-tables)
305+
target_compile_options(${target} PRIVATE -funwind-tables)
302306
endif()
303307

304308
if(SWIFT_HOST_VARIANT_SDK STREQUAL ANDROID)
305-
list(APPEND result -nostdinc++)
309+
target_compile_options(${target} PRIVATE -nostdinc++)
306310
swift_android_libcxx_include_paths(CFLAGS_CXX_INCLUDES)
307311
swift_android_include_for_arch("${SWIFT_HOST_VARIANT_ARCH}"
308312
"${SWIFT_HOST_VARIANT_ARCH}_INCLUDE")
309-
foreach(path IN LISTS CFLAGS_CXX_INCLUDES ${SWIFT_HOST_VARIANT_ARCH}_INCLUDE)
310-
list(APPEND result "SHELL:${CMAKE_INCLUDE_SYSTEM_FLAG_C}${path}")
311-
endforeach()
312-
list(APPEND result "-D__ANDROID_API__=${SWIFT_ANDROID_API_LEVEL}")
313+
target_include_directories(${target} SYSTEM PRIVATE
314+
${CFLAGS_CXX_INCLUDES}
315+
${${SWIFT_HOST_VARIANT_ARCH}_INCLUDE})
316+
target_compile_definitions(${target} PRIVATE
317+
__ANDROID_API__=${SWIFT_ANDROID_API_LEVEL})
313318
endif()
314319

315320
if(SWIFT_HOST_VARIANT_SDK STREQUAL "LINUX")
316321
if(SWIFT_HOST_VARIANT_ARCH STREQUAL x86_64)
317-
# this is the minimum architecture that supports 16 byte CAS, which is necessary to avoid a dependency to libatomic
318-
list(APPEND result "-march=core2")
322+
# this is the minimum architecture that supports 16 byte CAS, which is
323+
# necessary to avoid a dependency to libatomic
324+
target_compile_options(${target} PRIVATE -march=core2)
319325
endif()
320326
endif()
321-
322-
set("${CFLAGS_RESULT_VAR_NAME}" "${result}" PARENT_SCOPE)
323327
endfunction()
324328

325329
function(_add_host_variant_link_flags target)
@@ -586,18 +590,16 @@ function(_add_swift_host_library_single target)
586590
# Call llvm_config() only for libraries that are part of the compiler.
587591
swift_common_llvm_config("${target}" ${ASHLS_LLVM_LINK_COMPONENTS})
588592

589-
# Collect compile and link flags for the static and non-static targets.
590-
# Don't set PROPERTY COMPILE_FLAGS or LINK_FLAGS directly.
591-
set(c_compile_flags ${ASHLS_C_COMPILE_FLAGS})
592-
set(link_flags)
593-
594-
_add_host_variant_c_compile_flags(RESULT_VAR_NAME c_compile_flags)
595-
593+
target_compile_options(${target} PRIVATE
594+
${ASHLS_C_COMPILE_FLAGS})
596595
if(SWIFT_HOST_VARIANT_SDK STREQUAL WINDOWS)
597596
if(libkind STREQUAL SHARED)
598-
list(APPEND c_compile_flags -D_WINDLL)
597+
target_compile_definitions(${target} PRIVATE
598+
_WINDLL)
599599
endif()
600600
endif()
601+
602+
_add_host_variant_c_compile_flags(${target})
601603
_add_host_variant_link_flags(${target})
602604

603605
# Set compilation and link flags.
@@ -621,8 +623,6 @@ function(_add_swift_host_library_single target)
621623
endif()
622624
endif()
623625

624-
target_compile_options(${target} PRIVATE
625-
${c_compile_flags})
626626
if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_APPLE_PLATFORMS)
627627
target_link_options(${target} PRIVATE
628628
"LINKER:-compatibility_version,1")
@@ -743,11 +743,9 @@ function(add_swift_host_tool executable)
743743
precondition(ASHT_SWIFT_COMPONENT
744744
MESSAGE "Swift Component is required to add a host tool")
745745

746-
_add_host_variant_c_compile_flags(RESULT_VAR_NAME c_compile_flags)
747746

748747
add_executable(${executable} ${ASHT_UNPARSED_ARGUMENTS})
749-
target_compile_options(${executable} PRIVATE
750-
${c_compile_flags})
748+
_add_host_variant_c_compile_flags(${executable})
751749
_add_host_variant_link_flags(${executable})
752750
target_link_directories(${executable} PRIVATE
753751
${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR})

tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,8 @@ endfunction()
4141
# FIXME: this is a HACK. All SourceKit CMake code using this function should be
4242
# rewritten to use 'add_swift_host_library' or 'add_swift_target_library'.
4343
function(add_sourcekit_default_compiler_flags target)
44-
set(c_compile_flags)
45-
set(link_flags)
46-
4744
# Add variant-specific flags.
48-
_add_host_variant_c_compile_flags(RESULT_VAR_NAME c_compile_flags)
45+
_add_host_variant_c_compile_flags(${target})
4946
_add_host_variant_link_flags(${target})
5047

5148
# Set compilation and link flags.

0 commit comments

Comments
 (0)