en_guide_build-on-windows.md.0XosmiyO.js 22 KB

1234567891011121314151617181920212223
  1. import{_ as e,c as i,o as s,a1 as a}from"./chunks/framework.gjrnbxUT.js";const m=JSON.parse('{"title":"Build on Windows","description":"","frontmatter":{},"headers":[],"relativePath":"en/guide/build-on-windows.md","filePath":"en/guide/build-on-windows.md"}'),t={name:"en/guide/build-on-windows.md"},o=a(`<h1 id="build-on-windows" tabindex="-1">Build on Windows <a class="header-anchor" href="#build-on-windows" aria-label="Permalink to &quot;Build on Windows&quot;">​</a></h1><p>Because the Windows system is an NT kernel, the compilation tools and operating system interfaces used by Unix-like operating systems are almost completely different, so the build process on Windows will be slightly different from that of Unix systems.</p><h2 id="github-actions-build" tabindex="-1">GitHub Actions Build <a class="header-anchor" href="#github-actions-build" aria-label="Permalink to &quot;GitHub Actions Build&quot;">​</a></h2><p>Building the Windows version of static-php from Actions is now supported. Like Linux and macOS, you need to Fork the static-php-cli repository to your GitHub account first, then you can enter <a href="./extensions.html">Extension List</a> to select the extension to be compiled, and then go to your own <code>CI on Windows</code> select the PHP version, fill in the extension list (comma separated), and click Run.</p><p>If you&#39;re going to develop or build locally, please read on.</p><h2 id="requirements" tabindex="-1">Requirements <a class="header-anchor" href="#requirements" aria-label="Permalink to &quot;Requirements&quot;">​</a></h2><p>The tools required to build static PHP on Windows are the same as PHP&#39;s official Windows build tools. You can read <a href="https://wiki.php.net/internals/windows/stepbystepbuild_sdk_2" target="_blank" rel="noreferrer">Official Documentation</a>.</p><p>To sum up, you need the following environment and tools:</p><ul><li>Windows 10/11 (requires build 17063 or later)</li><li>Visual Studio 2019/2022 (recommended 2022)</li><li>C++ desktop development for Visual Studio</li><li>Git for Windows</li><li><a href="https://github.com/php/php-sdk-binary-tools" target="_blank" rel="noreferrer">php-sdk-binary-tools</a> (can be installed automatically using doctor)</li><li>strawberry-perl (can be installed automatically using doctor)</li><li>nasm (can be installed automatically using doctor)</li></ul><div class="tip custom-block"><p class="custom-block-title">TIP</p><p>The construction of static-php-cli on Windows refers to using MSVC to build PHP and is not based on MinGW, Cygwin, WSL and other environments.</p><p>If you prefer to use WSL, please refer to the chapter on Building on Linux.</p></div><p>After installing Visual Studio and selecting the C++ desktop development workload, you may download about 8GB of compilation tools, and the download speed depends on your network conditions.</p><h3 id="install-git" tabindex="-1">Install Git <a class="header-anchor" href="#install-git" aria-label="Permalink to &quot;Install Git&quot;">​</a></h3><p>Git for Windows can be downloaded and installed from <a href="https://git-scm.com/download/win" target="_blank" rel="noreferrer">here</a> <code>Standalone Installer 64-bit</code> version, installed in the default location (<code>C:\\Program Files\\Git\\</code>). If you don&#39;t want to download and install manually, you can also use Visual Studio Installer and check Git in the <strong>Individual component</strong> tab.</p><h3 id="prepare-static-php-cli" tabindex="-1">Prepare static-php-cli <a class="header-anchor" href="#prepare-static-php-cli" aria-label="Permalink to &quot;Prepare static-php-cli&quot;">​</a></h3><p>Downloading the static-php-cli project is very simple, just use git clone. It is recommended to place the project in <code>C:\\spc-build\\</code> or a similar directory. It is best <strong>not to have spaces in the path</strong>.</p><div class="language-shell vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">mkdir</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;C:\\spc-build&quot;</span></span>
  2. <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>
  3. <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>
  4. <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>It is a bit strange that static-php-cli itself requires a PHP environment, but now you can quickly install the PHP environment through a script. Generally, your computer will not have the Windows version of PHP installed, so we recommend that you use <code>bin/setup-runtime</code> directly after downloading static-php-cli to install PHP and Composer in the current directory.</p><div class="language-shell vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Install PHP and Composer to the ./runtime/ directory</span></span>
  5. <span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">bin/setup-runtime</span></span>
  6. <span class="line"></span>
  7. <span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># After installation, if you need to use PHP and Composer in global commands, </span></span>
  8. <span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># use the following command to add the runtime/ directory to PATH</span></span>
  9. <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>
  10. <span class="line"></span>
  11. <span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Delete the runtime/ directory in PATH</span></span>
  12. <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>Finally, now that you have PHP and Composer installed, you need to install static-php-cli&#39;s Composer dependencies:</p><div class="language-shell vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">composer</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span></span></code></pre></div><h3 id="install-other-tools-automatic" tabindex="-1">Install other Tools (automatic) <a class="header-anchor" href="#install-other-tools-automatic" aria-label="Permalink to &quot;Install other Tools (automatic)&quot;">​</a></h3><p>For <code>php-sdk-binary-tools</code>, <code>strawberry-perl</code>, and <code>nasm</code>, we recommend that you directly use the command <code>bin/spc doctor</code> to check and install them.</p><p>If doctor successfully installs automatically, please <strong>skip</strong> the steps below to manually install the above tools.</p><p>But if the automatic installation fails, please refer to the manual installation method below.</p><h3 id="install-php-sdk-binary-tools-manual" tabindex="-1">Install php-sdk-binary-tools (manual) <a class="header-anchor" href="#install-php-sdk-binary-tools-manual" aria-label="Permalink to &quot;Install php-sdk-binary-tools (manual)&quot;">​</a></h3><div class="language-shell vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><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>
  13. <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/php/php-sdk-binary-tools.git</span></span></code></pre></div><blockquote><p>You can also set the global variable <code>PHP_SDK_PATH</code> in Windows settings and clone the project to the path corresponding to the variable. Under normal circumstances, you don&#39;t need to change it.</p></blockquote><h3 id="install-strawberry-perl-manual" tabindex="-1">Install strawberry-perl (manual) <a class="header-anchor" href="#install-strawberry-perl-manual" aria-label="Permalink to &quot;Install strawberry-perl (manual)&quot;">​</a></h3><blockquote><p>If you don&#39;t need to compile the openssl extension, you don&#39;t need to install perl.</p></blockquote><ol><li>Download the latest version of strawberry-perl from <a href="https://github.com/StrawberryPerl/Perl-Dist-Strawberry/releases/" target="_blank" rel="noreferrer">GitHub</a>.</li><li>Install to the <code>C:\\spc-build\\static-php-cli\\pkgroot\\perl\\</code> directory.</li></ol><blockquote><p>You can download the <code>-portable</code> version and extract it directly to the above directory. The last <code>perl.exe</code> should be located at <code>C:\\spc-build\\static-php-cli\\pkgroot\\perl\\perl\\bin\\perl.exe</code>.</p></blockquote><h3 id="install-nasm-manual" tabindex="-1">Install nasm (manual) <a class="header-anchor" href="#install-nasm-manual" aria-label="Permalink to &quot;Install nasm (manual)&quot;">​</a></h3><blockquote><p>If you don&#39;t need to compile openssl extension, you don&#39;t need to install nasm.</p></blockquote><ol><li>Download the nasm tool (x64) from <a href="https://www.nasm.us/pub/nasm/releasebuilds/" target="_blank" rel="noreferrer">official website</a>.</li><li>Place <code>nasm.exe</code> and <code>ndisasm.exe</code> in the <code>C:\\spc-build\\static-php-cli\\php-sdk-binary-tools\\bin\\</code> directory.</li></ol><h2 id="download-required-sources" tabindex="-1">Download required sources <a class="header-anchor" href="#download-required-sources" aria-label="Permalink to &quot;Download required sources&quot;">​</a></h2><p>Same as <a href="./manual-build.html#command-download">Manual build - Download</a></p><h2 id="build-php" tabindex="-1">Build PHP <a class="header-anchor" href="#build-php" aria-label="Permalink to &quot;Build PHP&quot;">​</a></h2><p>Use the build command to start building the static php binary. Before executing the <code>bin/spc build</code> command, be sure to use the <code>download</code> command to download sources. It is recommended to use <code>doctor</code> to check the environment.</p><h3 id="build-sapi" tabindex="-1">Build SAPI <a class="header-anchor" href="#build-sapi" aria-label="Permalink to &quot;Build SAPI&quot;">​</a></h3><p>You need to go to <a href="./extensions.html">Extension List</a> or <a href="./cli-generator.html">Command Generator</a> to select the extension you want to add, and then use the command <code>bin/spc build</code> to compile. You need to specify targets, choose from the following parameters (at least one):</p><ul><li><code>--build-cli</code>: Build a cli sapi (command line interface, which can execute PHP code on the command line)</li><li><code>--build-micro</code>: Build a micro sapi (used to build a standalone executable binary containing PHP code)</li></ul><div class="language-shell vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Compile PHP with bcmath,openssl,zlib extensions, the compilation target is cli</span></span>
  14. <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>
  15. <span class="line"></span>
  16. <span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Compile PHP with phar,curl,posix,pcntl,tokenizer extensions, compile target is micro and cli</span></span>
  17. <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">WARNING</p><p>In Windows, it is best to use double quotes to wrap parameters containing commas, such as <code>&quot;bcmath,openssl,mbstring&quot;</code>.</p></div><h3 id="debug" tabindex="-1">Debug <a class="header-anchor" href="#debug" aria-label="Permalink to &quot;Debug&quot;">​</a></h3><p>If you encounter problems during the compilation process, or want to view each executing shell command, you can use <code>--debug</code> to enable debug mode and view all terminal logs:</p><div class="language-shell vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><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="build-options" tabindex="-1">Build Options <a class="header-anchor" href="#build-options" aria-label="Permalink to &quot;Build Options&quot;">​</a></h3><p>During the compilation process, in some special cases, the compiler and the content of the compilation directory need to be intervened. You can try to use the following commands:</p><ul><li><code>--with-clean</code>: clean up old make files before compiling PHP</li><li><code>--enable-zts</code>: Make compiled PHP thread-safe version (default is NTS version)</li><li><code>--with-libs=XXX,YYY</code>: Compile the specified dependent library before compiling PHP, and activate some extension optional functions</li><li><code>--with-config-file-scan-dir=XXX</code>: Set the directory to scan for <code>.ini</code> files after reading <code>php.ini</code> (Check <a href="./../faq/index.html#what-is-the-path-of-php-ini">here</a> for default paths)</li><li><code>-I xxx=yyy</code>: Hard compile INI options into PHP before compiling (support multiple options, alias is <code>--with-hardcoded-ini</code>)</li><li><code>--with-micro-fake-cli</code>: When compiling micro, let micro&#39;s <code>PHP_SAPI</code> pretend to be <code>cli</code> (for compatibility with some programs that check <code>PHP_SAPI</code>)</li><li><code>--disable-opcache-jit</code>: Disable opcache jit (enabled by default)</li><li><code>--without-micro-ext-test</code>: After building micro.sfx, do not test the running results of different extensions in micro.sfx</li><li><code>--with-suggested-exts</code>: Add <code>ext-suggests</code> as dependencies when compiling</li><li><code>--with-suggested-libs</code>: Add <code>lib-suggests</code> as dependencies when compiling</li><li><code>--with-upx-pack</code>: Use UPX to reduce the size of the binary file after compilation (you need to use <code>bin/spc install-pkg upx</code> to install upx first)</li><li><code>--with-micro-logo=XXX.ico</code>: Customize the icon of the <code>exe</code> executable file after customizing the micro build (in the format of <code>.ico</code>)</li></ul><p>Here is a simple example where we preset a larger <code>memory_limit</code> and disable the <code>system</code> function:</p><div class="language-shell vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><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;bcmath,openssl&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --build-cli</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -I</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;memory_limit=4G&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -I</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;disable_functions=system&quot;</span></span></code></pre></div><p>Another example: Customize our hello-world.exe program logo:</p><div class="language-shell vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><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;ffi,bcmath&quot;</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --build-micro</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --with-micro-logo=mylogo.ico</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --debug</span></span>
  18. <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>
  19. <span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Then we got \`my-app.exe\` with custom logo!</span></span>
  20. <span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">my-app.exe</span></span></code></pre></div><h2 id="use-php-exe" tabindex="-1">Use php.exe <a class="header-anchor" href="#use-php-exe" aria-label="Permalink to &quot;Use php.exe&quot;">​</a></h2><p>After php.exe is compiled, it is located in the <code>buildroot\\bin\\</code> directory. You can copy it to any location for use.</p><div class="language-shell vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><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="use-micro-sfx" tabindex="-1">Use micro.sfx <a class="header-anchor" href="#use-micro-sfx" aria-label="Permalink to &quot;Use micro.sfx&quot;">​</a></h2><blockquote><p>phpmicro is a SelF-extracted eXecutable SAPI module, provided by <a href="https://github.com/dixyes/phpmicro" target="_blank" rel="noreferrer">phpmicro</a> project. But this project is using a <a href="https://github.com/static-php/phpmicro" target="_blank" rel="noreferrer">fork</a> of phpmicro, because we need to add some features to it. It can put php runtime and your source code together.</p></blockquote><p>The final compilation result will output a file named <code>./micro.sfx</code>, which needs to be used with your PHP source code like <code>code.php</code>. This file will be located in the path <code>buildroot/bin/micro.sfx</code>.</p><p>Prepare your project source code, which can be a single PHP file or a Phar file, for use.</p><blockquote><p>If you want to combine phar files, you must add <code>phar</code> extension when compiling!</p></blockquote><div class="language-shell vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">shell</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><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>
  21. <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>
  22. <span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Run it!!! Copy it to another computer!!!</span></span>
  23. <span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">./my-app.exe</span></span></code></pre></div><p>If you package a PHAR file, just replace <code>code.php</code> with the phar file path. You can use <a href="https://github.com/box-project/box" target="_blank" rel="noreferrer">box-project/box</a> to package your CLI project as Phar, It is then combined with phpmicro to produce a standalone executable binary.</p><p>For more details on the <code>micro:combine</code> command, refer to <a href="./manual-build.html">command</a> on Unix systems.</p>`,64),l=[o];function n(p,d,h,c,r,u){return s(),i("div",null,l)}const b=e(t,[["render",n]]);export{m as __pageData,b as default};