CMake: per-target CFLAG & CXXFLAG support
authorCampbell Barton <ideasman42@gmail.com>
Thu, 14 Jul 2016 09:11:33 +0000 (19:11 +1000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 14 Jul 2016 09:17:34 +0000 (19:17 +1000)
Applying cflags globally can be problematic especially with extern, intern libs.

Now flags from target named will be used when defined,
allowing for developers to define flags for modules they maintain.

Convention is CMAKE_CFLAGS_${UPPERCASE_TARGET_NAME}, (CXXFLAGS for C++).

eg: CMAKE_CFLAGS_BF_BLENDER, CMAKE_CFLAGS_MAKESDNA, CMAKE_CXXFLAGS_CYCLES_KERNEL

On Linux run `make help` for full list of names, MSVC shows these in the solution.

build_files/cmake/macros.cmake
source/blender/makesdna/intern/CMakeLists.txt
source/blender/makesrna/intern/CMakeLists.txt
source/blenderplayer/CMakeLists.txt
source/creator/CMakeLists.txt

index d29f086069aff863f3aa57ac7bca2c43c93d8526..5ffd6c4e7f4089d41c715e0382cf397cdf19e591 100644 (file)
@@ -196,8 +196,33 @@ function(blender_source_group
 endfunction()
 
 
+# Support per-target CMake flags
+# Read from: CMAKE_C_FLAGS_**** (made upper case) when set.
+#
+# 'name' should alway match the target name,
+# use this macro before add_library or add_executable.
+#
+# Optionally takes an arg passed to set(), eg PARENT_SCOPE.
+macro(add_cc_flags_custom_test
+       name
+       )
+
+       string(TOUPPER ${name} _name_upper)
+       if(DEFINED CMAKE_C_FLAGS_${_name_upper})
+               message(STATUS "Using custom CFLAGS: CMAKE_C_FLAGS_${_name_upper} in \"${CMAKE_CURRENT_SOURCE_DIR}\"")
+               set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${_name_upper}}" ${ARGV1})
+       endif()
+       if(DEFINED CMAKE_CXX_FLAGS_${_name_upper})
+               message(STATUS "Using custom CXXFLAGS: CMAKE_CXX_FLAGS_${_name_upper} in \"${CMAKE_CURRENT_SOURCE_DIR}\"")
+               set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${_name_upper}}" ${ARGV1})
+       endif()
+       unset(_name_upper)
+
+endmacro()
+
+
 # only MSVC uses SOURCE_GROUP
-function(blender_add_lib_nolist
+function(blender_add_lib__impl
        name
        sources
        includes
@@ -225,6 +250,18 @@ function(blender_add_lib_nolist
 endfunction()
 
 
+function(blender_add_lib_nolist
+       name
+       sources
+       includes
+       includes_sys
+       )
+
+       add_cc_flags_custom_test(${name} PARENT_SCOPE)
+
+       blender_add_lib__impl(${name} "${sources}" "${includes}" "${includes_sys}")
+endfunction()
+
 function(blender_add_lib
        name
        sources
@@ -232,7 +269,9 @@ function(blender_add_lib
        includes_sys
        )
 
-       blender_add_lib_nolist(${name} "${sources}" "${includes}" "${includes_sys}")
+       add_cc_flags_custom_test(${name} PARENT_SCOPE)
+
+       blender_add_lib__impl(${name} "${sources}" "${includes}" "${includes_sys}")
 
        set_property(GLOBAL APPEND PROPERTY BLENDER_LINK_LIBS ${name})
 endfunction()
index 0bb6e866bf4a0345973f933db43e3097d1deaa48..8c758c33dc5d5ef7aeb860e1ba6a36323aae6784 100644 (file)
@@ -52,6 +52,8 @@ endif()
 
 # SRC_DNA_INC is defined in the parent dir
 
+add_cc_flags_custom_test(makesdna)
+
 add_executable(makesdna ${SRC} ${SRC_DNA_INC})
 
 # Output dna.c
index 31bf0c9389bfbd661f45d4efdb7490e0c151110d..7bfac9d0605f3bc8ccecaa95ef61bd1d3049dc7c 100644 (file)
@@ -350,7 +350,10 @@ blender_include_dirs_sys(
        "${GLEW_INCLUDE_PATH}"
 )
 
+add_cc_flags_custom_test(makesrna)
+
 add_executable(makesrna ${SRC} ${SRC_RNA_INC} ${SRC_DNA_INC})
+
 target_link_libraries(makesrna bf_dna)
 target_link_libraries(makesrna bf_dna_blenlib)
 
index 206007b8d8b8e93b67178f184fe13ad0fe45142d..ca841954b16c162d8683195e54f1efe226434924 100644 (file)
@@ -63,6 +63,8 @@ if(WIN32 AND NOT UNIX)
                        COMPONENT Blenderplayer
                        DESTINATION ".")
 
+add_cc_flags_custom_test(blenderplayer)
+
 elseif(APPLE)
        add_executable(blenderplayer ${EXETYPE} bad_level_call_stubs/stubs.c)
        # setup Info.plist
index 4e8dd6f9fd3536b79d92ffe13a6364b084de4a26..d8435284096bb7f3f9999f9cdda18e099ad9a2b6 100644 (file)
@@ -222,6 +222,8 @@ if(WITH_BUILDINFO)
        add_dependencies(buildinfoobj buildinfo)
 endif()
 
+add_cc_flags_custom_test(blender)
+
 # message(STATUS "Configuring blender")
 if(WITH_PYTHON_MODULE)
        add_definitions(-DWITH_PYTHON_MODULE)