Skip to content

Commit

Permalink
[msbuild/dotnet] Handle dylibs that don't start with 'lib' better. Fixes
Browse files Browse the repository at this point in the history
 xamarin#15044.

For a given dylib named '/path/to/libMyLibrary.dylib', we pass this to the native linker:

    -L/path/to -lMyLibrary

however, that doesn't work unless the dylib's name starts with 'lib'.

So detect this, and if the dylib doesn't start with 'lib' (say it's just
'MyLibrary.dylib'), then just pass the path to the dylib as-is to the native
linker:

	/path/to/MyLibrary.dylib

Fixes xamarin#15044.
  • Loading branch information
rolfbjarne committed Jun 23, 2023
1 parent aec6925 commit df061dd
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 26 deletions.
22 changes: 12 additions & 10 deletions msbuild/Xamarin.MacDev.Tasks/LinkerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@ public void BuildNativeReferenceFlags (TaskLoggingHelper Log, ITaskItem [] Nativ
} else if (kind == NativeReferenceKind.Dynamic) {
var path = item.ItemSpec;
var directory = Path.GetDirectoryName (path);
if (!string.IsNullOrEmpty (directory) && !libraryPaths.Contains (directory)) {
Arguments.AddQuoted ("-L" + directory);
libraryPaths.Add (directory);
}
// remove extension + "lib" prefix
var lib = Path.GetFileName (path);
if (lib.EndsWith (".dylib", StringComparison.OrdinalIgnoreCase))
lib = Path.GetFileNameWithoutExtension (lib);
if (lib.StartsWith ("lib", StringComparison.OrdinalIgnoreCase))
lib = lib.Substring (3);
Arguments.AddQuoted ("-l" + lib);
if (lib.StartsWith ("lib", StringComparison.Ordinal)) {
if (!string.IsNullOrEmpty (directory) && !libraryPaths.Contains (directory)) {
Arguments.AddQuoted ("-L" + directory);
libraryPaths.Add (directory);
}
// remove extension + "lib" prefix
if (lib.EndsWith (".dylib", StringComparison.OrdinalIgnoreCase))
lib = Path.GetFileNameWithoutExtension (lib);
Arguments.AddQuoted ("-l" + lib.Substring (3));
} else {
Arguments.AddQuoted (path);
}
} else {
Log.LogWarning (MSBStrings.W0052, item.ItemSpec);
continue;
Expand Down
10 changes: 6 additions & 4 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/LinkNativeCodeTaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,13 @@ bool ExecuteUnsafe ()
arguments.Add (lib);
break;
case ".dylib":
arguments.Add ("-L" + Path.GetDirectoryName (lib));
var libName = Path.GetFileNameWithoutExtension (lib);
if (libName.StartsWith ("lib", StringComparison.Ordinal))
libName = libName.Substring (3);
arguments.Add ("-l" + libName);
if (libName.StartsWith ("lib", StringComparison.Ordinal)) {
arguments.Add ("-L" + Path.GetDirectoryName (lib));
arguments.Add ("-l" + libName.Substring (3));
} else {
arguments.Add (libSpec.ItemSpec);
}
hasDylibs = true;
break;
case ".framework":
Expand Down
3 changes: 3 additions & 0 deletions tests/dotnet/BundleStructure/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ static int Main (string [] args)

return args.Length;
}

[DllImport ("__Internal")]
static extern IntPtr getNoLibPrefix ();
}
}
2 changes: 2 additions & 0 deletions tests/dotnet/BundleStructure/shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@
<!-- https://github.com/xamarin/xamarin-macios/issues/15727 -->
<None Include="$(RootTestsDirectory)/test-libraries/frameworks/.libs/$(RuntimeIdentifier)/Framework.With.Dots.framework" CopyToPublishDirectory="PreserveNewest" PublishFolderType="AppleFramework" />

<!-- https://github.com/xamarin/xamarin-macios/issues/15044 -->
<None Include="$(RootTestsDirectory)/test-libraries/libraries/.libs/$(RuntimeIdentifier)/NoLibPrefix.dylib" CopyToPublishDirectory="PreserveNewest" PublishFolderType="DynamicLibrary" />
<!-- Content items -->

