--- a/Makerules
+++ b/Makerules
@@ -5,6 +5,8 @@ ifeq ($(OS),)
   OS := $(shell uname)
 endif
 
+PKG_CONFIG ?= pkg-config
+CC_FOR_BUILD ?= $(CC)
 WARNING_CFLAGS := -Wall -Wsign-compare
 
 # Feature configuration options
@@ -222,13 +224,13 @@ ifeq ($(OS),Darwin)
   ifneq ($(ARCHFLAGS),)
     $(warning "MacOS with ARCHFLAGS set. Assuming we are building for arm64, and setting HAVE_LIBCRYPTO to no.")
     HAVE_LIBCRYPTO := no
-  else ifeq (, $(shell command -v pkg-config))
+  else ifeq (, $(shell command -v $(PKG_CONFIG)))
     $(warning "No pkg-config found, install it for proper integration of libcrypto")
   else
-    HAVE_LIBCRYPTO := $(shell pkg-config --exists 'libcrypto >= 1.1.0' && echo yes)
+    HAVE_LIBCRYPTO := $(shell $(PKG_CONFIG) --exists 'libcrypto >= 1.1.0' && echo yes)
     ifeq ($(HAVE_LIBCRYPTO),yes)
-      LIBCRYPTO_CFLAGS := $(shell pkg-config --cflags libcrypto) -DHAVE_LIBCRYPTO
-      LIBCRYPTO_LIBS := $(shell pkg-config --libs libcrypto)
+      LIBCRYPTO_CFLAGS := $(shell $(PKG_CONFIG) --cflags libcrypto) -DHAVE_LIBCRYPTO
+      LIBCRYPTO_LIBS := $(shell $(PKG_CONFIG) --libs libcrypto)
     endif
   endif
 
@@ -242,78 +244,78 @@ else
     LDFLAGS += -pthread
   endif
 
-  ifeq ($(shell pkg-config --exists 'freetype2 >= 18.3.12' && echo yes),yes)
-    SYS_FREETYPE_CFLAGS := $(shell pkg-config --cflags freetype2)
-    SYS_FREETYPE_LIBS := $(shell pkg-config --libs freetype2)
+  ifeq ($(shell $(PKG_CONFIG) --exists 'freetype2 >= 18.3.12' && echo yes),yes)
+    SYS_FREETYPE_CFLAGS := $(shell $(PKG_CONFIG) --cflags freetype2)
+    SYS_FREETYPE_LIBS := $(shell $(PKG_CONFIG) --libs freetype2)
   endif
-  ifeq ($(shell pkg-config --exists 'gumbo >= 0.10.0' && echo yes),yes)
-    SYS_GUMBO_CFLAGS := $(shell pkg-config --cflags gumbo)
-    SYS_GUMBO_LIBS := $(shell pkg-config --libs gumbo)
+  ifeq ($(shell $(PKG_CONFIG) --exists 'gumbo >= 0.10.0' && echo yes),yes)
+    SYS_GUMBO_CFLAGS := $(shell $(PKG_CONFIG) --cflags gumbo)
+    SYS_GUMBO_LIBS := $(shell $(PKG_CONFIG) --libs gumbo)
   endif
-  ifeq ($(shell pkg-config --exists 'harfbuzz >= 2.0.0' && echo yes),yes)
-    SYS_HARFBUZZ_CFLAGS := $(shell pkg-config --cflags harfbuzz)
-    SYS_HARFBUZZ_LIBS := $(shell pkg-config --libs harfbuzz)
+  ifeq ($(shell $(PKG_CONFIG) --exists 'harfbuzz >= 2.0.0' && echo yes),yes)
+    SYS_HARFBUZZ_CFLAGS := $(shell $(PKG_CONFIG) --cflags harfbuzz)
+    SYS_HARFBUZZ_LIBS := $(shell $(PKG_CONFIG) --libs harfbuzz)
   endif
-  ifeq ($(shell pkg-config --exists lcms2 && echo yes),yes)
-    SYS_LCMS2_CFLAGS := $(shell pkg-config --cflags lcms2)
-    SYS_LCMS2_LIBS := $(shell pkg-config --libs lcms2)
+  ifeq ($(shell $(PKG_CONFIG) --exists lcms2 && echo yes),yes)
+    SYS_LCMS2_CFLAGS := $(shell $(PKG_CONFIG) --cflags lcms2)
+    SYS_LCMS2_LIBS := $(shell $(PKG_CONFIG) --libs lcms2)
   endif
