Skip to content

Commit

Permalink
Support --subsystem linker flag by way of OutputType (added `WinE…
Browse files Browse the repository at this point in the history
…xe`).

Closes #43.
  • Loading branch information
alexrp committed Jan 4, 2024
1 parent cd3a976 commit 9006324
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 22 deletions.
4 changes: 3 additions & 1 deletion doc/configuration/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/samples/cexe/cexe.cproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Vezel.Zig.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<OutputType>WinExe</OutputType>
</PropertyGroup>

<ItemGroup>
Expand Down
21 changes: 12 additions & 9 deletions src/sdk/ZigCompile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
Expand All @@ -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");

Expand All @@ -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)
{
Expand Down Expand Up @@ -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: " ");
Expand Down
1 change: 1 addition & 0 deletions src/sdk/ZigOutputType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ namespace Vezel.Zig.Tasks;
public enum ZigOutputType
{
Exe,
WinExe,
Library,
}
6 changes: 3 additions & 3 deletions src/sdk/build/Vezel.Zig.Sdk.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
<Compile Include="**/*.cxx"
Excludes="$(DefaultItemExcludes); $(DefaultExcludesInProjectFolder)"
Condition="'$(CompilerMode)' == 'Cxx'" />
<Compile Include="main.zig"
Condition="'$(CompilerMode)' == 'Zig' and '$(OutputType)' == 'Exe'" />
<Compile Include="$(AssemblyName).zig"
Condition="'$(CompilerMode)' == 'Zig' and '$(OutputType)' != 'Exe'" />
Condition="'$(CompilerMode)' == 'Zig' and '$(OutputType)' == 'Library'" />
<Compile Include="main.zig"
Condition="'$(CompilerMode)' == 'Zig' and '$(OutputType)' != 'Library'" />
</ItemGroup>
</Project>
8 changes: 4 additions & 4 deletions src/sdk/build/Vezel.Zig.Sdk.Cross.targets
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
<IsCrossExecuting Condition="'$(IsCrossCompilingSystem)' == 'true'">true</IsCrossExecuting>
</PropertyGroup>

<PropertyGroup Condition="'$(OutputType)' == 'Exe'">
<TargetSuffix>$(TargetExeSuffix)</TargetSuffix>
</PropertyGroup>

<PropertyGroup Condition="'$(OutputType)' == 'Library'">
<TargetPrefix>$(TargetLibraryPrefix)</TargetPrefix>
<TargetSuffix>$(TargetLibrarySuffix)</TargetSuffix>
</PropertyGroup>

<PropertyGroup Condition="'$(OutputType)' != 'Library'">
<TargetSuffix>$(TargetExeSuffix)</TargetSuffix>
</PropertyGroup>

<!--
These are the properties used by the rest of the MSBuild ecosystem. We can
now set them based on the conditional logic above.
Expand Down
2 changes: 1 addition & 1 deletion src/sdk/build/Vezel.Zig.Sdk.Pack.targets
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
Returns="@(_NativeBinary)">
<PropertyGroup>
<_PackageNativePath>runtimes</_PackageNativePath>
<_PackageNativePath Condition="'$(OutputType)' == 'Exe'">tools</_PackageNativePath>
<_PackageNativePath Condition="'$(OutputType)' != 'Library'">tools</_PackageNativePath>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/sdk/build/Vezel.Zig.Sdk.Run.targets
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project>
<PropertyGroup Condition="'$(RunCommand)' == '' and '$(OutputType)' == 'Exe' and '$(EmulatorCommand)' != ''">
<PropertyGroup Condition="'$(RunCommand)' == '' and '$(OutputType)' != 'Library' and '$(EmulatorCommand)' != ''">
<RunCommand>$(EmulatorCommand)</RunCommand>
<RunArguments>$(EmulatorArgumentPrefix)&quot;$(TargetPath)&quot;$(EmulatorArgumentSuffix)$(StartArguments)</RunArguments>
</PropertyGroup>

<PropertyGroup Condition="'$(RunCommand)' == '' and '$(OutputType)' == 'Exe'">
<PropertyGroup Condition="'$(RunCommand)' == '' and '$(OutputType)' != 'Library'">
<RunCommand>$(TargetPath)</RunCommand>
<RunArguments>$(StartArguments)</RunArguments>
</PropertyGroup>
Expand All @@ -16,7 +16,7 @@

<Target Name="_UnableToRun">
<Error Text="A project with output type '$(OutputType)' cannot be executed"
Condition="'$(OutputType)' != 'Exe'" />
Condition="'$(OutputType)' == 'Library'" />
<Error Text="A suitable binary emulator is not available for '$(HostRuntimeIdentifier)' -> '$(TargetRuntimeIdentifier)'"
Condition="'$(EmulatorCommand)' == ''" />
</Target>
Expand Down

0 comments on commit 9006324

Please sign in to comment.