diff --git a/phpunit/src/CompilerBaseApiTest.php b/phpunit/src/CompilerBaseApiTest.php index 2b46ad67..5a91cdc0 100644 --- a/phpunit/src/CompilerBaseApiTest.php +++ b/phpunit/src/CompilerBaseApiTest.php @@ -286,7 +286,7 @@ public function testLinkedPhpRuntimeMustBeAtLeast84WithoutMatchingLanguageMinor( 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); diff --git a/phpunit/src/PreprocessorTest.php b/phpunit/src/PreprocessorTest.php index ae072fe4..3cd867b0 100644 --- a/phpunit/src/PreprocessorTest.php +++ b/phpunit/src/PreprocessorTest.php @@ -187,8 +187,7 @@ public function testGetObjectFile(): void $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 @@ public function testGetObjectFileDifferentObjectExtension(): void // 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); } // ======================================================================== diff --git a/src/Preprocessor.php b/src/Preprocessor.php index 9900b40c..8bd2301d 100644 --- a/src/Preprocessor.php +++ b/src/Preprocessor.php @@ -340,7 +340,25 @@ public function getObjectFile(string $cppFile): string 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