Support compiling third party library C files as C code

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

View File

@@ -20,7 +20,7 @@ namespace FlaxEditor.Content
} }
/// <inheritdoc /> /// <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 /> /// <inheritdoc />
public override SpriteHandle DefaultThumbnail => Editor.Instance.Icons.CPPScript128; public override SpriteHandle DefaultThumbnail => Editor.Instance.Icons.CPPScript128;

View File

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

View File

@@ -1008,7 +1008,7 @@ namespace FlaxEditor.Modules
ContentItem item; ContentItem item;
if (path.EndsWith(".cs")) if (path.EndsWith(".cs"))
item = new CSharpScriptItem(path); 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); item = new CppScriptItem(path);
else if (path.EndsWith(".shader") || path.EndsWith(".hlsl")) else if (path.EndsWith(".shader") || path.EndsWith(".hlsl"))
item = new ShaderSourceItem(path); item = new ShaderSourceItem(path);

View File

@@ -222,7 +222,7 @@ namespace FlaxEditor.Modules
outputExtension = extension; outputExtension = extension;
// Check if can place source files here // 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 // Error
Editor.LogWarning(string.Format("Cannot import \'{0}\' to \'{1}\'. The target directory cannot have scripts.", inputPath, targetLocation.Node.Path)); 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 // Discard non-source files or generated files
if ((!path.EndsWith(TEXT(".cs")) && if ((!path.EndsWith(TEXT(".cs")) &&
!path.EndsWith(TEXT(".cpp")) && !path.EndsWith(TEXT(".cpp")) &&
!path.EndsWith(TEXT(".c")) &&
!path.EndsWith(TEXT(".hpp")) &&
!path.EndsWith(TEXT(".h"))) || !path.EndsWith(TEXT(".h"))) ||
path.EndsWith(TEXT(".Gen.cs"))) path.EndsWith(TEXT(".Gen.cs")))
{
return; return;
}
ScopeLock scopeLock(_locker); ScopeLock scopeLock(_locker);
_lastSourceCodeEdited = DateTime::Now(); _lastSourceCodeEdited = DateTime::Now();

View File

@@ -127,7 +127,7 @@ namespace Flax.Build.Bindings
var headerFiles = new List<string>(moduleOptions.SourceFiles.Count / 2); var headerFiles = new List<string>(moduleOptions.SourceFiles.Count / 2);
for (int i = 0; i < moduleOptions.SourceFiles.Count; i++) 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]); headerFiles.Add(moduleOptions.SourceFiles[i]);
} }
if (headerFiles.Count == 0) if (headerFiles.Count == 0)

View File

@@ -482,7 +482,7 @@ namespace Flax.Build
var cppFiles = new List<string>(moduleOptions.SourceFiles.Count / 2); var cppFiles = new List<string>(moduleOptions.SourceFiles.Count / 2);
for (int i = 0; i < moduleOptions.SourceFiles.Count; i++) 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]); cppFiles.Add(moduleOptions.SourceFiles[i]);
} }

View File

@@ -49,7 +49,7 @@ namespace Flax.Deps.Dependencies
foreach (var protocolPath in protocolFiles) foreach (var protocolPath in protocolFiles)
{ {
var headerFile = Path.ChangeExtension(Path.GetFileName(protocolPath), "h"); 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", $"client-header {protocolPath} include/wayland/{headerFile}", null, dstPath, Utilities.RunOptions.DefaultTool);
Utilities.Run("wayland-scanner", $"private-code {protocolPath} {glueFile}", 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("-c");
commonArgs.Add("-pipe"); 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("-Wdelete-non-virtual-dtor");
commonArgs.Add("-fno-math-errno"); commonArgs.Add("-fno-math-errno");
@@ -407,7 +390,7 @@ namespace Flax.Build.Platforms
commonArgs.Add(string.Format("-I\"{0}\"", includePath.Replace('\\', '/'))); commonArgs.Add(string.Format("-I\"{0}\"", includePath.Replace('\\', '/')));
} }
// Compile all C++ files // Compile all C/C++ files
var args = new List<string>(); var args = new List<string>();
foreach (var sourceFile in sourceFiles) foreach (var sourceFile in sourceFiles)
{ {
@@ -417,6 +400,30 @@ namespace Flax.Build.Platforms
// Use shared arguments // Use shared arguments
args.Clear(); args.Clear();
args.AddRange(commonArgs); 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 // Object File Name
var objFile = Path.Combine(outputPath, sourceFilename + ".o"); var objFile = Path.Combine(outputPath, sourceFilename + ".o");

View File

@@ -241,11 +241,11 @@ namespace Flax.Build.Projects.VisualStudio
foreach (var file in files) foreach (var file in files)
{ {
string fileType; 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"; 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"; fileType = "ClCompile";
} }