From 7ef316f4f9e996654184e931dd3202e62aec7e2a Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 27 Dec 2021 16:00:26 +0100 Subject: [PATCH] Mac impl progress --- Source/ThirdParty/freetype/freetype.Build.cs | 1 + Source/ThirdParty/mono-2.0/mono.Build.cs | 1 + Source/ThirdParty/ogg/ogg.Build.cs | 1 + Source/ThirdParty/vorbis/vorbis.Build.cs | 1 + Source/Tools/Flax.Build/Build/DepsModule.cs | 1 + Source/Tools/Flax.Build/Build/Platform.cs | 4 +- Source/Tools/Flax.Build/Flax.Build.csproj | 2 +- .../Platforms/Linux/LinuxPlatform.cs | 4 +- .../Flax.Build/Platforms/Mac/MacPlatform.cs | 29 +++++- Source/Tools/Flax.Build/Projects/XCode.cs | 49 ---------- .../Projects/XCodeProjectGenerator.cs | 93 +++++++++++++++++++ 11 files changed, 131 insertions(+), 55 deletions(-) delete mode 100644 Source/Tools/Flax.Build/Projects/XCode.cs create mode 100644 Source/Tools/Flax.Build/Projects/XCodeProjectGenerator.cs diff --git a/Source/ThirdParty/freetype/freetype.Build.cs b/Source/ThirdParty/freetype/freetype.Build.cs index 99b50f269..f1178b0d4 100644 --- a/Source/ThirdParty/freetype/freetype.Build.cs +++ b/Source/ThirdParty/freetype/freetype.Build.cs @@ -41,6 +41,7 @@ public class freetype : DepsModule case TargetPlatform.PS5: case TargetPlatform.Android: case TargetPlatform.Switch: + case TargetPlatform.Mac: options.OutputFiles.Add(Path.Combine(depsRoot, "libfreetype.a")); break; default: throw new InvalidPlatformException(options.Platform.Target); diff --git a/Source/ThirdParty/mono-2.0/mono.Build.cs b/Source/ThirdParty/mono-2.0/mono.Build.cs index 784657d8f..e533811e6 100644 --- a/Source/ThirdParty/mono-2.0/mono.Build.cs +++ b/Source/ThirdParty/mono-2.0/mono.Build.cs @@ -71,6 +71,7 @@ public class mono : DepsModule break; } case TargetPlatform.Linux: + case TargetPlatform.Mac: options.PublicDefinitions.Add("USE_MONO_DYNAMIC_LIB"); options.DependencyFiles.Add(Path.Combine(depsRoot, "libmonosgen-2.0.so")); options.Libraries.Add(Path.Combine(depsRoot, "libmonosgen-2.0.so")); diff --git a/Source/ThirdParty/ogg/ogg.Build.cs b/Source/ThirdParty/ogg/ogg.Build.cs index 7afd1a306..d7b0d1ef7 100644 --- a/Source/ThirdParty/ogg/ogg.Build.cs +++ b/Source/ThirdParty/ogg/ogg.Build.cs @@ -41,6 +41,7 @@ public class ogg : DepsModule case TargetPlatform.PS5: case TargetPlatform.Android: case TargetPlatform.Switch: + case TargetPlatform.Mac: options.OutputFiles.Add(Path.Combine(depsRoot, "libogg.a")); break; default: throw new InvalidPlatformException(options.Platform.Target); diff --git a/Source/ThirdParty/vorbis/vorbis.Build.cs b/Source/ThirdParty/vorbis/vorbis.Build.cs index ffc48831a..7ec134eaf 100644 --- a/Source/ThirdParty/vorbis/vorbis.Build.cs +++ b/Source/ThirdParty/vorbis/vorbis.Build.cs @@ -41,6 +41,7 @@ public class vorbis : DepsModule case TargetPlatform.Linux: case TargetPlatform.Android: case TargetPlatform.Switch: + case TargetPlatform.Mac: options.OutputFiles.Add(Path.Combine(depsRoot, "libvorbis.a")); options.OutputFiles.Add(Path.Combine(depsRoot, "libvorbisenc.a")); options.OutputFiles.Add(Path.Combine(depsRoot, "libvorbisfile.a")); diff --git a/Source/Tools/Flax.Build/Build/DepsModule.cs b/Source/Tools/Flax.Build/Build/DepsModule.cs index dfd9defce..0a119e294 100644 --- a/Source/Tools/Flax.Build/Build/DepsModule.cs +++ b/Source/Tools/Flax.Build/Build/DepsModule.cs @@ -33,6 +33,7 @@ namespace Flax.Build case TargetPlatform.PS5: case TargetPlatform.Android: case TargetPlatform.Switch: + case TargetPlatform.Mac: options.OutputFiles.Add(Path.Combine(path, string.Format("lib{0}.a", name))); break; default: throw new InvalidPlatformException(options.Platform.Target); diff --git a/Source/Tools/Flax.Build/Build/Platform.cs b/Source/Tools/Flax.Build/Build/Platform.cs index a95c9c56c..2d051216b 100644 --- a/Source/Tools/Flax.Build/Build/Platform.cs +++ b/Source/Tools/Flax.Build/Build/Platform.cs @@ -36,9 +36,9 @@ namespace Flax.Build case PlatformID.WinCE: return TargetPlatform.Windows; case PlatformID.Unix: { - var p = new Process + Process p = new Process { - StartInfo = new ProcessStartInfo + StartInfo = { UseShellExecute = false, RedirectStandardOutput = true, diff --git a/Source/Tools/Flax.Build/Flax.Build.csproj b/Source/Tools/Flax.Build/Flax.Build.csproj index fdfce10d2..ad1e2a1fa 100644 --- a/Source/Tools/Flax.Build/Flax.Build.csproj +++ b/Source/Tools/Flax.Build/Flax.Build.csproj @@ -161,7 +161,7 @@ - + diff --git a/Source/Tools/Flax.Build/Platforms/Linux/LinuxPlatform.cs b/Source/Tools/Flax.Build/Platforms/Linux/LinuxPlatform.cs index 4455b926e..c30a8092a 100644 --- a/Source/Tools/Flax.Build/Platforms/Linux/LinuxPlatform.cs +++ b/Source/Tools/Flax.Build/Platforms/Linux/LinuxPlatform.cs @@ -37,7 +37,7 @@ namespace Flax.Build.Platforms public LinuxPlatform() { // Try to use system compiler - if (Environment.OSVersion.Platform == PlatformID.Unix) + if (Platform.BuildTargetPlatform == TargetPlatform.Linux) { // Pick the newest compiler (overriden by specified in command line) if (Which(Compiler) != null) @@ -62,7 +62,7 @@ namespace Flax.Build.Platforms Log.Verbose($"Using native Linux toolchain (compiler {Compiler})"); HasRequiredSDKsInstalled = true; } - else + else if (Platform.BuildTargetPlatform != TargetPlatform.Mac) { // Check if Linux toolchain is installed string toolchainName = "v13_clang-7.0.1-centos7"; diff --git a/Source/Tools/Flax.Build/Platforms/Mac/MacPlatform.cs b/Source/Tools/Flax.Build/Platforms/Mac/MacPlatform.cs index ab21dc54b..4ef2e8bc2 100644 --- a/Source/Tools/Flax.Build/Platforms/Mac/MacPlatform.cs +++ b/Source/Tools/Flax.Build/Platforms/Mac/MacPlatform.cs @@ -1,5 +1,8 @@ // Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. +using System; +using System.IO; +using System.Diagnostics; using Flax.Build.Projects; namespace Flax.Build.Platforms @@ -48,7 +51,31 @@ namespace Flax.Build.Platforms if (Platform.BuildTargetPlatform != TargetPlatform.Mac) return; - throw new System.NotImplementedException("TODO: detect MacSDK installation"); + try + { + // Check if XCode is installed + Process p = new Process + { + StartInfo = + { + FileName = "xcode-select", + Arguments = "--print-path", + UseShellExecute = false, + CreateNoWindow = true, + RedirectStandardOutput = true, + } + }; + p.Start(); + string xcodePath = p.StandardOutput.ReadToEnd().Trim(); + if (string.IsNullOrEmpty(xcodePath) || !Directory.Exists(xcodePath)) + throw new Exception(xcodePath); + Log.Verbose(string.Format("Found XCode at {0}", xcodePath)); + HasRequiredSDKsInstalled = true; + } + catch + { + Log.Warning("Missing XCode. Cannot build for Mac platform."); + } } /// diff --git a/Source/Tools/Flax.Build/Projects/XCode.cs b/Source/Tools/Flax.Build/Projects/XCode.cs deleted file mode 100644 index 56fdd894e..000000000 --- a/Source/Tools/Flax.Build/Projects/XCode.cs +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. - -using System; - -namespace Flax.Build.Projects -{ - /// - /// Project generator for XCode. - /// - public class XCodeProjectGenerator : ProjectGenerator - { - /// - /// Initializes a new instance of the class. - /// - public XCodeProjectGenerator() - { - } - - /// - public override string ProjectFileExtension => "xcodeproj"; - - /// - public override string SolutionFileExtension => string.Empty; - - /// - public override TargetType? Type => null; - - /// - public override Project CreateProject() - { - return new Project - { - Generator = this, - }; - } - - /// - public override void GenerateProject(Project project) - { - throw new NotImplementedException("TODO: XCode"); - } - - /// - public override void GenerateSolution(Solution solution) - { - throw new NotImplementedException("TODO: XCode"); - } - } -} diff --git a/Source/Tools/Flax.Build/Projects/XCodeProjectGenerator.cs b/Source/Tools/Flax.Build/Projects/XCodeProjectGenerator.cs new file mode 100644 index 000000000..05b8facc2 --- /dev/null +++ b/Source/Tools/Flax.Build/Projects/XCodeProjectGenerator.cs @@ -0,0 +1,93 @@ +// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace Flax.Build.Projects +{ + /// + /// Project generator for XCode. + /// + public class XCodeProjectGenerator : ProjectGenerator + { + private Random _rand = new Random(1995); + private byte[] _randBytes = new byte[12]; + + /// + /// Initializes a new instance of the class. + /// + public XCodeProjectGenerator() + { + } + + /// + public override string ProjectFileExtension => "pbxproj"; + + /// + public override string SolutionFileExtension => "xcodeproj"; + + /// + public override TargetType? Type => null; + + /// + public override Project CreateProject() + { + return new Project + { + Generator = this, + }; + } + + /// + public override void GenerateProject(Project project) + { + Console.WriteLine(project.Path); + var contents = new StringBuilder(); + + contents.AppendLine("// !$*UTF8*$!"); + contents.AppendLine("{"); + contents.AppendLine("\tarchiveVersion = 1;"); + contents.AppendLine("\tclasses = {"); + contents.AppendLine("\t};"); + contents.AppendLine("\tobjectVersion = 46;"); + contents.AppendLine("\tobjects = {"); + + contents.AppendLine("\t};"); + contents.AppendLine("\trootObject = " + GetRandomGuid() + " /* Project object */;"); + contents.AppendLine("}"); + + Utilities.WriteFileIfChanged(Path.Combine(project.Path), contents.ToString()); + } + + /// + public override void GenerateSolution(Solution solution) + { + Directory.CreateDirectory(solution.Path); + + var contents = new StringBuilder(); + + contents.AppendLine(""); + contents.AppendLine(""); + foreach (var project in solution.Projects) + { + } + contents.AppendLine(""); + + Utilities.WriteFileIfChanged(Path.Combine(solution.Path, solution.Name + ".xcworkspace", "contents.xcworkspacedata"), contents.ToString()); + } + + private string GetRandomGuid() + { + _rand.NextBytes(_randBytes); + string result = string.Empty; + for (int i = 0; i < 12; i++) + { + result += _randBytes[i].ToString("X2"); + } + return result; + } + } +}