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