Skip to content

Commit 65299ca

Browse files
committed
Add Nuitka-Python hooks.
1 parent 61a5a03 commit 65299ca

File tree

7 files changed

+96
-18
lines changed

7 files changed

+96
-18
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
@@ -661,6 +661,7 @@ def link(
661661
extra_postargs=None,
662662
build_temp=None,
663663
target_lang=None,
664+
extra_midargs=None,
664665
):
665666
"""Link a bunch of stuff together to create an executable or
666667
shared library file.
@@ -724,6 +725,7 @@ def link_shared_lib(
724725
build_temp=None,
725726
target_lang=None,
726727
):
728+
raise NotImplementedError('Building shared libs is disabled for Nuitka-Python')
727729
self.link(
728730
CCompiler.SHARED_LIBRARY,
729731
objects,
@@ -755,6 +757,7 @@ def link_shared_object(
755757
build_temp=None,
756758
target_lang=None,
757759
):
760+
raise NotImplementedError('Building shared libs is disabled for Nuitka-Python')
758761
self.link(
759762
CCompiler.SHARED_OBJECT,
760763
objects,
@@ -783,6 +786,7 @@ def link_executable(
783786
extra_preargs=None,
784787
extra_postargs=None,
785788
target_lang=None,
789+
extra_midargs=None,
786790
):
787791
self.link(
788792
CCompiler.EXECUTABLE,
@@ -798,6 +802,7 @@ def link_executable(
798802
extra_postargs,
799803
None,
800804
target_lang,
805+
extra_midargs,
801806
)
802807

803808
# -- Miscellaneous methods -----------------------------------------
@@ -1017,6 +1022,9 @@ def library_filename(
10171022
fmt = getattr(self, lib_type + "_lib_format")
10181023
ext = getattr(self, lib_type + "_lib_extension")
10191024

1025+
if libname.endswith(ext):
1026+
ext = ""
1027+
10201028
dir, base = os.path.split(libname)
10211029
filename = fmt % (base, ext)
10221030
if strip_dir:
@@ -1252,5 +1260,9 @@ def gen_lib_options(compiler, library_dirs, runtime_library_dirs, libraries):
12521260
f"no library file corresponding to '{lib}' found (skipping)"
12531261
)
12541262
else:
1255-
lib_opts.append(compiler.library_option(lib))
1263+
lib_file = compiler.find_library_file(library_dirs, lib_name)
1264+
if lib_file is not None and lib_file.endswith('.a'):
1265+
lib_opts.append(lib_file)
1266+
else:
1267+
lib_opts.append(compiler.library_option(lib))
12561268
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

@@ -578,18 +580,33 @@ def build_extension(self, ext):
578580
# Detect target language, if not provided
579581
language = ext.language or self.compiler.detect_language(sources)
580582

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

594611
def swig_sources(self, sources, extension):
595612
"""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
@@ -256,6 +256,8 @@ def _generic_abi() -> list[str]:
256256
# - graalpy: '.graalpy-38-native-x86_64-darwin.dylib'
257257
# => graalpy_38_native
258258

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

setuptools/command/build_ext.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import itertools
44
import os
55
import sys
6+
import itertools
7+
import json
8+
import shutil
69
from importlib.machinery import EXTENSION_SUFFIXES
710
from importlib.util import cache_from_source as _compiled_file_name
811
from pathlib import Path
@@ -398,7 +401,7 @@ def _compile_and_remove_stub(self, stub_file: str):
398401
os.unlink(stub_file)
399402

400403

401-
if use_stubs or os.name == 'nt':
404+
if False: # use_stubs or os.name == 'nt':
402405
# Build shared libraries
403406
#
404407
def link_shared_object(
@@ -466,3 +469,24 @@ def link_shared_object(
466469
basename = basename[3:]
467470

468471
self.create_static_lib(objects, basename, output_dir, debug, target_lang)
472+
473+
result_path = self.library_filename(basename, output_dir=os.path.abspath("."))
474+
475+
with open(result_path + '.link.json', 'w') as f:
476+
json.dump({
477+
'libraries': libraries,
478+
'library_dirs': ext.library_dirs,
479+
'runtime_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)