CMake: Use common function for adding samples, tests and demos

This commit is contained in:
Maarten Bent
2021-01-16 01:33:55 +01:00
parent 4a3ff8c180
commit 226a3de596
7 changed files with 112 additions and 186 deletions

View File

@@ -7,63 +7,6 @@
# Licence: wxWindows licence # Licence: wxWindows licence
############################################################################# #############################################################################
function(wx_add_demo name)
cmake_parse_arguments(DEMO "" "NAME" "DATA;LIBRARIES" ${ARGN})
if(NOT DEMO_NAME)
set(DEMO_NAME ${name})
endif()
wx_list_add_prefix(src_files
"${wxSOURCE_DIR}/demos/${name}/"
${DEMO_UNPARSED_ARGUMENTS})
if(WIN32)
list(APPEND src_files ${wxSOURCE_DIR}/demos/${name}/${DEMO_NAME}.rc)
endif()
if (WXMSW AND DEFINED wxUSE_DPI_AWARE_MANIFEST)
set(wxDPI_MANIFEST_PRFIX "wx")
if (wxARCH_SUFFIX)
set(wxDPI_MANIFEST_PRFIX "amd64")
endif()
set(wxUSE_DPI_AWARE_MANIFEST_VALUE 0)
if (${wxUSE_DPI_AWARE_MANIFEST} MATCHES "system")
set(wxUSE_DPI_AWARE_MANIFEST_VALUE 1)
list(APPEND src_files "${wxSOURCE_DIR}/include/wx/msw/${wxDPI_MANIFEST_PRFIX}_dpi_aware.manifest")
elseif(${wxUSE_DPI_AWARE_MANIFEST} MATCHES "per-monitor")
set(wxUSE_DPI_AWARE_MANIFEST_VALUE 2)
list(APPEND src_files "${wxSOURCE_DIR}/include/wx/msw/${wxDPI_MANIFEST_PRFIX}_dpi_aware_pmv2.manifest")
endif()
endif()
add_executable(${DEMO_NAME} WIN32 MACOSX_BUNDLE ${src_files})
if (DEFINED wxUSE_DPI_AWARE_MANIFEST_VALUE)
target_compile_definitions(${DEMO_NAME} PRIVATE wxUSE_DPI_AWARE_MANIFEST=${wxUSE_DPI_AWARE_MANIFEST_VALUE})
endif()
if(DEMO_DATA)
# TODO: unify with data handling for samples
# TODO: handle data files differently for OS X bundles
# Copy data files to output directory
foreach(data_file ${DEMO_DATA})
list(APPEND cmds COMMAND ${CMAKE_COMMAND}
-E copy ${wxSOURCE_DIR}/demos/${name}/${data_file}
${wxOUTPUT_DIR}/${wxPLATFORM_LIB_DIR}/${data_file})
endforeach()
add_custom_command(
TARGET ${DEMO_NAME} ${cmds}
COMMENT "Copying demo data files...")
endif()
if(wxBUILD_SHARED)
target_compile_definitions(${DEMO_NAME} PRIVATE WXUSINGDLL)
endif()
wx_exe_link_libraries(${DEMO_NAME} wxcore ${DEMO_LIBRARIES})
wx_set_common_target_properties(${DEMO_NAME})
set_target_properties(${DEMO_NAME} PROPERTIES FOLDER "Demos")
set_target_properties(${DEMO_NAME} PROPERTIES
VS_DEBUGGER_WORKING_DIRECTORY "${wxOUTPUT_DIR}/${wxCOMPILER_PREFIX}${wxARCH_SUFFIX}_${lib_suffix}"
)
endfunction()
wx_add_demo(bombs wx_add_demo(bombs
bombs.cpp bombs.cpp
bombs.h bombs.h

View File

@@ -367,17 +367,8 @@ macro(wx_target_enable_precomp target_name)
cotire(${target_name}) cotire(${target_name})
endmacro() endmacro()
# Enable precompiled headers for tests # Enable precompiled headers for applications
macro(wx_test_enable_precomp target_name) macro(wx_app_enable_precomp target_name)
if(wxBUILD_PRECOMP)
wx_target_enable_precomp(${target_name} "${wxSOURCE_DIR}/tests/testprec.h")
elseif(MSVC)
target_compile_definitions(${target_name} PRIVATE NOPCH)
endif()
endmacro()
# Enable precompiled headers for samples
macro(wx_sample_enable_precomp target_name)
if(wxBUILD_PRECOMP) if(wxBUILD_PRECOMP)
wx_target_enable_precomp(${target_name} "${wxSOURCE_DIR}/include/wx/wxprec.h") wx_target_enable_precomp(${target_name} "${wxSOURCE_DIR}/include/wx/wxprec.h")
elseif(MSVC) elseif(MSVC)
@@ -414,10 +405,12 @@ endmacro()
# wx_exe_link_libraries(target libs...) # wx_exe_link_libraries(target libs...)
# Link wx libraries to executable # Link wx libraries to executable
macro(wx_exe_link_libraries name) macro(wx_exe_link_libraries name)
if(wxBUILD_MONOLITHIC) if(TARGET ${name})
target_link_libraries(${name} PUBLIC wxmono) if(wxBUILD_MONOLITHIC)
else() target_link_libraries(${name} PUBLIC wxmono)
target_link_libraries(${name};PRIVATE;${ARGN}) else()
target_link_libraries(${name};PRIVATE;${ARGN})
endif()
endif() endif()
endmacro() endmacro()
@@ -612,82 +605,111 @@ function(wx_print_thirdparty_library_summary)
message(STATUS ${message}) message(STATUS ${message})
endfunction() endfunction()
# Add executable for sample # Add sample, test or demo
# wx_add_sample(<name> [CONSOLE|DLL] [IMPORTANT] [SRC_FILES...] # wx_add(<name> <group> [CONSOLE|DLL] [IMPORTANT] [SRC_FILES...]
# [LIBRARIES ...] [NAME target_name] [FOLDER folder]) # [LIBRARIES ...] [NAME target_name] [FOLDER folder]
# first parameter may be CONSOLE to indicate a console application # [DATA ...] [DEFINITIONS ...] [RES ...])
# all following parameters a src files for the executable # name default target name
# source files are relative to samples/${name}/ # group can be Samples, Tests or Demos
# first parameter may be CONSOLE to indicate a console application or DLL to indicate a shared library
# all following parameters are src files for the executable
#
# Optionally: # Optionally:
# DATA followed by required data files. Use a colon to separate different source and dest paths # IMPORTANT (samples only) does not require wxBUILD_SAMPLES=ALL
# DEFINITIONS list of definitions for the target # LIBRARIES followed by required libraries
# FOLDER subfolder in IDE # NAME alternative target_name
# LIBRARIES followed by required libraries # FOLDER subfolder in IDE
# NAME alternative target_name # DATA followed by required data files. Use a colon to separate different source and dest paths
# IMPORTANT does not require wxBUILD_SAMPLES=ALL # DEFINITIONS list of definitions for the target
# RES followed by WIN32 .rc files # RES followed by WIN32 .rc files
# #
# Additionally the following variables may be set before calling wx_add_sample: # Additionally the following variables may be set before calling wx_add_sample:
# wxSAMPLE_SUBDIR subdirectory in the samples/ folder to use as base # wxSAMPLE_SUBDIR subdirectory in the samples/ folder to use as base
# wxSAMPLE_FOLDER IDE sub folder to be used for the samples # wxSAMPLE_FOLDER IDE sub folder to be used for the samples
function(wx_add_sample name) function(wx_add_sample name)
cmake_parse_arguments(SAMPLE wx_add(${name} "Samples" ${ARGN})
endfunction()
function(wx_add_test name)
wx_add(${name} "Tests" ${ARGN})
endfunction()
function(wx_add_demo name)
wx_add(${name} "Demos" ${ARGN})
endfunction()
function(wx_add name group)
cmake_parse_arguments(APP
"CONSOLE;DLL;IMPORTANT" "CONSOLE;DLL;IMPORTANT"
"NAME;FOLDER" "NAME;FOLDER"
"DATA;DEFINITIONS;DEPENDS;LIBRARIES;RES" "DATA;DEFINITIONS;DEPENDS;LIBRARIES;RES"
${ARGN} ${ARGN}
) )
if(NOT SAMPLE_FOLDER AND wxSAMPLE_FOLDER)
set(SAMPLE_FOLDER ${wxSAMPLE_FOLDER}) if(APP_NAME)
set(target_name ${APP_NAME})
else()
set(target_name ${name})
endif() endif()
# Only build important samples without wxBUILD_SAMPLES=ALL if(group STREQUAL Samples)
if(NOT SAMPLE_IMPORTANT AND NOT wxBUILD_SAMPLES STREQUAL "ALL") if(NOT APP_IMPORTANT AND NOT wxBUILD_SAMPLES STREQUAL "ALL")
return()
endif()
set(SUB_DIR "samples/${wxSAMPLE_SUBDIR}${name}")
set(DEFAULT_RC_FILE "samples/sample.rc")
elseif(group STREQUAL Tests)
if(NOT APP_CONSOLE AND NOT wxBUILD_TESTS STREQUAL "ALL")
return()
endif()
set(SUB_DIR "tests")
set(DEFAULT_RC_FILE "samples/sample.rc")
elseif(group STREQUAL Demos)
set(SUB_DIR "demos/${name}")
set(DEFAULT_RC_FILE "demos/${name}/${target_name}.rc")
else()
message(WARNING "Unkown group \"${group}\"")
return() return()
endif() endif()
foreach(depend ${SAMPLE_DEPENDS})
foreach(depend ${APP_DEPENDS})
if(NOT ${depend}) if(NOT ${depend})
return() return()
endif() endif()
endforeach() endforeach()
# Only build GUI samples with wxUSE_GUI=1 # Only build GUI applications with wxUSE_GUI=1
if(NOT wxUSE_GUI AND NOT SAMPLE_CONSOLE) if(NOT wxUSE_GUI AND NOT APP_CONSOLE)
return() return()
endif() endif()
if(SAMPLE_UNPARSED_ARGUMENTS) if(APP_UNPARSED_ARGUMENTS)
wx_list_add_prefix(src_files wx_list_add_prefix(src_files
"${wxSOURCE_DIR}/samples/${wxSAMPLE_SUBDIR}${name}/" "${wxSOURCE_DIR}/${SUB_DIR}/"
${SAMPLE_UNPARSED_ARGUMENTS}) ${APP_UNPARSED_ARGUMENTS})
else() else()
# If no source files have been specified use default src name # If no source files have been specified use default src name
set(src_files ${wxSOURCE_DIR}/samples/${wxSAMPLE_SUBDIR}${name}/${name}.cpp) set(src_files ${wxSOURCE_DIR}/${SUB_DIR}/${name}.cpp)
endif() endif()
if(WIN32) if(WIN32)
if(SAMPLE_RES) if(APP_RES)
foreach(res ${SAMPLE_RES}) foreach(res ${APP_RES})
list(APPEND src_files ${wxSOURCE_DIR}/samples/${wxSAMPLE_SUBDIR}${name}/${res}) list(APPEND src_files ${wxSOURCE_DIR}/${SUB_DIR}/${res})
endforeach() endforeach()
else() else()
# Include default sample.rc # Include default resource file
list(APPEND src_files ${wxSOURCE_DIR}/samples/sample.rc) list(APPEND src_files ${wxSOURCE_DIR}/${DEFAULT_RC_FILE})
endif() endif()
elseif(APPLE AND NOT IPHONE) elseif(APPLE AND NOT IPHONE)
list(APPEND src_files ${wxSOURCE_DIR}/src/osx/carbon/wxmac.icns) list(APPEND src_files ${wxSOURCE_DIR}/src/osx/carbon/wxmac.icns)
endif() endif()
if(SAMPLE_NAME) if(APP_DLL)
set(target_name ${SAMPLE_NAME})
else()
set(target_name ${name})
endif()
if(SAMPLE_DLL)
add_library(${target_name} SHARED ${src_files}) add_library(${target_name} SHARED ${src_files})
else() else()
if(SAMPLE_CONSOLE) if(APP_CONSOLE OR group STREQUAL Tests)
set(exe_type) set(exe_type)
else() else()
set(exe_type WIN32 MACOSX_BUNDLE) set(exe_type WIN32 MACOSX_BUNDLE)
@@ -714,28 +736,37 @@ function(wx_add_sample name)
target_compile_definitions(${target_name} PRIVATE wxUSE_DPI_AWARE_MANIFEST=${wxUSE_DPI_AWARE_MANIFEST_VALUE}) target_compile_definitions(${target_name} PRIVATE wxUSE_DPI_AWARE_MANIFEST=${wxUSE_DPI_AWARE_MANIFEST_VALUE})
endif() endif()
endif() endif()
# All samples use at least the base library other libraries
# All applications use at least the base library other libraries
# will have to be added with wx_link_sample_libraries() # will have to be added with wx_link_sample_libraries()
wx_exe_link_libraries(${target_name} wxbase) wx_exe_link_libraries(${target_name} wxbase)
if(NOT SAMPLE_CONSOLE) if(NOT APP_CONSOLE)
# UI samples always require core # UI applications always require core
wx_exe_link_libraries(${target_name} wxcore) wx_exe_link_libraries(${target_name} wxcore)
else() else()
target_compile_definitions(${target_name} PRIVATE wxUSE_GUI=0 wxUSE_BASE=1) target_compile_definitions(${target_name} PRIVATE wxUSE_GUI=0 wxUSE_BASE=1)
endif() endif()
if(SAMPLE_LIBRARIES) if(APP_LIBRARIES)
wx_exe_link_libraries(${target_name} ${SAMPLE_LIBRARIES}) wx_exe_link_libraries(${target_name} ${APP_LIBRARIES})
endif() endif()
if(wxBUILD_SHARED) if(wxBUILD_SHARED)
target_compile_definitions(${target_name} PRIVATE WXUSINGDLL) target_compile_definitions(${target_name} PRIVATE WXUSINGDLL)
endif() endif()
if(SAMPLE_DEFINITIONS) if(APP_DEFINITIONS)
target_compile_definitions(${target_name} PRIVATE ${SAMPLE_DEFINITIONS}) target_compile_definitions(${target_name} PRIVATE ${APP_DEFINITIONS})
endif() endif()
if(SAMPLE_DATA)
if(group STREQUAL Samples)
target_include_directories(${target_name} PRIVATE ${wxSOURCE_DIR}/samples)
elseif(group STREQUAL Tests)
target_include_directories(${target_name} PRIVATE ${wxSOURCE_DIR}/tests)
target_include_directories(${target_name} PRIVATE ${wxSOURCE_DIR}/3rdparty/catch/include)
endif()
if(APP_DATA)
# TODO: handle data files differently for OS X bundles # TODO: handle data files differently for OS X bundles
# Copy data files to output directory # Copy data files to output directory
foreach(data_src ${SAMPLE_DATA}) foreach(data_src ${APP_DATA})
string(FIND ${data_src} ":" HAS_COLON) string(FIND ${data_src} ":" HAS_COLON)
if(${HAS_COLON} GREATER -1) if(${HAS_COLON} GREATER -1)
MATH(EXPR DEST_INDEX "${HAS_COLON}+1") MATH(EXPR DEST_INDEX "${HAS_COLON}+1")
@@ -746,17 +777,15 @@ function(wx_add_sample name)
endif() endif()
list(APPEND cmds COMMAND ${CMAKE_COMMAND} list(APPEND cmds COMMAND ${CMAKE_COMMAND}
-E copy ${wxSOURCE_DIR}/samples/${wxSAMPLE_SUBDIR}${name}/${data_src} -E copy ${wxSOURCE_DIR}/${SUB_DIR}/${data_src}
${wxOUTPUT_DIR}/${wxPLATFORM_LIB_DIR}/${data_dst}) ${wxOUTPUT_DIR}/${wxPLATFORM_LIB_DIR}/${data_dst})
endforeach() endforeach()
add_custom_command( add_custom_command(
TARGET ${target_name} ${cmds} TARGET ${target_name} ${cmds}
COMMENT "Copying sample data files...") COMMENT "Copying ${target_name} data files...")
endif() endif()
if(WIN32)
# The resource compiler needs this include directory to find res files if(APPLE)
target_include_directories(${target_name} PRIVATE ${wxSOURCE_DIR}/samples/)
elseif(APPLE)
if(NOT IPHONE) if(NOT IPHONE)
set_target_properties(${target_name} PROPERTIES set_target_properties(${target_name} PROPERTIES
MACOSX_BUNDLE_INFO_PLIST "${wxSOURCE_DIR}/samples/Info.plist.in" MACOSX_BUNDLE_INFO_PLIST "${wxSOURCE_DIR}/samples/Info.plist.in"
@@ -772,18 +801,27 @@ function(wx_add_sample name)
) )
endif() endif()
set(folder "Samples") if(APP_FOLDER)
if(SAMPLE_FOLDER) set(APP_FOLDER ${group}/${APP_FOLDER})
wx_string_append(folder "/${SAMPLE_FOLDER}") elseif(wxSAMPLE_FOLDER)
set(APP_FOLDER ${group}/${wxSAMPLE_FOLDER})
else()
set(APP_FOLDER ${group})
endif() endif()
wx_set_common_target_properties(${target_name}) wx_set_common_target_properties(${target_name})
wx_sample_enable_precomp(${target_name}) wx_app_enable_precomp(${target_name})
set_target_properties(${target_name} PROPERTIES set_target_properties(${target_name} PROPERTIES
FOLDER ${folder} FOLDER ${APP_FOLDER}
) )
set_target_properties(${target_name} PROPERTIES set_target_properties(${target_name} PROPERTIES
VS_DEBUGGER_WORKING_DIRECTORY "${wxOUTPUT_DIR}/${wxCOMPILER_PREFIX}${wxARCH_SUFFIX}_${lib_suffix}" VS_DEBUGGER_WORKING_DIRECTORY "${wxOUTPUT_DIR}/${wxCOMPILER_PREFIX}${wxARCH_SUFFIX}_${lib_suffix}"
) )
if(group STREQUAL Tests)
add_test(NAME ${target_name}
COMMAND ${target_name}
WORKING_DIRECTORY ${wxSOURCE_DIR}/tests)
endif()
endfunction() endfunction()
# Link libraries to a sample # Link libraries to a sample
@@ -864,43 +902,3 @@ macro(wx_dependent_option option doc default depends force)
set(${option} "${${option}_ISSET}") set(${option} "${${option}_ISSET}")
endif() endif()
endmacro() endmacro()
# wx_add_test(<name> [src...])
# Optionally:
# DATA followed by required data files
# RES followed by WIN32 .rc files
function(wx_add_test name)
cmake_parse_arguments(TEST "" "" "DATA;RES" ${ARGN})
wx_list_add_prefix(test_src "${wxSOURCE_DIR}/tests/" ${TEST_UNPARSED_ARGUMENTS})
if(WIN32 AND TEST_RES)
foreach(res ${TEST_RES})
list(APPEND test_src ${wxSOURCE_DIR}/tests/${res})
endforeach()
endif()
add_executable(${name} ${test_src})
target_include_directories(${name} PRIVATE "${wxSOURCE_DIR}/tests" "${wxSOURCE_DIR}/3rdparty/catch/include")
wx_exe_link_libraries(${name} wxbase)
if(wxBUILD_SHARED)
target_compile_definitions(${name} PRIVATE WXUSINGDLL)
endif()
if(TEST_DATA)
# Copy data files to output directory
foreach(data_file ${TEST_DATA})
list(APPEND cmds COMMAND ${CMAKE_COMMAND}
-E copy ${wxSOURCE_DIR}/tests/${data_file}
${wxOUTPUT_DIR}/${wxPLATFORM_LIB_DIR}/${data_file})
endforeach()
add_custom_command(
TARGET ${name} ${cmds}
COMMENT "Copying test data files...")
endif()
wx_set_common_target_properties(${name})
set_target_properties(${name} PROPERTIES FOLDER "Tests")
set_target_properties(${name} PROPERTIES
VS_DEBUGGER_WORKING_DIRECTORY "${wxSOURCE_DIR}/tests"
)
add_test(NAME ${name}
COMMAND ${name}
WORKING_DIRECTORY ${wxSOURCE_DIR}/tests)
endfunction()

View File

@@ -8,12 +8,6 @@
############################################################################# #############################################################################
add_subdirectory(base) add_subdirectory(base)
# Build GUI tests
if(wxUSE_GUI AND wxBUILD_TESTS STREQUAL "ALL")
add_subdirectory(drawing) add_subdirectory(drawing)
add_subdirectory(gui) add_subdirectory(gui)
add_subdirectory(headers) add_subdirectory(headers)
endif()

View File

@@ -117,14 +117,12 @@ set(TEST_DATA
testdata.fc testdata.fc
) )
wx_add_test(test_base ${TEST_SRC} wx_add_test(test_base CONSOLE ${TEST_SRC}
DATA ${TEST_DATA} DATA ${TEST_DATA}
) )
target_compile_definitions(test_base PRIVATE wxUSE_GUI=0 wxUSE_BASE=1)
if(wxUSE_SOCKETS) if(wxUSE_SOCKETS)
wx_exe_link_libraries(test_base wxnet) wx_exe_link_libraries(test_base wxnet)
endif() endif()
if(wxUSE_XML) if(wxUSE_XML)
wx_exe_link_libraries(test_base wxxml) wx_exe_link_libraries(test_base wxxml)
endif() endif()
wx_test_enable_precomp(test_base)

View File

@@ -43,8 +43,6 @@ wx_add_test(test_drawing ${TEST_DRAWING_SRC}
if(wxUSE_SOCKETS) if(wxUSE_SOCKETS)
wx_exe_link_libraries(test_drawing wxnet) wx_exe_link_libraries(test_drawing wxnet)
endif() endif()
wx_exe_link_libraries(test_drawing wxcore)
wx_test_enable_precomp(test_drawing)
# This is a sample plugin, it simply uses a wxImage based # This is a sample plugin, it simply uses a wxImage based
# wxGraphicsContext. It should render the same as the built-in test. Use # wxGraphicsContext. It should render the same as the built-in test. Use

View File

@@ -182,9 +182,7 @@ set(TEST_GUI_DATA
wx_add_test(test_gui ${TEST_GUI_SRC} wx_add_test(test_gui ${TEST_GUI_SRC}
DATA ${TEST_GUI_DATA} DATA ${TEST_GUI_DATA}
RES ../samples/sample.rc
) )
wx_exe_link_libraries(test_gui wxcore)
if(wxUSE_AUI) if(wxUSE_AUI)
wx_exe_link_libraries(test_gui wxaui) wx_exe_link_libraries(test_gui wxaui)
endif() endif()
@@ -212,4 +210,3 @@ endif()
if(wxUSE_WEBVIEW) if(wxUSE_WEBVIEW)
wx_exe_link_libraries(test_gui wxwebview) wx_exe_link_libraries(test_gui wxwebview)
endif() endif()
wx_test_enable_precomp(test_gui)

View File

@@ -18,8 +18,6 @@ set(TEST_SRC
) )
wx_add_test(test_headers ${TEST_SRC}) wx_add_test(test_headers ${TEST_SRC})
wx_exe_link_libraries(test_headers wxcore)
if(wxUSE_SOCKETS) if(wxUSE_SOCKETS)
wx_exe_link_libraries(test_headers wxnet) wx_exe_link_libraries(test_headers wxnet)
endif() endif()
wx_test_enable_precomp(test_headers)