1# Copyright (C) 2019-2021 Alexandros Theodotou <alex at zrythm dot org>
2#
3# This file is part of libaudec
4#
5# libaudec is free software: you can redistribute it and/or modify
6# it under the terms of the GNU Affero General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# libaudec is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU Affero General Public License for more details.
14#
15# You should have received a copy of the GNU Affero General Public License
16# along with libaudec.  If not, see <https://www.gnu.org/licenses/>.
17
18project (
19  'libaudec', ['c'],
20  version: '0.3.4',
21  license: 'AGPLv3+',
22  meson_version: '>= 0.55.0',
23  default_options: [
24    'warning_level=2',
25    'buildtype=debug',
26    'c_std=gnu11',
27    ],
28  )
29
30cmake = import ('cmake')
31cmake_opts = cmake.subproject_options ()
32cmake_opts.add_cmake_defines ({'CMAKE_POSITION_INDEPENDENT_CODE': 'ON'})
33
34srcs = []
35subdir('inc')
36subdir('src')
37
38cc = meson.get_compiler ('c')
39is_ld_bfd = cc.get_linker_id () == 'ld.bfd'
40prefix = get_option ('prefix')
41includedir = join_paths (
42  prefix, get_option('includedir'))
43libdir = join_paths (prefix, get_option ('libdir'))
44
45cdata = configuration_data ()
46cdata.set_quoted (
47  'PACKAGE_VERSION',
48  '@VCS_TAG@')
49cdata.set_quoted (
50  'COMPILER',
51  meson.get_compiler('c').get_id())
52cdata.set_quoted (
53  'PREFIX', prefix)
54cdata.set_quoted (
55  'COMPILER_VERSION',
56  meson.get_compiler('c').version())
57cdata.set_quoted (
58  'CONFIGURE_LIBDIR',
59  libdir)
60
61# detect OS
62os_darwin = false
63os_linux = false
64os_freebsd = false
65os_windows = false
66if host_machine.system() == 'darwin'
67  os_darwin = true
68elif host_machine.system() == 'linux'
69  os_linux = true
70elif host_machine.system() == 'freebsd'
71  os_freebsd = true
72elif host_machine.system() == 'windows'
73  os_windows = true
74endif
75
76inc = [
77  include_directories ('.'),
78  include_directories ('inc'),
79  ]
80
81# Compiler flags
82audec_cflags = [
83  '-Wall',
84  '-Wformat=2',
85  '-Wno-missing-field-initializers',
86  '-Wno-unused-parameter',
87  '-Wno-sequence-point',
88  '-Wignored-qualifiers',
89  '-Wno-cast-function-type',
90  '-Werror=cast-qual',
91  '-Werror=clobbered',
92  '-Werror=conversion',
93  '-Werror=disabled-optimization',
94  '-Werror=double-promotion',
95  '-Werror=float-equal',
96  '-Werror=logical-op',
97  '-Werror=pointer-arith',
98  '-Werror=sign-conversion',
99  '-Werror=overlength-strings',
100  '-Werror=stringop-truncation',
101  '-Werror=missing-declarations',
102  '-Werror=redundant-decls',
103  '-Werror=shadow',
104  '-Werror=undef',
105  '-Werror=unused',
106  '-Werror=strict-aliasing',
107  '-fstrict-aliasing',
108  '-Werror=strict-overflow',
109  '-Wstrict-overflow=2',
110  '-fstrict-overflow',
111  '-Werror=duplicated-branches',
112  '-Werror=duplicated-cond',
113  '-Werror=null-dereference',
114  '-Werror=init-self',
115  '-Werror=jump-misses-init',
116  '-Werror=missing-prototypes',
117  '-Werror=nested-externs',
118  '-Werror=write-strings',
119  '-Werror=implicit-fallthrough',
120  '-Werror=sign-compare',
121  '-Werror=discarded-qualifiers',
122  '-Werror=float-conversion',
123  '-Werror=implicit-function-declaration',
124  '-Werror=uninitialized',
125  '-Werror=maybe-uninitialized',
126  '-Werror=return-type',
127  '-Werror=int-conversion',
128  '-Werror=format-security',
129  '-Werror=incompatible-pointer-types',
130  '-Werror=implicit-int',
131  '-Werror=multistatement-macros',
132  '-Werror=switch',
133  '-Werror=overflow',
134  '-Werror=array-bounds',
135  '-Werror=enum-compare',
136  '-Werror=misleading-indentation',
137  '-Werror=int-in-bool-context',
138  '-Werror=type-limits',
139  '-Werror=deprecated-declarations',
140  '-fvisibility=hidden',
141  ]
142if os_windows
143  audec_cflags += [
144    '-D_WOE32=1',
145    ]
146endif
147audec_cflags = cc.get_supported_arguments (
148  audec_cflags)
149
150samplerate_dep = dependency (
151  'samplerate', version: '>=0.1.8', required: false)
152if not samplerate_dep.found ()
153  samplerate_subproject = cmake.subproject (
154    'samplerate', options: cmake_opts)
155  samplerate_dep = samplerate_subproject.dependency (
156    'samplerate')
157endif
158sndfile_dep = dependency (
159  'sndfile', version: '>=1.0.25', required: false)
160if not sndfile_dep.found ()
161  sndfile_subproject = cmake.subproject (
162    'sndfile', options: cmake_opts)
163  sndfile_dep = sndfile_subproject.dependency (
164    'sndfile')
165endif
166
167# Maths functions might be implemented in libm
168libm = cc.find_library (
169  'm', required: false)
170
171audec_deps = [
172  sndfile_dep,
173  samplerate_dep,
174  libm,
175  ]
176
177# create config.h and add to deps
178tmp_h = configure_file (
179  output: 'tmp.h',
180  configuration: cdata,
181  )
182config_h_vcs = vcs_tag (
183  input: tmp_h,
184  output: 'config.h',
185  )
186config_h_dep = declare_dependency (
187  sources: config_h_vcs,
188  )
189audec_deps += config_h_dep
190
191mapfile = configure_file(
192  input: 'link.map.in',
193  output: 'link.map',
194  configuration: { 'VERSION': meson.project_version () },
195)
196vflag = '-Wl,--version-script,@0@'.format (mapfile)
197
198audec = both_libraries (
199  'audec',
200  sources: srcs,
201  dependencies: [
202    audec_deps,
203    ],
204  include_directories: inc,
205  link_args: is_ld_bfd ? vflag : [],
206  link_depends: is_ld_bfd ? mapfile : [],
207  install: not meson.is_subproject(),
208)
209
210# this is so that it can be used as a meson
211# subproject
212libaudec_dep = declare_dependency (
213  include_directories: inc,
214  dependencies: audec_deps,
215  link_with: audec.get_static_lib ())
216
217if not meson.is_subproject()
218  pkg_mod = import('pkgconfig')
219  pkg_mod.generate(
220    libraries: audec,
221    version: meson.project_version (),
222    name: 'audec',
223    filebase: 'audec',
224    description: 'A library to read and resample audio files.',
225    )
226endif
227
228audec_exe = executable (
229  'audec',
230  'src/main.c',
231  dependencies: libaudec_dep,
232  install: not meson.is_subproject(),
233  )
234
235subdir('tests')
236
237summary = [
238  '',
239  '------',
240  'libaudec @0@'.format(meson.project_version()),
241  '',
242  '  Build type: @0@'.format(
243    get_option('buildtype')),
244  '  Directories:',
245  '            prefix: @0@'.format(prefix),
246  '        includedir: @0@'.format(includedir),
247  '            libdir: @0@'.format(libdir),
248  '------',
249  ''
250]
251message('\n'.join(summary))
252