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