From f1b44b42a993172e451db558cae64d809d633afa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C5=8Dshin?= Date: Thu, 27 Dec 2018 10:56:10 -0800 Subject: [PATCH] Get it working --- ent.c | 32 ++++++-------------------------- include/ent/ent.h | 19 +++++++++++++++++-- include/ent/meson.build | 20 +++++++++++++++----- meson.build | 9 ++++++--- 4 files changed, 44 insertions(+), 36 deletions(-) diff --git a/ent.c b/ent.c index ae6c44277..7ac88b286 100644 --- a/ent.c +++ b/ent.c @@ -1,28 +1,14 @@ -#include +#include -#include - -#include "config.h" - -#if defined(USE_GETENTROPY) -# include -# if defined(__APPLE__) || defined(__linux__) -# include -# endif -# define getentropy_impl getentropy -#elif defined(USE_URANDOM) +#if defined(ENT_USE_URANDOM) +# include # include # include # include -#else -# error "port: getentropy" -#endif -#if defined(USE_URANDOM) - -static int -getentropy_impl(void* buf, size_t buflen) +int +ent_getentropy(void* buf, size_t buflen) { int fd; @@ -38,10 +24,4 @@ getentropy_impl(void* buf, size_t buflen) return 0; } -#endif - -int -ent_getentropy(void *buf, size_t buflen) -{ - return getentropy_impl(buf, buflen); -} +#endif /* ENT_USE_URANDOM */ diff --git a/include/ent/ent.h b/include/ent/ent.h index f362a3596..cecf3ce1d 100644 --- a/include/ent/ent.h +++ b/include/ent/ent.h @@ -1,10 +1,21 @@ #ifndef _ENT_H #define _ENT_H -#include - #include +#if defined(ENT_GETENTROPY) +# if defined(ENT_GE_SYSRANDOM) +# include +# include /* OSX sys/random.h needs Availability.h from this */ +# include +# elif defined(ENT_GE_UNISTD) +# include +# else +# error "libent: this shouldn't happen" +# endif +# define ent_getentropy getentropy +#elif defined(ENT_URANDOM) + /* * Fills buf with high-quality entropy. * @@ -16,4 +27,8 @@ int ent_getentropy(void* buf, size_t buflen); +#else +# error "libent: platform not supported" +#endif + #endif /* _ENT_H */ diff --git a/include/ent/meson.build b/include/ent/meson.build index 134f391af..2f6df5a55 100644 --- a/include/ent/meson.build +++ b/include/ent/meson.build @@ -1,19 +1,29 @@ conf_data = configuration_data() compiler = meson.get_compiler('c') -code = '''#include +code = '''#include +#include /* OSX sys/random.h needs Availability.h from this */ #include int main() { return getentropy((void*)0, 0); } ''' -have_getentropy = compiler.links(code, name : 'system getentropy') -conf_data.set('ENT_USE_GETENTROPY', have_getentropy) +sysrandom = compiler.links(code, name : 'getentropy in sys/random.h') +conf_data.set('ENT_GE_SYSRANDOM', sysrandom) + +code = '''#include +int main() { return getentropy((void*)0, 0); } +''' +unistd = compiler.links(code, name : 'getentropy is in unistd.h') +conf_data.set('ENT_GE_UNISTD', unistd) + +have_getentropy = sysrandom or unistd +conf_data.set('ENT_GETENTROPY', have_getentropy) if not have_getentropy code = '''#include int main() { return -1 != open("/dev/urandom", O_RDONLY) ? 0 : 1; } ''' - use_urandom = compiler.run(code, name : 'open /dev/urandom') - conf_data.set('ENT_USE_URANDOM', use_urandom.returncode() == 0) + use_urandom = compiler.run(code, name : 'can open /dev/urandom') + conf_data.set('ENT_URANDOM', use_urandom.returncode() == 0) endif configure_file(output : 'config.h', diff --git a/meson.build b/meson.build index 4c4024dc6..ed2ff2b78 100644 --- a/meson.build +++ b/meson.build @@ -9,10 +9,13 @@ inc = include_directories('include') ent_sources = ['ent.c'] +ent_dep = declare_dependency(sources : ent_sources, + compile_args : ['-Wall', '-pedantic'], + include_directories : inc) + libent = both_libraries('ent', - ent_sources, - include_directories : inc, - install : true) + dependencies : [ent_dep], + install : false) pkg_mod = import('pkgconfig') pkg_mod.generate(libraries : libent,