Support compiling third party library C files as C code

This commit is contained in:
2025-01-03 15:11:59 +02:00
parent ef0c2a2785
commit 592215dd30
10 changed files with 40 additions and 29 deletions

View File

@@ -20,7 +20,7 @@ namespace FlaxEditor.Content
}
/// <inheritdoc />
public override string TypeDescription => Path.EndsWith(".h") ? "C++ Header File" : "C++ Source Code";
public override string TypeDescription => Path.EndsWith(".h") || Path.EndsWith(".hpp") ? "C++ Header File" : "C++ Source Code";
/// <inheritdoc />
public override SpriteHandle DefaultThumbnail => Editor.Instance.Icons.CPPScript128;

View File

@@ -186,12 +186,12 @@ namespace FlaxEditor.CustomEditors.Dedicated
foreach (var file in files)
FindNewKeysCSharp(file, newKeys, allKeys);
// C++
files = Directory.GetFiles(Globals.ProjectSourceFolder, "*.cpp", SearchOption.AllDirectories);
// C/C++
files = Directory.GetFiles(Globals.ProjectSourceFolder, "*.cpp", SearchOption.AllDirectories).Concat(Directory.GetFiles(Globals.ProjectSourceFolder, "*.c", SearchOption.AllDirectories)).ToArray();
filesCount += files.Length;
foreach (var file in files)
FindNewKeysCpp(file, newKeys, allKeys);
files = Directory.GetFiles(Globals.ProjectSourceFolder, "*.h", SearchOption.AllDirectories);
files = Directory.GetFiles(Globals.ProjectSourceFolder, "*.h", SearchOption.AllDirectories).Concat(Directory.GetFiles(Globals.ProjectSourceFolder, "*.hpp", SearchOption.AllDirectories)).ToArray();;
filesCount += files.Length;
foreach (var file in files)
FindNewKeysCpp(file, newKeys, allKeys);

View File

@@ -1013,7 +1013,7 @@ namespace FlaxEditor.Modules
ContentItem item;
if (path.EndsWith(".cs"))
item = new CSharpScriptItem(path);
else if (path.EndsWith(".cpp") || path.EndsWith(".h"))
else if (path.EndsWith(".cpp") || path.EndsWith(".h") || path.EndsWith(".c") || path.EndsWith(".hpp"))
item = new CppScriptItem(path);
else if (path.EndsWith(".shader") || path.EndsWith(".hlsl"))
item = new ShaderSourceItem(path);

View File

