Skip to content

Commit 78c6c00

Browse files
authored
Merge pull request #618 from ddiproietto/pkg_config
Makefile: Use pkg-config if available.
2 parents ea469d2 + bbb11c3 commit 78c6c00

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

core/Makefile

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ MAKEFLAGS += --no-builtin-rules
3434

3535
CXX ?= g++
3636
PROTOC ?= protoc
37+
PKG_CONFIG ?= pkg-config
3738

3839
VERBOSE ?= 0
3940

@@ -57,6 +58,8 @@ $(error g++ 5 or higher is required. Use container_build.py if newer g++ is not
5758
endif
5859
endif
5960

61+
HAS_PKG_CONFIG := $(shell command -v $(PKG_CONFIG) 2>&1 >/dev/null && echo yes || echo no)
62+
6063
RTE_SDK ?= $(abspath ../deps/dpdk-17.05)
6164
RTE_TARGET ?= $(shell uname -m)-native-linuxapp-gcc
6265
DPDK_LIB ?= dpdk
@@ -75,6 +78,28 @@ else ifeq ($(words $(MAKECMDGOALS)),1)
7578
endif
7679
endif
7780

81+
# We always want these libraries to be dynamically linked even when the
82+
# user requests a static build. These needs to be listed first, so even
83+
# if they're listed again in the static section, they will be dynamically
84+
# linked.
85+
ALWAYS_DYN_LIBS := -lpthread -ldl
86+
# These libraries are not supported by pkg-config.
87+
ALWAYS_LIBS := -lpcap -lgflags
88+
# If pkg-config is available, we just need a list of the dependecies.
89+
PKG_CONFIG_DEPS := libglog protobuf grpc++ libunwind zlib
90+
# If pkg-config is not available, we need to list the libs we depend on.
91+
NO_PKG_CONFIG_LIBS := -lglog -lgflags -lprotobuf -lgrpc++ -lunwind -lz
92+
# If pkg-config is not available and we're static linking, we also need
93+
# the indirect dependecies. This is annoying, because they may change
94+
# in future versions.
95+
NO_PKG_CONFIG_LIBS_INDIRECT := -lgrpc -lssl -lcrypto -llzma
96+
97+
ifeq ($(HAS_PKG_CONFIG), yes)
98+
PKG_CFLAGS := $(shell $(PKG_CONFIG) --cflags $(PKG_CONFIG_DEPS))
99+
else
100+
PKG_CFLAGS :=
101+
endif
102+
78103
# Plugins get to look in $COREDIR through -I $(COREDIR). Let them also
79104
# see their own top level directory, e.g., /some/path/to/modules/foo.cc
80105
# gets /some/path/to/modules/.. so that it can read its own
@@ -93,7 +118,7 @@ CXXFLAGS += -std=c++11 -g3 -ggdb3 $(CXXARCHFLAGS) \
93118
-isystem $(dir $<).. -isystem $(COREDIR)/modules \
94119
-D_GNU_SOURCE \
95120
-Werror \
96-
-Wall -Wextra -Wcast-align
121+
-Wall -Wextra -Wcast-align $(PKG_CFLAGS)
97122

98123
PERMISSIVE := -Wno-unused-parameter -Wno-missing-field-initializers \
99124
-Wno-unused-private-field
@@ -112,20 +137,28 @@ LDFLAGS += -rdynamic -L$(DPDK_LIB_DIR) -Wl,-rpath=$(DPDK_LIB_DIR) -pthread
112137
ifdef BESS_LINK_DYNAMIC
113138
LIBS_ALL_SHARED = -Wl,-call_shared
114139
LIBS_DL_SHARED =
140+
ifeq ($(HAS_PKG_CONFIG), yes)
141+
PKG_LIBS := $(shell $(PKG_CONFIG) --libs $(PKG_CONFIG_DEPS))
142+
else
143+
PKG_LIBS := $(NO_PKG_CONFIG_LIBS)
144+
endif
115145
else # Used static libraries
116146
LIBS_ALL_SHARED =
117147
LIBS_DL_SHARED = -Wl,-call_shared
118-
LIBS_LZMA = -llzma
119148
LDFLAGS += -static-libstdc++
149+
ifeq ($(HAS_PKG_CONFIG), yes)
150+
PKG_LIBS := $(shell $(PKG_CONFIG) --static --libs $(PKG_CONFIG_DEPS))
151+
else
152+
PKG_LIBS := $(NO_PKG_CONFIG_LIBS) $(NO_PKG_CONFIG_LIBS_INDIRECT)
153+
endif
120154
endif
121155

122-
LIBS += -Wl,-non_shared \
156+
LIBS += $(ALWAYS_DYN_LIBS) -Wl,-non_shared \
123157
-Wl,--whole-archive -l$(DPDK_LIB) -Wl,--no-whole-archive \
124158
$(LIBS_ALL_SHARED) \
125-
-lglog -lgflags -lprotobuf -lgrpc++ -lgrpc \
126-
-lssl -lcrypto -lunwind $(LIBS_LZMA) -lpcap -lz \
159+
$(PKG_LIBS) $(ALWAYS_LIBS) \
127160
$(LIBS_DL_SHARED) \
128-
-ldl
161+
$(ALWAYS_DYN_LIBS)
129162

130163
ifdef SANITIZE
131164
CXXFLAGS += -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer

env/packages.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- libtool
2222
- make
2323
- cmake
24+
- pkg-config
2425
- libpthread-stubs0-dev
2526
- libunwind8-dev
2627
- liblzma-dev

0 commit comments

Comments
 (0)