Skip to content

Commit

Permalink
Consolidate LocateNativeCompiler target (#103375)
Browse files Browse the repository at this point in the history
* Consolidate LocateNativeCompiler target

* Set install_name on all Apple platforms

---------

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
  • Loading branch information
am11 and jkotas authored Oct 16, 2024
1 parent 7bf8802 commit c627f38
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 85 deletions.
32 changes: 5 additions & 27 deletions eng/testing/linker/project.csproj.template
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
<NETCoreAppMaximumVersion>{NetCoreAppMaximumVersion}</NETCoreAppMaximumVersion>
<UseMonoRuntime>{UseMonoRuntime}</UseMonoRuntime>
<RuntimeIdentifier>{RuntimeIdentifier}</RuntimeIdentifier>
<PublishAot>{PublishAot}</PublishAot>
<AppHostSourcePath>{AppHostSourcePath}</AppHostSourcePath>
<SingleFileHostSourcePath>{SingleFileHostSourcePath}</SingleFileHostSourcePath>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

<!-- Implicitly force 'UseNativeAotForComponents' for AOT tests -->
<PublishAot>{PublishAot}</PublishAot>
<UseNativeAotForComponents Condition="'$(PublishAot)' == 'true'">true</UseNativeAotForComponents>

<!-- wasm specific -->
<MonoAOTCompilerDir>{MonoAOTCompilerDir}</MonoAOTCompilerDir>
<MonoProjectRoot>{MonoProjectRoot}</MonoProjectRoot>
Expand Down Expand Up @@ -59,14 +62,9 @@
<IlcSdkPath>{IlcSdkPath}</IlcSdkPath>
<IlcFrameworkPath>{IlcFrameworkPath}</IlcFrameworkPath>
<IlcFrameworkNativePath>{IlcFrameworkNativePath}</IlcFrameworkNativePath>
<SysRoot Condition="('$(CrossBuild)' == 'true' or '$(BuildArchitecture)' != '$(TargetArchitecture)') and '$(ROOTFS_DIR)' != ''">$(ROOTFS_DIR)</SysRoot>
<CoreCLRBuildIntegrationDir>{CoreCLRBuildIntegrationDir}</CoreCLRBuildIntegrationDir>
</PropertyGroup>

<ItemGroup>
<CustomLinkerArg Condition="'$(CrossBuild)' == 'true' and '$(_hostArchitecture)' == '$(_targetArchitecture)' and '$(ROOTFS_DIR)' != ''" Include="--gcc-toolchain=$(ROOTFS_DIR)/usr" />
</ItemGroup>

<ItemGroup>
{RuntimeHostConfigurationOptions}
</ItemGroup>
Expand All @@ -81,28 +79,8 @@
</ItemGroup>
</Target>

<Target Name="LocateNativeCompiler"
Condition="'$(PublishAot)' == 'true' and '$(_hostOS)' != 'win'"
BeforeTargets="SetupOSSpecificProps">
<PropertyGroup>
<CppCompilerAndLinker Condition="'$(CppCompilerAndLinker)' == ''">clang</CppCompilerAndLinker>
</PropertyGroup>

<Exec Command="sh -c 'build_arch=&quot;$(TargetArchitecture)&quot; compiler=&quot;$(CppCompilerAndLinker)&quot; . &quot;$(RepositoryEngineeringDir)/common/native/init-compiler.sh&quot; &amp;&amp; echo &quot;$CC;$LDFLAGS&quot;' 2>/dev/null"
EchoOff="true"
ConsoleToMsBuild="true"
StandardOutputImportance="Low">
<Output TaskParameter="ConsoleOutput" PropertyName="_CC_LDFLAGS" />
</Exec>

<PropertyGroup>
<CppLinker>$(_CC_LDFLAGS.SubString(0, $(_CC_LDFLAGS.IndexOf(';'))))</CppLinker>
<_LDFLAGS>$(_CC_LDFLAGS.SubString($([MSBuild]::Add($(_CC_LDFLAGS.IndexOf(';')), 1))))</_LDFLAGS>
<LinkerFlavor Condition="$(_LDFLAGS.Contains('lld'))">lld</LinkerFlavor>
</PropertyGroup>
</Target>

<Import Project="{NativeSanitizersTargets}" />
<Import Project="$(RepositoryEngineeringDir)toolAot.targets" />

<ItemGroup>
<Content Include="@(SanitizerRuntimeToCopy->'{SanitizerRuntimeFolder}/%(Identity)')" CopyToOutputDirectory="PreserveNewest" />
Expand Down
2 changes: 1 addition & 1 deletion eng/testing/linker/trimmingTests.targets
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@

<MSBuild Projects="@(TestConsoleApps)"
Targets="Publish"
Properties="Configuration=$(Configuration);BuildProjectReferences=false;TargetOS=$(TargetOS);TargetArchitecture=$(TargetArchitecture);_IsPublishing=true" />
Properties="Configuration=$(Configuration);BuildProjectReferences=false;HostOS=$(HostOS);TargetOS=$(TargetOS);TargetArchitecture=$(TargetArchitecture);_IsPublishing=true" />
</Target>

<Target Name="ExecuteApplications"
Expand Down
33 changes: 2 additions & 31 deletions eng/toolAot.targets
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,6 @@
<PackageReference Include="runtime.$(ToolsRID).Microsoft.DotNet.ILCompiler" Version="$(MicrosoftDotNetILCompilerVersion)" />
</ItemGroup>

<!-- Needed for the amd64 -> amd64 musl cross-build to pass the target flag. -->
<Target Name="_FixIlcTargetTriple"
AfterTargets="SetupOSSpecificProps"
Condition="'$(CrossBuild)' == 'true' and '$(HostOS)' != 'windows'">
<!-- Compute CrossCompileRid, and copy the downstream logic as-is. -->
<PropertyGroup>
<CrossCompileRid>$(RuntimeIdentifier)</CrossCompileRid>

<CrossCompileArch />
<CrossCompileArch Condition="$(CrossCompileRid.EndsWith('-x64'))">x86_64</CrossCompileArch>
<CrossCompileArch Condition="$(CrossCompileRid.EndsWith('-arm64')) and '$(_IsApplePlatform)' != 'true'">aarch64</CrossCompileArch>
<CrossCompileArch Condition="$(CrossCompileRid.EndsWith('-arm64')) and '$(_IsApplePlatform)' == 'true'">arm64</CrossCompileArch>

<TargetTriple />
<TargetTriple Condition="'$(CrossCompileArch)' != ''">$(CrossCompileArch)-linux-gnu</TargetTriple>
<TargetTriple Condition="'$(CrossCompileArch)' != '' and ($(CrossCompileRid.StartsWith('linux-musl')) or $(CrossCompileRid.StartsWith('alpine')))">$(CrossCompileArch)-alpine-linux-musl</TargetTriple>
<TargetTriple Condition="'$(CrossCompileArch)' != '' and ($(CrossCompileRid.StartsWith('freebsd')))">$(CrossCompileArch)-unknown-freebsd12</TargetTriple>
</PropertyGroup>

<ItemGroup>
<LinkerArg Include="--target=$(TargetTriple)" Condition="'$(TargetOS)' != 'osx' and '$(TargetTriple)' != ''" />
</ItemGroup>
</Target>

<ItemGroup Condition="'$(NativeAotSupported)' == 'true'">
<CustomLinkerArg Condition="'$(CrossBuild)' == 'true' and '$(_hostArchitecture)' == '$(_targetArchitecture)' and '$(_hostOS)' != 'windows'" Include="--gcc-toolchain=$(ROOTFS_DIR)/usr" />
</ItemGroup>
Expand All @@ -61,6 +37,7 @@
BeforeTargets="SetupOSSpecificProps">
<PropertyGroup>
<CppCompilerAndLinker Condition="'$(CppCompilerAndLinker)' == ''">clang</CppCompilerAndLinker>
<SysRoot Condition="'$(CrossBuild)' == 'true'">$(ROOTFS_DIR)</SysRoot>
</PropertyGroup>

<Exec Command="sh -c 'build_arch=&quot;$(TargetArchitecture)&quot; compiler=&quot;$(CppCompilerAndLinker)&quot; . &quot;$(RepositoryEngineeringDir)/common/native/init-compiler.sh&quot; &amp;&amp; echo &quot;$CC;$LDFLAGS&quot;' 2>/dev/null"
Expand All @@ -77,10 +54,4 @@
</PropertyGroup>
</Target>

<!-- Cross-build settings -->
<PropertyGroup>
<LinkerFlavor Condition="'$(CrossBuild)' == 'true' and '$(TargetsLinux)' == 'true'">lld</LinkerFlavor>
<SysRoot Condition="'$(CrossBuild)' == 'true' and '$(HostOS)' != 'windows'">$(ROOTFS_DIR)</SysRoot>
</PropertyGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ The .NET Foundation licenses this file to you under the MIT license.
<IlcRPath Condition="'$(IlcRPath)' == '' and '$(_IsApplePlatform)' != 'true'">$ORIGIN</IlcRPath>
<IlcRPath Condition="'$(IlcRPath)' == '' and '$(_IsApplePlatform)' == 'true'">@executable_path</IlcRPath>

<SharedLibraryInstallName Condition="'$(SharedLibraryInstallName)' == '' and '$(_IsApplePlatform)' == 'true' and '$(NativeLib)' == 'Shared'">@rpath/$(TargetName)$(NativeBinaryExt)</SharedLibraryInstallName>

<EventPipeName>libeventpipe-disabled</EventPipeName>
<EventPipeName Condition="'$(EventSourceSupport)' == 'true'">libeventpipe-enabled</EventPipeName>

Expand Down Expand Up @@ -92,7 +94,6 @@ The .NET Foundation licenses this file to you under the MIT license.
<_AppleTripleAbi Condition="$(_targetOS.EndsWith('simulator'))">simulator</_AppleTripleAbi>

<TargetTriple>$(CrossCompileArch)-apple-$(_AppleTripleOS)$(AppleMinOSVersion)-$(_AppleTripleAbi)</TargetTriple>
<SharedLibraryInstallName Condition="'$(SharedLibraryInstallName)' == '' and '$(NativeLib)' == 'Shared'">@rpath/$(TargetName)$(NativeBinaryExt)</SharedLibraryInstallName>
</PropertyGroup>

<PropertyGroup Condition="'$(_targetOS)' == 'osx'">
Expand Down Expand Up @@ -214,7 +215,7 @@ The .NET Foundation licenses this file to you under the MIT license.
<LinkerArg Include="-Wl,--strip-debug" Condition="$(NativeDebugSymbols) != 'true' and '$(_IsApplePlatform)' != 'true'" />
<LinkerArg Include="-Wl,-rpath,'$(IlcRPath)'" Condition="'$(StaticExecutable)' != 'true' and !$([MSBuild]::IsOSPlatform('Windows'))" />
<LinkerArg Include="-Wl,-rpath,&quot;$(IlcRPath)&quot;" Condition="'$(StaticExecutable)' != 'true' and $([MSBuild]::IsOSPlatform('Windows'))" />
<LinkerArg Include="-Wl,-install_name,&quot;$(SharedLibraryInstallName)&quot;" Condition="'$(_IsiOSLikePlatform)' == 'true' and '$(NativeLib)' == 'Shared'" />
<LinkerArg Include="-Wl,-install_name,&quot;$(SharedLibraryInstallName)&quot;" Condition="'$(_IsApplePlatform)' == 'true' and '$(NativeLib)' == 'Shared'" />
<LinkerArg Include="-Wl,--build-id=sha1" Condition="'$(_IsApplePlatform)' != 'true'" />
<LinkerArg Include="-Wl,--as-needed" Condition="'$(_IsApplePlatform)' != 'true'" />
<LinkerArg Include="-Wl,-e,0x0" Condition="'$(NativeLib)' == 'Shared' and '$(_IsApplePlatform)' != 'true'" />
Expand Down
4 changes: 0 additions & 4 deletions src/native/managed/compile-native.proj
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
<ItemGroup>
<!-- additional native compilation-specific properties to pass down to the ProjectReference -->
<SubprojectProps Include="NativeLib" Value="$(NativeLibKind)"/>

<SubprojectProps Condition="'$(SysRoot)' != ''" Include="SysRoot" Value="$(SysRoot)" />
<SubprojectProps Condition="'$(LinkerFlavor)' != ''" Include="LinkerFlavor" Value="$(LinkerFlavor)" />
<SubprojectProps Condition="'$(CustomLinkerArgToolchainArg)' != ''" Include="CustomLinkerArgToolchainArg" Value="$(CustomLinkerArgToolchainArg)" />
</ItemGroup>

<Import Project=".\subproject.props" />
Expand Down
19 changes: 0 additions & 19 deletions src/native/managed/native-library.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,9 @@
<ControlFlowGuard>Guard</ControlFlowGuard>
</PropertyGroup>

<!-- set the shared library name. this helps the native linker correctly reference this shared
library in dependents -->
<PropertyGroup>
<!-- in net9.0 we can do this, but only on mobile apple platforms, not OSX -->
<SharedLibraryInstallName>@rpath/$(MSBuildProjectName).dylib</SharedLibraryInstallName>
</PropertyGroup>
<ItemGroup Condition="'$(TargetsOSX)' == 'true'">
<LinkerArg Include="-Wl,-install_name,@rpath/$(MSBuildProjectName).dylib" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsUnix)' == 'true' and ! ('$(TargetsAppleMobile)' == 'true' or '$(TargetsOSX)' == 'true')">
<!-- If there is no soname, ld on Linux and some other Unixes will embed the full (build-time!)
path to this shared library into any binary that links against it. We don't want that. -->
<LinkerArg Include="-Wl,-soname=$(MSBuildProjectName).so" />
</ItemGroup>

<PropertyGroup>
<!-- if IsRuntimeComponent is true, we will put the native library into the specified locations under `artifacts/bin/$(RuntimeFlavor)/os.arch.config/` -->
<IsRuntimeComponent Condition="'$(IsRuntimeComponent)' == ''">true</IsRuntimeComponent>
</PropertyGroup>

<ItemGroup>
<!-- passed by compile-native.proj to set - -gcc-toolchain=$(ROOTFS_DIR)/usr -->
<CustomLinkerArg Condition="'$(CustomLinkerArgToolchainArg)' != ''" Include="$(CustomLinkerArgToolchainArg)" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/tests/Common/XUnitLogChecker/XUnitLogChecker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>$(NetCoreAppToolCurrent)</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PublishAot Condition="'$(NativeAotSupported)' == 'true'">true</PublishAot>
<PublishAot Condition="'$(UseNativeAotForComponents)' == 'true'">true</PublishAot>
<SuppressTrimAnalysisWarnings>false</SuppressTrimAnalysisWarnings>
</PropertyGroup>

Expand Down

0 comments on commit c627f38

Please sign in to comment.