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