summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-02-07 23:51:40 +0800
committerruki <[email protected]>2022-02-07 23:51:40 +0800
commit4ec0ff6570e24ed3e2434cc9f81c728fd002f20c (patch)
tree959d09f1a9635dd066c61a3ed2dab93495805bc8
parentc88d9afcb3e929d6c0a4f3644399ac3df572829f (diff)
update changelog
-rw-r--r--CHANGELOG.md2
-rw-r--r--tests/projects/c/linker_scripts/src/foo.c5
-rw-r--r--tests/projects/c/linker_scripts/src/foo.map7
-rw-r--r--tests/projects/c/linker_scripts/src/main.c8
-rw-r--r--tests/projects/c/linker_scripts/src/main.lds1
-rw-r--r--tests/projects/c/linker_scripts/test.lua6
-rw-r--r--tests/projects/c/linker_scripts/xmake.lua13
-rw-r--r--xmake/rules/asm/xmake.lua7
-rw-r--r--xmake/rules/c++/xmake.lua4
-rw-r--r--xmake/rules/dlang/xmake.lua4
-rw-r--r--xmake/rules/linker/link_scripts/xmake.lua49
-rw-r--r--xmake/rules/linker/version_scripts/xmake.lua50
-rw-r--r--xmake/rules/platform/windows/def/xmake.lua15
-rw-r--r--xmake/rules/platform/windows/manifest/xmake.lua15
14 files changed, 174 insertions, 12 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a453b971c..4794a166b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@
* Add on_download for package()
* [#2021](https://github.com/xmake-io/xmake/issues/2021): Support Swift for linux and windows
* [#2024](https://github.com/xmake-io/xmake/issues/2024): Add asn1c support
+* Support linker scripts and version scripts for add_files
### Bugs fixed
@@ -1212,6 +1213,7 @@
* 为 package() 添加 on_download 自定义下载
* [#2021](https://github.com/xmake-io/xmake/issues/2021): 支持 Linux/Windows 下构建 Swift 程序
* [#2024](https://github.com/xmake-io/xmake/issues/2024): 添加 asn1c 支持
+* 为 add_files 增加 linker scripts 和 version scripts 支持
### Bugs 修复
diff --git a/tests/projects/c/linker_scripts/src/foo.c b/tests/projects/c/linker_scripts/src/foo.c
new file mode 100644
index 000000000..bf480461b
--- /dev/null
+++ b/tests/projects/c/linker_scripts/src/foo.c
@@ -0,0 +1,5 @@
+#include <stdio.h>
+
+void foo() {
+ printf("hello world!\n");
+}
diff --git a/tests/projects/c/linker_scripts/src/foo.map b/tests/projects/c/linker_scripts/src/foo.map
new file mode 100644
index 000000000..e593cf4d2
--- /dev/null
+++ b/tests/projects/c/linker_scripts/src/foo.map
@@ -0,0 +1,7 @@
+{
+ global:
+ foo;
+
+ local:
+ *;
+};
diff --git a/tests/projects/c/linker_scripts/src/main.c b/tests/projects/c/linker_scripts/src/main.c
new file mode 100644
index 000000000..8635202c3
--- /dev/null
+++ b/tests/projects/c/linker_scripts/src/main.c
@@ -0,0 +1,8 @@
+#include <stdio.h>
+
+void foo();
+
+int main(int argc, char** argv) {
+ foo();
+ return 0;
+}
diff --git a/tests/projects/c/linker_scripts/src/main.lds b/tests/projects/c/linker_scripts/src/main.lds
new file mode 100644
index 000000000..0e84b7f9c
--- /dev/null
+++ b/tests/projects/c/linker_scripts/src/main.lds
@@ -0,0 +1 @@
+SEARCH_DIR("/lib")
diff --git a/tests/projects/c/linker_scripts/test.lua b/tests/projects/c/linker_scripts/test.lua
new file mode 100644
index 000000000..b76241be2
--- /dev/null
+++ b/tests/projects/c/linker_scripts/test.lua
@@ -0,0 +1,6 @@
+-- main entry
+function main(t)
+
+ -- build project
+ t:build()
+end
diff --git a/tests/projects/c/linker_scripts/xmake.lua b/tests/projects/c/linker_scripts/xmake.lua
new file mode 100644
index 000000000..2396d1b52
--- /dev/null
+++ b/tests/projects/c/linker_scripts/xmake.lua
@@ -0,0 +1,13 @@
+add_rules("mode.debug", "mode.release")
+
+target("test")
+ add_deps("foo")
+ set_kind("binary")
+ add_files("src/main.c")
+ add_files("src/main.lds")
+
+target("foo")
+ set_kind("shared")
+ add_files("src/foo.c")
+ add_files("src/foo.map")
+
diff --git a/xmake/rules/asm/xmake.lua b/xmake/rules/asm/xmake.lua
index 153bc4802..d916af720 100644
--- a/xmake/rules/asm/xmake.lua
+++ b/xmake/rules/asm/xmake.lua
@@ -38,3 +38,10 @@ rule("asm")
-- we attempt to extract symbols to the independent file and
-- strip self-target binary if `set_symbols("debug")` and `set_strip("all")` are enabled
add_deps("utils.symbols.extract")
+
+ -- add platform rules
+ add_deps("platform.windows")
+
+ -- add linker rules
+ add_deps("linker.link_scripts", "linker.version_scripts")
+
diff --git a/xmake/rules/c++/xmake.lua b/xmake/rules/c++/xmake.lua
index 14dc961d3..77223d0df 100644
--- a/xmake/rules/c++/xmake.lua
+++ b/xmake/rules/c++/xmake.lua
@@ -64,3 +64,7 @@ rule("c++")
-- add platform rules
add_deps("platform.windows")
+
+ -- add linker rules
+ add_deps("linker.link_scripts", "linker.version_scripts")
+
diff --git a/xmake/rules/dlang/xmake.lua b/xmake/rules/dlang/xmake.lua
index 0b4a5ff07..748026e85 100644
--- a/xmake/rules/dlang/xmake.lua
+++ b/xmake/rules/dlang/xmake.lua
@@ -38,3 +38,7 @@ rule("dlang")
-- we attempt to extract symbols to the independent file and
-- strip self-target binary if `set_symbols("debug")` and `set_strip("all")` are enabled
add_deps("utils.symbols.extract")
+
+ -- add linker rules
+ add_deps("linker.version_scripts")
+
diff --git a/xmake/rules/linker/link_scripts/xmake.lua b/xmake/rules/linker/link_scripts/xmake.lua
new file mode 100644
index 000000000..fdd714988
--- /dev/null
+++ b/xmake/rules/linker/link_scripts/xmake.lua
@@ -0,0 +1,49 @@
+--!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.link_scripts")
+ set_extensions(".ld", ".lds")
+ on_config(function (target)
+ if not target:is_binary() and not target:is_shared() then
+ return
+ end
+ local scriptfile
+ local sourcebatch = target:sourcebatches()["linker.link_scripts"]
+ if sourcebatch then
+ for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
+ scriptfile = sourcefile
+ break
+ end
+ end
+ if not scriptfile then
+ return
+ end
+ -- @note apple's linker does not support it
+ if target:is_plat("macosx", "iphoneos", "watchos", "appletvos") then
+ return
+ end
+ if target:has_tool("ld", "gcc", "gxx", "clang", "clangxx") or
+ target:has_tool("sh", "gcc", "gxx", "clang", "clangxx") then
+ target:add(target:is_shared() and "shflags" or "ldflags", "-T " .. scriptfile, {force = true})
+ elseif target:has_tool("ld", "ld") or target:has_tool("sh", "ld") then
+ target:add(target:is_shared() and "shflags" or "ldflags", "-T " .. scriptfile, {force = true})
+ end
+ end)
+
diff --git a/xmake/rules/linker/version_scripts/xmake.lua b/xmake/rules/linker/version_scripts/xmake.lua
new file mode 100644
index 000000000..3f5a901b1
--- /dev/null
+++ b/xmake/rules/linker/version_scripts/xmake.lua
@@ -0,0 +1,50 @@
+--!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.version_scripts")
+ set_extensions(".ver", ".map")
+ on_config(function (target)
+ if not target:is_binary() and not target:is_shared() then
+ return
+ end
+ local scriptfile
+ local sourcebatch = target:sourcebatches()["linker.version_scripts"]
+ if sourcebatch then
+ for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
+ scriptfile = sourcefile
+ break
+ end
+ end
+ if not scriptfile then
+ return
+ end
+ -- @note apple's linker does not support it
+ if target:is_plat("macosx", "iphoneos", "watchos", "appletvos") then
+ return
+ end
+ if target:has_tool("ld", "gcc", "gxx", "clang", "clangxx") or
+ target:has_tool("sh", "gcc", "gxx", "clang", "clangxx") then
+ target:add(target:is_shared() and "shflags" or "ldflags", "-Wl,--version-script=" .. scriptfile, {force = true})
+ elseif target:has_tool("ld", "dmd") or target:has_tool("sh", "dmd") then
+ target:add(target:is_shared() and "shflags" or "ldflags", "-L--version-script=" .. scriptfile, {force = true})
+ elseif target:has_tool("ld", "ld") or target:has_tool("sh", "ld") then
+ target:add(target:is_shared() and "shflags" or "ldflags", "--version-script=" .. scriptfile, {force = true})
+ end
+ end)
diff --git a/xmake/rules/platform/windows/def/xmake.lua b/xmake/rules/platform/windows/def/xmake.lua
index 7919553de..545d61fd1 100644
--- a/xmake/rules/platform/windows/def/xmake.lua
+++ b/xmake/rules/platform/windows/def/xmake.lua
@@ -22,12 +22,15 @@
rule("platform.windows.def")
set_extensions(".def")
on_config("windows", function (target)
- if target:has_tool("ld", "link") then
- for _, sourcebatch in pairs(target:sourcebatches()) do
- if sourcebatch.rulename == "platform.windows.def" then
- for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
- target:add("shflags", "/def:" .. path.translate(sourcefile), {force = true})
- end
+ if not target:is_shared() then
+ return
+ end
+ if target:has_tool("sh", "link") then
+ local sourcebatch = target:sourcebatches()["platform.windows.def"]
+ if sourcebatch then
+ for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
+ target:add("shflags", "/def:" .. path.translate(sourcefile), {force = true})
+ break;
end
end
end
diff --git a/xmake/rules/platform/windows/manifest/xmake.lua b/xmake/rules/platform/windows/manifest/xmake.lua
index fbcfe36d4..e791d9398 100644
--- a/xmake/rules/platform/windows/manifest/xmake.lua
+++ b/xmake/rules/platform/windows/manifest/xmake.lua
@@ -23,14 +23,17 @@
rule("platform.windows.manifest")
set_extensions(".manifest")
on_config("windows", function (target)
+ if not target:is_binary() then
+ return
+ end
if target:has_tool("ld", "link") then
local manifest = false
- for _, sourcebatch in pairs(target:sourcebatches()) do
- if sourcebatch.rulename == "platform.windows.manifest" then
- for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
- target:add("ldflags", "/ManifestInput:" .. path.translate(sourcefile), {force = true})
- manifest = true
- end
+ local sourcebatch = target:sourcebatches()["platform.windows.manifest"]
+ if sourcebatch then
+ for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
+ target:add("ldflags", "/ManifestInput:" .. path.translate(sourcefile), {force = true})
+ manifest = true
+ break
end
end
if manifest then