diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ff1be74..b2a5013 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,10 +11,158 @@ on: - "docs/**" env: - ROCKSPEC_VERSION: 0.0.4 + ROCKSPEC_VERSION: 0.0.5 DEV_ROCKSPEC: lua-uuid-dev-1.rockspec jobs: + c89-build: + name: Build C89 + runs-on: ubuntu-latest + + defaults: + run: + shell: pwsh + + strategy: + matrix: + + lua-version: + - 5.1.5 + - 5.2.4 + - 5.3.6 + - 5.4.7 + + steps: + + - name: Validate Lua version, and set environment variables + run: | + if (-not ("${{ matrix.lua-version }}" -match "^(\d+)\.(\d+)(\.\d+)*$")) + { + Write-Host "Invalid Lua version (X.Y.Z) expected"; + exit 1; + } + + $lua_short_version = "${{ matrix.lua-version }}" -split "\." | + Select-Object -First 2 | + Join-String -Separator "."; + + Add-Content "${{ github.env }}" "LUA_SHORT_VERSION=${lua_source_dir}"; + + - name: Set environment variable to hold the rockspec name + run: | + if ("${{ github.repository }}" -eq "luau-project/lua-uuid" -and "${{ github.ref_name }}" -eq "v${{ env.ROCKSPEC_VERSION }}" -and "${{ github.ref }}" -eq "refs/tags/v${{ env.ROCKSPEC_VERSION }}") + { + Add-Content "${{ github.env }}" "ROCKSPEC=lua-uuid-${{ env.ROCKSPEC_VERSION }}-1.rockspec"; + } + else + { + Add-Content "${{ github.env }}" "ROCKSPEC=${{ env.DEV_ROCKSPEC }}"; + } + + - name: Checkout + uses: actions/checkout@v4 + with: + path: lua-uuid + + - name: Install libuuid-dev + run: sudo apt install -y uuid-dev + + - name: Download and extract Lua ${{ matrix.lua-version }} source code, and set environment variables + run: | + $targz = "lua-${{ matrix.lua-version }}.tar.gz"; + $targz_path = Join-Path -Path "${{ runner.temp }}" -ChildPath $targz; + + Invoke-WebRequest -Uri "https://lua.org/ftp/${targz}" -OutFile "$targz_path"; + tar -C "${{ runner.temp }}" -xf "$targz_path"; + + $lua_source_dir = Join-Path -Path "${{ runner.temp }}" -ChildPath "lua-${{ matrix.lua-version }}"; + + if (-not (Test-Path $lua_source_dir)) + { + $color = (0x1b -as [char]) + "[36m"; + + Write-Host "Unable to find Lua source code directory: ${color}${lua_source_dir}"; + + exit 1; + } + + $install_dir = Join-Path -Path "${{ runner.temp }}" -ChildPath "installed-lua-${{ matrix.lua-version }}"; + + Add-Content "${{ github.env }}" "LUA_SRC_DIR=${lua_source_dir}"; + Add-Content "${{ github.env }}" "LUA_DIR=${install_dir}"; + + - name: Set environment variables depending on Lua version for C89 make targets + run: | + $c89_target = ""; + $c89_macro = ""; + + if ("5.1", "5.2" -contains "${{ env.LUA_SHORT_VERSION }}") + { + $c89_target = "ansi"; + $c89_macro = "LUA_ANSI"; + } + else + { + $c89_target = "c89"; + $c89_macro = "LUA_USE_C89"; + } + + Add-Content "${{ github.env }}" "C89_TARGET=${c89_target}"; + Add-Content "${{ github.env }}" "C89_MACRO=${c89_macro}"; + + - name: Build Lua ${{ matrix.lua-version }} + run: | + make -C "${{ env.LUA_SRC_DIR }}" "${{ env.C89_TARGET }}" "MYCFLAGS=-DLUA_USE_DLOPEN" "SYSLIBS=-Wl,-E -ldl" "CC=gcc -std=c89"; + + - name: Install Lua ${{ matrix.lua-version }}, and set environment variables + run: | + make -C "${{ env.LUA_SRC_DIR }}" install "INSTALL_TOP=${{ env.LUA_DIR }}" "MYCFLAGS=-DLUA_USE_DLOPEN" "SYSLIBS=-Wl,-E -ldl"; + + $lua_bindir = Join-Path -Path "${{ env.LUA_DIR }}" -ChildPath "bin"; + $lua_incdir = Join-Path -Path "${{ env.LUA_DIR }}" -ChildPath "include"; + + Add-Content "${{ github.path }}" "${lua_bindir}"; + Add-Content "${{ github.env }}" "LUA_INCDIR=${lua_incdir}"; + + - name: Compile lua-uuid + working-directory: lua-uuid + run: | + gcc "-std=c89" ` + "-c" ` + "-fPIC" ` + "-o" "src/lua-uuid.o" ` + "-I${{ env.LUA_INCDIR }}" ` + "-Isrc" ` + "-I/usr/include/uuid" ` + "-D${{ env.C89_TARGET }}" ` + "-DLUA_USE_DLOPEN" ` + "-DLUA_UUID_BUILD_SHARED" ` + "-DLUA_UUID_USE_LIBUUID" ` + "src/lua-uuid.c"; + + - name: Link lua-uuid + working-directory: lua-uuid + run: | + gcc "-shared" ` + "-o" "lua-uuid.so" ` + "-ldl" ` + "-luuid" ` + "src/lua-uuid.o"; + + - name: Run samples + working-directory: lua-uuid + run: | + Get-ChildItem "samples" -Recurse -File | + Where-Object Extension -EQ ".lua" | + Select-Object -ExpandProperty FullName | + Foreach-Object { + $color = (0x1b -as [char]) + "[36m"; + + Write-Host "Running sample file: ${color}$_"; + + lua "$_"; + }; + build: name: Build runs-on: ${{ matrix.os }} @@ -48,7 +196,7 @@ jobs: run: | if ("${{ github.repository }}" -eq "luau-project/lua-uuid" -and "${{ github.ref_name }}" -eq "v${{ env.ROCKSPEC_VERSION }}" -and "${{ github.ref }}" -eq "refs/tags/v${{ env.ROCKSPEC_VERSION }}") { - Add-Content "${{ github.env }}" "ROCKSPEC=lua-uuid-${{ env.ROCKSPEC_VERSION }}-0.rockspec"; + Add-Content "${{ github.env }}" "ROCKSPEC=lua-uuid-${{ env.ROCKSPEC_VERSION }}-1.rockspec"; } else { @@ -75,6 +223,20 @@ jobs: - name: Setup LuaRocks uses: luarocks/gh-actions-luarocks@v5 + + - name: Lint rockspecs + working-directory: lua-uuid + run: | + Get-ChildItem . -Recurse -File | + Where-Object Extension -Eq ".rockspec" | + Select-Object -ExpandProperty FullName | + Foreach-Object { + $color = (0x1b -as [char]) + "[36m"; + + Write-Host "Linting rockspec: ${color}$_"; + + luarocks lint "$_"; + } - name: Build lua-uuid working-directory: lua-uuid @@ -144,7 +306,7 @@ jobs: run: | if [[ "${{ github.repository }}" == "luau-project/lua-uuid" ]] && [[ "${{ github.ref_name }}" == "v${{ env.ROCKSPEC_VERSION }}" ]] && [[ "${{ github.ref }}" == "refs/tags/v${{ env.ROCKSPEC_VERSION }}" ]]; then - echo "ROCKSPEC=lua-uuid-${{ env.ROCKSPEC_VERSION }}-0.rockspec" >> "${{ github.env }}"; + echo "ROCKSPEC=lua-uuid-${{ env.ROCKSPEC_VERSION }}-1.rockspec" >> "${{ github.env }}"; else echo "ROCKSPEC=${{ env.DEV_ROCKSPEC }}" >> "${{ github.env }}"; fi; @@ -162,13 +324,22 @@ jobs: with: path: lua-uuid + - name: Lint rockspecs + working-directory: lua-uuid + run: | + for rockspec in rockspecs/*.rockspec; + do + echo -e "Linting rockspec: \e[36m${rockspec}\e[0m"; + luarocks lint "${rockspec}"; + done; + - name: Build lua-uuid working-directory: lua-uuid run: | - rockspec="rockspecs/${{ env.ROCKSPEC }}" - echo -e "Building rockspec: \e[36m${rockspec}\e[0m" + rockspec="rockspecs/${{ env.ROCKSPEC }}"; + echo -e "Building rockspec: \e[36m${rockspec}\e[0m"; - luarocks make ${rockspec} + luarocks make ${rockspec}; - name: Run samples working-directory: lua-uuid @@ -183,8 +354,9 @@ jobs: upload-rockspec: name: Upload rockspec runs-on: ubuntu-latest - if: ${{ github.repository == 'luau-project/lua-uuid' && startsWith(github.ref, 'refs/tags/') }} + if: ${{ github.repository == 'luau-project/lua-uuid' && github.ref_type == 'tag' }} needs: + - c89-build - build - msys2-build @@ -198,7 +370,7 @@ jobs: run: | if ("${{ github.repository }}" -eq "luau-project/lua-uuid" -and "${{ github.ref_name }}" -eq "v${{ env.ROCKSPEC_VERSION }}" -and "${{ github.ref }}" -eq "refs/tags/v${{ env.ROCKSPEC_VERSION }}") { - Add-Content "${{ github.env }}" "ROCKSPEC=lua-uuid-${{ env.ROCKSPEC_VERSION }}-0.rockspec"; + Add-Content "${{ github.env }}" "ROCKSPEC=lua-uuid-${{ env.ROCKSPEC_VERSION }}-1.rockspec"; } else { @@ -210,20 +382,37 @@ jobs: with: path: lua-uuid - - name: Install libuuid-dev - run: sudo apt install -y uuid-dev libssl-dev - - name: Setup Lua uses: luarocks/gh-actions-lua@v10 - name: Setup LuaRocks uses: luarocks/gh-actions-luarocks@v5 - - name: Install dependencies + - name: Make sure that tags from GitHub and rockspec are equal + run: | + $rockspec = Get-ChildItem . -Recurse -File | + Where-Object Name -EQ "${{ env.ROCKSPEC }}" | + Select-Object -ExpandProperty FullName -First 1; + + $rockspec_tag = lua -e "dofile(arg[0]); io.write(source.tag);" -- "${rockspec}"; + $github_tag = "v${{ github.ref_name }}"; + + if ("${rockspec_tag}" -ne "${github_tag}") + { + $color_msg = (0x1b -as [char]) + "[31m"; + $color_reset = (0x1b -as [char]) + "[0m"; + $color_tag = (0x1b -as [char]) + "[33m"; + + Write-Host "${color_msg}Tag mismatch${color_reset}: GitHub tag (${color_tag}${github_tag}${color_reset}) != rockspec tag (${color_tag}${rockspec_tag}${color_reset})"; + exit 1; + } + + - name: Install LuaRocks dependencies to upload run: | - luarocks install dkjson - luarocks install luasocket - luarocks install luasec + sudo apt install -y uuid-dev libssl-dev; + luarocks install dkjson; + luarocks install luasocket; + luarocks install luasec; - name: Upload rockspec to LuaRocks working-directory: lua-uuid @@ -237,4 +426,4 @@ jobs: $color = (0x1b -as [char]) + "[36m"; Write-Host "Uploading rockspec: ${color}${rockspec}"; - luarocks upload $rockspec "--api-key=$env:UPLOAD_KEY" --skip-pack \ No newline at end of file + luarocks upload $rockspec "--temp-key=$env:UPLOAD_KEY" --skip-pack \ No newline at end of file diff --git a/Makefile.macosx b/Makefile.macosx index 72ffc62..ce94610 100644 --- a/Makefile.macosx +++ b/Makefile.macosx @@ -5,7 +5,6 @@ LIBFLAG_EXTRA = -framework CoreFoundation LUA_DIR = /usr/local LUA_INCDIR = $(LUA_DIR)/include -LUA_LIBDIR = $(LUA_DIR)/lib LUA_VERSION = 5.1 INSTALL_PREFIX = /usr/local @@ -14,7 +13,7 @@ INSTALL_LIBDIR = $(INSTALL_PREFIX)/lib/lua/$(LUA_VERSION) all: src/lua-uuid.$(LIB_EXTENSION) src/lua-uuid.$(LIB_EXTENSION): src/lua-uuid.$(OBJ_EXTENSION) - $(CC) $(LIBFLAG_EXTRA) $(LIBFLAG) -o $@ -L$(LUA_LIBDIR) $< + $(CC) $(LIBFLAG_EXTRA) $(LIBFLAG) -o $@ $< src/lua-uuid.$(OBJ_EXTENSION): src/lua-uuid.c $(CC) -c $(CFLAGS_EXTRA) $(CFLAGS) -I$(LUA_INCDIR) $< -o $@ diff --git a/README.md b/README.md index 1fede35..b8694cb 100644 --- a/README.md +++ b/README.md @@ -213,6 +213,12 @@ print(id3:isnil()) ## Change log +* v0.0.5: + * Adhering to C89; + * Added a CI job to make sure that this library conforms to C89; + * Linting rockspecs on CI; + * Minor changes on the makefile for macOS / iOS; + * The naming format for the published rockspecs changed from ```vX.Y.Z-0``` to ```vX.Y.Z-1```. * v0.0.4: * Added support for BSD (e.g: FreeBSD, NetBSD, OpenBSD and DragonFly); * Moved ```#include ``` and ```LUA_UUID_EXPORT``` macro definition to outside of ```__cplusplus``` declarations on ```lua-uuid.h```. diff --git a/rockspecs/lua-uuid-0.0.5-1.rockspec b/rockspecs/lua-uuid-0.0.5-1.rockspec new file mode 100644 index 0000000..e2a1625 --- /dev/null +++ b/rockspecs/lua-uuid-0.0.5-1.rockspec @@ -0,0 +1,104 @@ +package = "lua-uuid" +version = "0.0.5-1" + +source = { + url = "git://github.com/luau-project/lua-uuid.git", + tag = "v0.0.5" +} + +description = { + homepage = "https://github.com/luau-project/lua-uuid", + summary = [[Lightweight, native GUID / UUID library for Lua]], + detailed = [=[ +lua-uuid is a lightweight, native library for Lua (5.1 and newer) to deal with Universally Unique Id (UUID). + +* On Linux and BSD, it uses libuuid to generate UUIDs; +* On Windows, it uses the WINAPI rpcrt4 library; +* On macOS / iOS, it uses the CoreFoundation framework. + +Visit the GitHub repository for more information.]=], + license = "MIT" +} + +supported_platforms = { "linux", "windows", "cygwin", "macosx", "bsd" } + +dependencies = { + "lua >= 5.1" +} + +external_dependencies = { + platforms = { + linux = { + ["UUID"] = { + header = "uuid/uuid.h" + } + }, + bsd = { + ["UUID"] = { + header = "uuid/uuid.h" + } + } + } +} + +local function build_plat(plat) + if (plat == "linux" or plat == "bsd") then + return { + type = "builtin", + modules = { + ["lua-uuid"] = { + sources = { "src/lua-uuid.c" }, + libraries = { "uuid" }, + defines = { "LUA_UUID_BUILD_SHARED", "LUA_UUID_USE_LIBUUID" }, + incdirs = { "src", "$(UUID_INCDIR)" }, + libdirs = { "$(UUID_LIBDIR)" } + } + } + } + elseif (plat == "windows" or plat == "cygwin") then + return { + type = "builtin", + modules = { + ["lua-uuid"] = { + sources = { "src/lua-uuid.c" }, + libraries = { "rpcrt4" }, + defines = { "LUA_UUID_BUILD_SHARED", "LUA_UUID_USE_WIN32" }, + incdirs = { "src" }, + libdirs = { } + } + } + } + elseif (plat == "macosx") then + return { + type = "make", + makefile = "Makefile.macosx", + build_variables = { + CFLAGS = "$(CFLAGS)", + LIBFLAG = "$(LIBFLAG)", + CFLAGS_EXTRA = "-DLUA_UUID_BUILD_SHARED -DLUA_UUID_USE_APPLE", + LIBFLAG_EXTRA = "-framework CoreFoundation", + LUA_INCDIR = "$(LUA_INCDIR)", + OBJ_EXTENSION = "$(OBJ_EXTENSION)", + LIB_EXTENSION = "$(LIB_EXTENSION)" + }, + install_variables = { + INSTALL_PREFIX = "$(PREFIX)", + INSTALL_LIBDIR = "$(LIBDIR)", + LUA_VERSION = "$(LUA_VERSION)", + LIB_EXTENSION = "$(LIB_EXTENSION)" + } + } + else + error("Unknown platform", 2) + end +end + +build = { + platforms = { + linux = build_plat("linux"), + windows = build_plat("windows"), + cygwin = build_plat("cygwin"), + macosx = build_plat("macosx"), + bsd = build_plat("bsd") + } +} diff --git a/rockspecs/lua-uuid-dev-1.rockspec b/rockspecs/lua-uuid-dev-1.rockspec index 8765546..daf1202 100644 --- a/rockspecs/lua-uuid-dev-1.rockspec +++ b/rockspecs/lua-uuid-dev-1.rockspec @@ -77,7 +77,6 @@ local function build_plat(plat) CFLAGS_EXTRA = "-DLUA_UUID_BUILD_SHARED -DLUA_UUID_USE_APPLE", LIBFLAG_EXTRA = "-framework CoreFoundation", LUA_INCDIR = "$(LUA_INCDIR)", - LUA_LIBDIR = "$(LUA_INCDIR)/../lib", OBJ_EXTENSION = "$(OBJ_EXTENSION)", LIB_EXTENSION = "$(LIB_EXTENSION)" }, diff --git a/src/lua-uuid.c b/src/lua-uuid.c index 879fb59..bbbed88 100644 --- a/src/lua-uuid.c +++ b/src/lua-uuid.c @@ -9,10 +9,19 @@ #endif #include + +#ifdef __cplusplus +extern "C" { +#endif + #include #include #include +#ifdef __cplusplus +} +#endif + typedef struct tagLuaUuid { #if defined(LUA_UUID_USE_WIN32) @@ -33,11 +42,13 @@ static LuaUuid *lua_uuid_check(lua_State *L, int index) return (LuaUuid *)ud; } -// The following function -// was copied from Lua 5.4 -// source code in order -// to provide compatibility -// to Lua 5.1 and Lua 5.2 +/* +** The following function +** was copied from Lua 5.4 +** source code in order +** to provide compatibility +** to Lua 5.1 and Lua 5.2 +*/ static void *lua_uuid_testudata(lua_State *L, int ud, const char *tname) { #if LUA_VERSION_NUM == 501 || LUA_VERSION_NUM == 502 @@ -140,9 +151,9 @@ static int lua_uuid_parse(lua_State *L) if (ud == NULL) { #if defined(LUA_UUID_USE_WIN32) - // do nothing + /* do nothing */ #elif defined(LUA_UUID_USE_LIBUUID) - // do nothing + /* do nothing */ #elif defined(LUA_UUID_USE_APPLE) CFRelease(data); #endif @@ -312,9 +323,9 @@ static int lua_uuid_equal(lua_State *L) static int lua_uuid_gc(lua_State *L) { #if defined(LUA_UUID_USE_WIN32) - // do nothing + /* do nothing */ #elif defined(LUA_UUID_USE_LIBUUID) - // do nothing + /* do nothing */ #elif defined(LUA_UUID_USE_APPLE) LuaUuid *uuid = lua_uuid_check(L, 1); CFRelease(uuid->data); @@ -342,8 +353,7 @@ static const luaL_Reg lua_uuid_member_functions[] = { { NULL, NULL } }; -LUA_UUID_EXPORT -int luaopen_uuid(lua_State *L) +LUA_UUID_EXPORT int luaopen_uuid(lua_State *L) { lua_createtable(L, 0, 0); diff --git a/src/lua-uuid.h b/src/lua-uuid.h index 41288c1..95d0b1e 100644 --- a/src/lua-uuid.h +++ b/src/lua-uuid.h @@ -1,40 +1,39 @@ #ifndef LUA_UUID_H #define LUA_UUID_H -#include - #ifdef LUA_UUID_BUILD_STATIC - #define LUA_UUID_EXPORT +#define LUA_UUID_EXPORT #else - #ifdef LUA_UUID_BUILD_SHARED - #if defined(_WIN32) - #if defined(__GNUC__) || defined(__MINGW32__) - #define LUA_UUID_EXPORT __attribute__((dllexport)) - #else - #define LUA_UUID_EXPORT __declspec(dllexport) - #endif - #else - #define LUA_UUID_EXPORT __attribute__((visibility("default"))) - #endif - #else - #if defined(_WIN32) - #if defined(__GNUC__) || defined(__MINGW32__) - #define LUA_UUID_EXPORT __attribute__((dllimport)) - #else - #define LUA_UUID_EXPORT __declspec(dllimport) - #endif - #else - #define LUA_UUID_EXPORT - #endif - #endif +#ifdef LUA_UUID_BUILD_SHARED /* { */ +#if defined(_WIN32) /* { */ +#if defined(__GNUC__) || defined(__MINGW32__) /* { */ +#define LUA_UUID_EXPORT __attribute__((dllexport)) +#else /* }{ */ +#define LUA_UUID_EXPORT __declspec(dllexport) +#endif /* } */ +#else /* }{ */ +#define LUA_UUID_EXPORT __attribute__((visibility("default"))) +#endif /* } */ +#else /* }{ */ +#if defined(_WIN32) /* { */ +#if defined(__GNUC__) || defined(__MINGW32__) /* { */ +#define LUA_UUID_EXPORT __attribute__((dllimport)) +#else /* }{ */ +#define LUA_UUID_EXPORT __declspec(dllimport) +#endif /* } */ +#else /* }{ */ +#define LUA_UUID_EXPORT +#endif /* } */ +#endif /* } */ #endif #ifdef __cplusplus extern "C" { #endif -LUA_UUID_EXPORT -int luaopen_uuid(lua_State *L); +#include + +LUA_UUID_EXPORT int luaopen_uuid(lua_State *L); #ifdef __cplusplus }