diff options
| author | ruki <[email protected]> | 2025-02-13 16:59:00 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-02-13 16:59:00 +0800 |
| commit | f6179e3d0f3a073dd2e9a4308ca5a8f4f056a95e (patch) | |
| tree | 8602e6265f5b18c9673f4008d5aea7e1855f3947 | |
| parent | ef99de52cdba31eec636256da37be6c468637dd0 (diff) | |
| parent | 900eb24cae747371b296962e09add22d8d9f95c1 (diff) | |
Merge pull request #6153 from xmake-io/kotlin
Add kotlin language and kotlin-native compiler support
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | README_zh.md | 2 | ||||
| -rw-r--r-- | tests/projects/kotlin-native/console/src/main.kt | 3 | ||||
| -rw-r--r-- | tests/projects/kotlin-native/console/xmake.lua | 7 | ||||
| -rw-r--r-- | tests/projects/kotlin-native/shared_library/src/main.kt | 3 | ||||
| -rw-r--r-- | tests/projects/kotlin-native/shared_library/xmake.lua | 7 | ||||
| -rw-r--r-- | tests/projects/kotlin-native/static_library/src/main.kt | 3 | ||||
| -rw-r--r-- | tests/projects/kotlin-native/static_library/xmake.lua | 7 | ||||
| -rw-r--r-- | xmake/core/tool/toolchain.lua | 6 | ||||
| -rw-r--r-- | xmake/languages/kotlin/check_main.lua | 40 | ||||
| -rw-r--r-- | xmake/languages/kotlin/load.lua | 56 | ||||
| -rw-r--r-- | xmake/languages/kotlin/xmake.lua | 112 | ||||
| -rw-r--r-- | xmake/modules/core/tools/kotlinc_native.lua | 54 | ||||
| -rw-r--r-- | xmake/modules/core/tools/kotlinc_native/has_flags.lua | 112 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/find_kotlinc_native.lua | 50 | ||||
| -rw-r--r-- | xmake/rules/kotlin-native/xmake.lua | 34 | ||||
| -rw-r--r-- | xmake/toolchains/kotlin-native/xmake.lua | 61 |
17 files changed, 558 insertions, 1 deletions
@@ -249,6 +249,7 @@ hdk Harmony SDK ti-c2000 TI-CGT C2000 compiler ti-c6000 TI-CGT C6000 compiler iararm IAR ARM C/C++ Compiler +kotlin-native Kotlin Native Programming Language Compiler ``` ## Supported languages @@ -272,6 +273,7 @@ iararm IAR ARM C/C++ Compiler * YASM * MASM32 * Cppfront +* Kotlin ## Features diff --git a/README_zh.md b/README_zh.md index ba8929c26..1ed1c4231 100644 --- a/README_zh.md +++ b/README_zh.md @@ -310,6 +310,7 @@ hdk Harmony SDK ti-c2000 TI-CGT C2000 compiler ti-c6000 TI-CGT C6000 compiler iararm IAR ARM C/C++ Compiler +kotlin-native Kotlin Native Programming Language Compiler ``` ## 支持语言 @@ -333,6 +334,7 @@ iararm IAR ARM C/C++ Compiler * YASM * MASM32 * Cppfront +* Kotlin ## 支持特性 diff --git a/tests/projects/kotlin-native/console/src/main.kt b/tests/projects/kotlin-native/console/src/main.kt new file mode 100644 index 000000000..543f5ac8f --- /dev/null +++ b/tests/projects/kotlin-native/console/src/main.kt @@ -0,0 +1,3 @@ +fun main() { + println("hello xmake!") +} diff --git a/tests/projects/kotlin-native/console/xmake.lua b/tests/projects/kotlin-native/console/xmake.lua new file mode 100644 index 000000000..81211d52d --- /dev/null +++ b/tests/projects/kotlin-native/console/xmake.lua @@ -0,0 +1,7 @@ +add_rules("mode.debug", "mode.release") +add_requires("kotlin-native") +target("test") + set_kind("binary") + add_files("src/*.kt") + set_toolchains("@kotlin-native") + diff --git a/tests/projects/kotlin-native/shared_library/src/main.kt b/tests/projects/kotlin-native/shared_library/src/main.kt new file mode 100644 index 000000000..543f5ac8f --- /dev/null +++ b/tests/projects/kotlin-native/shared_library/src/main.kt @@ -0,0 +1,3 @@ +fun main() { + println("hello xmake!") +} diff --git a/tests/projects/kotlin-native/shared_library/xmake.lua b/tests/projects/kotlin-native/shared_library/xmake.lua new file mode 100644 index 000000000..20c401d76 --- /dev/null +++ b/tests/projects/kotlin-native/shared_library/xmake.lua @@ -0,0 +1,7 @@ +add_rules("mode.debug", "mode.release") +add_requires("kotlin-native") +target("test") + set_kind("shared") + add_files("src/*.kt") + set_toolchains("@kotlin-native") + diff --git a/tests/projects/kotlin-native/static_library/src/main.kt b/tests/projects/kotlin-native/static_library/src/main.kt new file mode 100644 index 000000000..543f5ac8f --- /dev/null +++ b/tests/projects/kotlin-native/static_library/src/main.kt @@ -0,0 +1,3 @@ +fun main() { + println("hello xmake!") +} diff --git a/tests/projects/kotlin-native/static_library/xmake.lua b/tests/projects/kotlin-native/static_library/xmake.lua new file mode 100644 index 000000000..2e827ec78 --- /dev/null +++ b/tests/projects/kotlin-native/static_library/xmake.lua @@ -0,0 +1,7 @@ +add_rules("mode.debug", "mode.release") +add_requires("kotlin-native") +target("test") + set_kind("static") + add_files("src/*.kt") + set_toolchains("@kotlin-native") + diff --git a/xmake/core/tool/toolchain.lua b/xmake/core/tool/toolchain.lua index 1085df198..e5a7626c2 100644 --- a/xmake/core/tool/toolchain.lua +++ b/xmake/core/tool/toolchain.lua @@ -454,7 +454,11 @@ function _instance:_description(toolkind) nc = "the nim compiler", ncld = "the nim linker", ncsh = "the nim shared library linker", - ncar = "the nim static library archiver" + ncar = "the nim static library archiver", + kc = "the kotlin native compiler", + kcld = "the kotlin native linker", + kcsh = "the kotlin native shared library linker", + kcar = "the kotlin native static library archiver", } self._DESCRIPTIONS = descriptions end diff --git a/xmake/languages/kotlin/check_main.lua b/xmake/languages/kotlin/check_main.lua new file mode 100644 index 000000000..7fa4a4183 --- /dev/null +++ b/xmake/languages/kotlin/check_main.lua @@ -0,0 +1,40 @@ +--!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 check_main.lua +-- + +-- check it +function main(sourcefile) + + -- load source code + local sourcecode = io.readfile(sourcefile) + + -- remove comment first + sourcecode = sourcecode:gsub("/%*.-%*/", "") + sourcecode = sourcecode:gsub("//.-\n", "\n") + + -- find fun main() { + if sourcecode:find("fun%s+main%s*%(.-%)") then + return true + end + + -- no main function + return false +end + + diff --git a/xmake/languages/kotlin/load.lua b/xmake/languages/kotlin/load.lua new file mode 100644 index 000000000..8d7ad0430 --- /dev/null +++ b/xmake/languages/kotlin/load.lua @@ -0,0 +1,56 @@ +--!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 load.lua +-- + +function _get_apis() + local apis = {} + apis.values = { + -- target.add_xxx + "target.add_kcflags" + , "target.add_ldflags" + , "target.add_arflags" + , "target.add_shflags" + , "target.add_rpathdirs" -- @note do not translate path, it's usually an absolute path or contains $ORIGIN/@loader_path + -- option.add_xxx + , "option.add_kcflags" + , "option.add_ldflags" + , "option.add_arflags" + , "option.add_shflags" + , "option.add_rpathdirs" + -- toolchain.add_xxx + , "toolchain.add_kcflags" + , "toolchain.add_ldflags" + , "toolchain.add_arflags" + , "toolchain.add_shflags" + , "toolchain.add_rpathdirs" + } + apis.paths = { + -- target.add_xxx + "target.add_linkdirs" + -- option.add_xxx + , "option.add_linkdirs" + } + return apis +end + +function main() + return {apis = _get_apis()} +end + + diff --git a/xmake/languages/kotlin/xmake.lua b/xmake/languages/kotlin/xmake.lua new file mode 100644 index 000000000..3269ea6cd --- /dev/null +++ b/xmake/languages/kotlin/xmake.lua @@ -0,0 +1,112 @@ +--!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 +-- + +language("kotlin") + add_rules("kotlin-native") + set_sourcekinds {kc = ".kt"} + set_sourceflags {kc = "kcflags"} + set_targetkinds {binary = "kcld", static = "kcar", shared = "kcsh"} + set_targetflags {binary = "ldflags", static = "arflags", shared = "shflags"} + set_langkinds {kotlin = "kc"} + set_mixingkinds("kc", "cc", "cxx") + + on_load("load") + on_check_main("check_main") + + set_nameflags { + object = { + "target.symbols" + , "target.warnings" + , "target.optimize:check" + , "target.vectorexts:check" + } + , binary = { + "config.linkdirs" + , "config.frameworkdirs" + , "target.linkdirs" + , "target.frameworkdirs" + , "target.rpathdirs" + , "target.strip" + , "target.symbols" + , "toolchain.linkdirs" + , "toolchain.frameworkdirs" + , "toolchain.rpathdirs" + , "config.frameworks" + , "target.frameworks" + , "toolchain.frameworks" + , "config.links" + , "target.links" + , "toolchain.links" + , "config.syslinks" + , "target.syslinks" + , "toolchain.syslinks" + } + , shared = { + "config.linkdirs" + , "config.frameworkdirs" + , "target.linkdirs" + , "target.frameworkdirs" + , "target.rpathdirs" + , "target.strip" + , "target.symbols" + , "toolchain.linkdirs" + , "toolchain.frameworkdirs" + , "toolchain.rpathdirs" + , "config.frameworks" + , "target.frameworks" + , "toolchain.frameworks" + , "config.links" + , "target.links" + , "toolchain.links" + , "config.syslinks" + , "target.syslinks" + , "toolchain.syslinks" + } + , static = { + "config.linkdirs" + , "config.frameworkdirs" + , "target.linkdirs" + , "target.frameworkdirs" + , "target.strip" + , "target.symbols" + , "toolchain.linkdirs" + , "toolchain.frameworkdirs" + , "config.frameworks" + , "target.frameworks" + , "toolchain.frameworks" + } + } + + set_menu { + config = + { + {category = "Cross Complation Configuration/Compiler Configuration" } + , {nil, "kc", "kv", nil, "The Rust Compiler" } + + , {category = "Cross Complation Configuration/Linker Configuration" } + , {nil, "kcld", "kv", nil, "The Rust Linker" } + , {nil, "kcar", "kv", nil, "The Rust Static Library Archiver" } + , {nil, "kcsh", "kv", nil, "The Rust Shared Library Linker" } + + , {category = "Cross Complation Configuration/Builtin Flags Configuration" } + , {nil, "linkdirs", "kv", nil, "The Link Search Directories" } + } + } + diff --git a/xmake/modules/core/tools/kotlinc_native.lua b/xmake/modules/core/tools/kotlinc_native.lua new file mode 100644 index 000000000..1ca5cc172 --- /dev/null +++ b/xmake/modules/core/tools/kotlinc_native.lua @@ -0,0 +1,54 @@ +--!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 kotlinc_native.lua +-- + +-- imports +import("core.base.option") +import("core.project.config") +import("core.project.project") +import("core.language.language") + +-- init it +function init(self) +end + +-- make the build arguments list +function buildargv(self, sourcefiles, targetkind, targetfile, flags) + return self:program(), table.join(flags, sourcefiles, "-o", targetfile) +end + +-- build the target file +function build(self, sourcefiles, targetkind, targetfile, flags) + os.mkdir(path.directory(targetfile)) + os.runv(buildargv(self, sourcefiles, targetkind, targetfile, flags)) + if targetkind == "binary" then + local targetfile_real = targetfile .. (self:is_plat("windows") and ".exe" or ".kexe") + if os.isfile(targetfile_real) then + os.mv(targetfile_real, targetfile) + if self:is_plat("macosx", "iphoneos") then + local symbolfile_real = targetfile_real .. ".dSYM" + local symbolfile = targetfile .. ".dSYM" + if os.isdir(symbolfile_real) then + os.mv(symbolfile_real, symbolfile) + end + end + end + end +end + diff --git a/xmake/modules/core/tools/kotlinc_native/has_flags.lua b/xmake/modules/core/tools/kotlinc_native/has_flags.lua new file mode 100644 index 000000000..73484f6e3 --- /dev/null +++ b/xmake/modules/core/tools/kotlinc_native/has_flags.lua @@ -0,0 +1,112 @@ +--!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 has_flags.lua +-- + +-- imports +import("core.cache.detectcache") + +-- is linker? +function _islinker(flags, opt) + local toolkind = opt.toolkind or "" + return toolkind == "ld" or toolkind == "sh" or toolkind:endswith("ld") or toolkind:endswith("sh") +end + +-- try running +function _try_running(...) + + local argv = {...} + local errors = nil + return try { function () os.runv(table.unpack(argv)); return true end, catch { function (errs) errors = (errs or ""):trim() end }}, errors +end + +-- attempt to check it from the argument list +function _check_from_arglist(flags, opt) + + -- only one flag? + if #flags > 1 then + return + end + + -- make cache key + local key = "core.tools.kotlinc_native.has_flags" + + -- make allflags key + local flagskey = opt.program .. "_" .. (opt.programver or "") + + -- get all allflags from argument list + local allflags = detectcache:get2(key, flagskey) + if not allflags then + + -- get argument list + allflags = {} + local arglist = os.iorunv(opt.program, {"-help"}) + if arglist then + for arg in arglist:gmatch("%s+(%-[%-%a%d]+)%s+") do + allflags[arg] = true + end + end + + -- save cache + detectcache:set2(key, flagskey, allflags) + detectcache:save() + end + return allflags[flags[1]] +end + +-- try running to check flags +function _check_try_running(flags, opt, islinker) + + -- make an stub source file + local sourcefile = path.join(os.tmpdir(), "detect", "kotlinc_native_has_flags.kt") + if not os.isfile(sourcefile) then + io.writefile(sourcefile, "fun main() {\n}") + end + + -- check flags for linker + if islinker then + return _try_running(opt.program, table.join(flags, sourcefile, "-o", os.tmpfile()), opt) + end + + -- check flags for compiler + local libraryfile = os.tmpfile() + local ok, errors = _try_running(opt.program, table.join("-produce", "static", flags, sourcefile, "-o", libraryfile)) + os.tryrm(libraryfile) + return ok, errors +end + +-- has_flags(flags)? +-- +-- @param opt the argument options, e.g. {toolname = "", program = "", programver = "", toolkind = "[cc|cxx|ld|ar|sh|gc|rc|dc|mm|mxx]"} +-- +-- @return true or false +-- +function main(flags, opt) + + -- is linker? + local islinker = _islinker(flags, opt) + + -- attempt to check it from the argument list + if _check_from_arglist(flags, opt) then + return true + end + + -- try running to check it + return _check_try_running(flags, opt, islinker) +end + diff --git a/xmake/modules/detect/tools/find_kotlinc_native.lua b/xmake/modules/detect/tools/find_kotlinc_native.lua new file mode 100644 index 000000000..fe5a1a7c2 --- /dev/null +++ b/xmake/modules/detect/tools/find_kotlinc_native.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 find_kotlinc_native.lua +-- + +-- imports +import("lib.detect.find_program") +import("lib.detect.find_programver") + +-- find kotlinc_native +-- +-- @param opt the argument options, e.g. {version = true} +-- +-- @return program, version +-- +-- @code +-- +-- local kotlinc_native = find_kotlinc_native() +-- local kotlinc_native, version = find_kotlinc_native({version = true}) +-- +-- @endcode +-- +function main(opt) + opt = opt or {} + opt.check = opt.check or "-version" + opt.command = opt.command or "-version" + + local program = find_program(opt.program or "kotlinc_native", opt) + local version = nil + if program and opt and opt.version then + version = find_programver(program, opt) + end + return program, version +end + diff --git a/xmake/rules/kotlin-native/xmake.lua b/xmake/rules/kotlin-native/xmake.lua new file mode 100644 index 000000000..7b359094a --- /dev/null +++ b/xmake/rules/kotlin-native/xmake.lua @@ -0,0 +1,34 @@ +--!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("kotlin-native.build") + set_sourcekinds("kc") + on_load(function (target) + if target:is_static() then + target:add("arflags", {"-produce", "static"}, {force = true}) + elseif target:is_shared() then + target:add("shflags", {"-produce", "dynamic"}, {force = true}) + end + end) + on_build("build.target") + +rule("kotlin-native") + add_deps("kotlin-native.build") + add_deps("utils.inherit.links") diff --git a/xmake/toolchains/kotlin-native/xmake.lua b/xmake/toolchains/kotlin-native/xmake.lua new file mode 100644 index 000000000..9787fc204 --- /dev/null +++ b/xmake/toolchains/kotlin-native/xmake.lua @@ -0,0 +1,61 @@ +--!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 +-- + +toolchain("kotlin-native") + set_kind("standalone") + set_homepage("https://kotlinlang.org") + set_description("The Kotlin Programming Language Compiler. ") + + set_toolset("kc", "$(env KC)", "kotlinc-native") + set_toolset("kcld", "$(env KC)", "kotlinc-native") + set_toolset("kcsh", "$(env KC)", "kotlinc-native") + set_toolset("kcar", "$(env KC)", "kotlinc-native") + + on_check(function (toolchain) + import("lib.detect.find_tool") + + local paths = {} + for _, package in ipairs(toolchain:packages()) do + local envs = package:envs() + if envs then + table.join2(paths, envs.PATH) + end + end + if toolchain:bindir() then + table.insert(paths, toolchain:bindir()) + end + local kotlinc_native = find_tool("kotlinc-native", {paths = paths}) + if kotlinc_native and kotlinc_native.program then + kotlinc_native = kotlinc_native.program + end + if kotlinc_native then + if path.is_absolute(kotlinc_native) then + local bindir = path.directory(kotlinc_native) + toolchain:config_set("bindir", bindir) + toolchain:config_set("sdkdir", path.directory(bindir)) + end + toolchain:configs_save() + return true + end + end) + + on_load(function (toolchain) + -- TODO + end) |
