CMake: clean up setting of platform specific linker flags

Set flags directly on the target, and use common function for all cases.
This refactoring helps with the next commit for test executables.

Ref D8714
This commit is contained in:
Brecht Van Lommel
2020-09-15 16:00:15 +02:00
parent 0700c045bc
commit f9fcb25d52
11 changed files with 22 additions and 51 deletions

View File

@@ -840,11 +840,10 @@ set(CXX_WARNINGS)
set(C_REMOVE_STRICT_FLAGS) set(C_REMOVE_STRICT_FLAGS)
set(CXX_REMOVE_STRICT_FLAGS) set(CXX_REMOVE_STRICT_FLAGS)
# libraries to link the binary with passed to target_link_libraries() # Libraries to link to targets in setup_platform_linker_libs
# known as LLIBS to scons
set(PLATFORM_LINKLIBS "") set(PLATFORM_LINKLIBS "")
# Added to linker flags in setup_liblinks # Added to target linker flags in setup_platform_linker_flags
# - CMAKE_EXE_LINKER_FLAGS # - CMAKE_EXE_LINKER_FLAGS
# - CMAKE_EXE_LINKER_FLAGS_DEBUG # - CMAKE_EXE_LINKER_FLAGS_DEBUG
set(PLATFORM_LINKFLAGS "") set(PLATFORM_LINKFLAGS "")

View File

@@ -39,6 +39,7 @@ macro(BLENDER_SRC_GTEST_EX)
add_definitions(${GLOG_DEFINES}) add_definitions(${GLOG_DEFINES})
add_executable(${TARGET_NAME} ${ARG_SRC} ${MANIFEST}) add_executable(${TARGET_NAME} ${ARG_SRC} ${MANIFEST})
setup_platform_linker_flags(${TARGET_NAME})
target_include_directories(${TARGET_NAME} PUBLIC "${TEST_INC}") target_include_directories(${TARGET_NAME} PUBLIC "${TEST_INC}")
target_include_directories(${TARGET_NAME} SYSTEM PUBLIC "${TEST_INC_SYS}") target_include_directories(${TARGET_NAME} SYSTEM PUBLIC "${TEST_INC_SYS}")
target_link_libraries(${TARGET_NAME} ${ARG_EXTRA_LIBS} ${PLATFORM_LINKLIBS}) target_link_libraries(${TARGET_NAME} ${ARG_EXTRA_LIBS} ${PLATFORM_LINKLIBS})

View File

@@ -514,33 +514,18 @@ function(SETUP_LIBDIRS)
endif() endif()
endfunction() endfunction()
macro(setup_platform_linker_flags) # Platform specific linker flags for targets.
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}") function(setup_platform_linker_flags
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} ${PLATFORM_LINKFLAGS_RELEASE}") target)
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}") set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS " ${PLATFORM_LINKFLAGS}")
endmacro() set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE " ${PLATFORM_LINKFLAGS_RELEASE}")
set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS_DEBUG " ${PLATFORM_LINKFLAGS_DEBUG}")
endfunction()
function(setup_liblinks # Platform specific libraries for targets.
function(setup_platform_linker_libs
target target
) )
# NOTE: This might look like it affects global scope, accumulating linker flags on every call
# to setup_liblinks, but this isn't how CMake works. These flags will only affect current
# directory from where the function is called.
# This means that setup_liblinks() called for ffmpeg_test will not affect blender, and each
# of thsoe targets will have single set of linker flags.
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}" PARENT_SCOPE)
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}" PARENT_SCOPE)
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} ${PLATFORM_LINKFLAGS_RELEASE}" PARENT_SCOPE)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}" PARENT_SCOPE)
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}" PARENT_SCOPE)
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} ${PLATFORM_LINKFLAGS_RELEASE}" PARENT_SCOPE)
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}" PARENT_SCOPE)
set(CMAKE_MODULE_LINKER_FLAGS_DEBUG "${CMAKE_MODULE_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}" PARENT_SCOPE)
set(CMAKE_MODULE_LINKER_FLAGS_RELEASE "${CMAKE_MODULE_LINKER_FLAGS_RELEASE} ${PLATFORM_LINKFLAGS_RELEASE}" PARENT_SCOPE)
# jemalloc must be early in the list, to be before pthread (see T57998) # jemalloc must be early in the list, to be before pthread (see T57998)
if(WITH_MEM_JEMALLOC) if(WITH_MEM_JEMALLOC)
target_link_libraries(${target} ${JEMALLOC_LIBRARIES}) target_link_libraries(${target} ${JEMALLOC_LIBRARIES})

View File

@@ -50,9 +50,6 @@ include_directories(${INC})
cycles_link_directories() cycles_link_directories()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}")
CYCLES_TEST(render_graph_finalize "${ALL_CYCLES_LIBRARIES};bf_intern_numaapi") CYCLES_TEST(render_graph_finalize "${ALL_CYCLES_LIBRARIES};bf_intern_numaapi")
cycles_target_link_libraries(cycles_render_graph_finalize_test) cycles_target_link_libraries(cycles_render_graph_finalize_test)
CYCLES_TEST(util_aligned_malloc "cycles_util") CYCLES_TEST(util_aligned_malloc "cycles_util")

View File

