242 lines
5.7 KiB
Makefile
242 lines
5.7 KiB
Makefile
@{define:projectname:luabins}
|
|
@{define:test-dir:test/}
|
|
@{define:source-dir:src/}
|
|
@{define:sharedlib:$(LIBDIR)/$(SONAME)}
|
|
@{define:staticlib:$(LIBDIR)/$(ANAME)}
|
|
@{define-table:release-info:
|
|
{suffix="";
|
|
libdir="$(LIBDIR)";objprefix="$(OBJDIR)/";
|
|
cflags="";
|
|
CC="$(CC)";
|
|
LD="$(LD)";
|
|
};
|
|
}
|
|
@{define-table:std-info:
|
|
{suffix="c89";
|
|
libdir="$(TMPDIR)/c89";objprefix="$(OBJDIR)/c89-";
|
|
cflags="-Werror -Wall -Wextra -pedantic -x c -std=c89";
|
|
CC="$(CC)";
|
|
LD="$(LD)";
|
|
};
|
|
{suffix="c99";
|
|
libdir="$(TMPDIR)/c99";objprefix="$(OBJDIR)/c99-";
|
|
cflags="-Werror -Wall -Wextra -pedantic -x c -std=c99";
|
|
CC="$(CC)";
|
|
LD="$(LD)";
|
|
};
|
|
{suffix="c++98";
|
|
libdir="$(TMPDIR)/c++98";objprefix="$(OBJDIR)/c++98-";
|
|
cflags="-Werror -Wall -Wextra -pedantic -x c++ -std=c++98";
|
|
CC="$(CXX)";
|
|
LD="$(LDXX)";
|
|
};
|
|
}
|
|
@{define-dep:dep-objects:dep-template::@{source-dir}}
|
|
@{define-dep:test-dep-objects:test-dep-template:-I@{source-dir}:@{test-dir}}
|
|
|
|
## CONFIGURATION ##############################################################
|
|
|
|
ifeq ($(shell uname),Darwin)
|
|
LUA_DIR := /usr/local
|
|
LUA_LIBDIR := $(LUA_DIR)/lib/lua/5.1
|
|
LUA_INCDIR := $(LUA_DIR)/include
|
|
LUALIB := lua
|
|
else
|
|
# Assuming Ubuntu
|
|
LUA_LIBDIR := /usr/lib
|
|
LUA_INCDIR := /usr/include/lua5.1
|
|
LUALIB := lua5.1
|
|
endif
|
|
|
|
PROJECTNAME := @{projectname}
|
|
|
|
SONAME := $(PROJECTNAME).so
|
|
ANAME := lib$(PROJECTNAME).a
|
|
HNAME := $(PROJECTNAME).h
|
|
TESTNAME := $(PROJECTNAME)-test
|
|
TESTLUA := test.lua
|
|
|
|
LUA := lua
|
|
CP := cp
|
|
RM := rm -f
|
|
RMDIR := rm -df
|
|
MKDIR := mkdir -p
|
|
CC := gcc
|
|
LD := gcc
|
|
AR := ar rcu
|
|
RANLIB := ranlib
|
|
ECHO := @echo
|
|
TOUCH := touch
|
|
|
|
# Needed for tests only
|
|
CXX := g++
|
|
LDXX := g++
|
|
|
|
OBJDIR := ./obj
|
|
TMPDIR := ./tmp
|
|
INCDIR := ./include
|
|
LIBDIR := ./lib
|
|
|
|
HFILE := $(INCDIR)/$(HNAME)
|
|
|
|
CFLAGS += -O2 -Wall -I$(LUA_INCDIR)
|
|
LDFLAGS += -L$(LUA_LIBDIR)
|
|
|
|
# Tested on OS X and Ubuntu
|
|
SOFLAGS :=
|
|
ifeq ($(shell uname),Darwin)
|
|
SOFLAGS += -dynamiclib -undefined dynamic_lookup
|
|
else
|
|
CFLAGS += -fPIC
|
|
SOFLAGS += -shared
|
|
LDFLAGS += -ldl
|
|
RMDIR := rm -rf
|
|
endif
|
|
|
|
CFLAGS += $(MYCFLAGS)
|
|
LDFLAGS += $(MYLDFLAGS)
|
|
|
|
## MAIN TARGETS ###############################################################
|
|
|
|
@{define-table:.PHONY:}
|
|
|
|
@{insert:.PHONY:all}
|
|
all: @{sharedlib} @{staticlib} $(HFILE)
|
|
|
|
@{insert:.PHONY:clean}
|
|
clean: cleanlibs cleantest
|
|
$(RM) $(HFILE)
|
|
|
|
@{insert:.PHONY:install}
|
|
install: @{sharedlib}
|
|
# Note header and static library are not copied anywhere
|
|
$(CP) @{sharedlib} $(LUA_LIBDIR)/$(SONAME)
|
|
|
|
$(HFILE):
|
|
$(CP) src/$(HNAME) $(HFILE)
|
|
|
|
@{--:-------------------------------------------------------------------------}
|
|
|
|
@{define:objects-macro:
|
|
|
|
@{insert:.PHONY:clean%<prefix>objects@{suffix}}
|
|
clean%<prefix>objects@{suffix}:
|
|
$(RM) %<objects>
|
|
|
|
%<dep>
|
|
|
|
}
|
|
|
|
@{define:lib-targets:
|
|
|
|
@{define-fill:objects:@{fill-template:@{dep-objects}:}}
|
|
|
|
@{insert:.PHONY:cleanlibs@{suffix}}
|
|
cleanlibs@{suffix}: cleanobjects@{suffix}
|
|
$(RM) @{sharedlib}
|
|
$(RM) @{staticlib}
|
|
|
|
@{sharedlib}: @{objects}
|
|
$(MKDIR) @{libdir}
|
|
@{LD} -o $@ @{objects} $(LDFLAGS) $(SOFLAGS)
|
|
|
|
@{staticlib}: @{objects}
|
|
$(MKDIR) @{libdir}
|
|
$(AR) $@ @{objects}
|
|
$(RANLIB) $@
|
|
|
|
@{--: note implicit objprefix parameter }
|
|
# objects@{suffix}:
|
|
@{fill-macro:objects-macro:%<>:
|
|
prefix="",
|
|
objects=@{lua-escape:@{objects}};
|
|
dep=@{lua-escape:@{fill-template:dep-template:}};
|
|
}
|
|
|
|
}
|
|
|
|
@{define:std-targets:
|
|
|
|
## ----- Begin @{suffix} -----
|
|
|
|
@{define-fill:cflags-orig:@{cflags}}
|
|
@{define-fill:cflags:@{cflags} -I@{source-dir}}
|
|
|
|
@{define-fill:sharedlib:@{libdir}/$(SONAME)}
|
|
@{define-fill:staticlib:@{libdir}/$(ANAME)}
|
|
@{define-fill:objects:@{fill-template:@{test-dep-objects}:}}
|
|
|
|
@{insert:.PHONY:test@{suffix}}
|
|
test@{suffix}: lua-tests@{suffix} c-tests@{suffix}
|
|
|
|
@{insert:.PHONY:lua-tests@{suffix}}
|
|
lua-tests@{suffix}: @{libdir}/.luatestspassed
|
|
|
|
@{insert:.PHONY:c-tests@{suffix}}
|
|
c-tests@{suffix}: @{libdir}/.ctestspassed
|
|
|
|
@{libdir}/.luatestspassed: @{sharedlib} test/$(TESTLUA)
|
|
$(ECHO) "===== Running Lua tests for @{suffix} ====="
|
|
@$(LUA) \
|
|
-e "package.cpath='@{sharedlib};'..package.cpath" \
|
|
test/$(TESTLUA)
|
|
$(TOUCH) @{libdir}/.luatestspassed
|
|
$(ECHO) "===== Lua tests for @{suffix} PASSED ====="
|
|
|
|
@{libdir}/.ctestspassed: @{libdir}/$(TESTNAME) test/$(TESTLUA)
|
|
$(ECHO) "===== Running C tests for @{suffix} ====="
|
|
@{libdir}/$(TESTNAME)
|
|
$(TOUCH) @{libdir}/.ctestspassed
|
|
$(ECHO) "===== C tests for @{suffix} PASSED ====="
|
|
|
|
@{libdir}/$(TESTNAME): @{objects} @{staticlib}
|
|
$(MKDIR) @{libdir}
|
|
@{LD} -o $@ @{objects} $(LDFLAGS) -lm -l$(LUALIB) -l$(PROJECTNAME) -L@{libdir}
|
|
|
|
@{insert:.PHONY:resettest@{suffix}}
|
|
resettest@{suffix}:
|
|
$(RM) @{libdir}/.luatestspassed
|
|
$(RM) @{libdir}/.ctestspassed
|
|
|
|
@{insert:.PHONY:cleantest@{suffix}}
|
|
cleantest@{suffix}: cleanlibs@{suffix} resettest@{suffix} \
|
|
cleantestobjects@{suffix}
|
|
$(RM) @{libdir}/$(TESTNAME)
|
|
$(RMDIR) @{libdir}
|
|
|
|
@{--: Note implicit objprefix parameter }
|
|
# testobjects@{suffix}:
|
|
@{fill-macro:objects-macro:%<>:
|
|
prefix="test",
|
|
objects=@{lua-escape:@{objects}};
|
|
dep=@{lua-escape:@{fill-template:test-dep-template:}};
|
|
}
|
|
|
|
@{--: Libraries must use original cflags }
|
|
@{fill-template:lib-targets:cflags=@{lua-escape:@{cflags-orig}}}
|
|
|
|
}
|
|
|
|
## GENERATED RELEASE TARGETS ##################################################
|
|
|
|
@{map-template:release-info:lib-targets}
|
|
|
|
## TEST TARGETS ###############################################################
|
|
|
|
@{insert:.PHONY:test}
|
|
test:@{map-template:std-info: test@{suffix}}
|
|
$(ECHO) "===== TESTS PASSED ====="
|
|
|
|
@{insert:.PHONY:resettest}
|
|
resettest:@{map-template:std-info: resettest@{suffix}}
|
|
|
|
@{insert:.PHONY:cleantest}
|
|
cleantest:@{map-template:std-info: cleantest@{suffix}}
|
|
|
|
## GENERATED TEST TARGETS #####################################################
|
|
|
|
@{map-template:std-info:std-targets}
|
|
|
|
## END OF GENERATED TARGETS ###################################################
|
|
|
|
.PHONY: @{concat:.PHONY: }
|