Add support for meson build system
[tinc] / meson.build
1 project('tinc', 'c',
2   version: run_command('./src/git_tag.sh', check: true).stdout().strip(),
3   license: 'GPL-2.0-or-later',
4   meson_version: '>=0.51',
5   default_options: [
6     'c_std=c11',
7     'warning_level=3',
8     'buildtype=debugoptimized',
9   ],
10 )
11
12 dir_run_state = get_option('runstatedir')
13 opt_crypto = get_option('crypto')
14 opt_curses = get_option('curses')
15 opt_docs = get_option('docs')
16 opt_harden = get_option('hardening')
17 opt_jumbograms = get_option('jumbograms')
18 opt_lz4 = get_option('lz4')
19 opt_lzo = get_option('lzo')
20 opt_miniupnpc = get_option('miniupnpc')
21 opt_readline = get_option('readline')
22 opt_static = get_option('static')
23 opt_systemd = get_option('systemd')
24 opt_tests = get_option('tests')
25 opt_tunemu = get_option('tunemu')
26 opt_uml = get_option('uml')
27 opt_vde = get_option('vde')
28 opt_zlib = get_option('zlib')
29
30 meson_version = meson.version()
31
32 cc = meson.get_compiler('c')
33 os_name = host_machine.system()
34 cc_name = cc.get_id()
35
36 cc_defs = ['-D_GNU_SOURCE']
37 cc_flags = [cc_defs]
38 ld_flags = []
39
40 if opt_static.auto()
41   static = os_name == 'windows'
42 else
43   static = opt_static.enabled()
44 endif
45
46 if static
47   ld_flags += '-static'
48 endif
49
50 if opt_harden
51   cc_flags += [
52     '-D_FORTIFY_SOURCE=2',
53     '-fwrapv',
54     '-fno-strict-overflow',
55     '-Wreturn-type',
56     '-Wold-style-definition',
57     '-Wmissing-declarations',
58     '-Wmissing-prototypes',
59     '-Wstrict-prototypes',
60     '-Wredundant-decls',
61     '-Wbad-function-cast',
62     '-Wwrite-strings',
63     '-fdiagnostics-show-option',
64     '-fstrict-aliasing',
65     '-Wmissing-noreturn',
66   ]
67   if cc_name == 'clang'
68     cc_flags += '-Qunused-arguments'
69   endif
70   ld_flags += ['-Wl,-z,relro', '-Wl,-z,now']
71   if os_name == 'windows'
72     ld_flags += ['-Wl,--dynamicbase', '-Wl,--nxcompat']
73   endif
74 endif
75
76 cc_flags = cc.get_supported_arguments(cc_flags)
77 ld_flags = cc.get_supported_link_arguments(ld_flags)
78
79 add_project_arguments(cc_flags, language: 'c')
80 add_project_link_arguments(ld_flags, language: 'c')
81
82 build_root = meson.current_build_dir()
83 src_root = meson.current_source_dir()
84
85 prefix = get_option('prefix')
86 dir_bin = prefix / get_option('bindir')
87 dir_data = prefix / get_option('datadir')
88 dir_info = prefix / get_option('infodir')
89 dir_lib = prefix / get_option('libdir')
90 dir_local_state = prefix / get_option('localstatedir')
91 dir_locale = prefix / get_option('localedir')
92 dir_man = prefix / get_option('mandir')
93 dir_sbin = prefix / get_option('sbindir')
94 dir_sysconf = prefix / get_option('sysconfdir')
95
96 if dir_run_state == ''
97   dir_run_state = dir_local_state / 'run'
98 endif
99
100 if not opt_docs.disabled()
101   subdir('doc')
102 endif
103
104 subdir('src')
105
106 if not opt_tests.disabled()
107   subdir('test')
108 endif
109
110 subdir('bash_completion.d')
111
112 if os_name == 'linux' and not opt_systemd.disabled()
113   subdir('systemd')
114 endif
115
116 prog_reformat = find_program('astyle', native: true, required: false)
117 if prog_reformat.found()
118   run_target('reformat', command: [
119     prog_reformat,
120     '--options=@SOURCE_ROOT@/.astylerc',
121     '@SOURCE_ROOT@/*.c', '@SOURCE_ROOT@/*.h',
122   ])
123 endif
124