@@ -26,8 +26,5 @@ set(INC
setup_libdirs() setup_libdirs()
include_directories(${INC}) include_directories(${INC})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}")
BLENDER_TEST_PERFORMANCE(BLI_ghash_performance "bf_blenlib") BLENDER_TEST_PERFORMANCE(BLI_ghash_performance "bf_blenlib")
BLENDER_TEST_PERFORMANCE(BLI_task_performance "bf_blenlib") BLENDER_TEST_PERFORMANCE(BLI_task_performance "bf_blenlib")

View File

@@ -33,15 +33,12 @@ set(SRC
setup_libdirs() setup_libdirs()
add_cc_flags_custom_test(msgfmt) add_cc_flags_custom_test(msgfmt)
if(APPLE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}")
endif()
if(WIN32) if(WIN32)
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /nodefaultlib:MSVCRT.lib") set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /nodefaultlib:MSVCRT.lib")
endif() endif()
add_executable(msgfmt ${SRC}) add_executable(msgfmt ${SRC})
setup_platform_linker_flags(msgfmt)
target_link_libraries(msgfmt bf_blenlib) target_link_libraries(msgfmt bf_blenlib)
target_link_libraries(msgfmt bf_intern_guardedalloc) target_link_libraries(msgfmt bf_intern_guardedalloc)

View File

@@ -34,7 +34,7 @@ if(NOT WITH_HEADLESS)
set(SRC set(SRC
datatoc_icon.c datatoc_icon.c
) )
setup_platform_linker_flags() setup_platform_linker_flags(datatoc)
if(WIN32) if(WIN32)
include_directories( include_directories(
../blenlib ../blenlib

View File

@@ -56,9 +56,9 @@ endif()
# SRC_DNA_INC is defined in the parent dir # SRC_DNA_INC is defined in the parent dir
add_cc_flags_custom_test(makesdna) add_cc_flags_custom_test(makesdna)
setup_platform_linker_flags()
add_executable(makesdna ${SRC} ${SRC_DNA_INC}) add_executable(makesdna ${SRC} ${SRC_DNA_INC})
setup_platform_linker_flags(makesdna)
if(WIN32 AND NOT UNIX) if(WIN32 AND NOT UNIX)
target_link_libraries(makesdna ${PTHREADS_LIBRARIES}) target_link_libraries(makesdna ${PTHREADS_LIBRARIES})

View File

@@ -386,9 +386,9 @@ blender_include_dirs(
) )
add_cc_flags_custom_test(makesrna) add_cc_flags_custom_test(makesrna)
setup_platform_linker_flags()
add_executable(makesrna ${SRC} ${SRC_RNA_INC} ${SRC_DNA_INC}) add_executable(makesrna ${SRC} ${SRC_RNA_INC} ${SRC_DNA_INC})
setup_platform_linker_flags(makesrna)
target_link_libraries(makesrna bf_dna) target_link_libraries(makesrna bf_dna)
target_link_libraries(makesrna bf_dna_blenlib) target_link_libraries(makesrna bf_dna_blenlib)

View File

@@ -256,13 +256,7 @@ if(WITH_PYTHON_MODULE)
) )
if(APPLE) if(APPLE)
set_target_properties( set_target_properties(blender PROPERTIES MACOSX_BUNDLE TRUE)
blender
PROPERTIES
MACOSX_BUNDLE TRUE
LINK_FLAGS_RELEASE "${PLATFORM_LINKFLAGS}"
LINK_FLAGS_DEBUG "${PLATFORM_LINKFLAGS_DEBUG}"
)
endif() endif()
if(WIN32) if(WIN32)
@@ -1127,7 +1121,8 @@ add_dependencies(blender makesdna)
target_link_libraries(blender ${LIB}) target_link_libraries(blender ${LIB})
unset(LIB) unset(LIB)
setup_liblinks(blender) setup_platform_linker_flags(blender)
setup_platform_linker_libs(blender)
if(APPLE) if(APPLE)
set_target_properties(blender PROPERTIES LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/osx_locals.map) set_target_properties(blender PROPERTIES LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/osx_locals.map)
@@ -1162,9 +1157,9 @@ if(WIN32)
# CMAKE_BUILD_TYPE. This can be simplified by target_link_options and the $<CONFIG> # CMAKE_BUILD_TYPE. This can be simplified by target_link_options and the $<CONFIG>
# generator expression in newer cmake (2.13+) but until that time this fill have suffice. # generator expression in newer cmake (2.13+) but until that time this fill have suffice.
if(CMAKE_BUILD_TYPE) if(CMAKE_BUILD_TYPE)
set_property(TARGET blender APPEND PROPERTY LINK_FLAGS " /PDBSTRIPPED:${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/blender_public.pdb") set_property(TARGET blender APPEND_STRING PROPERTY LINK_FLAGS " /PDBSTRIPPED:${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/blender_public.pdb")
else() else()
set_property(TARGET blender APPEND PROPERTY LINK_FLAGS " /PDBSTRIPPED:${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/blender_public.pdb") set_property(TARGET blender APPEND_STRING PROPERTY LINK_FLAGS " /PDBSTRIPPED:${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/blender_public.pdb")
endif() endif()
endif() endif()
endif() endif()

View File

@@ -51,7 +51,7 @@ BLENDER_SRC_GTEST_EX(
EXTRA_LIBS "${TEST_LIBS}" EXTRA_LIBS "${TEST_LIBS}"
SKIP_ADD_TEST SKIP_ADD_TEST
) )
setup_liblinks(blender_test) setup_platform_linker_libs(blender_test)
if(WIN32) if(WIN32)
foreach(_lib ${_test_libs}) foreach(_lib ${_test_libs})