1515from collections .abc import Callable
1616from pathlib import Path
1717
18- try :
19- from setuptools ._distutils .ccompiler import CCompiler , new_compiler
20- from setuptools ._distutils .errors import CCompilerError , DistutilsExecError
21- from setuptools ._distutils .sysconfig import customize_compiler
22- except ImportError : # pragma: no cover - fallback for older Python environments
23- from distutils .ccompiler import CCompiler , new_compiler # type: ignore
24- from distutils .errors import CCompilerError , DistutilsExecError # type: ignore
25- from distutils .sysconfig import customize_compiler # type: ignore
18+
19+ from setuptools ._distutils .ccompiler import CCompiler , new_compiler
20+ from setuptools ._distutils .errors import CCompilerError , DistutilsExecError
21+ from setuptools ._distutils .sysconfig import customize_compiler
2622
2723
2824ROOT_DIR = Path (__file__ ).resolve ().parent
5147
5248LIBRARY_BASENAME = "libpcre2-8"
5349
54- __all__ = ["MODULE_SOURCES" , "collect_build_config" ]
50+ RUNTIME_LIBRARY_FILES : list [str ] = []
51+
52+ __all__ = ["MODULE_SOURCES" , "collect_build_config" , "RUNTIME_LIBRARY_FILES" ]
5553
5654
5755def _run_pkg_config (* args : str ) -> list [str ]:
@@ -221,9 +219,10 @@ def _has_built_library() -> bool:
221219 "-B" ,
222220 str (build_dir ),
223221 "-DPCRE2_SUPPORT_JIT=ON" ,
224- "-DPCRE2_BUILD_PCRE2_16 =ON" ,
222+ "-DPCRE2_BUILD_PCRE2_8 =ON" ,
225223 "-DPCRE2_BUILD_TESTS=OFF" ,
226- "-DBUILD_SHARED_LIBS=ON" ,
224+ "-DBUILD_SHARED_LIBS=OFF" , # don't build DLLs
225+ "-DPCRE2_STATIC=ON" , # ensure static linking symbols are used
227226 ]
228227 if not _is_windows_platform ():
229228 cmake_args .append ("-DCMAKE_BUILD_TYPE=Release" )
@@ -235,8 +234,9 @@ def _has_built_library() -> bool:
235234 str (build_dir ),
236235 ]
237236 if _is_windows_platform ():
238- build_command .extend (["--config" , "Release" ])
239- build_command .extend (["--" , "-j4" ])
237+ build_command .extend (["--config" , "Release" , "--parallel" , "4" ])
238+ else :
239+ build_command .extend (["--" , "-j4" ])
240240 subprocess .run (build_command , cwd = destination , env = env , check = True )
241241 except (FileNotFoundError , subprocess .CalledProcessError ) as exc :
242242 cmake_error = exc
@@ -255,6 +255,8 @@ def _has_built_library() -> bool:
255255 "--enable-jit" ,
256256 "--enable-pcre2-8" ,
257257 "--disable-tests" ,
258+ "--enable-static" ,
259+ "--disable-shared" ,
258260 ]
259261 subprocess .run (configure_command , cwd = build_dir , env = env , check = True )
260262 subprocess .run (["make" , "-j4" ], cwd = build_dir , env = env , check = True )
@@ -452,6 +454,9 @@ def _augment_compile_flags(flags: list[str]) -> None:
452454 if _is_truthy_env ("PCRE2_DISABLE_OPT_FLAGS" ):
453455 return
454456
457+ if _is_windows_platform ():
458+ return
459+
455460 disable_native = _is_truthy_env ("PCRE2_DISABLE_NATIVE_FLAGS" )
456461 compiler = _get_test_compiler ()
457462 if not disable_native and _should_disable_native_flags_for_macos (compiler ):
@@ -789,7 +794,7 @@ def collect_build_config() -> dict[str, list[str] | list[tuple[str, str | None]]
789794 if env_ldflags :
790795 extra_link_args .extend (shlex .split (env_ldflags ))
791796
792- if not any (flag .startswith ("-std=" ) for flag in extra_compile_args ):
797+ if not _is_windows_platform () and not any (flag .startswith ("-std=" ) for flag in extra_compile_args ):
793798 extra_compile_args .append ("-std=c99" )
794799
795800 if not _has_header (include_dirs ):
@@ -798,7 +803,15 @@ def collect_build_config() -> dict[str, list[str] | list[tuple[str, str | None]]
798803 if not _has_library (library_dirs ):
799804 library_dirs .extend (_discover_library_dirs ())
800805
806+ global RUNTIME_LIBRARY_FILES
807+ runtime_libraries : list [str ] = []
808+
801809 if library_files :
810+ for runtime_path in library_files :
811+ lower_name = runtime_path .lower ()
812+ if lower_name .endswith (".dll" ) or lower_name .endswith (".dylib" ) or ".so" in Path (runtime_path ).name :
813+ _extend_unique (runtime_libraries , runtime_path )
814+
802815 linkable_files : list [str ] = []
803816 for path in library_files :
804817 suffix = Path (path ).suffix .lower ()
@@ -821,8 +834,18 @@ def collect_build_config() -> dict[str, list[str] | list[tuple[str, str | None]]
821834 if sys .platform .startswith ("linux" ) and "dl" not in libraries :
822835 libraries .append ("dl" )
823836
837+ if _is_windows_platform ():
838+ has_runtime_dll = any (path .lower ().endswith (".dll" ) for path in runtime_libraries )
839+ force_static_env = _is_truthy_env ("PCRE2_FORCE_STATIC" )
840+ if (force_static_env or (library_files and not has_runtime_dll )) and not any (
841+ macro [0 ] == "PCRE2_STATIC" for macro in define_macros
842+ ):
843+ define_macros .append (("PCRE2_STATIC" , "1" ))
844+
824845 _augment_compile_flags (extra_compile_args )
825846
847+ RUNTIME_LIBRARY_FILES = runtime_libraries
848+
826849 return {
827850 "include_dirs" : include_dirs ,
828851 "library_dirs" : library_dirs ,
0 commit comments