Files
Kévin Dunglas 25ed020036 feat: Windows support (#2119)
Closes #83 #880 #1286.

Working patch for Windows support.

Supports linking to the [official PHP release (TS
version)](https://www.php.net/downloads.php).
Includes some work from #1286 (thanks @TenHian!!)

This patch allows using Visual Studio to compile the cgo code. To do so,
it must be compiled with Go 1.26 (RC) with the following setup:

```powershell
winget install -e --id Microsoft.VisualStudio.2022.Community --override "--passive --wait --add Microsoft.VisualStudio.Workload.NativeDesktop --add Microsoft.VisualStudio.Component.VC.Llvm.Clang --includeRecommended"
winget install -e --id GoLang.Go

$env:PATH += ';C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\Llvm\bin'

cd c:\
gh repo clone microsoft/vcpkg
.\vcpkg\bootstrap-vcpkg.bat
.\vcpkg\vcpkg install pthreads brotli

# build watcher
Invoke-WebRequest -Uri "https://github.com/e-dant/watcher/releases/download/0.14.3/x86_64-pc-windows-msvc.tar" -OutFile "$env:TEMP\watcher.tar"
tar -xf "$env:TEMP\watcher.tar" -C C:\
Rename-Item -Path "C:\x86_64-pc-windows-msvc" -NewName "watcher-x86_64-pc-windows-msvc"
Remove-Item "$env:TEMP\watcher.tar"

# download php
Invoke-WebRequest -Uri "https://downloads.php.net/~windows/releases/archives/php-8.5.1-Win32-vs17-x64.zip" -OutFile "$env:TEMP\php.zip"
Expand-Archive -Path "$env:TEMP\php.zip" -DestinationPath "C:\"
Remove-Item "$env:TEMP\php.zip"

# download php development package
Invoke-WebRequest -Uri "https://downloads.php.net/~windows/releases/archives/php-devel-pack-8.5.1-Win32-vs17-x64.zip" -OutFile "$env:TEMP\php-devel.zip"
Expand-Archive -Path "$env:TEMP\php-devel.zip" -DestinationPath "C:\"
Remove-Item "$env:TEMP\php-devel.zip"

$env:GOTOOLCHAIN = 'go1.26rc1'
$env:CC = 'clang'
$env:CXX = 'clang++'
$env:CGO_CFLAGS = "-I$env:C:\vcpkg\installed\x64-windows\include -IC:\watcher-x86_64-pc-windows-msvc -IC:\php-8.5.1-devel-vs17-x64\include -IC:\php-8.5.1-devel-vs17-x64\include\main -IC:\php-8.5.1-devel-vs17-x64\include\TSRM -IC:\php-8.5.1-devel-vs17-x64\include\Zend -IC:\php-8.5.1-devel-vs17-x64\include\ext"
$env:CGO_LDFLAGS = '-LC:\vcpkg\installed\x64-windows\lib -lbrotlienc -LC:\watcher-x86_64-pc-windows-msvc -llibwatcher-c -LC:\php-8.5.1-Win32-vs17-x64 -LC:\php-8.5.1-devel-vs17-x64\lib -lphp8ts -lphp8embed'

# clone frankenphp and build
git clone -b windows https://github.com/php/frankenphp.git
cd frankenphp\caddy\frankenphp
go build -ldflags '-extldflags="-fuse-ld=lld"' -tags nowatcher,nobadger,nomysql,nopgx

# Tests

$env:PATH += ";$env:VCPKG_ROOT\installed\x64-windows\bin;C:\watcher-x86_64-pc-windows-msvc";C:\php-8.5.1-Win32-vs17-x64"
"opcache.enable=0`r`nopcache.enable_cli=0" | Out-File -Encoding ascii php.ini
$env:PHPRC = Get-Location
go test -ldflags '-extldflags="-fuse-ld=lld"' -tags nowatcher,nobadger,nomysql,nopgx .
```

TODO:

- [x] Fix remaining skipped tests (scaling and watcher)
- [x] Test if the watcher mode works as expected
- [x] Automate the build with GitHub Actions

---------

Signed-off-by: Marc <m@pyc.ac>
Co-authored-by: Kévin Dunglas <kevin@dunglas.dev>
Co-authored-by: DubbleClick <m@pyc.ac>
2026-02-26 12:38:14 +01:00

234 lines
5.9 KiB
Go

//go:build !nowatcher
package watcher
import (
"log/slog"
"path/filepath"
"strings"
"github.com/dunglas/frankenphp/internal/fastabs"
"github.com/e-dant/watcher/watcher-go"
)
const sep = string(filepath.Separator)
type pattern struct {
patternGroup *PatternGroup
value string
parsedValues []string
events chan eventHolder
failureCount int
watcher *watcher.Watcher
}
func (p *pattern) startSession() {
p.watcher = watcher.NewWatcher(p.value, p.handle)
if globalLogger.Enabled(globalCtx, slog.LevelDebug) {
globalLogger.LogAttrs(globalCtx, slog.LevelDebug, "watching", slog.String("pattern", p.value))
}
}
// this method prepares the pattern struct (aka /path/*pattern)
func (p *pattern) parse() (err error) {
// first we clean the value
absPattern, err := fastabs.FastAbs(p.value)
if err != nil {
return err
}
p.value = absPattern
volumeName := filepath.VolumeName(p.value)
p.value = strings.TrimPrefix(p.value, volumeName)
// then we split the pattern to determine where the directory ends and the pattern starts
splitPattern := strings.Split(p.value, sep)
patternWithoutDir := ""
for i, part := range splitPattern {
isFilename := i == len(splitPattern)-1 && strings.Contains(part, ".")
isGlobCharacter := strings.ContainsAny(part, "[*?{")
if isFilename || isGlobCharacter {
patternWithoutDir = filepath.Join(splitPattern[i:]...)
p.value = filepath.Join(splitPattern[:i]...)
break
}
}
// now we split the pattern according to the recursive '**' syntax
p.parsedValues = strings.Split(patternWithoutDir, "**")
for i, pp := range p.parsedValues {
p.parsedValues[i] = strings.Trim(pp, sep)
}
// remove the trailing separator and add leading separator (except on Windows)
if volumeName == "" {
p.value = sep + strings.Trim(p.value, sep)
} else {
p.value = volumeName + sep + strings.Trim(p.value, sep)
}
// try to canonicalize the path
canonicalPattern, err := filepath.EvalSymlinks(p.value)
if err == nil {
p.value = canonicalPattern
}
return nil
}
func (p *pattern) allowReload(event *watcher.Event) bool {
if !isValidEventType(event.EffectType) || !isValidPathType(event) {
return false
}
// some editors create temporary files and never actually modify the original file
// so we need to also check Event.AssociatedPathName
// see https://github.com/php/frankenphp/issues/1375
return p.isValidPattern(event.PathName) || p.isValidPattern(event.AssociatedPathName)
}
func (p *pattern) handle(event *watcher.Event) {
// If the watcher prematurely sends the die@ event, retry watching
if event.PathType == watcher.PathTypeWatcher && strings.HasPrefix(event.PathName, "e/self/die@") && watcherIsActive.Load() {
p.retryWatching()
return
}
if p.allowReload(event) {
p.events <- eventHolder{p.patternGroup, event}
}
}
func (p *pattern) stop() {
p.watcher.Close()
}
func isValidEventType(effectType watcher.EffectType) bool {
return effectType <= watcher.EffectTypeDestroy
}
func isValidPathType(event *watcher.Event) bool {
if event.PathType == watcher.PathTypeWatcher && globalLogger.Enabled(globalCtx, slog.LevelDebug) {
globalLogger.LogAttrs(globalCtx, slog.LevelDebug, "special e-dant/watcher event", slog.Any("event", event))
}
return event.PathType <= watcher.PathTypeHardLink
}
func (p *pattern) isValidPattern(fileName string) bool {
if fileName == "" {
return false
}
// first we remove the dir from the file name
if !strings.HasPrefix(fileName, p.value) {
return false
}
// remove the directory path and separator from the filename
fileNameWithoutDir := strings.TrimPrefix(strings.TrimPrefix(fileName, p.value), sep)
// if the pattern has size 1 we can match it directly against the filename
if len(p.parsedValues) == 1 {
return matchCurlyBracePattern(p.parsedValues[0], fileNameWithoutDir)
}
return p.matchPatterns(fileNameWithoutDir)
}
func (p *pattern) matchPatterns(fileName string) bool {
partsToMatch := strings.Split(fileName, sep)
cursor := 0
// if there are multiple parsedValues due to '**' we need to match them individually
for i, pattern := range p.parsedValues {
patternSize := strings.Count(pattern, sep) + 1
// if we are at the last pattern we will start matching from the end of the filename
if i == len(p.parsedValues)-1 {
cursor = len(partsToMatch) - patternSize
if cursor < 0 {
return false
}
}
// the cursor will move through the fileName until the pattern matches
for j := cursor; j < len(partsToMatch); j++ {
if j+patternSize > len(partsToMatch) {
return false
}
cursor = j
subPattern := strings.Join(partsToMatch[j:j+patternSize], sep)
if matchCurlyBracePattern(pattern, subPattern) {
cursor = j + patternSize - 1
break
}
if cursor > len(partsToMatch)-patternSize-1 {
return false
}
}
}
return true
}
// we also check for the following syntax: /path/*.{php,twig,yaml}
func matchCurlyBracePattern(pattern string, fileName string) bool {
for _, subPattern := range expandCurlyBraces(pattern) {
if matchPattern(subPattern, fileName) {
return true
}
}
return false
}
// {dir1,dir2}/path -> []string{"dir1/path", "dir2/path"}
func expandCurlyBraces(s string) []string {
before, rest, found := strings.Cut(s, "{")
if !found {
return []string{s}
}
inside, after, found := strings.Cut(rest, "}")
if !found {
return []string{s} // no closing brace
}
var out []string
for _, subPattern := range strings.Split(inside, ",") {
out = append(out, expandCurlyBraces(before+subPattern+after)...)
}
return out
}
func matchPattern(pattern string, fileName string) bool {
if pattern == "" {
return true
}
patternMatches, err := filepath.Match(pattern, fileName)
if err != nil {
if globalLogger.Enabled(globalCtx, slog.LevelError) {
globalLogger.LogAttrs(globalCtx, slog.LevelError, "failed to match filename", slog.String("file", fileName), slog.Any("error", err))
}
return false
}
return patternMatches
}