Add basic pledge/unveil sandbox on OpenBSD
[tinc] / meson.build
1 project('tinc', 'c',
2   version: run_command([find_program('python3'), 'version.py', 'short'], check: true).stdout(),
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_debug = get_option('debug')
16 opt_docs = get_option('docs')
17 opt_harden = get_option('hardening')
18 opt_jumbograms = get_option('jumbograms')
19 opt_lz4 = get_option('lz4')
20 opt_lzo = get_option('lzo')
21 opt_miniupnpc = get_option('miniupnpc')
22 opt_readline = get_option('readline')
23 opt_sandbox = get_option('sandbox')
24 opt_static = get_option('static')
25 opt_systemd = get_option('systemd')
26 opt_tests = get_option('tests')
27 opt_tunemu = get_option('tunemu')
28 opt_uml = get_option('uml')
29 opt_vde = get_option('vde')
30 opt_zlib = get_option('zlib')
31
32 meson_version = meson.version()
33
34 cc = meson.get_compiler('c')
35 os_name = host_machine.system()
36 cpu_family = host_machine.cpu_family()
37 cc_name = cc.get_id()
38
39 python = find_program('python3')
40 if meson_version.version_compare('>=0.55')
41   python_path = python.full_path()
42 else
43   python_path = python.path()
44 endif
45
46 cc_defs = ['-D_GNU_SOURCE']
47 if os_name == 'sunos'
48   cc_defs += '-D__EXTENSIONS__'
49 endif
50
51 cc_flags = [cc_defs]
52 ld_flags = []
53
54 if cc_name != 'msvc'
55   cc_flags += [
56     '-Qunused-arguments',
57     '-Wbad-function-cast',
58     '-Wduplicated-branches',
59     '-Wduplicated-cond',
60     '-Wformat-overflow=2',
61     '-Wformat-truncation=1', # 2 prints too much noise
62     '-Wformat=2',
63     '-Wlogical-op',
64     '-Wmissing-declarations',
65     '-Wmissing-noreturn',
66     '-Wmissing-prototypes',
67     '-Wno-embedded-directive',
68     '-Wold-style-definition',
69     '-Wredundant-decls',
70     '-Wreturn-type',
71     '-Wstrict-prototypes',
72     '-Wswitch-enum',
73     '-Wtrampolines', # may require executable stack which is disabled
74     '-Wvla', # VLAs are not supported by MSVC
75     '-Wwrite-strings',
76     '-fdiagnostics-show-option',
77     '-fno-strict-overflow',
78     '-fstrict-aliasing',
79   ]
80 endif
81
82 if opt_static.auto()
83   static = os_name == 'windows'
84 else
85   static = opt_static.enabled()
86 endif
87
88 if static and cc_name != 'msvc'
89   ld_flags += '-static'
90 endif
91
92 if opt_harden
93   if cc_name == 'msvc'
94     # Most of these flags are already ON by default in the latest version of MSVC.
95     # Add anyway in case someone is building using an old toolchain.
96     cc_flags += ['/guard:cf', '/GS']
97     ld_flags += [
98       '/guard:cf',
99       '/NXCOMPAT',
100       '/DYNAMICBASE',
101       cpu_family.endswith('64') ? '/HIGHENTROPYVA' : '/SAFESEH',
102     ]
103   else
104     cc_flags += [
105       '-D_FORTIFY_SOURCE=2',
106       '-fcf-protection=full',
107       '-fstack-protector-strong',
108     ]
109     ld_flags += ['-Wl,-z,relro', '-Wl,-z,now', '-Wl,-z,noexecstack']
110     if os_name == 'windows'
111       ld_flags += ['-Wl,--dynamicbase', '-Wl,--nxcompat']
112     else
113       # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90458
114       cc_flags += '-fstack-clash-protection'
115     endif
116   endif
117 endif
118
119 cc_flags = cc.get_supported_arguments(cc_flags)
120 ld_flags = cc.get_supported_link_arguments(ld_flags)
121
122 add_project_arguments(cc_flags, language: 'c')
123 add_project_link_arguments(ld_flags, language: 'c')
124
125 build_root = meson.current_build_dir()
126 src_root = meson.current_source_dir()
127
128 prefix = get_option('prefix')
129 dir_bin = prefix / get_option('bindir')
130 dir_data = prefix / get_option('datadir')
131 dir_info = prefix / get_option('infodir')
132 dir_lib = prefix / get_option('libdir')
133 dir_local_state = prefix / get_option('localstatedir')
134 dir_locale = prefix / get_option('localedir')
135 dir_man = prefix / get_option('mandir')
136 dir_sbin = prefix / get_option('sbindir')
137 dir_sysconf = prefix / get_option('sysconfdir')
138
139 if dir_run_state == ''
140   dir_run_state = dir_local_state / 'run'
141 endif
142
143 if not opt_docs.disabled()
144   subdir('doc')
145 endif
146
147 subdir('src')
148
149 if not opt_tests.disabled()
150   subdir('test')
151 endif
152
153 subdir('bash_completion.d')
154
155 if os_name == 'linux' and not opt_systemd.disabled()
156   subdir('systemd')
157 endif
158
159 run_target('reformat', command: [
160   python,
161   '@SOURCE_ROOT@/lint.py',
162   '--fix',
163 ])
164
165 run_target('lint', command: [
166   python,
167   '@SOURCE_ROOT@/lint.py',
168 ])