Skip to content

Commit 1dd04b4

Browse files
authored
Add platform-specific linker flags (#74)
1 parent 932f3bd commit 1dd04b4

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/linking.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ function link_products(recipe::LinkRecipe)
109109
end
110110
# Link in the whole archive and user-provided objects, then undo WHOLE_ARCHIVE
111111
cmd2 = `$cmd2 -Wl,$(Base.Linking.WHOLE_ARCHIVE) $(image_recipe.img_path) $(image_recipe.extra_objects...) -Wl,$(Base.Linking.NO_WHOLE_ARCHIVE) $(julia_libs)`
112+
# Platform-specific linker flags
113+
lib_name = basename(recipe.outname)
114+
if Sys.iswindows()
115+
lib_basename, _ = splitext(lib_name)
116+
import_lib_path = joinpath(dirname(recipe.outname), lib_basename * ".dll.a")
117+
cmd2 = `$cmd2 -Wl,--out-implib=$(import_lib_path)`
118+
elseif Sys.isapple()
119+
cmd2 = `$cmd2 -Wl,-install_name,@rpath/$(lib_name)`
120+
elseif Sys.islinux()
121+
cmd2 = `$cmd2 -Wl,-soname,$(lib_name)`
122+
end
112123
image_recipe.verbose && println("Running: $cmd2")
113124
run(cmd2)
114125
catch e

test/programatic.jl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,59 @@
114114
@test occursin("42", out)
115115
end
116116
end
117+
118+
# https://github.com/JuliaLang/JuliaC.jl/pull/74
119+
@testset "Library has SONAME (Linux)" begin
120+
if Sys.islinux()
121+
outdir = mktempdir()
122+
libname = "libhassonametest"
123+
libout = joinpath(outdir, libname)
124+
link = JuliaC.LinkRecipe(image_recipe=img_lib, outname=libout)
125+
JuliaC.link_products(link)
126+
bun = JuliaC.BundleRecipe(link_recipe=link, output_dir=outdir)
127+
JuliaC.bundle_products(bun)
128+
129+
soname = libname * "." * Base.BinaryPlatforms.platform_dlext()
130+
libpath = joinpath(outdir, "lib", soname)
131+
actual_soname = readchomp(`$(Patchelf_jll.patchelf()) --print-soname $(libpath)`)
132+
@test actual_soname == soname
133+
end
134+
end
135+
136+
# https://github.com/JuliaLang/JuliaC.jl/pull/74
137+
@testset "Library has install_name (MacOS)" begin
138+
if Sys.isapple()
139+
outdir = mktempdir()
140+
libname = "libhasinstallnametest"
141+
libout = joinpath(outdir, libname)
142+
link = JuliaC.LinkRecipe(image_recipe=img_lib, outname=libout)
143+
JuliaC.link_products(link)
144+
bun = JuliaC.BundleRecipe(link_recipe=link, output_dir=outdir)
145+
JuliaC.bundle_products(bun)
146+
147+
dylibname = libname * "." * Base.BinaryPlatforms.platform_dlext()
148+
libpath = joinpath(outdir, "lib", dylibname)
149+
# otool -D prints filename on first line, install_name on second
150+
install_name = split(readchomp(`otool -D $(libpath)`), '\n')[end]
151+
@test install_name == "@rpath/$(dylibname)"
152+
end
153+
end
154+
155+
# https://github.com/JuliaLang/JuliaC.jl/pull/74
156+
@testset "Library has import library (Windows)" begin
157+
if Sys.iswindows()
158+
outdir = mktempdir()
159+
libname = "libhasimplibtest"
160+
libout = joinpath(outdir, libname)
161+
link = JuliaC.LinkRecipe(image_recipe=img_lib, outname=libout)
162+
JuliaC.link_products(link)
163+
bun = JuliaC.BundleRecipe(link_recipe=link, output_dir=outdir)
164+
JuliaC.bundle_products(bun)
165+
166+
implibpath = joinpath(outdir, libname * ".dll.a")
167+
@test isfile(implibpath)
168+
end
169+
end
117170
end
118171

119172
@testset "Programmatic binary (trim)" begin

0 commit comments

Comments
 (0)