-  ifeq ($(shell pkg-config --exists libjpeg && echo yes),yes)
-    SYS_LIBJPEG_CFLAGS := $(shell pkg-config --cflags libjpeg)
-    SYS_LIBJPEG_LIBS := $(shell pkg-config --libs libjpeg)
+  ifeq ($(shell $(PKG_CONFIG) --exists libjpeg && echo yes),yes)
+    SYS_LIBJPEG_CFLAGS := $(shell $(PKG_CONFIG) --cflags libjpeg)
+    SYS_LIBJPEG_LIBS := $(shell $(PKG_CONFIG) --libs libjpeg)
   endif
-  ifeq ($(shell pkg-config --exists 'libopenjp2 >= 2.1.0' && echo yes),yes)
-    SYS_OPENJPEG_CFLAGS := $(shell pkg-config --cflags libopenjp2)
-    SYS_OPENJPEG_LIBS := $(shell pkg-config --libs libopenjp2)
+  ifeq ($(shell $(PKG_CONFIG) --exists 'libopenjp2 >= 2.1.0' && echo yes),yes)
+    SYS_OPENJPEG_CFLAGS := $(shell $(PKG_CONFIG) --cflags libopenjp2)
+    SYS_OPENJPEG_LIBS := $(shell $(PKG_CONFIG) --libs libopenjp2)
   endif
-  ifeq ($(shell pkg-config --exists 'zlib >= 1.2.6' && echo yes),yes)
-    SYS_ZLIB_CFLAGS := $(shell pkg-config --cflags zlib)
-    SYS_ZLIB_LIBS := $(shell pkg-config --libs zlib)
+  ifeq ($(shell $(PKG_CONFIG) --exists 'zlib >= 1.2.6' && echo yes),yes)
+    SYS_ZLIB_CFLAGS := $(shell $(PKG_CONFIG) --cflags zlib)
+    SYS_ZLIB_LIBS := $(shell $(PKG_CONFIG) --libs zlib)
   endif
-  ifeq ($(shell pkg-config --exists 'libbrotlidec libbrotlienc >= 0.6.0' && echo yes),yes)
-    SYS_BROTLI_CFLAGS := $(shell pkg-config --cflags libbrotlidec libbrotlienc)
-    SYS_BROTLI_LIBS := $(shell pkg-config --libs libbrotlidec libbrotlienc)
+  ifeq ($(shell $(PKG_CONFIG) --exists 'libbrotlidec libbrotlienc >= 0.6.0' && echo yes),yes)
+    SYS_BROTLI_CFLAGS := $(shell $(PKG_CONFIG) --cflags libbrotlidec libbrotlienc)
+    SYS_BROTLI_LIBS := $(shell $(PKG_CONFIG) --libs libbrotlidec libbrotlienc)
   endif
 
-  HAVE_SYS_LEPTONICA := $(shell pkg-config --exists 'lept >= 1.7.4' && echo yes)
+  HAVE_SYS_LEPTONICA := $(shell $(PKG_CONFIG) --exists 'lept >= 1.7.4' && echo yes)
   ifeq ($(HAVE_SYS_LEPTONICA),yes)
-    SYS_LEPTONICA_CFLAGS := $(shell pkg-config --cflags lept)
-    SYS_LEPTONICA_LIBS := $(shell pkg-config --libs lept)
+    SYS_LEPTONICA_CFLAGS := $(shell $(PKG_CONFIG) --cflags lept)
+    SYS_LEPTONICA_LIBS := $(shell $(PKG_CONFIG) --libs lept)
   endif
 
-  HAVE_SYS_TESSERACT := $(shell pkg-config --exists 'tesseract >= 4.0.0' && echo yes)
+  HAVE_SYS_TESSERACT := $(shell $(PKG_CONFIG) --exists 'tesseract >= 4.0.0' && echo yes)
   ifeq ($(HAVE_SYS_TESSERACT),yes)
-    SYS_TESSERACT_CFLAGS := $(shell pkg-config --cflags tesseract)
-    SYS_TESSERACT_LIBS := $(shell pkg-config --libs tesseract)
+    SYS_TESSERACT_CFLAGS := $(shell $(PKG_CONFIG) --cflags tesseract)
+    SYS_TESSERACT_LIBS := $(shell $(PKG_CONFIG) --libs tesseract)
   endif
 
-  HAVE_SYS_LIBARCHIVE := $(shell pkg-config --exists 'libarchive' && echo yes)
+  HAVE_SYS_LIBARCHIVE := $(shell $(PKG_CONFIG) --exists 'libarchive' && echo yes)
   ifeq ($(HAVE_SYS_LIBARCHIVE),yes)
-    SYS_LIBARCHIVE_CFLAGS := $(shell pkg-config --cflags libarchive)
-    SYS_LIBARCHIVE_LIBS := $(shell pkg-config --libs libarchive)
+    SYS_LIBARCHIVE_CFLAGS := $(shell $(PKG_CONFIG) --cflags libarchive)
+    SYS_LIBARCHIVE_LIBS := $(shell $(PKG_CONFIG) --libs libarchive)
   endif
 
