From 9006324be96dd4f05761b0426f94b3f23d16e957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Thu, 4 Jan 2024 19:21:54 +0100 Subject: [PATCH] Support `--subsystem` linker flag by way of `OutputType` (added `WinExe`). Closes #43. --- doc/configuration/properties.md | 4 +++- src/samples/cexe/cexe.cproj | 2 +- src/sdk/ZigCompile.cs | 21 ++++++++++++--------- src/sdk/ZigOutputType.cs | 1 + src/sdk/build/Vezel.Zig.Sdk.Build.props | 6 +++--- src/sdk/build/Vezel.Zig.Sdk.Cross.targets | 8 ++++---- src/sdk/build/Vezel.Zig.Sdk.Pack.targets | 2 +- src/sdk/build/Vezel.Zig.Sdk.Run.targets | 6 +++--- 8 files changed, 28 insertions(+), 22 deletions(-) diff --git a/doc/configuration/properties.md b/doc/configuration/properties.md index c54fbc4..8b78ad9 100644 --- a/doc/configuration/properties.md +++ b/doc/configuration/properties.md @@ -15,7 +15,9 @@ historical reasons. * `AssemblyName`: Name of the project. By default, this is set to the file name of the project file. Used to compute the final binary name (e.g. `foo` becomes `libfoo.so`). -* `OutputType` (`Exe`, `Library`): Output binary type. Defaults to `Library`. +* `OutputType` (`Exe`, `WinExe`, `Library`): Output binary type. When targeting + Windows and building executables, `Exe` and `WinExe` will target the CUI and + GUI subsystems, respectively. Defaults to `Library`. * `IsTestable` (`true`, `false`): Enable/disable `dotnet test` for Zig projects. Defaults to `true`. * `IsPackable` (`true`, `false`): Enable/disable `dotnet pack`. Defaults to diff --git a/src/samples/cexe/cexe.cproj b/src/samples/cexe/cexe.cproj index 39997e2..b7cf470 100644 --- a/src/samples/cexe/cexe.cproj +++ b/src/samples/cexe/cexe.cproj @@ -1,6 +1,6 @@ - Exe + WinExe diff --git a/src/sdk/ZigCompile.cs b/src/sdk/ZigCompile.cs index efbd42b..eff479f 100644 --- a/src/sdk/ZigCompile.cs +++ b/src/sdk/ZigCompile.cs @@ -211,8 +211,8 @@ protected override string GenerateCommandLineCommands() { (ZigCompilerMode.C, _) => "cc", (ZigCompilerMode.Cxx, _) => "c++", - (ZigCompilerMode.Zig, ZigOutputType.Exe) => "build-exe", (ZigCompilerMode.Zig, ZigOutputType.Library) => "build-lib", + (ZigCompilerMode.Zig, _) => "build-exe", (ZigCompilerMode.Test, _) => "test", _ => throw new Exception(), }); @@ -224,14 +224,7 @@ protected override string GenerateCommandLineCommands() builder.AppendSwitch($"-target {TargetTriple.ToLowerInvariant()}"); - if (_outputType == ZigOutputType.Exe) - { - builder.AppendSwitch("-fPIE"); - - if (_symbolExports == ZigSymbolExports.All) - builder.AppendSwitch("-rdynamic"); - } - else + if (_outputType == ZigOutputType.Library) { builder.AppendSwitch("-fPIC"); @@ -247,6 +240,13 @@ protected override string GenerateCommandLineCommands() builder.AppendSwitch(isZig ? $"-z {un}defs" : $"-Wl,-z,{un}defs"); builder.AppendSwitch(isZig ? $"-f{no}allow-shlib-undefined" : $"-Wl,--{no}allow-shlib-undefined"); } + else + { + builder.AppendSwitch("-fPIE"); + + if (_symbolExports == ZigSymbolExports.All) + builder.AppendSwitch("-rdynamic"); + } if (isZig) { @@ -604,6 +604,9 @@ void TryAppendWarningSwitch(string name) builder.AppendSwitch(isZig ? "-z origin" : "-Wl,-z,origin"); builder.AppendSwitchIfNotNull(isZig ? "-rpath " : "-Wl,-rpath,", "$ORIGIN"); + builder.AppendSwitchIfNotNull( + isZig ? "--subsystem " : "-Wl,--subsystem,", _outputType == ZigOutputType.WinExe ? "windows" : "console"); + // TODO: https://github.com/vezel-dev/zig-sdk/issues/8 builder.AppendFileNamesIfNotNull(Sources, delimiter: " "); diff --git a/src/sdk/ZigOutputType.cs b/src/sdk/ZigOutputType.cs index 9252811..8552601 100644 --- a/src/sdk/ZigOutputType.cs +++ b/src/sdk/ZigOutputType.cs @@ -3,5 +3,6 @@ namespace Vezel.Zig.Tasks; public enum ZigOutputType { Exe, + WinExe, Library, } diff --git a/src/sdk/build/Vezel.Zig.Sdk.Build.props b/src/sdk/build/Vezel.Zig.Sdk.Build.props index f5e920f..8872365 100644 --- a/src/sdk/build/Vezel.Zig.Sdk.Build.props +++ b/src/sdk/build/Vezel.Zig.Sdk.Build.props @@ -20,9 +20,9 @@ - + Condition="'$(CompilerMode)' == 'Zig' and '$(OutputType)' == 'Library'" /> + diff --git a/src/sdk/build/Vezel.Zig.Sdk.Cross.targets b/src/sdk/build/Vezel.Zig.Sdk.Cross.targets index 8ad46aa..28c9e92 100644 --- a/src/sdk/build/Vezel.Zig.Sdk.Cross.targets +++ b/src/sdk/build/Vezel.Zig.Sdk.Cross.targets @@ -31,15 +31,15 @@ true - - $(TargetExeSuffix) - - $(TargetLibraryPrefix) $(TargetLibrarySuffix) + + $(TargetExeSuffix) + +