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