meson.build 12.4 KB
Newer Older
1
# Copyright © 2018-2019, VideoLAN and dav1d authors
Marvin Scholz's avatar
Marvin Scholz committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

project('dav1d', ['c'],
26
    version: '0.3.1',
27
    default_options: ['c_std=c99',
28
                      'warning_level=2',
29 30
                      'buildtype=release',
                      'b_ndebug=if-release'],
Marvin Scholz's avatar
Marvin Scholz committed
31 32
    meson_version: '>= 0.47.0')

33
dav1d_soname_version       = '2.0.0'
34 35 36 37
dav1d_api_version_array    = dav1d_soname_version.split('.')
dav1d_api_version_major    = dav1d_api_version_array[0]
dav1d_api_version_minor    = dav1d_api_version_array[1]
dav1d_api_version_revision = dav1d_api_version_array[2]
38

Marvin Scholz's avatar
Marvin Scholz committed
39
dav1d_src_root = meson.current_source_dir()
40 41 42
cc = meson.get_compiler('c')

# Configuratin data for config.h
Marvin Scholz's avatar
Marvin Scholz committed
43
cdata = configuration_data()
44 45

# Configuration data for config.asm
Marvin Scholz's avatar
Marvin Scholz committed
46
cdata_asm = configuration_data()
Marvin Scholz's avatar
Marvin Scholz committed
47

48
# Include directories
49
dav1d_inc_dirs = include_directories(['.', 'include/dav1d', 'include'])
50

Marvin Scholz's avatar
Marvin Scholz committed
51

52 53 54 55

#
# Option handling
#
56 57

# Bitdepth option
58
dav1d_bitdepths = get_option('bitdepths')
59
foreach bitdepth : ['8', '16']
60
    cdata.set10('CONFIG_@0@BPC'.format(bitdepth), dav1d_bitdepths.contains(bitdepth))
61
endforeach
Marvin Scholz's avatar
Marvin Scholz committed
62

63
# ASM option
64
is_asm_enabled = (get_option('enable_asm') == true and
65 66
    (host_machine.cpu_family().startswith('x86') or
     host_machine.cpu_family() == 'aarch64'      or
Luca Barbato's avatar
Luca Barbato committed
67 68
     host_machine.cpu_family().startswith('arm') or
     host_machine.cpu() == 'ppc64le'))
69 70
cdata.set10('HAVE_ASM', is_asm_enabled)

71
if is_asm_enabled and get_option('b_sanitize') == 'memory'
72
    error('asm causes false positive with memory sanitizer. Use \'-Denable_asm=false\'.')
73
endif
74

75
# Logging option
76
cdata.set10('CONFIG_LOG', get_option('logging'))
77

Marvin Scholz's avatar
Marvin Scholz committed
78
#
79
# OS/Compiler checks and defines
Marvin Scholz's avatar
Marvin Scholz committed
80 81
#

82 83 84
# Arguments in test_args will be used even on feature tests
test_args = []

85 86
optional_arguments = []

87 88 89 90 91 92 93 94 95
# Define _POSIX_C_SOURCE to POSIX.1–2001 (IEEE Std 1003.1-2001)
test_args  += '-D_POSIX_C_SOURCE=200112L'
add_project_arguments('-D_POSIX_C_SOURCE=200112L', language: 'c')

if host_machine.system() == 'windows'
    cdata.set('_WIN32_WINNT',           '0x0601')
    cdata.set('UNICODE',                1) # Define to 1 for Unicode (Wide Chars) APIs
    cdata.set('_UNICODE',               1) # Define to 1 for Unicode (Wide Chars) APIs
    cdata.set('__USE_MINGW_ANSI_STDIO', 1) # Define to force use of MinGW printf
96 97 98 99 100 101
    if cc.has_function('fseeko', prefix : '#include <stdio.h>', args : test_args)
        cdata.set('_FILE_OFFSET_BITS', 64) # Not set by default by Meson on Windows
    else
        cdata.set('fseeko', '_fseeki64')
        cdata.set('ftello', '_ftelli64')
    endif
Marvin Scholz's avatar
Marvin Scholz committed
102

