CMake: Fix building webview sample with Chromium

The macOS code is based on the cefsimple example in the cef distribution.
This commit is contained in:
Maarten Bent 2023-12-21 20:42:59 +01:00
parent 2821fe0127
commit 6b6c5f5378
No known key found for this signature in database
GPG key ID: 58AAEE3F4A4FD070
2 changed files with 82 additions and 84 deletions

View file

@ -14,75 +14,90 @@ if(wxUSE_STC)
wx_exe_link_libraries(webviewsample wxstc)
endif()
if(wxUSE_WEBVIEW_CHROMIUM)
wx_add_sample(webview LIBRARIES webview_chromium stc adv NAME webviewsample_chromium)
if(TARGET webviewsample_chromium)
target_compile_definitions(webviewsample_chromium PRIVATE -DwxWEBVIEW_SAMPLE_CHROMIUM)
if(WIN32)
add_custom_command(
TARGET webviewsample_chromium
POST_BUILD
# Add compatiblity manifest
COMMAND
mt.exe /nologo -manifest ${wxSOURCE_DIR}/samples/webview/compatibility.manifest
"-inputresource:$<TARGET_FILE:webviewsample_chromium>;#1"
"-outputresource:$<TARGET_FILE:webviewsample_chromium>;#1"
# Copy CEF libraries
COMMAND
${CMAKE_COMMAND} -E copy_directory ${CEF_ROOT}/$<CONFIG> $<TARGET_FILE_DIR:webviewsample_chromium>
# Copy CEF resources
COMMAND
${CMAKE_COMMAND} -E copy_directory ${CEF_ROOT}/Resources $<TARGET_FILE_DIR:webviewsample_chromium>
COMMENT "Prepare executable for runtime..."
)
elseif(APPLE)
# Define helper bundle
set(CEF_HELPER_OUTPUT_NAME "webviewsample_chromium Helper")
add_executable(webviewsample_chromium_helper MACOSX_BUNDLE ${wxSOURCE_DIR}/samples/webview/cef_process_helper.cpp)
target_include_directories(webviewsample_chromium_helper PRIVATE ${CEF_ROOT})
target_link_libraries(webviewsample_chromium_helper libcef libcef_dll_wrapper)
set_target_properties(webviewsample_chromium_helper PROPERTIES
MACOSX_BUNDLE_INFO_PLIST ${wxSOURCE_DIR}/samples/webview/cef_helper_info.plist.in
INSTALL_RPATH "@executable_path/../../../.."
if(wxUSE_WEBVIEW_CHROMIUM AND TARGET webviewsample)
if(WIN32 OR (UNIX AND NOT APPLE))
# Copy CEF libraries and resources next to the executable.
add_custom_command(
TARGET webviewsample
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CEF_ROOT}/$<CONFIG> $<TARGET_FILE_DIR:webviewsample>
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CEF_ROOT}/Resources $<TARGET_FILE_DIR:webviewsample>
COMMENT "Copying webviewsample CEF resources..."
)
if(UNIX)
# Set rpath so that libraries can be placed next to the executable.
# Fixes error: "Invalid file descriptor to ICU data received."
set_target_properties(webviewsample PROPERTIES
INSTALL_RPATH "$ORIGIN"
BUILD_WITH_INSTALL_RPATH TRUE
OUTPUT_NAME ${CEF_HELPER_OUTPUT_NAME}
CXX_STANDARD 11
)
if(NOT wxBUILD_SHARED)
set_target_properties(webviewsample_chromium PROPERTIES
INSTALL_RPATH "@executable_path/.."
BUILD_WITH_INSTALL_RPATH TRUE
)
endif()
add_dependencies(webviewsample_chromium webviewsample_chromium_helper)
add_custom_command(
TARGET webviewsample_chromium
POST_BUILD
# Copy the helper app bundle into the Frameworks directory.
COMMAND
${CMAKE_COMMAND} -E copy_directory
"$<TARGET_FILE_DIR:webviewsample_chromium_helper>/../../../${CEF_HELPER_OUTPUT_NAME}.app"
"$<TARGET_FILE_DIR:webviewsample_chromium>/../Frameworks/${CEF_HELPER_OUTPUT_NAME}.app"
# Copy the CEF framework into the Frameworks directory.
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${CEF_ROOT}/$<CONFIG>/Chromium Embedded Framework.framework"
"$<TARGET_FILE_DIR:webviewsample_chromium>/../Frameworks/Chromium Embedded Framework.framework"
)
else()
add_custom_command(
TARGET webviewsample_chromium
POST_BUILD
# Copy CEF libraries
COMMAND
${CMAKE_COMMAND} -E copy_directory ${CEF_ROOT}/Release $<TARGET_FILE_DIR:webviewsample_chromium>
# Copy CEF resources
COMMAND
${CMAKE_COMMAND} -E copy_directory ${CEF_ROOT}/Resources $<TARGET_FILE_DIR:webviewsample_chromium>
COMMENT "Prepare executable for runtime..."
)
endif()
elseif(APPLE)
# Copy the CEF framework into the Frameworks directory.
add_custom_command(
TARGET webviewsample
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${CEF_ROOT}/$<CONFIG>/Chromium Embedded Framework.framework"
"$<TARGET_FILE_DIR:webviewsample>/../Frameworks/Chromium Embedded Framework.framework"
)
# CEF Helper app suffixes.
# Format is "<name suffix>:<target suffix>:<plist suffix>".
set(CEF_HELPER_APP_SUFFIXES
"::"
" (Alerts):_alerts:.alerts"
" (GPU):_gpu:.gpu"
" (Plugin):_plugin:.plugin"
" (Renderer):_renderer:.renderer"
)
set(CEF_HELPER_TARGET "webviewsample_Helper")
set(CEF_HELPER_OUTPUT_NAME "webviewsample Helper")
# Create the multiple Helper app bundle targets.
foreach(_suffix_list ${CEF_HELPER_APP_SUFFIXES})
# Convert to a list and extract the suffix values.
string(REPLACE ":" ";" _suffix_list ${_suffix_list})
list(GET _suffix_list 0 _name_suffix)
list(GET _suffix_list 1 _target_suffix)
list(GET _suffix_list 2 _plist_suffix)
# Define Helper target and output names.
set(_helper_target "${CEF_HELPER_TARGET}${_target_suffix}")
set(_helper_output_name "${CEF_HELPER_OUTPUT_NAME}${_name_suffix}")
# Create Helper-specific variants of the helper-Info.plist file. Do this
# manually because the configure_file command (which is executed as part of
# MACOSX_BUNDLE_INFO_PLIST) uses global env variables and would insert the
# wrong values with multiple targets.
set(_helper_info_plist "${CMAKE_CURRENT_BINARY_DIR}/helper-Info${_target_suffix}.plist")
file(READ "${wxSOURCE_DIR}/samples/webview/cef_helper_info.plist.in" _plist_contents)
string(REPLACE "\${EXECUTABLE_NAME}" "${_helper_output_name}" _plist_contents ${_plist_contents})
string(REPLACE "\${PRODUCT_NAME}" "${_helper_output_name}" _plist_contents ${_plist_contents})
string(REPLACE "\${BUNDLE_ID_SUFFIX}" "${_plist_suffix}" _plist_contents ${_plist_contents})
file(WRITE ${_helper_info_plist} ${_plist_contents})
# Create Helper executable target.
add_executable(${_helper_target} MACOSX_BUNDLE ${wxSOURCE_DIR}/samples/webview/cef_process_helper.cpp)
target_include_directories(${_helper_target} PRIVATE "${CEF_ROOT}")
target_link_libraries(${_helper_target} libcef_dll_wrapper)
set_target_properties(${_helper_target} PROPERTIES
MACOSX_BUNDLE_INFO_PLIST ${_helper_info_plist}
OUTPUT_NAME ${_helper_output_name}
)
# Add the Helper as a dependency of the main executable target.
add_dependencies(webviewsample "${_helper_target}")
# Copy the Helper app bundle into the Frameworks directory.
add_custom_command(
TARGET webviewsample
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"$<TARGET_FILE_DIR:${_helper_target}>/../../../${_helper_output_name}.app"
"$<TARGET_FILE_DIR:webviewsample>/../Frameworks/${_helper_output_name}.app"
VERBATIM
)
endforeach()
endif()
endif()