# SPDX-License-Identifier: GPL-3.0-or-later
#                                                    -*- mode: makefile -*-
#
#
# This is a simple Makefile, given here as an example. We try here to have
# something simple, which can easily understood (this file is a bit simpler
# than the Makefile generated by the GNU autotools, which is used when
# compile the STklos package, and can be used without a prior STklos installation).
#
# We suppose here that Stklos has been previously installed.
#
#
#           Author: Jeronimo Pellegrini [j_p@aleph0.info]
#    Creation date: 28-May-2024 19:12 (jpellegrini)

# STKLOS_SRC is the directory with the C source of STklos.
# Since this example is in 'examples/c-module', then the
# 'src' subdir is this:
STKLOS_SRC=../../src/

# The following variables are:
# SCHEME_SOURCE -- the Scheme part of the module
# C_INCL        -- C file to be included (generated by stklos-compile)
# C_SOURCE      -- C module source, written by the user
# C_OBJ         -- C object file (intermediate)
# SHARED_OBJ    -- C shared object (loadable library)

SCHEME_SOURCE=string-obfuscate.stk
C_INCL=string-obfuscate-incl.c
C_SOURCE=string-obfuscate.c
C_OBJ=string-obfuscate.o
SHARED_OBJ=string-obfuscate.so

# Instead of making a compact Makefile that uses wildcards and variable names,
# we opted for using the filenames in the Maefile rule heads, and the
# variables above in the rule bodies. This could, perhaps, make it somewhat
# easier for beginners to understand the file.

all: $(SHARED_OBJ)

# We first compile the Scheme file string-obfuscate.stk into a C file
# string-obfuscate-incl.c, so it will be included in the library C file
# later:
string-obfuscate-incl.c: $(SCHEME_SOURCE) $(C_INCL)
	stklos-compile -c --no-time \
	-C \
	--output=$(C_INCL) \
	$(SCHEME_SOURCE)

# Now we compile the C library, which will include string-obfuscate-incl.c.
# The compiler flags are:
# -fpic is of course necessary
# -nostdlib, becaues STklos is already linked against libc
# -c compile only, don't link
string-obfuscate.o: $(C_SOURCE) $(C_INCL)
	$(CC) -g -O2 -fpic -nostdlib \
	-I$(STKLOS_SRC) \
	-c \
	-o $(C_OBJ) \
	$(C_SOURCE)

# And finally, we make it a shared library so STklos will be
# able to load it:
string-obfuscate.so: $(C_OBJ)
	$(LD) -shared  -o $(SHARED_OBJ) $(C_OBJ)

clean:
	rm -f \
	string-obfuscate-incl.c \
	string-obfuscate-incl.o \
	string-obfuscate.o \
	string-obfuscate.so
