diff options
| author | ruki <[email protected]> | 2023-07-25 12:57:22 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-07-25 12:57:22 +0800 |
| commit | 841c94b81a39dbb360ffb6fb140b2e8241aadb42 (patch) | |
| tree | 7ffd12ece3ea73a6d2a34bccab3d3cd6ec656c6a | |
| parent | 6bece03efd07829d4e59f53de36a219a2397c0e1 (diff) | |
| parent | 3160045851a53d4ae014f7827e5cd34ac88fe647 (diff) | |
Merge pull request #4003 from xmake-io/soname
Add soname support
36 files changed, 192 insertions, 90 deletions
diff --git a/tests/projects/c++/console/test.lua b/tests/projects/c++/console/test.lua index b76241be2..b57362078 100644 --- a/tests/projects/c++/console/test.lua +++ b/tests/projects/c++/console/test.lua @@ -1,6 +1,3 @@ --- main entry function main(t) - - -- build project t:build() end diff --git a/tests/projects/c++/shared_library/test.lua b/tests/projects/c++/shared_library/test.lua index b76241be2..b57362078 100644 --- a/tests/projects/c++/shared_library/test.lua +++ b/tests/projects/c++/shared_library/test.lua @@ -1,6 +1,3 @@ --- main entry function main(t) - - -- build project t:build() end diff --git a/tests/projects/c++/shared_library_export_all/test.lua b/tests/projects/c++/shared_library_export_all/test.lua index b76241be2..b57362078 100644 --- a/tests/projects/c++/shared_library_export_all/test.lua +++ b/tests/projects/c++/shared_library_export_all/test.lua @@ -1,6 +1,3 @@ --- main entry function main(t) - - -- build project t:build() end diff --git a/tests/projects/c++/shared_library_with_soname/.gitignore b/tests/projects/c++/shared_library_with_soname/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/projects/c++/shared_library_with_soname/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/projects/c++/shared_library_with_soname/src/foo.cpp b/tests/projects/c++/shared_library_with_soname/src/foo.cpp new file mode 100644 index 000000000..1a1fb3425 --- /dev/null +++ b/tests/projects/c++/shared_library_with_soname/src/foo.cpp @@ -0,0 +1,5 @@ +#include "foo.h" + +int add(int a, int b) { + return a + b; +} diff --git a/tests/projects/c++/shared_library_with_soname/src/foo.h b/tests/projects/c++/shared_library_with_soname/src/foo.h new file mode 100644 index 000000000..20df0baa4 --- /dev/null +++ b/tests/projects/c++/shared_library_with_soname/src/foo.h @@ -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 diff --git a/tests/projects/c++/shared_library_with_soname/src/main.cpp b/tests/projects/c++/shared_library_with_soname/src/main.cpp new file mode 100644 index 000000000..ed1924789 --- /dev/null +++ b/tests/projects/c++/shared_library_with_soname/src/main.cpp @@ -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; +} diff --git a/tests/projects/c++/shared_library_with_soname/test.lua b/tests/projects/c++/shared_library_with_soname/test.lua new file mode 100644 index 000000000..b57362078 --- /dev/null +++ b/tests/projects/c++/shared_library_with_soname/test.lua @@ -0,0 +1,3 @@ +function main(t) + t:build() +end diff --git a/tests/projects/c++/shared_library_with_soname/xmake.lua b/tests/projects/c++/shared_library_with_soname/xmake.lua new file mode 100644 index 000000000..4ebc1c501 --- /dev/null +++ b/tests/projects/c++/shared_library_with_soname/xmake.lua @@ -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") + diff --git a/tests/projects/c++/static_library/test.lua b/tests/projects/c++/static_library/test.lua index b76241be2..b57362078 100644 --- a/tests/projects/c++/static_library/test.lua +++ b/tests/projects/c++/static_library/test.lua @@ -1,6 +1,3 @@ --- main entry function main(t) - - -- build project t:build() end diff --git a/tests/projects/c++/test(brackets)/test.lua b/tests/projects/c++/test(brackets)/test.lua index b76241be2..b57362078 100644 --- a/tests/projects/c++/test(brackets)/test.lua +++ b/tests/projects/c++/test(brackets)/test.lua @@ -1,6 +1,3 @@ --- main entry function main(t) - - -- build project t:build() end diff --git a/tests/projects/c++/unity_build/test.lua b/tests/projects/c++/unity_build/test.lua index b76241be2..b57362078 100644 --- a/tests/projects/c++/unity_build/test.lua +++ b/tests/projects/c++/unity_build/test.lua @@ -1,6 +1,3 @@ --- main entry function main(t) - - -- build project t:build() end diff --git a/tests/projects/c/console/test.lua b/tests/projects/c/console/test.lua index b76241be2..b57362078 100644 --- a/tests/projects/c/console/test.lua +++ b/tests/projects/c/console/test.lua @@ -1,6 +1,3 @@ --- main entry function main(t) - - -- build project t:build() end diff --git a/tests/projects/c/headeronly/test.lua b/tests/projects/c/headeronly/test.lua index b76241be2..b57362078 100644 --- a/tests/projects/c/headeronly/test.lua +++ b/tests/projects/c/headeronly/test.lua @@ -1,6 +1,3 @@ --- main entry function main(t) - - -- build project t:build() end diff --git a/tests/projects/c/static library with spaces/test.lua b/tests/projects/c/static library with spaces/test.lua index b76241be2..b57362078 100644 --- a/tests/projects/c/static library with spaces/test.lua +++ b/tests/projects/c/static library with spaces/test.lua @@ -1,6 +1,3 @@ --- main entry function main(t) - - -- build project t:build() end diff --git a/tests/projects/c/static_library/test.lua b/tests/projects/c/static_library/test.lua index b76241be2..b57362078 100644 --- a/tests/projects/c/static_library/test.lua +++ b/tests/projects/c/static_library/test.lua @@ -1,6 +1,3 @@ --- main entry function main(t) - - -- build project t:build() end diff --git a/tests/projects/c/unity_build/test.lua b/tests/projects/c/unity_build/test.lua index b76241be2..b57362078 100644 --- a/tests/projects/c/unity_build/test.lua +++ b/tests/projects/c/unity_build/test.lua @@ -1,6 +1,3 @@ --- main entry function main(t) - - -- build project t:build() end diff --git a/tests/projects/dlang/console/test.lua b/tests/projects/dlang/console/test.lua index 92168a8c1..060aa6ca5 100644 --- a/tests/projects/dlang/console/test.lua +++ b/tests/projects/dlang/console/test.lua @@ -1,7 +1,4 @@ --- main entry function main(t) - - -- build project if is_host("macosx") and os.arch() ~= "arm64" then t:build() else diff --git a/tests/projects/dlang/dub_package/test.lua b/tests/projects/dlang/dub_package/test.lua index 92168a8c1..060aa6ca5 100644 --- a/tests/projects/dlang/dub_package/test.lua +++ b/tests/projects/dlang/dub_package/test.lua @@ -1,7 +1,4 @@ --- main entry function main(t) - - -- build project if is_host("macosx") and os.arch() ~= "arm64" then t:build() else diff --git a/tests/projects/dlang/shared_library/test.lua b/tests/projects/dlang/shared_library/test.lua index 92168a8c1..060aa6ca5 100644 --- a/tests/projects/dlang/shared_library/test.lua +++ b/tests/projects/dlang/shared_library/test.lua @@ -1,7 +1,4 @@ --- main entry function main(t) - - -- build project if is_host("macosx") and os.arch() ~= "arm64" then t:build() else diff --git a/tests/projects/dlang/static_library/test.lua b/tests/projects/dlang/static_library/test.lua index 92168a8c1..060aa6ca5 100644 --- a/tests/projects/dlang/static_library/test.lua +++ b/tests/projects/dlang/static_library/test.lua @@ -1,7 +1,4 @@ --- main entry function main(t) - - -- build project if is_host("macosx") and os.arch() ~= "arm64" then t:build() else diff --git a/tests/projects/objc++/console/test.lua b/tests/projects/objc++/console/test.lua index 60277b691..149a15ed0 100644 --- a/tests/projects/objc++/console/test.lua +++ b/tests/projects/objc++/console/test.lua @@ -1,7 +1,4 @@ --- main entry function main(t) - - -- build project if os.host() == "macosx" then t:build({iphoneos = true}) else diff --git a/tests/projects/objc/console/test.lua b/tests/projects/objc/console/test.lua index 60277b691..149a15ed0 100644 --- a/tests/projects/objc/console/test.lua +++ b/tests/projects/objc/console/test.lua @@ -1,7 +1,4 @@ --- main entry function main(t) - - -- build project if os.host() == "macosx" then t:build({iphoneos = true}) else diff --git a/tests/projects/other/build_deps/test.lua b/tests/projects/other/build_deps/test.lua index b76241be2..b57362078 100644 --- a/tests/projects/other/build_deps/test.lua +++ b/tests/projects/other/build_deps/test.lua @@ -1,6 +1,3 @@ --- main entry function main(t) - - -- build project t:build() end diff --git a/tests/projects/other/merge_archive/test.lua b/tests/projects/other/merge_archive/test.lua index b76241be2..b57362078 100644 --- a/tests/projects/other/merge_archive/test.lua +++ b/tests/projects/other/merge_archive/test.lua @@ -1,6 +1,3 @@ --- main entry function main(t) - - -- build project t:build() end diff --git a/tests/projects/other/merge_archive2/test.lua b/tests/projects/other/merge_archive2/test.lua index b76241be2..b57362078 100644 --- a/tests/projects/other/merge_archive2/test.lua +++ b/tests/projects/other/merge_archive2/test.lua @@ -1,6 +1,3 @@ --- main entry function main(t) - - -- build project t:build() end diff --git a/tests/projects/other/merge_object/test.lua b/tests/projects/other/merge_object/test.lua index b76241be2..b57362078 100644 --- a/tests/projects/other/merge_object/test.lua +++ b/tests/projects/other/merge_object/test.lua @@ -1,6 +1,3 @@ --- main entry function main(t) - - -- build project t:build() end diff --git a/tests/projects/rust/console/test.lua b/tests/projects/rust/console/test.lua index 92168a8c1..060aa6ca5 100644 --- a/tests/projects/rust/console/test.lua +++ b/tests/projects/rust/console/test.lua @@ -1,7 +1,4 @@ --- main entry function main(t) - - -- build project if is_host("macosx") and os.arch() ~= "arm64" then t:build() else diff --git a/tests/projects/rust/rust_call_cxx_library/test.lua b/tests/projects/rust/rust_call_cxx_library/test.lua index 92168a8c1..060aa6ca5 100644 --- a/tests/projects/rust/rust_call_cxx_library/test.lua +++ b/tests/projects/rust/rust_call_cxx_library/test.lua @@ -1,7 +1,4 @@ --- main entry function main(t) - - -- build project if is_host("macosx") and os.arch() ~= "arm64" then t:build() else diff --git a/tests/projects/rust/static_library/test.lua b/tests/projects/rust/static_library/test.lua index 92168a8c1..060aa6ca5 100644 --- a/tests/projects/rust/static_library/test.lua +++ b/tests/projects/rust/static_library/test.lua @@ -1,7 +1,4 @@ --- main entry function main(t) - - -- build project if is_host("macosx") and os.arch() ~= "arm64" then t:build() else diff --git a/tests/projects/swift/console/test.lua b/tests/projects/swift/console/test.lua index 60277b691..149a15ed0 100644 --- a/tests/projects/swift/console/test.lua +++ b/tests/projects/swift/console/test.lua @@ -1,7 +1,4 @@ --- main entry function main(t) - - -- build project if os.host() == "macosx" then t:build({iphoneos = true}) else diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index ae3212343..e0b2ebdd2 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -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") diff --git a/xmake/modules/target/action/install/unix.lua b/xmake/modules/target/action/install/unix.lua index ed49460dc..fa4834df2 100644 --- a/xmake/modules/target/action/install/unix.lua +++ b/xmake/modules/target/action/install/unix.lua @@ -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) diff --git a/xmake/modules/target/action/uninstall/unix.lua b/xmake/modules/target/action/uninstall/unix.lua index 1ad1d78ae..695fc1194 100644 --- a/xmake/modules/target/action/uninstall/unix.lua +++ b/xmake/modules/target/action/uninstall/unix.lua @@ -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) diff --git a/xmake/rules/linker/soname/xmake.lua b/xmake/rules/linker/soname/xmake.lua new file mode 100644 index 000000000..3bacb721d --- /dev/null +++ b/xmake/rules/linker/soname/xmake.lua @@ -0,0 +1,58 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xmake.lua +-- + +rule("linker.soname") + on_config(function (target) + local soname = target:soname() + if target:is_shared() and soname then + if target:has_tool("sh", "gcc", "gxx", "clang", "clangxx") then + if target:is_plat("macosx", "iphoneos", "watchos", "appletvos") then + target:add("shflags", "-Wl,-install_name," .. soname, {force = true}) + else + target:add("shflags", "-Wl,-soname," .. soname, {force = true}) + end + target:data_set("soname.enabled", true) + end + end + end) + after_link(function (target) + local soname = target:soname() + if target:is_shared() and soname and target:data("soname.enabled") then + local version = target:version() + local filename = target:filename() + local extension = path.extension(filename) + local targetfile_with_version = path.join(target:targetdir(), filename .. "." .. version) + if extension == ".dylib" then + targetfile_with_version = path.join(target:targetdir(), path.basename(filename) .. "." .. version .. extension) + end + local targetfile_with_soname = path.join(target:targetdir(), soname) + local targetfile = target:targetfile() + if soname ~= filename and soname ~= path.filename(targetfile_with_version) then + os.cp(target:targetfile(), targetfile_with_version) + os.rm(target:targetfile()) + os.rm(targetfile_with_soname) + local oldir = os.cd(target:targetdir()) + os.ln(path.filename(targetfile_with_version), soname) + os.ln(soname, path.filename(targetfile)) + os.cd(oldir) + end + end + end) + diff --git a/xmake/rules/linker/xmake.lua b/xmake/rules/linker/xmake.lua index 602eecdfe..c4632d6cf 100644 --- a/xmake/rules/linker/xmake.lua +++ b/xmake/rules/linker/xmake.lua @@ -21,4 +21,5 @@ rule("linker") add_deps("linker.link_scripts") add_deps("linker.version_scripts") + add_deps("linker.soname") |
