Skip to content

Commit

Permalink
Convert CEF CMake to library.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreduvoisin committed Mar 20, 2019
1 parent cadd303 commit c0f74d8
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 126 deletions.
223 changes: 115 additions & 108 deletions cmake/CEF.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include(ExternalProject)

cmake_policy(SET CMP0074 NEW)
if(POLICY CMP0077)
cmake_policy(SET CMP0077 NEW)
Expand Down Expand Up @@ -35,119 +37,124 @@ endif()
# CMake Reference:
# https://bitbucket.org/chromiumembedded/cef-project/src/master/CMakeLists.txt
# https://bitbucket.org/chromiumembedded/cef/src/master/CMakeLists.txt.in
function(BuildCEF)
if(OS_WINDOWS)
set(BUILD_BYPRODUCTS
"${CEF_ROOT}/libcef_dll_wrapper/libcef_dll_wrapper.dll"
"${CEF_ROOT}/libcef_dll_wrapper/libcef_dll_wrapper.lib"
"${CEF_ROOT}/${CEF_CONFIGURATION}/libcef.lib"
)
elseif(OS_MACOSX)
set(BUILD_BYPRODUCTS
"${CEF_ROOT}/libcef_dll_wrapper/libcef_dll_wrapper.a"
"${CEF_ROOT}/${CEF_CONFIGURATION}/Chromium Embedded Framework.framework"
)
endif()

