refactor(backend): 将调试配置选项从 debug_info 统一更改为 debug

- 将所有编译器后端(GCC、Clang、MSVC)中的 debug_info 参数重命名为 debug
- 更新单元测试中相应的配置选项参数名称
- 修改命令行参数解析从 --debug-info 改为 --debug
- 调整文档中关于调试选项的描述和示例
- 修复链接阶段不应包含调试符号的逻辑错误
- 重构内部调试变量命名从 debugInfo 改为 debug
- 更新 YAML 配置文件中的调试选项键名
pull/1/head
韩天峰 4 months ago
parent e80083bf6a
commit 9186d04da4
  1. 2
      REFACTORING_README.md
  2. 6
      docs/COMPILER_CLI.md
  3. 93
      examples/tetris-sdl/cpp-src/tetris.cc
  4. 1
      examples/tetris-sdl/main.php
  5. 1
      examples/tetris-sdl/php-src/tetris.stub.php
  6. 4
      examples/win32-hello/CONFIG_PRIORITY_RULES.md
  7. 8
      examples/win32-hello/YAML_CONFIG_NAMING_CONVENTION.md
  8. 26
      phpunit/src/Backend/BackendOptionsTest.php
  9. 12
      phpunit/src/Backend/BackendTest.php
  10. 12
      src/Php/Backend/Clang.php
  11. 8
      src/Php/Backend/Gcc.php
  12. 10
      src/Php/Backend/Msvc.php
  13. 8
      src/Php/CompilerBase.php
  14. 8
      src/Php/Constants.php
  15. 10
      src/Php/Translator.php

@ -180,7 +180,7 @@ $linkCmd = $compiler->buildLinkCommand(
['hello.obj', 'main.obj'],
'hello.exe',
[
'debug_info' => false,
'debug' => false,
'no_console' => false,
]
);

@ -274,7 +274,7 @@ cat benchmark.prof
---
### 10. `--debug-info` - 启用调试信息
### 10. `--debug` - 启用调试模式
**类型**: 开关
@ -283,7 +283,7 @@ cat benchmark.prof
**示例**:
```bash
# 启用调试信息
./bin/compiler.php app.php --debug-info
./bin/compiler.php app.php --debug
```
---
@ -651,7 +651,7 @@ skip: /path/to/file.php
./bin/compiler.php app.php -O3 -j 16 -p
# 调试构建
./bin/compiler.php app.php -O0 -v --debug-info
./bin/compiler.php app.php -O0 -v --debug
# 扩展构建
./bin/compiler.php ext/ -m ext -o myext -O2 -v

