Skip to content

Commit

Permalink
Merge pull request #17 from pallene-lang/new-makefile
Browse files Browse the repository at this point in the history
Project restructure and huge Makefile overhaul
  • Loading branch information
singul4ri7y authored Aug 10, 2024
2 parents bbc022f + 5f0c41d commit 91e7fb3
Show file tree
Hide file tree
Showing 22 changed files with 134 additions and 184 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- name: Build
run: |
sudo make install
make all && sudo make install
- name: Install Busted
run: luarocks --local install busted
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.o
*.a
*.so
/pt-run

pt-run
90 changes: 66 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,37 +1,79 @@
# Copyright (c) 2024, The Pallene Developers
# Pallene Tracer is licensed under the MIT license.
# Please refer to the LICENSE and AUTHORS files for details
# SPDX-License-Identifier: MIT
# SPDX-License-Identifier: MIT

CC := gcc
CFLAGS := -DPT_DEBUG -O2 -std=c99 -pedantic -Wall -Wextra
# Where to install our things
PREFIX = /usr/local
BINDIR = $(PREFIX)/bin
INCDIR = $(PREFIX)/include

LUA_DIR := /usr
# Where to find Lua libraries
LUA_PREFIX = /usr/local
LUA_BINDIR = $(LUA_PREFIX)/bin
LUA_INCDIR = $(LUA_PREFIX)/include
LUA_LIBDIR = $(LUA_PREFIX)/lib

INSTALL_DIR := /usr/local
INSTALL_INCDIR := $(INSTALL_DIR)/include
INSTALL_LIBDIR := $(INSTALL_DIR)/lib
INSTALL_BINDIR := $(INSTALL_DIR)/bin
# How to install files
INSTALL= install -p
INSTALL_EXEC= $(INSTALL) -m 0755
INSTALL_DATA= $(INSTALL) -m 0644

.PHONY: install ptracer_header pt-run libptracer uninstall clean
# C compilation flags
CFLAGS = -DPT_DEBUG -O2 -std=c99 -pedantic -Wall -Wextra -Wformat-security
CPPFLAGS = -I$(LUA_INCDIR) -I.
LIBFLAG = -fPIC -shared

install: ptracer_header pt-run
cp pt-run $(INSTALL_BINDIR)
# The -Wl,-E tells the linker to not throw away unused Lua API symbols.
# We need them for Lua modules that are dynamically linked via require
PTRUN_LDFLAGS = -L$(LUA_LIBDIR) -Wl,-E
PTRUN_LDLIBS = -llua -lm

# We need the `ptracer.h` header to be installed first.
ptracer_header:
cp lib/ptracer.h $(INSTALL_INCDIR)
# ===================
# Compilation targets
# ===================

pt-run:
$(CC) $(CFLAGS) src/pt-run/main.c -o pt-run -llua -lm -Wl,-E -L$(LUA_DIR)/lib
.PHONY: library examples tests all install uninstall clean

uninstall:
rm -rf $(INSTALL_INCDIR)/ptracer.h
rm -rf $(INSTALL_BINDIR)/pt-run
library: \
pt-run

clean:
rm -rf examples/*/*.so
rm -rf spec/tracebacks/*/*.so
rm -rf *.so
rm -rf pt-run
examples: library \
examples/fibonacci/fibonacci.so

tests: library \
spec/tracebacks/anon_lua/module.so \
spec/tracebacks/depth_recursion/module.so \
spec/tracebacks/dispatch/module.so \
spec/tracebacks/ellipsis/module.so \
spec/tracebacks/multimod/module_a.so \
spec/tracebacks/multimod/module_b.so \
spec/tracebacks/singular/module.so

all: library examples tests

install:
$(INSTALL_EXEC) pt-run $(BINDIR)
$(INSTALL_DATA) ptracer.h $(INCDIR)

uninstall:
rm -rf $(INCDIR)/ptracer.h
rm -rf $(BINDIR)/pt-run

clean:
rm -rf pt-run examples/*/*.so spec/tracebacks/*/*.so

%.so: %.c
$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(LIBFLAG) $< -o $@

pt-run: pt-run.c ptracer.h
$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(PTRUN_LDFLAGS) $< -o $@ $(PTRUN_LDLIBS)

examples/fibonacci/fibonacci.so: examples/fibonacci/fibonacci.c ptracer.h
spec/tracebacks/anon_lua/module.so: spec/tracebacks/anon_lua/module.c ptracer.h
spec/tracebacks/depth_recursion/module.so: spec/tracebacks/depth_recursion/module.c ptracer.h
spec/tracebacks/dispatch/module.so: spec/tracebacks/dispatch/module.c ptracer.h
spec/tracebacks/ellipsis/module.so: spec/tracebacks/ellipsis/module.c ptracer.h
spec/tracebacks/multimod/module_a.so: spec/tracebacks/multimod/module_a.c ptracer.h
spec/tracebacks/multimod/module_b.so: spec/tracebacks/multimod/module_b.c ptracer.h
spec/tracebacks/singular/module.so: spec/tracebacks/singular/module.c ptracer.h
18 changes: 0 additions & 18 deletions examples/fibonacci/Makefile

