| 1 |
##===- meson.build - Rules for building clang-extract *- meson ----------*-===## |
| 2 |
## |
| 3 |
## This project is licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
## See https://llvm.org/LICENSE.txt for license information. |
| 5 |
## SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
## |
| 7 |
##===----------------------------------------------------------------------===## |
| 8 |
## |
| 9 |
### \file |
| 10 |
### Rules and scripts for building libcextract. |
| 11 |
## |
| 12 |
##===----------------------------------------------------------------------===## |
| 13 |
# |
| 14 |
# Author: Marcos Paulo de Souza |
| 15 |
|
| 16 |
project('clang-extract', 'cpp', |
| 17 |
version : '0.1', |
| 18 |
default_options : [ |
| 19 |
'cpp_std=c++20', |
| 20 |
'warning_level=3' |
| 21 |
] |
| 22 |
) |
| 23 |
|
| 24 |
add_project_arguments( |
| 25 |
[ |
| 26 |
'-Wno-unused-parameter', |
| 27 |
'-Wno-vla-extension' |
| 28 |
], language: 'cpp' |
| 29 |
) |
| 30 |
|
| 31 |
fs = import('fs') |
| 32 |
|
| 33 |
# For debug builds, we want every debug info we can get plus addrsan. |
| 34 |
buildtype = get_option('buildtype') |
| 35 |
if buildtype == 'debug' or buildtype == 'debugoptimized' |
| 36 |
add_project_arguments('-g3', '-fsanitize=address', language : 'cpp') |
| 37 |
add_project_link_arguments('-fsanitize=address', language : 'cpp') |
| 38 |
endif |
| 39 |
|
| 40 |
# Ensure that we are using clang, and not gcc. |
| 41 |
cpp = meson.get_compiler('cpp') |
| 42 |
compiler_name = cpp.get_id() |
| 43 |
# FIXME: We currently don't support g++, and get_id returns clang for clang++ |
| 44 |
assert(compiler_name == 'clang', compiler_name + ' found but only clang is supported') |
| 45 |
|
| 46 |
# We need gcc at least 7.5.0 |
| 47 |
gcc = find_program('gcc', version : '>=7.5.0') |
| 48 |
|
| 49 |
# We also need to get the system GCC installation folder to make sure we are |
| 50 |
# building with the correct headers, otherwise we run the risk of building |
| 51 |
# clang-extract with different headers than libLLVM |
| 52 |
gcc_install_dir = '' |
| 53 |
build_dir_absolute_path = meson.build_root() |
| 54 |
simple_program_file = configure_file(output: 'simple_program.c', input: 'simple_program.c.in', copy: true ) |
| 55 |
|
| 56 |
gcc_output = run_command(gcc.full_path(), '-fno-PIC', '-v', '-o', build_dir_absolute_path / 'simple_program.out', |
| 57 |
build_dir_absolute_path / 'simple_program.c', check: true).stderr().strip().split('\n') |
| 58 |
foreach line : gcc_output |
| 59 |
if line.contains('crtbegin.o') |
| 60 |
ilines = line.strip().split(' ') |
| 61 |
foreach iline : ilines |
| 62 |
if iline.contains('crtbegin.o') |
| 63 |
gcc_install_dir = fs.parent(iline) |
| 64 |
break |
| 65 |
endif |
| 66 |
endforeach |
| 67 |
endif |
| 68 |
endforeach |
| 69 |
if build_machine.system() == 'linux' |
| 70 |
rm = find_program('rm') |
| 71 |
run_command(rm.full_path(),'-f', build_dir_absolute_path / 'simple_program.out', check: false) |
| 72 |
endif |
| 73 |
|
| 74 |
# Check if we got the gcc installation dir and add it to the project arguments. |
| 75 |
assert(gcc_install_dir != '', 'GCC headers dir not found. Check \'gcc -v\'') |
| 76 |
add_project_arguments('--gcc-install-dir=' + gcc_install_dir, language: 'cpp') |
| 77 |
|
| 78 |
########## Dependency: additional clang includes ################ |
| 79 |
llvm_config = find_program('llvm-config') |
| 80 |
if llvm_config.found() |
| 81 |
extra_args_line = run_command(llvm_config.full_path(), '--cxxflags').stdout().strip().split('\n')[0] |
| 82 |
extra_args = extra_args_line.split(' -') |
| 83 |
i = 0 |
| 84 |
foreach arg : extra_args |
| 85 |
targ = arg |
| 86 |
if i > 0 |
| 87 |
targ = '-' + arg |
| 88 |
endif |
| 89 |
if(not targ.startswith('-I')) |
| 90 |
i += 1 |
| 91 |
continue |
| 92 |
endif |
| 93 |
message('Adding extra compilation flag: ' + targ) |
| 94 |
add_project_arguments(targ, language: 'cpp') |
| 95 |
i += 1 |
| 96 |
endforeach |
| 97 |
endif |
| 98 |
|
| 99 |
########## Dependency: clang libraries ################ |
| 100 |
llvm_libdir = dependency('llvm', version : '>=16').get_variable(cmake : 'LLVM_LIBRARY_DIR', configtool: 'libdir') |
| 101 |
|
| 102 |
clang_dep = [] |
| 103 |
clang_dep += cpp.find_library('clang-cpp', dirs : llvm_libdir) |
| 104 |
clang_dep += cpp.find_library('LLVM', dirs : llvm_libdir) |
| 105 |
############################# ######################### |
| 106 |
elf_dep = dependency('libelf') # libelf |
| 107 |
zlib_dep = dependency('zlib') |
| 108 |
zstd_dep = dependency('libzstd') |
| 109 |
|
| 110 |
subdir('libcextract') |
| 111 |
|
| 112 |
incdir = include_directories('libcextract') |
| 113 |
|
| 114 |
executable('ce-inline', 'Inline.cpp', |
| 115 |
include_directories : incdir, |
| 116 |
install : true, |
| 117 |
link_args : ['--gcc-install-dir=' + gcc_install_dir], |
| 118 |
link_with : libcextract_static, |
| 119 |
dependencies : [elf_dep, zlib_dep, zstd_dep] |
| 120 |
) |
| 121 |
|
| 122 |
executable('ce-includetree', 'TreeDump.cpp', |
| 123 |
include_directories : incdir, |
| 124 |
install : true, |
| 125 |
link_args : ['--gcc-install-dir=' + gcc_install_dir], |
| 126 |
link_with : libcextract_static, |
| 127 |
dependencies : [elf_dep, clang_dep, zlib_dep, zstd_dep] |
| 128 |
) |
| 129 |
|
| 130 |
executable('clang-extract', 'Main.cpp', |
| 131 |
include_directories : incdir, |
| 132 |
install : true, |
| 133 |
link_args : ['--gcc-install-dir=' + gcc_install_dir], |
| 134 |
link_with : libcextract_static, |
| 135 |
dependencies : [elf_dep, clang_dep, zlib_dep, zstd_dep] |
| 136 |
) |
| 137 |
|
| 138 |
######### |
| 139 |
# Tests # |
| 140 |
######### |
| 141 |
|
| 142 |
subdir('testsuite') |