@@ -222,7 +222,7 @@ namespace FlaxEditor.Modules
outputExtension = extension;
// Check if can place source files here
if (!targetLocation.CanHaveScripts && (extension == ".cs" || extension == ".cpp" || extension == ".h"))
if (!targetLocation.CanHaveScripts && (extension == ".cs" || extension == ".cpp" || extension == ".h" || extension == ".c" || extension == ".hpp"))
{
// Error
Editor.LogWarning(string.Format("Cannot import \'{0}\' to \'{1}\'. The target directory cannot have scripts.", inputPath, targetLocation.Node.Path));

View File

@@ -120,9 +120,13 @@ void ScriptsBuilderImpl::sourceDirEvent(const String& path, FileSystemAction act
// Discard non-source files or generated files
if ((!path.EndsWith(TEXT(".cs")) &&
!path.EndsWith(TEXT(".cpp")) &&
!path.EndsWith(TEXT(".c")) &&
!path.EndsWith(TEXT(".hpp")) &&
!path.EndsWith(TEXT(".h"))) ||
path.EndsWith(TEXT(".Gen.cs")))
{
return;
}
ScopeLock scopeLock(_locker);
_lastSourceCodeEdited = DateTime::Now();

View File

@@ -127,7 +127,7 @@ namespace Flax.Build.Bindings
var headerFiles = new List<string>(moduleOptions.SourceFiles.Count / 2);
for (int i = 0; i < moduleOptions.SourceFiles.Count; i++)
{
if (moduleOptions.SourceFiles[i].EndsWith(".h", StringComparison.OrdinalIgnoreCase))
if (moduleOptions.SourceFiles[i].EndsWith(".h", StringComparison.OrdinalIgnoreCase) || moduleOptions.SourceFiles[i].EndsWith(".hpp", StringComparison.OrdinalIgnoreCase))
headerFiles.Add(moduleOptions.SourceFiles[i]);
}
if (headerFiles.Count == 0)

View File

@@ -482,7 +482,7 @@ namespace Flax.Build
var cppFiles = new List<string>(moduleOptions.SourceFiles.Count / 2);
for (int i = 0; i < moduleOptions.SourceFiles.Count; i++)
{
if (moduleOptions.SourceFiles[i].EndsWith(".cpp", StringComparison.OrdinalIgnoreCase))
if (moduleOptions.SourceFiles[i].EndsWith(".cpp", StringComparison.OrdinalIgnoreCase) || moduleOptions.SourceFiles[i].EndsWith(".c", StringComparison.OrdinalIgnoreCase))
cppFiles.Add(moduleOptions.SourceFiles[i]);
}

View File

@@ -49,7 +49,7 @@ namespace Flax.Deps.Dependencies
foreach (var protocolPath in protocolFiles)
{
var headerFile = Path.ChangeExtension(Path.GetFileName(protocolPath), "h");
var glueFile = Path.ChangeExtension(Path.GetFileName(protocolPath), "cpp");
var glueFile = Path.ChangeExtension(Path.GetFileName(protocolPath), "c");
Utilities.Run("wayland-scanner", $"client-header {protocolPath} include/wayland/{headerFile}", null, dstPath, Utilities.RunOptions.DefaultTool);
Utilities.Run("wayland-scanner", $"private-code {protocolPath} {glueFile}", null, dstPath, Utilities.RunOptions.DefaultTool);
}

View File

@@ -333,23 +333,6 @@ namespace Flax.Build.Platforms
{
commonArgs.Add("-c");
commonArgs.Add("-pipe");
commonArgs.Add("-x");
commonArgs.Add("c++");
// C++ version
switch (compileEnvironment.CppVersion)
{
case CppVersion.Cpp14:
commonArgs.Add("-std=c++14");
break;
case CppVersion.Cpp17:
case CppVersion.Latest:
commonArgs.Add("-std=c++17");
break;
case CppVersion.Cpp20:
commonArgs.Add("-std=c++20");
break;
}
commonArgs.Add("-Wdelete-non-virtual-dtor");
commonArgs.Add("-fno-math-errno");
@@ -407,7 +390,7 @@ namespace Flax.Build.Platforms
commonArgs.Add(string.Format("-I\"{0}\"", includePath.Replace('\\', '/')));
}
// Compile all C++ files
// Compile all C/C++ files
var args = new List<string>();
foreach (var sourceFile in sourceFiles)
{
@@ -417,6 +400,30 @@ namespace Flax.Build.Platforms
// Use shared arguments
args.Clear();
args.AddRange(commonArgs);
// Language for the file
args.Add("-x");
if (Path.GetExtension(sourceFile).Equals(".c", StringComparison.OrdinalIgnoreCase))
args.Add("c");
else
{
args.Add("c++");
// C++ version
switch (compileEnvironment.CppVersion)
{
case CppVersion.Cpp14:
args.Add("-std=c++14");
break;
case CppVersion.Cpp17:
case CppVersion.Latest:
args.Add("-std=c++17");
break;
case CppVersion.Cpp20:
args.Add("-std=c++20");
break;
}
}
// Object File Name
var objFile = Path.Combine(outputPath, sourceFilename + ".o");

View File

@@ -241,11 +241,11 @@ namespace Flax.Build.Projects.VisualStudio
foreach (var file in files)
{
string fileType;
if (file.EndsWith(".h", StringComparison.OrdinalIgnoreCase) || file.EndsWith(".inl", StringComparison.OrdinalIgnoreCase))
if (file.EndsWith(".h", StringComparison.OrdinalIgnoreCase) || file.EndsWith(".inl", StringComparison.OrdinalIgnoreCase) || file.EndsWith(".hpp", StringComparison.OrdinalIgnoreCase))
{
fileType = "ClInclude";
}
else if (file.EndsWith(".cpp", StringComparison.OrdinalIgnoreCase) || file.EndsWith(".cc", StringComparison.OrdinalIgnoreCase))
else if (file.EndsWith(".cpp", StringComparison.OrdinalIgnoreCase) || file.EndsWith(".cc", StringComparison.OrdinalIgnoreCase) || file.EndsWith(".c", StringComparison.OrdinalIgnoreCase))
{
fileType = "ClCompile";
}