dunglas / frankenphp

🧟 The modern PHP app server
https://frankenphp.dev
MIT License
6.75k stars 227 forks source link

[test] about franken on windows #880

Open TenHian opened 3 months ago

TenHian commented 3 months ago

Describe you feature request

I've build a frankenphp.exe on windows

After modifying some code in php-src and some code in c-thread-pool and some code in frankenphp, I came up with a windows version of minimal frankenphp(barely tested). It still uses dynamic links now. Maybe in the future I'll modify it to make it static (larger change). It now does not contain any php extensions, only the core functionality.

Barely tested

I ran some basic tests on the php-cli, like some scripts that only use the core extension. And ran phpinfo on php-server. However these tests are minimal. So I recalled php-src/run-tests.php.

Hassles

As comprehensive as these tests are, they're not ready to run straight away, and it looks like I'm going to have to make some modifications to it(frankenphp php-cli is different from php.exe). So I read the script. Then it tells me at line 2300+, "OK, we've taken care of all the pre-conditions, now we should run the tests!". When I reached this point, I muttered to myself, "Wait, buddy, what were the prerequisites we dealt with before?". This script is pretty good, honestly. But its two big big functions and numerous pre-conditions made me dizzy. I'd even like to see what it those .phpt have to test and write a script myself. This will less suffering.

And more

83 #420

Now I understand why some people give up after trying for a while.

End

I need to take a nap for a while. Anyway, I will persist on it. If you have any ideas about testing franken's integrity on Windows, kick me.

wwccss commented 3 months ago

Well done. I think you can upload a binary package thus we can test it.

dunglas commented 3 months ago

Thank you very much for working on this! Could you give us the steps you followed?

TenHian commented 3 months ago

Well done. I think you can upload a binary package thus we can test it.

https://drive.google.com/drive/folders/12niHlETBT_Inb3XE2QKkoRdro_7YpTIh

The demo product is larger than 25MB, so I put it on google drive first. It also need to make libwinpthread-1.dll in the same dir of frankenphp.exe, which could be find in \mingw64\bin after msys2 installed.

TenHian commented 3 months ago

Thank you very much for working on this! Could you give us the steps you followed?

It takes a while to organize the code, and right now it's implemented in an ugly way. And it relies on the following to run up.

  1. The mingw-w64 gcc is able to use the .dll compiled by MSVC. In practice, functions that follow the calling conventions __cdecl __stdcall __fastcall can be called. And functions that follow __thiscall __vectorcall can not be called. The amount of code that needs to be modified is greatly reduced because of this.
  2. Alter Makefile and small portion of SAPI embed code to get php8embed.dll.
  3. Remove or replace some MSVC unique features, like __forceinline __assume, etc.
  4. Handling OS differences, for example SIGUSR1 is used in thpool_pause in C-Thread-Pool, but mingw-w64 gcc doesn't support this, thanks to franken doesn't need it. Other cases will be more complicated.
  5. Modify the flags passed to cgo.

The exact steps and code come later.

mudevai commented 3 months ago

Could building against https://github.com/jart/cosmopolitan be helpful in this scenario?

TenHian commented 3 months ago

Could building against https://github.com/jart/cosmopolitan be helpful in this scenario?

Cool project. I'll see if it bridges to cgo, after I port run-tests.php to frankenphp. And I'll let you know if I make any progress on it.

TenHian commented 2 months ago

Build frankenphp on windows

Build tool-chain