@ -1,7 +1,10 @@
#include <phpx.h>
#include <SDL2/SDL.h>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace php;
@ -74,6 +77,69 @@ static const SDL_Color COLORS[7] = {
{255, 165, 0, 255} // L - Orange
};
static void draw_filled_rect(SDL_Renderer* renderer, int x, int y, int w, int h) {
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
SDL_RenderFillRect(renderer, &rect);
}
static void draw_seven_segment_digit(SDL_Renderer* renderer, int x, int y, int digit, int scale) {
static const bool SEGMENTS[10][7] = {
{true, true, true, true, true, true, false}, // 0
{false, true, true, false, false, false, false}, // 1
{true, true, false, true, true, false, true }, // 2
{true, true, true, true, false, false, true }, // 3
{false, true, true, false, false, true, true }, // 4
{true, false, true, true, false, true, true }, // 5
{true, false, true, true, true, true, true }, // 6
{true, true, true, false, false, false, false}, // 7
{true, true, true, true, true, true, true }, // 8
{true, true, true, true, false, true, true } // 9
};
if (digit < 0 || digit > 9) {
return;
}
const int width = 10 * scale;
const int height = 18 * scale;
const int thickness = 2 * scale;
const int midY = y + height / 2 - thickness / 2;
if (SEGMENTS[digit][0]) draw_filled_rect(renderer, x + thickness, y, width - 2 * thickness, thickness);
if (SEGMENTS[digit][1]) draw_filled_rect(renderer, x + width - thickness, y + thickness, thickness, height / 2 - thickness);
if (SEGMENTS[digit][2]) draw_filled_rect(renderer, x + width - thickness, y + height / 2, thickness, height / 2 - thickness);
if (SEGMENTS[digit][3]) draw_filled_rect(renderer, x + thickness, y + height - thickness, width - 2 * thickness, thickness);
if (SEGMENTS[digit][4]) draw_filled_rect(renderer, x, y + height / 2, thickness, height / 2 - thickness);
if (SEGMENTS[digit][5]) draw_filled_rect(renderer, x, y + thickness, thickness, height / 2 - thickness);
if (SEGMENTS[digit][6]) draw_filled_rect(renderer, x + thickness, midY, width - 2 * thickness, thickness);
}
static void draw_score_number(SDL_Renderer* renderer, int x, int y, int score) {
char scoreStr[16];
snprintf(scoreStr, sizeof(scoreStr), "%d", std::max(0, score));
int length = 0;
while (scoreStr[length] != '\0') {
length++;
}
const int scale = length > 6 ? 1 : 2;
const int digitWidth = 10 * scale;
const int spacing = 4 * scale;
const int maxDigits = scale == 1 ? 12 : 6;
int start = std::max(0, length - maxDigits);
for (int i = start; i < length; i++) {
int digit = scoreStr[i] - '0';
int offset = i - start;
draw_seven_segment_digit(renderer, x + offset * (digitWidth + spacing), y, digit, scale);
}
}
// Simple game state - stores board data from PHP
class TetrisBox : public Box {
public:
@ -187,6 +253,12 @@ void php_tetris_set_board(var box, Array board) {
}
}
// Set score calculated by PHP game logic
void php_tetris_set_score(var box, Int score) {
auto tetris = box.toBox<TetrisBox>();
tetris->score = (int)score;
}
// Render game - Draw board from PHP
void php_tetris_render(var box, Int hWnd) {
auto tetris = box.toBox<TetrisBox>();
@ -244,26 +316,9 @@ void php_tetris_render(var box, Int hWnd) {
SDL_RenderFillRect(renderer, &bar);
}
// Display score value as simple horizontal bars
// Display score value as seven-segment digits
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
int score = tetris->score;
// Draw score digits as bars
char scoreStr[16];
snprintf(scoreStr, sizeof(scoreStr), "%d", score);
int yPos = panelY + 40;
for (int i = 0; scoreStr[i] != '\0' && i < 6; i++) {
int digit = scoreStr[i] - '0';
// Each digit represented by vertical bar height
int barHeight = digit * 15 + 5;
SDL_Rect digitBar;
digitBar.x = panelX + 20 + i * 25;
digitBar.y = yPos + (150 - barHeight);
digitBar.w = 15;
digitBar.h = barHeight;
SDL_RenderFillRect(renderer, &digitBar);
}
draw_score_number(renderer, panelX + 20, panelY + 55, tetris->score);
// Update the screen
SDL_RenderPresent(renderer);

@ -246,6 +246,7 @@ class TetrisGame
// Sync to C++
tetris_set_board($this->game, $renderBoard);
tetris_set_score($this->game, $this->score);
}
private function handleKeyPress(int $keyCode): void

@ -21,6 +21,7 @@ function tetris_hard_drop(mixed $game): void {}
// SDL 窗口和渲染函数
function tetris_poll_event(mixed $game): array {}
function tetris_set_board(mixed $game, array $board): void {}
function tetris_set_score(mixed $game, int $score): void {}
function tetris_render(mixed $game, int $hWnd): void {}
// SDL 工具函数