103 104 105 106 107
    if (host_machine.cpu_family() == 'x86_64' and cc.get_id() == 'gcc')
        optional_arguments += '-mcmodel=small'
    endif

    # On Windows, we use a compatibility layer to emulate pthread
108 109
    thread_dependency = []
    thread_compat_dep = declare_dependency(sources : files('src/win32/thread.c'))
110 111
else
    thread_dependency = dependency('threads')
112
    thread_compat_dep = []
Marvin Scholz's avatar
Marvin Scholz committed
113 114
endif

115 116 117

# Header checks

118
stdatomic_dependency = []
Marvin Scholz's avatar
Marvin Scholz committed
119
if not cc.check_header('stdatomic.h')
120 121 122
    if cc.get_id() == 'msvc'
        # we have a custom replacement for MSVC
        stdatomic_dependency = declare_dependency(
123
            include_directories : include_directories('include/compat/msvc'),
124
        )
125 126 127 128 129
    elif cc.compiles('''int main() { int v = 0; return __atomic_fetch_add(&v, 1, __ATOMIC_SEQ_CST); }''',
                     name : 'GCC-style atomics', args : test_args)
        stdatomic_dependency = declare_dependency(
            include_directories : include_directories('include/compat/gcc'),
        )
130 131 132
    else
        error('Atomics not supported')
    endif
Marvin Scholz's avatar
Marvin Scholz committed
133 134
endif

135 136 137 138
if cc.check_header('unistd.h')
    cdata.set('HAVE_UNISTD_H', 1)
endif

139 140 141 142
if cc.check_header('io.h')
    cdata.set('HAVE_IO_H', 1)
endif

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

# Function checks

if not cc.has_function('getopt_long', prefix : '#include <getopt.h>', args : test_args)
    getopt_dependency = declare_dependency(
        sources: files('tools/compat/getopt.c'),
        include_directories : include_directories('include/compat'),
    )
else
    getopt_dependency = []
endif

if cc.has_function('posix_memalign', prefix : '#include <stdlib.h>', args : test_args)
    cdata.set('HAVE_POSIX_MEMALIGN', 1)
elif cc.has_function('_aligned_malloc', prefix : '#include <malloc.h>', args : test_args)
    cdata.set('HAVE_ALIGNED_MALLOC', 1)
159 160
elif cc.has_function('memalign', prefix : '#include <malloc.h>', args : test_args)
    cdata.set('HAVE_MEMALIGN', 1)
161 162
endif

163
if (host_machine.cpu_family() == 'aarch64' or
Luca Barbato's avatar
Luca Barbato committed
164 165
    host_machine.cpu_family().startswith('arm') or
    host_machine.cpu() == 'ppc64le')
166 167 168
    if cc.has_function('getauxval', prefix : '#include <sys/auxv.h>', args : test_args)
        cdata.set('HAVE_GETAUXVAL', 1)
    endif
169 170 171
    if cc.has_function('elf_aux_info', prefix : '#include <sys/auxv.h>', args : test_args)
        cdata.set('HAVE_ELF_AUX_INFO', 1)
    endif
172
endif
173 174 175 176 177 178 179 180 181 182 183 184

# Compiler flag tests

if cc.has_argument('-fvisibility=hidden')
    add_project_arguments('-fvisibility=hidden', language: 'c')
else
    warning('Compiler does not support -fvisibility=hidden, all symbols will be public!')
endif

# Compiler flags that should be set
# But when the compiler does not supports them
# it is not an error and silently tolerated
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
if cc.get_id() != 'msvc'
    optional_arguments += [
      '-Wundef',
      '-Werror=vla',
      '-Wno-maybe-uninitialized',
      '-Wno-missing-field-initializers',
      '-Wno-unused-parameter',
      '-Werror=missing-prototypes',
      '-Wshorten-64-to-32',
    ]
    if host_machine.cpu_family() == 'x86'
        optional_arguments += [
          '-msse2',
          '-mfpmath=sse',
        ]
    endif
else
202 203 204 205 206
    optional_arguments += [
      '-wd4028', # parameter different from declaration
      '-wd4996'  # use of POSIX functions
    ]
