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