<!-- Content without any metadata: bundled -->
Expand Down
2 changes: 2 additions & 0 deletions tests/dotnet/UnitTests/BundleStructureTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ internal static void CheckAppBundleContents (ApplePlatform platform, IEnumerable

AddExpectedFrameworkFiles (platform, expectedFiles, "Framework.With.Dots", isSigned); // https://github.com/xamarin/xamarin-macios/issues/15727

expectedFiles.Add (Path.Combine (assemblyDirectory, "NoLibPrefix.dylib")); // https://github.com/xamarin/xamarin-macios/issues/15044

expectedFiles.Add (Path.Combine (resourcesDirectory, "ContentA.txt"));
expectedFiles.Add (Path.Combine (resourcesDirectory, "ContentB.txt"));
expectedFiles.Add (Path.Combine (resourcesDirectory, "ContentC.txt"));
Expand Down
26 changes: 14 additions & 12 deletions tests/test-libraries/libraries/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ TOP=../../..
include $(TOP)/Make.config

NAMES+=NoneE NoneF UnknownD SomewhatUnknownD SkipInstallNameTool
NOPREFIX+=NoLibPrefix

NO_INSTALL_NAME_TOOL_SkipInstallNameTool=1

Expand Down Expand Up @@ -32,33 +33,33 @@ maccatalyst-arm64_ARCHITECTURES=arm64
define DotNetTemplate

$(1)_$(3)_TARGETS += \
.libs/$(3)/lib$(1).a \
.libs/$(3)/lib$(1).o \
.libs/$(3)/lib$(1).dylib \
.libs/$(3)/lib$(1).so \
.libs/$(3)/$(4)$(1).a \
.libs/$(3)/$(4)$(1).o \
.libs/$(3)/$(4)$(1).dylib \
.libs/$(3)/$(4)$(1).so \

$(3)_TARGETS += \
$$($(1)_$(3)_TARGETS) \

all-local:: $$($(3)_TARGETS)

.libs/$(3)/lib$(1).dylib: shared.c | .libs/$(3)
.libs/$(3)/$(4)$(1).dylib: shared.c | .libs/$(3)
$$(call Q_2,CC, [$(3)]) $$(XCODE_CC) -o $$@ $$(foreach arch,$$($(3)_ARCHITECTURES),-arch $$(arch)) $$($(2)_COMPILER_FLAGS) $$($(3)_COMPILER_FLAGS) -DNAME=$(1) -shared -framework Foundation -lz
ifneq ($(NO_INSTALL_NAME_TOOL_$(1)),1)
$$(Q) $(XCODE_DEVELOPER_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/install_name_tool -id @rpath/lib$(1).dylib $$@
$$(Q) $(XCODE_DEVELOPER_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/install_name_tool -id @rpath/$(4)$(1).dylib $$@
endif

.libs/$(3)/lib$(1).o: shared.c | .libs/$(3)
.libs/$(3)/$(4)$(1).o: shared.c | .libs/$(3)
$$(call Q_2,CC, [$(3)]) $$(XCODE_CC) -o $$@ $$(foreach arch,$$($(3)_ARCHITECTURES),-arch $$(arch)) $$($(2)_COMPILER_FLAGS) $$($(3)_COMPILER_FLAGS) -DNAME=$(1) -c
ifneq ($(NO_INSTALL_NAME_TOOL_$(1)),1)
$$(Q) $(XCODE_DEVELOPER_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/install_name_tool -id @rpath/lib$(1).dylib $$@
$$(Q) $(XCODE_DEVELOPER_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/install_name_tool -id @rpath/$(4)$(1).dylib $$@
endif

.libs/$(3)/lib$(1).a: .libs/$(3)/lib$(1).o
.libs/$(3)/$(4)$(1).a: .libs/$(3)/$(4)$(1).o
$$(call Q_2,AR [$(1)]) $(XCODE_DEVELOPER_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar cru $$@ $$<

.libs/$(3)/lib$(1).so: .libs/$(3)/lib$(1).dylib
$$(Q) $(CP) .libs/$(3)/lib$(1).dylib .libs/$(3)/lib$(1).so
.libs/$(3)/$(4)$(1).so: .libs/$(3)/$(4)$(1).dylib
$$(Q) $(CP) .libs/$(3)/$(4)$(1).dylib .libs/$(3)/$(4)$(1).so

$$($(3)_$(1)_DIRECTORIES):
$$(Q) mkdir -p $$@
Expand All @@ -68,7 +69,8 @@ endef
# 1: name
# 2: sdk platform (iphoneos, iphonesimulator, tvos, tvsimulator, maccatalyst, mac)
# 3: runtime identifier
$(foreach name,$(NAMES),$(foreach platform,$(DOTNET_PLATFORMS),$(foreach rid,$(DOTNET_$(platform)_RUNTIME_IDENTIFIERS),$(eval $(call DotNetTemplate,$(name),$(DOTNET_$(rid)_SDK_PLATFORM),$(rid))))))
$(foreach name,$(NAMES),$(foreach platform,$(DOTNET_PLATFORMS),$(foreach rid,$(DOTNET_$(platform)_RUNTIME_IDENTIFIERS),$(eval $(call DotNetTemplate,$(name),$(DOTNET_$(rid)_SDK_PLATFORM),$(rid),lib)))))
$(foreach name,$(NOPREFIX),$(foreach platform,$(DOTNET_PLATFORMS),$(foreach rid,$(DOTNET_$(platform)_RUNTIME_IDENTIFIERS),$(eval $(call DotNetTemplate,$(name),$(DOTNET_$(rid)_SDK_PLATFORM),$(rid))))))

$(foreach platform,$(DOTNET_PLATFORMS),$(foreach rid,$(DOTNET_$(platform)_RUNTIME_IDENTIFIERS),.libs/$(rid))):
$(Q) mkdir -p $@

0 comments on commit df061dd

Please sign in to comment.