Fix monolithic build in cmake (wxBUILD_MONOLITHIC)

- Functions that set wxMONO_* vars need to set them in parent scope, from macros instead of functions (functions would need grandparent scope)
- Fix a conflict with xml library linking Mono which made core include the wrong png headers
- Fix setting of NOPCH on windows to go through the wx_lib_compile_definitions instead of target_compile_definitions so it carries to monolithic build
- Allow use of precompiled header in MSVC via calling wx_finalize_lib(mono)
- Introduce wxMONO_NONCOMPILED_CPP_FILES to deal with set_source_files_properties only applying within the current CMakeLists file
- Fix demos building against monolithic library by using wx_exe_link_libraries instead of target_link_libraries (similar to what the tests already do)

Closes #18074
This commit is contained in:
Jeff Bland
2018-05-18 14:01:02 -06:00
parent ffb44a2f38
commit fbf319641e
3 changed files with 70 additions and 47 deletions

View File

@@ -35,7 +35,7 @@ function(wx_add_demo name)
if(wxBUILD_SHARED) if(wxBUILD_SHARED)
target_compile_definitions(${DEMO_NAME} PRIVATE WXUSINGDLL) target_compile_definitions(${DEMO_NAME} PRIVATE WXUSINGDLL)
endif() endif()
target_link_libraries(${DEMO_NAME} core ${DEMO_LIBRARIES}) wx_exe_link_libraries(${DEMO_NAME} core ${DEMO_LIBRARIES})
wx_set_common_target_properties(${DEMO_NAME}) wx_set_common_target_properties(${DEMO_NAME})
set_target_properties(${DEMO_NAME} PROPERTIES FOLDER "Demos") set_target_properties(${DEMO_NAME} PROPERTIES FOLDER "Demos")
set_target_properties(${DEMO_NAME} PROPERTIES set_target_properties(${DEMO_NAME} PROPERTIES

View File

@@ -20,19 +20,24 @@ include(CMakePrintHelpers)
# This function adds a list of headers to a variable while prepending # This function adds a list of headers to a variable while prepending
# include/ to the path # include/ to the path
function(wx_add_headers src_var) macro(wx_add_headers src_var)
set(headers) set(headers)
list(REMOVE_AT ARGV 0) foreach(header ${ARGN})
foreach(header ${ARGV})
list(APPEND headers ${wxSOURCE_DIR}/include/${header}) list(APPEND headers ${wxSOURCE_DIR}/include/${header})
if(header MATCHES "\\.cpp$") if(header MATCHES "\\.cpp$")
# .cpp files in include directory should not be compiled # .cpp files in include directory should not be compiled
set_source_files_properties(${wxSOURCE_DIR}/include/${header} if (wxBUILD_MONOLITHIC)
PROPERTIES HEADER_FILE_ONLY TRUE) # set_source_files_properties only works within the same CMakeLists.txt
list(APPEND wxMONO_NONCOMPILED_CPP_FILES ${wxSOURCE_DIR}/include/${header})
set(wxMONO_NONCOMPILED_CPP_FILES ${wxMONO_NONCOMPILED_CPP_FILES} PARENT_SCOPE)
else()
set_source_files_properties(${wxSOURCE_DIR}/include/${header}
PROPERTIES HEADER_FILE_ONLY TRUE)
endif()
endif() endif()
endforeach() endforeach()
set(${src_var} ${${src_var}} ${headers} PARENT_SCOPE) list(APPEND ${src_var} ${headers})
endfunction() endmacro()
# Add sources from a ..._SRC variable and headers from a ..._HDR # Add sources from a ..._SRC variable and headers from a ..._HDR
macro(wx_append_sources src_var source_base_name) macro(wx_append_sources src_var source_base_name)
@@ -269,45 +274,45 @@ endfunction()
# first parameter is the name of the library # first parameter is the name of the library
# if the second parameter is set to IS_BASE a non UI lib is created # if the second parameter is set to IS_BASE a non UI lib is created
# all additional parameters are source files for the library # all additional parameters are source files for the library
function(wx_add_library name) macro(wx_add_library name)
cmake_parse_arguments(wxADD_LIBRARY "IS_BASE" "" "" ${ARGN}) cmake_parse_arguments(wxADD_LIBRARY "IS_BASE" "" "" ${ARGN})
set(src_files ${wxADD_LIBRARY_UNPARSED_ARGUMENTS}) set(src_files ${wxADD_LIBRARY_UNPARSED_ARGUMENTS})
if(wxBUILD_MONOLITHIC AND NOT name STREQUAL "mono") if(wxBUILD_MONOLITHIC AND NOT ${name} STREQUAL "mono")
# collect all source files for mono library # collect all source files for mono library
set(wxMONO_SRC_FILES ${wxMONO_SRC_FILES} ${src_files} PARENT_SCOPE) set(wxMONO_SRC_FILES ${wxMONO_SRC_FILES} ${src_files} PARENT_SCOPE)
return()
endif()
if(wxBUILD_PRECOMP AND MSVC)
# Add dummy source file to be used by cotire for PCH creation
list(INSERT src_files 0 "${wxSOURCE_DIR}/src/common/dummy.cpp")
endif()
list(APPEND src_files ${wxSETUP_HEADER_FILE})
if(wxBUILD_SHARED)
set(wxBUILD_LIB_TYPE SHARED)
if(WIN32)
# Add WIN32 version information
list(APPEND src_files "${wxSOURCE_DIR}/src/msw/version.rc" "${wxSOURCE_DIR}/include/wx/msw/genrcdefs.h")
endif()
else() else()
set(wxBUILD_LIB_TYPE STATIC)
if(wxBUILD_PRECOMP AND MSVC)
# Add dummy source file to be used by cotire for PCH creation
list(INSERT src_files 0 "${wxSOURCE_DIR}/src/common/dummy.cpp")
endif()
list(APPEND src_files ${wxSETUP_HEADER_FILE})
if(wxBUILD_SHARED)
set(wxBUILD_LIB_TYPE SHARED)
if(WIN32)
# Add WIN32 version information
list(APPEND src_files "${wxSOURCE_DIR}/src/msw/version.rc" "${wxSOURCE_DIR}/include/wx/msw/genrcdefs.h")
endif()
else()
set(wxBUILD_LIB_TYPE STATIC)
endif()
add_library(${name} ${wxBUILD_LIB_TYPE} ${src_files})
wx_set_target_properties(${name} ${wxADD_LIBRARY_IS_BASE})
# Setup install
wx_install(TARGETS ${name}
LIBRARY DESTINATION "lib${wxPLATFORM_LIB_DIR}"
ARCHIVE DESTINATION "lib${wxPLATFORM_LIB_DIR}"
RUNTIME DESTINATION "lib${wxPLATFORM_LIB_DIR}"
BUNDLE DESTINATION Applications/wxWidgets
)
list(APPEND wxLIB_TARGETS ${name})
endif() endif()
endmacro()
add_library(${name} ${wxBUILD_LIB_TYPE} ${src_files})
wx_set_target_properties(${name} ${wxADD_LIBRARY_IS_BASE})
# Setup install
wx_install(TARGETS ${name}
LIBRARY DESTINATION "lib${wxPLATFORM_LIB_DIR}"
ARCHIVE DESTINATION "lib${wxPLATFORM_LIB_DIR}"
RUNTIME DESTINATION "lib${wxPLATFORM_LIB_DIR}"
BUNDLE DESTINATION Applications/wxWidgets
)
set(wxLIB_TARGETS ${wxLIB_TARGETS} ${name} PARENT_SCOPE)
endfunction()
# Enable cotire for target if precompiled headers are enabled # Enable cotire for target if precompiled headers are enabled
macro(wx_target_enable_precomp target_name) macro(wx_target_enable_precomp target_name)
@@ -336,13 +341,15 @@ endmacro()
# Enable precompiled headers for wx libraries # Enable precompiled headers for wx libraries
macro(wx_finalize_lib target_name) macro(wx_finalize_lib target_name)
set(wxLIB_TARGETS ${wxLIB_TARGETS} PARENT_SCOPE) set(wxLIB_TARGETS ${wxLIB_TARGETS} PARENT_SCOPE)
if(wxBUILD_PRECOMP AND TARGET ${target_name}) if(wxBUILD_PRECOMP)
target_compile_definitions(${target_name} PRIVATE WX_PRECOMP) if(TARGET ${target_name})
set_target_properties(${target_name} PROPERTIES target_compile_definitions(${target_name} PRIVATE WX_PRECOMP)
COTIRE_CXX_PREFIX_HEADER_INIT "${wxSOURCE_DIR}/include/wx/wxprec.h") set_target_properties(${target_name} PROPERTIES
wx_target_enable_precomp(${target_name}) COTIRE_CXX_PREFIX_HEADER_INIT "${wxSOURCE_DIR}/include/wx/wxprec.h")
wx_target_enable_precomp(${target_name})
endif()
elseif(MSVC) elseif(MSVC)
target_compile_definitions(${target_name} PRIVATE NOPCH) wx_lib_compile_definitions(${target_name} PRIVATE NOPCH)
endif() endif()
endmacro() endmacro()
@@ -354,6 +361,8 @@ macro(wx_lib_link_libraries name)
cmake_parse_arguments(_LIB_LINK "" "" "PUBLIC;PRIVATE" ${ARGN}) cmake_parse_arguments(_LIB_LINK "" "" "PUBLIC;PRIVATE" ${ARGN})
list(APPEND wxMONO_LIBS_PUBLIC ${_LIB_LINK_PUBLIC}) list(APPEND wxMONO_LIBS_PUBLIC ${_LIB_LINK_PUBLIC})
list(APPEND wxMONO_LIBS_PRIVATE ${_LIB_LINK_PRIVATE}) list(APPEND wxMONO_LIBS_PRIVATE ${_LIB_LINK_PRIVATE})
set(wxMONO_LIBS_PUBLIC ${wxMONO_LIBS_PUBLIC} PARENT_SCOPE)
set(wxMONO_LIBS_PRIVATE ${wxMONO_LIBS_PRIVATE} PARENT_SCOPE)
else() else()
target_link_libraries(${name};${ARGN}) target_link_libraries(${name};${ARGN})
endif() endif()
@@ -377,6 +386,8 @@ macro(wx_lib_include_directories name)
cmake_parse_arguments(_LIB_INCLUDE_DIRS "" "" "PUBLIC;PRIVATE" ${ARGN}) cmake_parse_arguments(_LIB_INCLUDE_DIRS "" "" "PUBLIC;PRIVATE" ${ARGN})
list(APPEND wxMONO_INCLUDE_DIRS_PUBLIC ${_LIB_INCLUDE_DIRS_PUBLIC}) list(APPEND wxMONO_INCLUDE_DIRS_PUBLIC ${_LIB_INCLUDE_DIRS_PUBLIC})
list(APPEND wxMONO_INCLUDE_DIRS_PRIVATE ${_LIB_INCLUDE_DIRS_PRIVATE}) list(APPEND wxMONO_INCLUDE_DIRS_PRIVATE ${_LIB_INCLUDE_DIRS_PRIVATE})
set(wxMONO_INCLUDE_DIRS_PUBLIC ${wxMONO_INCLUDE_DIRS_PUBLIC} PARENT_SCOPE)
set(wxMONO_INCLUDE_DIRS_PRIVATE ${wxMONO_INCLUDE_DIRS_PRIVATE} PARENT_SCOPE)
else() else()
target_include_directories(${name};BEFORE;${ARGN}) target_include_directories(${name};BEFORE;${ARGN})
endif() endif()
@@ -390,6 +401,8 @@ macro(wx_lib_compile_definitions name)
cmake_parse_arguments(_LIB_DEFINITIONS "" "" "PUBLIC;PRIVATE" ${ARGN}) cmake_parse_arguments(_LIB_DEFINITIONS "" "" "PUBLIC;PRIVATE" ${ARGN})
list(APPEND wxMONO_DEFINITIONS_PUBLIC ${_LIB_DEFINITIONS_PUBLIC}) list(APPEND wxMONO_DEFINITIONS_PUBLIC ${_LIB_DEFINITIONS_PUBLIC})
list(APPEND wxMONO_DEFINITIONS_PRIVATE ${_LIB_DEFINITIONS_PRIVATE}) list(APPEND wxMONO_DEFINITIONS_PRIVATE ${_LIB_DEFINITIONS_PRIVATE})
set(wxMONO_DEFINITIONS_PUBLIC ${wxMONO_DEFINITIONS_PUBLIC} PARENT_SCOPE)
set(wxMONO_DEFINITIONS_PRIVATE ${wxMONO_DEFINITIONS_PRIVATE} PARENT_SCOPE)
else() else()
target_compile_definitions(${name};${ARGN}) target_compile_definitions(${name};${ARGN})
endif() endif()

View File

@@ -17,6 +17,7 @@ if(wxBUILD_MONOLITHIC)
set(wxMONO_LIBS_PUBLIC) set(wxMONO_LIBS_PUBLIC)
set(wxMONO_INCLUDE_DIRS_PRIVATE) set(wxMONO_INCLUDE_DIRS_PRIVATE)
set(wxMONO_INCLUDE_DIRS_PUBLIC) set(wxMONO_INCLUDE_DIRS_PUBLIC)
set(wxMONO_NONCOMPILED_CPP_FILES)
endif() endif()
# Define third party libraries # Define third party libraries
@@ -39,7 +40,6 @@ endmacro()
# Define base libraries # Define base libraries
set(LIBS base) set(LIBS base)
add_opt_lib(net wxUSE_SOCKETS) add_opt_lib(net wxUSE_SOCKETS)
add_opt_lib(xml wxUSE_XML)
# Define UI libraries # Define UI libraries
if(wxUSE_GUI) if(wxUSE_GUI)
@@ -62,6 +62,12 @@ if(wxUSE_GUI)
add_opt_lib(qa wxUSE_DEBUGREPORT) add_opt_lib(qa wxUSE_DEBUGREPORT)
endif() # wxUSE_GUI endif() # wxUSE_GUI
# Include XML library last
# In the monolithic build, where all target properties (include dirs) from different targets are concatenated,
# wxml might include system expat, which might use Mono, which has it's own copy of png.
# Thus to ensure wx's core library includes the right png class, core must be processed first before xml
add_opt_lib(xml wxUSE_XML)
# Include cmake file for every library # Include cmake file for every library
foreach(LIB ${LIBS}) foreach(LIB ${LIBS})
add_subdirectory(${LIB}) add_subdirectory(${LIB})
@@ -87,6 +93,10 @@ if(wxBUILD_MONOLITHIC)
target_compile_definitions(mono ${vis} ${wxMONO_DEFINITIONS_${vis}}) target_compile_definitions(mono ${vis} ${wxMONO_DEFINITIONS_${vis}})
endif() endif()
endforeach() endforeach()
foreach(file ${wxMONO_NONCOMPILED_CPP_FILES})
set_source_files_properties(${file} PROPERTIES HEADER_FILE_ONLY TRUE)
endforeach()
wx_finalize_lib(mono)
endif() endif()
# Propagate variable(s) to parent scope # Propagate variable(s) to parent scope