chemoelectric

Ways to generate a UUID

Dec 6th, 2025
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. # Copyright (C) 2025 Barry Schwartz
  2. #
  3. # Copying and distribution of this file, with or without modification,
  4. # are permitted in any medium without royalty provided the copyright
  5. # notice and this notice are preserved. This file is offered as-is,
  6. # without any warranty.
  7.  
  8. # serial 1
  9.  
  10. # chem_unique_id([CHEM_UNIQUE_ID([VAR])
  11. # -------------------------------------
  12. #
  13. # Set VAR to shell code for a unique identifier. The result is
  14. # AC_SUBSTed and is also precious. Cached as chem_cv_unique_id.
  15. #
  16. # For no good reason, we prefer to create identifiers in the common
  17. # UUID format. But our purpose is mainly to use the identifiers in
  18. # filenames and such during build processes.
  19. #
  20. AC_DEFUN([CHEM_UNIQUE_ID],
  21. [AC_ARG_VAR([$1],[shell code to create a unique identifier without spaces])
  22. if test -n "@S|@{$1}"; then chem_cv_unique_id="@S|@{$1}"; fi
  23. AC_CACHE_CHECK([for shell code to create a unique identifier without spaces or newline],[chem_cv_unique_id],
  24. [if true; then
  25. # Using awk to chop a final newline is more portable than "head
  26. # -c-1", which is a GNU coreutils extension. The only option
  27. # POSIX requires of head is -n.
  28. __chop='awk '\''{printf "%s",@S|@0}'\'
  29. __os=`uname -s`
  30. if test "@S|@{__os}" = Linux &&
  31. test -r /proc/sys/kernel/random/uuid; then
  32. # Use the Linux proc filesystem.
  33. chem_cv_unique_id='if :;then cat /proc/sys/kernel/random/uuid|'@S|@{__chop}';fi'
  34. elif test "@S|@{__os}" = Linux &&
  35. uuidgen -V 2>&1 | grep -q 'uuidgen from util-linux'; then
  36. # Using the util-linux command uuidgen.
  37. chem_cv_unique_id='if :;then uuidgen|'@S|@{__chop}';fi'
  38. elif ruby -e 'require "securerandom";print SecureRandom.uuid' 1>/dev/null 2>/dev/null; then
  39. # Use Ruby.
  40. chem_cv_unique_id='ruby -e '\''require "securerandom";print SecureRandom.uuid'\'
  41. elif python -c 'import uuid; print (uuid.uuid4().urn)' 1>/dev/null 2>/dev/null; then
  42. # Use Python.
  43. chem_cv_unique_id='if :;then python -c '\''import uuid;print(uuid.uuid4().urn,end="")'\''|sed '\''s/^urn:uuid://'\'';fi'
  44. elif perl -e 'use Data::UUID;print Data::UUID->new()->create_str();' 1>/dev/null 2>/dev/null; then
  45. # Use Perl.
  46. chem_cv_unique_id='perl -e '\''use Data::UUID;print Data::UUID->new()->create_str();'\'
  47. elif node -e 'console.log(crypto.randomUUID())' 1>/dev/null 2>/dev/null; then
  48. # Use nodejs.
  49. chem_cv_unique_id='if :;then node -e '\''console.log(crypto.randomUUID())'\''|'@S|@{__chop}';fi'
  50. elif openssl rand -hex 16 1>/dev/null 2>/dev/null; then
  51. # Use OpenSSL to generate a random 16-digit hex number. Then
  52. # use someone's magical combination of POSIX commands to put that
  53. # number into UUID format.
  54. chem_cv_unique_id='if :;then openssl rand -hex 16|tr '\''@<:@:lower:@:>@'\'' '\''@<:@:upper:@:>@'\''|sed '\''s/\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)/\1\2\3\4-\5\6-\7\8-\9\10-\11\12\13\14\15\16/'\''|awk '\''{uuid=@S|@0;sub("-..-","-4!-",uuid);sub("-..-","-8!-",uuid);gsub("!","-",uuid);printf uuid}'\'';fi'
  55. else
  56. # As a last resort, use ${RANDOM} and issue a warning.
  57. chem_cv_unique_id='printf "%04x%s" ${RANDOM} `date +%Y%j%H%M%S`'
  58. AC_MSG_WARN([])
  59. AC_MSG_WARN([])
  60. AC_MSG_WARN([No good unique-identifier method found. Using \`\@S|@{RANDOM}' and \`date'.])
  61. AC_MSG_WARN([])
  62. AC_MSG_WARN([])
  63. fi
  64. fi])
  65. $1="${chem_cv_unique_id}"
  66. AC_SUBST([$1])
  67. ])
  68.  
  69. # local variables:
  70. # mode: shell-script
  71. # sh-shell: sh
  72. # coding: ascii
  73. # end:
  74.  
Advertisement
Add Comment
Please, Sign In to add comment