From 4ec0ff6570e24ed3e2434cc9f81c728fd002f20c Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 7 Feb 2022 23:51:40 +0800 Subject: update changelog --- CHANGELOG.md | 2 + tests/projects/c/linker_scripts/src/foo.c | 5 +++ tests/projects/c/linker_scripts/src/foo.map | 7 ++++ tests/projects/c/linker_scripts/src/main.c | 8 ++++ tests/projects/c/linker_scripts/src/main.lds | 1 + tests/projects/c/linker_scripts/test.lua | 6 +++ tests/projects/c/linker_scripts/xmake.lua | 13 +++++++ xmake/rules/asm/xmake.lua | 7 ++++ xmake/rules/c++/xmake.lua | 4 ++ xmake/rules/dlang/xmake.lua | 4 ++ xmake/rules/linker/link_scripts/xmake.lua | 49 ++++++++++++++++++++++++ xmake/rules/linker/version_scripts/xmake.lua | 50 +++++++++++++++++++++++++ xmake/rules/platform/windows/def/xmake.lua | 15 +++++--- xmake/rules/platform/windows/manifest/xmake.lua | 15 +++++--- 14 files changed, 174 insertions(+), 12 deletions(-) create mode 100644 tests/projects/c/linker_scripts/src/foo.c create mode 100644 tests/projects/c/linker_scripts/src/foo.map create mode 100644 tests/projects/c/linker_scripts/src/main.c create mode 100644 tests/projects/c/linker_scripts/src/main.lds create mode 100644 tests/projects/c/linker_scripts/test.lua create mode 100644 tests/projects/c/linker_scripts/xmake.lua create mode 100644 xmake/rules/linker/link_scripts/xmake.lua create mode 100644 xmake/rules/linker/version_scripts/xmake.lua 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 + +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 + +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 -- cgit v1.3.1 From 4d8cb21dacfc6a8cb77d4555e04997cf5fa3d906 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 7 Feb 2022 23:52:42 +0800 Subject: update changelog --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4794a166b..c82fd5000 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +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 +* [#2031](https://github.com/xmake-io/xmake/issues/2031): Support linker scripts and version scripts for add_files ### Bugs fixed @@ -1213,7 +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 支持 +* [#2031](https://github.com/xmake-io/xmake/issues/2031): 为 add_files 增加 linker scripts 和 version scripts 支持 ### Bugs 修复 -- cgit v1.3.1 From 17d633d99e0adc0871b72c6b970349c5d077e3fd Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 8 Feb 2022 00:00:06 +0800 Subject: add empty lds --- tests/projects/c/linker_scripts/src/main.lds | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/projects/c/linker_scripts/src/main.lds b/tests/projects/c/linker_scripts/src/main.lds index 0e84b7f9c..e69de29bb 100644 --- a/tests/projects/c/linker_scripts/src/main.lds +++ b/tests/projects/c/linker_scripts/src/main.lds @@ -1 +0,0 @@ -SEARCH_DIR("/lib") -- cgit v1.3.1 From 45cc977dfc486fd4ac1506f351ab0ccc37a72e7b Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 8 Feb 2022 00:35:44 +0800 Subject: fix tests --- tests/projects/c/linker_scripts/src/foo.def | 2 ++ tests/projects/c/linker_scripts/xmake.lua | 6 +++++- xmake/rules/asm/xmake.lua | 2 +- xmake/rules/c++/xmake.lua | 2 +- xmake/rules/dlang/xmake.lua | 2 +- xmake/rules/linker/xmake.lua | 24 ++++++++++++++++++++++++ 6 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 tests/projects/c/linker_scripts/src/foo.def create mode 100644 xmake/rules/linker/xmake.lua diff --git a/tests/projects/c/linker_scripts/src/foo.def b/tests/projects/c/linker_scripts/src/foo.def new file mode 100644 index 000000000..7e3ffcda0 --- /dev/null +++ b/tests/projects/c/linker_scripts/src/foo.def @@ -0,0 +1,2 @@ +EXPORTS +foo diff --git a/tests/projects/c/linker_scripts/xmake.lua b/tests/projects/c/linker_scripts/xmake.lua index 2396d1b52..51f17f68b 100644 --- a/tests/projects/c/linker_scripts/xmake.lua +++ b/tests/projects/c/linker_scripts/xmake.lua @@ -9,5 +9,9 @@ target("test") target("foo") set_kind("shared") add_files("src/foo.c") - add_files("src/foo.map") + if is_plat("windows") then + add_files("src/foo.def") + else + add_files("src/foo.map") + end diff --git a/xmake/rules/asm/xmake.lua b/xmake/rules/asm/xmake.lua index d916af720..78b693137 100644 --- a/xmake/rules/asm/xmake.lua +++ b/xmake/rules/asm/xmake.lua @@ -43,5 +43,5 @@ rule("asm") add_deps("platform.windows") -- add linker rules - add_deps("linker.link_scripts", "linker.version_scripts") + add_deps("linker") diff --git a/xmake/rules/c++/xmake.lua b/xmake/rules/c++/xmake.lua index 77223d0df..91ac9549c 100644 --- a/xmake/rules/c++/xmake.lua +++ b/xmake/rules/c++/xmake.lua @@ -66,5 +66,5 @@ rule("c++") add_deps("platform.windows") -- add linker rules - add_deps("linker.link_scripts", "linker.version_scripts") + add_deps("linker") diff --git a/xmake/rules/dlang/xmake.lua b/xmake/rules/dlang/xmake.lua index 748026e85..3e123e0d8 100644 --- a/xmake/rules/dlang/xmake.lua +++ b/xmake/rules/dlang/xmake.lua @@ -40,5 +40,5 @@ rule("dlang") add_deps("utils.symbols.extract") -- add linker rules - add_deps("linker.version_scripts") + add_deps("linker") diff --git a/xmake/rules/linker/xmake.lua b/xmake/rules/linker/xmake.lua new file mode 100644 index 000000000..602eecdfe --- /dev/null +++ b/xmake/rules/linker/xmake.lua @@ -0,0 +1,24 @@ +--!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") + add_deps("linker.link_scripts") + add_deps("linker.version_scripts") + -- cgit v1.3.1 From e9e6b38bf57dc0694a3e3f9aae50567554b4e96c Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 8 Feb 2022 00:36:52 +0800 Subject: fix tests --- tests/projects/c/linker_scripts/src/main.lds | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/projects/c/linker_scripts/src/main.lds b/tests/projects/c/linker_scripts/src/main.lds index e69de29bb..dac76f4f1 100644 --- a/tests/projects/c/linker_scripts/src/main.lds +++ b/tests/projects/c/linker_scripts/src/main.lds @@ -0,0 +1 @@ +SEARCH_DIR(.) -- cgit v1.3.1 From f141767f666d1b29c3e6c00c00c6813b26baf48e Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 8 Feb 2022 00:39:10 +0800 Subject: add a valid lds --- tests/projects/c/linker_scripts/src/main.lds | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/projects/c/linker_scripts/src/main.lds b/tests/projects/c/linker_scripts/src/main.lds index dac76f4f1..888c10be1 100644 --- a/tests/projects/c/linker_scripts/src/main.lds +++ b/tests/projects/c/linker_scripts/src/main.lds @@ -1 +1,15 @@ -SEARCH_DIR(.) +SECTIONS +{ + . = 0x10000; + .text : { *(.text) } + .init_array : + { + PROVIDE_HIDDEN (__init_array_start = .); + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array*)) + PROVIDE_HIDDEN (__init_array_end = .); + } + . = 0x8000000; + .data : { *(.data) } + .bss : { *(.bss) } +} -- cgit v1.3.1 From a2abcd6ce23c4742481e0ce832d06498557c890e Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 8 Feb 2022 00:46:37 +0800 Subject: fix tests --- .github/workflows/linux.yml | 5 ++++- .github/workflows/linux_luajit.yml | 4 ++++ tests/projects/c/linker_scripts/test.lua | 3 --- tests/projects/c/linker_scripts/xmake.lua | 4 +++- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 1851e45ab..296d97f46 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -27,6 +27,10 @@ jobs: - uses: little-core-labs/get-git-tag@v3.0.2 id: tagName + - name: Prepare + run: | + sudo apt install -y ruby ruby-dev rubygems build-essential llvm + - name: Tests run: | xmake lua -v -D tests/run.lua @@ -34,7 +38,6 @@ jobs: - name: Artifact run: | - sudo apt install -y ruby ruby-dev rubygems build-essential sudo gem install --no-document fpm scripts/makepkg deb - uses: actions/upload-artifact@v2 diff --git a/.github/workflows/linux_luajit.yml b/.github/workflows/linux_luajit.yml index 919c11550..674e5bd34 100644 --- a/.github/workflows/linux_luajit.yml +++ b/.github/workflows/linux_luajit.yml @@ -29,6 +29,10 @@ jobs: source ~/.xmake/profile xmake --version + - name: Prepare + run: | + sudo apt install -y build-essential llvm + - name: Tests run: | xmake lua -v -D tests/run.lua diff --git a/tests/projects/c/linker_scripts/test.lua b/tests/projects/c/linker_scripts/test.lua index b76241be2..b57362078 100644 --- a/tests/projects/c/linker_scripts/test.lua +++ b/tests/projects/c/linker_scripts/test.lua @@ -1,6 +1,3 @@ --- 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 index 51f17f68b..036c4b486 100644 --- a/tests/projects/c/linker_scripts/xmake.lua +++ b/tests/projects/c/linker_scripts/xmake.lua @@ -4,7 +4,9 @@ target("test") add_deps("foo") set_kind("binary") add_files("src/main.c") - add_files("src/main.lds") + if is_plat("linux") and is_arch("x86_64") then + add_files("src/main.lds") + end target("foo") set_kind("shared") -- cgit v1.3.1