构建适用于 MinGW 的 CEF3

由于 CEF3 二进制发行版(以下简称 CEF3) 并没有提供用于 MinGW 编译器(以下简称 MinGW) 的构建配置,且由 MSVC 编译器 构建出的 libcef_dll_wrapper 静态库 无法在 MinGW 上链接使用(ABI 不兼容),因此我们需要自行编辑一份用于 MinGW 的构建配置,以构建出可用于其上的 libcef_dll_wrapper 静态库。

MinGW 是 GCC 的 Windows 版本,因此我们对 CEF3 所提供的用于 GCC 的构建配置稍加修改,就能够用于 MinGW。

以 CEF3 4280 版本举例:

  1. 打开 CEF3 的 cmake 子目录下的 cef_variables.cmake 文件(在较老的版本中则是打开根目录下的 CMakeLists.txt 文件):
    cef_variables.cmake
    CMakeLists.txt

  2. 找到 Linux configuration 段,复制 if(OS_LINUX) 块下的内容:
    Linux configuration

  3. 找到 Windows configuration 段,将先前复制的内容与 if(OS_WINDOWS) 块下的内容做合并。
    合并的准则为:编译器及链接器等参数信息以 Linux 为准,文件名信息以 Windows 为准,将针对 MSVC 或 Linux 平台才有的内容直接删除。
    以下是一个合并完成的示例:

    点击查看代码
    #
    # Windows configuration.
    #
    
    if(OS_WINDOWS)
      # Platform-specific compiler/linker flags.
      set(CEF_LIBTYPE STATIC)
      set(USE_SANDBOX OFF)
    
      list(APPEND CEF_COMPILER_FLAGS
        -fno-strict-aliasing            # Avoid assumptions regarding non-aliasing of objects of different types
        -funwind-tables                 # Support stack unwinding for backtrace()
        -fvisibility=hidden             # Give hidden visibility to declarations that are not explicitly marked as visible
        --param=ssp-buffer-size=4       # Set the minimum buffer size protected by SSP (security feature, related to stack-protector)
        -pipe                           # Use pipes rather than temporary files for communication between build stages
        -pthread                        # Use the pthread library
        -Wall                           # Enable all warnings
        -Wno-missing-field-initializers # Don't warn about missing field initializers
        -Wno-unused-parameter           # Don't warn about unused parameters
        -Wno-error=comment              # Don't warn about code in comments
        -Wno-comment                    # Don't warn about code in comments
        )
        # 需要注意的是, 我们不能启用 -fstack-protector 和 -Werror 参数, 因为前者在此处并不适用, 后者会将警告作为错误处理
    
      list(APPEND CEF_C_COMPILER_FLAGS
        -std=c99                        # Use the C99 language standard
        )
      list(APPEND CEF_CXX_COMPILER_FLAGS
        -fno-exceptions                 # Disable exceptions
        -fno-rtti                       # Disable real-time type information
        -fno-threadsafe-statics         # Don't generate thread-safe statics
        -fvisibility-inlines-hidden     # Give hidden visibility to inlined class member functions
        -std=gnu++11                    # Use the C++11 language standard including GNU extensions
        -Wsign-compare                  # Warn about mixed signed/unsigned type comparisons
        )
      list(APPEND CEF_COMPILER_FLAGS_DEBUG
        -O0                             # Disable optimizations
        -g                              # Generate debug information
        )
      list(APPEND CEF_COMPILER_FLAGS_RELEASE
        -O2                             # Optimize for maximum speed
        -fdata-sections                 # Enable linker optimizations to improve locality of reference for data sections
        -ffunction-sections             # Enable linker optimizations to improve locality of reference for function sections
        -fno-ident                      # Ignore the #ident directive
        -U_FORTIFY_SOURCE               # Undefine _FORTIFY_SOURCE in case it was previously defined
        -D_FORTIFY_SOURCE=2             # Add memory and string function protection (security feature, related to stack-protector)
        )
      list(APPEND CEF_LINKER_FLAGS
        -fPIC                           # Generate position-independent code for shared libraries
        -pthread                        # Use the pthread library
        -Wl,--disable-new-dtags         # Don't generate new-style dynamic tags in ELF
        -Wl,--fatal-warnings            # Treat warnings as errors
        -Wl,-rpath,.                    # Set rpath so that libraries can be placed next to the executable
        -Wl,-z,noexecstack              # Mark the stack as non-executable (security feature)
        -Wl,-z,now                      # Resolve symbols on program start instead of on first use (security feature)
        -Wl,-z,relro                    # Mark relocation sections as read-only (security feature)
        )
      list(APPEND CEF_LINKER_FLAGS_RELEASE
        -Wl,-O1                         # Enable linker optimizations
        -Wl,--as-needed                 # Only link libraries that export symbols used by the binary
        -Wl,--gc-sections               # Remove unused code resulting from -fdata-sections and -function-sections
        )
      list(APPEND CEF_COMPILER_DEFINES
        _FILE_OFFSET_BITS=64            # Allow the Large File Support (LFS) interface to replace the old interface
        )
      list(APPEND CEF_COMPILER_DEFINES_RELEASE
        NDEBUG                          # Not a debug build
        )
    
      include(CheckCCompilerFlag)
      include(CheckCXXCompilerFlag)
    
      CHECK_CXX_COMPILER_FLAG(-Wno-undefined-var-template COMPILER_SUPPORTS_NO_UNDEFINED_VAR_TEMPLATE)
      if(COMPILER_SUPPORTS_NO_UNDEFINED_VAR_TEMPLATE)
        list(APPEND CEF_CXX_COMPILER_FLAGS
          -Wno-undefined-var-template   # Don't warn about potentially uninstantiated static members
          )
      endif()
    
      CHECK_C_COMPILER_FLAG(-Wno-unused-local-typedefs COMPILER_SUPPORTS_NO_UNUSED_LOCAL_TYPEDEFS)
      if(COMPILER_SUPPORTS_NO_UNUSED_LOCAL_TYPEDEFS)
        list(APPEND CEF_C_COMPILER_FLAGS
          -Wno-unused-local-typedefs  # Don't warn about unused local typedefs
          )
      endif()
    
      CHECK_CXX_COMPILER_FLAG(-Wno-literal-suffix COMPILER_SUPPORTS_NO_LITERAL_SUFFIX)
      if(COMPILER_SUPPORTS_NO_LITERAL_SUFFIX)
        list(APPEND CEF_CXX_COMPILER_FLAGS
          -Wno-literal-suffix         # Don't warn about invalid suffixes on literals
          )
      endif()
    
      CHECK_CXX_COMPILER_FLAG(-Wno-narrowing COMPILER_SUPPORTS_NO_NARROWING)
      if(COMPILER_SUPPORTS_NO_NARROWING)
        list(APPEND CEF_CXX_COMPILER_FLAGS
          -Wno-narrowing              # Don't warn about type narrowing
          )
      endif()
    
      if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
        list(APPEND CEF_CXX_COMPILER_FLAGS
          -Wno-attributes             # The cfi-icall attribute is not supported by the GNU C++ compiler
          )
      endif()
    
      if(PROJECT_ARCH STREQUAL "x86_64")
        # 64-bit architecture.
        list(APPEND CEF_COMPILER_FLAGS
          -m64
          -march=x86-64
          )
        list(APPEND CEF_LINKER_FLAGS
          -m64
          )
      elseif(PROJECT_ARCH STREQUAL "x86")
        # 32-bit architecture.
        list(APPEND CEF_COMPILER_FLAGS
          -msse2
          -mfpmath=sse
          -mmmx
          -m32
          )
        list(APPEND CEF_LINKER_FLAGS
          -m32
          )
      endif()
    
      # CEF directory paths.
      set(CEF_RESOURCE_DIR        "${_CEF_ROOT}/Resources")
      set(CEF_BINARY_DIR          "${_CEF_ROOT}/${CMAKE_BUILD_TYPE}")
      set(CEF_BINARY_DIR_DEBUG    "${_CEF_ROOT}/Debug")
      set(CEF_BINARY_DIR_RELEASE  "${_CEF_ROOT}/Release")
    
      # CEF library paths.
      set(CEF_LIB_DEBUG   "${CEF_BINARY_DIR_DEBUG}/libcef.lib")
      set(CEF_LIB_RELEASE "${CEF_BINARY_DIR_RELEASE}/libcef.lib")
    
      # List of CEF binary files.
      set(CEF_BINARY_FILES
        chrome_elf.dll
        d3dcompiler_47.dll
        libcef.dll
        libEGL.dll
        libGLESv2.dll
        snapshot_blob.bin
        v8_context_snapshot.bin
        swiftshader
        )
    
      # List of CEF resource files.
      set(CEF_RESOURCE_FILES
        cef.pak
        cef_100_percent.pak
        cef_200_percent.pak
        cef_extensions.pak
        devtools_resources.pak
        icudtl.dat
        locales
        )
    
      if(USE_SANDBOX)
        list(APPEND CEF_COMPILER_DEFINES
          PSAPI_VERSION=1   # Required by cef_sandbox.lib
          CEF_USE_SANDBOX   # Used by apps to test if the sandbox is enabled
          )
    
        # Libraries required by cef_sandbox.lib.
        set(CEF_SANDBOX_STANDARD_LIBS
          dbghelp.lib
          Delayimp.lib
          PowrProf.lib
          Propsys.lib
          psapi.lib
          SetupAPI.lib
          version.lib
          wbemuuid.lib
          winmm.lib
          )
    
        # CEF sandbox library paths.
        set(CEF_SANDBOX_LIB_DEBUG "${CEF_BINARY_DIR_DEBUG}/cef_sandbox.lib")
        set(CEF_SANDBOX_LIB_RELEASE "${CEF_BINARY_DIR_RELEASE}/cef_sandbox.lib")
      endif()
    endif()
    
  4. 接着我们还需要对 libcef_dll_wrapper 库的源码做一些修改,因为其中为特定平台编写的代码并不适配 MinGW 。
    以下是一个修改内容的补丁示例(不同 CEF3 版本需要修改的内容可能不同,那时读者根据编译时的错误信息进行修改即可):

    点击查看代码
    diff -r -u source/include/base/cef_atomicops.h modified/include/base/cef_atomicops.h
    --- source/include/base/cef_atomicops.h	2020-11-19 12:16:38.000000000 +0800
    +++ modified/include/base/cef_atomicops.h	2021-07-12 19:06:35.142175100 +0800
    @@ -176,7 +176,7 @@
     }  // namespace base
     
     // Include our platform specific implementation.
    -#if defined(OS_WIN) && defined(COMPILER_MSVC) && defined(ARCH_CPU_X86_FAMILY)
    +#if defined(OS_WIN) && (defined(COMPILER_MSVC) || defined(__MINGW32__)) && defined(ARCH_CPU_X86_FAMILY)
     #include "include/base/internal/cef_atomicops_x86_msvc.h"
     #elif defined(OS_WIN) && (defined(__ARM_ARCH_ISA_A64) || defined(_M_ARM64))
     #include "include/base/internal/cef_atomicops_arm64_msvc.h"
    diff -r -u source/include/base/internal/cef_atomicops_x86_msvc.h modified/include/base/internal/cef_atomicops_x86_msvc.h
    --- source/include/base/internal/cef_atomicops_x86_msvc.h	2020-11-19 12:16:38.000000000 +0800
    +++ modified/include/base/internal/cef_atomicops_x86_msvc.h	2021-07-12 19:06:58.852531300 +0800
    @@ -79,7 +79,7 @@
       return Barrier_AtomicIncrement(ptr, increment);
     }
     
    -#if !(defined(_MSC_VER) && _MSC_VER >= 1400)
    +#if !(defined(_MSC_VER) && _MSC_VER >= 1400) && !defined(__MINGW32__)
     #error "We require at least vs2005 for MemoryBarrier"
     #endif
     inline void MemoryBarrier() {
    diff -r -u source/include/internal/cef_export.h modified/include/internal/cef_export.h
    --- source/include/internal/cef_export.h	2020-11-19 12:16:38.000000000 +0800
    +++ modified/include/internal/cef_export.h	2021-07-12 19:08:16.620979400 +0800
    @@ -44,7 +44,7 @@
     #define CEF_EXPORT
     #endif
     
    -#elif defined(COMPILER_GCC)
    +#elif defined(COMPILER_GCC) || defined(__MINGW32__)
     
     #define CEF_EXPORT __attribute__((visibility("default")))
     
    diff -r -u source/include/internal/cef_string_wrappers.h modified/include/internal/cef_string_wrappers.h
    --- source/include/internal/cef_string_wrappers.h	2020-11-19 12:16:38.000000000 +0800
    +++ modified/include/internal/cef_string_wrappers.h	2021-07-12 19:09:30.971232000 +0800
    @@ -34,6 +34,10 @@
     #include <memory.h>
     #include <string>
     
    +#if defined(__MINGW32__)
    +    #include <cstring>
    +#endif
    +
     #include "include/base/cef_string16.h"
     #include "include/internal/cef_string_types.h"
     
    diff -r -u source/libcef_dll/base/cef_logging.cc modified/libcef_dll/base/cef_logging.cc
    --- source/libcef_dll/base/cef_logging.cc	2020-11-19 12:16:38.000000000 +0800
    +++ modified/libcef_dll/base/cef_logging.cc	2021-07-12 19:05:43.059196200 +0800
    @@ -9,7 +9,7 @@
     #include <windows.h>
     #include <algorithm>
     #include <sstream>
    -#elif defined(OS_POSIX)
    +#elif defined(OS_POSIX) || defined(__MINGW32__)
     #include <errno.h>
     #include <stdio.h>
     #include <string.h>   
    
  5. 然后在 CEF3 根目录下创建如下所示的构建脚本 build.bat:

    @echo OFF
    
    mkdir build
    cd build
    
    ::!!!设置你的 Qt MinGW 安装路径至环境变量!!!
    set PATH=D:\IDE\Qt\Qt5.6.3\Tools\mingw492_32\bin;%PATH%
    
    cmake -G "MinGW Makefiles" -DPROJECT_ARCH="x86" -DCMAKE_BUILD_TYPE="Release" ..
    cmake --build ./ --target libcef_dll_wrapper -- -j4
    
    pause
    
  6. 执行脚本,等待构建完成后,读者可在 build\libcef_dll_wrapper 目录下找到 libcef_dll_wrapper 静态库。至此,整个构建工作已完毕。

posted @   邓加领  阅读(1530)  评论(4编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
点击右上角即可分享
微信分享提示