Skip to content

Commit

Permalink
Merge pull request #4003 from xmake-io/soname
Browse files Browse the repository at this point in the history
Add soname support
  • Loading branch information
waruqi committed Jul 25, 2023
2 parents 6bece03 + 3160045 commit 841c94b
Show file tree
Hide file tree
Showing 36 changed files with 192 additions and 90 deletions.
3 changes: 0 additions & 3 deletions tests/projects/c++/console/test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
-- main entry
function main(t)

-- build project
t:build()
end
3 changes: 0 additions & 3 deletions tests/projects/c++/shared_library/test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
-- main entry
function main(t)

-- build project
t:build()
end
3 changes: 0 additions & 3 deletions tests/projects/c++/shared_library_export_all/test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
-- main entry
function main(t)

-- build project
t:build()
end
8 changes: 8 additions & 0 deletions tests/projects/c++/shared_library_with_soname/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Xmake cache
.xmake/
build/

# MacOS Cache
.DS_Store


5 changes: 5 additions & 0 deletions tests/projects/c++/shared_library_with_soname/src/foo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "foo.h"

int add(int a, int b) {
return a + b;
}
17 changes: 17 additions & 0 deletions tests/projects/c++/shared_library_with_soname/src/foo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifdef __cplusplus
extern "C" {
#endif

#if defined(_WIN32)
# define __export __declspec(dllexport)
#elif defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3))
# define __export __attribute__((visibility("default")))
#else
# define __export
#endif

__export int add(int a, int b);

#ifdef __cplusplus
}
#endif
9 changes: 9 additions & 0 deletions tests/projects/c++/shared_library_with_soname/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "foo.h"
#include <iostream>

using namespace std;

int main(int argc, char** argv) {
cout << "add(1, 2) = " << add(1, 2) << endl;
return 0;
}
3 changes: 3 additions & 0 deletions tests/projects/c++/shared_library_with_soname/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function main(t)
t:build()
end
16 changes: 16 additions & 0 deletions tests/projects/c++/shared_library_with_soname/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
add_rules("mode.debug", "mode.release")

set_version("1.0.1", {soname = true})

target("foo")
set_kind("shared")
add_files("src/foo.cpp")
after_build(function (target)
print(target:soname())
end)

target("tests")
set_kind("binary")
add_deps("foo")
add_files("src/main.cpp")

3 changes: 0 additions & 3 deletions tests/projects/c++/static_library/test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
-- main entry
function main(t)

-- build project
t:build()
end
3 changes: 0 additions & 3 deletions tests/projects/c++/test(brackets)/test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
-- main entry
function main(t)

-- build project
t:build()
end
3 changes: 0 additions & 3 deletions tests/projects/c++/unity_build/test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
-- main entry
function main(t)

-- build project
t:build()
end
3 changes: 0 additions & 3 deletions tests/projects/c/console/test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
-- main entry
function main(t)

-- build project
t:build()
end
3 changes: 0 additions & 3 deletions tests/projects/c/headeronly/test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
-- main entry
function main(t)

-- build project
t:build()
end
3 changes: 0 additions & 3 deletions tests/projects/c/static library with spaces/test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
-- main entry
function main(t)

-- build project
t:build()
end
3 changes: 0 additions & 3 deletions tests/projects/c/static_library/test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
-- main entry
function main(t)

-- build project
t:build()
end
3 changes: 0 additions & 3 deletions tests/projects/c/unity_build/test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
-- main entry
function main(t)

-- build project
t:build()
end
3 changes: 0 additions & 3 deletions tests/projects/dlang/console/test.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-- main entry
function main(t)

-- build project
if is_host("macosx") and os.arch() ~= "arm64" then
t:build()
else
Expand Down
3 changes: 0 additions & 3 deletions tests/projects/dlang/dub_package/test.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-- main entry
function main(t)

-- build project
if is_host("macosx") and os.arch() ~= "arm64" then
t:build()
else
Expand Down
3 changes: 0 additions & 3 deletions tests/projects/dlang/shared_library/test.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-- main entry
function main(t)

-- build project
if is_host("macosx") and os.arch() ~= "arm64" then
t:build()
else
Expand Down
3 changes: 0 additions & 3 deletions tests/projects/dlang/static_library/test.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-- main entry
function main(t)

-- build project
if is_host("macosx") and os.arch() ~= "arm64" then
t:build()
else
Expand Down
3 changes: 0 additions & 3 deletions tests/projects/objc++/console/test.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-- main entry
function main(t)

-- build project
if os.host() == "macosx" then
t:build({iphoneos = true})
else
Expand Down
3 changes: 0 additions & 3 deletions tests/projects/objc/console/test.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-- main entry
function main(t)

-- build project
if os.host() == "macosx" then
t:build({iphoneos = true})
else
Expand Down
3 changes: 0 additions & 3 deletions tests/projects/other/build_deps/test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
-- main entry
function main(t)

-- build project
t:build()
end
3 changes: 0 additions & 3 deletions tests/projects/other/merge_archive/test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
-- main entry
function main(t)

-- build project
t:build()
end
3 changes: 0 additions & 3 deletions tests/projects/other/merge_archive2/test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
-- main entry
function main(t)

-- build project
t:build()
end
3 changes: 0 additions & 3 deletions tests/projects/other/merge_object/test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
-- main entry
function main(t)

