- Moved common compile command options logic to NativeCommandOptionsTrait - Extracted Windows resource file compilation functionality to ResourceCompilationTrait - Removed duplicate resource compilation methods from Translator class - Added trait usage in Translator class for better code organization - Maintained all existing functionality while improving code structure - Centralized compile options generation across different compilation scenariospull/17/head
parent
1fab000209
commit
11d4c23085
15 changed files with 187 additions and 194 deletions
@ -0,0 +1,100 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* This file is part of TypePHP. |
||||||
|
* |
||||||
|
* @link https://www.swoole.com/ |
||||||
|
* @contact service@swoole.com |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace TypePhp\Build; |
||||||
|
|
||||||
|
use TypePhp\Metadata\Constants; |
||||||
|
|
||||||
|
trait NativeCommandOptionsTrait |
||||||
|
{ |
||||||
|
protected function getCommonCompileCommandOptions(): array |
||||||
|
{ |
||||||
|
$includePaths = $this->getIncludePaths(); |
||||||
|
if (!empty($this->userIncludePaths)) { |
||||||
|
$includePaths = array_merge($includePaths, $this->userIncludePaths); |
||||||
|
} |
||||||
|
|
||||||
|
$userDefines = $this->userDefines; |
||||||
|
if ($this->isBuildModeLib()) { |
||||||
|
$userDefines[] = 'TYPEPHP_NO_MAIN=1'; |
||||||
|
} |
||||||
|
|
||||||
|
return [ |
||||||
|
'include_paths' => $includePaths, |
||||||
|
'optimize' => $this->optimizeLevel, |
||||||
|
'debug' => $this->debug, |
||||||
|
'sanitize' => $this->sanitize, |
||||||
|
'march' => $this->march, |
||||||
|
'target_platform' => $this->targetPlatform, |
||||||
|
'is_zts' => $this->isPhpZts, |
||||||
|
'build_mode' => $this->buildMode, |
||||||
|
'enable_profiler' => $this->enableProfiler, |
||||||
|
'prof_output' => $this->targetName . '.prof', |
||||||
|
'user_defines' => $userDefines, |
||||||
|
'lto' => $this->enableLto, |
||||||
|
]; |
||||||
|
} |
||||||
|
|
||||||
|
protected function getCompileCommandOptions(): array |
||||||
|
{ |
||||||
|
$options = $this->getCommonCompileCommandOptions(); |
||||||
|
$options['cpp_std'] = $this->cxxStd; |
||||||
|
$options['cxxflags'] = $this->cxxFlags; |
||||||
|
$options['suppressed_warnings'] = Constants::MSVC_SUPPRESSED_WARNINGS ?? []; |
||||||
|
return $options; |
||||||
|
} |
||||||
|
|
||||||
|
protected function getCCompileCommandOptions(): array |
||||||
|
{ |
||||||
|
$options = $this->getCommonCompileCommandOptions(); |
||||||
|
$options['suppressed_warnings'] = ['4244', '4146']; |
||||||
|
return $options; |
||||||
|
} |
||||||
|
|
||||||
|
protected function getNativeCompileCommandOptions(string $language = ''): array |
||||||
|
{ |
||||||
|
$options = $this->getCommonCompileCommandOptions(); |
||||||
|
$options['suppressed_warnings'] = Constants::MSVC_SUPPRESSED_WARNINGS ?? []; |
||||||
|
|
||||||
|
if ($language === 'objective-c++') { |
||||||
|
$options['cpp_std'] = $this->cxxStd; |
||||||
|
$options['cxxflags'] = $this->cxxFlags; |
||||||
|
} |
||||||
|
|
||||||
|
return $options; |
||||||
|
} |
||||||
|
|
||||||
|
protected function getLinkCommandOptions(): array |
||||||
|
{ |
||||||
|
$libraryPaths = array_merge($this->getLibraryPaths(), $this->linkPaths); |
||||||
|
$libraries = $this->getLibraries(); |
||||||
|
if ($this->enableProfiler) { |
||||||
|
$libraries[] = 'profiler'; |
||||||
|
} |
||||||
|
$libraries = array_merge($libraries, $this->linkLibs); |
||||||
|
|
||||||
|
$options = [ |
||||||
|
'library_paths' => $libraryPaths, |
||||||
|
'libraries' => $libraries, |
||||||
|
'ldflags' => $this->ldflags, |
||||||
|
'debug' => $this->debug, |
||||||
|
'no_console' => $this->noConsole, |
||||||
|
'build_mode' => $this->buildMode, |
||||||
|
'sanitize' => $this->sanitize, |
||||||
|
'lto' => $this->enableLto, |
||||||
|
'target_platform' => $this->targetPlatform, |
||||||
|
]; |
||||||
|
|
||||||
|
$rpaths = $this->getPlatform()->getDefaultRpaths($this->getPhpxDir(), $this->getPhpDir()); |
||||||
|
if (!empty($rpaths)) { |
||||||
|
$options['rpath'] = $rpaths; |
||||||
|
} |
||||||
|
|
||||||
|
return $options; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,83 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* This file is part of TypePHP. |
||||||
|
* |
||||||
|
* @link https://www.swoole.com/ |
||||||
|
* @contact service@swoole.com |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace TypePhp\Build; |
||||||
|
|
||||||
|
use TypePhp\Backend\Msvc; |
||||||
|
use TypePhp\Generator\ResourceFileGenerator; |
||||||
|
|
||||||
|
trait ResourceCompilationTrait |
||||||
|
{ |
||||||
|
public function hasResourceFile(): bool |
||||||
|
{ |
||||||
|
if (!$this->isWindows()) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
$generator = $this->createResourceGenerator(); |
||||||
|
return $generator !== null && $generator->hasResource(); |
||||||
|
} |
||||||
|
|
||||||
|
public function getResourceRcFile(): string |
||||||
|
{ |
||||||
|
return $this->getBuildDir() . DIRECTORY_SEPARATOR . 'app_resource.rc'; |
||||||
|
} |
||||||
|
|
||||||
|
public function getResourceResFile(): string |
||||||
|
{ |
||||||
|
return $this->getBuildDir() . DIRECTORY_SEPARATOR . 'app_resource.res'; |
||||||
|
} |
||||||
|
|
||||||
|
protected function createResourceGenerator(): ?ResourceFileGenerator |
||||||
|
{ |
||||||
|
if (empty($this->resourceConfig)) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
$projectDir = $this->resourceConfig['_projectDir'] ?? getcwd(); |
||||||
|
return new ResourceFileGenerator($this->resourceConfig, $projectDir); |
||||||
|
} |
||||||
|
|
||||||
|
protected function compileResourceFile(): void |
||||||
|
{ |
||||||
|
if (!$this->isWindows()) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
$generator = $this->createResourceGenerator(); |
||||||
|
if ($generator === null || !$generator->hasResource()) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
$rcFile = $this->getResourceRcFile(); |
||||||
|
$rcContent = $generator->generate(); |
||||||
|
$this->writeFile($rcFile, "\xEF\xBB\xBF" . $rcContent); |
||||||
|
$this->climate->info('Generated resource file: ' . $rcFile); |
||||||
|
|
||||||
|
$backend = $this->getCompilerBackend(); |
||||||
|
if ($backend instanceof Msvc) { |
||||||
|
$resFile = $this->getResourceResFile(); |
||||||
|
$cmd = $backend->compileResourceFile($rcFile, $resFile); |
||||||
|
$this->climate->comment($cmd); |
||||||
|
|
||||||
|
exec($cmd . ' 2>&1', $output, $ret); |
||||||
|
if (!empty($output)) { |
||||||
|
foreach ($output as $line) { |
||||||
|
$this->climate->out($line); |
||||||
|
} |
||||||
|
} |
||||||
|
if ($ret !== 0) { |
||||||
|
$this->error('Resource compilation failed: ' . $rcFile); |
||||||
|
} |
||||||
|
if (!file_exists($resFile)) { |
||||||
|
$this->error('Resource file not generated: ' . $resFile); |
||||||
|
} |
||||||
|
$this->climate->green('Resource compiled: ' . $resFile); |
||||||
|
} else { |
||||||
|
$this->climate->warning('Resource files are only supported with MSVC backend on Windows'); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue