diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-29 04:41:38 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-29 04:41:38 +0000 |
commit | 7b6e527f440cd7e6f8be2b07cee320ee6ca18786 (patch) | |
tree | 4a2738d69fa2814659fdadddf5826282e73d81f4 /test cases/frameworks/15 llvm | |
parent | Initial commit. (diff) | |
download | meson-upstream.tar.xz meson-upstream.zip |
Adding upstream version 1.0.1.upstream/1.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test cases/frameworks/15 llvm')
-rw-r--r-- | test cases/frameworks/15 llvm/meson.build | 51 | ||||
-rw-r--r-- | test cases/frameworks/15 llvm/meson_options.txt | 10 | ||||
-rw-r--r-- | test cases/frameworks/15 llvm/sum.c | 76 | ||||
-rw-r--r-- | test cases/frameworks/15 llvm/test.json | 18 |
4 files changed, 155 insertions, 0 deletions
diff --git a/test cases/frameworks/15 llvm/meson.build b/test cases/frameworks/15 llvm/meson.build new file mode 100644 index 0000000..3855fae --- /dev/null +++ b/test cases/frameworks/15 llvm/meson.build @@ -0,0 +1,51 @@ +project('llvmtest', ['c', 'cpp'], default_options : ['c_std=c99']) + +method = get_option('method') +static = get_option('link-static') +d = dependency('llvm', required : false, method : method, static : static) +if not d.found() + error('MESON_SKIP_TEST llvm not found.') +endif + +d = dependency('llvm', modules : 'not-found', required : false, static : static, method : method) +assert(d.found() == false, 'not-found llvm module found') + +d = dependency('llvm', version : '<0.1', required : false, static : static, method : method) +assert(d.found() == false, 'ancient llvm module found') + +d = dependency('llvm', optional_modules : 'not-found', required : false, static : static, method : method) +assert(d.found() == true, 'optional module stopped llvm from being found.') + +# Check we can apply a version constraint +d = dependency('llvm', version : ['< 500', '>=@0@'.format(d.version())], required: false, static : static, method : method) +assert(d.found() == true, 'Cannot set version constraints') + +dep_tinfo = dependency('tinfo', required : false) +if not dep_tinfo.found() + cpp = meson.get_compiler('cpp') + dep_tinfo = cpp.find_library('tinfo', required: false) +endif + +llvm_dep = dependency( + 'llvm', + modules : ['bitwriter', 'asmprinter', 'executionengine', 'target', + 'mcjit', 'nativecodegen', 'amdgpu'], + required : false, + static : static, + method : method, +) + +if not llvm_dep.found() + error('MESON_SKIP_TEST required llvm modules not found.') +endif + +executable( + 'sum', + 'sum.c', + dependencies : [ + llvm_dep, dep_tinfo, + # zlib will be statically linked on windows + dependency('zlib', required : host_machine.system() != 'windows'), + meson.get_compiler('c').find_library('dl', required : false), + ] + ) diff --git a/test cases/frameworks/15 llvm/meson_options.txt b/test cases/frameworks/15 llvm/meson_options.txt new file mode 100644 index 0000000..de3d172 --- /dev/null +++ b/test cases/frameworks/15 llvm/meson_options.txt @@ -0,0 +1,10 @@ +option( + 'method', + type : 'combo', + choices : ['config-tool', 'cmake'] +) +option( + 'link-static', + type : 'boolean', + value : false, +) diff --git a/test cases/frameworks/15 llvm/sum.c b/test cases/frameworks/15 llvm/sum.c new file mode 100644 index 0000000..e35fe95 --- /dev/null +++ b/test cases/frameworks/15 llvm/sum.c @@ -0,0 +1,76 @@ +/** This code is public domain, and taken from + * https://github.com/paulsmith/getting-started-llvm-c-api/blob/master/sum.c + */ +/** + * LLVM equivalent of: + * + * int sum(int a, int b) { + * return a + b; + * } + */ + +#include <llvm-c/Core.h> +#include <llvm-c/ExecutionEngine.h> +#include <llvm-c/Target.h> +#include <llvm-c/Analysis.h> +#include <llvm-c/BitWriter.h> + +#include <inttypes.h> +#include <stdio.h> +#include <stdlib.h> + +int main(int argc, char const *argv[]) { + LLVMModuleRef mod = LLVMModuleCreateWithName("my_module"); + + LLVMTypeRef param_types[] = { LLVMInt32Type(), LLVMInt32Type() }; + LLVMTypeRef ret_type = LLVMFunctionType(LLVMInt32Type(), param_types, 2, 0); + LLVMValueRef sum = LLVMAddFunction(mod, "sum", ret_type); + + LLVMBasicBlockRef entry = LLVMAppendBasicBlock(sum, "entry"); + + LLVMBuilderRef builder = LLVMCreateBuilder(); + LLVMPositionBuilderAtEnd(builder, entry); + LLVMValueRef tmp = LLVMBuildAdd(builder, LLVMGetParam(sum, 0), LLVMGetParam(sum, 1), "tmp"); + LLVMBuildRet(builder, tmp); + + char *error = NULL; + LLVMVerifyModule(mod, LLVMAbortProcessAction, &error); + LLVMDisposeMessage(error); + + LLVMExecutionEngineRef engine; + error = NULL; + LLVMLinkInMCJIT(); + LLVMInitializeNativeAsmPrinter(); + LLVMInitializeNativeTarget(); + if (LLVMCreateExecutionEngineForModule(&engine, mod, &error) != 0) { + fprintf(stderr, "failed to create execution engine\n"); + abort(); + } + if (error) { + fprintf(stderr, "error: %s\n", error); + LLVMDisposeMessage(error); + exit(EXIT_FAILURE); + } + + if (argc < 3) { + fprintf(stderr, "usage: %s x y\n", argv[0]); + exit(EXIT_FAILURE); + } + long long x = strtoll(argv[1], NULL, 10); + long long y = strtoll(argv[2], NULL, 10); + + LLVMGenericValueRef args[] = { + LLVMCreateGenericValueOfInt(LLVMInt32Type(), x, 0), + LLVMCreateGenericValueOfInt(LLVMInt32Type(), y, 0) + }; + LLVMGenericValueRef res = LLVMRunFunction(engine, sum, 2, args); + printf("%d\n", (int)LLVMGenericValueToInt(res, 0)); + + // Write out bitcode to file + if (LLVMWriteBitcodeToFile(mod, "sum.bc") != 0) { + fprintf(stderr, "error writing bitcode to file, skipping\n"); + } + + LLVMDisposeBuilder(builder); + LLVMDisposeExecutionEngine(engine); +} diff --git a/test cases/frameworks/15 llvm/test.json b/test cases/frameworks/15 llvm/test.json new file mode 100644 index 0000000..e70edd5 --- /dev/null +++ b/test cases/frameworks/15 llvm/test.json @@ -0,0 +1,18 @@ +{ + "matrix": { + "options": { + "method": [ + { "val": "config-tool", "skip_on_jobname": ["msys2-gcc"]}, + { "val": "cmake", "skip_on_jobname": ["msys2-gcc"] } + ], + "link-static": [ + { "val": true, "skip_on_jobname": ["opensuse"] }, + { "val": false } + ] + }, + "exclude": [ + { "method": "cmake", "link-static": false } + ] + }, + "skip_on_jobname": ["azure", "cygwin"] +} |