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