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