--- a/pipcl.py
+++ b/pipcl.py
@@ -1568,7 +1568,7 @@ def build_extension(
 
             rpath_flag = ''
         else:
-            rpath_flag = "-Wl,-rpath,'$ORIGIN',-z,origin"
+            rpath_flag = ""
         path_so = f'{outdir}/{path_so_leaf}'
         # Fun fact - on Linux, if the -L and -l options are before '{path_cpp}'
         # they seem to be ignored...
@@ -1665,7 +1665,7 @@ def build_extension(
 #
 
 
-def base_compiler(vs=None, pythonflags=None, cpp=False, use_env=True):
+def base_compiler(vs=None, pythonflags=None, cpp=False, use_env=False):
     '''
     Returns basic compiler command and PythonFlags.
 
@@ -1829,7 +1829,7 @@ def run( command, capture=False, check=1):
     lines = _command_lines( command)
     nl = '\n'
     log2( f'Running: {nl.join(lines)}')
-    sep = ' ' if windows() else '\\\n'
+    sep = ' '
     command2 = sep.join( lines)
     cp = subprocess.run(
             command2,
@@ -1849,7 +1849,7 @@ def darwin():
     return sys.platform.startswith( 'darwin')
 
 def windows():
-    return platform.system() == 'Windows'
+    return platform.system() == 'Windows' and 'MSC' in sys.version
 
 def wasm():
     return os.environ.get( 'OS') in ('wasm', 'wasm-mt')
@@ -1932,10 +1932,10 @@ class PythonFlags:
                     python_config = f'{python_exe}-config'
             log1(f'Using {python_config=}.')
             try:
-                self.includes = run( f'{python_config} --includes', capture=1).strip()
+                self.includes = run( f'sh -c "{python_config} --includes"', capture=1).strip()
             except Exception as e:
                 raise Exception('We require python development tools to be installed.') from e
-            self.ldflags = run( f'{python_config} --ldflags', capture=1).strip()
+            self.ldflags = run( f'sh -c "{python_config} --ldflags"', capture=1).strip()
             if linux():
                 # It seems that with python-3.10 on Linux, we can get an
                 # incorrect -lcrypt flag that on some systems (e.g. WSL)
--- a/setup.py
+++ b/setup.py
@@ -499,7 +499,7 @@ linux = sys.platform.startswith( 'linux') or 'gnu' in sys.platform
 openbsd = sys.platform.startswith( 'openbsd')
 freebsd = sys.platform.startswith( 'freebsd')
 darwin = sys.platform.startswith( 'darwin')
-windows = platform.system() == 'Windows' or platform.system().startswith('CYGWIN')
+windows = platform.system() == 'Windows' and 'MSC' in sys.version
 msys2 = platform.system().startswith('MSYS_NT-')
 pyodide = os.environ.get('OS') == 'pyodide'
 
@@ -1012,7 +1012,7 @@ def _extension_flags( mupdf_local, mupdf_build_dir, build_type):
         libraries = f'{mupdf_local}\\platform\\{infix}\\{wp.cpu.windows_subdir}{build_type_infix}\\{libs}'
         compiler_extra = ''
     else:
-        libs = ['mupdf']
+        libs = ['mupdf', 'mupdf-third', 'jbig2dec', 'openjp2', 'jpeg', 'png', 'z', 'freetype', 'gumbo', 'harfbuzz']
         defines = None
         compiler_extra += (
                 ' -Wall'
@@ -1040,6 +1040,9 @@ def _extension_flags( mupdf_local, mupdf_build_dir, build_type):
         # Use system MuPDF.
         includes = list()
         pi = os.environ.get('PYMUPDF_INCLUDES')
+        mingw_prefix = os.getenv("MINGW_PREFIX", "/ucrt64")
+        includes.append(f'{mingw_prefix}/include')
+        includes.append(f'{mingw_prefix}/include/freetype2')
         if pi:
             includes += pi.split(':')
         pmi = os.environ.get('PYMUPDF_MUPDF_INCLUDE')
