Skip to content

Commit

Permalink
Merge branch 'xcode-custom-fastbuild-launcher' into 'main'
Browse files Browse the repository at this point in the history
Support properly custom fastbuild launcher for xcode projects

See merge request Sharpmake/sharpmake!544
  • Loading branch information
jspelletier committed Aug 23, 2024
2 parents 50e69d1 + 9140bb3 commit 7a5e318
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 68 deletions.
64 changes: 15 additions & 49 deletions Sharpmake.Generators/Apple/XCodeProj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -570,11 +570,12 @@ private void PrepareSections(XCodeGenerationContext context, List<Project.Config
var masterBff = conf.FastBuildMasterBffList.FirstOrDefault();
if (masterBff != null)
{
var buildToolPath = Util.SimplifyPath(FastBuildSettings.FastBuildMakeCommand);
string fastBuildCommandLine = ProjectLegacyTarget.BuildArgumentsStringByCommandLineOptions(conf, true);
var fastbuildMakeArguments = FastBuildSettings.MakeCommandGenerator.GetArguments(FastBuildMakeCommandGenerator.BuildType.Build, conf, fastBuildCommandLine);
var fastbuildMakeExe = FastBuildSettings.MakeCommandGenerator.GetExecutablePath(conf);
var buildWorkingDirectory = Path.GetDirectoryName(masterBff);
var buildArgumentsString = ProjectLegacyTarget.BuildArgumentsStringByCommandLineOptions(masterBff, true);
var fastbuildCmd = @$"pushd {buildWorkingDirectory}
{buildToolPath} {buildArgumentsString}
{fastbuildMakeExe} {fastbuildMakeArguments}
exit_code=$?
if (( $exit_code != 0 )); then
exit $exit_code
Expand Down Expand Up @@ -730,7 +731,7 @@ private void PrepareSections(XCodeGenerationContext context, List<Project.Config
}
else
{
target = new ProjectLegacyTarget(xCodeTargetName, targetOutputFile, configurationListForNativeTarget, masterBffFilePath);
target = new ProjectLegacyTarget(xCodeTargetName, targetOutputFile, configurationListForNativeTarget, firstConf);
}
target.ResourcesBuildPhase = _resourcesBuildPhases[xCodeTargetName];
if (targetUnitTest != null)
Expand Down Expand Up @@ -2501,82 +2502,47 @@ public override void GetAdditionalResolverParameters(ProjectItem item, Resolver

private class ProjectLegacyTarget : ProjectTarget
{
private string _masterBffFilePath;
private Project.Configuration _conf;

public ProjectLegacyTarget(string identifier, ProjectOutputFile outputFile, ProjectConfigurationList configurationList, string masterBffFilePath)
public ProjectLegacyTarget(string identifier, ProjectOutputFile outputFile, ProjectConfigurationList configurationList, Project.Configuration conf)
: base(ItemSection.PBXLegacyTarget, identifier, outputFile, configurationList)
{
_masterBffFilePath = masterBffFilePath;
_conf = conf;
}

internal static string BuildArgumentsStringByCommandLineOptions(string masterBffFilePath, bool forShellCmd)
internal static string BuildArgumentsStringByCommandLineOptions(Project.Configuration conf, bool forShellCmd)
{
var fastBuildCommandLineOptions = new List<string>();

fastBuildCommandLineOptions.Add(FastBuild.UtilityMethods.GetFastBuildCommandLineArguments(conf));
fastBuildCommandLineOptions.Add("-config " + conf.FastBuildMasterBffList.First());
fastBuildCommandLineOptions.Add(forShellCmd ? "$FASTBUILD_TARGET" : "$(FASTBUILD_TARGET)"); // special envvar hardcoded in the template

if (FastBuildSettings.FastBuildUseIDE)
fastBuildCommandLineOptions.Add("-ide");

if (FastBuildSettings.FastBuildReport)
fastBuildCommandLineOptions.Add("-report");

if (FastBuildSettings.FastBuildNoSummaryOnError)
fastBuildCommandLineOptions.Add("-nosummaryonerror");

if (FastBuildSettings.FastBuildSummary)
fastBuildCommandLineOptions.Add("-summary");

if (FastBuildSettings.FastBuildVerbose)
fastBuildCommandLineOptions.Add("-verbose");

if (FastBuildSettings.FastBuildMonitor)
fastBuildCommandLineOptions.Add("-monitor");

if (FastBuildSettings.FastBuildWait)
fastBuildCommandLineOptions.Add("-wait");

if (FastBuildSettings.FastBuildNoStopOnError)
fastBuildCommandLineOptions.Add("-nostoponerror");

if (FastBuildSettings.FastBuildFastCancel)
fastBuildCommandLineOptions.Add("-fastcancel");

if (FastBuildSettings.FastBuildNoUnity)
fastBuildCommandLineOptions.Add("-nounity");

if (FastBuildSettings.FastBuildDistribution)
fastBuildCommandLineOptions.Add("-dist");

if (!string.IsNullOrEmpty(FastBuildSettings.FastBuildCustomArguments))
fastBuildCommandLineOptions.Add(FastBuildSettings.FastBuildCustomArguments);

fastBuildCommandLineOptions.Add("-config " + masterBffFilePath);

return string.Join(" ", fastBuildCommandLineOptions);
}

public string BuildArgumentsString
{
get
{
return BuildArgumentsStringByCommandLineOptions(Path.GetFileName(_masterBffFilePath), false);
string fastbuildArgs = BuildArgumentsStringByCommandLineOptions(_conf, false);
return FastBuildSettings.MakeCommandGenerator.GetArguments(FastBuildMakeCommandGenerator.BuildType.Build, _conf, fastbuildArgs);
}
}

public string BuildToolPath
{
get
{
return XCodeUtil.XCodeFormatSingleItem(Util.SimplifyPath(FastBuildSettings.FastBuildMakeCommand));
return FastBuildSettings.MakeCommandGenerator.GetExecutablePath(_conf);
}
}

public string BuildWorkingDirectory
{
get
{
return XCodeUtil.XCodeFormatSingleItem(Path.GetDirectoryName(_masterBffFilePath));
return XCodeUtil.XCodeFormatSingleItem(Path.GetDirectoryName(_conf.FastBuildMasterBffList.First()));
}
}
}
Expand Down
43 changes: 30 additions & 13 deletions Sharpmake.Generators/FastBuild/Bff.Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,25 +267,42 @@ public override string Resolve(string rootPath, string bffFilePath, Resolver res
}
}

// This NMake command generator is for supporting legacy code without any client code change.
internal class FastBuildDefaultNMakeCommandGenerator : FastBuildMakeCommandGenerator
/// <summary>
/// This class is used as a helper for generating fastbuild commands for vcxproj and xcode projects.
/// By default is uses the default fastbuild executable defined in FastBuildSettings but scripts could define another class to
/// to launch a totally different launcher with other parameters.
/// </summary>
internal class FastBuildDefaultCommandGenerator : FastBuildMakeCommandGenerator
{
public override string GetCommand(BuildType buildType, Sharpmake.Project.Configuration conf, string fastbuildArguments)
public override string GetExecutablePath(Sharpmake.Project.Configuration conf)
{
Project project = conf.Project;
string fastBuildShortProjectName = Bff.GetShortProjectName(project, conf);
string fastbuildMakeCommand = FastBuildSettings.FastBuildMakeCommand;
if (fastbuildMakeCommand != null)
{
if (!Path.IsPathRooted(FastBuildSettings.FastBuildMakeCommand))
fastbuildMakeCommand = Path.Combine(conf.Project.RootPath, FastBuildSettings.FastBuildMakeCommand);
fastbuildMakeCommand = Util.SimplifyPath(fastbuildMakeCommand);
}
return fastbuildMakeCommand ?? "<Please define FastBuildSettings.FastBuildMakeCommand>";
}

string makePath = FastBuildSettings.FastBuildMakeCommand;
if (!Path.IsPathRooted(FastBuildSettings.FastBuildMakeCommand))
makePath = conf.Project.RootPath + Path.DirectorySeparatorChar + FastBuildSettings.FastBuildMakeCommand;
makePath = Util.SimplifyPath(makePath);
public override string GetArguments(BuildType buildType, Sharpmake.Project.Configuration conf, string fastbuildArguments)
{
// Note: XCode is special, the target identifier is written in the xcode project file for each target using the FASTBUILD_TARGET special variable.
string targetIdentifier = "";
if (!conf.Target.TryGetFragment<DevEnv>(out DevEnv devEnv) || devEnv != DevEnv.xcode)
{
targetIdentifier = GetTargetIdentifier(conf);
}

string fastBuildExecutable = Util.PathGetRelative(conf.ProjectPath, makePath, true);
string buildCommand = buildType == BuildType.Rebuild ? " -clean" : "";

string rebuildCmd = buildType == BuildType.Rebuild ? " -clean" : "";
return $@"{buildCommand} {targetIdentifier} {fastbuildArguments}";
}

// $(ProjectDir) has a trailing slash
return $@"""$(ProjectDir){fastBuildExecutable}""{rebuildCmd} {fastBuildShortProjectName} {fastbuildArguments}";
public override string GetTargetIdentifier(Sharpmake.Project.Configuration conf)
{
return Bff.GetShortProjectName(conf.Project, conf);
}
}

Expand Down
6 changes: 6 additions & 0 deletions Sharpmake.Generators/GeneratorManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public void InitializeBuilder(Builder builder)
Bff.InitializeBuilder(builder);
}

public void BeforeGenerate()
{
// Insure we always have a command generator before starting the actual generation
FastBuildSettings.MakeCommandGenerator ??= new Bff.FastBuildDefaultCommandGenerator();
}

public void Generate(Builder builder,
Project project,
List<Project.Configuration> configurations,
Expand Down
9 changes: 5 additions & 4 deletions Sharpmake.Generators/VisualStudio/Vcxproj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public GenerationContext(Builder builder, string projectPath, Project project, I

PresentPlatforms = ProjectConfigurations.Select(conf => conf.Platform).Distinct().ToDictionary(p => p, p => PlatformRegistry.Get<IPlatformVcxproj>(p));

FastBuildMakeCommandGenerator = FastBuildSettings.MakeCommandGenerator ?? new Bff.FastBuildDefaultNMakeCommandGenerator();
FastBuildMakeCommandGenerator = FastBuildSettings.MakeCommandGenerator;
}

public void Reset()
Expand Down Expand Up @@ -520,10 +520,11 @@ private void GenerateImpl(GenerationContext context, IList<string> generatedFile

commandLine += " -config $(SolutionName)" + FastBuildSettings.FastBuildConfigFileExtension;

string makeExecutable = context.FastBuildMakeCommandGenerator.GetExecutablePath(conf);
using (fileGenerator.Declare("relativeMasterBffPath", "$(SolutionDir)"))
using (fileGenerator.Declare("fastBuildMakeCommandBuild", context.FastBuildMakeCommandGenerator.GetCommand(FastBuildMakeCommandGenerator.BuildType.Build, conf, commandLine)))
using (fileGenerator.Declare("fastBuildMakeCommandRebuild", context.FastBuildMakeCommandGenerator.GetCommand(FastBuildMakeCommandGenerator.BuildType.Rebuild, conf, commandLine)))
using (fileGenerator.Declare("fastBuildMakeCommandCompileFile", context.FastBuildMakeCommandGenerator.GetCommand(FastBuildMakeCommandGenerator.BuildType.CompileFile, conf, commandLine)))
using (fileGenerator.Declare("fastBuildMakeCommandBuild", $"{makeExecutable} {context.FastBuildMakeCommandGenerator.GetArguments(FastBuildMakeCommandGenerator.BuildType.Build, conf, commandLine)}"))
using (fileGenerator.Declare("fastBuildMakeCommandRebuild", $"{makeExecutable} {context.FastBuildMakeCommandGenerator.GetArguments(FastBuildMakeCommandGenerator.BuildType.Rebuild, conf, commandLine)}"))
using (fileGenerator.Declare("fastBuildMakeCommandCompileFile", $"{makeExecutable} {context.FastBuildMakeCommandGenerator.GetArguments(FastBuildMakeCommandGenerator.BuildType.CompileFile, conf, commandLine)}"))
{
platformVcxproj.GenerateProjectConfigurationFastBuildMakeFile(context, fileGenerator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public virtual void SelectCompilerOptions(IGenerationContext context)

if (conf.IsFastBuild)
{
options["FastBuildTarget"] = Bff.GetShortProjectName(project, conf);
options["FastBuildTarget"] = FastBuildSettings.MakeCommandGenerator.GetTargetIdentifier(conf);
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions Sharpmake/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,8 @@ public IDictionary<Type, GenerationOutput> Generate()
using (new Util.StopwatchProfiler(ms => { LogWriteLine(" generation done in {0:0.0} sec", ms / 1000.0f); }))
using (CreateProfilingScope("Generation"))
{
_getGeneratorsManagerCallBack().BeforeGenerate();

var projects = new List<Project>(_projects.Values);
var solutions = new List<Solution>(_solutions.Values);

Expand Down
4 changes: 3 additions & 1 deletion Sharpmake/FastBuildSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public enum BuildType
CompileFile
};

public abstract string GetCommand(BuildType buildType, Sharpmake.Project.Configuration conf, string fastbuildArguments);
public abstract string GetTargetIdentifier(Sharpmake.Project.Configuration conf);
public abstract string GetExecutablePath(Sharpmake.Project.Configuration conf);
public abstract string GetArguments(BuildType buildType, Sharpmake.Project.Configuration conf, string fastbuildArguments);
}


Expand Down
2 changes: 2 additions & 0 deletions Sharpmake/GeneratorManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ void Generate(Builder builder,
public interface IGeneratorManager : IProjectGenerator, ISolutionGenerator
{
void InitializeBuilder(Builder builder);

void BeforeGenerate();
}
}

0 comments on commit 7a5e318

Please sign in to comment.