Skip to content

Commit

Permalink
New makefile
Browse files Browse the repository at this point in the history
1. A single root makefile for everything: library, examples, tests
2. Introduce makefile variables for all compilation flags
3. Add comments to explain the -Wl,-E
4. Use "install" instead of "cp", to set correct permissions.
5. Can run tests without "make install", using local ptracer.h and pt-run.

Fixes #11

ps.: The new filenames changed all the tracebacks. Can we change the tests so
that they don't break so easily?
  • Loading branch information
hugomg committed Aug 9, 2024
1 parent bbc022f commit 32bc5f8
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 170 deletions.
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
CPPFLAGS = -I$(LUA_INCDIR) -Iinclude
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: \
src/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 specs

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

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

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

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

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

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

This file was deleted.

32 changes: 16 additions & 16 deletions lib/ptracer.h → include/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
12 changes: 0 additions & 12 deletions spec/tracebacks/anon_lua/Makefile

This file was deleted.

12 changes: 0 additions & 12 deletions spec/tracebacks/depth_recursion/Makefile

This file was deleted.

12 changes: 0 additions & 12 deletions spec/tracebacks/dispatch/Makefile

This file was deleted.

12 changes: 0 additions & 12 deletions spec/tracebacks/ellipsis/Makefile

This file was deleted.

14 changes: 0 additions & 14 deletions spec/tracebacks/multimod/Makefile

This file was deleted.

12 changes: 0 additions & 12 deletions spec/tracebacks/singular/Makefile

This file was deleted.

Loading

0 comments on commit 32bc5f8

Please sign in to comment.