@ -267,7 +267,7 @@ php bin/compiler.php test.php --cxx-std=c++17 -O2
| 配置项 | 命令行参数 | 说明 |
|--------|-----------|------|
| 优化级别 | `-O <level>` | 0-3 |
| 调试信息 | `--debug-info` | 启用调试 |
| 调试信息 | `--debug` | 启用调试 |
| 性能分析 | `--profile` | 启用 profiling |
| Sanitizer | `--sanitize` | 内存检测 |
| 并行任务 | `-j <num>` | 并行编译数 |
@ -296,7 +296,7 @@ cxx-flags:
```bash
# 开发时使用调试模式
php bin/compiler.php project.yml --debug-info
php bin/compiler.php project.yml --debug
# 发布时使用优化
php bin/compiler.php project.yml -O3

@ -19,7 +19,7 @@ cxx-flags:
- -Wall
ld-flags:
- -lm
debug-info: true
debug: true
no-console: true
```
@ -59,7 +59,7 @@ cxxflags: # 等同于 cxx-flags
| `ld-flags` | `ldflags` | 链接器选项 | array/string |
| `sources` | - | 源文件列表 | array |
| `ignore` | - | 忽略的文件/目录 | array |
| `debug-info` | - | 启用调试信息 | boolean |
| `debug` | - | 启用调试模式 | boolean |
| `no-console` | - | 隐藏控制台窗口 | boolean |
**注意:**
@ -318,7 +318,7 @@ ld-flags:
# ✅ 好
cxx-std: c++17
build-mode: bin
debug-info: true
debug: true
# ❌ 避免
cxx_std: c++17
@ -447,7 +447,7 @@ php bin/compiler.php project.yml --cxx-std=c++20
php bin/compiler.php project.yml --mode=ext
# 启用调试信息
php bin/compiler.php project.yml --debug-info
php bin/compiler.php project.yml --debug
```
---

@ -22,7 +22,7 @@ class BackendOptionsTest extends TestCase
$options = $compiler->buildCompileOptions([
'optimize' => 2,
'debug_info' => false,
'debug' => false,
'cpp_std' => 'c++17',
'is_zts' => false,
]);
@ -61,7 +61,7 @@ class BackendOptionsTest extends TestCase
$compiler = new Msvc($platform);
$options = $compiler->buildCompileOptions([
'debug_info' => true,
'debug' => true,
]);
$this->assertStringContainsString('/Od', $options); // 禁用优化
@ -159,7 +159,7 @@ class BackendOptionsTest extends TestCase
$compiler = new Msvc($platform);
$options = $compiler->buildLinkOptions([
'debug_info' => true,
'debug' => true,
]);
$this->assertStringContainsString('/DEBUG', $options);
@ -206,7 +206,7 @@ class BackendOptionsTest extends TestCase
$options = $compiler->buildCompileOptions([
'optimize' => 2,
'debug_info' => false,
'debug' => false,
'cpp_std' => 'c++17',
]);
@ -224,7 +224,7 @@ class BackendOptionsTest extends TestCase
$compiler = new Gcc($platform);
$options = $compiler->buildCompileOptions([
'debug_info' => true,
'debug' => true,
]);
$this->assertStringContainsString('-O0', $options);
@ -290,7 +290,7 @@ class BackendOptionsTest extends TestCase
}
/**
* 测试 GCC 链接选项 - 调试
* 测试 GCC 链接选项 - 调试模式(链接时不需要 -g)
*/
public function testGccLinkOptionsDebug(): void
{
@ -298,10 +298,11 @@ class BackendOptionsTest extends TestCase
$compiler = new Gcc($platform);
$options = $compiler->buildLinkOptions([
'debug_info' => true,
'debug' => true,
]);
$this->assertStringContainsString('-g', $options);
// 链接时不应该包含 -g,调试信息在编译阶段已经生成
$this->assertStringNotContainsString('-g', $options);
}
/**
@ -395,7 +396,7 @@ class BackendOptionsTest extends TestCase
$compiler = new Clang($platform);
$options = $compiler->buildLinkOptions([
'debug_info' => true,
'debug' => true,
'no_console' => true,
'build_mode' => 'ext',
]);
@ -407,7 +408,7 @@ class BackendOptionsTest extends TestCase
}
/**
* 测试 Clang 链接选项 - Unix
* 测试 Clang 链接选项 - Unix(链接时不需要 -g)
*/
public function testClangLinkOptionsUnix(): void
{
@ -415,12 +416,13 @@ class BackendOptionsTest extends TestCase
$compiler = new Clang($platform);
$options = $compiler->buildLinkOptions([
'debug_info' => true,
'debug' => true,
'build_mode' => 'ext',
'rpath' => ['/usr/lib'],
]);
$this->assertStringContainsString('-g', $options);
// 链接时不应该包含 -g,调试信息在编译阶段已经生成
$this->assertStringNotContainsString('-g', $options);
$this->assertStringContainsString('-shared', $options);
$this->assertStringContainsString('-Wl,-rpath', $options);
}

@ -135,7 +135,7 @@ class BackendTest extends TestCase
$options = $compiler->buildFullCompileOptions([
'optimize' => 2,
'debug_info' => false,
'debug' => false,
'sanitize' => null,
'cpp_std' => 'c++17',
'suppressed_warnings' => [
@ -165,7 +165,7 @@ class BackendTest extends TestCase
$compiler = new Msvc($platform);
$options = $compiler->buildFullCompileOptions([
'debug_info' => true,
'debug' => true,
]);
$this->assertStringContainsString('/Od', $options); // 禁用优化
@ -181,7 +181,7 @@ class BackendTest extends TestCase
$compiler = new Msvc($platform);
$options = $compiler->buildFullLinkOptions([
'debug_info' => true,
'debug' => true,
'no_console' => true,
'shared' => true,
]);
@ -241,7 +241,7 @@ class BackendTest extends TestCase
$options = $compiler->buildFullCompileOptions([
'optimize' => 2,
'debug_info' => false,
'debug' => false,
'cpp_std' => 'c++17',
'sanitize' => 'address',
'pic' => true,
@ -263,7 +263,7 @@ class BackendTest extends TestCase
$compiler = new Gcc($platform);
$options = $compiler->buildFullCompileOptions([
'debug_info' => true,
'debug' => true,
]);
$this->assertStringContainsString('-O0', $options);
@ -361,7 +361,7 @@ class BackendTest extends TestCase
$compiler = new Clang($platform);
$options = $compiler->buildFullLinkOptions([
'debug_info' => true,
'debug' => true,
'no_console' => true,
]);

@ -123,7 +123,7 @@ class Clang extends CompilerBackend
$optimizeLevel = $options['optimize'] ?? 2;
// 调试模式
if (!empty($options['debug_info'])) {
if (!empty($options['debug'])) {
$cmd .= ' -O0 -g';
} else {
$cmd .= ' -O' . $optimizeLevel;
@ -172,7 +172,7 @@ class Clang extends CompilerBackend
$optimizeLevel = $options['optimize'] ?? 0;
// 调试模式
if (!empty($options['debug_info'])) {
if (!empty($options['debug'])) {
$cmd .= ' -O0 -g';
} else {
$cmd .= ' -O' . $optimizeLevel;
@ -251,7 +251,7 @@ class Clang extends CompilerBackend
}
// 优化级别
if (!empty($options['debug_info'])) {
if (!empty($options['debug'])) {
$cmd .= ' -O0 -g';
} else {
$optimizeLevel = $options['optimize'] ?? 2;
@ -289,7 +289,7 @@ class Clang extends CompilerBackend
// Windows 特定选项
if ($this->platform instanceof \PhpAot\Php\Platform\Windows) {
// 调试
if (!empty($options['debug_info'])) {
if (!empty($options['debug'])) {
$cmd .= ' /DEBUG';
}
@ -342,7 +342,7 @@ class Clang extends CompilerBackend
}
// 优化和调试
if (!empty($config['debug_info'])) {
if (!empty($config['debug'])) {
$cmd .= ' -O0 -g';
} else {
$optimizeLevel = $config['optimize'] ?? 2;
@ -389,7 +389,7 @@ class Clang extends CompilerBackend
// Windows 特定选项
if ($this->platform instanceof \PhpAot\Php\Platform\Windows) {
// 调试
if (!empty($config['debug_info'])) {
if (!empty($config['debug'])) {
$cmd .= ' /DEBUG';
}

@ -98,7 +98,7 @@ class Gcc extends CompilerBackend
$cmd .= ' -O' . $optimizeLevel;
// 调试信息
if (!empty($options['debug_info'])) {
if (!empty($options['debug'])) {
$cmd .= ' -g';
}
@ -132,7 +132,7 @@ class Gcc extends CompilerBackend
$cmd .= ' -O' . $optimizeLevel;
// 调试信息
if (!empty($options['debug_info'])) {
if (!empty($options['debug'])) {
$cmd .= ' -g';
}
@ -176,7 +176,7 @@ class Gcc extends CompilerBackend
$cmd = '';
// 优化级别
if (!empty($options['debug_info'])) {
if (!empty($options['debug'])) {
$cmd .= ' -O0 -g';
} else {
$optimizeLevel = $options['optimize'] ?? 2;
@ -246,7 +246,7 @@ class Gcc extends CompilerBackend
}
// 优化和调试
if (!empty($config['debug_info'])) {
if (!empty($config['debug'])) {
$cmd .= ' -O0 -g';
} else {
$optimizeLevel = $config['optimize'] ?? 2;

@ -182,7 +182,7 @@ class Msvc extends CompilerBackend
$cmd .= ' /OUT:' . escapeshellarg($outputFile);
// 调试信息
if (!empty($options['debug_info'])) {
if (!empty($options['debug'])) {
$cmd .= ' /DEBUG';
}
@ -253,7 +253,7 @@ class Msvc extends CompilerBackend
}
// 优化和调试
if (!empty($options['debug_info'])) {
if (!empty($options['debug'])) {
$cmd .= ' /Od /Zi';
} else {
$optimizeLevel = $options['optimize'] ?? 2;
@ -294,7 +294,7 @@ class Msvc extends CompilerBackend
$cmd = '';
// 调试
if (!empty($options['debug_info'])) {
if (!empty($options['debug'])) {
$cmd .= ' /DEBUG';
}
@ -340,7 +340,7 @@ class Msvc extends CompilerBackend
}
// 优化和调试
if (!empty($config['debug_info'])) {
if (!empty($config['debug'])) {
$cmd .= ' /Od /Zi';
} else {
$optimizeLevel = $config['optimize'] ?? 2;
@ -396,7 +396,7 @@ class Msvc extends CompilerBackend
$cmd = '';
// 调试
if (!empty($config['debug_info'])) {
if (!empty($config['debug'])) {
$cmd .= ' /DEBUG';
}

@ -166,7 +166,7 @@ class CompilerBase extends \PhpAot\Core\Translator
protected string $ldflags = '';
protected string $linker = 'link'; // Windows linker: link.exe or lld-link
protected int $floatPrecision = 17;
protected bool $debugInfo = false;
protected bool $debug = false;
protected bool $formatCode = false;
protected bool $printBacktraceOnError = false;
protected bool $noLiteralStrings = false;
@ -2563,7 +2563,7 @@ class CompilerBase extends \PhpAot\Core\Translator
// 再添加编译选项(编译器相关)
$config = [
'optimize' => $this->optimizeLevel,
'debug_info' => $this->debugInfo,
'debug' => $this->debug,
'sanitize' => $this->sanitize,
'cpp_std' => $this->cxxStd,
'is_zts' => $this->isPhpZts,
@ -2582,7 +2582,7 @@ class CompilerBase extends \PhpAot\Core\Translator
// 再添加链接选项(编译器相关)
$config = [
'debug_info' => $this->debugInfo,
'debug' => $this->debug,
'no_console' => $this->noConsole,
'build_mode' => $this->buildMode,
'sanitize' => $this->sanitize,
@ -5421,7 +5421,7 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function genDebugInfo(?NodeAbstract $stmt = null): string
{
$code = '';
if ($this->debugInfo) {
if ($this->debug) {
if ($stmt) {
$code .= 'php::traceDebugInfo("' . $this->escapeString($this->file) . '", ' . $stmt->getLine() . ');' . PHP_EOL;
} else {

@ -124,11 +124,11 @@ class Constants
'required' => false,
'defaultValue' => 0,
],
'debug-info' => [
'longPrefix' => 'debug-info',
'description' => 'Enable debug info',
'debug' => [
'longPrefix' => 'debug',
'description' => 'Enable debug mode (auto-disable optimizations, add debug symbols)',
'required' => false,
'defaultValue' => 0,
'noValue' => true,
],
'job' => [
'prefix' => 'j',

@ -94,7 +94,7 @@ class Translator extends Preprocessor
$climate->bold('OPTIONS:');
$climate->tab()->out('-O <level> Optimization level (0-3, default: 0)');
$climate->tab()->out('-p, --profile Enable performance profiling');
$climate->tab()->out('-d, --debug-info Enable debug info (auto-disable optimizations, add -g/-Zi)');
$climate->tab()->out('-d, --debug Enable debug mode (auto-disable optimizations, add debug symbols)');
$climate->tab()->out('--cxx-std <version> C++ standard version (c++17, c++20, etc.)');
$climate->tab()->out('-o, --output <file> Output binary name (default: input basename)');
$climate->tab()->out('-v, --version Show version');
@ -145,9 +145,9 @@ class Translator extends Preprocessor
$this->maxJob = intval($this->climate->arguments->get('job'));
}
// 调试信息
if ($this->climate->arguments->defined('debug-info')) {
$this->debugInfo = true;
// 调试模式
if ($this->climate->arguments->defined('debug')) {
$this->debug = true;
}
// 禁用字面量字符串优化
@ -748,7 +748,7 @@ CODE;
$objectFile,
[
'optimize' => $this->optimizeLevel,
'debug_info' => $this->debugInfo,
'debug' => $this->debug,
'sanitize' => $this->sanitize,
'cpp_std' => $this->cxxStd,
'is_zts' => $this->isPhpZts,

Loading…
Cancel
Save