Permalink
Browse files

use native uuid_ functions on BSD

  • Loading branch information...
1 parent 8fa3870 commit 48b5eeae3e4ec3881aefa594c20932f556a7b509 Joe Caswell committed Aug 24, 2012
Showing with 80 additions and 18 deletions.
  1. +59 −0 c_src/druid.c
  2. +0 −18 rebar.config
  3. +21 −0 rebar.config.script
View
@@ -21,7 +21,19 @@
*********************************************************************/
#include "erl_nif.h"
+
+#if (defined(__unix__) || defined(unix)) && !defined(USG)
+#include <sys/param.h>
+#endif
+
+#if (defined(BSD) && (BSD >= 199306))
+#include <uuid.h>
+#include <string.h>
+#define uuid_rc_t uint32_t
+#define USE_BSD_UUID
+#else
#include "uuid.h"
+#endif
static ErlNifResourceType* druuid_RESOURCE;
@@ -30,6 +42,7 @@ static ERL_NIF_TERM druuid_uuid_v4(ErlNifEnv* env, int argc,
const ERL_NIF_TERM argv[]);
static ERL_NIF_TERM druuid_uuid_v4_str(ErlNifEnv* env, int argc,
const ERL_NIF_TERM argv[]);
+
static ERL_NIF_TERM error_tuple(ErlNifEnv* env,
ERL_NIF_TERM reason,
uuid_rc_t rc);
@@ -47,7 +60,52 @@ static ErlNifFunc nif_funcs[] =
{"v4", 0, druuid_uuid_v4},
{"v4_str", 0, druuid_uuid_v4_str}
};
+#ifdef USE_BSD_UUID
+static ERL_NIF_TERM druuid_uuid_v4(ErlNifEnv* env, int argc,
+ const ERL_NIF_TERM argv[])
+{
+ struct uuid uuid;
+ uint32_t status;
+
+ uuid_create(&uuid, &status);
+ if (status != uuid_s_ok) {
+ return error_tuple(env, ATOM_UUID_CREATE_FAILED, status);
+ }
+
+ ERL_NIF_TERM value_bin;
+ unsigned char* value = enif_make_new_binary(env, sizeof(uuid), &value_bin);
+ memcpy(value, &uuid, sizeof(uuid));
+
+ return value_bin;
+
+}
+
+static ERL_NIF_TERM druuid_uuid_v4_str(ErlNifEnv* env, int argc,
+ const ERL_NIF_TERM argv[])
+{
+
+ uuid_t uuid;
+ uint32_t status;
+ char *str;
+
+ uuid_create(&uuid, &status);
+ if (status != uuid_s_ok) {
+ return error_tuple(env, ATOM_UUID_CREATE_FAILED, status);
+ }
+ uuid_to_string(&uuid, &str, &status);
+ if (status != uuid_s_ok) {
+ return error_tuple(env, ATOM_UUID_EXPORT_FAILED, status);
+ }
+
+ ERL_NIF_TERM value_bin;
+ unsigned char* value = enif_make_new_binary(env, strlen(str), &value_bin);
+ memcpy(value, str, strlen(str));
+
+ return value_bin;
+
+}
+#else
static ERL_NIF_TERM druuid_uuid_v4(ErlNifEnv* env, int argc,
const ERL_NIF_TERM argv[])
{
@@ -121,6 +179,7 @@ static ERL_NIF_TERM druuid_uuid_v4_str(ErlNifEnv* env, int argc,
return value_bin;
}
+#endif
static void druuid_resource_cleanup(ErlNifEnv* env, void* arg)
{
View
@@ -1,18 +0,0 @@
-{so_name, "druuid.so"}.
-
-{port_sources, ["c_src/*.c"]}.
-
-{port_envs, [
- %% Make sure to set -fPIC when compiling ossp-uuid
- {"CFLAGS", "$CFLAGS -fPIC"},
- {"DRV_CFLAGS", "$DRV_CFLAGS -Werror -I c_src/uuid-1.6.2"},
- {"DRV_LDFLAGS", "$DRV_LDFLAGS c_src/uuid-1.6.2/.libs/libuuid.a"}
- ]}.
-
-%% {pre_hooks, [{compile, "make -C c_src/uuid-1.6.2"},
-%% {clean, "make -C c_src/uuid-1.6.2 clean"}]}.
-
-{pre_hooks, [{compile, "c_src/build_deps.sh"}]}.
-
-{post_hooks, [{clean, "c_src/build_deps.sh clean"}]}.
-
View
@@ -0,0 +1,21 @@
+Config=[
+ {so_name, "druuid,so"},
+ {port_sources, ["c_src/*,c"]},
+ {deps, [ ]},
+ {deps_dir, "c_src"},
+ {port_envs, [
+ %% Make sure to set -fPIC when compiling ossp-uuid
+ {"CFLAGS", "$CFLAGS -fPIC"},
+ {"^bsd","DRV_CFLAGS", "$DRV_CFLAGS -Werror -I c_src/uuid-1,6,2"},
+ {"^bsd","DRV_LDFLAGS", "$DRV_LDFLAGS c_src/uuid-1,6,2/,libs/libuuid,a"},
+ %% BSD provides uuid_create natively
+ {"bsd","DRV_CFLAGS", "$DRV_CFLAGS -Werror"},
+ ]},
+],
+case rebar_utils:is_arch("bsd") of
+match ->
+ true;
+nomatch ->
+ Config1 = lists:keystore(pre_hooks, 1, Config, {pre_hooks, [{compile, "c_src/build_deps,sh"}]}),
+ lists:keystore(post_hooks, 1, Config1, {post_hooks, [{clean, "c_src/build_deps,sh clean"}]})
+end.

0 comments on commit 48b5eea

Please sign in to comment.