diff options
| author | ruki <[email protected]> | 2024-01-24 22:39:30 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2024-01-25 12:09:59 +0800 |
| commit | fec77395b2f96bac0bde5cd4d79115fd6d4b377e (patch) | |
| tree | 7d7cec18c2db28b41c0c547a72dfed366a0b439b | |
| parent | c6445c7e401b348c5ca8afce229a1f4f45e9cf61 (diff) | |
split c and c++ languages
| -rw-r--r-- | xmake/languages/c++/load.lua | 13 | ||||
| -rw-r--r-- | xmake/languages/c++/xmake.lua | 11 | ||||
| -rw-r--r-- | xmake/languages/c/check_main.lua | 41 | ||||
| -rw-r--r-- | xmake/languages/c/load.lua | 120 | ||||
| -rw-r--r-- | xmake/languages/c/xmake.lua | 152 | ||||
| -rw-r--r-- | xmake/modules/core/tools/clang.lua | 24 | ||||
| -rw-r--r-- | xmake/rules/c++/xmake.lua | 27 |
7 files changed, 359 insertions, 29 deletions
diff --git a/xmake/languages/c++/load.lua b/xmake/languages/c++/load.lua index 932769a7c..b38f4874b 100644 --- a/xmake/languages/c++/load.lua +++ b/xmake/languages/c++/load.lua @@ -25,7 +25,6 @@ function _get_apis() -- target.add_xxx "target.add_links" , "target.add_syslinks" - , "target.add_cflags" , "target.add_cxflags" , "target.add_cxxflags" , "target.add_ldflags" @@ -37,15 +36,11 @@ function _get_apis() , "target.add_rpathdirs" -- @note do not translate path, it's usually an absolute path or contains $ORIGIN/@loader_path , "target.add_forceincludes" -- option.add_xxx - , "option.add_cincludes" , "option.add_cxxincludes" - , "option.add_cfuncs" , "option.add_cxxfuncs" - , "option.add_ctypes" , "option.add_cxxtypes" , "option.add_links" , "option.add_syslinks" - , "option.add_cflags" , "option.add_cxflags" , "option.add_cxxflags" , "option.add_ldflags" @@ -58,7 +53,6 @@ function _get_apis() -- package.add_xxx , "package.add_links" , "package.add_syslinks" - , "package.add_cflags" , "package.add_cxflags" , "package.add_cxxflags" , "package.add_ldflags" @@ -75,7 +69,6 @@ function _get_apis() -- toolchain.add_xxx , "toolchain.add_links" , "toolchain.add_syslinks" - , "toolchain.add_cflags" , "toolchain.add_cxflags" , "toolchain.add_cxxflags" , "toolchain.add_ldflags" @@ -100,8 +93,7 @@ function _get_apis() } apis.paths = { -- target.set_xxx - "target.set_pcheader" - , "target.set_pcxxheader" + "target.set_pcxxheader" -- target.add_xxx , "target.add_headerfiles" , "target.add_linkdirs" @@ -116,8 +108,7 @@ function _get_apis() } apis.dictionary = { -- option.add_xxx - "option.add_csnippets" - , "option.add_cxxsnippets" + "option.add_cxxsnippets" } return apis end diff --git a/xmake/languages/c++/xmake.lua b/xmake/languages/c++/xmake.lua index 22a69a417..487746303 100644 --- a/xmake/languages/c++/xmake.lua +++ b/xmake/languages/c++/xmake.lua @@ -20,11 +20,11 @@ language("c++") add_rules("c++") - set_sourcekinds {cc = ".c", cxx = {".cpp", ".cc", ".cxx", ".mpp", ".mxx", ".cppm", ".ixx", ".c++"}} - set_sourceflags {cc = {"cflags", "cxflags"}, cxx = {"cxxflags", "cxflags"}} + set_sourcekinds {cxx = {".cpp", ".cc", ".cxx", ".mpp", ".mxx", ".cppm", ".ixx", ".c++"}} + set_sourceflags {cxx = {"cxxflags", "cxflags"}} set_targetkinds {binary = "ld", static = "ar", shared = "sh"} set_targetflags {binary = "ldflags", static = "arflags", shared = "shflags"} - set_langkinds {c = "cc", cxx = "cxx"} + set_langkinds {cxx = "cxx"} set_mixingkinds("cc", "cxx", "as", "mrc") on_load("load") @@ -49,7 +49,6 @@ language("c++") , "target.frameworks" , "target.exceptions" , "target.encodings" - , "target.pcheader" , "target.pcxxheader" , "target.forceincludes" , "toolchain.includedirs" @@ -118,9 +117,8 @@ language("c++") config = { {category = "Cross Complation Configuration/Compiler Configuration" } - , {nil, "cc", "kv", nil, "The C Compiler" } , {nil, "cxx", "kv", nil, "The C++ Compiler" } - , {nil, "cpp", "kv", nil, "The C Preprocessor" } + , {nil, "cpp", "kv", nil, "The C/C++ Preprocessor" } , {category = "Cross Complation Configuration/Linker Configuration" } , {nil, "ld", "kv", nil, "The Linker" } @@ -129,7 +127,6 @@ language("c++") , {nil, "ranlib", "kv", nil, "The Static Library Index Generator" } , {category = "Cross Complation Configuration/Compiler Flags Configuration" } - , {nil, "cflags", "kv", nil, "The C Compiler Flags" } , {nil, "cxflags", "kv", nil, "The C/C++ compiler Flags" } , {nil, "cxxflags", "kv", nil, "The C++ Compiler Flags" } diff --git a/xmake/languages/c/check_main.lua b/xmake/languages/c/check_main.lua new file mode 100644 index 000000000..55d00e2de --- /dev/null +++ b/xmake/languages/c/check_main.lua @@ -0,0 +1,41 @@ +--!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 int main(int argc, char** argv) {} + if sourcecode:find("%s+main%s*%(.-%)") then + return true + end + + -- no main function + return false +end + + diff --git a/xmake/languages/c/load.lua b/xmake/languages/c/load.lua new file mode 100644 index 000000000..3c470e0eb --- /dev/null +++ b/xmake/languages/c/load.lua @@ -0,0 +1,120 @@ +--!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 +-- + +-- get apis +function _get_apis() + local apis = {} + apis.values = { + -- target.add_xxx + "target.add_links" + , "target.add_syslinks" + , "target.add_cflags" + , "target.add_cxflags" + , "target.add_ldflags" + , "target.add_arflags" + , "target.add_shflags" + , "target.add_defines" + , "target.add_undefines" + , "target.add_frameworks" + , "target.add_rpathdirs" -- @note do not translate path, it's usually an absolute path or contains $ORIGIN/@loader_path + , "target.add_forceincludes" + -- option.add_xxx + , "option.add_cincludes" + , "option.add_cfuncs" + , "option.add_ctypes" + , "option.add_links" + , "option.add_syslinks" + , "option.add_cflags" + , "option.add_cxflags" + , "option.add_ldflags" + , "option.add_arflags" + , "option.add_shflags" + , "option.add_defines" + , "option.add_undefines" + , "option.add_frameworks" + , "option.add_rpathdirs" + -- package.add_xxx + , "package.add_links" + , "package.add_syslinks" + , "package.add_cflags" + , "package.add_cxflags" + , "package.add_ldflags" + , "package.add_arflags" + , "package.add_shflags" + , "package.add_defines" + , "package.add_undefines" + , "package.add_frameworks" + , "package.add_rpathdirs" + , "package.add_linkdirs" + , "package.add_includedirs" --@note we need not uses paths for package, see https://github.com/xmake-io/xmake/issues/717 + , "package.add_sysincludedirs" + , "package.add_frameworkdirs" + -- toolchain.add_xxx + , "toolchain.add_links" + , "toolchain.add_syslinks" + , "toolchain.add_cflags" + , "toolchain.add_cxflags" + , "toolchain.add_ldflags" + , "toolchain.add_arflags" + , "toolchain.add_shflags" + , "toolchain.add_defines" + , "toolchain.add_undefines" + , "toolchain.add_frameworks" + , "toolchain.add_rpathdirs" + , "toolchain.add_linkdirs" + , "toolchain.add_includedirs" + , "toolchain.add_sysincludedirs" + , "toolchain.add_frameworkdirs" + } + apis.groups = { + -- target.add_xxx + "target.add_linkorders" + , "target.add_linkgroups" + -- package.add_xxx + , "package.add_linkorders" + , "package.add_linkgroups" + } + apis.paths = { + -- target.set_xxx + "target.set_pcheader" + -- target.add_xxx + , "target.add_headerfiles" + , "target.add_linkdirs" + , "target.add_includedirs" + , "target.add_sysincludedirs" + , "target.add_frameworkdirs" + -- option.add_xxx + , "option.add_linkdirs" + , "option.add_includedirs" + , "option.add_sysincludedirs" + , "option.add_frameworkdirs" + } + apis.dictionary = { + -- option.add_xxx + "option.add_csnippets" + } + return apis +end + +function main() + return {apis = _get_apis()} +end + + diff --git a/xmake/languages/c/xmake.lua b/xmake/languages/c/xmake.lua new file mode 100644 index 000000000..4e1a0041b --- /dev/null +++ b/xmake/languages/c/xmake.lua @@ -0,0 +1,152 @@ +--!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("c") + add_rules("c") + set_sourcekinds {cc = ".c"} + set_sourceflags {cc = {"cflags", "cxflags"}} + set_targetkinds {binary = "ld", static = "ar", shared = "sh"} + set_targetflags {binary = "ldflags", static = "arflags", shared = "shflags"} + set_langkinds {c = "cc"} + set_mixingkinds("cc", "cxx", "as", "mrc") + + on_load("load") + on_check_main("check_main") + + set_nameflags { + object = { + "config.includedirs" + , "config.frameworkdirs" + , "config.frameworks" + , "target.symbols" + , "target.warnings" + , "target.fpmodels" + , "target.optimize:check" + , "target.vectorexts:check" + , "target.languages" + , "target.runtimes" + , "target.includedirs" + , "target.defines" + , "target.undefines" + , "target.frameworkdirs" + , "target.frameworks" + , "target.exceptions" + , "target.encodings" + , "target.pcheader" + , "target.forceincludes" + , "toolchain.includedirs" + , "toolchain.defines" + , "toolchain.undefines" + , "toolchain.frameworkdirs" + , "toolchain.frameworks" + , "target.sysincludedirs" + , "toolchain.sysincludedirs" + } + , binary = { + "config.linkdirs" + , "config.frameworkdirs" + , "target.linkdirs" + , "target.frameworkdirs" + , "target.rpathdirs" + , "target.strip" + , "target.symbols" + , "target.optimize:check" + , "target.runtimes" + , "toolchain.linkdirs" + , "toolchain.rpathdirs" + , "toolchain.frameworkdirs" + , "config.links" + , "target.linkgroups" -- we must move it before target.links, because we need sort correct order for package and its deps + , "target.links" + , "toolchain.links" + , "config.frameworks" + , "target.frameworks" + , "toolchain.frameworks" + , "config.syslinks" + , "target.syslinks" + , "toolchain.syslinks" + } + , shared = { + "config.linkdirs" + , "config.frameworkdirs" + , "target.linkdirs" + , "target.frameworkdirs" + , "target.rpathdirs" + , "target.strip" + , "target.symbols" + , "target.optimize:check" + , "target.runtimes" + , "toolchain.linkdirs" + , "toolchain.rpathdirs" + , "toolchain.frameworkdirs" + , "config.links" + , "target.links" + , "target.linkgroups" + , "toolchain.links" + , "config.frameworks" + , "target.frameworks" + , "toolchain.frameworks" + , "config.syslinks" + , "target.syslinks" + , "toolchain.syslinks" + } + , static = { + "target.strip" + , "target.symbols" + } + } + + set_menu { + config = + { + {category = "Cross Complation Configuration/Compiler Configuration" } + , {nil, "cc", "kv", nil, "The C Compiler" } + , {nil, "cpp", "kv", nil, "The C/C++ Preprocessor" } + + , {category = "Cross Complation Configuration/Linker Configuration" } + , {nil, "ld", "kv", nil, "The Linker" } + , {nil, "ar", "kv", nil, "The Static Library Linker" } + , {nil, "sh", "kv", nil, "The Shared Library Linker" } + , {nil, "ranlib", "kv", nil, "The Static Library Index Generator" } + + , {category = "Cross Complation Configuration/Compiler Flags Configuration" } + , {nil, "cflags", "kv", nil, "The C Compiler Flags" } + , {nil, "cxflags", "kv", nil, "The C/C++ compiler Flags" } + + , {category = "Cross Complation Configuration/Linker Flags Configuration" } + , {nil, "ldflags", "kv", nil, "The Binary Linker Flags" } + , {nil, "arflags", "kv", nil, "The Static Library Linker Flags" } + , {nil, "shflags", "kv", nil, "The Shared Library Linker Flags" } + + , {category = "Cross Complation Configuration/Builtin Flags Configuration" } + , {nil, "links", "kv", nil, "The Link Libraries" } + , {nil, "syslinks", "kv", nil, "The System Link Libraries" } + , {nil, "linkdirs", "kv", nil, "The Link Search Directories" } + , {nil, "includedirs", "kv", nil, "The Include Search Directories" } + , {nil, "frameworks", "kv", nil, "The Frameworks" } + , {nil, "frameworkdirs", "kv", nil, "The Frameworks Search Directories" } + } + } + + + + + + diff --git a/xmake/modules/core/tools/clang.lua b/xmake/modules/core/tools/clang.lua index 0ca812f39..313728894 100644 --- a/xmake/modules/core/tools/clang.lua +++ b/xmake/modules/core/tools/clang.lua @@ -194,7 +194,8 @@ end -- make the runtime flag -- @see https://github.com/xmake-io/xmake/issues/3546 -function nf_runtime(self, runtime) +function nf_runtime(self, runtime, opt) + opt = opt or {} local kind = self:kind() if self:is_plat("windows") and runtime then if not _has_ms_runtime_lib(self) then @@ -230,15 +231,18 @@ function nf_runtime(self, runtime) ["stdc++_shared"] = "-stdlib=libstdc++", } elseif kind == "ld" or kind == "sh" then - maps = { - ["c++_static"] = "-stdlib=libc++", - ["c++_shared"] = "-stdlib=libc++", - ["stdc++_static"] = "-stdlib=libstdc++", - ["stdc++_shared"] = "-stdlib=libstdc++", - } - if runtime:endswith("_static") and _has_static_libstdcxx(self) then - maps["c++_static"] = table.join(maps["c++_static"], "-static-libstdc++") - maps["stdc++_static"] = table.join(maps["stdc++_static"], "-static-libstdc++") + local target = opt.target + if target and target.sourcekinds and table.contains(table.wrap(target:sourcekinds()), "cxx") then + maps = { + ["c++_static"] = "-stdlib=libc++", + ["c++_shared"] = "-stdlib=libc++", + ["stdc++_static"] = "-stdlib=libstdc++", + ["stdc++_shared"] = "-stdlib=libstdc++", + } + if runtime:endswith("_static") and _has_static_libstdcxx(self) then + maps["c++_static"] = table.join(maps["c++_static"], "-static-libstdc++") + maps["stdc++_static"] = table.join(maps["stdc++_static"], "-static-libstdc++") + end end end return maps and maps[runtime] diff --git a/xmake/rules/c++/xmake.lua b/xmake/rules/c++/xmake.lua index 80d2e1417..dedb63ed5 100644 --- a/xmake/rules/c++/xmake.lua +++ b/xmake/rules/c++/xmake.lua @@ -46,10 +46,35 @@ rule("c++.build") end end) +rule("c") + + -- add build rules + add_deps("c.build") + + -- set compiler runtime, e.g. vs runtime + add_deps("utils.compiler.runtime") + + -- inherit links and linkdirs of all dependent targets by default + add_deps("utils.inherit.links") + + -- support `add_files("src/*.o")` and `add_files("src/*.a")` to merge object and archive files to target + add_deps("utils.merge.object", "utils.merge.archive") + + -- 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.wasm") + add_deps("platform.windows") + + -- add linker rules + add_deps("linker") + rule("c++") -- add build rules - add_deps("c++.build", "c.build") + add_deps("c++.build") -- set compiler runtime, e.g. vs runtime add_deps("utils.compiler.runtime") |
