Skip to content

Commit

Permalink
Update distutils monkey patch to remove lib dirs in the same way as i…
Browse files Browse the repository at this point in the history
…nclude dirs, and limit both of them to known safe locations
  • Loading branch information
mhsmith committed Jul 23, 2023
1 parent 65a5fad commit 6034adf
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions server/pypi/env/lib/python/sitecustomize.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,79 @@
# PYTHONPATH when launching a PEP 517 build, which will prevent this file from taking
# effect in the subprocess. So we may need to use a different approach, perhaps even using
# a different PEP 517 front end like pypa/build.
#
# Since we're now using a setuptools version later than 60, all references to distutils
# will be redirected to setuptools._distutils.

import os
from os.path import abspath, commonpath
import sys

from setuptools._distutils import ccompiler, dist, sysconfig, util


# --no-clean currently has no effect when running `pip wheel`
# (https://github.com/pypa/pip/issues/5661), so disable the clean command to prevent it
# destroying the evidence after a build failure. Monkey-patching at this level also handles
# packages overriding the `clean` command using `cmdclass.`
from distutils.dist import Distribution
run_command_original = Distribution.run_command
run_command_original = dist.Distribution.run_command

def run_command_override(self, command):
if command == "clean":
print("Chaquopy: clean command disabled")
else:
run_command_original(self, command)

Distribution.run_command = run_command_override
dist.Distribution.run_command = run_command_override


# Remove include and library directories which are not in known safe locations.
# Monkey-patching at this level handles both default paths added by distutils itself,
# and paths added explicitly by package build scripts.
src_dir = os.environ["SRC_DIR"]
valid_dirs = [abspath(path) for path in [src_dir, f"{src_dir}/../requirements"]]

def filter_dirs(dir_type, dirs):
result = []
for dir in dirs:
if any(commonpath([vd, abspath(dir)]) == vd for vd in valid_dirs):
result.append(dir)
else:
print(f"Chaquopy: ignored invalid {dir_type} directory: {dir!r}")
return result

# Remove include paths for the build Python, including any virtualenv. Monkey-patching at this
# level handles both default paths added by distutils itself, and paths added explicitly by
# setup.py scripts.
import distutils.ccompiler
import distutils.sysconfig

gen_preprocess_options_original = distutils.ccompiler.gen_preprocess_options
gen_preprocess_options_original = ccompiler.gen_preprocess_options

def gen_preprocess_options_override(macros, include_dirs):
include_dirs = [
item for item in include_dirs
if item not in [distutils.sysconfig.get_python_inc(),
distutils.sysconfig.get_python_inc(plat_specific=True),
os.path.join(sys.exec_prefix, 'include')]]
return gen_preprocess_options_original(macros, include_dirs)
return gen_preprocess_options_original(macros, filter_dirs("include", include_dirs))

ccompiler.gen_preprocess_options = gen_preprocess_options_override


gen_lib_options_original = ccompiler.gen_lib_options

def gen_lib_options_override(compiler, library_dirs, runtime_library_dirs, libraries):
return gen_lib_options_original(
compiler, filter_dirs("library", library_dirs), runtime_library_dirs, libraries)

distutils.ccompiler.gen_preprocess_options = gen_preprocess_options_override
ccompiler.gen_lib_options = gen_lib_options_override


# Override the CFLAGS from the build Python sysconfigdata file.
# TODO: look into using crossenv to extract this from the Android sysconfigdata.
distutils.sysconfig.get_config_vars() # Ensure _config_vars has been initialized.
distutils.sysconfig._config_vars["CFLAGS"] = \
sysconfig.get_config_vars() # Ensure _config_vars has been initialized.
sysconfig._config_vars["CFLAGS"] = \
"-Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall"


# Fix distutils ignoring LDFLAGS when building executables.
from distutils.util import split_quoted
customize_compiler_original = distutils.sysconfig.customize_compiler
customize_compiler_original = sysconfig.customize_compiler

def customize_compiler_override(compiler):
customize_compiler_original(compiler)
ldflags = os.environ["LDFLAGS"]
if ldflags not in " ".join(compiler.linker_exe):
compiler.linker_exe += split_quoted(ldflags)
compiler.linker_exe += util.split_quoted(ldflags)

distutils.sysconfig.customize_compiler = customize_compiler_override
sysconfig.customize_compiler = customize_compiler_override


# Call the next sitecustomize script if there is one
Expand Down

0 comments on commit 6034adf

Please sign in to comment.