Commit 3c3fc8f9 authored by Denis Filatov's avatar Denis Filatov
Browse files

multi-targets in build

parent 32f0678d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
build/*-d
build/*-d
+2 −2
Original line number Diff line number Diff line
all clean:
all clean:
	$(MAKE) -C certgen $@
+108 −82
Original line number Diff line number Diff line
######################################################################
##
##  Created by: Denis Filatov
##
##  Copyleft (c) 2015
##  This code is provided under the CeCill-C license agreement.
######################################################################
export LANG=en_US
ALL_CONFIGURATIONS := POSIX WIN32
.PHONY: all clean tests docs cleandocs distr DUMMY

BUILDROOT ?= $(PROJECTROOT)/build
CSHAREDDIR ?= $(PROJECTROOT)/cshared 
CXMLDIR ?= $(PROJECTROOT)/cxml

ifeq ($(ARCH),)
 ARCH = $(shell gcc -dumpmachine)
 GCC := gcc
 GPP  := g++
 STRIP := strip
 AR := ar
else
 GCC   := $(addprefix $(addsuffix -,$(ARCH)), gcc)
 GPP  := $(addprefix $(addsuffix -,$(ARCH)), g++)
 STRIP := $(addprefix $(addsuffix -,$(ARCH)), strip)
 AR    := $(addprefix $(addsuffix -,$(ARCH)), ar)
endif
LINK := $(GCC)

-include $(BUILDROOT)/$(ARCH).mk

ifneq ($(findstring w32,$(ARCH)),)
 packages := $(filter-out readline threads, $(packages))
@@ -30,7 +30,11 @@ ifneq ($(findstring cygwin,$(ARCH)),)
 CFG += CYGWIN
endif

cflags   := -Wall -fPIC $(cflags)
ifneq ($(findstring openwrt,$(ARCH)),)
 CFG += OPENWRT
endif

cflags   += -fPIC -Wall

ifeq ($(DEBUG),)
 DEBUG=no
@@ -56,51 +60,55 @@ ifneq ($(filter dmalloc, $(packages)),)
  dsuffix   = -dmalloc
endif

ifneq ($(filter thread, $(packages)),)
  cflags  += -pthread
  defines += USE_THREADS
  libs    += -lpthread
endif

ifneq ($(filter profile, $(packages)),)
  cflags += -pg
endif

ifneq (,$(filter openssl, $(packages)))
 ifneq (,$(filter WIN32, $(CFG)))
  OPENSSL_DIR ?= C:/Tools/OpenSSL/Win64
  includes += $(OPENSSL_DIR)/include
  libs     += $(OPENSSL_DIR)/lib/libcrypto.lib $(OPENSSL_DIR)/lib/libssl.lib
ifneq ($(filter openssl, $(packages)),)
 ifneq ($(findstring mingw32,$(ARCH)),)
  ifeq ($(OPENSSL_DIR),)
    OPENSSL_DIR := C:/OpenSSL/Win32
  endif
  libs += $(OPENSSL_DIR)/lib/MinGW/libcrypto.a $(OPENSSL_DIR)/lib/MinGW/libssl.a
 else
  libs    += -L/usr/local/lib -lssl -lcrypto
  libs += -lssl -lcrypto
 endif
 ifneq ($(OPENSSL_DIR),)
  includes += $(OPENSSL_DIR)/include
  libs += -L $(OPENSSL_DIR)/lib
 endif

ifneq ($(filter googletest, $(packages)),)
  includes += /usr/local/include
  libs     += -L/usr/local/lib -lgtest -lgtest_main
endif

ifneq ($(filter cxml, $(packages)),)
  predirs += $(PROJECTROOT)/cxml
  predirs += $(CXMLDIR)
  includes += $(CXMLDIR)
endif

ifneq ($(filter cshared, $(packages)),)
  predirs += $(PROJECTROOT)/cshared
  predirs += $(CSHAREDDIR)
  includes += $(CSHAREDDIR)
endif

ifeq ($(testdir), )
  testdir := tests
ifneq ($(filter pcap, $(packages)),)
 ifneq ($(findstring cygwin,$(ARCH)),)
  ifneq ($(NPCAP_SDK),)
   includes += "$(NPCAP_SDK)/Include"
   libs     += "/cygdrive/c/Windows/System32/Npcap/wpcap.dll"
  endif
 else
  libs    += -lpcap
 endif
endif

ifeq ($(PROJECTROOT),)
 PROJECTROOT := .
ifneq ($(filter thread, $(packages)),)
  defines += USE_THREADS
  libs    += -lpthread
endif

ifeq ($(BUILDROOT),)
 BUILDROOT := $(PROJECTROOT)

ifeq ($(testdir), )
  testdir := tests
endif
includes  += $(PROJECTROOT)

includes  += $(foreach cfg,$(CFG),$(includes-$(cfg)))
defines   += $(foreach cfg,$(CFG),$(defines-$(cfg)))
@@ -113,22 +121,23 @@ predirs += $(foreach cfg,$(CFG),$(predirs-$(cfg)))
postdirs  += $(foreach cfg,$(CFG),$(postdirs-$(cfg)))

tests     := $(addprefix $(addsuffix /,$(testdir)),$(tests))

sources   := $(addprefix $(addsuffix /,$(srcdir)),$(sources))
csources   := $(filter %.c,  $(sources))
ccsources  := $(filter %.cc, $(sources)) 
cppsources := $(filter %.cpp, $(sources))

headers   := $(addprefix $(addsuffix /,$(incdir)),$(headers))

cflags    += $(addprefix -I, $(includes)) $(addprefix -D, $(defines))

ifeq ($(BUILDROOT),)
 BUILDROOT = .
endif

outdir    := $(BUILDROOT)/$(ARCH)$(dsuffix)
objdir    := $(outdir)/o-$(PROJECT)
cobjects  := $(patsubst %.c,  $(objdir)/%.o, $(csources))
ccobjects := $(patsubst %.cc, $(objdir)/%.o, $(ccsources))
objects   := $(cobjects) $(ccobjects) $(cppobjects)
objects   := $(patsubst %.c, $(objdir)/%.o, $(sources))
$(foreach b,$(bins),$(eval objects-$(b)=$$(patsubst %.c,$$(objdir)/%.o,$$(sources-$(b)))))
$(info objects-certgen=$(objects-certgen))
$(info objects-keygen=$(objects-keygen))

binobjects:= $(patsubst %.c, $(objdir)/%.o, $(foreach b,$(bins),$(sources-$(b)))) 
testbins  := $(patsubst %.c, $(outdir)/%, $(tests))
dirs      := $(objdir) $(outdir)/tests

@@ -139,54 +148,71 @@ binnames := $(patsubst %, $(outdir)/%, $(bins))
ldflags += $(patsubst %, -L%, $(outdir) $(libdirs))

ifneq ($(filter cxml, $(packages)),)
  libs += $(outdir)/libcxml.a
  deplibs += $(outdir)/libcxml.a
endif

ifneq ($(filter cshared, $(packages)),)
  libs += $(outdir)/libcshared.a
  deplibs += $(outdir)/libcshared.a
endif

ifneq (,$(sort $(ccobjects) $(cppobjects)))
  LINK := $(GPP)
endif

all: $(dirs) $(predirs) $(alibnames) $(solibnames) $(binnames) $(postdirs)
all: $(dirs) $(pre) $(predirs) $(alibnames) $(solibnames) $(binnames) $(postdirs) $(post)

tests: all $(testbins)

$(predirs) $(postdirs): DUMMY
	$(MAKE) -C $@ DEBUG=$(DEBUG) PROJECTROOT=../$(PROJECTROOT) BUILDROOT=../$(BUILDROOT)
	$(MAKE) -C $@ ARCH=$(ARCH) PROJECTROOT=$(realpath $(PROJECTROOT)) BUILDROOT=$(realpath $(BUILDROOT)) DEBUG=$(DEBUG)

$(alibnames): $(outdir)/lib%.a : $(objects)
	ar rcs $@ $^
define ALibRule
$$(outdir)/lib$(l).a: $$(outdir)/lib%.a : $$(objects-$(l)) $$(objects)
	$$(AR) rcs $$@ $$^
endef
$(foreach l, $(alibs), $(eval $(ALibRule)))

$(solibnames): $(outdir)/lib%.so : $(objects)
	$(LINK) $(cflags) -shared $(ldflags) -o $@ $^ $(csharedlib) $(libs)
define SoLibRule
$$(outdir)/lib$(l).so: $$(outdir)/lib%.so : $$(objects-$(l)) $$(objects) $$(deplibs)
	$$(GCC) $$(cflags) -shared $$(ldflags) -o $$@ $$^ $$(csharedlib) $$(deplibs) $$(libs) $$(libs_$$*)
ifeq (no,$$(DEBUG))
	$$(STRIP) $@
endif
endef
$(foreach l, $(solibs), $(eval $(SoLibRule)))

define BinRule
$$(outdir)/$(b): $$(outdir)/%: $$(objects-$(b)) $$(objects) $$(deplibs)
	$$(GCC) $$(cflags) $$(ldflags) -o $$@ $$^ $$(csharedlib) $$(deplibs) $$(libs) $$(libs_$$*) 
ifeq (no,$$(DEBUG))
	$$(STRIP) $$@
endif
endef
$(foreach b, $(bins), $(eval $(BinRule)))

#$(eval $(call BinRule, certgen))

#$(eval $(call BinRule, keygen))
 
$(binnames): $(outdir)/% : $(objects) $(binobjects-%)
	$(LINK) $(cflags)  $(ldflags) -o $@ $^ $(csharedlib) $(libs)
#$(binnames): $(outdir)/% : $(eval $$(objects-%)) $(objects) $(deplibs)
#	$(GCC) $(cflags) $(ldflags) -o $@ $^ $(csharedlib) $(deplibs) $(libs) $(libs_$*) 
#ifeq (no,$(DEBUG))
#	$(STRIP) $@
#endif

$(testbins): $(alibnames)
$(testbins): $(outdir)/tests/% : tests/%.c
	$(LINK) $(cflags) -o $@ $< $(alibnames) $(libs)
	$(GCC) $(cflags) $(cflags_$*) -o $@ $< $(alibnames) $(libs) $(libs_$*)
ifeq (no,$(DEBUG))
	$(STRIP) $@
endif

$(dirs):
	mkdir -p $@

$(cobjects): $(objdir)/%.o: %.c
	@mkdir -p $(dir $@)
	$(GCC)  $(cflags) -o $@ -MMD -MF $(objdir)/$*.d -c $<

$(ccobjects): $(objdir)/%.o: %.cc
	@mkdir -p $(dir $@)
	$(GPP)  $(cflags) -o $@ -MMD -MF $(objdir)/$*.d -c $<

$(cppobjects): $(objdir)/%.o: %.cpp
$(objects) $(binobjects): $(objdir)/%.o: %.c
	@mkdir -p $(dir $@)
	$(GPP)  $(cflags) -o $@ -MMD -MF $(objdir)/$*.d -c $<
	$(GCC)  $(cflags) -o $@ -MMD -MF $(objdir)/$*.d -c $(abspath $<)

clean:
	rm -rf $(outdir) $(wildcard $(addsuffix /*~, . $(predirs) $(postdirs)))
	rm -rf $(alibnames) $(solibnames) $(binnames) $(testbins) $(objects)

distfiles += $(wildcard Makefile $(DOXYFILE))
dist: