https://codeberg.org/interpeer/vessel.git
Raw File
Tip revision: 5c7a39214ce9b5396b7aafb9d576b159b57ef04a authored by Jens Finkhaeuser on 09 January 2024, 12:10:19 UTC
(void) and () have different meanings in C
Tip revision: 5c7a392
meson.build
##############################################################################
# Project
project('vessel', ['cpp', 'c'],
  version: '0.1.1',
  license: 'GPLv3',
  meson_version: '>=0.64.0',
  default_options: [
    'cpp_std=c++17',
    'default_library=both',
  ])


##############################################################################
# Versioning, project and libraries

# There are so many versioning schemes, each of which is incompatible with
# others. We'll manage best by keeping things simple:
#
# - The package version follows semver
# - The library version is the package version
# - The ABI version, i.e. the compatibility we expect, is the major
#   component of the package
_splitver = meson.project_version().split('.')
PACKAGE_MAJOR = _splitver[0]
PACKAGE_MINOR = _splitver[1]
PACKAGE_PATCH = _splitver[2]

ABI_VERSION = PACKAGE_MAJOR
LIB_VERSION = meson.project_version()
PACKAGE_VERSION = meson.project_version()

##############################################################################
# Configuration

conf_data = configuration_data()
compiler = meson.get_compiler('cpp')

host_type = ''
if host_machine.system() in [ 'cygwin', 'darwin', 'dragonfly', 'freebsd', 'gnu', 'linux', 'netbsd' ]
  host_type = 'posix'
elif host_machine.system() == 'windows'
  host_type = 'win32'
elif host_machine.system().startswith('android')
  host_type = 'android'
endif
summary('Host platform', host_type, section: 'Platform')

# For Windows, try to determine the SDK version.
winsdk_ver = -1
if compiler.has_header('ntverp.h')
  code = '''#include <iostream>
#include <ntverp.h>

int main()
{
  std::cout << VER_PRODUCTMAJORVERSION;
}
'''
  result = compiler.run(code, name: 'Windows SDK version check.')
  winsdk_ver = result.stdout().to_int()
  summary('Windows SDK major version', winsdk_ver, section: 'Platform')
endif


### Build configuration
bt = get_option('buildtype')

libtype = get_option('default_library')
build_shared = false
if libtype in ['shared', 'both']
  build_shared = true
endif

build_static = false
if libtype in ['static', 'both']
  build_static = true
endif


### Compiler flags
compiler_id = compiler.get_id()

cpp_args = []
link_args = []
static_link_args = []
cpp_defines = []
cpp_define_args = []
define_prefix = '-D'

posix_common_args = [
  '-Wall', '-Wextra', '-Wpedantic', '-Wshadow', '-Wstrict-aliasing',
  '-Wstrict-overflow=5', '-Wcast-align',
  '-Wpointer-arith', '-Wcast-qual', '-Wold-style-cast',
]
if compiler_id == 'clang'
  cpp_args += posix_common_args + [
    '-Wabi', '-flto',
  ]
  link_args += ['-flto']
  static_link_args = [
    '-lstdc++', '-lm',
  ]
  cpp_defines += ['VESSEL_IS_BUILDING=1']
elif compiler_id == 'gcc'
  cpp_args += posix_common_args + [
    '-Wstrict-null-sentinel', '-flto',
  ]
  link_args += ['-flto']
  static_link_args = [
    '-lstdc++', '-lm',
  ]
  cpp_defines += ['VESSEL_IS_BUILDING=1']
elif compiler_id == 'msvc'
  cpp_args += [
    '/wd4250', '/wd4251', '/wd4275',
  ]
  cpp_defines += ['VESSEL_IS_BUILDING=1']
  define_prefix = '/D'
endif

if bt in ['debug', 'debugoptimized']
  cpp_defines += ['DEBUG=1']

  if compiler_id == 'clang'
    cpp_args += ['-ggdb']
  elif compiler_id == 'gcc'
    cpp_args += ['-g3']
  elif compiler_id == 'msvc'
    cpp_args += ['/FS']
  endif
else
  cpp_defines += ['NDEBUG=1']

  posix_release_args = [
    '-fvisibility=hidden', '-fvisibility-inlines-hidden',
  ]
  if compiler_id == 'clang'
    cpp_args += posix_release_args
  elif compiler_id == 'gcc'
    cpp_args += posix_release_args
  elif compiler_id == 'msvc'
    cpp_args += ['/Oi']
  endif
endif

if host_type == 'android'
  # Only posix compilers supported (?)
  cpp_defines += ['ANDROID_STL=c++_shared']

  cpp_args += [
    '-fexceptions', '-frtti',
  ]
  link_args = static_link_args
endif


