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