1# This file is part of libmicrodns.
2#
3# Copyright © 2019 VideoLabs SAS
4#
5# Author: Mathieu Duponchelle <mathieu@centricular.com>
6#
7#########################################################################
8# libmicrodns is released under LGPLv2.1 (or later) and is also available
9# under a commercial license.
10#########################################################################
11# This program is free software; you can redistribute it and/or modify it
12# under the terms of the GNU Lesser General Public License as published by
13# the Free Software Foundation; either version 2.1 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU Lesser General Public License for more details.
20#
21# You should have received a copy of the GNU Lesser General Public License
22# along with this program; if not, write to the Free Software Foundation,
23# Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24
25project('microdns', ['c'],
26  version : '0.2.0',
27  meson_version : '>= 0.50.0',
28  default_options : ['warning_level=2',
29                     'buildtype=release',
30                     'b_ndebug=if-release'])
31
32mdns_version = meson.project_version()
33mdns_soname_version = '1.0.0'
34
35ver_arr = mdns_soname_version.split('.')
36mdns_major_version = ver_arr[0]
37mdns_minor_version = ver_arr[1]
38mdns_micro_version = ver_arr[2]
39
40cc = meson.get_compiler('c')
41
42warning_flags = []
43
44warning_flags += [
45    '-Wsign-compare',
46    '-Wstrict-aliasing',
47    '-Wstrict-overflow',
48    '-Wformat=2',
49    '-Wno-unused-parameter',
50    '-Wcast-align',
51    '-Wpointer-arith',
52    '-Wmissing-prototypes',
53    '-Wwrite-strings',
54    '-Wlogical-op',
55]
56
57add_project_arguments(cc.get_supported_arguments(warning_flags), language: 'c')
58
59cdata = configuration_data()
60
61deps = []
62
63host_system = host_machine.system()
64
65if host_system == 'windows'
66    deps += [cc.find_library('ws2_32')]
67    deps += [cc.find_library('iphlpapi')]
68endif
69
70inet_ntop_src = '''
71#ifdef _WIN32
72#include <ws2tcpip.h>
73#include <windows.h>
74# if _WIN32_WINNT < 0x600
75#  error Needs vista+
76# endif
77#else
78#include <sys/socket.h>
79#include <arpa/inet.h>
80#endif
81int main() {
82  inet_ntop(AF_INET, NULL, NULL, 0);
83}
84'''
85
86if cc.links(inet_ntop_src, dependencies: deps)
87    cdata.set('HAVE_INET_NTOP', 1)
88endif
89
90poll_src = '''
91#include <stddef.h>
92#ifdef _WIN32
93#include <winsock2.h>
94#include <windows.h>
95# if _WIN32_WINNT < 0x600
96#  error Needs vista+
97# endif
98# if defined(_MSC_VER)
99#   error
100# endif
101#else
102#include <poll.h>
103#endif
104int main() {
105  poll(NULL, 0, 0);
106}
107'''
108
109if cc.links(poll_src, dependencies: deps)
110    cdata.set('HAVE_POLL', 1)
111endif
112
113pollfd_check_prefix = '#include <sys/types.h>\n'
114
115if cdata.get('HAVE_POLL', 0) == 1
116    pollfd_check_prefix += '#include <poll.h>\n'
117elif host_system == 'windows'
118    pollfd_check_prefix += '#include <winsock2.h>'
119endif
120
121if cc.has_type('struct pollfd', prefix: pollfd_check_prefix)
122    cdata.set('HAVE_STRUCT_POLLFD', 1)
123endif
124
125if cc.has_function('getifaddrs')
126    cdata.set('HAVE_GETIFADDRS', 1)
127endif
128
129if cc.has_header('ifaddrs.h')
130    cdata.set('HAVE_IFADDRS_H', 1)
131endif
132
133if cc.has_header('unistd.h')
134	cdata.set('HAVE_UNISTD_H', 1)
135endif
136
137configure_file(output : 'config.h', configuration : cdata)
138
139c_args = ['-DHAVE_CONFIG_H']
140
141if host_system == 'windows'
142    c_args += [
143        '-D_UNICODE=1',
144        '-DUNICODE=1',
145        '-D_POSIX_C_SOURCE=200809L',
146        '-D_BSD_SOURCE=1'
147    ]
148endif
149
150if cc.get_id() == 'msvc'
151    c_args += [
152    '-D_CRT_NONSTDC_NO_DEPRECATE',
153    '-D_CRT_SECURE_NO_WARNINGS',
154    '-D_CRT_SECURE_NO_DEPRECATE',
155]
156endif
157
158link_flags=[]
159
160if get_option('fuzzing')
161    if cc.has_argument('-fsanitize=fuzzer')
162        fuzz_flags = ['-fsanitize=fuzzer-no-link,address,undefined']
163        c_args += fuzz_flags
164        c_args += ['-fprofile-instr-generate', '-fcoverage-mapping']
165        link_flags += fuzz_flags
166        link_flags += ['-fprofile-instr-generate', '-fcoverage-mapping']
167    else
168        error('Unsupported required option: -fsanitize=fuzzer')
169    endif
170endif
171
172incdirs = include_directories('.', 'include', 'compat')
173
174subdir('compat')
175subdir('include')
176subdir('src')
177
178pkg_cdata = configuration_data()
179
180pkg_cdata.set('prefix', join_paths(get_option('prefix')))
181pkg_cdata.set('exec_prefix', '${prefix}')
182pkg_cdata.set('libdir', '${prefix}/@0@'.format(get_option('libdir')))
183pkg_cdata.set('includedir', '${prefix}/@0@'.format(get_option('includedir')))
184pkg_cdata.set('VERSION', mdns_version)
185pkg_cdata.set('LIBSOCKET', host_system == 'windows' ? '-lws2_32 -liphlpapi': '')
186
187pkg_install_dir = '@0@/pkgconfig'.format(get_option('libdir'))
188
189configure_file(
190    input: 'src/microdns.pc.in',
191    output: 'microdns.pc',
192    configuration: pkg_cdata,
193    install_dir: pkg_install_dir,
194    install: true,
195)
196
197mdns_dep = declare_dependency(link_with : libmicrodns,
198    include_directories : incdirs,
199    dependencies: deps,
200)
201
202doc_cdata = configuration_data()
203
204doc_cdata.set('PACKAGE_NAME', meson.project_name())
205doc_cdata.set('VERSION', mdns_version)
206doc_cdata.set('abs_top_srcdir', meson.current_source_dir())
207
208configure_file(
209    input: 'doc/Doxyfile.in',
210    output: 'Doxyfile',
211    configuration: doc_cdata
212)
213
214subdir('examples')
215subdir('tests')
216