-- build project
t:build()
end
3 changes: 0 additions & 3 deletions tests/projects/rust/console/test.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-- main entry
function main(t)

-- build project
if is_host("macosx") and os.arch() ~= "arm64" then
t:build()
else
Expand Down
3 changes: 0 additions & 3 deletions tests/projects/rust/rust_call_cxx_library/test.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-- main entry
function main(t)

-- build project
if is_host("macosx") and os.arch() ~= "arm64" then
t:build()
else
Expand Down
3 changes: 0 additions & 3 deletions tests/projects/rust/static_library/test.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-- main entry
function main(t)

-- build project
if is_host("macosx") and os.arch() ~= "arm64" then
t:build()
else
Expand Down
3 changes: 0 additions & 3 deletions tests/projects/swift/console/test.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-- main entry
function main(t)

-- build project
if os.host() == "macosx" then
t:build({iphoneos = true})
else
Expand Down
54 changes: 41 additions & 13 deletions xmake/core/project/target.lua
Original file line number Diff line number Diff line change
Expand Up @@ -635,26 +635,54 @@ end

-- get the target version
function _instance:version()

-- get version and build version
local version = self:get("version")
local version_build = nil
local version_build
if version then
local version_extra = self:get("__extra_version")
if version_extra then
version_build = self._VERSION_BUILD
if not version_build then
version_build = table.wrap(version_extra[version]).build
if type(version_build) == "string" then
version_build = os.date(version_build, os.time())
self._VERSION_BUILD = version_build
end
end
version_build = self:extraconf("version", version, "build")
if type(version_build) == "string" then
version_build = os.date(version_build, os.time())
end
end
return version, version_build
end

-- get the target soname
-- @see https://github.com/tboox/tbox/issues/214
--
-- set_version("1.0.1", {soname = "1.0"}) -> libfoo.so.1.0, libfoo.1.0.dylib
-- set_version("1.0.1", {soname = "1"}) -> libfoo.so.1, libfoo.1.dylib
-- set_version("1.0.1", {soname = true}) -> libfoo.so.1, libfoo.1.dylib
-- set_version("1.0.1", {soname = ""}) -> libfoo.so, libfoo.dylib
function _instance:soname()
if not self:is_shared() then
return
end
if not self:is_plat("macosx", "linux", "bsd", "cross") then
return
end
local version = self:get("version")
local version_soname
if version then
version_soname = self:extraconf("version", version, "soname")
if version_soname == true then
version_soname = version:split(".", {plain = true})[1]
end
end
if not version_soname then
return
end
local soname = self:filename()
if type(version_soname) == "string" and #version_soname > 0 then
local extension = path.extension(soname)
if extension == ".dylib" then
soname = path.basename(soname) .. "." .. version_soname .. extension
else
soname = soname .. "." .. version_soname
end
end
return soname
end

-- get the target license
function _instance:license()
return self:get("license")
Expand Down
19 changes: 18 additions & 1 deletion xmake/modules/target/action/install/unix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,24 @@ function install_shared(target, opt)
-- install libraries
local librarydir = path.join(target:installdir(), opt and opt.libdir or "lib")
os.mkdir(librarydir)
os.vcp(target:targetfile(), librarydir)
local targetfile = target:targetfile()
if os.islink(targetfile) then
local targetfile_with_soname = os.readlink(targetfile)
if not path.is_absolute(targetfile_with_soname) then
targetfile_with_soname = path.join(target:targetdir(), targetfile_with_soname)
end
if os.islink(targetfile_with_soname) then
local targetfile_with_version = os.readlink(targetfile_with_soname)
if not path.is_absolute(targetfile_with_version) then
targetfile_with_version = path.join(target:targetdir(), targetfile_with_version)
end
os.vcp(targetfile_with_version, librarydir, {symlink = true})
end
os.vcp(targetfile_with_soname, librarydir, {symlink = true})
os.vcp(targetfile, librarydir, {symlink = true})
else
os.vcp(targetfile, librarydir)
end

-- install shared libraries for all packages
_install_shared_for_packages(target, librarydir)
Expand Down
17 changes: 16 additions & 1 deletion xmake/modules/target/action/uninstall/unix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,22 @@ function uninstall_shared(target, opt)

-- remove the target file
local librarydir = path.join(target:installdir(), opt and opt.libdir or "lib")
os.vrm(path.join(librarydir, path.filename(target:targetfile())))
local targetfile = path.join(librarydir, path.filename(target:targetfile()))
if os.islink(targetfile) then
local targetfile_with_soname = os.readlink(targetfile)
if not path.is_absolute(targetfile_with_soname) then
targetfile_with_soname = path.join(librarydir, targetfile_with_soname)
end
if os.islink(targetfile_with_soname) then
local targetfile_with_version = os.readlink(targetfile_with_soname)
if not path.is_absolute(targetfile_with_version) then
targetfile_with_version = path.join(librarydir, targetfile_with_version)
end
os.vrm(targetfile_with_version)
end
os.vrm(targetfile_with_soname)
end
os.vrm(targetfile)

-- remove headers from the include directory
_uninstall_headers(target, opt)
Expand Down
Loading

0 comments on commit 841c94b

Please sign in to comment.