This file was deleted.

6 changes: 3 additions & 3 deletions examples/fibonacci/fibonacci.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
/*
* Copyright (c) 2024, The Pallene Developers
* Pallene Tracer is licensed under the MIT license.
* Please refer to the LICENSE and AUTHORS files for details
* SPDX-License-Identifier: MIT
* SPDX-License-Identifier: MIT
*/

#define PT_IMPLEMENTATION
#include <ptracer.h>
#include "ptracer.h"

/* User specific macros when Pallene Tracer debug mode is enabled. */
#ifdef PT_DEBUG
Expand Down
20 changes: 10 additions & 10 deletions src/pt-run/main.c → pt-run.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2024, The Pallene Developers
* Pallene Tracer is licensed under the MIT license.
* Please refer to the LICENSE and AUTHORS files for details
* SPDX-License-Identifier: MIT
* SPDX-License-Identifier: MIT
*/

#include <stdio.h>
Expand All @@ -13,7 +13,7 @@
#include <lualib.h>

#define PT_IMPLEMENTATION
#include <ptracer.h>
#include "ptracer.h"

/* Traceback ellipsis top threshold. How many frames should we print
first to trigger ellipsis? */
Expand All @@ -23,7 +23,7 @@

/* This should always be 2 fewer than top threshold, for symmetry.
Becuase we will always have 2 tail frames lingering around at
at the end which is not captured by '_countlevels'. Lua also
at the end which is not captured by '_countlevels'. Lua also
do it like this. */
#ifndef PT_RUN_TRACEBACK_BOTTOM_THRESHOLD
#define PT_RUN_TRACEBACK_BOTTOM_THRESHOLD 8
Expand All @@ -38,7 +38,7 @@ static void pt_run_help() {
printf("Pallene Tracer runner for call-stack backtrace\n\n");
printf("Arguments:\n"
" lua_script Lua file to debug\n"
" args ... Arguments passed to Lua script\n\n");
" args ... Arguments passed to Lua script\n\n");
printf("Options:\n"
" -h, --help Show this help message and exit\n");
}
Expand Down Expand Up @@ -149,7 +149,7 @@ static void pt_run_dbg_print(const char *buf, bool *ellipsis, int *pframes, int
|| ((nframes - *pframes) <= PT_RUN_TRACEBACK_BOTTOM_THRESHOLD);

if(luai_likely(should_print))
fprintf(stderr, buf);
fprintf(stderr, "%s", buf);
else if(*ellipsis) {
fprintf(stderr, "\n ... (Skipped %d frames) ...\n\n",
nframes - (PT_RUN_TRACEBACK_TOP_THRESHOLD
Expand Down Expand Up @@ -279,7 +279,7 @@ int main(int argc, char **argv) {
if(!strncmp(argv[1], "-", 1)) {
char *match = argv[1] + 1;

if(!strcmp(match, "h") || !strcmp(match, "-help"))
if(!strcmp(match, "h") || !strcmp(match, "-help"))
pt_run_help();
else {
pt_run_usage(stderr);
Expand Down Expand Up @@ -314,17 +314,17 @@ int main(int argc, char **argv) {
}

/* Push arguments to Lua stack. */
for(int i = 2; i < argc; i++)
for(int i = 2; i < argc; i++)
lua_pushstring(L, argv[i]);

/* Moment of truth. */
status = lua_pcall(L, argc - 2, LUA_MULTRET, traceback_fn);
if(status != LUA_OK)
if(status != LUA_OK)
res = EXIT_FAILURE;

finalize:
finalize:
/* Cleanup. */
lua_close(L);
out:
out:
return res;
}
32 changes: 16 additions & 16 deletions lib/ptracer.h → ptracer.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
/*
* Copyright (c) 2024, The Pallene Developers
* Pallene Tracer is licensed under the MIT license.
* Please refer to the LICENSE and AUTHORS files for details
* SPDX-License-Identifier: MIT
* SPDX-License-Identifier: MIT
*/

#ifndef PALLENE_TRACER_H
Expand Down Expand Up @@ -50,16 +50,16 @@
#define PALLENE_TRACER_SETLINE(fnstack, line) pallene_tracer_setline(fnstack, line)
#define PALLENE_TRACER_FRAMEEXIT(fnstack) pallene_tracer_frameexit(fnstack)

#else
#define PALLENE_TRACER_FRAMEENTER(fnstack, frame)
#define PALLENE_TRACER_SETLINE(fnstack, line)
#define PALLENE_TRACER_FRAMEEXIT(fnstack)
#else
#define PALLENE_TRACER_FRAMEENTER(fnstack, frame)
#define PALLENE_TRACER_SETLINE(fnstack, line)
#define PALLENE_TRACER_FRAMEEXIT(fnstack)
#endif // PT_DEBUG

/* Not part of the API. */
#ifdef PT_DEBUG
#define _PALLENE_TRACER_PREPARE_C_FRAME(fn_name, filename, var_name) \
pt_fn_details_t var_name##_details = \
#define _PALLENE_TRACER_PREPARE_C_FRAME(fn_name, filename, var_name) \
pt_fn_details_t var_name##_details = \
PALLENE_TRACER_FN_DETAILS(fn_name, filename); \
pt_frame_t var_name = PALLENE_TRACER_C_FRAME(var_name##_details)

Expand All @@ -70,21 +70,21 @@ pt_frame_t var_name = PALLENE_TRACER_LUA_FRAME(fnptr)
lua_toclose(L, -1)

#else
#define _PALLENE_TRACER_PREPARE_LUA_FRAME(fnptr, var_name)
#define _PALLENE_TRACER_PREPARE_C_FRAME(fn_name, filename, var_name)
#define _PALLENE_TRACER_FINALIZER(L, location)
#define _PALLENE_TRACER_PREPARE_LUA_FRAME(fnptr, var_name)
#define _PALLENE_TRACER_PREPARE_C_FRAME(fn_name, filename, var_name)
#define _PALLENE_TRACER_FINALIZER(L, location)
#endif // PT_DEBUG

/* ---- DATA-STRUCTURE HELPER MACROS ---- */

/* Use this macro to fill in the details structure. */
/* E.U.:
/* E.U.:
pt_fn_details_t det = PALLENE_TRACER_FN_DETAILS("fn_name", "some_mod.c");
*/
#define PALLENE_TRACER_FN_DETAILS(name, fname) \
{ .fn_name = name, .filename = fname }

/* Use this macro to fill in the frame structure as a
/* Use this macro to fill in the frame structure as a
Lua interface frame. */
/* E.U.: `pt_frame_t frame = PALLENE_TRACER_LUA_FRAME(lua_fn);` */
#define PALLENE_TRACER_LUA_FRAME(fnptr) \
Expand All @@ -103,9 +103,9 @@ pt_frame_t var_name = PALLENE_TRACER_LUA_FRAME(fnptr)
/* ---- API HELPER MACROS ---- */

/* Use this macro the bypass some frameenter boilerplates for Lua interface frames. */
/* Note: `location` is where the finalizer object is in the stack, acquired from
/* Note: `location` is where the finalizer object is in the stack, acquired from
`pallene_tracer_init()` function. If the object is passed to Lua C functions as an
upvalue, this should be `lua_upvalueindex(n)`. Otherwise, it should just be a number
upvalue, this should be `lua_upvalueindex(n)`. Otherwise, it should just be a number
denoting the parameter index where the object is found if passed as a plain parameter
to the functon. */
/* The `var_name` indicates the name of the `pt_frame_t` structure variable. */
Expand Down Expand Up @@ -185,7 +185,7 @@ PT_API pt_fnstack_t *pallene_tracer_init(lua_State *L);
/* Pushes a frame to the stack. The frame structure is self-managed for every function. */
static inline void pallene_tracer_frameenter(pt_fnstack_t *fnstack, pt_frame_t *restrict frame) {
/* Have we ran out of stack entries? If we do, stop pushing frames. */
if(luai_likely(fnstack->count < PALLENE_TRACER_MAX_CALLSTACK))
if(luai_likely(fnstack->count < PALLENE_TRACER_MAX_CALLSTACK))
fnstack->stack[fnstack->count] = *frame;

fnstack->count++;
Expand Down
3 changes: 1 addition & 2 deletions run-tests
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@
# ./run-tests
# ./run-tests -k
# ./run-tests spec/traceback_spec.lua

busted --verbose --no-keep-going "$@"
make tests && busted --verbose --no-keep-going "$@"
12 changes: 0 additions & 12 deletions spec/tracebacks/anon_lua/Makefile

This file was deleted.

2 changes: 1 addition & 1 deletion spec/tracebacks/anon_lua/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/* Static use of the library would suffice. */
#define PT_IMPLEMENTATION
#include <ptracer.h>
#include "ptracer.h"

/* Here goes user specifc macros when Pallene Tracer debug mode is active. */
#ifdef PT_DEBUG
Expand Down
12 changes: 0 additions & 12 deletions spec/tracebacks/depth_recursion/Makefile

This file was deleted.

2 changes: 1 addition & 1 deletion spec/tracebacks/depth_recursion/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/* Static use of the library would suffice. */
#define PT_IMPLEMENTATION
#include <ptracer.h>
#include "ptracer.h"

/* Here goes user specific macros when Pallene Tracer debug mode is active. */
#ifdef PT_DEBUG
Expand Down
12 changes: 0 additions & 12 deletions spec/tracebacks/dispatch/Makefile

This file was deleted.

2 changes: 1 addition & 1 deletion spec/tracebacks/dispatch/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/* Static use of the library would suffice. */
#define PT_IMPLEMENTATION
#include <ptracer.h>
#include "ptracer.h"

/* Here goes user specific macros when Pallene Tracer debug mode is active. */
#ifdef PT_DEBUG
Expand Down
12 changes: 0 additions & 12 deletions spec/tracebacks/ellipsis/Makefile

This file was deleted.

Loading

0 comments on commit 91e7fb3

Please sign in to comment.