lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



Hi,

when building my own libraries I found it cumbersome to set up some framework
that allows me to run my stuff and and not install it locally which would have
contaminated the local installation.  Instead I used a custom patched version
of Lua that extended the package path to something relative of the Lua
executable (Windows allows that out of the box) and run it this way.  All this
was orchestrated by a rather magical Makefile.  It seemed cool at the time,
elegant even, but in all honesty it was pretty convoluted.

So I set up a Dockerfile that installs Lua(5.3) and mounts your current
directory into /build.  The inside of the docker container mimics being
installed like within a Linux distro.  You can modify the Dockerfile to use
Lua-5.4-work1 for example instead.  The mount of the local directory is
bind-based, so all editing can still happen on the host, then compile, install
within the container.  Debugging isn't really figured out yet.  Comments and
ideas are welcome, I hope someone finds it useful.

All attached files are meant to live inside the root directory of your Lua
project.

-tobbik
diff --git a/Makefile b/Makefile
index 7fa91c8..dccf485 100644
--- a/Makefile
+++ b/Makefile
@@ -52,7 +52,7 @@ R= $V.0
 all:	$(PLAT)
 
 $(PLATS) clean:
-	cd src && $(MAKE) $@
+	cd src && $(MAKE) $@ V=$(V) R=$(R)
 
 test:	dummy
 	src/lua -v
diff --git a/src/Makefile b/src/Makefile
index 2e7a412..fa5769f 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -29,6 +29,7 @@ MYOBJS=
 PLATS= aix bsd c89 freebsd generic linux macosx mingw posix solaris
 
 LUA_A=	liblua.a
+LUA_SO= liblua.so
 CORE_O=	lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o \
 	lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o \
 	ltm.o lundump.o lvm.o lzio.o
@@ -43,7 +44,7 @@ LUAC_T=	luac
 LUAC_O=	luac.o
 
 ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O)
-ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T)
+ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T) $(LUA_SO)
 ALL_A= $(LUA_A)
 
 # Targets start here.
@@ -59,6 +60,12 @@ $(LUA_A): $(BASE_O)
 	$(AR) $@ $(BASE_O)
 	$(RANLIB) $@
 
+$(LUA_SO): $(CORE_O) $(LIB_O)
+	$(CC) -shared -ldl -Wl,-soname,$(LUA_SO).$(V) -o $@.$(R) $? -lm $(MYLDFLAGS)
+	ln -sf $(LUA_SO).$(R) $(LUA_SO).$(V)
+	ln -sf $(LUA_SO).$(R) $(LUA_SO)
+
+
 $(LUA_T): $(LUA_O) $(LUA_A)
 	$(CC) -o $@ $(LDFLAGS) $(LUA_O) $(LUA_A) $(LIBS)
 
diff --git a/src/luaconf.h b/src/luaconf.h
index fd28d21..e2662cc 100644
--- a/src/luaconf.h
+++ b/src/luaconf.h
@@ -175,7 +175,7 @@
 
 #else			/* }{ */
 
-#define LUA_ROOT	"/usr/local/"
+#define LUA_ROOT	"/usr/"
 #define LUA_LDIR	LUA_ROOT "share/lua/" LUA_VDIR "/"
 #define LUA_CDIR	LUA_ROOT "lib/lua/" LUA_VDIR "/"
 #define LUA_PATH_DEFAULT  \
V=%VER%
R=%REL%

prefix=/usr
INSTALL_BIN=${prefix}/bin
INSTALL_INC=${prefix}/include
INSTALL_LIB=${prefix}/lib
INSTALL_MAN=${prefix}/man/man1
INSTALL_LMOD=${prefix}/share/lua/${V}
INSTALL_CMOD=${prefix}/lib/lua/${V}
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: Lua
Description: An Extensible Extension Language
Version: ${R}
Requires: 
Libs: -L${libdir} -llua -lm
Cflags: -I${includedir}
FROM       alpine:latest
MAINTAINER Tobias Kieslich

ENV LVER=5.3 LREL=4 LURL=http://www.lua.org/ftp/

# this is a dev container allowing to rebuild and rebuildd and rebuild inside

# Build dependencies. It might be beneficial to add gdb and some others here.
# This is not mean to be a slim container anyways...
RUN apk update && \
    apk --no-cache add \
    build-base linux-headers git bash unzip curl readline readline-dev clang && \
    mkdir /build

# Build a reasonably vanilla lua version. This models the archlinux package
COPY liblua.so.patch /tmp/
COPY lua.pc /tmp/
RUN cd /tmp && \
    curl -o lua.tgz ${LURL}/lua-${LVER}.${LREL}.tar.gz && \
    tar xzvf lua.tgz && mv lua-${LVER}.${LREL} lua && cd lua && \
    patch -p1 -i ../liblua.so.patch && \
    sed "s/%VER%/${LVER}/g;s/%REL%/${LVER}.${LREL}/g" ../lua.pc > lua.pc && \
    make MYCFLAGS="-fPIC -fbuiltin -g -O0" linux && \
    make \
      TO_LIB="liblua.a liblua.so liblua.so.${LVER} liblua.so.${LVER}.${LREL}" \
      INSTALL_DATA='cp -d' \
      INSTALL_TOP=/usr \
      INSTALL_MAN=/usr/share/man/man1 \
      install && \
    ln -sf /usr/bin/lua   /usr/bin/lua${LVER} && \
    ln -sf /usr/bin/luac  /usr/bin/luac${LVER} && \
    ln -sf /usr/lib/liblua.so.${LVER}.${LREL} /usr/lib/liblua${LVER}.so && \
    install -Dm644 lua.pc /usr/lib/pkgconfig/lua53.pc && \
    ln -sf /usr/lib/pkgconfig/lua53.pc /usr/lib/pkgconfig/lua.pc

# vim: ts=3 sw=3 st=3 sts=3 sta noet tw=80 list
#
# \file      Makefile
#            Basic makefile for your project to be developed in docker.
#            It mounts your current source directory into a docker container
#            and allows you to compile and install it within there.  That keeps
#            your local system clean.
# \author    tkieslich

LVER=5.3

PLAT=linux
MYCFLAGS=" -O2"

CC=gcc
LD=gcc

# Docker build specific
DOCKER=$(shell which docker)
DOCKERPS=$(DOCKER) ps --format "table {{.Names}}"
IMAGE=lua$(shell echo $(LVER) | sed "s/\.//")

# These targets can be called from within the container.

all:
	# whatever it takes to compile or prepare your library

install:
	# do your own logic here. Usually copy to /usr/shar/lua/...

clean:
	# do your own logic here.

# ############################################################################
# Docker for development -- anything from here is meant to be called from the
# host, NOT from within the container.

# Build and run the image. It throws you into the container where you would 
# basically run `make` and `make install`.  This will compile your lib and copy
# everything into the container.  When you exit, the container stops.  Restart
# with `make docker-start`.
docker-create: $(DOCKER) get-tz
	$(DOCKER) build  --tag  $(IMAGE) -f Dockerfile .
	$(DOCKER) run    -i -t \
	  --name   $(IMAGE) \
	  --mount  src="$(CURDIR)",target=/build,type=bind \
	  $(IMAGE)dev \
	  /bin/bash

# restart an existing container
docker-start:
	$(DOCKER) start -i $(IMAGE)dev

# remove container
docker-clean:
	$(DOCKER) rm $(IMAGE)dev

# remove image
docker-pristine:
	$(DOCKER) image rm $(IMAGE)dev

docker-rinse:
	$(MAKE) docker-clean
	$(MAKE) docker-pristine