about summary refs log tree commit diff
path: root/Makefile.lib
blob: d197fa6c1c6330fe56b385a1b7a570f5b0a4b017 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
default: all


# Include Autoconf variables.
Makefile.config: Makefile.config.in
	./config.status

include Makefile.config


# Include all sub-Makefiles.
define include_sub_makefile =
  d := $$(patsubst %/, %, $$(dir $(1)))
  include $(1)
endef

$(foreach mf, $(SUBS), $(eval $(call include_sub_makefile, $(mf))))


# Initialise some variables.
clean_list :=
dist_files :=
QUIET = @


# Pass -fPIC if we're building dynamic libraries.
ifeq ($(BUILD_SHARED_LIBS), 1)
  GLOBAL_CFLAGS += -fPIC
  GLOBAL_CXXFLAGS += -fPIC
endif


# Pass -g if we want debug info.
ifeq ($(BUILD_DEBUG), 1)
  GLOBAL_CFLAGS += -g
  GLOBAL_CXXFLAGS += -g
endif


# Pattern rules.

%.o: %.cc
	$(QUIET) $(CXX) -o $@ -c $< $(GLOBAL_CXXFLAGS) $(CXXFLAGS) $($@_CXXFLAGS) -MMD -MF $(basename $@).dep -MP

%.o: %.c
	$(QUIET) $(CC) -o $@ -c $< $(GLOBAL_CFLAGS) $(CFLAGS) $($@_CFLAGS) -MMD -MF $(basename $@).dep -MP


# Generate Make rules for libraries.
libs_list :=

define LIBS_template =
  _d := $$(strip $$($(1)_DIR))
  _srcs := $$(foreach src, $$($(1)_SOURCES), $$(_d)/$$(src))
  _objs := $$(addsuffix .o, $$(basename $$(_srcs)))

  $(1)_LDFLAGS_USE :=

  ifeq ($(BUILD_SHARED_LIBS), 1)
    _lib := $$(_d)/$(1).so
    $$(_lib): $$(_objs)
	$(QUIET) $(CC) -o $$@ -shared $$^ $$($(1)_LDFLAGS)
    $(1)_LDFLAGS_USE += -L$$(_d) -Wl,-rpath,$$(abspath $$(_d)) -l$$(patsubst lib%,%,$$(strip $(1)))
  else
    _lib := $$(_d)/$(1).a
    $$(_lib): $$(_objs)
	$(QUIET) ar crs $$@ $$?
    $(1)_LDFLAGS_USE += $$(_lib) $$($(1)_LDFLAGS)
  endif

  $(1)_LDFLAGS_USE += $$($(1)_LDFLAGS_PROPAGATED)
  $(1)_NAME := $$(_lib)

  # Propagate CXXFLAGS to the individual object files.
  $$(foreach obj, $$(_objs), $$(eval $$(obj)_CXXFLAGS=$$($(1)_CXXFLAGS)))

  include $$(wildcard $$(_d)/*.dep)

  libs_list += $$(_lib)
  clean_list += $$(_d)/*.a $$(_d)/*.so $$(_d)/*.o $$(_d)/*.dep
  dist_files += $$(_srcs)
endef

$(foreach lib, $(LIBS), $(eval $(call LIBS_template,$(lib))))


# Generate Make rules for programs.
programs_list :=

define PROGRAMS_template =
  _d := $$($(1)_DIR)
  _srcs := $$(foreach src, $$($(1)_SOURCES), $$(_d)/$$(src))
  $(1)_OBJS := $$(addsuffix .o, $$(basename $$(_srcs)))
  _libs := $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_NAME))
  _prog := $$(_d)/$(1)

  $$(_prog): $$($(1)_OBJS) $$(_libs)
	$(QUIET) $(CXX) -o $$@ -Wl,--no-copy-dt-needed-entries $$($(1)_OBJS) $$($(1)_LDFLAGS) $$(foreach lib, $$($(1)_LIBS), $$($$(lib)_LDFLAGS_USE))

  # Propagate CXXFLAGS to the individual object files.
  $$(foreach obj, $$($(1)_OBJS), $$(eval $$(obj)_CXXFLAGS=$$($(1)_CXXFLAGS)))

  include $$(wildcard $$(_d)/*.dep)

  programs_list += $$(_prog)
  clean_list += $$(_prog) $$(_d)/*.o $$(_d)/*.dep
  dist_files += $$(_srcs)
endef

$(foreach prog, $(PROGRAMS), $(eval $(call PROGRAMS_template,$(prog))))


# Distributing stuff.
dist_name = $(PACKAGE_NAME)-$(PACKAGE_VERSION)

dist:
	@echo $(dist_files)
	$(QUIET) tar cvfj $(dist_name).tar.bz2 $(dist_files) --transform 's,^,$(dist_name)/,'


# Cleaning stuff.
clean:
	rm -fv $(clean_list)

dryclean:
	@echo $(clean_list)


all: $(programs_list)