Base on The patch from Arch[1] but better suited to Gentoo needs. [1]: https://gitlab.archlinux.org/archlinux/packaging/packages/libnvidia-container/-/raw/a642a3cc488862a8483075ee63f8a8c470860149/fix-makefile.patch diff --git a/Makefile b/Makefile index 6fb6976..c6f26d3 100644 --- a/Makefile +++ b/Makefile @@ -18,18 +18,16 @@ ##### Global variables ##### WITH_NVCGO ?= yes -WITH_LIBELF ?= no -WITH_TIRPC ?= no +WITH_TIRPC ?= yes WITH_SECCOMP ?= yes ##### Global definitions ##### -export prefix = /usr/local +export prefix ?= /usr export exec_prefix = $(prefix) export bindir = $(exec_prefix)/bin -export libdir = $(exec_prefix)/lib +export libdir ?= $(exec_prefix)/lib64 export docdir = $(prefix)/share/doc -export libdbgdir = $(prefix)/lib/debug$(libdir) export includedir = $(prefix)/include export pkgconfdir = $(libdir)/pkgconfig @@ -158,20 +156,17 @@ ifeq ($(WITH_NVCGO), yes) LIB_CPPFLAGS += -DWITH_NVCGO LIB_LDLIBS_SHARED += -lpthread endif -ifeq ($(WITH_LIBELF), yes) +# Build with system libelf unconditionally LIB_CPPFLAGS += -DWITH_LIBELF LIB_LDLIBS_SHARED += -lelf -else -LIB_LDLIBS_STATIC += -l:libelf.a -endif ifeq ($(WITH_TIRPC), yes) -LIB_CPPFLAGS += -isystem $(DEPS_DIR)$(includedir)/tirpc -DWITH_TIRPC -LIB_LDLIBS_STATIC += -l:libtirpc.a -LIB_LDLIBS_SHARED += -lpthread +LIB_CPPFLAGS += -isystem $(includedir)/tirpc -DWITH_TIRPC +# dynamically link with system tirpc +LIB_LDLIBS_SHARED += -lpthread -ltirpc endif ifeq ($(WITH_SECCOMP), yes) -LIB_CPPFLAGS += -DWITH_SECCOMP $(shell pkg-config --cflags libseccomp) -LIB_LDLIBS_SHARED += $(shell pkg-config --libs libseccomp) +LIB_CPPFLAGS += -DWITH_SECCOMP $(shell $(PKG_CONFIG) --cflags libseccomp) +LIB_LDLIBS_SHARED += $(shell $(PKG_CONFIG) --libs libseccomp) endif LIB_CPPFLAGS += $(CPPFLAGS) LIB_CFLAGS += $(CFLAGS) @@ -222,22 +217,14 @@ $(BIN_OBJS): %.o: %.c | shared -include $(DEPENDENCIES) $(LIB_SHARED): $(LIB_OBJS) - $(MKDIR) -p $(DEBUG_DIR) $(CC) $(LIB_CFLAGS) $(LIB_CPPFLAGS) $(LIB_LDFLAGS) $(OUTPUT_OPTION) $^ $(LIB_SCRIPT) $(LIB_LDLIBS) - $(OBJCPY) --only-keep-debug $@ $(LIB_SONAME) - $(OBJCPY) --add-gnu-debuglink=$(LIB_SONAME) $@ - $(MV) $(LIB_SONAME) $(DEBUG_DIR) - $(STRIP) --strip-unneeded -R .comment $@ $(LIB_STATIC_OBJ): $(LIB_OBJS) # FIXME Handle user-defined LDFLAGS and LDLIBS $(LD) -d -r --exclude-libs ALL -L$(DEPS_DIR)$(libdir) $(OUTPUT_OPTION) $^ $(LIB_LDLIBS_STATIC) - $(OBJCPY) --localize-hidden $@ - $(STRIP) --strip-unneeded -R .comment $@ $(BIN_NAME): $(BIN_OBJS) $(CC) $(BIN_CFLAGS) $(BIN_CPPFLAGS) $(BIN_LDFLAGS) $(OUTPUT_OPTION) $^ $(BIN_SCRIPT) $(BIN_LDLIBS) - $(STRIP) --strip-unneeded -R .comment $@ ##### Public rules ##### @@ -262,15 +249,9 @@ deps: $(LIB_RPC_SRCS) $(BUILD_DEFS) ifeq ($(WITH_NVCGO), yes) $(MAKE) -f $(MAKE_DIR)/nvcgo.mk DESTDIR=$(DEPS_DIR) MAJOR=$(MAJOR) VERSION=$(VERSION) LIB_NAME=$(LIBGO_NAME) install endif -ifeq ($(WITH_LIBELF), no) - $(MAKE) -f $(MAKE_DIR)/elftoolchain.mk DESTDIR=$(DEPS_DIR) install -endif -ifeq ($(WITH_TIRPC), yes) - $(MAKE) -f $(MAKE_DIR)/libtirpc.mk DESTDIR=$(DEPS_DIR) install -endif install: all - $(INSTALL) -d -m 755 $(addprefix $(DESTDIR),$(includedir) $(bindir) $(libdir) $(docdir) $(libdbgdir) $(pkgconfdir)) + $(INSTALL) -d -m 755 $(addprefix $(DESTDIR),$(includedir) $(bindir) $(libdir) $(pkgconfdir)) # Install header files $(INSTALL) -m 644 $(LIB_INCS) $(DESTDIR)$(includedir) # Install library files @@ -279,18 +260,18 @@ install: all $(LN) -sf $(LIB_SONAME) $(DESTDIR)$(libdir)/$(LIB_SYMLINK) ifeq ($(WITH_NVCGO), yes) $(INSTALL) -m 755 $(DEPS_DIR)$(libdir)/$(LIBGO_SHARED) $(DESTDIR)$(libdir) + # FIXME: for some reason ldconfig stopped creating this symlink after applying Arch Linux LDFLAGS + $(LN) -sf $(LIBGO_SHARED) $(DESTDIR)$(libdir)/$(LIBGO_SONAME) $(LN) -sf $(LIBGO_SONAME) $(DESTDIR)$(libdir)/$(LIBGO_SYMLINK) endif $(LDCONFIG) -n $(DESTDIR)$(libdir) - # Install debugging symbols - $(INSTALL) -m 644 $(DEBUG_DIR)/$(LIB_SONAME) $(DESTDIR)$(libdbgdir) # Install configuration files $(MAKE_DIR)/$(LIB_PKGCFG).in "$(strip $(VERSION))" "$(strip $(LIB_LDLIBS_SHARED))" > $(DESTDIR)$(pkgconfdir)/$(LIB_PKGCFG) # Install binary files $(INSTALL) -m 755 $(BIN_NAME) $(DESTDIR)$(bindir) # Install documentation files - $(INSTALL) -d -m 755 $(DESTDIR)$(docdir)/$(LIB_NAME)-$(VERSION) - $(INSTALL) -m 644 $(DOC_FILES) $(DESTDIR)$(docdir)/$(LIB_NAME)-$(VERSION) + # $(INSTALL) -d -m 755 $(DESTDIR)$(docdir)/$(LIB_NAME)-$(VERSION) + # $(INSTALL) -m 644 $(DOC_FILES) $(DESTDIR)$(docdir)/$(LIB_NAME)-$(VERSION) uninstall: # Uninstall header files @@ -300,14 +281,12 @@ uninstall: ifeq ($(WITH_NVCGO), yes) $(RM) $(addprefix $(DESTDIR)$(libdir)/,$(LIBGO_SHARED) $(LIBGO_SONAME) $(LIBGO_SYMLINK)) endif - # Uninstall debugging symbols - $(RM) $(DESTDIR)$(libdbgdir)/$(LIB_SONAME) # Uninstall configuration files $(RM) $(DESTDIR)$(pkgconfdir)/$(LIB_PKGCFG) # Uninstall binary files $(RM) $(DESTDIR)$(bindir)/$(BIN_NAME) # Uninstall documentation files - $(RM) -r $(DESTDIR)$(docdir)/$(LIB_NAME)-$(VERSION) + # $(RM) -r $(DESTDIR)$(docdir)/$(LIB_NAME)-$(VERSION) dist: DESTDIR:=$(DIST_DIR)/$(LIB_NAME)_$(VERSION)$(addprefix -,$(TAG)) dist: install @@ -320,12 +299,6 @@ depsclean: ifeq ($(WITH_NVCGO), yes) -$(MAKE) -f $(MAKE_DIR)/nvcgo.mk clean endif -ifeq ($(WITH_LIBELF), no) - -$(MAKE) -f $(MAKE_DIR)/elftoolchain.mk clean -endif -ifeq ($(WITH_TIRPC), yes) - -$(MAKE) -f $(MAKE_DIR)/libtirpc.mk clean -endif mostlyclean: $(RM) $(LIB_OBJS) $(LIB_STATIC_OBJ) $(BIN_OBJS) $(DEPENDENCIES) diff --git a/mk/common.mk b/mk/common.mk index f6d5fbf..594ec08 100644 --- a/mk/common.mk +++ b/mk/common.mk @@ -19,6 +19,7 @@ RPCGEN ?= rpcgen BMAKE ?= MAKEFLAGS= bmake DOCKER ?= docker PATCH ?= patch +PKG_CONFIG ?= pkg-config UID := $(shell id -u) GID := $(shell id -g) diff --git a/mk/nvcgo.mk b/mk/nvcgo.mk index 0060f0e..877a72b 100644 --- a/mk/nvcgo.mk +++ b/mk/nvcgo.mk @@ -17,7 +17,7 @@ include $(MAKE_DIR)/common.mk ##### Source definitions ##### PREFIX := nvcgo -SRCS_DIR := $(DEPS_DIR)/src/$(PREFIX) +SRCS_DIR := $(CURDIR)/src/$(PREFIX) VERSION := $(VERSION) ##### Public rules ##### @@ -25,9 +25,6 @@ VERSION := $(VERSION) .PHONY: all install clean build: - $(RM) -rf $(SRCS_DIR) - $(CP) -R $(CURDIR)/src/$(PREFIX) $(SRCS_DIR) - $(MAKE) -C $(SRCS_DIR) VERSION=$(VERSION) clean $(MAKE) -C $(SRCS_DIR) VERSION=$(VERSION) build install: build diff --git a/mk/nvidia-modprobe.mk b/mk/nvidia-modprobe.mk index 3406222..339275f 100644 --- a/mk/nvidia-modprobe.mk +++ b/mk/nvidia-modprobe.mk @@ -25,7 +25,7 @@ PATCH_FILE := $(MAKE_DIR)/nvidia-modprobe.patch ARFLAGS := -rU CPPFLAGS := -D_FORTIFY_SOURCE=2 -DNV_LINUX -CFLAGS := -O2 -g -fdata-sections -ffunction-sections -fstack-protector -fno-strict-aliasing -fPIC +CFLAGS := -fdata-sections -ffunction-sections -fstack-protector -fno-strict-aliasing -fPIC $(CFLAGS) ##### Private rules ##### diff --git a/src/nvcgo/Makefile b/src/nvcgo/Makefile index eaee95a..7d5bd74 100644 --- a/src/nvcgo/Makefile +++ b/src/nvcgo/Makefile @@ -31,15 +31,15 @@ OBJ_NAME := $(LIB_NAME).so HDR_NAME := $(LIB_NAME).h CTYPES_H := ctypes.h -CGO_CFLAGS := -std=gnu11 -O2 -CGO_LDFLAGS := -Wl,--gc-sections -Wl,-s -Wl,-soname,$(LIB_SONAME) +CGO_CFLAGS := -std=gnu11 $(CGO_CFLAGS) +CGO_LDFLAGS := -Wl,--gc-sections -Wl,-soname,$(LIB_SONAME) $(CGO_LDFLAGS) build: $(OBJ_NAME) $(OBJ_NAME): $(wildcard $(CURDIR)/*.go) $(wildcard */*.go) export CGO_CFLAGS="$(CGO_CFLAGS)"; \ - export CGO_LDFLAGS="$(CGO_LDFLAGS)"; \ - $(GO) build -o $(@) -ldflags "-s -w" -buildmode=c-shared . + export CGO_LDFLAGS="-Wl,--gc-sections -Wl,-soname,$(LIB_SONAME) $(CGO_LDFLAGS)"; \ + $(GO) build -o $(@) -ldflags "$(GO_LDFLAGS)" -buildmode=c-shared . install: $(OBJ_NAME) $(INSTALL) -d -m 755 $(addprefix $(DESTDIR),$(libdir) $(includedir)/$(PKG_NAME))