summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSaikari <[email protected]>2026-04-03 00:02:53 +0300
committerSaikari <[email protected]>2026-04-03 00:02:53 +0300
commit616e2fb1b343f0a0613924edf857dde3a8c88c5f (patch)
treeed6a4e20573a50b3661122981cf82c11b1f02060
parent39fd35ea9d5d14079e4669b85423d488c9097cd4 (diff)
Fil-C add toolchain
-rw-r--r--tests/projects/c/filc/safety_test/src/main.c19
-rw-r--r--tests/projects/c/filc/safety_test/test.lua46
-rw-r--r--tests/projects/c/filc/safety_test/xmake.lua9
-rw-r--r--xmake/modules/core/tools/filcc.lua21
-rw-r--r--xmake/modules/detect/tools/find_filcc.lua41
-rw-r--r--xmake/toolchains/filc/xmake.lua94
6 files changed, 230 insertions, 0 deletions
diff --git a/tests/projects/c/filc/safety_test/src/main.c b/tests/projects/c/filc/safety_test/src/main.c
new file mode 100644
index 000000000..4f5ad91b9
--- /dev/null
+++ b/tests/projects/c/filc/safety_test/src/main.c
@@ -0,0 +1,19 @@
+/* Test that filc catches calling a function pointer with a wrong address.
+ *
+ * Expected runtime output (exit non-zero):
+ * filc safety error: cannot access pointer as function with ptr != aux ...
+ * filc panic: thwarted a futile attempt to violate memory safety.
+ */
+#include <stdfil.h>
+#include <stdlib.h>
+
+static void foo(void)
+{
+}
+
+int main()
+{
+ void (*my_foo)(void) = (void(*)(void))((char*)foo + 42);
+ my_foo();
+ return 0;
+}
diff --git a/tests/projects/c/filc/safety_test/test.lua b/tests/projects/c/filc/safety_test/test.lua
new file mode 100644
index 000000000..34d462e7b
--- /dev/null
+++ b/tests/projects/c/filc/safety_test/test.lua
@@ -0,0 +1,46 @@
+import("lib.detect.find_tool")
+import("utils.ci.is_running", {alias = "ci_is_running"})
+
+function main(t)
+ -- filc only supports Linux x86_64
+ if os.host() ~= "linux" or os.arch() ~= "x86_64" then
+ return t:skip("filc is only supported on linux/x86_64")
+ end
+
+ local filcc = find_tool("filcc")
+ if not filcc then
+ return t:skip("filcc not found, please install the filc package first")
+ end
+
+ -- build the safety test
+ local flags = ci_is_running() and "-vD" or ""
+ os.exec("xmake f -c -y " .. flags)
+ os.exec("xmake -r " .. flags)
+
+ -- find the compiled binary
+ local binfile = path.join("build", os.host(), os.arch(), "release", "safety_test")
+ assert(os.isfile(binfile), "binary not found: " .. binfile)
+
+ -- run the binary — it MUST exit non-zero and emit a filc safety error
+ -- os.iorunv raises on non-zero exit in the sandbox, so catch the failure
+ local filc_output = nil
+ try
+ {
+ function ()
+ os.iorunv(binfile, {})
+ end,
+ catch
+ {
+ function (errors)
+ filc_output = tostring(errors)
+ end
+ }
+ }
+ assert(filc_output, "expected safety_test to exit non-zero, but it succeeded")
+ assert(filc_output:find("filc safety error", 1, true),
+ "expected 'filc safety error' in output, got:\n" .. filc_output)
+ assert(filc_output:find("filc panic", 1, true),
+ "expected 'filc panic' in output, got:\n" .. filc_output)
+
+ cprint("${green}filc safety check triggered as expected${reset}")
+end
diff --git a/tests/projects/c/filc/safety_test/xmake.lua b/tests/projects/c/filc/safety_test/xmake.lua
new file mode 100644
index 000000000..e2d47da57
--- /dev/null
+++ b/tests/projects/c/filc/safety_test/xmake.lua
@@ -0,0 +1,9 @@
+add_rules("mode.debug", "mode.release")
+
+add_requires("filc")
+
+target("safety_test")
+ set_kind("binary")
+ set_toolchains("filc", {packages = "filc"})
+ add_packages("filc")
+ add_files("src/*.c")
diff --git a/xmake/modules/core/tools/filcc.lua b/xmake/modules/core/tools/filcc.lua
new file mode 100644
index 000000000..28aefc34a
--- /dev/null
+++ b/xmake/modules/core/tools/filcc.lua
@@ -0,0 +1,21 @@
+--!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, Xmake Open Source Community.
+--
+-- @file filcc.lua
+--
+
+-- filcc is Fil-C's clang-based C compiler - inherit clang driver behaviour
+inherit("clang")
diff --git a/xmake/modules/detect/tools/find_filcc.lua b/xmake/modules/detect/tools/find_filcc.lua
new file mode 100644
index 000000000..2f6c96fcc
--- /dev/null
+++ b/xmake/modules/detect/tools/find_filcc.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, Xmake Open Source Community.
+--
+-- @file find_filcc.lua
+--
+
+import("lib.detect.find_program")
+import("lib.detect.find_programver")
+
+-- find filcc (Fil-C memory-safe C compiler, clang-based)
+--
+-- @param opt the argument options, e.g. {version = true}
+--
+-- @return program, version
+--
+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 "filcc", opt)
+
+ local version = nil
+ if program and opt.version then
+ version = find_programver(program, opt)
+ end
+ return program, version
+end
diff --git a/xmake/toolchains/filc/xmake.lua b/xmake/toolchains/filc/xmake.lua
new file mode 100644
index 000000000..e540aaab3
--- /dev/null
+++ b/xmake/toolchains/filc/xmake.lua
@@ -0,0 +1,94 @@
+--!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, Xmake Open Source Community.
+--
+-- @file xmake.lua
+--
+
+toolchain("filc")
+ set_kind("standalone")
+ set_homepage("https://fil-c.org/")
+ set_description("A memory safe implementation of the C and C++ programming languages.")
+
+ on_check(function (toolchain)
+ import("lib.detect.find_tool")
+
+ -- look in package installdir/build/bin first, then bindir/sdkdir, then PATH
+ local paths = {}
+ for _, package in ipairs(toolchain:packages()) do
+ local installdir = package:installdir()
+ if installdir then
+ table.insert(paths, path.join(installdir, "build", "bin"))
+ end
+ end
+ local bindir = toolchain:bindir()
+ if bindir then
+ table.insert(paths, bindir)
+ end
+ local sdkdir = toolchain:sdkdir()
+ if sdkdir then
+ table.insert(paths, path.join(sdkdir, "build", "bin"))
+ table.insert(paths, path.join(sdkdir, "bin"))
+ end
+
+ local filcc = find_tool("filcc", {paths = paths, force = true})
+ if filcc then
+ toolchain:config_set("filcc", filcc.program)
+ if os.isfile(filcc.program) then
+ toolchain:config_set("bindir", path.directory(filcc.program))
+ end
+ return true
+ end
+ end)
+
+ on_load(function (toolchain)
+ local filcc = toolchain:config("filcc") or "filcc"
+
+ -- filcc is the compiler and linker; ar uses the system archiver
+ toolchain:set("toolset", "cc", filcc)
+ toolchain:set("toolset", "ld", filcc)
+ toolchain:set("toolset", "sh", filcc)
+ toolchain:set("toolset", "ar", "ar")
+ toolchain:set("toolset", "as", filcc)
+
+ -- add runtime include/lib dirs from the installed package
+ for _, package in ipairs(toolchain:packages()) do
+ local installdir = package:installdir()
+ if installdir then
+ local pizfix = path.join(installdir, "pizfix")
+ -- stdfil.h lives in pizfix/stdfil-include/
+ toolchain:add("sysincludedirs", path.join(pizfix, "stdfil-include"))
+ -- standard C headers (musl-based) live in pizfix/include/
+ toolchain:add("sysincludedirs", path.join(pizfix, "include"))
+ toolchain:add("linkdirs", path.join(pizfix, "lib"))
+ toolchain:add("linkdirs", path.join(pizfix, "lib64"))
+ end
+ end
+ local sdkdir = toolchain:sdkdir()
+ if sdkdir and os.isdir(sdkdir) then
+ local pizfix = path.join(sdkdir, "pizfix")
+ for _, d in ipairs({"stdfil-include", "include"}) do
+ local includedir = path.join(pizfix, d)
+ if os.isdir(includedir) then
+ toolchain:add("sysincludedirs", includedir)
+ end
+ end
+ for _, libdir in ipairs({path.join(pizfix, "lib64"), path.join(pizfix, "lib")}) do
+ if os.isdir(libdir) then
+ toolchain:add("linkdirs", libdir)
+ end
+ end
+ end
+ end)