Add support for building object files from LLVM assembly sources - close #175

This commit is contained in:
Josh Holtrop 2024-03-23 22:24:45 -04:00
parent 6e7139abb1
commit ba71e8d5e3
10 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,4 @@
env do |env|
env["LLVMAS_FLAGS"] += %w[-Wno-override-module]
env.Program("llvmtest.exe", %w[main.c one.ll])
end

View File

@ -0,0 +1,8 @@
configure do
check_c_compiler "clang"
end
env do |env|
env["LLVMAS_FLAGS"] += %w[-Wno-override-module]
env.Program("llvmtest.exe", %w[one.ll two.ll main2.c], direct: true)
end

6
build_tests/llvm/main.c Normal file
View File

@ -0,0 +1,6 @@
extern int one(void);
int main(int argc, char * argv[])
{
return one();
}

9
build_tests/llvm/main2.c Normal file
View File

@ -0,0 +1,9 @@
extern int one(void);
extern int two(void);
int main(int argc, char * argv[])
{
one();
two();
return 0;
}

9
build_tests/llvm/one.ll Normal file
View File

@ -0,0 +1,9 @@
@str = private unnamed_addr constant [12 x i8] c"hello world\00"
declare i32 @puts(ptr nocapture) nounwind
define i32 @one()
{
call i32 @puts(ptr @str)
ret i32 0
}

9
build_tests/llvm/two.ll Normal file
View File

@ -0,0 +1,9 @@
@str = private unnamed_addr constant [12 x i8] c"hello again\00"
declare i32 @puts(ptr nocapture) nounwind
define i32 @two()
{
call i32 @puts(ptr @str)
ret i32 0
}

View File

@ -156,6 +156,7 @@ require_relative "rscons/builders/lang/asm"
require_relative "rscons/builders/lang/c" require_relative "rscons/builders/lang/c"
require_relative "rscons/builders/lang/cxx" require_relative "rscons/builders/lang/cxx"
require_relative "rscons/builders/lang/d" require_relative "rscons/builders/lang/d"
require_relative "rscons/builders/lang/llvm_asm"
# Unbuffer $stdout # Unbuffer $stdout
$stdout.sync = true $stdout.sync = true

View File

@ -0,0 +1,10 @@
Rscons::Builders::Object.register(
command: "${LLVMAS_CMD}",
direct_command: "${LLVMAS_CMD:direct}",
suffix: "${LLVMAS_SUFFIX}",
short_description: "Assembling")
Rscons::Builders::SharedObject.register(
command: "${LLVMAS_CMD}",
direct_command: "${LLVMAS_CMD:direct}",
suffix: "${LLVMAS_SUFFIX}",
short_description: "Assembling")

View File

@ -58,6 +58,11 @@ module Rscons
"LIBPATH" => [], "LIBPATH" => [],
"LIBS" => [], "LIBS" => [],
"LIBSUFFIX" => ".a", "LIBSUFFIX" => ".a",
"LLVMAS" => "clang",
"LLVMAS_CMD" => %w[${LLVMAS} -c -o ${_TARGET} ${LLVMAS_FLAGS} ${_SOURCES}],
"LLVMAS_CMD:direct" => %w[${LLVMAS} -o ${_TARGET} ${LLVMAS_FLAGS} ${LDFLAGS} ${_SOURCES} ${LIBDIRPREFIX}${LIBPATH} ${LIBLINKPREFIX}${LIBS}],
"LLVMAS_FLAGS" => [],
"LLVMAS_SUFFIX" => ".ll",
"OBJDUMP" => "objdump", "OBJDUMP" => "objdump",
"OBJSUFFIX" => %w[.o], "OBJSUFFIX" => %w[.o],
"PROGSUFFIX" => on_windows ? ".exe" : "", "PROGSUFFIX" => on_windows ? ".exe" : "",

View File

@ -3408,4 +3408,22 @@ EOF
end end
end end
it "supports building LLVM assembly files with the Program builder" do
test_dir "llvm"
result = run_rscons
expect(result.stderr).to eq ""
expect(result.status).to eq 0
expect(File.exist?("llvmtest.exe")).to be_truthy
expect(`./llvmtest.exe`).to match /hello world/
end
it "supports building LLVM assembly files with the Program builder in direct mode" do
test_dir "llvm"
result = run_rscons(args: %w[-f direct.rb])
expect(result.stderr).to eq ""
expect(result.status).to eq 0
expect(File.exist?("llvmtest.exe")).to be_truthy
expect(`./llvmtest.exe`).to match /hello again/
end
end end