diff options
| author | ruki <[email protected]> | 2022-02-08 00:18:31 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-02-08 00:18:31 +0800 |
| commit | c80d0c5638f83d7de838a09554daa48800c71fe3 (patch) | |
| tree | a2aa57e8711b6b9f906f2eb8924a6ce6d67c4808 | |
| parent | c88d9afcb3e929d6c0a4f3644399ac3df572829f (diff) | |
| parent | a2abcd6ce23c4742481e0ce832d06498557c890e (diff) | |
Merge pull request #2030 from xmake-io/linkscript
Support linker scripts
| -rw-r--r-- | .github/workflows/linux.yml | 5 | ||||
| -rw-r--r-- | .github/workflows/linux_luajit.yml | 4 | ||||
| -rw-r--r-- | CHANGELOG.md | 2 | ||||
| -rw-r--r-- | tests/projects/c/linker_scripts/src/foo.c | 5 | ||||
| -rw-r--r-- | tests/projects/c/linker_scripts/src/foo.def | 2 | ||||
| -rw-r--r-- | tests/projects/c/linker_scripts/src/foo.map | 7 | ||||
| -rw-r--r-- | tests/projects/c/linker_scripts/src/main.c | 8 | ||||
| -rw-r--r-- | tests/projects/c/linker_scripts/src/main.lds | 15 | ||||
| -rw-r--r-- | tests/projects/c/linker_scripts/test.lua | 3 | ||||
| -rw-r--r-- | tests/projects/c/linker_scripts/xmake.lua | 19 | ||||
| -rw-r--r-- | xmake/rules/asm/xmake.lua | 7 | ||||
| -rw-r--r-- | xmake/rules/c++/xmake.lua | 4 | ||||
| -rw-r--r-- | xmake/rules/dlang/xmake.lua | 4 | ||||
| -rw-r--r-- | xmake/rules/linker/link_scripts/xmake.lua | 49 | ||||
| -rw-r--r-- | xmake/rules/linker/version_scripts/xmake.lua | 50 | ||||
| -rw-r--r-- | xmake/rules/linker/xmake.lua | 24 | ||||
| -rw-r--r-- | xmake/rules/platform/windows/def/xmake.lua | 15 | ||||
| -rw-r--r-- | xmake/rules/platform/windows/manifest/xmake.lua | 15 |
18 files changed, 225 insertions, 13 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/[email protected] 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/CHANGELOG.md b/CHANGELOG.md index a453b971c..c82fd5000 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 +* [#2031](https://github.com/xmake-io/xmake/issues/2031): 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 支持 +* [#2031](https://github.com/xmake-io/xmake/issues/2031): 为 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.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/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..888c10be1 --- /dev/null +++ b/tests/projects/c/linker_scripts/src/main.lds @@ -0,0 +1,15 @@ +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) } +} diff --git a/tests/projects/c/linker_scripts/test.lua b/tests/projects/c/linker_scripts/test.lua new file mode 100644 index 000000000..b57362078 --- /dev/null +++ b/tests/projects/c/linker_scripts/test.lua @@ -0,0 +1,3 @@ +function main(t) + 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..036c4b486 --- /dev/null +++ b/tests/projects/c/linker_scripts/xmake.lua @@ -0,0 +1,19 @@ +add_rules("mode.debug", "mode.release") + +target("test") + add_deps("foo") + set_kind("binary") + add_files("src/main.c") + if is_plat("linux") and is_arch("x86_64") then + add_files("src/main.lds") + end + +target("foo") + set_kind("shared") + add_files("src/foo.c") + 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 153bc4802..78b693137 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") + diff --git a/xmake/rules/c++/xmake.lua b/xmake/rules/c++/xmake.lua index 14dc961d3..91ac9549c 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") + diff --git a/xmake/rules/dlang/xmake.lua b/xmake/rules/dlang/xmake.lua index 0b4a5ff07..3e123e0d8 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") + 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/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") + 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 |
