From f4367b3feea2200132fe41ea9c3acc7992449e55 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Sat, 9 Apr 2022 16:28:52 +0200 Subject: [PATCH] CMake: Use target_precompile_headers when available Keep using cotire for CMake versions older than 3.16, or when user specifies wxBUILD_PRECOMP=COTIRE. The scintilla headers need to be specified for target_precompile_headers to work. Enable it when the target has at least 2 source files (same as cotire does). --- build/cmake/functions.cmake | 24 ++++++++++++++++-------- build/cmake/init.cmake | 4 ++-- build/cmake/lib/stc/CMakeLists.txt | 12 +----------- build/cmake/options.cmake | 2 +- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/build/cmake/functions.cmake b/build/cmake/functions.cmake index bb67ce841f..cd01b6dc69 100644 --- a/build/cmake/functions.cmake +++ b/build/cmake/functions.cmake @@ -15,7 +15,9 @@ if(CMAKE_GENERATOR STREQUAL "Xcode") # include Obj-C files when using precompiled headers with Xcode set(COTIRE_UNITY_SOURCE_EXCLUDE_EXTENSIONS "" CACHE STRING "wxWidgets override of cotire exclude") endif() -include(cotire) # For precompiled header handling +if((wxBUILD_PRECOMP STREQUAL "ON" AND CMAKE_VERSION VERSION_LESS "3.16") OR (wxBUILD_PRECOMP STREQUAL "COTIRE")) + include(cotire) # For precompiled header handling +endif() include(CMakePrintHelpers) # Use the MSVC/makefile naming convention, or the configure naming convention, @@ -436,16 +438,22 @@ macro(wx_add_library name) endif() endmacro() -# Enable cotire for target, use optional second argument for prec. header -macro(wx_target_enable_precomp target_name) +# Enable precompiled headers for target +macro(wx_target_enable_precomp target_name prec_header) if(wxBUILD_PRECOMP) target_compile_definitions(${target_name} PRIVATE WX_PRECOMP) - if(${ARGC} GREATER 1 AND NOT ${ARGV1} STREQUAL "") - set_target_properties(${target_name} PROPERTIES - COTIRE_CXX_PREFIX_HEADER_INIT ${ARGV1}) + if(CMAKE_VERSION VERSION_LESS "3.16" OR wxBUILD_PRECOMP STREQUAL "COTIRE") + set_target_properties(${target_name} PROPERTIES COTIRE_CXX_PREFIX_HEADER_INIT ${prec_header}) + set_target_properties(${target_name} PROPERTIES COTIRE_ADD_UNITY_BUILD FALSE) + cotire(${target_name}) + else() + get_target_property(target_source_files ${target_name} SOURCES) + list(FILTER target_source_files INCLUDE REGEX ".*(.cpp|.cxx)$") + list(LENGTH target_source_files target_source_count) + if(target_source_count GREATER_EQUAL 2) + target_precompile_headers(${target_name} PRIVATE "$<$:${prec_header}>") + endif() endif() - set_target_properties(${target_name} PROPERTIES COTIRE_ADD_UNITY_BUILD FALSE) - cotire(${target_name}) elseif(MSVC) target_compile_definitions(${target_name} PRIVATE NOPCH) endif() diff --git a/build/cmake/init.cmake b/build/cmake/init.cmake index b5175236e2..19449d9d37 100644 --- a/build/cmake/init.cmake +++ b/build/cmake/init.cmake @@ -582,7 +582,7 @@ if(DEFINED wxBUILD_PRECOMP_PREV AND NOT wxBUILD_PRECOMP STREQUAL wxBUILD_PRECOMP endif() set(wxBUILD_PRECOMP_PREV ${wxBUILD_PRECOMP} CACHE INTERNAL "") -if(wxBUILD_PRECOMP) +if((wxBUILD_PRECOMP STREQUAL "ON" AND CMAKE_VERSION VERSION_LESS "3.16") OR (wxBUILD_PRECOMP STREQUAL "COTIRE")) if(DEFINED CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED) set(try_flags "-DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED=${CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED}") endif() @@ -614,4 +614,4 @@ if(wxBUILD_PRECOMP) message(WARNING "precompiled header (PCH) test failed, it will be turned off") wx_option_force_value(wxBUILD_PRECOMP OFF) endif() -endif(wxBUILD_PRECOMP) +endif() diff --git a/build/cmake/lib/stc/CMakeLists.txt b/build/cmake/lib/stc/CMakeLists.txt index 02e50c9758..7d3b39c205 100644 --- a/build/cmake/lib/stc/CMakeLists.txt +++ b/build/cmake/lib/stc/CMakeLists.txt @@ -171,17 +171,7 @@ target_compile_definitions(wxscintilla PRIVATE LINK_LEXERS ) -if(wxBUILD_PRECOMP) - # The auto-generated header causes undefined members and identifiers in the - # standard c++ headers when using clang. - # Do not disable precompiled headers entirely but use the main Scintilla - # header as prefix header so there is at least a small speedup. - if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") - set(wxSCINTILLA_PREC_HEADER "${wxSOURCE_DIR}/src/stc/scintilla/include/Scintilla.h") - endif() - wx_target_enable_precomp(wxscintilla ${wxSCINTILLA_PREC_HEADER}) -endif() - +wx_target_enable_precomp(wxscintilla "${wxSOURCE_DIR}/src/stc/scintilla/include/Scintilla.h") wx_add_library(wxstc ${STC_FILES}) wx_lib_include_directories(wxstc ${wxSOURCE_DIR}/src/stc/scintilla/include diff --git a/build/cmake/options.cmake b/build/cmake/options.cmake index 70193f755d..842d057cbc 100644 --- a/build/cmake/options.cmake +++ b/build/cmake/options.cmake @@ -16,7 +16,7 @@ wx_option(wxBUILD_TESTS "Build console tests (CONSOLE_ONLY) or ALL" OFF STRINGS CONSOLE_ONLY ALL OFF) wx_option(wxBUILD_DEMOS "Build demos" OFF) wx_option(wxBUILD_BENCHMARKS "Build benchmarks" OFF) -wx_option(wxBUILD_PRECOMP "Use precompiled headers") +wx_option(wxBUILD_PRECOMP "Use precompiled headers" ON STRINGS ON OFF COTIRE) mark_as_advanced(wxBUILD_PRECOMP) wx_option(wxBUILD_INSTALL "Create install/uninstall target for wxWidgets") wx_option(wxBUILD_COMPATIBILITY