-  HAVE_SYS_ZXINGCPP := $(shell pkg-config --exists 'zxing >= 2.0.0' && echo yes)
+  HAVE_SYS_ZXINGCPP := $(shell $(PKG_CONFIG) --exists 'zxing >= 2.0.0' && echo yes)
   ifeq ($(HAVE_SYS_ZXINGCPP),yes)
-    SYS_ZXINGCPP_CFLAGS := $(shell pkg-config --cflags zxing)
-    SYS_ZXINGCPP_LIBS := $(shell pkg-config --libs zxing)
+    SYS_ZXINGCPP_CFLAGS := $(shell $(PKG_CONFIG) --cflags zxing)
+    SYS_ZXINGCPP_LIBS := $(shell $(PKG_CONFIG) --libs zxing)
   endif
 
-  HAVE_SYS_CURL := $(shell pkg-config --exists libcurl && echo yes)
+  HAVE_SYS_CURL := $(shell $(PKG_CONFIG) --exists libcurl && echo yes)
   ifeq ($(HAVE_SYS_CURL),yes)
-    SYS_CURL_CFLAGS := $(shell pkg-config --cflags libcurl)
-    SYS_CURL_LIBS := $(shell pkg-config --libs libcurl)
+    SYS_CURL_CFLAGS := $(shell $(PKG_CONFIG) --cflags libcurl)
+    SYS_CURL_LIBS := $(shell $(PKG_CONFIG) --libs libcurl)
   endif
 
   ifeq ($(HAVE_GLUT),)
-    HAVE_GLUT := $(shell pkg-config --exists gl x11 xrandr && echo yes)
+    HAVE_GLUT := $(shell $(PKG_CONFIG) --exists gl x11 xrandr && echo yes)
   endif
   ifeq ($(HAVE_GLUT),yes)
-    SYS_GL_CFLAGS := $(shell pkg-config --cflags gl x11 xrandr)
-    SYS_GL_LIBS := $(shell pkg-config --libs gl x11 xrandr)
-    ifeq ($(shell pkg-config --exists glut && echo yes),yes)
-      SYS_GLUT_CFLAGS := $(shell pkg-config --cflags glut)
-      SYS_GLUT_LIBS := $(shell pkg-config --libs glut)
+    SYS_GL_CFLAGS := $(shell $(PKG_CONFIG) --cflags gl x11 xrandr)
+    SYS_GL_LIBS := $(shell $(PKG_CONFIG) --libs gl x11 xrandr)
+    ifeq ($(shell $(PKG_CONFIG) --exists glut && echo yes),yes)
+      SYS_GLUT_CFLAGS := $(shell $(PKG_CONFIG) --cflags glut)
+      SYS_GLUT_LIBS := $(shell $(PKG_CONFIG) --libs glut)
     else
       SYS_GLUT_CFLAGS :=
       SYS_GLUT_LIBS := -lglut
@@ -321,19 +323,19 @@ else
   endif
 
   ifeq ($(HAVE_X11),)
-    HAVE_X11 := $(shell pkg-config --exists x11 xext && echo yes)
+    HAVE_X11 := $(shell $(PKG_CONFIG) --exists x11 xext && echo yes)
   endif
   ifeq ($(HAVE_X11),yes)
-    X11_CFLAGS := $(shell pkg-config --cflags x11 xext)
-    X11_LIBS := $(shell pkg-config --libs x11 xext)
+    X11_CFLAGS := $(shell $(PKG_CONFIG) --cflags x11 xext)
+    X11_LIBS := $(shell $(PKG_CONFIG) --libs x11 xext)
   endif
 
   ifeq ($(HAVE_LIBCRYPTO),)
-    HAVE_LIBCRYPTO := $(shell pkg-config --exists 'libcrypto >= 1.1.0' && echo yes)
+    HAVE_LIBCRYPTO := $(shell $(PKG_CONFIG) --exists 'libcrypto >= 1.1.0' && echo yes)
   endif
   ifeq ($(HAVE_LIBCRYPTO),yes)
-    LIBCRYPTO_CFLAGS := $(shell pkg-config --cflags libcrypto) -DHAVE_LIBCRYPTO
-    LIBCRYPTO_LIBS := $(shell pkg-config --libs libcrypto)
+    LIBCRYPTO_CFLAGS := $(shell $(PKG_CONFIG) --cflags libcrypto) -DHAVE_LIBCRYPTO
+    LIBCRYPTO_LIBS := $(shell $(PKG_CONFIG) --libs libcrypto)
   endif
 
   HAVE_PTHREAD := yes
