Skip to content

Commit

Permalink
Add support for additional compiler arguments
Browse files Browse the repository at this point in the history
Closes #1807
  • Loading branch information
Cyberboss committed Apr 20, 2024
1 parent 94ca8d3 commit 1be9f50
Show file tree
Hide file tree
Showing 25 changed files with 4,603 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,12 @@ public abstract class DreamMakerSettings
/// </summary>
[Required]
public TimeSpan? Timeout { get; set; }

/// <summary>
/// Additional arguments added to the compiler command line.
/// </summary>
[StringLength(Limits.MaximumStringLength)]
[ResponseOptions]
public string? CompilerAdditionalArguments { get; set; }
}
}
5 changes: 5 additions & 0 deletions src/Tgstation.Server.Api/Rights/DreamMakerRights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,10 @@ public enum DreamMakerRights : ulong
/// User may modify <see cref="Models.Internal.DreamMakerSettings.Timeout"/>.
/// </summary>
SetTimeout = 1 << 8,

/// <summary>
/// User may modify <see cref="Models.Internal.DreamMakerSettings.CompilerAdditionalArguments"/>.
/// </summary>
SetCompilerArguments = 1 << 9,
}
}
11 changes: 8 additions & 3 deletions src/Tgstation.Server.Host/Components/Deployment/DreamMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ await eventConsumer.HandleEvent(

// run compiler
progressReporter.StageName = "Running Compiler";
var compileSuceeded = await RunDreamMaker(engineLock, job, cancellationToken);
var compileSuceeded = await RunDreamMaker(engineLock, job, dreamMakerSettings.CompilerAdditionalArguments, cancellationToken);

// Session takes ownership of the lock and Disposes it so save this for later
var engineVersion = engineLock.Version;
Expand Down Expand Up @@ -850,12 +850,17 @@ async ValueTask VerifyApi(
/// </summary>
/// <param name="engineLock">The <see cref="IEngineExecutableLock"/> to use.</param>
/// <param name="job">The <see cref="CompileJob"/> for the operation.</param>
/// <param name="additionalCompilerArguments">Additional arguments to be added to the compiler.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="ValueTask{TResult}"/> resulting in <see langword="true"/> if compilation succeeded, <see langword="false"/> otherwise.</returns>
async ValueTask<bool> RunDreamMaker(IEngineExecutableLock engineLock, Models.CompileJob job, CancellationToken cancellationToken)
async ValueTask<bool> RunDreamMaker(
IEngineExecutableLock engineLock,
Models.CompileJob job,
string? additionalCompilerArguments,
CancellationToken cancellationToken)
{
var environment = await engineLock.LoadEnv(logger, true, cancellationToken);
var arguments = engineLock.FormatCompilerArguments($"{job.DmeName}.{DmeExtension}");
var arguments = engineLock.FormatCompilerArguments($"{job.DmeName}.{DmeExtension}", additionalCompilerArguments);

await using var dm = await processExecutor.LaunchProcess(
engineLock.CompilerExePath,
Expand Down
11 changes: 9 additions & 2 deletions src/Tgstation.Server.Host/Components/Engine/ByondInstallation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,14 @@ public override string FormatServerArguments(
}

/// <inheritdoc />
public override string FormatCompilerArguments(string dmePath)
=> $"-clean \"{dmePath ?? throw new ArgumentNullException(nameof(dmePath))}\"";
public override string FormatCompilerArguments(string dmePath, string? additionalArguments)
{
if (String.IsNullOrWhiteSpace(additionalArguments))
additionalArguments = String.Empty;
else
additionalArguments = $"{additionalArguments.Trim()} ";

return $"-clean {additionalArguments}\"{dmePath ?? throw new ArgumentNullException(nameof(dmePath))}\"";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public string FormatServerArguments(
logFilePath);

/// <inheritdoc />
public string FormatCompilerArguments(string dmePath) => Instance.FormatCompilerArguments(dmePath);
public string FormatCompilerArguments(string dmePath, string? additionalArguments) => Instance.FormatCompilerArguments(dmePath, additionalArguments);

/// <inheritdoc />
public ValueTask StopServerProcess(ILogger logger, IProcess process, string accessIdentifier, ushort port, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public EngineInstallationBase(IIOManager installationIOManager)
}

/// <inheritdoc />
public abstract string FormatCompilerArguments(string dmePath);
public abstract string FormatCompilerArguments(string dmePath, string? additionalArguments);

/// <inheritdoc />
public abstract string FormatServerArguments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ string FormatServerArguments(
/// Return the command line arguments for compiling a given <paramref name="dmePath"/> if compilation is necessary.
/// </summary>
/// <param name="dmePath">The full path to the .dme to compile.</param>
/// <param name="additionalArguments">Optional additional arguments provided to the compiler.</param>
/// <returns>The formatted arguments <see cref="string"/>.</returns>
string FormatCompilerArguments(string dmePath);
string FormatCompilerArguments(string dmePath, string? additionalArguments);

/// <summary>
/// Kills a given engine server <paramref name="process"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,15 @@ public override string FormatServerArguments(
}

/// <inheritdoc />
public override string FormatCompilerArguments(string dmePath)
=> $"--suppress-unimplemented --notices-enabled \"{dmePath ?? throw new ArgumentNullException(nameof(dmePath))}\"";
public override string FormatCompilerArguments(string dmePath, string? additionalArguments)
{
if (String.IsNullOrWhiteSpace(additionalArguments))
additionalArguments = String.Empty;
else
additionalArguments = $"{additionalArguments.Trim()} ";

return $"--suppress-unimplemented --notices-enabled {additionalArguments}\"{dmePath ?? throw new ArgumentNullException(nameof(dmePath))}\"";
}

/// <inheritdoc />
public override async ValueTask StopServerProcess(
Expand Down
21 changes: 19 additions & 2 deletions src/Tgstation.Server.Host/Controllers/DreamMakerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ await jobManager.RegisterOperation(
| DreamMakerRights.SetApiValidationPort
| DreamMakerRights.SetSecurityLevel
| DreamMakerRights.SetApiValidationRequirement
| DreamMakerRights.SetTimeout)]
| DreamMakerRights.SetTimeout
| DreamMakerRights.SetCompilerArguments)]
[ProducesResponseType(typeof(DreamMakerResponse), 200)]
[ProducesResponseType(204)]
[ProducesResponseType(typeof(ErrorMessageResponse), 410)]
Expand All @@ -196,7 +197,8 @@ public async ValueTask<IActionResult> Update([FromBody] DreamMakerRequest model,
{
if (!dreamMakerRights.HasFlag(DreamMakerRights.SetDme))
return Forbid();
if (model.ProjectName.Length == 0)

if (model.ProjectName.Length == 0) // can't use isnullorwhitespace because linux memes
hostModel.ProjectName = null;
else
hostModel.ProjectName = model.ProjectName;
Expand Down Expand Up @@ -230,23 +232,38 @@ public async ValueTask<IActionResult> Update([FromBody] DreamMakerRequest model,
{
if (!dreamMakerRights.HasFlag(DreamMakerRights.SetSecurityLevel))
return Forbid();

hostModel.ApiValidationSecurityLevel = model.ApiValidationSecurityLevel;
}

if (model.RequireDMApiValidation.HasValue)
{
if (!dreamMakerRights.HasFlag(DreamMakerRights.SetApiValidationRequirement))
return Forbid();

hostModel.RequireDMApiValidation = model.RequireDMApiValidation;
}

if (model.Timeout.HasValue)
{
if (!dreamMakerRights.HasFlag(DreamMakerRights.SetTimeout))
return Forbid();

hostModel.Timeout = model.Timeout;
}

if (model.CompilerAdditionalArguments != null)
{
if (!dreamMakerRights.HasFlag(DreamMakerRights.SetCompilerArguments))
return Forbid();

var sanitizedArguments = model.CompilerAdditionalArguments.Trim();
if (sanitizedArguments.Length == 0)
hostModel.CompilerAdditionalArguments = null;
else
hostModel.CompilerAdditionalArguments = sanitizedArguments;
}

await DatabaseContext.Save(cancellationToken);

if (!dreamMakerRights.HasFlag(DreamMakerRights.Read))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ public async ValueTask<IActionResult> GrantPermissions(long id, CancellationToke
ApiValidationSecurityLevel = DreamDaemonSecurity.Safe,
RequireDMApiValidation = true,
Timeout = TimeSpan.FromHours(1),
CompilerAdditionalArguments = null,
},
Name = initialSettings.Name,
Online = false,
Expand Down
18 changes: 14 additions & 4 deletions src/Tgstation.Server.Host/Database/DatabaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,22 +375,22 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
/// <summary>
/// Used by unit tests to remind us to setup the correct MSSQL migration downgrades.
/// </summary>
internal static readonly Type MSLatestMigration = typeof(MSAddMinidumpsOption);
internal static readonly Type MSLatestMigration = typeof(MSAddCompilerAdditionalArguments);

/// <summary>
/// Used by unit tests to remind us to setup the correct MYSQL migration downgrades.
/// </summary>
internal static readonly Type MYLatestMigration = typeof(MYAddMinidumpsOption);
internal static readonly Type MYLatestMigration = typeof(MYAddCompilerAdditionalArguments);

/// <summary>
/// Used by unit tests to remind us to setup the correct PostgresSQL migration downgrades.
/// </summary>
internal static readonly Type PGLatestMigration = typeof(PGAddMinidumpsOption);
internal static readonly Type PGLatestMigration = typeof(PGAddCompilerAdditionalArguments);

/// <summary>
/// Used by unit tests to remind us to setup the correct SQLite migration downgrades.
/// </summary>
internal static readonly Type SLLatestMigration = typeof(SLAddMinidumpsOption);
internal static readonly Type SLLatestMigration = typeof(SLAddCompilerAdditionalArguments);

/// <inheritdoc />
#pragma warning disable CA1502 // Cyclomatic complexity
Expand Down Expand Up @@ -419,6 +419,16 @@ public async ValueTask SchemaDowngradeForServerVersion(

string BadDatabaseType() => throw new ArgumentException($"Invalid DatabaseType: {currentDatabaseType}", nameof(currentDatabaseType));

if (targetVersion < new Version(6, 5, 0))
targetMigration = currentDatabaseType switch
{
DatabaseType.MySql => nameof(MYAddMinidumpsOption),
DatabaseType.PostgresSql => nameof(PGAddMinidumpsOption),
DatabaseType.SqlServer => nameof(MSAddMinidumpsOption),
DatabaseType.Sqlite => nameof(SLAddMinidumpsOption),
_ => BadDatabaseType(),
};

if (targetVersion < new Version(6, 2, 0))
targetMigration = currentDatabaseType switch
{
Expand Down
Loading

0 comments on commit 1be9f50

Please sign in to comment.