From 9fbedd0db42305c904fc28deb9c69c7fc8f3d870 Mon Sep 17 00:00:00 2001
From: Eli Schwartz <eschwartz@archlinux.org>
Date: Thu, 26 Aug 2021 02:21:35 -0400
Subject: [PATCH] Add meson build description

https://mesonbuild.com/

requirements:
- meson 0.38
- python 3.3 (required by meson)
- ninja (C++11) or samurai (C99)

Note:

HAVE_SSE41 is checked for but never ever defined, not even by ax_ext.m4,
so I have not bothered setting it in the conf file.
---
 meson.build       | 39 +++++++++++++++++++++
 meson_options.txt |  2 ++
 src/meson.build   | 87 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 128 insertions(+)
 create mode 100644 meson.build
 create mode 100644 meson_options.txt
 create mode 100644 src/meson.build

diff --git a/meson.build b/meson.build
new file mode 100644
index 0000000..69d281c
--- /dev/null
+++ b/meson.build
@@ -0,0 +1,39 @@
+project('libb2', 'c', version: '0.98.1',
+  meson_version: '>=0.38.0',
+  default_options: ['optimization=3'])
+
+conf = configuration_data()
+cc = meson.get_compiler('c')
+
+testcode = '''
+int main() {
+    __builtin_cpu_init();
+    return !__builtin_cpu_supports ("@0@");
+}
+'''
+
+fat = get_option('fat')
+native = get_option('native') and not fat
+
+if fat or native
+  foreach iset: ['avx', 'avx2', 'sse2', 'ssse3', 'sse4.1', 'xop']
+    arg = '-m'+iset
+    has_arg = cc.has_argument(arg)
+    if fat and not has_arg
+      error('fat binary but compiler does not support "@0@"'.format(arg))
+    elif native and has_arg and cc.run(testcode.format(iset)).returncode() == 0
+      add_project_arguments(arg, language: 'c')
+      # this is technically unnecessary as blake2-config.h additionally defines it
+      # based on gcc builtin __<iset>__ once -m<iset> is added
+      conf.set('HAVE_' + iset.underscorify().to_upper(), true)
+    endif
+  endforeach
+endif
+
+if host_machine.endian() == 'little'
+  conf.set('NATIVE_LITTLE_ENDIAN', true)
+endif
+
+use_sse = conf.get('HAVE_SSSE3', false)
+
+subdir('src')
diff --git a/meson_options.txt b/meson_options.txt
new file mode 100644
index 0000000..437009c
--- /dev/null
+++ b/meson_options.txt
@@ -0,0 +1,2 @@
+option('fat', type: 'boolean', value: false, description: 'build a fat binary on systems which support it')
+option('native', type: 'boolean', value: true, description: 'build a native binary with all optimizations supported')
diff --git a/src/meson.build b/src/meson.build
new file mode 100644
index 0000000..785988b
--- /dev/null
+++ b/src/meson.build
@@ -0,0 +1,87 @@
+configure_file(output: 'config.h', configuration: conf)
+
+sources = files(
+  'blake2sp.c',
+  'blake2bp.c',
+)
+
+if false # the code uses defines, not separate files
+  simd_mod = import('unstable-simd')
+  libb2_simd = simd_mod.check('blake2b',
+    sse2: 'blake2b-sse2.c',
+    ssse3: 'blake2b-ssse3.c',
+    sse41: 'blake2b-sse41.c',
+    avx: 'blake2b-avx.c',
+    # xop is not available in meson
+  )
+endif
+
+internal_libs = []
+
+if get_option('fat')
+  sources += files(
+    'blake2-dispatch.c',
+  )
+  internal_libs += [
+    static_library('blake2_ref',
+      'blake2b-ref.c', 'blake2s-ref.c',
+      c_args: ['-DSUFFIX=_ref']
+    ),
+    static_library('blake2_sse2',
+      'blake2b.c', 'blake2s.c',
+      c_args: ['-DSUFFIX=_sse2', '-msse2']
+    ),
+    static_library('blake2_ssse3',
+      'blake2b.c', 'blake2s.c',
+      c_args: ['-DSUFFIX=_ssse3', '-msse2', '-mssse3']
+    ),
+    static_library('blake2_sse4.1',
+      'blake2b.c', 'blake2s.c',
+      c_args: ['-DSUFFIX=_sse41', '-msse2', '-mssse3', '-msse4.1']
+    ),
+    static_library('blake2_avx',
+      'blake2b.c', 'blake2s.c',
+      c_args: ['-DSUFFIX=_avx', '-msse2', '-mssse3', '-msse4.1', '-mavx']
+    ),
+    static_library('blake2_xop',
+      'blake2b.c', 'blake2s.c',
+      c_args: ['-DSUFFIX=_xop', '-msse2', '-mssse3', '-msse4.1', '-mavx', '-mxop']
+    ),
+  ]
+elif use_sse
+  sources += files(
+    'blake2s.c',
+    'blake2b.c',
+  )
+else
+  sources += files(
+    'blake2s-ref.c',
+    'blake2b-ref.c',
+  )
+endif
+
+install_headers('blake2.h')
+libb2 = library('b2', sources,
+  c_args: '-DSUFFIX=',
+  link_with: internal_libs,
+  version: '1.0.4',
+  install: true,
+)
+
+libb2_dep = declare_dependency(
+  link_with: libb2,
+  include_directories: include_directories('.')
+)
+import('pkgconfig').generate( #libb2, # requires meson_version 0.46, not in Ubuntu 18.04
+  name: 'libb2',
+  libraries: libb2, # implied in 0.46
+  version: meson.project_version(), # implied in 0.46
+  description: 'C library providing BLAKE2b, BLAKE2s, BLAKE2bp, BLAKE2sp',
+  url: 'https://github.com/BLAKE2/libb2',
+)
+
+foreach t: ['2s', '2b', '2sp', '2bp']
+  name = 'blake'+t+'-test'
+  testprog = executable(name, name+'.c', link_with: libb2)
+  test(name, testprog)
+endforeach
