platform.mk 3.49 KB
Newer Older
1 2 3 4
# Platform-specific stuff
# Don't touch this unless you're changing the way targets are compiled
# You have been warned

Marek Vavruša's avatar
Marek Vavruša committed
5
CCLD := $(CC)
6
CGO := go tool cgo
7
GO := go
8
CAT := cat
Marek Vavruša's avatar
Marek Vavruša committed
9 10
LIBEXT := .so
MODEXT := $(LIBEXT)
11
AREXT  := .a
Marek Vavruša's avatar
Marek Vavruša committed
12 13
LIBTYPE := shared
MODTYPE := shared
14
ARTYPE  := static
Marek Vavruša's avatar
Marek Vavruša committed
15 16
BINEXT :=
PLATFORM = Linux
17
ARCH := $(word 1, $(subst -, ,$(shell $(CC) -dumpmachine)))
Marek Vavruša's avatar
Marek Vavruša committed
18 19 20 21 22 23 24 25 26 27 28 29 30
ifeq ($(OS),Windows_NT)
	PLATFORM := Windows
	RM := del
	LN := link
	LIBEXT := .lib
	BINEXT := .exe
else
	UNAME := $(shell uname -s)
    ifeq ($(UNAME),Darwin)
        PLATFORM := Darwin
        LIBEXT := .dylib
        MODTYPE := dynamiclib
    else
31
        PLATFORM := POSIX
32
        LDFLAGS += -pthread -lm -Wl,-E
33 34 35
        ifeq (,$(findstring BSD,$(UNAME)))
            LDFLAGS += -ldl
        endif
Marek Vavruša's avatar
Marek Vavruša committed
36 37 38 39 40 41 42 43 44 45 46
    endif
endif

# Silent compilation
ifeq ($(V),1)
	quiet = $($1)
else
	quiet = @echo "  $1	$2"; $($1)
endif	

%.o: %.c
47
	$(call quiet,CC,$<) $(BUILD_CFLAGS) -MMD -MP -c $< -o $@
Marek Vavruša's avatar
Marek Vavruša committed
48 49 50 51 52

# Make objects and depends (name)
define make_objs
$(1)_OBJ := $$($(1)_SOURCES:.c=.o)
$(1)_DEP := $$($(1)_SOURCES:.c=.d)
53

Marek Vavruša's avatar
Marek Vavruša committed
54 55 56
-include $$($(1)_DEP)
endef

57
# Make target (name,path,ext,ldflags,dst,amalgable)
Marek Vavruša's avatar
Marek Vavruša committed
58
define make_target
59 60 61 62 63 64 65 66 67
ifeq ($(AMALG)|$(6), yes|yes)
$(1).amalg.c: $$($(1)_SOURCES)
	$(call quiet,CAT,$$@) $$($(1)_SOURCES) > $$@
# AR requires .o compiled
$(1)_OBJ := $(1).amalg.c
ifeq ($(4),-$(ARTYPE))
$(1)_OBJ := $(1).amalg.o
endif
else
Marek Vavruša's avatar
Marek Vavruša committed
68
$$(eval $$(call make_objs,$(1)))
69
endif
70
$(1) := $(2)/$(1)$(3)
Marek Vavruša's avatar
Marek Vavruša committed
71
$(2)/$(1)$(3): $$($(1)_OBJ) $$($(1)_DEPEND)
72 73 74
ifeq ($(4),-$(ARTYPE))
	$(call quiet,AR,$$@) rcs $$@ $$($(1)_OBJ)
else
75
	$(call quiet,CCLD,$$@) $(BUILD_CFLAGS) $$($(1)_CFLAGS) $$($(1)_OBJ) -o $$@ $(4) $$($(1)_LIBS) $(BUILD_LDFLAGS)
76
endif
Marek Vavruša's avatar
Marek Vavruša committed
77
$(1)-clean:
78 79 80 81
	$(RM) $$($(1)_OBJ) $$($(1)_DEP) $(2)/$(1)$(3)
ifeq ($(6), yes)
	$(RM) $(1).amalg.c $(1).amalg.o
endif
Marek Vavruša's avatar
Marek Vavruša committed
82
$(1)-install: $(2)/$(1)$(3)
83
ifneq ($(5),$(MODULEDIR))
84
	$(INSTALL) -d $(DESTDIR)$(5)
85
endif
86
	$(INSTALL) $(2)/$(1)$(3) $(DESTDIR)$(5)
Marek Vavruša's avatar
Marek Vavruša committed
87
ifneq ($$(strip $$($(1)_HEADERS)),)
88 89
	$(INSTALL) -d $(DESTDIR)$(INCLUDEDIR)/$(1)
	$(INSTALL) -m 644 $$($(1)_HEADERS) $(DESTDIR)$(INCLUDEDIR)/$(1)
Marek Vavruša's avatar
Marek Vavruša committed
90
endif
91
.PHONY: $(1)-clean $(1)-install
Marek Vavruša's avatar
Marek Vavruša committed
92 93
endef

94 95 96 97 98 99
# Make targets (name,path,amalgable yes|no)
make_bin = $(call make_target,$(1),$(2),$(BINEXT),$(BINFLAGS),$(BINDIR),$(3))
make_lib = $(call make_target,$(1),$(2),$(LIBEXT),-$(LIBTYPE),$(LIBDIR),$(3))
make_module = $(call make_target,$(1),$(2),$(LIBEXT),-$(LIBTYPE),$(MODULEDIR),$(3))
make_shared = $(call make_target,$(1),$(2),$(MODEXT),-$(MODTYPE),$(LIBDIR),$(3))
make_static = $(call make_target,$(1),$(2),$(AREXT),-$(ARTYPE),$(LIBDIR),$(3))
Marek Vavruša's avatar
Marek Vavruša committed
100 101 102

# Find library (pkg-config)
define find_lib
103
	$(call find_alt,$(1),$(1),$(2))
Marek Vavruša's avatar
Marek Vavruša committed
104 105
endef

106 107
# Find library alternative (pkg-config)
define find_alt
108
	ifeq ($$(strip $$($(1)_LIBS)),)
109 110 111 112 113 114 115
		ifneq ($(strip $(3)),)
			$(1)_VER := $(shell pkg-config --atleast-version=$(3) $(2) && echo $(3))
		endif
		ifeq ($(strip $(3)),$$($(1)_VER))
			$(1)_CFLAGS := $(shell pkg-config --cflags $(2) --silence-errors)
			$(1)_LIBS := $(shell pkg-config --libs $(2)  --silence-errors)
		endif
116
	endif
117 118 119 120 121
	ifeq ($$(strip $$($(1)_LIBS)),)
		HAS_$(1) := no
	else
		HAS_$(1) := yes
	endif
122 123
endef

124 125 126
# Find binary
define find_bin
	ifeq ($$(strip $$($(1)_BIN)),)
127 128 129 130
		$(1)_BIN := $(shell which $(1))
	endif
	ifeq ($$(strip $$($(1)_BIN)),)
		HAS_$(1) := no
131 132 133 134 135
	else
		HAS_$(1) := yes
		$(1) := $$($(1)_BIN)
	endif
endef
136 137 138

# Find version
define find_ver
139
	ifeq ($(shell test $(2) -ge $(3); echo $$?),0)
140 141 142 143 144 145
		HAS_$(1) := yes
	else
		HAS_$(1) := no
	endif
endef

146 147 148 149
# Find Go package
define find_gopkg
	HAS_$(1) := $(shell go list $(2) > /dev/null 2>&1 && echo yes || echo no)
endef