fix(compiler): avoid native object filename collisions (#91)

master
yavon007 1 month ago committed by GitHub
parent 42846579b7
commit 72b7ce9bf4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      phpunit/src/CompilerBaseApiTest.php
  2. 16
      phpunit/src/PreprocessorTest.php
  3. 20
      src/Preprocessor.php

@ -286,7 +286,7 @@ PHP);
public function testMiscObjectCacheIsInvalidatedWhenCompileOptionsChange(): void
{
$source = $this->testDir . '/typephp_runtime.cc';
$object = $this->testDir . '/typephp_runtime.o';
$object = $this->compiler->getObjectFile($source);
file_put_contents($source, "int typephp_runtime_test = 1;\n");
file_put_contents($object, 'object');
touch($source, time() - 10);

@ -187,8 +187,7 @@ class PreprocessorTest extends TestCase
$cppFile = $this->compiler->getBuildDir() . '/include/test.cc';
$result = $this->compiler->getObjectFile($cppFile);
$this->assertStringEndsWith('.o', $result);
$this->assertStringContainsString('test', $result);
$this->assertSame($this->compiler->getBuildDir() . '/include/test.o', $result);
}
public function testGetObjectFileDifferentObjectExtension(): void
@ -196,7 +195,18 @@ class PreprocessorTest extends TestCase
// On Linux the object extension is .o
$path = '/some/path/file.cc';
$result = $this->compiler->getObjectFile($path);
$this->assertStringEndsWith('file.o', $result);
$this->assertStringEndsWith('file.cc.o', $result);
}
public function testGetObjectFileKeepsNativeSourceExtensionsDistinct(): void
{
$cObject = $this->compiler->getObjectFile('/some/path/foo.c');
$cppObject = $this->compiler->getObjectFile('/some/path/foo.cpp');
$ccObject = $this->compiler->getObjectFile('/some/path/foo.cc');
$this->assertSame('/some/path/foo.c.o', $cObject);
$this->assertSame('/some/path/foo.cpp.o', $cppObject);
$this->assertSame('/some/path/foo.cc.o', $ccObject);
}
// ========================================================================

@ -340,7 +340,25 @@ class Preprocessor extends CompilerBase
return $objectDir . $separator . $info['filename'] . $ext;
}
return $info['dirname'] . $this->getPlatform()->getPathSeparator() . $info['filename'] . $ext;
$filename = $info['filename'];
if (!$this->isGeneratedCppFile($cppFile)) {
// Native sources keep their original paths, so their extensions
// distinguish files such as foo.c and foo.cpp in one directory.
$filename .= '.' . $info['extension'];
}
return $info['dirname'] . $this->getPlatform()->getPathSeparator() . $filename . $ext;
}
private function isGeneratedCppFile(string $file): bool
{
if (pathinfo($file, PATHINFO_EXTENSION) !== 'cc') {
return false;
}
$normalizedFile = str_replace('\\', '/', $file);
$normalizedBuildDir = rtrim(str_replace('\\', '/', $this->buildDir), '/') . '/';
return str_starts_with($normalizedFile, $normalizedBuildDir);
}
protected function isProjectRuntimeEntryFile(string $file): bool

Loading…
Cancel
Save