Skip to content

Commit

Permalink
Support Linux/!x86, Darwin/arm64, various build improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
hillu committed Nov 21, 2024
1 parent 029563a commit 84b777e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 19 deletions.
43 changes: 29 additions & 14 deletions 3rdparty.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@ endef
# Determine host architecture:
3rdparty_NATIVE_ARCH := $(shell cc -dumpmachine)
# Determine target architectures:
# On Linux, we can cross-build for Linux and Windows.
# On MacOSX, we can only build for MacOSX.
# On Linux, we can cross-build for Windows.
# On other systems, we only build for the respective host architecture
$(or \
$(if $(or $(findstring -linux-gnu,$(3rdparty_NATIVE_ARCH)),\
$(findstring -redhat-linux,$(3rdparty_NATIVE_ARCH))),\
$(eval 3rdparty_ARCHS=i386-linux-musl x86_64-linux-musl i686-w64-mingw32 x86_64-w64-mingw32)\
$(if $(or $(findstring x86_64-linux-gnu,$(3rdparty_NATIVE_ARCH)),\
$(findstring x86_64-redhat-linux,$(3rdparty_NATIVE_ARCH))),\
$(eval 3rdparty_ARCHS=\
i386-linux-musl x86_64-linux-musl \
i686-w64-mingw32 x86_64-w64-mingw32)\
$(foreach arch,i686-w64-mingw32 x86_64-w64-mingw32,\
$(if $(not $(shell which $(arch)-gcc)),$(error $(arch)-gcc not found)))),\
$(if $(findstring -linux-gnu,$(3rdparty_NATIVE_ARCH)),\
$(eval 3rdparty_ARCHS=\
$(patsubst %-linux-gnu,%-linux-musl,$(3rdparty_NATIVE_ARCH)) \
i686-w64-mingw32 x86_64-w64-mingw32)\
$(foreach arch,i686-w64-mingw32 x86_64-w64-mingw32,\
$(if $(not $(shell which $(arch)-gcc)),$(error $(arch)-gcc not found)))),\
$(if $(or $(findstring -linux-gnu,$(3rdparty_NATIVE_ARCH))),\
$(eval 3rdparty_ARCHS=$(patsubst %-linux-gnu,%-linux-musl,$(3rdparty_NATIVE_ARCH)))),\
$(if $(or $(findstring -apple-darwin,$(3rdparty_NATIVE_ARCH)),\
$(findstring -freebsd,$(3rdparty_NATIVE_ARCH))),\
$(eval 3rdparty_ARCHS=$(3rdparty_NATIVE_ARCH))),\
Expand Down Expand Up @@ -48,15 +58,16 @@ yara_PREP := ./bootstrap.sh
musl_VERSION := 1.2.5
musl_URL := https://musl.libc.org/releases/musl-$(musl_VERSION).tar.gz
musl_ARCHS := $(filter %-linux-musl,$(3rdparty_ARCHS))
musl_PATCHES := getauxval.patch

openssl_VERSION := 1.1.1w
openssl_URL := https://www.openssl.org/source/openssl-$(openssl_VERSION).tar.gz
openssl_ARCHS := $(3rdparty_ARCHS)

# Declare dependencies
$(eval $(call DEPENDS,yara,openssl,))
$(eval $(call DEPENDS,yara,musl,i386-linux-musl x86_64-linux-musl))
$(eval $(call DEPENDS,openssl,musl,i386-linux-musl x86_64-linux-musl))
$(eval $(call DEPENDS,yara,musl,i386-linux-musl x86_64-linux-musl aarch64-linux-musl))
$(eval $(call DEPENDS,openssl,musl,i386-linux-musl x86_64-linux-musl aarch64-linux-musl))

# Rules/Templates
# ---------------
Expand Down Expand Up @@ -103,7 +114,8 @@ _3rdparty/build/$1/musl-$(musl_VERSION)/.build-stamp: _3rdparty/src/musl-$(musl_
--syslibdir=$(abspath _3rdparty/tgt/$1/lib) \
CC=gcc \
CROSS_COMPILE= \
CFLAGS="-fPIC $(if $(findstring x86_64,$1),-m64,-m32)"
CFLAGS="-fPIC $(or $(if $(findstring x86_64,$1),-m64),\
$(if $(or $(findstring i386,$1),$(findstring i686,$1)),-m32))"
$(MAKE) -s -j$(3rdparty_JOBS) -C $$(@D) AR=ar RANLIB=ranlib
$(MAKE) -s -C $$(@D) install
$(abspath _3rdparty)/patch-musl-spec.sh $(abspath _3rdparty/tgt/$1)
Expand All @@ -117,6 +129,7 @@ endef
# Out-of-tree build for yara, architecture $1, with dependency on musl
# where appropriate
define build_yara_TEMPLATE
_3rdparty/build/$1/yara-$(yara_VERSION)/.build-stamp: export PKG_CONFIG_PATH=$(abspath _3rdparty/tgt/$1/lib/pkgconfig)
_3rdparty/build/$1/yara-$(yara_VERSION)/.build-stamp: _3rdparty/src/yara-$(yara_VERSION)/.unpack-stamp
@mkdir -p $$(@D)
cd $$(@D) && $$(abspath $$(<D))/configure \
Expand All @@ -127,10 +140,7 @@ _3rdparty/build/$1/yara-$(yara_VERSION)/.build-stamp: _3rdparty/src/yara-$(yara_
--disable-magic --disable-cuckoo --enable-dotnet --enable-macho --enable-dex \
CC=$$(firstword $$(shell PATH=$$(PATH) which $1-gcc gcc cc)) \
CPPFLAGS="-I$(abspath _3rdparty/tgt/$1/include) $(if $(findstring -mingw32,$1),-UHAVE__MKGMTIME)" \
CFLAGS="$(if $(findstring -linux-musl,$1),-static)" \
LDFLAGS="$$(shell PKG_CONFIG_PATH=$$(abspath _3rdparty/tgt/$1/lib/pkgconfig) \
pkg-config --static --libs libcrypto \
| $(SED) -e 's/-ldl//g' )"
CFLAGS="$(if $(findstring -linux-musl,$1),-static)"
$(MAKE) -s -C $$(@D) uninstall
$(MAKE) -s -j$(3rdparty_JOBS) -C $$(@D)
$(MAKE) -s -C $$(@D) install
Expand All @@ -148,12 +158,17 @@ _3rdparty/build/$1/openssl-$(openssl_VERSION)/.build-stamp: \
$(if $(shell which cc),cc),\
$(error 3rdparty/openssl: gcc or cc not found))
_3rdparty/build/$1/openssl-$(openssl_VERSION)/.build-stamp: \
private export CFLAGS=$(if $(findstring -linux-musl,$1),-static) $(if $(findstring x86_64,$1),-m64,-m32)
private export CFLAGS=$(if $(findstring -linux-musl,$1),-static) $(or $(if $(findstring x86_64,$1),-m64),\
$(if $(or $(findstring i386,$1),$(findstring i686,$1)),-m32))
_3rdparty/build/$1/openssl-$(openssl_VERSION)/.build-stamp: \
private export MACHINE=$(or \
$(if $(and $(findstring freebsd,$1),$(findstring x86_64,$1)),\
$(patsubst x86_64-%,amd64-%,$1)),\
$(if $(findstring x86_64,$1),x86_64,i386))
$(if $(or $(findstring aarch64,$1),$(findstring arm64,$1)),\
aarch64),\
$(if $(findstring x86_64,$1),x86_64),\
$(if $(or $(findstring i386,$1),$(findstring i686,$1)),i386),\
$(error 3rdparty/openssl: Unknown MACHINE setting for $1))
_3rdparty/build/$1/openssl-$(openssl_VERSION)/.build-stamp: \
private export SYSTEM=$(or \
$(if $(findstring mingw,$1),$(if $(findstring x86_64,$1),MINGW64,MINGW32)),\
Expand Down
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,17 @@ $(foreach arch,$(ARCHS),\
$(eval _build/$(arch)/%: private export PKG_CONFIG_PATH=$(CURDIR)/_3rdparty/tgt/$(arch)/lib/pkgconfig)\
$(eval _build/$(arch)/%: private export GOOS=\
$(or $(if $(findstring linux,$(arch)),linux),\
$(if $(findstring mingw,$(arch)),windows),\
$(if $(findstring darwin,$(arch)),darwin),\
$(if $(findstring freebsd,$(arch)),freebsd),\
$(error Could not derive GOOS from $(arch))))\
$(if $(findstring mingw,$(arch)),windows),\
$(if $(findstring darwin,$(arch)),darwin),\
$(if $(findstring freebsd,$(arch)),freebsd),\
$(error Could not derive GOOS from $(arch))))\
$(eval _build/$(arch)/%: private export GOARCH=\
$(or $(if $(findstring x86_64,$(arch)),amd64),\
$(if $(or $(findstring arm64,$(arch)),$(findstring aarch64,$(arch))),arm64),\
$(if $(or $(findstring i386,$(arch)),$(findstring i686,$(arch))),386),\
$(error Could not derive GOARCH from $(arch)))))

$(if $(findstring linux,$(3rdparty_NATIVE_ARCH)),\
$(if $(findstring x86_64-linux,$(3rdparty_NATIVE_ARCH)),\
$(eval unit-test: private export CC=x86_64-linux-musl-gcc)\
$(eval unit-test: private export GOOS=linux)\
$(eval unit-test: private export GOARCH=amd64)\
Expand Down
36 changes: 36 additions & 0 deletions _3rdparty/getauxval.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Message-ID: <[email protected]>
Date: Thu, 12 Oct 2023 20:31:48 +0200
From: Szabolcs Nagy <[email protected]>
To: [email protected]
Subject: [PATCH] make __getauxval a public ABI symbol

This is needed so that libgcc can access AT_HWCAP without violating
link namespace rules.

Internally musl already used __getauxval symbol for the same reason,
we just remove the hidden marking.

Currently libgcc only uses __getauxval on aarch64 for outline atomics
and only on glibc, but there is plan to add more usage (this time in
the unwinder) which may cause link failure on musl in the future.
(libgcc cannot easily check for libc symbols due to bootstrap order,
the feature could be unconditionally disabled for *-linux-musl, but
it's probably more future proof to provide the symbol.)
---
src/include/sys/auxv.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/include/sys/auxv.h b/src/include/sys/auxv.h
index 9358a4a5..63c5bfe9 100644
--- a/src/include/sys/auxv.h
+++ b/src/include/sys/auxv.h
@@ -5,6 +5,6 @@

#include <features.h>

-hidden unsigned long __getauxval(unsigned long);
+unsigned long __getauxval(unsigned long);

#endif
--
2.41.0

0 comments on commit 84b777e

Please sign in to comment.