Skip to content

Commit f4e2c1f

Browse files
authored
[dotnet] Add support for the @(LinkerArgument) item group to pass arguments to the native linker. Fixes #21331. (#23468)
Quite often customers need to pass custom arguments to the native linker. Until now we haven't had an official way of doing this, besides the rather clunky: ```xml <AppBundleExtraOptions>$(AppBundleExtraOptions) --gcc_flags '...'</AppBundleExtraOptions> ``` So add support for the `LinkerArgument` item group, which will now serve this purpose. A code search on GitHub for '<LinkerArgument' shows exactly 1 result, which seems to be unrelated (it's for Java code), so hopefully no existing projects will already have 'LinkerArguments' set. Fixes #21331.
1 parent fddccd3 commit f4e2c1f

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

docs/building-apps/build-items.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,38 @@ Additional xml files to pass to the trimmer.
198198

199199
This is the same as setting [TrimmerRootDescriptor](/dotnet/core/deploying/trimming/trimming-options?#root-descriptors).
200200

201+
## LinkerArgument
202+
203+
Additional arguments to pass to the native linker (`ld`) when compiling the main executable for an app or app extension.
204+
205+
Example 1 (to link with the `AudioToolbox` framework):
206+
207+
```xml
208+
<ItemGroup>
209+
<LinkerArgument Include="-framework" />
210+
<LinkerArgument Include="AudioToolbox" />
211+
</ItemGroup>
212+
```
213+
214+
Example 2 (to link with a custom static library):
215+
216+
```xml
217+
<ItemGroup>
218+
<LinkerArgument Include="$(MSBuildProjectDirectory)/libCustom.a" />
219+
</ItemGroup>
220+
```
221+
222+
Each argument to the linker is a separate `LinkerArgument`, and arguments must not be quoted.
223+
224+
All the arguments will be passed to the native linker in the order they're
225+
added to the `LinkerArgument` item group, but the exact location within all
226+
the arguments passed to the native linker is not defined.
227+
228+
The native executable will be rebuilt automatically if the set of
229+
`LinkerArgument` changes between builds, but if a `LinkerArgument` points to a
230+
file (such as a static library), and that file changes, this change will not
231+
be detected and the native executable will not be rebuilt automatically.
232+
201233
## Metal
202234

203235
An item group that contains metal assets.

dotnet/targets/Xamarin.Shared.Sdk.targets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,6 +1701,8 @@
17011701

17021702
<_AllLinkerFlags Condition="'$(_UseClassicLinker)' == 'true'" Include="-Xlinker" />
17031703
<_AllLinkerFlags Condition="'$(_UseClassicLinker)' == 'true'" Include="-ld_classic" />
1704+
1705+
<_AllLinkerFlags Include="@(LinkerArgument)" />
17041706
</ItemGroup>
17051707

17061708
<!-- write a hash of all the relevant input so that we can force a re-link if necessary -->

0 commit comments

Comments
 (0)