windows下编译pdfium并去掉额外的依赖

此流程参考了网上其他教程及bblanchon的工程,当前流程截至2023/3/3有效

1、提前安装好工具链(SDK版本请使用pdfium代码里面指定的版本)

VS2017 + Win10 SDK 10.0.20348 + Git for windows + tortoisegit + 代理

2、下载depot_tools

命令行中设置环境变量:

set http_proxy=http://127.0.0.1:1080

set https_proxy=http://127.0.0.1:1080 (注意是http)

# 设置git代理,如果有验证请参考其他教程填用户和密码,特殊字符转换为%+ascii码

git config --global http.proxy http://127.0.0.1:1080

git config --global https.proxy http://127.0.0.1:1080

下载 depot_tools 源码; 
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
下载完成后,将下载的 depot_tools 源码目录添加系统环境变量之中,并且添加以下环境变量:

vs2017_install=C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise

命令行进入depot_tools\\bootstrap目录,执行win_tools.bat自动下载编译工具。

注意:每次打开一个cmd执行命令时务必设置好环境变量及代理。

3、下载 PDFium 源码;

额外再设置环境变量:

set DEPOT_TOOLS_WIN_TOOLCHAIN=0

set GYP_MSVS_VERSION=2017

set GYP_MSVS_OVERRIDE_PATH=C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise

下面两种命令2选一执行:
1)gclient config --unmanaged https://pdfium.googlesource.com/pdfium.git  (此命令会下载完整的代码仓库,很大)

这个过程中会报proxy_compiler is not running,不用管。
gclient sync

2)gclient config --unmanaged https://pdfium.googlesource.com/pdfium.git --custom-var=checkout_configuration=minimal (只下载主工程,不包括V8,代码量小很多)

gclient sync -r "origin/chromium/5619" --no-history --shallow (分支名自己填,可以用origin/master)

4、编译

进入 PDFium 源码目录,
在命令行上输入:gn args out/x64,此时会弹出编译选项文本,要你输入。不输入默认生成 x64、动态库版本
在命令行上输入:ninja -C out/x64 pdfium,进行编译。
编译过程会根据编译选项不同,花费的时间不同。

部分编译选项:

#是否启用 goma 支持
use_goma = false

# 是否编译为 Chrome 插件
clang_use_chrome_plugins = false

#是否进行编译测试
pdf_is_standalone = true

#是否启用 skia 支持
pdf_use_skia = false

#true 编译为 debug 版本,false 编译为 release 版本
is_debug = false

#true 编译为动态库,false 编译为静态库
is_component_build = true

#编译为一个独立的静态库(is_component_build 必须为 false)
#pdf_is_complete_lib 为 false 时, 编译为多个静态库,true 编译为一个独立的静态库
pdf_is_complete_lib = false

#xfa 支持
pdf_enable_xfa = false

#v8 支持;启用 v8 后,编译时间会增加
pdf_enable_v8 = false

#cpu 架构;x86、x64 可选
target_cpu = "x86"

#true 将用 clang 进行编译,false 将用 VS2017 编译
is_clang = true

5、编译为单独的pdfium.dll

用上面参数编译出来的pdfium.dll依赖了absl.dll,partition_alloc.dll,libc++.dll等众多库,使用时必须一起拷贝非常不便,并且在win7上使用还有点问题。

我们可以参考bblanchon将其编译为单独的pdfium.dll,使用时只拷贝这一个dll即可。

重点在于如下2个步骤:

1)参考public_headers.patch去掉public/fpdfview.h里面的COMPONENT_BUILD宏:

-#if defined(COMPONENT_BUILD)
-// FPDF_EXPORT should be consistent with |export| in the pdfium_fuzzer
-// template in testing/fuzzers/BUILD.gn.

-#else
-#define FPDF_EXPORT
-#endif // defined(COMPONENT_BUILD)

2)参考shared_library.patch将BUILD.gn修改为:

-component("pdfium") {
+shared_library("pdfium") {

修改完毕后再次进行编译,就生成了去掉多余依赖的pdfium.dll

我使用的编译参数args.gn如下:

is_component_build = false
is_debug = false
pdf_enable_v8 = false
pdf_enable_xfa = false
pdf_is_standalone = true
target_cpu = "x86"
target_os = "win"
treat_warnings_as_errors = false

6、将其编译为静态库进行链接使用

不要应用5.1/5.2这两个修改,将上面的参数pdf_is_complete_lib修改为true,编译出pdfium.lib

然后使用如下命令生成libc++.lib

"C:\path\to\pdfium\third_party\llvm-build\Release+Asserts\bin\lld-link.exe" /lib /OUT:libc++.lib /nologo /WX /ignore:4221 C:\out_dir\obj\buildtools\third_party\libc++\libc++\*.obj

在工程里链接pdfium.lib和libc++.lib即可。

posted @ 2023-02-21 19:35  aij  阅读(1573)  评论(3编辑  收藏  举报