Wipe (some) secrets from memory after use
[tinc] / src / meson.build
1 inc_conf = include_directories('include')
2
3 cdata = configuration_data()
4
5 cdata.set_quoted('PACKAGE', meson.project_name())
6 cdata.set_quoted('VERSION', meson.project_version())
7 cdata.set_quoted('CONFDIR', dir_sysconf)
8 cdata.set_quoted('RUNSTATEDIR', dir_run_state)
9 cdata.set_quoted('LOCALSTATEDIR', dir_local_state)
10 cdata.set_quoted('SBINDIR', dir_sbin)
11
12 cdata.set('HAVE_' + os_name.to_upper(), 1)
13
14 foreach attr : ['malloc', 'nonnull', 'warn_unused_result', 'packed', 'format']
15   if cc.has_function_attribute(attr)
16     cdata.set('HAVE_ATTR_' + attr.to_upper(), 1,
17               description: '__attribute__((__@0@__))'.format(attr))
18   endif
19 endforeach
20
21 if cc.compiles('''
22     _Static_assert(1, "ok");
23     int main(void) { return 0; }
24 ''')
25   cdata.set('HAVE_STATIC_ASSERT', 1,
26             description: 'C11 _Static_assert()')
27 endif
28
29 check_headers = [
30   'alloca.h',
31   'arpa/inet.h',
32   'arpa/nameser.h',
33   'dirent.h',
34   'getopt.h',
35   'inttypes.h',
36   'net/ethernet.h',
37   'net/if.h',
38   'net/if_arp.h',
39   'net/if_types.h',
40   'netdb.h',
41   'netinet/icmp6.h',
42   'netinet/if_ether.h',
43   'netinet/in.h',
44   'netinet/in6.h',
45   'netinet/in_systm.h',
46   'netinet/ip.h',
47   'netinet/ip6.h',
48   'netinet/ip_icmp.h',
49   'netinet/tcp.h',
50   'resolv.h',
51   'stddef.h',
52   'sys/file.h',
53   'sys/ioctl.h',
54   'sys/mman.h',
55   'sys/param.h',
56   'sys/resource.h',
57   'sys/socket.h',
58   'sys/stat.h',
59   'sys/time.h',
60   'sys/types.h',
61   'sys/wait.h',
62   'syslog.h',
63   'string.h',
64   'termios.h',
65   'unistd.h',
66 ]
67
68 # 'struct msghdr' misses some required fields
69 if os_name != 'sunos'
70   check_headers += 'sys/un.h'
71 endif
72
73 check_functions = [
74   'asprintf',
75   'daemon',
76   'explicit_bzero',
77   'explicit_memset',
78   'fchmod',
79   'gettimeofday',
80   'memset_s',
81   'mlockall',
82   'putenv',
83   'strsignal',
84   'unsetenv',
85 ]
86
87 # Broken definition, fails to link
88 if os_name != 'windows'
89   check_functions += 'fork'
90 endif
91
92 check_types = [
93   'struct arphdr',
94   'struct ether_arp',
95   'struct ether_header',
96   'struct icmp',
97   'struct icmp6_hdr',
98   'struct ip',
99   'struct ip6_hdr',
100   'struct nd_neighbor_solicit',
101   'struct nd_opt_hdr',
102 ]
103
104 subdir('ed25519')
105 subdir('chacha-poly1305')
106
107 src_lib_common = [
108   'conf.c',
109   'dropin.c',
110   'keys.c',
111   'list.c',
112   'logger.c',
113   'names.c',
114   'netutl.c',
115   'pidfile.c',
116   'script.c',
117   'splay_tree.c',
118   'sptps.c',
119   'subnet_parse.c',
120   'utils.c',
121   'version.c',
122   'xoshiro.c',
123 ]
124
125 src_tinc = [
126   'fsck.c',
127   'ifconfig.c',
128   'info.c',
129   'invitation.c',
130   'top.c',
131 ]
132
133 src_tincd = [
134   'address_cache.c',
135   'autoconnect.c',
136   'buffer.c',
137   'compression.h',
138   'conf_net.c',
139   'connection.c',
140   'control.c',
141   'dummy_device.c',
142   'edge.c',
143   'event.c',
144   'graph.c',
145   'meta.c',
146   'multicast_device.c',
147   'net.c',
148   'net_packet.c',
149   'net_setup.c',
150   'net_socket.c',
151   'node.c',
152   'process.c',
153   'protocol.c',
154   'protocol_auth.c',
155   'protocol_edge.c',
156   'protocol_key.c',
157   'protocol_misc.c',
158   'protocol_subnet.c',
159   'raw_socket_device.c',
160   'route.c',
161   'subnet.c',
162 ]
163
164 cc_flags_tincd = cc_flags
165
166 deps_common = []
167 deps_tinc = []
168 deps_tincd = [cc.find_library('m', required: false)]
169
170 if os_name != 'windows'
171   src_lib_common += 'random.c'
172 endif
173
174 if os_name in ['linux', 'android']
175   subdir('linux')
176 elif os_name.endswith('bsd') or os_name in ['dragonfly', 'darwin']
177   subdir('bsd')
178 elif os_name == 'sunos'
179   subdir('solaris')
180 elif os_name == 'windows'
181   subdir('windows')
182 endif
183
184 foreach h : check_headers
185   if cc.has_header(h)
186     cdata.set('HAVE_' + h.to_upper().underscorify(),
187               1,
188               description: '#include <' + h + '>')
189   endif
190 endforeach
191
192 if cdata.has('HAVE_SYS_UN_H')
193   src_tincd += 'fd_device.c'
194 endif
195
196 confdata = configuration_data()
197 confdata.merge_from(cdata)
198 configure_file(output: 'meson_config.h', configuration: confdata)
199
200 have_prefix = '''
201   #include "@0@/src/meson_config.h"
202   #include "@1@/have.h"
203 '''.format(build_root, meson.current_source_dir())
204
205 foreach f : check_functions
206   if cc.has_function(f, prefix: have_prefix, args: cc_defs)
207     cdata.set('HAVE_' + f.to_upper(),
208               1,
209               description: 'function ' + f)
210   endif
211 endforeach
212
213 if cc.has_function('res_init', prefix: '''
214   #include <netinet/in.h>
215   #include <resolv.h>
216 ''', args: cc_defs)
217   cdata.set('HAVE_DECL_RES_INIT', 1)
218 endif
219
220 foreach type : check_types
221   if cc.has_type(type, prefix: have_prefix, args: cc_defs)
222     name = 'HAVE_' + type.to_upper().underscorify()
223     cdata.set(name, 1, description: type)
224   endif
225 endforeach
226
227 src_getopt = []
228 if not cdata.has('HAVE_GETOPT_H') or not cc.has_function('getopt_long', prefix: have_prefix, args: cc_defs)
229   src_getopt = ['getopt.c', 'getopt1.c']
230   src_lib_common += src_getopt
231 endif
232
233 if not opt_miniupnpc.disabled()
234   dep_miniupnpc = dependency('miniupnpc', required: false, static: static)
235   if not dep_miniupnpc.found()
236     # No pkg-config files on MinGW
237     dep_miniupnpc = cc.find_library('miniupnpc', required: opt_miniupnpc, static: static)
238   endif
239   if dep_miniupnpc.found()
240     src_tincd += 'upnp.c'
241     deps_tincd += [
242       dependency('threads', static: static),
243       dep_miniupnpc,
244     ]
245     if static
246       cc_flags_tincd += '-DMINIUPNP_STATICLIB'
247     endif
248     cdata.set('HAVE_MINIUPNPC', 1)
249   endif
250 endif
251
252 if not opt_curses.disabled()
253   # The meta-dependency covers more alternatives, but is only available in 0.54+
254   curses_name = meson_version.version_compare('>=0.54') ? 'curses' : 'ncurses'
255   dep_curses = dependency(curses_name, required: opt_curses, static: static)
256   if dep_curses.found()
257     cdata.set('HAVE_CURSES', 1)
258     deps_tinc += dep_curses
259   endif
260 endif
261
262 # Some distributions do not supply pkg-config files for readline
263 if opt_readline.auto() and os_name == 'windows'
264   message('readline not available on Windows')
265 else
266   dep_readline = dependency('readline', required: opt_readline, static: static)
267   if not dep_readline.found()
268     dep_readline = cc.find_library('readline', required: opt_readline, static: static)
269   endif
270   if dep_readline.found() and \
271      cc.has_header('readline/readline.h', dependencies: dep_readline) and \
272      cc.has_header('readline/history.h', dependencies: dep_readline)
273     cdata.set('HAVE_READLINE', 1)
274     deps_tinc += dep_readline
275   endif
276 endif
277
278 dep_zlib = dependency('zlib',
279                       required: opt_zlib,
280                       static: static,
281                       fallback: ['zlib', 'zlib_dep'])
282 if dep_zlib.found()
283   cdata.set('HAVE_ZLIB', 1)
284   deps_tincd += dep_zlib
285 endif
286
287 if not opt_lzo.disabled()
288   dep_lzo = dependency('lzo2', required: false, static: static)
289   if not dep_lzo.found()
290     dep_lzo = cc.find_library('lzo2', required: opt_lzo, static: static)
291   endif
292   if not dep_lzo.found()
293     dep_lzo = dependency('lzo2',
294                          required: false,
295                          static: static,
296                          fallback: ['lzo2', 'lzo2_dep'])
297   endif
298   if dep_lzo.found()
299     if dep_lzo.type_name() == 'internal' or cc.has_header('lzo/lzo1x.h', dependencies: dep_lzo)
300       cdata.set('LZO1X_H', '<lzo/lzo1x.h>')
301     elif cc.has_header('lzo1x.h', dependencies: dep_lzo)
302       cdata.set('LZO1X_H', '<lzo1x.h>')
303     else
304       msg = 'lzo1x.h not found'
305       if opt_lzo.auto()
306         warning(msg)
307       else
308         error(msg)
309       endif
310     endif
311     if cdata.has('LZO1X_H')
312       cdata.set('HAVE_LZO', 1)
313       deps_tincd += dep_lzo
314     endif
315   endif
316 endif
317
318 dep_lz4 = dependency('liblz4',
319                      required: opt_lz4,
320                      static: static,
321                      fallback: ['lz4', 'liblz4_dep'])
322 if dep_lz4.found()
323   deps_tincd += dep_lz4
324   cdata.set('HAVE_LZ4', 1)
325 endif
326
327 dep_vde = dependency('vdeplug', required: opt_vde, static: static)
328 dep_dl = cc.find_library('dl', required: opt_vde)
329 if dep_vde.found() and dep_dl.found()
330   cdata.set('ENABLE_VDE', 1)
331   src_tincd += 'vde_device.c'
332   deps_tincd += [dep_dl, dep_vde]
333 endif
334
335 if opt_jumbograms
336   cdata.set('ENABLE_JUMBOGRAMS', 1)
337 endif
338
339 subdir(opt_crypto)
340
341 if opt_crypto != 'openssl'
342   src_lib_crypto += 'crypto.c'
343 endif
344
345 if opt_crypto != 'nolegacy'
346   src_lib_crypto += ['cipher.c', 'digest.c']
347 endif
348
349 subdir('include')
350
351 lib_crypto = static_library(
352   'tinc_crypto',
353   sources: src_lib_crypto,
354   dependencies: dep_crypto,
355   implicit_include_directories: false,
356   include_directories: inc_conf,
357   build_by_default: false,
358 )
359
360 deps_lib_common = [deps_common, dep_crypto]
361 deps_tinc += deps_lib_common
362 deps_tincd += deps_lib_common
363
364 lib_common = static_library(
365   'common',
366   sources: src_lib_common,
367   dependencies: deps_lib_common,
368   link_with: [lib_ed25519, lib_chacha_poly, lib_crypto],
369   implicit_include_directories: false,
370   include_directories: inc_conf,
371   build_by_default: false,
372 )
373
374 lib_tinc = static_library(
375   'tinc',
376   sources: src_tinc,
377   dependencies: deps_tinc,
378   link_with: lib_common,
379   implicit_include_directories: false,
380   include_directories: inc_conf,
381   build_by_default: false,
382 )
383
384 lib_tincd = static_library(
385   'tincd',
386   sources: src_tincd,
387   dependencies: deps_tincd,
388   link_with: lib_common,
389   c_args: cc_flags_tincd,
390   implicit_include_directories: false,
391   include_directories: inc_conf,
392   build_by_default: false,
393 )
394
395 exe_tinc = executable(
396   'tinc',
397   sources: 'tincctl.c',
398   dependencies: deps_tinc,
399   link_with: lib_tinc,
400   implicit_include_directories: false,
401   include_directories: inc_conf,
402   install: true,
403   install_dir: dir_sbin,
404 )
405
406 exe_tincd = executable(
407   'tincd',
408   sources: 'tincd.c',
409   dependencies: deps_tincd,
410   link_with: lib_tincd,
411   c_args: cc_flags_tincd,
412   implicit_include_directories: false,
413   include_directories: inc_conf,
414   install: true,
415   install_dir: dir_sbin,
416 )
417
418 exe_sptps_test = executable(
419   'sptps_test',
420   sources: [src_getopt, 'sptps_test.c'],
421   dependencies: deps_lib_common,
422   link_with: lib_common,
423   implicit_include_directories: false,
424   include_directories: inc_conf,
425   build_by_default: false,
426 )
427
428 exe_sptps_keypair = executable(
429   'sptps_keypair',
430   sources: [src_getopt, 'sptps_keypair.c'],
431   dependencies: deps_lib_common,
432   link_with: lib_common,
433   implicit_include_directories: false,
434   include_directories: inc_conf,
435   build_by_default: false,
436 )
437
438 if os_name == 'linux'
439   dep_rt = cc.find_library('rt')
440
441   exe_sptps_speed = executable(
442     'sptps_speed',
443     sources: 'sptps_speed.c',
444     dependencies: [deps_lib_common, dep_rt],
445     link_with: lib_common,
446     implicit_include_directories: false,
447     include_directories: inc_conf,
448     build_by_default: false,
449   )
450
451   benchmark('sptps_speed', exe_sptps_speed, timeout: 90)
452 endif
453