Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: C89 adherence
Browse files Browse the repository at this point in the history
luau-project committed Dec 16, 2024
1 parent 75d6287 commit 6f58e95
Showing 7 changed files with 380 additions and 56 deletions.
239 changes: 223 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -11,10 +11,176 @@ 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_short_version}";
- 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 }}")
{
$cc = "gcc";
$c89_src_dir = Join-Path -Path "${{ env.LUA_SRC_DIR }}" -ChildPath "src";
$c89_cflags_name = "MYCFLAGS";
$c89_libs_name = "MYLIBS";
$c89_target = "all";
$c89_macro = "LUA_ANSI";
}
else
{
$cc = "gcc -std=c89";
$c89_src_dir = "${{ env.LUA_SRC_DIR }}";
$c89_cflags_name = "MYCFLAGS";
$c89_libs_name = "SYSLIBS";
$c89_target = "c89";
$c89_macro = "LUA_USE_C89";
}
Add-Content "${{ github.env }}" "CC=${cc}";
Add-Content "${{ github.env }}" "C89_SRC_DIR=${c89_src_dir}";
Add-Content "${{ github.env }}" "C89_CFLAGS_NAME=${c89_cflags_name}";
Add-Content "${{ github.env }}" "C89_LIBS_NAME=${c89_libs_name}";
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.C89_SRC_DIR }}" `
"${{ env.C89_TARGET }}" `
"${{ env.C89_CFLAGS_NAME }}=-DLUA_USE_DLOPEN" `
"${{ env.C89_LIBS_NAME }}=-Wl,-E -ldl" `
"CC=${{ env.CC }}";
- name: Install Lua ${{ matrix.lua-version }}, and set environment variables
run: |
make -C "${{ env.LUA_SRC_DIR }}" `
install `
"INSTALL_TOP=${{ env.LUA_DIR }}";
$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: |
${{ env.CC }} `
"-c" `
"-fPIC" `
"-o" "src/lua-uuid.o" `
"-I${{ env.LUA_INCDIR }}" `
"-Isrc" `
"-I/usr/include/uuid" `
"-D${{ env.C89_MACRO }}" `
"-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" `
"src/lua-uuid.o" `
"-ldl" `
"-luuid";
- 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 +214,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 +241,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 +324,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 +342,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 +372,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 +388,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 +400,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 +444,4 @@ jobs:
$color = (0x1b -as [char]) + "[36m";
Write-Host "Uploading rockspec: ${color}${rockspec}";
luarocks upload $rockspec "--api-key=$env:UPLOAD_KEY" --skip-pack
luarocks upload $rockspec "--temp-key=$env:UPLOAD_KEY" --skip-pack
3 changes: 1 addition & 2 deletions Makefile.macosx
Original file line number Diff line number Diff line change
@@ -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 $@
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <lua.h>``` and ```LUA_UUID_EXPORT``` macro definition to outside of ```__cplusplus``` declarations on ```lua-uuid.h```.
104 changes: 104 additions & 0 deletions rockspecs/lua-uuid-0.0.5-1.rockspec
Original file line number Diff line number Diff line change
@@ -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")
}
}
1 change: 0 additions & 1 deletion rockspecs/lua-uuid-dev-1.rockspec
Original file line number Diff line number Diff line change
@@ -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)"
},
32 changes: 21 additions & 11 deletions src/lua-uuid.c
Original file line number Diff line number Diff line change
@@ -9,10 +9,19 @@
#endif

#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

#include <luaconf.h>
#include <lauxlib.h>
#include <lualib.h>

#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);

51 changes: 25 additions & 26 deletions src/lua-uuid.h
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
#ifndef LUA_UUID_H
#define LUA_UUID_H

#include <lua.h>

#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.h>

LUA_UUID_EXPORT int luaopen_uuid(lua_State *L);

#ifdef __cplusplus
}

0 comments on commit 6f58e95

Please sign in to comment.