endif
207 208 209 210 211 212 213 214

if (get_option('buildtype') != 'debug' and get_option('buildtype') != 'plain')
    optional_arguments += '-fomit-frame-pointer'
    optional_arguments += '-ffast-math'
endif

add_project_arguments(cc.get_supported_arguments(optional_arguments), language : 'c')

215
# libFuzzer related things
216 217
fuzzing_engine = get_option('fuzzing_engine')
if fuzzing_engine == 'libfuzzer'
218
    if not cc.has_argument('-fsanitize=fuzzer')
219
        error('fuzzing_engine libfuzzer requires "-fsanitize=fuzzer"')
220 221 222 223
    endif
    fuzzer_args = ['-fsanitize=fuzzer-no-link', '-fsanitize=fuzzer']
    add_project_arguments(cc.first_supported_argument(fuzzer_args), language : 'c')
endif
224 225 226

# Stack alignments flags

227 228 229
stackalign_flag = []
stackrealign_flag = []

230 231
cdata.set10('ENDIANNESS_BIG', host_machine.endian() == 'big')

232
if host_machine.cpu_family().startswith('x86')
233 234 235
    if get_option('stack_alignment') > 0
        stack_alignment = get_option('stack_alignment')
    elif host_machine.cpu_family() == 'x86_64'
236 237 238 239 240 241 242 243 244 245 246
        if cc.has_argument('-mpreferred-stack-boundary=5')
            stackalign_flag = ['-mpreferred-stack-boundary=5']
            stackrealign_flag = ['-mincoming-stack-boundary=4']
            stack_alignment = 32
        elif cc.has_argument('-mstack-alignment=32')
            stackalign_flag = ['-mstack-alignment=32']
            stackrealign_flag = ['-mstackrealign']
            stack_alignment = 32
        else
            stack_alignment = 16
        endif
247
    else
248 249 250 251 252 253 254 255 256 257
        if host_machine.system() == 'linux' or host_machine.system() == 'darwin'
            stack_alignment = 16
        elif cc.has_argument('-mpreferred-stack-boundary=4')
            stackalign_flag = ['-mpreferred-stack-boundary=4']
            stackrealign_flag = ['-mincoming-stack-boundary=2']
            stack_alignment = 16
        elif cc.has_argument('-mstack-alignment=16')
            stackalign_flag = ['-mstack-alignment=16']
            stackrealign_flag = ['-mstackrealign']
            stack_alignment = 16
Marvin Scholz's avatar
Marvin Scholz committed
258
        else
259
            stack_alignment = 4
Marvin Scholz's avatar
Marvin Scholz committed
260
        endif
261
    endif
262 263
    cdata_asm.set('STACK_ALIGNMENT', stack_alignment)
    cdata.set('STACK_ALIGNMENT', stack_alignment)
Marvin Scholz's avatar
Marvin Scholz committed
264 265
endif

266 267 268 269 270 271 272 273 274 275 276 277 278
cdata.set10('ARCH_AARCH64', host_machine.cpu_family() == 'aarch64')
cdata.set10('ARCH_ARM',     host_machine.cpu_family().startswith('arm'))
if (is_asm_enabled and
    (host_machine.cpu_family() == 'aarch64' or
     host_machine.cpu_family().startswith('arm')))

   as_func_code = '''__asm__ (
".func meson_test"
".endfunc"
);
'''
    have_as_func = cc.compiles(as_func_code)
    cdata.set10('HAVE_AS_FUNC', have_as_func)
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

    # fedora package build infrastructure uses a gcc specs file to enable
    # '-fPIE' by default. The chosen way only adds '-fPIE' to the C compiler
    # with integrated preprocessor. It is not added to the standalone
    # preprocessor or the preprocessing stage of '.S' files. So we have to
    # compile code to check if we have to define PIC for the arm asm to
    # avoid absolute relocations when building for example checkasm.
    check_pic_code = '''
#if defined(PIC)
#error "PIC already defined"
#elif !(defined(__PIC__) || defined(__pic__))
#error "no pic"
#endif
'''
    if cc.compiles(check_pic_code)
        cdata.set('PIC', '3')
    endif
296 297
endif

