platform.mk 3.06 KB
Newer Older
Marek Vavruša's avatar
Marek Vavruša committed
1
2
# Platform-specific
CCLD := $(CC)
3
4
CGO := go tool cgo
GCCGO := gccgo
Marek Vavruša's avatar
Marek Vavruša committed
5
6
LIBEXT := .so
MODEXT := $(LIBEXT)
7
AREXT  := .a
Marek Vavruša's avatar
Marek Vavruša committed
8
9
LIBTYPE := shared
MODTYPE := shared
10
ARTYPE  := static
Marek Vavruša's avatar
Marek Vavruša committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
BINEXT :=
PLATFORM = Linux
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
26
        PLATFORM := POSIX
27
28
29
30
        LDFLAGS += -pthread
        ifeq (,$(findstring BSD,$(UNAME)))
            LDFLAGS += -ldl
        endif
Marek Vavruša's avatar
Marek Vavruša committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    endif
endif

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

%.o: %.c
	$(call quiet,CC,$<) $(CFLAGS) -MMD -MP -c $< -o $@

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

# Make target (name,path,ext,ldflags,dst)
define make_target
$$(eval $$(call make_objs,$(1)))
54
$(1) := $(2)/$(1)$(3)
Marek Vavruša's avatar
Marek Vavruša committed
55
$(2)/$(1)$(3): $$($(1)_OBJ) $$($(1)_DEPEND)
56
57
58
ifeq ($(4),-$(ARTYPE))
	$(call quiet,AR,$$@) rcs $$@ $$($(1)_OBJ)
else
59
	$(call quiet,CCLD,$$@) $(CFLAGS) $$($(1)_OBJ) -o $$@ $(4) $$($(1)_LIBS) $(LDFLAGS)
60
endif
Marek Vavruša's avatar
Marek Vavruša committed
61
62
63
$(1)-clean:
	$(RM) $$($(1)_OBJ) $$($(1)_DEP) $(2)/$(1)$(3)
$(1)-install: $(2)/$(1)$(3)
64
65
	$(INSTALL) -d $(PREFIX)/$(5)
	$(INSTALL) $$^ $(PREFIX)/$(5)
Marek Vavruša's avatar
Marek Vavruša committed
66
67
ifneq ($$(strip $$($(1)_HEADERS)),)
	$(INSTALL) -d $(PREFIX)/$(INCLUDEDIR)/$(1)
68
	$(INSTALL) -m 644 $$($(1)_HEADERS) $(PREFIX)/$(INCLUDEDIR)/$(1)
Marek Vavruša's avatar
Marek Vavruša committed
69
endif
70
.PHONY: $(1)-clean $(1)-install
Marek Vavruša's avatar
Marek Vavruša committed
71
72
73
74
75
endef

# Make targets (name,path)
make_bin = $(call make_target,$(1),$(2),$(BINEXT),,$(BINDIR))
make_lib = $(call make_target,$(1),$(2),$(LIBEXT),-$(LIBTYPE),$(LIBDIR))
76
77
make_module = $(call make_target,$(1),$(2),$(LIBEXT),-$(LIBTYPE),$(MODULEDIR))
make_shared = $(call make_target,$(1),$(2),$(MODEXT),-$(MODTYPE),$(LIBDIR))
78
make_static = $(call make_target,$(1),$(2),$(AREXT),-$(ARTYPE),$(LIBDIR))
Marek Vavruša's avatar
Marek Vavruša committed
79
80
81
82
83
84
85
86
87
88
89
90

# Evaluate library
define have_lib
ifeq ($$(strip $$($(1)_LIBS)),)
	HAS_$(1) := no
else
	HAS_$(1) := yes
endif
endef

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

94
95
# Find library alternative (pkg-config)
define find_alt
Marek Vavruša's avatar
Marek Vavruša committed
96
	ifeq ($$(strip $$($(1)_LIBS)),)
97
98
99
100
101
102
103
		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
Marek Vavruša's avatar
Marek Vavruša committed
104
	endif
105
	$(call have_lib,$(1),$(3))
106
107
endef

108
109
110
# Find binary
define find_bin
	ifeq ($$(strip $$($(1)_BIN)),)
Marek Vavruša's avatar
Marek Vavruša committed
111
112
113
114
		$(1)_BIN := $(shell which $(1))
	endif
	ifeq ($$(strip $$($(1)_BIN)),)
		HAS_$(1) := no
115
116
117
118
119
120
	else
		HAS_$(1) := yes
		$(1) := $$($(1)_BIN)
	endif
endef

Marek Vavruša's avatar
Marek Vavruša committed
121
122
123
124
125
# Find Python 
define find_python
	python_CFLAGS := $(shell $(PYTHON) -c "from distutils import sysconfig as c;print('-I%s' % c.get_python_inc())")
	python_LIBS := $(shell $(PYTHON) -c "from distutils import sysconfig as c;print('-L%s -lpython%s' % (c.get_config_var('LIBDIR'), c.get_config_var('VERSION')))")
	$(call have_lib,python)
Marek Vavruša's avatar
Marek Vavruša committed
126
endef