# Make things work with MSVC and Windows SDK <10
if compiler_id == 'msvc' and winsdk_ver < 10
  cpp_args += ['/permissive']
endif

# Turn defines into cpp args
foreach def : cpp_defines
  cpp_define_args += [define_prefix + def]
endforeach


add_project_arguments(cpp_args, language: 'cpp')


### Version and package information
conf_data.set('VESSEL_PACKAGE_MAJOR', PACKAGE_MAJOR)
conf_data.set('VESSEL_PACKAGE_MINOR', PACKAGE_MINOR)
conf_data.set('VESSEL_PACKAGE_PATCH', PACKAGE_PATCH)
conf_data.set_quoted('VESSEL_PACKAGE_VERSION', PACKAGE_VERSION)
conf_data.set_quoted('VESSEL_ABI_VERSION', ABI_VERSION)
conf_data.set_quoted('VESSEL_LIB_VERSION', LIB_VERSION)

conf_data.set_quoted('VESSEL_PACKAGE_NAME', meson.project_name())
conf_data.set_quoted('VESSEL_PACKAGE_URL', 'https://codeberg.org/interpeer/vessel')

### Host platform details
conf_data.set('VESSEL_BIGENDIAN', host_machine.endian() == 'big')

### Headers
have_mman = compiler.has_header('sys' / 'mman.h')
conf_data.set('VESSEL_HAVE_SYS_MMAN_H', have_mman)


### Types

compiler.sizeof('int32_t', prefix: '#include <stdint.h>')
compiler.sizeof('uint32_t', prefix: '#include <stdint.h>')
compiler.sizeof('int64_t', prefix: '#include <stdint.h>')
compiler.sizeof('uint64_t', prefix: '#include <stdint.h>')
compiler.sizeof('size_t', prefix: '#include <stdint.h>')
compiler.sizeof('ssize_t', prefix: '#include <stdint.h>')
char8_size = compiler.sizeof('char8_t')
if char8_size > 0
  conf_data.set('VESSEL_HAVE_CHAR8_T', true)
endif
wchar_size = compiler.sizeof('wchar_t')
if wchar_size > 0
  conf_data.set('VESSEL_HAVE_WCHAR_T', true)
endif
stdbyte_size = compiler.sizeof('std::byte', prefix: '#include <cstddef>')
if stdbyte_size > 0
  conf_data.set('VESSEL_HAVE_STD_BYTE', true)
endif

### Symbols
have_stat = compiler.compiles('''
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main(int, char **)
{
  struct stat buf;
  stat("foo", &buf);
}''', name: 'stat()')
conf_data.set('VESSEL_HAVE_STAT', have_stat)

have__stat = compiler.compiles('''
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main(int, char **)
{
  struct _stat buf;
  _stat("foo", &buf);
}''', name: '_stat()')
conf_data.set('VESSEL_HAVE__STAT', have__stat)



### Options
default_extent_size_multiplier = get_option('default_extent_size_multiplier')
conf_data.set('VESSEL_DEFAULT_EXTENT_SIZE_MULTIPLIER', default_extent_size_multiplier)



##############################################################################
# Dependencies
subproject_opts = [
  'default_library=' + libtype,
]

libvar_postfix = ''
if libtype in ['static', 'both']
  libvar_postfix = '_static_dep'
else
  libvar_postfix = '_dep'
endif

deps = []

##### ICU

icu_dep = dependency(
  'icu-uc',
  modules: ['icu-uc'],
  fallback: ['icu', 'icuuc_dep'],
  default_options: subproject_opts,
  required: false,
)

icui18n_dep = dependency(
  'icu-i18n',
  modules: ['icu-i18n'],
  fallback: ['icu', 'icui18n_dep'],
  default_options: subproject_opts,
  required: false,
)

deps += [icu_dep, icui18n_dep]

icu_type = icu_dep.type_name() + ', ' + icu_dep.version()

summary('ICU', icu_type, section: 'Unicode Libraries')
conf_data.set('VESSEL_HAVE_ICU', icu_dep.found())

##### magic

magic_dep = dependency(
  'libmagic',
  required: false,
)

conf_data.set('VESSEL_HAVE_LIBMAGIC', magic_dep.found())

deps += magic_dep


##### liberate

liberate_dep = dependency(
  'liberate',
  fallback: ['liberate', 'liberate' + libvar_postfix],
  default_options: subproject_opts,
)

deps += [liberate_dep]

summary('liberate', liberate_dep.version(), section: 'Other Dependencies')

##### s3kr1t

s3kr1t_dep = dependency(
  's3kr1t',
  fallback: ['s3kr1t', 's3kr1t' + libvar_postfix],
  default_options: subproject_opts,
)

deps += [s3kr1t_dep]

summary('s3kr1t', s3kr1t_dep.version(), section: 'Other Dependencies')


