Skip to content

Commit 56b45f5

Browse files
committed
Add Nuitka-Python hooks.
1 parent ba24375 commit 56b45f5

File tree

7 files changed

+96
-19
lines changed

7 files changed

+96
-19
lines changed

setuptools/_distutils/_msvccompiler.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,17 +301,18 @@ def initialize(self, plat_name=None):
301301
self.rc = _find_exe("rc.exe", paths) # resource compiler
302302
self.mc = _find_exe("mc.exe", paths) # message compiler
303303
self.mt = _find_exe("mt.exe", paths) # message compiler
304+
self.dumpbin = _find_exe("dumpbin.exe", paths)
304305

305306
self.preprocess_options = None
306307
# bpo-38597: Always compile with dynamic linking
307308
# Future releases of Python 3.x will include all past
308309
# versions of vcruntime*.dll for compatibility.
309-
self.compile_options = ['/nologo', '/O2', '/W3', '/GL', '/DNDEBUG', '/MD']
310+
self.compile_options = ['/nologo', '/O2', '/W3', '/GL', '/DNDEBUG', '/MT']
310311

311312
self.compile_options_debug = [
312313
'/nologo',
313314
'/Od',
314-
'/MDd',
315+
'/MTd',
315316
'/Zi',
316317
'/W3',
317318
'/D_DEBUG',
@@ -493,6 +494,7 @@ def link(
493494
extra_postargs=None,
494495
build_temp=None,
495496
target_lang=None,
497+
extra_midargs=None,
496498
):
497499
if not self.initialized:
498500
self.initialize()
@@ -534,6 +536,8 @@ def link(
534536

535537
if extra_preargs:
536538
ld_args[:0] = extra_preargs
539+
if extra_midargs:
540+
ld_args.extend(extra_midargs)
537541
if extra_postargs:
538542
ld_args.extend(extra_postargs)
539543

@@ -588,6 +592,8 @@ def library_option(self, lib):
588592
return self.library_filename(lib)
589593

590594
def find_library_file(self, dirs, lib, debug=False):
595+
if len(dirs) == 1 and os.path.isfile(os.path.join(dirs[0], lib)):
596+
return os.path.join(dirs[0], lib)
591597
# Prefer a debugging library if found (and requested), but deal
592598
# with it if we don't have one.
593599
if debug:

setuptools/_distutils/ccompiler.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ def link(
662662
extra_postargs=None,
663663
build_temp=None,
664664
target_lang=None,
665+
extra_midargs=None,
665666
):
666667
"""Link a bunch of stuff together to create an executable or
667668
shared library file.
@@ -725,6 +726,7 @@ def link_shared_lib(
725726
build_temp=None,
726727
target_lang=None,
727728
):
729+
raise NotImplementedError('Building shared libs is disabled for Nuitka-Python')
728730
self.link(
729731
CCompiler.SHARED_LIBRARY,
730732
objects,
@@ -756,6 +758,7 @@ def link_shared_object(
756758
build_temp=None,
757759
target_lang=None,
758760
):
761+
raise NotImplementedError('Building shared libs is disabled for Nuitka-Python')
759762
self.link(
760763
CCompiler.SHARED_OBJECT,
761764
objects,
@@ -784,6 +787,7 @@ def link_executable(
784787
extra_preargs=None,
785788
extra_postargs=None,
786789
target_lang=None,
790+
extra_midargs=None,
787791
):
788792
self.link(
789793
CCompiler.EXECUTABLE,
@@ -799,6 +803,7 @@ def link_executable(
799803
extra_postargs,
800804
None,
801805
target_lang,
806+
extra_midargs,
802807
)
803808

804809
# -- Miscellaneous methods -----------------------------------------
@@ -1024,6 +1029,9 @@ def library_filename(
10241029
fmt = getattr(self, lib_type + "_lib_format")
10251030
ext = getattr(self, lib_type + "_lib_extension")
10261031

1032+
if libname.endswith(ext):
1033+
ext = ""
1034+
10271035
dir, base = os.path.split(libname)
10281036
filename = fmt % (base, ext)
10291037
if strip_dir:
@@ -1259,5 +1267,9 @@ def gen_lib_options(compiler, library_dirs, runtime_library_dirs, libraries):
12591267
f"no library file corresponding to '{lib}' found (skipping)"
12601268
)
12611269
else:
1262-
lib_opts.append(compiler.library_option(lib))
1270+
lib_file = compiler.find_library_file(library_dirs, lib_name)
1271+
if lib_file is not None and lib_file.endswith('.a'):
1272+
lib_opts.append(lib_file)
1273+
else:
1274+
lib_opts.append(compiler.library_option(lib))
12631275
return lib_opts

setuptools/_distutils/command/build_ext.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import os
99
import re
1010
import sys
11+
import json
12+
import shutil
1113
from distutils._log import log
1214
from site import USER_BASE
1315

@@ -583,18 +585,33 @@ def build_extension(self, ext):
583585
# Detect target language, if not provided
584586
language = ext.language or self.compiler.detect_language(sources)
585587

586-
self.compiler.link_shared_object(
587-
objects,
588-
ext_path,
589-
libraries=self.get_libraries(ext),
590-
library_dirs=ext.library_dirs,
591-
runtime_library_dirs=ext.runtime_library_dirs,
592-
extra_postargs=extra_args,
593-
export_symbols=self.get_export_symbols(ext),
588+
self.compiler.create_static_lib(
589+
objects, ext_path,
590+
output_dir=os.path.abspath("."),
594591
debug=self.debug,
595-
build_temp=self.build_temp,
596-
target_lang=language,
597-
)
592+
target_lang=language)
593+
594+
result_path = self.compiler.library_filename(ext_path,
595+
output_dir=os.path.abspath("."))
596+
597+
with open(result_path + '.link.json', 'w') as f:
598+
json.dump({
599+
'libraries': self.get_libraries(ext),
600+
'library_dirs': ext.library_dirs,
601+
'runtime_library_dirs': ext.runtime_library_dirs,
602+
'extra_postargs': extra_args}, f)
603+
604+
for lib in self.get_libraries(ext):
605+
for dir in ext.library_dirs:
606+
lib_install_dir = os.path.join(os.path.dirname(ext_path), dir)
607+
print(os.path.join(ext_path, dir, lib + '.lib'))
608+
if os.path.isfile(os.path.join(dir, lib + '.lib')):
609+
if not os.path.isabs(dir):
610+
if not os.path.exists(lib_install_dir):
611+
os.makedirs(lib_install_dir)
612+
shutil.copyfile(os.path.join(dir, lib + '.lib'),
613+
os.path.join(lib_install_dir, lib + '.lib'))
614+
break
598615

599616
def swig_sources(self, sources, extension):
600617
"""Walk the list of source files in 'sources', looking for SWIG

setuptools/_distutils/sysconfig.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ def customize_compiler(compiler):
350350
ldshared = _add_flags(ldshared, 'CPP')
351351
ldcxxshared = _add_flags(ldcxxshared, 'CPP')
352352

353+
cflags = ' '.join([x for x in cflags.split(' ') if
354+
not x.startswith('-I') or (
355+
'Nuitka-Python-Deps' not in x and 'dependencies' not in x)])
356+
353357
ar = os.environ.get('AR', ar)
354358

355359
archiver = ar + ' ' + os.environ.get('ARFLAGS', ar_flags)

setuptools/_distutils/unixccompiler.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ class UnixCCompiler(CCompiler):
131131
if sys.platform[:6] == "darwin":
132132
executables['ranlib'] = ["ranlib"]
133133

134+
# Nuitka: Make sure to use the original settings
135+
executables["compiler"] = sysconfig.get_config_var("CC")
136+
executables["compiler_so"] = sysconfig.get_config_var("CC")
137+
executables["compiler_cxx"] = sysconfig.get_config_var("CXX")
138+
executables["linker_so"] = sysconfig.get_config_var("CC")
139+
executables["linker_exe"] = sysconfig.get_config_var("CC")
140+
134141
# Needed for the filename generation methods provided by the base
135142
# class, CCompiler. NB. whoever instantiates/uses a particular
136143
# UnixCCompiler instance should set 'shared_lib_ext' -- we set a
@@ -240,6 +247,7 @@ def link(
240247
extra_postargs=None,
241248
build_temp=None,
242249
target_lang=None,
250+
extra_midargs=None,
243251
):
244252
objects, output_dir = self._fix_object_args(objects, output_dir)
245253
fixed_args = self._fix_lib_args(libraries, library_dirs, runtime_library_dirs)
@@ -252,11 +260,14 @@ def link(
252260
output_filename = os.path.join(output_dir, output_filename)
253261

254262
if self._need_link(objects, output_filename):
255-
ld_args = objects + self.objects + lib_opts + ['-o', output_filename]
263+
ld_args = objects + self.objects
256264
if debug:
257265
ld_args[:0] = ['-g']
266+
if extra_midargs:
267+
ld_args += extra_midargs
258268
if extra_preargs:
259269
ld_args[:0] = extra_preargs
270+
ld_args += lib_opts + ['-o', output_filename]
260271
if extra_postargs:
261272
ld_args.extend(extra_postargs)
262273
self.mkpath(os.path.dirname(output_filename))
@@ -265,6 +276,8 @@ def link(
265276
# building an executable or linker_so (with shared options)
266277
# when building a shared library.
267278
building_exe = target_desc == CCompiler.EXECUTABLE
279+
if not building_exe:
280+
raise NotImplemented("No shared libs in Nuitka-Python")
268281
linker = (
269282
self.linker_exe
270283
if building_exe
@@ -387,10 +400,10 @@ def find_library_file(self, dirs, lib, debug=False):
387400
assume that *all* Unix C compilers do,
388401
ignoring even GCC's "-static" option.
389402
"""
390-
lib_names = (
403+
lib_names = [lib] + [
391404
self.library_filename(lib, lib_type=type)
392405
for type in 'dylib xcode_stub shared static'.split()
393-
)
406+
]
394407

395408
roots = map(self._library_root, dirs)
396409

setuptools/_vendor/packaging/tags.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ def _generic_abi() -> list[str]:
255255
# - graalpy: '.graalpy-38-native-x86_64-darwin.dylib'
256256
# => graalpy_38_native
257257

258+
return [_normalize_string(sysconfig.get_config_var("SOABI"))]
259+
258260
ext_suffix = _get_config_var("EXT_SUFFIX", warn=True)
259261
if not isinstance(ext_suffix, str) or ext_suffix[0] != ".":
260262
raise SystemError("invalid sysconfig.get_config_var('EXT_SUFFIX')")

setuptools/command/build_ext.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import os
55
import sys
66
from collections.abc import Iterator
7+
import itertools
8+
import json
9+
import shutil
710
from importlib.machinery import EXTENSION_SUFFIXES
811
from importlib.util import cache_from_source as _compiled_file_name
912
from pathlib import Path
@@ -399,7 +402,7 @@ def _compile_and_remove_stub(self, stub_file: str):
399402
os.unlink(stub_file)
400403

401404

402-
if use_stubs or os.name == 'nt':
405+
if False: # use_stubs or os.name == 'nt':
403406
# Build shared libraries
404407
#
405408
def link_shared_object(
@@ -460,10 +463,30 @@ def link_shared_object(
460463

461464
assert output_dir is None # distutils build_ext doesn't pass this
462465
output_dir, filename = os.path.split(output_libname)
463-
basename, _ext = os.path.splitext(filename)
466+
basename, ext = os.path.splitext(filename)
464467
if self.library_filename("x").startswith('lib'):
465468
# strip 'lib' prefix; this is kludgy if some platform uses
466469
# a different prefix
467470
basename = basename[3:]
468471

469472
self.create_static_lib(objects, basename, output_dir, debug, target_lang)
473+
474+
result_path = self.library_filename(basename, output_dir=os.path.abspath("."))
475+
476+
with open(result_path + '.link.json', 'w') as f:
477+
json.dump({
478+
'libraries': libraries,
479+
'library_dirs': library_dirs,
480+
'extra_postargs': extra_postargs,
481+
'extra_preargs': extra_preargs}, f)
482+
483+
for lib in libraries:
484+
for dir in library_dirs:
485+
lib_install_dir = os.path.join(output_dir, dir)
486+
if os.path.isfile(os.path.join(dir, lib + '.lib')):
487+
if not os.path.isabs(dir):
488+
if not os.path.exists(lib_install_dir):
489+
os.makedirs(lib_install_dir)
490+
shutil.copyfile(os.path.join(dir, lib + '.lib'),
491+
os.path.join(lib_install_dir, lib + '.lib'))
492+
break

0 commit comments

Comments
 (0)