Skip to content

Commit cba306e

Browse files
cosminbascatimfel
authored andcommitted
ginstall: add scipy-1.9.3.patch
- scipy 1.9.x - setup env to build with flang and meson - scipy-1.9.3.patch - cythonize in process
1 parent c870075 commit cba306e

File tree

2 files changed

+200
-20
lines changed

2 files changed

+200
-20
lines changed

graalpython/lib-graalpython/modules/ginstall.py

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ def get_module_name(package_name):
8484
return module_name.replace('-', '_')
8585

8686

87+
def have_flang_new():
88+
env_path = os.environ.get('PATH', '').split(os.pathsep)
89+
for p in env_path:
90+
flang_new = os.path.join(p, 'flang-new')
91+
if os.path.isfile(flang_new):
92+
return True
93+
return False
94+
95+
96+
def get_flang_new_lib_dir():
97+
try:
98+
output = subprocess.check_output(['flang-new', '--version'])
99+
except (OSError, subprocess.CalledProcessError):
100+
pass
101+
else:
102+
flang_dir = output.splitlines()[-1].split()[-1].strip().decode("utf-8")
103+
return os.path.normpath(os.path.join(flang_dir, '..', 'lib'))
104+
return None
105+
106+
87107
def get_path_env_var(var):
88108
env_var = os.environ.get(var, None)
89109
if isinstance(env_var, str) and env_var.lower() == 'none':
@@ -406,10 +426,13 @@ def make_site_cfg(root):
406426
append_env_var(numpy_build_env, 'CFLAGS', '-Wno-error=implicit-function-declaration')
407427
info(f"have lapack or blas ... CLFAGS={numpy_build_env['CFLAGS']}")
408428

409-
numpy_build_env["FC"] = "flang-new"
410-
install_from_pypi("numpy==1.23.5", build_cmd=["build_ext", "--disable-optimization",
411-
"config", "--fcompiler=flang-new"], env=numpy_build_env,
412-
pre_install_hook=make_site_cfg, **kwargs)
429+
with_flang = have_flang_new()
430+
build_cmd = ["build_ext", "--disable-optimization"]
431+
if with_flang:
432+
numpy_build_env["FC"] = "flang-new"
433+
build_cmd += ["config", "--fcompiler=flang-new"]
434+
install_from_pypi("numpy==1.23.5", build_cmd=build_cmd, env=numpy_build_env, pre_install_hook=make_site_cfg,
435+
**kwargs)
413436

414437
# print numpy configuration
415438
if sys.implementation.name != 'graalpy' or __graalpython__.platform_id != 'managed':
@@ -491,7 +514,6 @@ def scipy(**kwargs):
491514
xit("SciPy can only be installed within a venv.")
492515
from distutils.sysconfig import get_config_var
493516
scipy_build_env["LDFLAGS"] = get_config_var("LDFLAGS")
494-
scipy_build_env["FC"] = "flang-new"
495517

496518
if have_lapack() or have_openblas():
497519
append_env_var(scipy_build_env, 'CFLAGS', '-Wno-error=implicit-function-declaration')
@@ -502,8 +524,34 @@ def scipy(**kwargs):
502524
pybind11(**kwargs)
503525
pythran(**kwargs)
504526

505-
install_from_pypi("scipy==1.8.1", build_cmd=["config", "--fcompiler=flang-new"],
506-
env=scipy_build_env, **kwargs)
527+
scipy_version = "1.8.1"
528+
# scipy_version = "1.9.3"
529+
with_meson = False
530+
if scipy_version >= "1.9.1":
531+
meson(**kwargs)
532+
ninja(**kwargs)
533+
with_flang = have_flang_new()
534+
with_meson = True
535+
if with_flang:
536+
# until flang-new can compile propack we disable it (during runtime)
537+
scipy_build_env["USE_PROPACK"] = "0"
538+
scipy_build_env['CC'] = 'clang'
539+
scipy_build_env['CXX'] = 'clang++'
540+
scipy_build_env['FC'] = 'flang-new'
541+
if sys.implementation.name == "graalpy":
542+
cflags = "-flto=full"
543+
else:
544+
ld = 'lld'
545+
cflags = "-flto=full -fuse-ld=lld -Wl,--mllvm=-lto-embed-bitcode=optimized,--lto-O0"
546+
scipy_build_env['CC_LD'] = ld
547+
scipy_build_env['CXX_LD'] = ld
548+
scipy_build_env['FC_LD'] = ld
549+
scipy_build_env['CFLAGS'] = cflags
550+
scipy_build_env['CXXFLAGS'] = cflags
551+
scipy_build_env['FFLAGS'] = cflags
552+
scipy_build_env['FFLAGS'] = cflags
553+
append_env_var(scipy_build_env, 'LDFLAGS', f'-L{get_flang_new_lib_dir()}')
554+
install_from_pypi(f"scipy=={scipy_version}", env=scipy_build_env, with_meson=with_meson, **kwargs)
507555

