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