Files
static-php-cli/assets/zh_guide_build-on-windows.md.oogWrPn_.js

23 lines
22 KiB
JavaScript
Raw Normal View History

import{_ as s,c as a,o as e,ah as l}from"./chunks/framework.Bhsyh9kO.js";const k=JSON.parse('{"title":"在 Windows 上构建","description":"","frontmatter":{},"headers":[],"relativePath":"zh/guide/build-on-windows.md","filePath":"zh/guide/build-on-windows.md"}'),t={name:"zh/guide/build-on-windows.md"};function h(n,i,p,o,d,c){return e(),a("div",null,[...i[0]||(i[0]=[l(`<h1 id="在-windows-上构建" tabindex="-1">在 Windows 上构建 <a class="header-anchor" href="#在-windows-上构建" aria-label="Permalink to “在 Windows 上构建”"></a></h1><p>因为 Windows 系统是 NT 内核,与类 Unix 的操作系统使用的编译工具及操作系统接口几乎完全不同,所以在 Windows 上的构建流程会与 Unix 系统有些许不同。</p><h2 id="github-actions-构建" tabindex="-1">GitHub Actions 构建 <a class="header-anchor" href="#github-actions-构建" aria-label="Permalink to “GitHub Actions 构建”"></a></h2><p>现在已支持从 Actions 构建 Windows 版本的 static-php 了。 和 Linux、macOS 一样,你需要先 Fork static-php-cli 仓库到你的 GitHub 账户中,然后你可以进入 <a href="./extensions.html">扩展列表</a> 选择要编译的扩展,然后进入自己仓库的 <code>CI on Windows</code> 选择 PHP 版本、填入扩展列表(逗号分割),点击 Run 即可。</p><p>如果你要在本地开发或构建,请继续向下阅读。</p><h2 id="环境准备" tabindex="-1">环境准备 <a class="header-anchor" href="#环境准备" aria-label="Permalink to “环境准备”"></a></h2><p>在 Windows 上构建静态 PHP 所需要的工具与 PHP 官方的 Windows 构建工具是相同的。你可以阅读 <a href="https://wiki.php.net/internals/windows/stepbystepbuild_sdk_2" target="_blank" rel="noreferrer">官方文档</a>。</p><p>总结下来,你需要以下环境及工具:</p><ul><li>Windows 10需要 build 17063 或以后的更新)</li><li>Visual Studio 2019/2022推荐 2022</li><li>Visual Studio 的 C++ 桌面开发</li><li>Git for Windows</li><li>static-php-cli 仓库</li><li>PHP 和 Composerstatic-php-cli 需要它们,可使用 <code>bin/setup-runtime</code> 自动安装)</li><li><a href="https://github.com/php/php-sdk-binary-tools" target="_blank" rel="noreferrer">php-sdk-binary-tools</a>(可使用 doctor 自动安装)</li><li>strawberry-perl可使用 doctor 自动安装)</li><li>nasm可使用 doctor 自动安装)</li></ul><div class="tip custom-block"><p class="custom-block-title custom-block-title-default">TIP</p><p>static-php-cli 在 Windows 上的构建指的是使用 MSVC 构建 PHP不基于 MinGW、Cygwin、WSL 等环境。</p><p>如果你更倾向使用 WSL请参考在 Linux 上构建的章节。</p></div><p>在安装 Visual Studio 后,选择 C++ 桌面开发的工作负荷后,可能会下载 8GB 左右的编译工具,下载速度取决于你的网络状况。</p><h3 id="安装-git" tabindex="-1">安装 Git <a class="header-anchor" href="#安装-git" aria-label="Permalink to “安装 Git”"></a></h3><p>Git for Windows 可以从 <a href="https://git-scm.com/download/win" target="_blank" rel="noreferrer">这里</a> 下载并安装 <code>Standalone Installer 64-bit</code> 版本,安装在默认位置(<code>C:\\Program Files\\Git\\</code>)。 如果不想手动下载和安装,你也可以使用 Visual Studio Installer在<strong>单个组件</strong>的选择列表中,勾选 Git。</p><h3 id="准备-static-php-cli" tabindex="-1">准备 static-php-cli <a class="header-anchor" href="#准备-static-php-cli" aria-label="Permalink to “准备 static-php-cli”"></a></h3><p>static-php-cli 项目的下载方式很简单,只需要使用 git clone 即可。推荐将项目放在 <code>C:\\spc-build\\</code> 或类似目录,路径最好不要有空格。</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">cd</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> C:</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\\s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">pc-build</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> clone</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> https://github.com/crazywhalecc/static-php-cli.git</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">cd</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> static-php-cli</span></span></code></pre></div><p>static-php-cli PHP PHP Windows PHP static-php-cli 使 <code>bin/setup-runtime</code> PHP Composer</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e;" tabindex="0" dir="ltr"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># PHP Composer ./runtime/ </span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">bin/setup-runtime</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 安装后如需在全局命令中使用 PHP Composer使用下面的命令将 runtime/ 目录添加到 PATH</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">bin/setup-runtime</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -action</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> add-path</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 删除 PATH 中的 runtime/ 目录</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">bin/setup-runtime</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -action</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> remove-path</span></span></code></pre></div><p> PHP Composer 使 <code>composer</code> static-php-cli </p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e;" tabindex="0" dir="ltr"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">cd</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> C:</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\\s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">pc-build</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">\\s</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">tatic-php-cli</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">runtime/composer</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --no-dev</span></span></code></pre></div><h3 id="" tabindex="-1"> <a class="header-anchor" href="#" aria-label="Permalink to "></a></h3><p> <code>php-sdk-binary-tools</code><code>strawberry-perl</code><code>nasm</code>使 <code>bin/spc doctor --auto-fix</code> </p><p> doctor <strong></strong></p><p></p><h3 id="-php-sdk-binary-tools" tabindex="-1"> php-sdk-binary-tools <a class="header-anchor" href="#-php-sdk-binary-tools" aria-label="Permalink to php-sdk-binary-tools"></a></h3><div class="language-bat"><button title="Copy Code" class="copy"></button><span class="lang">bat</span><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e;" tabindex="0" dir="ltr"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">cd</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> C:\\spc-build\\static-php-cli</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">git clone https://github.com/php/php-sdk-binary-tools.git</span></span></code></pre></div><blockquote><p>你也可以在 Windows 设置中设置全局变量 <code>PHP_SDK_PATH</code>,并将该项目克隆至变量对应的路径。一般情况下,默认即可。</p></blockquote><h3 id="手动安装-strawberry-perl" tabindex="-1">手动安装 strawberry-perl <a class="header-anchor" href="#手动安装-strawberry-perl" aria-label="Permalink to “手动安装 strawberry-perl”"></a></h3><blockquote><p>如果你不需要编译 openssl 扩展,可不安装 perl。</p></blockquote><ol><li>从 <a href="https://github.com/StrawberryPerl/Perl-Dist-Strawberry/releases/" target="_blank" rel="noreferrer">GitHub</a> 下载 strawberry-perl 最新版。</li><li>安装到 <code>C:\\spc-build\\static-php-cli\\pkgroot\\perl\\</code> 目录。</li></ol><blockquote><p>你可以下载 <code>-portable</code> 版本,并直接解压到上述目录。 最后的 <code>perl.exe</code> 应该位于 <code>C:\\spc-build\\static-php-cli\\pkgroot\\perl\\perl\\bin\\perl.exe</code>。</p></blockquote><h3 id="手动安装-nasm" tabindex="-1">手动安装 nasm <a class="header-anchor" href="#手动安装-nasm" aria-label="Permalink to “手动安装 nasm”"></a></h3><blockquote><p>如果你不需要编译 openssl 扩展,可不安装 nasm。</p></blockquote><ol><li>从 <a href="https://www.nasm.us/pub/nasm/releasebuilds/" target="_blank" rel="noreferrer">官网</a> 下载 nasm 工具x64。</li><li>将 <code>nasm.exe</code>、<code>ndisasm.exe</code> 放在 <code>C:\\spc-build\\static-php-cli\\php-sdk-binary-tools\\bin\\</code> 目录。</li></ol><h2 id="下载源码" tabindex="-1">下载源码 <a class="header-anchor" href="#下载源码" aria-label="Permalink to “下载源码”"></a></h2><p>见 <a href="./manual-build.html#命令-download-下载依赖包">本地构建 - download</a></p><h2 id="编译-php" tabindex="-1">编译 PHP <a class="header-anchor" href="#编译-php" aria-label="Permalink to “编译 PHP”"></a></h2><p>使用 build 命令可以开始构建静态 php 二进制,在执行 <code>bin/spc build</code> 命令前,务必先使用 <code>download</code> 命令下载资源,建议使用 <code>doctor</code> 检查环境。</p><h3 id="基本用法" tabindex="-1">基本用法 <a class="header-anchor" href="#基本用法" aria-label="Permalink to “基本用法”"></a></h3><p>你需要先到 <a href="./extensions.html">扩展列表</a> 或 <a href="./cli-generator.html">命令生成器</a> 选择你要加入的扩展,然后使用命令 <code>bin/spc build</code> 进行编译。你需要指定编译目标,从如下参数中选择:</p><ul><li><code>--build-cli</code>: 构建一个 cli sapi命令行界面可在命令行执行 PHP 代码)</li><li><code>--build-micro</code>: 构建一个 micro sapi用于构建一个包含 PHP 代码的独立可执行二进制)</li></ul><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e;" tabindex="0" dir="ltr"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 编译 PHP附带 bcmath,openssl,zlib 扩展,编译目标为 cli</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">bin/spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> build</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;bcmath,openssl,zlib&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --build-cli</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 编译 PHP附带 bcmath,openssl,zlib 扩展编译目标为 micro cli</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">bin/spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> build</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;bcmath,openssl,zlib&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --build-micro</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --build-cli</span></span></code></pre></div><div class="warning custom-block"><p class="custom-block-title custom-block-title-default">WARNING</p><p>Windows使 <code>&quot;bcmath,openssl,mbstring&quot;</code></p></div><h3 id="" tabindex="-1"> <a class="header-anchor" href="#" aria-label="Permalink to "></a></h3><p> shell 使 <code>--debug</code> debug </p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e;" tabindex="0" dir="ltr"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">bin/spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> build</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;openssl&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --build-cli</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --debug</span></span></code></pre></div><h3 id="" tabindex="-1"> <a class="header-anchor" href="#" aria-label="Permalink to "></a></h3><p>使</p><ul><li><code>--with-clean</code>: PHP make </li><li><code>--enable-zts</code>: PHP 线 NTS </li><li><code>--with-libs=XXX,YYY</code>: PHP </li><li><code>--with-config-file-scan-dir=XXX</code> <code>php.ini</code> <code>.ini</code> <a href="./../faq/index.html#php-ini-"></a> </li><li><code>-I xxx=yyy</code>: INI PHP <code>--with-hardcoded-ini</code></li><li><code>--with-micro-fake-cli</code>: micro micro SAPI <code>cli</code> <code>PHP_SAPI</code> </li><li><code>--disable-opcache-jit</code>: opcache jit</li><li><code>--without-micro-ext-test</code>: micro.sfx micro.sfx </li><li><code>--with-suggested-exts</code>: <code>ext-suggests</code> </li><li><code>--with-suggested-libs</code>: <code>lib-suggests</code> </li><li><code>--with-upx-pack</code>: 使 UPX 使 <code>bin/spc install-pkg upx</code> upx</li><li><code>--with-micro-logo=XXX.ico</code>: micro <code>exe</code> <code>.ico</code></li></ul><p> INI <code>memory_limit</code> <code>system</code> </p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e;" tabindex="0" dir="ltr"><code><span class="line"><span
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">bin/spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> micro:combine</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> hello.php</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Then we got \`my-app.exe\` with custom logo!</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">my-app.exe</span></span></code></pre></div><h2 id="使-php-exe" tabindex="-1">使 php.exe <a class="header-anchor" href="#使-php-exe" aria-label="Permalink to 使 php.exe"></a></h2><p>php.exe <code>buildroot\\bin\\</code> 使</p><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e;" tabindex="0" dir="ltr"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">.</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">\\</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">php</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -v</span></span></code></pre></div><h2 id="使-micro" tabindex="-1">使 micro <a class="header-anchor" href="#使-micro" aria-label="Permalink to 使 micro"></a></h2><blockquote><p>phpmicro PHP phpmicro <a href="https://github.com/dixyes/phpmicro" target="_blank" rel="noreferrer">dixyes/phpmicro</a></p></blockquote><p> <code>./micro.sfx</code> PHP 使 <code>buildroot/bin/</code> </p><p>使 PHP Phar </p><blockquote><p> phar phar </p></blockquote><div class="language-shell"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e;" tabindex="0" dir="ltr"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># code.php &quot;&lt;?php echo &#39;Hello world&#39; . PHP_EOL;&quot;</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">bin/spc</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> micro:combine</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> code.php</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -O</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my-app.exe</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Run it!!! Copy it to another computer!!!</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">./my-app.exe</span></span></code></pre></div><p> PHAR code.php phar 使 <a href="https://github.com/box-project/box" target="_blank" rel="noreferrer">box-project/box</a> CLI Phar phpmicro </p><p> <code>micro:combine</code> Unix <a href="./manual-build.html"></a></p>`,64)])])}const b=s(t,[["render",h]]);export{k as __pageData,b as default};