298 299 300 301
cdata.set10('ARCH_X86', host_machine.cpu_family().startswith('x86'))
cdata.set10('ARCH_X86_64', host_machine.cpu_family() == 'x86_64')
cdata.set10('ARCH_X86_32', host_machine.cpu_family() == 'x86')

Marvin Scholz's avatar
Marvin Scholz committed
302
if host_machine.cpu_family().startswith('x86')
303 304 305
    cdata_asm.set10('ARCH_X86_64', host_machine.cpu_family() == 'x86_64')
    cdata_asm.set10('ARCH_X86_32', host_machine.cpu_family() == 'x86')
    cdata_asm.set10('PIC', true)
Marvin Scholz's avatar
Marvin Scholz committed
306 307
endif

Luca Barbato's avatar
Luca Barbato committed
308 309
cdata.set10('ARCH_PPC64LE', host_machine.cpu() == 'ppc64le')

Marvin Scholz's avatar
Marvin Scholz committed
310
if cc.symbols_have_underscore_prefix()
311
    cdata.set10('PREFIX', true)
Marvin Scholz's avatar
Marvin Scholz committed
312 313 314
    cdata_asm.set10('PREFIX', true)
endif

315 316
# Generate config.h
config_h_target = configure_file(output: 'config.h', configuration: cdata)
Marvin Scholz's avatar
Marvin Scholz committed
317 318


Marvin Scholz's avatar
Marvin Scholz committed
319

Marvin Scholz's avatar
Marvin Scholz committed
320
#
321
# ASM specific stuff
Marvin Scholz's avatar
Marvin Scholz committed
322
#
323
if is_asm_enabled and host_machine.cpu_family().startswith('x86')
Marvin Scholz's avatar
Marvin Scholz committed
324

325 326
    # Generate config.asm
    config_asm_target = configure_file(output: 'config.asm', output_format: 'nasm', configuration: cdata_asm)
327 328


329
    # NASM compiler support
Marvin Scholz's avatar
Marvin Scholz committed
330 331 332

    nasm = find_program('nasm')

333 334 335
    # check NASM version
    if nasm.found()
        nasm_r = run_command(nasm, '-v')
336 337 338 339 340

        if nasm_r.returncode() != 0
            error('failed running nasm to obtain its version')
        endif

341 342
        out = nasm_r.stdout().strip().split()
        if out[1].to_lower() == 'version'
343 344
            if out[2].version_compare('<2.13.02')
                error('nasm 2.13.02 or later is required, found nasm @0@'.format(out[2]))
345 346 347 348 349 350
            endif
        else
            error('unexpected nasm version string: @0@'.format(nasm_r.stdout()))
        endif
    endif

Marvin Scholz's avatar
Marvin Scholz committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    if host_machine.system() == 'windows'
        nasm_format = 'win'
    elif host_machine.system() == 'darwin'
        nasm_format = 'macho'
    else
        nasm_format = 'elf'
    endif
    if host_machine.cpu_family() == 'x86_64'
        nasm_format += '64'
    else
        nasm_format += '32'
    endif

    nasm_gen = generator(nasm,
        output: '@BASENAME@.obj',
        depfile: '@BASENAME@.obj.ndep',
        arguments: [
            '-f', nasm_format,
369
            '-I', '@SOURCE_DIR@/src/',
370
            '-I', '@0@/'.format(meson.current_build_dir()),
Marvin Scholz's avatar
Marvin Scholz committed
371 372 373 374 375 376 377
            '-MQ', '@OUTPUT@', '-MF', '@DEPFILE@',
            '@EXTRA_ARGS@',
            '@INPUT@',
            '-o', '@OUTPUT@'
        ])
endif

Henrik Gramner's avatar
Henrik Gramner committed
378

379

380 381 382
#
# Include subdir meson.build files
# The order is important!
Marvin Scholz's avatar
Marvin Scholz committed
383

384
subdir('include')
Marvin Scholz's avatar
Marvin Scholz committed
385

Janne Grunau's avatar
Janne Grunau committed
386 387
subdir('doc')

388
subdir('src')
389

390
subdir('tools')
391

392
subdir('tests')