##############################################################################
# Vendored dependencies
# subdir('vendor')
# deps += [vendored_deps]

##############################################################################
# Set values from options
configure_file(
  input: 'build-config.h.in',
  output: 'build-config.h',
  configuration: conf_data
)

main_build_dir = declare_dependency(
  include_directories: include_directories('.')
)


##############################################################################
# Library

includes = include_directories(
  'include',
)

libincludes = include_directories(
  'lib',
)


install_headers(
  'include' / 'vessel.h',
)

install_headers(
  'include' / 'vessel' / 'version.h',
  'include' / 'vessel' / 'visibility.h',
  'include' / 'vessel' / 'error.h',
  'include' / 'vessel' / 'author.h',
  'include' / 'vessel' / 'extent.h',
  'include' / 'vessel' / 'algorithms.h',
  'include' / 'vessel' / 'section.h',
  'include' / 'vessel' / 'store.h',
  'include' / 'vessel' / 'context.h',
  'include' / 'vessel' / 'resource.h',
  'include' / 'vessel' / 'plugins.h',

  subdir: 'vessel',
)

install_headers(
  'include' / 'vessel' / 'generated' / 'enums.h',

  subdir: 'vessel' / 'generated',
)


install_data(
  'data' / 'vessel.magic',

  # XXX Packagers can link this file
  install_dir: get_option('datadir') / 'vessel' / 'magic',
)


libsrc = [
  'lib' / 'version.cpp',
  'lib' / 'error.cpp',
  'lib' / 'author.cpp',
  'lib' / 'extent.cpp',
  'lib' / 'algorithms.cpp',
  'lib' / 'section.cpp',
  'lib' / 'store.cpp',
  'lib' / 'context.cpp',
  'lib' / 'resource.cpp',
]

dep_internal = false

# Shared library?
if build_shared
  so_lib = shared_library('vessel', libsrc,
      include_directories: [includes],
      dependencies: deps,
      link_args: link_args,
      cpp_args: cpp_define_args,
      version: LIB_VERSION,
      soversion: ABI_VERSION,
      install: true
  )

  vessel_dep = declare_dependency(
      include_directories: [includes],
      dependencies: deps,
      compile_args: [],
      link_with: [so_lib],
      link_args: link_args,
      version: LIB_VERSION,
  )

  dep_internal = vessel_dep
endif

# Static library?
if build_static
  static_args = [define_prefix + 'VESSEL_STATIC=1']

  sta_lib = static_library('vessel', libsrc,
      include_directories: [includes],
      dependencies: deps,
      link_args: link_args,
      cpp_args: cpp_define_args + static_args,
      install: true,
  )

  vessel_static_dep = declare_dependency(
      include_directories: [includes],
      dependencies: deps,
      compile_args: static_args,
      link_with: [sta_lib],
      link_args: link_args + static_link_args,
      version: LIB_VERSION,
  )

  dep_internal = vessel_static_dep
endif


##############################################################################
# Subdirectories
subdir('test')
subdir('plugins')
subdir('cli')

##############################################################################
# Linter, etc.
cppcheck = find_program('cppcheck', required: false)

if cppcheck.found()
  run_target('cppcheck', command: [
    cppcheck.full_path(),
    '--cppcheck-build-dir=@0@/cppcheck'.format(meson.current_build_dir()),
    '--enable=all', '-v',
    '--suppress=missingIncludeSystem',
    '--inline-suppr',
    '-I', '@0@/include'.format(meson.current_source_dir()),
    '-I', '@0@/lib'.format(meson.current_source_dir()),
    '-I', meson.current_build_dir(),
    '--std=c++17',
    cpp_define_args,
    '--quiet',
    '@0@/lib'.format(meson.current_source_dir()),
  ])
endif

oclint = find_program('oclint', required: false)

if oclint.found()
  oclint_config = custom_target('oclint_config',
      output: '.oclint',
      input: '.oclint',
      command: ['cp', '@INPUT@', '@OUTPUT@'],
      install: false,
  )

  oclintsrc = []
  foreach fname : libsrc
    oclintsrc += [meson.current_source_dir() / fname]
  endforeach

  run_target('oclint', command: [
      oclint.full_path(),
      oclintsrc,
      '-o', '@0@/oclint.log'.format(meson.current_build_dir()),
      '--',
      '-I', '@0@/include'.format(meson.current_source_dir()),
      '-I', '@0@/lib'.format(meson.current_source_dir()),
      '-I', meson.current_build_dir(),
      '--std=@0@'.format(get_option('cpp_std')),
      cpp_args,
      cpp_define_args,
      '-DOCLINT_IS_RUNNING',
    ],
    depends: oclint_config,
  )
endif
back to top