Skip to content

Commit 64aef54

Browse files
committed
Add Nuitka-Python hooks.
1 parent f91fa3d commit 64aef54

File tree

7 files changed

+97
-22
lines changed

7 files changed

+97
-22
lines changed

setuptools/_distutils/_msvccompiler.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,18 @@ def initialize(self, plat_name=None):
265265
self.rc = _find_exe("rc.exe", paths) # resource compiler
266266
self.mc = _find_exe("mc.exe", paths) # message compiler
267267
self.mt = _find_exe("mt.exe", paths) # message compiler
268+
self.dumpbin = _find_exe("dumpbin.exe", paths)
268269

269270
self.preprocess_options = None
270271
# bpo-38597: Always compile with dynamic linking
271272
# Future releases of Python 3.x will include all past
272273
# versions of vcruntime*.dll for compatibility.
273-
self.compile_options = ['/nologo', '/O2', '/W3', '/GL', '/DNDEBUG', '/MD']
274+
self.compile_options = ['/nologo', '/O2', '/W3', '/GL', '/DNDEBUG', '/MT']
274275

275276
self.compile_options_debug = [
276277
'/nologo',
277278
'/Od',
278-
'/MDd',
279+
'/MTd',
279280
'/Zi',
280281
'/W3',
281282
'/D_DEBUG',
@@ -457,6 +458,7 @@ def link(
457458
extra_postargs=None,
458459
build_temp=None,
459460
target_lang=None,
461+
extra_midargs=None,
460462
):
461463
if not self.initialized:
462464
self.initialize()
@@ -498,6 +500,8 @@ def link(
498500

499501
if extra_preargs:
500502
ld_args[:0] = extra_preargs
503+
if extra_midargs:
504+
ld_args.extend(extra_midargs)
501505
if extra_postargs:
502506
ld_args.extend(extra_postargs)
503507

@@ -552,6 +556,8 @@ def library_option(self, lib):
552556
return self.library_filename(lib)
553557

554558
def find_library_file(self, dirs, lib, debug=0):
559+
if len(dirs) == 1 and os.path.isfile(os.path.join(dirs[0], lib)):
560+
return os.path.join(dirs[0], lib)
555561
# Prefer a debugging library if found (and requested), but deal
556562
# with it if we don't have one.
557563
if debug:

setuptools/_distutils/ccompiler.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ def link(
655655
extra_postargs=None,
656656
build_temp=None,
657657
target_lang=None,
658+
extra_midargs=None,
658659
):
659660
"""Link a bunch of stuff together to create an executable or
660661
shared library file.
@@ -718,6 +719,7 @@ def link_shared_lib(
718719
build_temp=None,
719720
target_lang=None,
720721
):
722+
raise NotImplementedError('Building shared libs is disabled for Nuitka-Python')
721723
self.link(
722724
CCompiler.SHARED_LIBRARY,
723725
objects,
@@ -749,6 +751,7 @@ def link_shared_object(
749751
build_temp=None,
750752
target_lang=None,
751753
):
754+
raise NotImplementedError('Building shared libs is disabled for Nuitka-Python')
752755
self.link(
753756
CCompiler.SHARED_OBJECT,
754757
objects,
@@ -777,6 +780,7 @@ def link_executable(
777780
extra_preargs=None,
778781
extra_postargs=None,
779782
target_lang=None,
783+
extra_midargs=None,
780784
):
781785
self.link(
782786
CCompiler.EXECUTABLE,
@@ -792,6 +796,7 @@ def link_executable(
792796
extra_postargs,
793797
None,
794798
target_lang,
799+
extra_midargs,
795800
)
796801

797802
# -- Miscellaneous methods -----------------------------------------
@@ -1013,6 +1018,9 @@ def library_filename(
10131018
fmt = getattr(self, lib_type + "_lib_format")
10141019
ext = getattr(self, lib_type + "_lib_extension")
10151020

1021+
if libname.endswith(ext):
1022+
ext = ""
1023+
10161024
dir, base = os.path.split(libname)
10171025
filename = fmt % (base, ext)
10181026
if strip_dir:
@@ -1248,5 +1256,9 @@ def gen_lib_options(compiler, library_dirs, runtime_library_dirs, libraries):
12481256
"no library file corresponding to '%s' found (skipping)" % lib
12491257
)
12501258
else:
1251-
lib_opts.append(compiler.library_option(lib))
1259+
lib_file = compiler.find_library_file(library_dirs, lib_name)
1260+
if lib_file is not None and lib_file.endswith('.a'):
1261+
lib_opts.append(lib_file)
1262+
else:
1263+
lib_opts.append(compiler.library_option(lib))
12521264
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

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

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

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

setuptools/_distutils/msvccompiler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,23 +314,23 @@ def initialize(self):
314314

315315
self.preprocess_options = None
316316
if self.__arch == "Intel":
317-
self.compile_options = ['/nologo', '/O2', '/MD', '/W3', '/GX', '/DNDEBUG']
317+
self.compile_options = ['/nologo', '/O2', '/MT', '/W3', '/GX', '/DNDEBUG']
318318
self.compile_options_debug = [
319319
'/nologo',
320320
'/Od',
321-
'/MDd',
321+
'/MTd',
322322
'/W3',
323323
'/GX',
324324
'/Z7',
325325
'/D_DEBUG',
326326
]
327327
else:
328328
# Win64
329-
self.compile_options = ['/nologo', '/O2', '/MD', '/W3', '/GS-', '/DNDEBUG']
329+
self.compile_options = ['/nologo', '/O2', '/MT', '/W3', '/GS-', '/DNDEBUG']
330330
self.compile_options_debug = [
331331
'/nologo',
332332
'/Od',
333-
'/MDd',
333+
'/MTd',
334334
'/W3',
335335
'/GS-',
336336
'/Z7',

setuptools/_distutils/sysconfig.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ def customize_compiler(compiler): # noqa: C901
345345
else:
346346
archiver = ar + ' ' + ar_flags
347347

348+
cflags = ' '.join([x for x in cflags.split(' ') if
349+
not x.startswith('-I') or (
350+
'Nuitka-Python-Deps' not in x and 'dependencies' not in x)])
351+
348352
cc_cmd = cc + ' ' + cflags
349353
compiler.set_executables(
350354
preprocessor=cpp,

setuptools/_distutils/unixccompiler.py

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

131+
# Nuitka: Make sure to use the original settings
132+
executables["compiler"] = sysconfig.get_config_var("CC")
133+
executables["compiler_so"] = sysconfig.get_config_var("CC")
134+
executables["compiler_cxx"] = sysconfig.get_config_var("CXX")
135+
executables["linker_so"] = sysconfig.get_config_var("CC")
136+
executables["linker_exe"] = sysconfig.get_config_var("CC")
137+
131138
# Needed for the filename generation methods provided by the base
132139
# class, CCompiler. NB. whoever instantiates/uses a particular
133140
# UnixCCompiler instance should set 'shared_lib_ext' -- we set a
@@ -228,6 +235,7 @@ def link(
228235
extra_postargs=None,
229236
build_temp=None,
230237
target_lang=None,
238+
extra_midargs=None,
231239
):
232240
objects, output_dir = self._fix_object_args(objects, output_dir)
233241
fixed_args = self._fix_lib_args(libraries, library_dirs, runtime_library_dirs)
@@ -240,11 +248,14 @@ def link(
240248
output_filename = os.path.join(output_dir, output_filename)
241249

242250
if self._need_link(objects, output_filename):
243-
ld_args = objects + self.objects + lib_opts + ['-o', output_filename]
251+
ld_args = objects + self.objects
244252
if debug:
245253
ld_args[:0] = ['-g']
254+
if extra_midargs:
255+
ld_args += extra_midargs
246256
if extra_preargs:
247257
ld_args[:0] = extra_preargs
258+
ld_args += lib_opts + ['-o', output_filename]
248259
if extra_postargs:
249260
ld_args.extend(extra_postargs)
250261
self.mkpath(os.path.dirname(output_filename))
@@ -253,6 +264,8 @@ def link(
253264
# building an executable or linker_so (with shared options)
254265
# when building a shared library.
255266
building_exe = target_desc == CCompiler.EXECUTABLE
267+
if not building_exe:
268+
raise NotImplemented("No shared libs in Nuitka-Python")
256269
linker = (self.linker_exe if building_exe else self.linker_so)[:]
257270

258271
if target_lang == "c++" and self.compiler_cxx:
@@ -385,10 +398,10 @@ def find_library_file(self, dirs, lib, debug=0):
385398
>>> compiler.find_library_file(reversed(dirs), 'abc').replace('\\', '/')
386399
'/foo/bar/existing/libabc.a'
387400
"""
388-
lib_names = (
401+
lib_names = [lib] + [
389402
self.library_filename(lib, lib_type=type)
390403
for type in 'dylib xcode_stub shared static'.split()
391-
)
404+
]
392405

393406
roots = map(self._library_root, dirs)
394407

setuptools/command/build_ext.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
22
import sys
33
import itertools
4+
import json
5+
import shutil
46
from importlib.machinery import EXTENSION_SUFFIXES
57
from importlib.util import cache_from_source as _compiled_file_name
68
from typing import Dict, Iterator, List, Tuple
@@ -387,7 +389,7 @@ def _compile_and_remove_stub(self, stub_file: str):
387389
os.unlink(stub_file)
388390

389391

390-
if use_stubs or os.name == 'nt':
392+
if False: # use_stubs or os.name == 'nt':
391393
# Build shared libraries
392394
#
393395
def link_shared_object(
@@ -455,3 +457,24 @@ def link_shared_object(
455457
basename = basename[3:]
456458

457459
self.create_static_lib(objects, basename, output_dir, debug, target_lang)
460+
461+
result_path = self.library_filename(basename, output_dir=os.path.abspath("."))
462+
463+
with open(result_path + '.link.json', 'w') as f:
464+
json.dump({
465+
'libraries': libraries,
466+
'library_dirs': ext.library_dirs,
467+
'runtime_library_dirs': library_dirs,
468+
'extra_postargs': extra_postargs,
469+
'extra_preargs': extra_preargs}, f)
470+
471+
for lib in libraries:
472+
for dir in library_dirs:
473+
lib_install_dir = os.path.join(output_dir, dir)
474+
if os.path.isfile(os.path.join(dir, lib + '.lib')):
475+
if not os.path.isabs(dir):
476+
if not os.path.exists(lib_install_dir):
477+
os.makedirs(lib_install_dir)
478+
shutil.copyfile(os.path.join(dir, lib + '.lib'),
479+
os.path.join(lib_install_dir, lib + '.lib'))
480+
break

0 commit comments

Comments
 (0)