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