Make sure that you have a development environment could make php source build complete(buildconf & configure & nmake), by follow php windows build manal([PHP: internals:windows:stepbystepbuild_sdk_2](https://wiki.php.net/internals/windows/stepbystepbuild_sdk_2) and [php SDK README](https://github.com/php/php-sdk-binary-tools/blob/master/README.md)). You coud check it work by next few commands.

# CMD that in php SDK environment
cd php-src\x64\Release_TS
php -i 

How I setup php SDK

Install visual studio 2022

You can also install visual studio 2019 to follow php windows build manal strictly.

If not, install vs2022, make sure you follow [php SDK README #Requirements](https://github.com/php/php-sdk-binary-tools/blob/master/README.md#Requirements), install the module that needed. Next, install the other tools mention by [php SDK README ##Other tools](https://github.com/php/php-sdk-binary-tools/blob/master/README.md#other-tools). Most of them could installed by package manager, and few of them need to install by hand.

Set up php SDK

# recommand use powershell, the windows default one.
git clone https://github.com/php/php-sdk-binary-tools.git c:\php-sdk
cd C:\php-sdk
git checkout php-sdk-2.2.0
phpsdk-vs16-x64.bat
# now in CMD that in php SDK environment, after execute .bat above.
# CMD that in php SDK environment
phpsdk_buildtree phpdev
# now in C:\php-sdk\phpdev\vs16\x64
git clone https://github.com/php/php-src.git
cd php-src
phpsdk_deps -b 8.3 -u
git checkout php-8.3.7
git checkout -b d8.3.7
buildconf
configure
nmake
cd x64\Release_TS
php -i
# now in C:\php-sdk\phpdev\vs16\x64
git clone https://github.com/php/php-src.git
cd php-src
git checkout php-8.3.7
git checkout -b d8.3.7
buildconf
configure
nmake
cd x64\Release_TS
php -i

If its output like it should to be, you PHP SDK is complete.

Build php libs and headers

If you follow the step ##How I setup php SDK, you need clean repository.

# CMD that in php SDK environment
cd C:\php-sdk\phpdev\vs16\x64\php-src
nmake clean-all

Clone fputils and apply php-patch

# CMD that in php SDK environment
cd
git clone https://github.com/TenHian/fputils.git
cd php-src
cp -r C:\fputils\php-patch ./
git apply --stat php-patch\*.patch
git apply --check php-patch\*.patch
git am --abort
git am php-patch\*.patch
git log

configure, nmake, nmake build-devel

# CMD that in php SDK environment
# if you first to build run `buildconf`
# in C:\php-sdk\phpdev\vs16\x64\php-src
configure --disable-all --enable-zts --enable-embed --enable-cli --disable-opcache-jit --without-pcre-jit --enable-session --with-mysqlnd --enable-pdo --with-pdo-mysql
# I wanted to write this paragraph as a bat script, but .bat is shit.
# Makefile path in this command is a example
cd C:\fputils
frankenphp.exe php-cli modify-makefile.php "C:\php-sdk\phpdev\vs16\x64\php-src\Makefile"
nmake
nmake build-devel

We need php8ts.dll php8embed.dll in php-src\x64\Release_TS and headers under php-src\x64\Release_TS\php-8.3.7-devel-vs16-x64\include .

And you could also do some checks on those dlls, ensure that the symbols what we need is exported.

# CMD that in php SDK environment
cd C:\php-sdk\phpdev\vs16\x64\php-src\x64\Release_TS
dumpbin /exports php8ts.dll
dumpbin /exports php8embed.dll

Prepare Msys2 environment

Click [msys2-x86_64-20240507.exe](https://github.com/msys2/msys2-installer/releases/download/2024-05-07/msys2-x86_64-20240507.exe) dowload and install it. We use environment MINGW64/MSYS2.

Install basic tools

# mingw64/msys2 bash
pacman -S git
pacman -S vim
pacman -S make
pacman -S wget

Install gcc

# mingw64/msys2 bash
pacman -U https://repo.msys2.org/mingw/mingw64/mingw-w64-x86_64-gcc-13.2.0-6-any.pkg.tar.zst

Setup go

# mingw64/msys2 bash
cd
mv /c/fputils/go /mingw64/lib
cp /c/fputils/go/bin/*.exe /mingw64/bin

Set go environment variables

# mingw64/msys2 bash
vim ~/.bashrc

Add the following two lines to your .bashrc.

# ~/.bashrc
export GOROOT=/mingw64/lib/go
export GOPATH=C:\\Users\\your user name\\go

then

# mingw64/msys2 bash
source ~/.bashrc

If you feel some go env is wrong, just modify it adapt to your environment. Mine blow:

set GO111MODULE=on
set GOARCH=amd64
set GOBIN=C:\Users\admin\go\bin
set GOCACHE=C:\Users\admin\AppData\Local\go-build
set GOENV=C:\Users\admin\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\admin\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\admin\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:/msys64/mingw64/lib/go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\msys64\mingw64\lib\go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.2
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\msys64\tmp\go-build3468928067=/tmp/go-build -gno-record-gcc-switches

Install brotli

# mingw64/msys2 bash
pacman -S mingw-w64-x86_64-brotli

Frankenphp dependencies

Set libs

Copy your php8ts.dll and php8embed.dll into /usr/local/lib , my php-src path is "C:\php-sdk\phpdev\vs16\x64\php-src", I should execute:

# migw64/msys2 bash
cp /c/php-sdk/phpdev/vs16/x64/php-src/x64/Release_TS/*.dll /usr/local/lib

Copy your libbrotlicommon.a libbrotlidec.a libbrotlienc.a to /usr/local/lib, my command is:

# mingww64/msys2 bash
cp /mingw64/lib/libbrotlicommon.a /mingw64/lib/libbrotlidec.a /mingw64/lib/libbrotlienc.a /usr/local/lib

Set headers

Copy all things that under your php-src/x64/Release_TS/php-8.3.7-devel-vs16-x64/inlude to /usr/local/include/php, my command is:

# mingww64/msys2 bash
cp /c/php-sdk/phpdev/vs16/x64/php-src/x64/Release_TS/php-8.3.7-devel-vs16-x64/include/* /usr/local/include/php

then modify /usr/local/include/php/main/php.h

// orign line 95
typedef int pid_t;
// modify it to
typedef long long pid_t;

Prepare and build frankenphp

Clone and make new branch

# mingw64/msys2 bash
cd
git clone https://github.com/dunglas/frankenphp.git
cd frankenphp
git checkout v1.1.5
git checkout -b d1.1.5

Patch

# mingw64/msys2 bash
# in ~/frankenphp
cp -r /c/fputils/fp-patch ./
git am --abort
git am fp-patch/*.patch

Build

If your msys2 has installed in default path, and you followed my step, now you can build.

# mingw64/msys2 bash
cd ~/frankenphp
cd caddy/frankenphp
make build
# for clean
make clean

If not, like you installed msys2 in some where other, check the ~/frankenphp/caddy/frankenphp/Makefile.

Run demo

Ok, we got frankenphp built, now we could run some demo.

Phpinfo

Time for a classic session.

Copy you frankenphp.exe to a new path. And copy php8ts.dll php8embed.dll. Like this:

# mingw64/msys2 bash
tree phpinfo/

output:

phpinfo
β”œβ”€β”€ frankenphp.exe
β”œβ”€β”€ index.php
β”œβ”€β”€ php8embed.dll
└── php8ts.dll

index.php has phpinfo. Then run server:

# mingw64/msys2 bash
./frankenphp php-server

Now access 127.0.0.1:80 .

Adminer

Adminer is an opensource, singal file, mutli-db-supported db manager. Its nice to use it as a demo.

# mingw64/msys2 bash
wget https://github.com/vrana/adminer/releases/download/v4.8.1/adminer-4.8.1.php

If you follow steps above, the exts we have now are just right for running adminer. And steps same to ##phpinfo, just rename adminer.php to index.php.

Zentao PMS

Zentao PMS is an opensource project management software, a lit bit more complicated than adminer. Its also need more php extensions, this section want to make frankenphp on windows to server bigger project.

Rebuild php-src

We need more php exts to supprot zentao pms, so rebuild php-src.

If you have closed terminal that build php-src, you should inital it again.

cd C:\php-sdk
phpsdk-vs16-x64.bat
# now in CMD that in php SDK environment, after execute .bat above.
cd phpdev\vs16\x64\php-src
nmake clean-all
configure --disable-all --enable-zts --enable-embed --enable-cli --disable-opcache-jit --without-pcre-jit --enable-session --with-mysqlnd --enable-pdo --with-pdo-mysql --enable-filter --enable-mbstring --enable-zlib --with-gd --with-iconv --with-openssl --with-curl --enable-ctype
cd c:\fputils
frankenphp.exe php-cli modify-makefile.php "C:\php-sdk\phpdev\vs16\x64\php-src\Makefile"
nmake
nmake build-devel

Then, follow the section #Frankenphp dependencies.

Rebuild frankenphp

# mingw64/msys2 bash
cd ~/frankenphp/caddy/frankenphp
make clean
make build

Run Zentao PMS

# mingw64/msys2 bash
mkdir ~/zentao
cd zentao
wget https://github.com/easysoft/zentaopms/archive/refs/tags/zentaopms_18.12.tar.gz
tar -zxvf zentaopms_18.12.tar.gz
cp ~/frankenphp/caddy/frankenphp/frankenphp.exe ./
cp /usr/local/lib/*.dll ./
# copy dll that need by php8ts.dll
cp /c/php-sdk/phpdev/vs16/x64/deps/bin/libcrypto-3-x64.dll ./
cp /c/php-sdk/phpdev/vs16/x64/deps/bin/libssh2.dll ./
cp /c/php-sdk/phpdev/vs16/x64/deps/bin/libssl-3-x64.dll ./
cp /c/php-sdk/phpdev/vs16/x64/deps/bin/nghttp2.dll ./
# copy extension php_openssl
mkdir ext
cp /c/php-sdk/phpdev/vs16/x64/php-src/x64/Release_TS/php_openssl.dll ./ext/
vim php.ini
# >> add 2 line
# extension_dir="C:\msys64\home\*your username*\zentao\ext"
# extension=php_openssl.dll
# >> 2 line end
# run zentao pms
./frankenphp.exe php-server --root zentaopms/www

Now you will see install page.

Test

I've modified the php-src/run-tests.php to fit frankenphp, like frankenphp no need to run sapi tests. So far I've only tested frankenphp with no ext. Even so, I didn't modify run-tests.php to the point where it adapts perfectly to frankenphp, there still some tests failed but not frankenphp's problem. Modify run-tests.php is a boring job.

Rebuild php with no ext

If you have closed terminal that build php-src, you should inital it again.

cd C:\php-sdk
phpsdk-vs16-x64.bat
# now in CMD that in php SDK environment, after execute .bat above.
cd phpdev\vs16\x64\php-src
nmake clean-all
configure --disable-all --enable-zts --enable-embed --enable-cli --disable-opcache-jit --without-pcre-jit
cd c:\fputils
frankenphp.exe php-cli modify-makefile.php "C:\php-sdk\phpdev\vs16\x64\php-src\Makefile"
nmake
nmake build-devel

Then, follow the section #Frankenphp dependencies.

Rebuild frankenphp

# mingw64/msys2 bash
cd ~/frankenphp/caddy/frankenphp
make clean
make build

Copy files

Copy frankenphp.exe php8ts.dll php8embed.dll into php-src

# mingw64/msys2 bash
cp ~/frankenphp/caddy/frankenphp/frankenphp.exe /c/php-sdk/phpdev/vs16/x64/php-src
cp /c/php-sdk/phpdev/vs16/x64/php-src/x64/Release_TS/*.dll /c/php-sdk/phpdev/vs16/x64/php-src

Make patch

If follow section #(Build php libs and headers) you already use all patches. If not the patch that modify is 0002-modify-run-tests.php.patch .

Run-tests

# mingw64/msys2 bash
cd /c/php-sdk/phpdev/vs16/x64/php-src
./frankenphp php-cli run-tests.php

I remember 89% passed.