Disable function attributes on unsupported compilers
[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 if not cdata.has('HAVE_GETOPT_H') or not cc.has_function('getopt_long', prefix: have_prefix, args: cc_defs)
220   src_lib_common += ['getopt.c', 'getopt1.c']
221 endif
222
223 if not opt_miniupnpc.disabled()
224   dep_miniupnpc = dependency('miniupnpc', required: false, static: static)
225   if not dep_miniupnpc.found()
226     # No pkg-config files on MinGW
227     dep_miniupnpc = cc.find_library('miniupnpc', required: opt_miniupnpc, static: static)
228   endif
229   if dep_miniupnpc.found()
230     src_tincd += 'upnp.c'
231     deps_tincd += [
232       dependency('threads', static: static),
233       dep_miniupnpc,
234     ]
235     if static
236       cc_flags_tincd += '-DMINIUPNP_STATICLIB'
237     endif
238     cdata.set('HAVE_MINIUPNPC', 1)
239   endif
240 endif
241
242 if not opt_curses.disabled()
243   # The meta-dependency covers more alternatives, but is only available in 0.54+
244   curses_name = meson_version.version_compare('>=0.54') ? 'curses' : 'ncurses'
245   dep_curses = dependency(curses_name, required: opt_curses, static: static)
246   if dep_curses.found()
247     cdata.set('HAVE_CURSES', 1)
248     deps_tinc += dep_curses
249   endif
250 endif
251
252 # Some distributions do not supply pkg-config files for readline
253 if opt_readline.auto() and os_name == 'windows'
254   message('readline not available on Windows')
255 else
256   dep_readline = dependency('readline', required: opt_readline, static: static)
257   if not dep_readline.found()
258     dep_readline = cc.find_library('readline', required: opt_readline, static: static)
259   endif
260   if dep_readline.found() and \
261      cc.has_header('readline/readline.h', dependencies: dep_readline) and \
262      cc.has_header('readline/history.h', dependencies: dep_readline)
263     cdata.set('HAVE_READLINE', 1)
264     deps_tinc += dep_readline
265   endif
266 endif
267
268 dep_zlib = dependency('zlib',
269                       required: opt_zlib,
270                       static: static,
271                       fallback: ['zlib', 'zlib_dep'])
272 if dep_zlib.found()
273   cdata.set('HAVE_ZLIB', 1)
274   deps_tincd += dep_zlib
275 endif
276
277 if not opt_lzo.disabled()
278   dep_lzo = dependency('lzo2', required: false, static: static)
279   if not dep_lzo.found()
280     dep_lzo = cc.find_library('lzo2', required: opt_lzo, static: static)
281   endif
282   if not dep_lzo.found()
283     dep_lzo = dependency('lzo2',
284                          required: false,
285                          static: static,
286                          fallback: ['lzo2', 'lzo2_dep'])
287   endif
288   if dep_lzo.found()
289     if dep_lzo.type_name() == 'internal' or cc.has_header('lzo/lzo1x.h', dependencies: dep_lzo)
290       cdata.set('LZO1X_H', '<lzo/lzo1x.h>')
291     elif cc.has_header('lzo1x.h', dependencies: dep_lzo)
292       cdata.set('LZO1X_H', '<lzo1x.h>')
293     else
294       msg = 'lzo1x.h not found'
295       if opt_lzo.auto()
296         warning(msg)
297       else
298         error(msg)
299       endif
300     endif
301     if cdata.has('LZO1X_H')
302       cdata.set('HAVE_LZO', 1)
303       deps_tincd += dep_lzo
304     endif
305   endif
306 endif
307
308 dep_lz4 = dependency('liblz4',
309                      required: opt_lz4,
310                      static: static,
311                      fallback: ['lz4', 'liblz4_dep'])
312 if dep_lz4.found()
313   deps_tincd += dep_lz4
314   cdata.set('HAVE_LZ4', 1)
315 endif
316
317 dep_vde = dependency('vdeplug', required: opt_vde, static: static)
318 dep_dl = cc.find_library('dl', required: opt_vde)
319 if dep_vde.found() and dep_dl.found()
320   cdata.set('ENABLE_VDE', 1)
321   src_tincd += 'vde_device.c'
322   deps_tincd += [dep_dl, dep_vde]
323 endif
324
325 if opt_jumbograms
326   cdata.set('ENABLE_JUMBOGRAMS', 1)
327 endif
328
329 subdir(opt_crypto)
330
331 if opt_crypto != 'nolegacy'
332   src_lib_crypto += ['cipher.c', 'digest.c']
333 endif
334
335 subdir('include')
336
337 lib_crypto = static_library(
338   'tinc_crypto',
339   sources: src_lib_crypto,
340   dependencies: dep_crypto,
341   implicit_include_directories: false,
342   include_directories: inc_conf,
343   build_by_default: false,
344 )
345
346 deps_lib_common = [deps_common, dep_crypto]
347 deps_tinc += deps_lib_common
348 deps_tincd += deps_lib_common
349
350 lib_common = static_library(
351   'common',
352   sources: src_lib_common,
353   dependencies: deps_lib_common,
354   link_with: [lib_ed25519, lib_chacha_poly, lib_crypto],
355   implicit_include_directories: false,
356   include_directories: inc_conf,
357   build_by_default: false,
358 )
359
360 lib_tinc = static_library(
361   'tinc',
362   sources: src_tinc,
363   dependencies: deps_tinc,
364   link_with: lib_common,
365   implicit_include_directories: false,
366   include_directories: inc_conf,
367   build_by_default: false,
368 )
369
370 lib_tincd = static_library(
371   'tincd',
372   sources: src_tincd,
373   dependencies: deps_tincd,
374   link_with: lib_common,
375   c_args: cc_flags_tincd,
376   implicit_include_directories: false,
377   include_directories: inc_conf,
378   build_by_default: false,
379 )
380
381 exe_tinc = executable(
382   'tinc',
383   sources: 'tincctl.c',
384   dependencies: deps_tinc,
385   link_with: lib_tinc,
386   implicit_include_directories: false,
387   include_directories: inc_conf,
388   install: true,
389   install_dir: dir_sbin,
390 )
391
392 exe_tincd = executable(
393   'tincd',
394   sources: 'tincd.c',
395   dependencies: deps_tincd,
396   link_with: lib_tincd,
397   c_args: cc_flags_tincd,
398   implicit_include_directories: false,
399   include_directories: inc_conf,
400   install: true,
401   install_dir: dir_sbin,
402 )
403
404 exe_sptps_test = executable(
405   'sptps_test',
406   sources: 'sptps_test.c',
407   dependencies: deps_lib_common,
408   link_with: lib_common,
409   implicit_include_directories: false,
410   include_directories: inc_conf,
411   build_by_default: false,
412 )
413
414 exe_sptps_keypair = executable(
415   'sptps_keypair',
416   sources: 'sptps_keypair.c',
417   dependencies: deps_lib_common,
418   link_with: lib_common,
419   implicit_include_directories: false,
420   include_directories: inc_conf,
421   build_by_default: false,
422 )
423
424 if os_name == 'linux'
425   dep_rt = cc.find_library('rt')
426
427   exe_sptps_speed = executable(
428     'sptps_speed',
429     sources: 'sptps_speed.c',
430     dependencies: [deps_lib_common, dep_rt],
431     link_with: lib_common,
432     implicit_include_directories: false,
433     include_directories: inc_conf,
434     build_by_default: false,
435   )
436
437   benchmark('sptps_speed', exe_sptps_speed, timeout: 90)
438 endif
439