508556
@pip_package()
509557
def scikit_learn(**kwargs):
@@ -631,7 +679,7 @@ def _download_with_curl_and_extract(dest_dir, url, quiet=False):
631679

632680

633681
def _install_from_url(url, package, extra_opts=None, add_cflags="", ignore_errors=False, env=None, version=None,
634-
pre_install_hook=None, build_cmd=None, debug_build=False):
682+
pre_install_hook=None, build_cmd=None, debug_build=False, with_meson=False):
635683
if build_cmd is None:
636684
build_cmd = []
637685
if env is None:
@@ -683,24 +731,36 @@ def _install_from_url(url, package, extra_opts=None, add_cflags="", ignore_error
683731
os.path.join(tempdir, bare_name, subdir)
684732
run_cmd(["patch", "-d", os.path.join(tempdir, bare_name, subdir), "-p1", "-i", patch_file_path], quiet=quiet)
685733

734+
tmp_cwd = os.path.join(tempdir, bare_name)
686735
if pre_install_hook:
687-
pre_install_hook(os.path.join(tempdir, bare_name))
736+
pre_install_hook(tmp_cwd)
688737

689738
if "--user" not in extra_opts and "--prefix" not in extra_opts and site.ENABLE_USER_SITE:
690739
user_arg = ["--user"]
691740
else:
692741
user_arg = []
742+
693743
start = time.time()
694-
cmd = [sys.executable]
695-
if debug_build and sys.implementation.name == 'graalpy':
696-
cmd += ["-debug-java", "--python.ExposeInternalSources", "--python.WithJavaStacktrace=2"]
697-
cmd += ["setup.py"] + build_cmd + ["install"] + user_arg + extra_opts
698-
status = run_cmd(cmd, env=setup_env,
699-
cwd=os.path.join(tempdir, bare_name), quiet=quiet)
744+
if with_meson:
745+
for cmd in [
746+
['meson', 'setup', f'--prefix={os.environ.get("VIRTUAL_ENV")}', 'build'],
747+
['meson', 'compile', '-C', 'build', '--ninja-args=-j4'], # limit concurrency to 4
748+
['meson', 'install', '-C', 'build'],
749+
]:
750+
status = run_cmd(cmd, env=setup_env, cwd=tmp_cwd, quiet=quiet)
751+
if status != 0 and not ignore_errors:
752+
xit(f"An error occurred trying to run `{' '.join(cmd)}'")
753+
else:
754+
cmd = [sys.executable]
755+
if debug_build and sys.implementation.name == 'graalpy':
756+
cmd += ["-debug-java", "--python.ExposeInternalSources", "--python.WithJavaStacktrace=2"]
757+
cmd += ["setup.py"] + build_cmd + ["install"] + user_arg + extra_opts
758+
status = run_cmd(cmd, env=setup_env, cwd=tmp_cwd, quiet=quiet)
759+
if status != 0 and not ignore_errors:
760+
xit("An error occurred trying to run `setup.py install {!s} {}'", user_arg, " ".join(extra_opts))
761+
700762
end = time.time()
701-
if status != 0 and not ignore_errors:
702-
xit("An error occurred trying to run `setup.py install {!s} {}'", user_arg, " ".join(extra_opts))
703-
elif quiet:
763+
if quiet:
704764
info("{} successfully installed (took {:.2f} s)", package, (end - start))
705765

706766

@@ -754,7 +814,7 @@ def package_from_path(pth):
754814

755815

756816
def install_from_pypi(package, extra_opts=None, add_cflags="", ignore_errors=True, env=None, pre_install_hook=None,
757-
build_cmd=None, debug_build=False):
817+
build_cmd=None, debug_build=False, with_meson=False):
758818
if build_cmd is None:
759819
build_cmd = []
760820
if extra_opts is None:
@@ -810,7 +870,7 @@ def set_if_exists(env_var, conf_var):
810870
if url:
811871
_install_from_url(url, package=package, extra_opts=extra_opts, add_cflags=add_cflags,
812872
ignore_errors=ignore_errors, env=env, version=version, pre_install_hook=pre_install_hook,
813-
build_cmd=build_cmd, debug_build=debug_build)
873+
build_cmd=build_cmd, debug_build=debug_build, with_meson=with_meson)
814874
else:
815875
xit("Package not found: '{!s}'", package)
816876

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
diff --git a/meson.build b/meson.build
2+
index 5b4a1d52..c29ec297 100644
3+
--- a/meson.build
4+
+++ b/meson.build
5+
@@ -11,7 +11,7 @@ project(
6+
'buildtype=debugoptimized',
7+
'c_std=c99',
8+
'cpp_std=c++14',
9+
- 'fortran_std=legacy',
10+
+ 'fortran_std=none',
11+
'blas=openblas',
12+
'lapack=openblas'
13+
],
14+
diff --git a/scipy/sparse/linalg/meson.build b/scipy/sparse/linalg/meson.build
15+
index db2f199d..763e1ef6 100644
16+
--- a/scipy/sparse/linalg/meson.build
17+
+++ b/scipy/sparse/linalg/meson.build
18+
@@ -16,7 +16,7 @@ py3.install_sources([
19+
subdir: 'scipy/sparse/linalg'
20+
)
21+
22+
-subdir('_propack')
23+
+# subdir('_propack')
24+
subdir('_isolve')
25+
subdir('_dsolve')
26+
subdir('_eigen')
27+
diff --git a/scipy/special/meson.build b/scipy/special/meson.build
28+
index 8fe534be..133be12b 100644
29+
--- a/scipy/special/meson.build
30+
+++ b/scipy/special/meson.build
31+
@@ -459,24 +459,24 @@ py3.extension_module('_test_round',
32+
# Must use `custom_target`, because `py3.install_sources` does not work with
33+
# generated sources - see https://github.com/mesonbuild/meson/issues/7372
34+
npz_files = [
35+
- [
36+
- '_data_boost',
37+
- 'tests/data/boost/acosh_data_ipp/acosh_data.txt',
38+
- 'boost',
39+
- 'boost.npz'
40+
- ],
41+
- [
42+
- '_data_gsl',
43+
- 'tests/data/gsl/mathieu_ab.txt',
44+
- 'gsl',
45+
- 'gsl.npz'
46+
- ],
47+
- [
48+
- '_data_local',
49+
- 'tests/data/local/ellipkm1.txt',
50+
- 'local',
51+
- 'local.npz'
52+
- ],
53+
+ # [
54+
+ # '_data_boost',
55+
+ # 'tests/data/boost/acosh_data_ipp/acosh_data.txt',
56+
+ # 'boost',
57+
+ # 'boost.npz'
58+
+ # ],
59+
+ # [
60+
+ # '_data_gsl',
61+
+ # 'tests/data/gsl/mathieu_ab.txt',
62+
+ # 'gsl',
63+
+ # 'gsl.npz'
64+
+ # ],
65+
+ # [
66+
+ # '_data_local',
67+
+ # 'tests/data/local/ellipkm1.txt',
68+
+ # 'local',
69+
+ # 'local.npz'
70+
+ # ],
71+
]
72+
73+
foreach npz_file: npz_files
74+
diff --git a/tools/cythonize.py b/tools/cythonize.py
75+
index 402824cb..b6aba959 100755
76+
--- a/tools/cythonize.py
77+
+++ b/tools/cythonize.py
78+
@@ -95,21 +95,19 @@ def process_pyx(fromfile, tofile, cwd):
79+
if tofile.endswith('.cxx'):
80+
flags += ['--cplus']
81+
82+
- try:
83+
+ try:
84+
+ from Cython.Compiler.Main import setuptools_main
85+
+ prev_argv = sys.argv[1:]
86+
try:
87+
- r = subprocess.call(['cython'] + flags + ["-o", tofile, fromfile], cwd=cwd)
88+
- if r != 0:
89+
- raise Exception('Cython failed')
90+
- except OSError as e:
91+
- # There are ways of installing Cython that don't result in a cython
92+
- # executable on the path, see gh-2397.
93+
- r = subprocess.call([sys.executable, '-c',
94+
- 'import sys; from Cython.Compiler.Main import '
95+
- 'setuptools_main as main; sys.exit(main())'] + flags +
96+
- ["-o", tofile, fromfile],
97+
- cwd=cwd)
98+
- if r != 0:
99+
- raise Exception("Cython either isn't installed or it failed.") from e
100+
+ oldcwd = os.getcwd()
101+
+ os.chdir(cwd)
102+
+ sys.argv[1:] = flags + ["-o", tofile, fromfile]
103+
+ retcode = setuptools_main()
104+
+ if retcode:
105+
+ sys.exit(retcode)
106+
+ finally:
107+
+ sys.argv[1:] = prev_argv
108+
+ os.chdir(oldcwd)
109+
except OSError as e:
110+
raise OSError('Cython needs to be installed') from e
111+
112+
@@ -253,7 +251,7 @@ def find_process_files(root_dir):
113+
lock = Lock()
114+
115+
try:
116+
- num_proc = int(os.environ.get('SCIPY_NUM_CYTHONIZE_JOBS', cpu_count()))
117+
+ num_proc = int(os.environ.get('SCIPY_NUM_CYTHONIZE_JOBS', 1))
118+
pool = Pool(processes=num_proc)
119+
except ImportError as e:
120+
# Allow building (single-threaded) on GNU/Hurd, which does not

0 commit comments

Comments
 (0)