Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: (v1.0.3) ESP32 support #3

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
build/
build/
dist/
.DS_Store
142 changes: 82 additions & 60 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,82 +1,104 @@

cmake_minimum_required(VERSION 3.19)

project(
uuid4
VERSION 1.0.2
LANGUAGES C
)

include(GNUInstallDirs)
cmake_minimum_required(VERSION 3.24)

set(UUID4_SOURCES
${CMAKE_CURRENT_LIST_DIR}/src/uuid4.c
)

set(UUID4_HEADERS
set(UUID4_INCLUDE_DIR
${CMAKE_CURRENT_LIST_DIR}/include
)

add_library(uuid4-static STATIC ${UUID4_SOURCES})
add_library(uuid4-shared SHARED ${UUID4_SOURCES})

target_include_directories(uuid4-static PUBLIC
$<BUILD_INTERFACE:${UUID4_HEADERS}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_include_directories(uuid4-shared PUBLIC
$<BUILD_INTERFACE:${UUID4_HEADERS}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

# ==============
# configures the install function
if(ESP_PLATFORM)
# Run special CMake commands for ESP32
idf_component_register(
SRCS ${UUID4_SOURCES}
INCLUDE_DIRS ${UUID4_INCLUDE_DIR}
)

# installs the lib targets (.a and .dylib to /usr/local/lib)
install(
TARGETS uuid4-static uuid4-shared
EXPORT uuid4-config
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
# Copy header files to include directory and libraries to lib directory
add_custom_command(
TARGET ${COMPONENT_LIB}
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy_directory ${UUID4_INCLUDE_DIR}
${CMAKE_SOURCE_DIR}/include
COMMAND
${CMAKE_COMMAND} -E copy $<TARGET_FILE:${COMPONENT_LIB}>
${CMAKE_SOURCE_DIR}/lib/lib${COMPONENT_NAME}.a
COMMENT "Copying built archive file and header to lib directory..."
)
endif()

# installs the headers (/usr/local/include/uuid4)
install(
DIRECTORY ${UUID4_HEADERS}/${RPOJECT_NAME}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
project(
uuid4
VERSION 1.0.3
LANGUAGES C
)

# ==============

# ==============

if(NOT DEFINED UUID4_AS_SUBPROJECT)
set(UUID4_AS_SUBPROJECT ON)
if(NOT ESP_PLATFORM)
include(GNUInstallDirs)

if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(UUID4_AS_SUBPROJECT OFF)
endif()
endif()
add_library(uuid4-static STATIC ${UUID4_SOURCES})
add_library(uuid4-shared SHARED ${UUID4_SOURCES})

if(NOT UUID4_AS_SUBPROJECT)
export(
PACKAGE uuid4
target_include_directories(uuid4-static PUBLIC
$<BUILD_INTERFACE:${UUID4_HEADERS}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_include_directories(uuid4-shared PUBLIC
$<BUILD_INTERFACE:${UUID4_HEADERS}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

# ==============
# configures the install function

# installs the cmake config file (/usr/local/lib/cmake/uuid4/uuid4-config.cmake), which is used by find_package
# installs the lib targets (.a and .dylib to /usr/local/lib)
install(
TARGETS uuid4-static uuid4-shared
EXPORT uuid4-config
NAMESPACE uuid4::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/uuid4
FILE uuid4-config.cmake
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

export(
EXPORT uuid4-config
NAMESPACE uuid4::
FILE uuid4-config.cmake
# installs the headers (/usr/local/include/uuid4)
install(
DIRECTORY ${UUID4_HEADERS}/${RPOJECT_NAME}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
endif()

add_library(uuid4::uuid4-static ALIAS uuid4-static)
add_library(uuid4::uuid4-shared ALIAS uuid4-shared)
# ==============
# ==============

# ==============
if(NOT DEFINED UUID4_AS_SUBPROJECT)
set(UUID4_AS_SUBPROJECT ON)

if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(UUID4_AS_SUBPROJECT OFF)
endif()
endif()

if(NOT UUID4_AS_SUBPROJECT)
export(
PACKAGE uuid4
)

# installs the cmake config file (/usr/local/lib/cmake/uuid4/uuid4-config.cmake), which is used by find_package
install(
EXPORT uuid4-config
NAMESPACE uuid4::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/uuid4
FILE uuid4-config.cmake
)

export(
EXPORT uuid4-config
NAMESPACE uuid4::
FILE uuid4-config.cmake
)
endif()

add_library(uuid4::uuid4-static ALIAS uuid4-static)
add_library(uuid4::uuid4-shared ALIAS uuid4-shared)

# ==============
endif()
4 changes: 4 additions & 0 deletions idf_component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version: "1.0.3"
description: "uuidv4 library"
url: "https://github.com/atsign-foundation/uuid4"
license: "BSD-3-Clause"
JeremyTubongbanua marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 9 additions & 1 deletion src/uuid4.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include <wincrypt.h>
#endif

#if defined(CONFIG_IDF_TARGET_ESP32)
#include "esp_random.h"
#endif

#include "uuid4/uuid4.h"

#if (__STDC_VERSION__ >= 201112L)
Expand Down Expand Up @@ -59,13 +63,17 @@ int uuid4_init(void) {
return UUID4_EFAILURE;
}

#elif defined(CONFIG_IDF_TARGET_ESP32)
for (int i = 0; i < 2; i++) {
seed[i] = ((uint64_t)esp_random() << 32) | esp_random(); // Generate random 64-bit values
}

#else
#error "unsupported platform"
#endif
return UUID4_ESUCCESS;
}


void uuid4_generate(char *dst) {
static const char *template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
static const char *chars = "0123456789abcdef";
Expand Down