From eb01832c489f7a8e5abb7d23ff4640ef28fe29c0 Mon Sep 17 00:00:00 2001 From: reuk Date: Fri, 24 Apr 2020 21:13:21 +0100 Subject: [PATCH] CMake: Add targets for recommended flags --- CMakeLists.txt | 2 + examples/CMake/readme.md | 37 ++++++++ examples/CMakeLists.txt | 17 +++- examples/DemoRunner/CMakeLists.txt | 5 +- extras/AudioPerformanceTest/CMakeLists.txt | 6 +- extras/AudioPluginHost/CMakeLists.txt | 9 +- extras/BinaryBuilder/CMakeLists.txt | 6 +- extras/Build/CMake/JUCEUtils.cmake | 98 ++++++++++++++++++++-- extras/Build/juceaide/CMakeLists.txt | 6 +- extras/NetworkGraphicsDemo/CMakeLists.txt | 5 +- extras/Projucer/CMakeLists.txt | 5 +- extras/UnitTestRunner/CMakeLists.txt | 5 +- 12 files changed, 184 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0432eb1646..f99cbda18c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,8 @@ set_property(GLOBAL PROPERTY USE_FOLDERS YES) include(extras/Build/CMake/JUCEUtils.cmake) +juce_disable_default_flags() + set_directory_properties(PROPERTIES JUCE_COMPANY_NAME "JUCE" JUCE_COMPANY_WEBSITE "juce.com" diff --git a/examples/CMake/readme.md b/examples/CMake/readme.md index 1f3dc81f55..d542aebc0c 100644 --- a/examples/CMake/readme.md +++ b/examples/CMake/readme.md @@ -513,3 +513,40 @@ quick proof-of-concept demo apps with minimal set-up. For any use-case more comp proof-of-concept, you should prefer the `juce_add_gui_app`, `juce_add_plugin`, or `juce_add_console_app` functions, which provide more fine-grained control over the properties of your target. + +### `juce_disable_default_flags` + +``` +juce_disable_default_flags() +``` + +This function sets the `CMAKE__FLAGS_` to empty in the current directory and below, +allowing alternative optimisation/debug flags to be supplied without conflicting with the +CMake-supplied defaults. + +### `juce::juce_recommended_warning_flags + +``` +target_link_libraries(myTarget PRIVATE juce::juce_recommended_warning_flags) +``` + +This is a target which can be linked to other targets using `target_link_libraries`, in order to +enable the recommended JUCE warnings when building them. + +### `juce::juce_recommended_config_flags + +``` +target_link_libraries(myTarget PRIVATE juce::juce_recommended_config_flags) +``` + +This is a target which can be linked to other targets using `target_link_libraries`, in order to +enable the recommended JUCE optimisation and debug flags. + +### `juce::juce_recommended_lto_flags + +``` +target_link_libraries(myTarget PRIVATE juce::juce_recommended_lto_flags) +``` + +This is a target which can be linked to other targets using `target_link_libraries`, in order to +enable the recommended JUCE link time optimisation settings. diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index e6aee08c01..bd2f3ed61a 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -23,7 +23,22 @@ function(_juce_add_pips) "${CMAKE_CURRENT_SOURCE_DIR}/*.h") foreach(header IN ITEMS ${headers}) - juce_add_pip(${header}) + juce_add_pip(${header} added_target) + target_link_libraries(${added_target} PRIVATE + juce::juce_recommended_config_flags + juce::juce_recommended_lto_flags + juce::juce_recommended_warning_flags) + + get_target_property(active_targets ${added_target} JUCE_ACTIVE_PLUGIN_TARGETS) + + if(active_targets) + foreach(plugin_target IN LISTS active_targets) + target_link_libraries(${plugin_target} PRIVATE + juce::juce_recommended_config_flags + juce::juce_recommended_lto_flags + juce::juce_recommended_warning_flags) + endforeach() + endif() endforeach() endfunction() diff --git a/examples/DemoRunner/CMakeLists.txt b/examples/DemoRunner/CMakeLists.txt index 65833d043c..c8a3d463e2 100644 --- a/examples/DemoRunner/CMakeLists.txt +++ b/examples/DemoRunner/CMakeLists.txt @@ -57,6 +57,9 @@ target_link_libraries(DemoRunner PRIVATE juce::juce_opengl juce::juce_osc juce::juce_product_unlocking - juce::juce_video) + juce::juce_video + juce::juce_recommended_config_flags + juce::juce_recommended_lto_flags + juce::juce_recommended_warning_flags) juce_add_bundle_resources_directory(DemoRunner ../Assets) diff --git a/extras/AudioPerformanceTest/CMakeLists.txt b/extras/AudioPerformanceTest/CMakeLists.txt index e0a182f7a1..6dd642bb18 100644 --- a/extras/AudioPerformanceTest/CMakeLists.txt +++ b/extras/AudioPerformanceTest/CMakeLists.txt @@ -24,4 +24,8 @@ target_sources(AudioPerformanceTest PRIVATE target_compile_definitions(AudioPerformanceTest PRIVATE JUCE_USE_CURL=0 JUCE_WEB_BROWSER=0) -target_link_libraries(AudioPerformanceTest PRIVATE juce::juce_audio_utils) +target_link_libraries(AudioPerformanceTest PRIVATE + juce::juce_audio_utils + juce::juce_recommended_config_flags + juce::juce_recommended_lto_flags + juce::juce_recommended_warning_flags) diff --git a/extras/AudioPluginHost/CMakeLists.txt b/extras/AudioPluginHost/CMakeLists.txt index 95f92b74b8..88defdffc8 100644 --- a/extras/AudioPluginHost/CMakeLists.txt +++ b/extras/AudioPluginHost/CMakeLists.txt @@ -51,8 +51,9 @@ target_link_libraries(AudioPluginHost PRIVATE juce::juce_cryptography juce::juce_dsp juce::juce_opengl - juce::juce_video) + juce::juce_video + juce::juce_recommended_config_flags + juce::juce_recommended_lto_flags + juce::juce_recommended_warning_flags) -if(CMAKE_SYSTEM_NAME STREQUAL "iOS") - juce_add_bundle_resources_directory(AudioPluginHost ../../examples/Assets) -endif() +juce_add_bundle_resources_directory(AudioPluginHost ../../examples/Assets) diff --git a/extras/BinaryBuilder/CMakeLists.txt b/extras/BinaryBuilder/CMakeLists.txt index d434295963..9b917d8bdf 100644 --- a/extras/BinaryBuilder/CMakeLists.txt +++ b/extras/BinaryBuilder/CMakeLists.txt @@ -22,4 +22,8 @@ target_sources(BinaryBuilder PRIVATE Source/Main.cpp) target_compile_definitions(BinaryBuilder PRIVATE JUCE_USE_CURL=0) -target_link_libraries(BinaryBuilder PRIVATE juce::juce_core) +target_link_libraries(BinaryBuilder PRIVATE + juce::juce_core + juce::juce_recommended_config_flags + juce::juce_recommended_lto_flags + juce::juce_recommended_warning_flags) diff --git a/extras/Build/CMake/JUCEUtils.cmake b/extras/Build/CMake/JUCEUtils.cmake index f7de9ca158..fd12e5d3b4 100644 --- a/extras/Build/CMake/JUCEUtils.cmake +++ b/extras/Build/CMake/JUCEUtils.cmake @@ -1353,7 +1353,7 @@ function(_juce_get_iaa_type_code target out_var) endfunction() function(_juce_configure_plugin_targets target) - if(${CMAKE_VERSION} VERSION_LESS "3.15.0") + if(CMAKE_VERSION VERSION_LESS "3.15.0") message(FATAL_ERROR "Plugin targets require CMake 3.15 or higher") endif() @@ -1466,7 +1466,7 @@ function(_juce_configure_plugin_targets target) endif() if(TARGET ${target}_AUv3) - target_link_options(${target}_AUv3 PUBLIC -fapplication-extension -e _NSExtensionMain) + target_link_libraries(${target}_AUv3 PUBLIC -fapplication-extension "-e _NSExtensionMain") endif() if((TARGET ${target}_AUv3) AND (TARGET ${target}_Standalone)) @@ -1480,7 +1480,7 @@ endfunction() # ================================================================================================== -function(_juce_set_property_if_not_set target property) +function(_juce_set_generic_property_if_not_set target property) list(LENGTH ARGN num_extra_args) if(num_extra_args EQUAL 0) @@ -1488,13 +1488,17 @@ function(_juce_set_property_if_not_set target property) endif() set(existing_property) - get_target_property(existing_property ${target} JUCE_${property}) + get_target_property(existing_property ${target} ${property}) if(${existing_property} STREQUAL "existing_property-NOTFOUND") - set_target_properties(${target} PROPERTIES JUCE_${property} "${ARGN}") + set_target_properties(${target} PROPERTIES ${property} "${ARGN}") endif() endfunction() +function(_juce_set_property_if_not_set target property) + _juce_set_generic_property_if_not_set(${target} JUCE_${property} ${ARGN}) +endfunction() + function(_juce_make_valid_4cc out_var) string(RANDOM LENGTH 1 ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZ" head) string(RANDOM LENGTH 3 ALPHABET "abcdefghijklmnopqrstuvwxyz" tail) @@ -1958,6 +1962,10 @@ function(juce_add_pip header) message(FATAL_ERROR "PIP kind must be either AudioProcessor, Component, or Console") endif() + if(NOT ARGV1 STREQUAL "") + set("${ARGV1}" "${JUCE_PIP_NAME}" PARENT_SCOPE) + endif() + # Generate Main.cpp _juce_get_metadata("${metadata_dict}" mainClass JUCE_PIP_MAIN_CLASS) get_target_property(juce_library_code ${JUCE_PIP_NAME} JUCE_GENERATED_SOURCES_DIRECTORY) @@ -2082,3 +2090,83 @@ function(juce_set_vst2_sdk_path path) $ "${path}") endfunction() + +# ================================================================================================== + +function(juce_disable_default_flags) + set(langs C CXX) + set(modes DEBUG RELEASE RELWITHDEBINFO MINSIZEREL) + + foreach(lang IN LISTS langs) + foreach(mode IN LISTS modes) + list(FILTER CMAKE_${lang}_FLAGS_${mode} INCLUDE REGEX "[/-]M[TD]d?") + set(CMAKE_${lang}_FLAGS_${mode} "${CMAKE_${lang}_FLAGS_${mode}}" PARENT_SCOPE) + endforeach() + endforeach() +endfunction() + +# ================================================================================================== + +add_library(juce_recommended_warning_flags INTERFACE) +add_library(juce::juce_recommended_warning_flags ALIAS juce_recommended_warning_flags) + +if((CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") OR (CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")) + target_compile_options(juce_recommended_warning_flags INTERFACE "/W4") +elseif((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") OR (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")) + target_compile_options(juce_recommended_warning_flags INTERFACE + -Wall -Wshadow-all -Wshorten-64-to-32 -Wstrict-aliasing -Wuninitialized + -Wunused-parameter -Wconversion -Wsign-compare -Wint-conversion + -Wconditional-uninitialized -Woverloaded-virtual -Wreorder + -Wconstant-conversion -Wsign-conversion -Wunused-private-field + -Wbool-conversion -Wextra-semi -Wunreachable-code + -Wzero-as-null-pointer-constant -Wcast-align + -Winconsistent-missing-destructor-override -Wshift-sign-overflow + -Wnullable-to-nonnull-conversion -Wno-missing-field-initializers + -Wno-ignored-qualifiers -Wswitch-enum) +elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + target_compile_options(juce_recommended_warning_flags INTERFACE + -Wall -Wextra -Wstrict-aliasing -Wuninitialized -Wunused-parameter + -Wsign-compare -Woverloaded-virtual -Wreorder -Wsign-conversion + -Wunreachable-code -Wzero-as-null-pointer-constant -Wcast-align + -Wno-implicit-fallthrough -Wno-maybe-uninitialized + -Wno-missing-field-initializers -Wno-ignored-qualifiers -Wswitch-enum + -Wswitch-default -Wredundant-decls) + + if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "7.0.0") + target_compile_options(juce_recommended_warning_flags INTERFACE "-Wno-strict-overflow") + endif() +endif() + +install(TARGETS juce_recommended_warning_flags EXPORT JUCE) + +# ================================================================================================== + +add_library(juce_recommended_config_flags INTERFACE) +add_library(juce::juce_recommended_config_flags ALIAS juce_recommended_config_flags) + +if((CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") OR (CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")) + target_compile_options(juce_recommended_config_flags INTERFACE + $<$:/Od /MP /EHsc> + $<$:/Ox /MP /EHsc>) +elseif((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + OR (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") + OR (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")) + target_compile_options(juce_recommended_config_flags INTERFACE + $<$:-g -O0> + $<$:-O3>) +endif() + +# ================================================================================================== + +add_library(juce_recommended_lto_flags INTERFACE) +add_library(juce::juce_recommended_lto_flags ALIAS juce_recommended_lto_flags) + +if((CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") OR (CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")) + target_compile_options(juce_recommended_lto_flags INTERFACE $<$:/GL>) + target_link_libraries(juce_recommended_lto_flags INTERFACE $<$:-LTCG>) +elseif((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + OR (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") + OR (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")) + target_compile_options(juce_recommended_lto_flags INTERFACE $<$:-flto>) + target_link_libraries(juce_recommended_lto_flags INTERFACE $<$:-flto>) +endif() diff --git a/extras/Build/juceaide/CMakeLists.txt b/extras/Build/juceaide/CMakeLists.txt index d11da3d93e..7dfa3aea6e 100644 --- a/extras/Build/juceaide/CMakeLists.txt +++ b/extras/Build/juceaide/CMakeLists.txt @@ -29,7 +29,11 @@ if(JUCE_BUILD_HELPER_TOOLS) JUCE_DISABLE_JUCE_VERSION_PRINTING=1 JUCE_USE_CURL=0) - target_link_libraries(juceaide PRIVATE juce::juce_build_tools) + target_link_libraries(juceaide PRIVATE + juce::juce_build_tools + juce::juce_recommended_config_flags + juce::juce_recommended_lto_flags + juce::juce_recommended_warning_flags) export(TARGETS juceaide NAMESPACE juce_tools:: diff --git a/extras/NetworkGraphicsDemo/CMakeLists.txt b/extras/NetworkGraphicsDemo/CMakeLists.txt index be1b297200..580dd1ad75 100644 --- a/extras/NetworkGraphicsDemo/CMakeLists.txt +++ b/extras/NetworkGraphicsDemo/CMakeLists.txt @@ -27,4 +27,7 @@ target_link_libraries(NetworkGraphicsDemo PRIVATE juce::juce_audio_utils juce::juce_cryptography juce::juce_opengl - juce::juce_osc) + juce::juce_osc + juce::juce_recommended_config_flags + juce::juce_recommended_lto_flags + juce::juce_recommended_warning_flags) diff --git a/extras/Projucer/CMakeLists.txt b/extras/Projucer/CMakeLists.txt index c71d1fb856..d733ad21bd 100644 --- a/extras/Projucer/CMakeLists.txt +++ b/extras/Projucer/CMakeLists.txt @@ -159,4 +159,7 @@ target_link_libraries(Projucer PRIVATE ProjucerData juce::juce_build_tools juce::juce_cryptography - juce::juce_gui_extra) + juce::juce_gui_extra + juce::juce_recommended_config_flags + juce::juce_recommended_lto_flags + juce::juce_recommended_warning_flags) diff --git a/extras/UnitTestRunner/CMakeLists.txt b/extras/UnitTestRunner/CMakeLists.txt index 8bf122acf3..1e144969ba 100644 --- a/extras/UnitTestRunner/CMakeLists.txt +++ b/extras/UnitTestRunner/CMakeLists.txt @@ -33,4 +33,7 @@ target_link_libraries(UnitTestRunner PRIVATE juce::juce_opengl juce::juce_osc juce::juce_product_unlocking - juce::juce_video) + juce::juce_video + juce::juce_recommended_config_flags + juce::juce_recommended_lto_flags + juce::juce_recommended_warning_flags)