ExternalProject_Add(
CEF
PREFIX ${CEF_DISTRIBUTION}

DOWNLOAD_DIR ${EXTERN_DIR}
URL "http://opensource.spotify.com/cefbuilds/${CEF_DISTRIBUTION}.tar.bz2"

SOURCE_DIR ${CEF_ROOT}
BINARY_DIR ${CEF_ROOT}

# Custom configuration for Composite Engine.
#
# Linking Reference: https://bitbucket.org/chromiumembedded/cef/wiki/LinkingDifferentRunTimeLibraries.md
# opengl32.lib (and glu32.lib) link with /MD, so we can't link with /MT.
# /MD[d] is included in CMAKE_CXX_FLAGS[_*] by default.
# Also, to maintain our sanity, we would like to avoid having to build Chromium and CEF.
#
# Sandbox Reference: https://magpcss.org/ceforum/viewtopic.php?f=6&t=15482
CMAKE_ARGS
-DCMAKE_MAKE_PROGRAM=ninja
-DCEF_RUNTIME_LIBRARY_FLAG=/MD
-DUSE_SANDBOX=OFF

BUILD_COMMAND ninja
INSTALL_COMMAND ""

BUILD_BYPRODUCTS ${BUILD_BYPRODUCTS}
if(OS_WINDOWS)
set(BUILD_BYPRODUCTS
"${CEF_ROOT}/libcef_dll_wrapper/libcef_dll_wrapper.dll"
"${CEF_ROOT}/libcef_dll_wrapper/libcef_dll_wrapper.lib"
"${CEF_ROOT}/${CEF_CONFIGURATION}/libcef.lib"
)
endfunction()
elseif(OS_MACOSX)
set(BUILD_BYPRODUCTS
"${CEF_ROOT}/libcef_dll_wrapper/libcef_dll_wrapper.a"
"${CEF_ROOT}/${CEF_CONFIGURATION}/Chromium Embedded Framework.framework"
)
endif()

ExternalProject_Add(
CEFExternal
PREFIX ${CEF_DISTRIBUTION}

function(BootstrapCEF TARGET_NAME EXECUTABLE_SUBDIR)
IncludeCEF()
LinkCEF(${TARGET_NAME})
CopyCEFFiles(${TARGET_NAME} ${EXECUTABLE_SUBDIR})
endfunction()
DOWNLOAD_DIR ${EXTERN_DIR}
URL "http://opensource.spotify.com/cefbuilds/${CEF_DISTRIBUTION}.tar.bz2"

function(IncludeCEF)
include_directories(${CEF_ROOT})
endfunction()
SOURCE_DIR ${CEF_ROOT}
BINARY_DIR ${CEF_ROOT}

# Custom configuration for Composite Engine.
#
# Linking Reference: https://bitbucket.org/chromiumembedded/cef/wiki/LinkingDifferentRunTimeLibraries.md
# opengl32.lib (and glu32.lib) link with /MD, so we can't link with /MT.
# /MD[d] is included in CMAKE_CXX_FLAGS[_*] by default.
# Also, to maintain our sanity, we would like to avoid having to build Chromium and CEF.
#
# Sandbox Reference: https://magpcss.org/ceforum/viewtopic.php?f=6&t=15482
CMAKE_ARGS
-DCMAKE_MAKE_PROGRAM=ninja
-DCEF_RUNTIME_LIBRARY_FLAG=/MD
-DUSE_SANDBOX=OFF

BUILD_COMMAND ninja
INSTALL_COMMAND ""

BUILD_BYPRODUCTS ${BUILD_BYPRODUCTS}
)

add_library(CEF INTERFACE)
add_dependencies(CEF CEFExternal)

target_include_directories(CEF INTERFACE ${CEF_ROOT})

# Reference: https://bitbucket.org/chromiumembedded/cef/wiki/LinkingDifferentRunTimeLibraries.md
# Sandbox support (linking cef_sandbox.lib) is only possible when your application is built with the /MT flag.
function(LinkCEF TARGET_NAME)
if(OS_WINDOWS)
set(LIBRARIES
"${CEF_ROOT}/libcef_dll_wrapper/libcef_dll_wrapper.lib"
"${CEF_ROOT}/${CEF_CONFIGURATION}/libcef.lib"
)
#set(LIBRARIES ${LIBRARIES} "${CEF_ROOT}/${CEF_CONFIGURATION}/cef_sandbox.lib")
elseif(OS_MACOSX)
set(LIBRARIES
"${CEF_ROOT}/libcef_dll_wrapper/libcef_dll_wrapper.a"
"${CEF_ROOT}/${CEF_CONFIGURATION}/Chromium Embedded Framework.framework"
)
#set(LIBRARIES ${LIBRARIES} "${CEF_ROOT}/${CEF_CONFIGURATION}/cef_sandbox.a")
endif()
if(OS_WINDOWS)
set(LIBRARIES
"${CEF_ROOT}/libcef_dll_wrapper/libcef_dll_wrapper.lib"
"${CEF_ROOT}/${CEF_CONFIGURATION}/libcef.lib"
)
#set(LIBRARIES ${LIBRARIES} "${CEF_ROOT}/${CEF_CONFIGURATION}/cef_sandbox.lib")
elseif(OS_MACOSX)
set(LIBRARIES
"${CEF_ROOT}/libcef_dll_wrapper/libcef_dll_wrapper.a"
"${CEF_ROOT}/${CEF_CONFIGURATION}/Chromium Embedded Framework.framework"
)
#set(LIBRARIES ${LIBRARIES} "${CEF_ROOT}/${CEF_CONFIGURATION}/cef_sandbox.a")
endif()

target_link_libraries(CEF INTERFACE ${LIBRARIES})

target_link_libraries(${TARGET_NAME} PRIVATE ${LIBRARIES})
endfunction()

function(CopyCEFFiles TARGET_NAME EXECUTABLE_SUBDIR)
if(OS_WINDOWS)
# TODO: As far as I know, the *.lib files from this copy are unnecessary,
# which means this should be changed to only copy *.dll and *.bin files.
# However, the CEF README.txt implies that the whole directory should be copied.
add_custom_command(
TARGET ${TARGET_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${CEF_ROOT}/${CEF_CONFIGURATION}"
"${PROJECT_BINARY_DIR}/${EXECUTABLE_SUBDIR}/${CE_CONFIGURATION}"
VERBATIM
)
if(OS_WINDOWS)
# TODO: As far as I know, the *.lib files from this copy are unnecessary,
# which means this should be changed to only copy *.dll and *.bin files.
# However, the CEF README.txt implies that the whole directory should be copied.
#add_custom_command(
# TARGET ${TARGET_NAME}
# POST_BUILD
# COMMAND ${CMAKE_COMMAND} -E copy_directory
# "${CEF_ROOT}/${CEF_CONFIGURATION}"
# "${PROJECT_BINARY_DIR}/${EXECUTABLE_SUBDIR}/${CE_CONFIGURATION}"
# VERBATIM
#)

add_custom_command(
TARGET ${TARGET_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${CEF_ROOT}/Resources"
"${PROJECT_BINARY_DIR}/${EXECUTABLE_SUBDIR}/${CE_CONFIGURATION}"
VERBATIM
)
elseif(OS_MACOSX)
# TODO: This should use EXECUTABLE_SUBDIR, and only one of these two should exist.
add_custom_command(
TARGET ${TARGET_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${CEF_ROOT}/${CEF_CONFIGURATION}/Chromium Embedded Framework.framework"
"${PROJECT_BINARY_DIR}/engine/${CE_CONFIGURATION}/CompositeEngine.app/Contents/Frameworks/Chromium Embedded Framework.framework"
VERBATIM
)
add_custom_command(
TARGET ${TARGET_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${CEF_ROOT}/${CEF_CONFIGURATION}/Chromium Embedded Framework.framework"
"${PROJECT_BINARY_DIR}/ui/${CE_CONFIGURATION}/CompositeCefSubprocess.app/Contents/Frameworks/Chromium Embedded Framework.framework"
VERBATIM
)
endif()
endfunction()
#add_custom_command(
# TARGET ${TARGET_NAME}
# POST_BUILD
# COMMAND ${CMAKE_COMMAND} -E copy_directory
# "${CEF_ROOT}/Resources"
# "${PROJECT_BINARY_DIR}/${EXECUTABLE_SUBDIR}/${CE_CONFIGURATION}"
# VERBATIM
#)

# The trailing slashes are important.
# Reference: https://cmake.org/cmake/help/v3.12/command/install.html#installing-directories
# "The last component of each directory name is appended to the destination directory but
# a trailing slash may be used to avoid this because it leaves the last component empty."
install(
DIRECTORY "${CEF_ROOT}/${CEF_CONFIGURATION}/"
DESTINATION "${CMAKE_INSTALL_PREFIX}"
FILES_MATCHING
PATTERN "*.dll"
PATTERN "*.bin"
)
install(
DIRECTORY "${CEF_ROOT}/Resources/"
DESTINATION "${CMAKE_INSTALL_PREFIX}"
)
elseif(OS_MACOSX)
# TODO: This should use EXECUTABLE_SUBDIR, and only one of these two should exist.
add_custom_command(
TARGET ${TARGET_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${CEF_ROOT}/${CEF_CONFIGURATION}/Chromium Embedded Framework.framework"
"${PROJECT_BINARY_DIR}/engine/${CE_CONFIGURATION}/CompositeEngine.app/Contents/Frameworks/Chromium Embedded Framework.framework"
VERBATIM
)
add_custom_command(
TARGET ${TARGET_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${CEF_ROOT}/${CEF_CONFIGURATION}/Chromium Embedded Framework.framework"
"${PROJECT_BINARY_DIR}/ui/${CE_CONFIGURATION}/CompositeCefSubprocess.app/Contents/Frameworks/Chromium Embedded Framework.framework"
VERBATIM
)
endif()
10 changes: 1 addition & 9 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
include("${CMAKE_DIR}/CEF.cmake")

if(OS_MACOSX)
set(COMPOSITE_ENGINE_EXECUTABLE_ARGS MACOSX_BUNDLE)
Expand All @@ -21,14 +20,9 @@ add_executable(CompositeEngine ${COMPOSITE_ENGINE_EXECUTABLE_ARGS} ${ENGINE_SRC_
add_dependencies(
CompositeEngine
CompositeCefSubprocess
CEF
#GLEW
#GLM
#RapidJSON
#SDL
)

target_link_libraries(CompositeEngine PRIVATE GLEW GLM OpenGL RapidJSON SDL)
target_link_libraries(CompositeEngine PRIVATE CEF GLEW GLM OpenGL RapidJSON SDL)

source_group(TREE ${CMAKE_SOURCE_DIR} FILES ${ENGINE_SRC_FILES})

Expand All @@ -43,8 +37,6 @@ if(WIN32)
#target_link_libraries(CompositeEngine -manifest:embed -manifestinput:"${ENGINE_SRC_DIR}/CompositeEngine.manifest")
endif()

BootstrapCEF(CompositeEngine "engine")

install(
TARGETS CompositeEngine
RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}"
Expand Down
2 changes: 1 addition & 1 deletion engine/main/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ bool StartCef()
settings.external_message_pump = true;
settings.windowless_rendering_enabled = true;
settings.remote_debugging_port = 3469;
CefString(&settings.browser_subprocess_path).FromASCII("../../ui/Debug/CompositeCefSubprocess.exe");
CefString(&settings.browser_subprocess_path).FromASCII("CompositeCefSubprocess.exe");
if (!CefInitialize(main_args, settings, app, NULL))
{
printf("CEF failed to initialize.\n");
Expand Down
2 changes: 0 additions & 2 deletions extern/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ include("${CMAKE_DIR}/OpenGL.cmake")
include("${CMAKE_DIR}/RapidJSON.cmake")
include("${CMAKE_DIR}/SDL.cmake")
include("${CMAKE_DIR}/STB.cmake")

BuildCEF()
16 changes: 10 additions & 6 deletions ui/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
include("${CMAKE_DIR}/CEF.cmake")

if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
set(COMPOSITE_CEF_SUBPROCESS_EXECUTABLE_ARGS MACOSX_BUNDLE)
Expand All @@ -19,10 +18,7 @@ endif()

add_executable(CompositeCefSubprocess ${COMPOSITE_CEF_SUBPROCESS_EXECUTABLE_ARGS} ${CEF_SUBPROCESS_SRC_FILES})

add_dependencies(
CompositeCefSubprocess
CEF
)
target_link_libraries(CompositeCefSubprocess PRIVATE CEF)

source_group(TREE ${CMAKE_SOURCE_DIR} FILES ${CEF_SUBPROCESS_SRC_FILES})

Expand All @@ -33,4 +29,12 @@ if(WIN32)
#target_link_libraries(CompositeCefSubprocess -manifest:embed -manifestinput:"${ENGINE_SRC_DIR}/CompositeEngine.manifest")
endif()

BootstrapCEF(CompositeCefSubprocess "ui")
install(
TARGETS CompositeCefSubprocess
RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}"
)
install(
FILES $<TARGET_PDB_FILE:CompositeCefSubprocess>
DESTINATION "${CMAKE_INSTALL_PREFIX}"
OPTIONAL
)

0 comments on commit c0f74d8

Please sign in to comment.