Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use meson in sage-the-distro #39030

Draft
wants to merge 41 commits into
base: develop
Choose a base branch
from

Conversation

tobiasdiez
Copy link
Contributor

@tobiasdiez tobiasdiez commented Nov 25, 2024

Replace the old setuptools-based build by the new meson-based one in sage-the-distro. Delete most of the old stuff that is no longer needed now.

Definitely shouldn't be merged in this release cycle. I'm happy to discuss the timeline and prerequisites of fully migrating from setuptools to meson.

📝 Checklist

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation and checked the documentation preview.

⌛ Dependencies

@tobiasdiez tobiasdiez changed the title Distro-meson Use meson in sage-the-distro Nov 25, 2024
@tobiasdiez
Copy link
Contributor Author

@dimpase @orlitzky Here is a first version of replacing the old setuptools-based build with meson in sage-the-distro. I'm not familiar enough with sage-the-distro to effectively finish this PR. Do you have the resources to take over from here?

@dimpase
Copy link
Member

dimpase commented Nov 30, 2024

Sure, I'm ready to help as much as I can. Could you share an outline of how this is meant to work?

I imagine it is an adaptation to the Sage's venv (which is a more or less standard venv, no?) of what one can get e.g. with a standard venv on Gentoo Linux, or another environment where all the dependencies are available. Would this approach also work for a Conda-based environment?

@tobiasdiez
Copy link
Contributor Author

As a first step, I would not change how sage-the-distro works. So leave all the dependency installation (python and non-python deps) untouched for now. (We can discuss later how to transfer the configure checks to meson, and perhaps even completely replace the install scripts by mesons wrap files)

So, basically the only required change would be pip install ./src to pip install . , i.e. instead of using the pyproject.toml in src, the meson-based one in the root should be used to install sagelib. I tried to that in build/pkgs/sagelib/spkg-install.in in this branch, but not sure if the changes are correct. Then see what errors popup and resolve those.

@dimpase
Copy link
Member

dimpase commented Dec 1, 2024

running ./bootstrap gives

sed: can't read ../sagelib/package-version.txt: No such file or directory

@dimpase
Copy link
Member

dimpase commented Dec 1, 2024

that's cause

$ git grep "sagelib/package-version.txt"
pkgs/sagemath-bliss/requirements.txt.m4:sagemath-standard==esyscmd(`printf $(sed "s/[.]p.*//;" ../sagelib/package-version.txt)')
pkgs/sagemath-coxeter3/requirements.txt.m4:sagemath-standard==esyscmd(`printf $(sed "s/[.]p.*//;" ../sagelib/package-version.txt)')
pkgs/sagemath-mcqd/requirements.txt.m4:sagemath-standard==esyscmd(`printf $(sed "s/[.]p.*//;" ../sagelib/package-version.txt)')
pkgs/sagemath-meataxe/requirements.txt.m4:sagemath-standard==esyscmd(`printf $(sed "s/[.]p.*//;" ../sagelib/package-version.txt)')
pkgs/sagemath-sirocco/requirements.txt.m4:sagemath-standard==esyscmd(`printf $(sed "s/[.]p.*//;" ../sagelib/package-version.txt)')
pkgs/sagemath-tdlib/requirements.txt.m4:sagemath-standard==esyscmd(`printf $(sed "s/[.]p.*//;" ../sagelib/package-version.txt)')

@tobiasdiez
Copy link
Contributor Author

I think these requirements files can be safely deleted. At least I'm not aware of any usage of them.

@dimpase
Copy link
Member

dimpase commented Dec 1, 2024

I think these requirements files can be safely deleted. At least I'm not aware of any usage of them.

these are for building all these sagemath-* pseudo-packages.

By the way, can you resolve the git conflict?

@tobiasdiez
Copy link
Contributor Author

I think these requirements files can be safely deleted. At least I'm not aware of any usage of them.

these are for building all these sagemath-* pseudo-packages.

You are right, these are actually used.

Does ae33069 works for you? (I cannot test it atm on linux)

@tobiasdiez tobiasdiez marked this pull request as draft December 1, 2024 11:05
@dimpase
Copy link
Member

dimpase commented Dec 8, 2024

Sage venv can only be characterised as a totally overdesigned steampunk.

that's from our basement here, in fact. The thing on the right leaked, and took 5 days and $12K to fix.

@dimpase
Copy link
Member

dimpase commented Dec 8, 2024

The copying comes from the lines around SAGE_PYTHON_DISTUTILS_C_CONFTEST. For some reason the temporary conftest is a placed in a directory where src is present as well (probably via a symlink). As the standard directory for source files, src is then picked up and its content is copied over.

CC=gcc CXX=g++ -std=gnu++11 conftest_venv/bin/python3 conftest.py --verbose build --build-base=conftest.dir
No `packages` or `py_modules` configuration, performing automatic discovery.
`src-layout` detected -- analysing ./src

I tried to fix it (untested).

The cython error seems to come from a problem in

sage/src/sage/env.py

Lines 324 to 327 in 56f95c9

import sage
dirs = [os.path.dirname(directory)
for directory in sage.__path__]
try:

I would assume if you fix the failing test for this function, then the other cython tests should pass as well. I'm not totally sure but my guess would be adding the directory returned by importlib.util.find_spec("sage") to dirs there should work.

Doesn't seem to be any different from sage.__path__:

sage: importlib.util.find_spec("sage")
ModuleSpec(name='sage', loader=<_frozen_importlib_external.NamespaceLoader object at 0x7f5ddc461220>, submodule_search_locations=['/mnt/opt/Sage/sage-dev/local/var/lib/sage/venv-python3.12/lib/python3.12/site-packages/_sagemath_editable_loader.py/sage'])
sage: 
sage: sage.__path__
['/mnt/opt/Sage/sage-dev/local/var/lib/sage/venv-python3.12/lib/python3.12/site-packages/_sagemath_editable_loader.py/sage']

I'm attaching the new config.log, doesn't seem to make a difference.
config.log

@dimpase
Copy link
Member

dimpase commented Dec 10, 2024

looks like my rebase didn't fly. I'll do a merge and post update

@dimpase
Copy link
Member

dimpase commented Dec 10, 2024

This test fails - but actually I don't know its purpose:

File "src/sage/misc/cython.py", line 232, in sage.misc.cython.?
Failed example:
    cython('''
    from sage.misc cimport cachefunc
    ''')
Expected:
    Traceback (most recent call last):
    ...
    RuntimeError: Error compiling Cython file:
    ...
    ...: 'sage/misc.pxd' not found
Got:
    <BLANKLINE>

with the current fix - adding SAGE_SRC to the list of directories to search for Cython headers (see the change in src/sage/misc/cython.py) - it just works.

SAGE_SRC isn't quite right in some cases, e.g. for a setup where Sage is installed from a package manager.
Can @orlitzky @antonio-rojas @tornaria comment?

In particular, 5ee730d was added by @tornaria - for the purpose I don't understand

@dimpase
Copy link
Member

dimpase commented Dec 10, 2024

targets sagelib-clean and fast-rebuild-clean in the top-level Makefile refer to build/pkgs/sagelib/src/ and should be fixed or removed.

@tobiasdiez
Copy link
Contributor Author

tobiasdiez commented Dec 11, 2024

The difference comes from the fact that cython doesn't fully work with PEP 420 implicit namespace packages (essentially the test is recording this fact). However, when we use meson to build sagelib, then __init__ files are created during build so that sage.misc is no longer an implicit namespace package. Hence the compilation in this example actually works.

Can we guard the test be the absence of sage/misc/__init__.py?

@tornaria
Copy link
Contributor

Some quick comments:

  • I think the test is reasonably explained there; since sage.misc is a PEP 420 "namespace package" (i.e. no __init__.py) using from <PACKAGE> cimport <MODULE> does not work (shortcoming of cython, it's not fixed in cython 3).
  • Please do not add random directories to search paths. Sometimes it might seem to fix something, but in the end it often ends up biting back and causing pain.
  • For similar reasons, I think sage.misc should either be a namespace package, or else be a regular namespace. Making different choices on different build systems seems a bad idea to me, since there are subtle differences in behavior (as we can see here).

@dimpase
Copy link
Member

dimpase commented Dec 11, 2024

meson is meant to become the only system to build sagelib, as far as I am concerned. Transition is not straightforward, though.

@dimpase
Copy link
Member

dimpase commented Dec 11, 2024

@tornaria - my question was: how do you set paths to sagelib Cython header files on Void, say. I imagine you keep a full source directory somewhere (to provide the Cython functionality and the docs), but where? How can it be customised?

I am talking about cythonising at Sage prompt, not the package building

@dimpase
Copy link
Member

dimpase commented Dec 11, 2024

Can we guard the test be the absence of sage/misc/__init__.py?

I don't think this is easily doable in our current doctest framework. Unless, perhaps, pytest gets used.

@orlitzky
Copy link
Contributor

orlitzky commented Dec 13, 2024

Why keep this test at all?

"""
    In Cython 0.29.33 using `from PACKAGE cimport MODULE` is broken            
    when `PACKAGE` is a namespace package, see :issue:`35322`::                
                                                                               
        sage: cython('''                                                       
        ....: from sage.misc cimport cachefunc                                 
        ....: ''')                                                             
        Traceback (most recent call last):                                     
        ...                                                                    
        RuntimeError: Error compiling Cython file:                             
        ...                                                                    
        ...: 'sage/misc.pxd' not found
"""

If the test fails, then it's no longer broken: great! Meson solved the problem.

@orlitzky
Copy link
Contributor

with the current fix - adding SAGE_SRC to the list of directories to search for Cython headers (see the change in src/sage/misc/cython.py) - it just works.

SAGE_SRC isn't quite right in some cases, e.g. for a setup where Sage is installed from a package manager. Can @orlitzky @antonio-rojas @tornaria comment?

I'm far from an expert on this. For the pxd files, things usually just work if they're installed next to the pyx files? For the *.h files, the last time I needed to do this, I copied Tobias's solution of importing the corresponding module and using __file__.

@dimpase
Copy link
Member

dimpase commented Dec 13, 2024

with the current fix - adding SAGE_SRC to the list of directories to search for Cython headers (see the change in src/sage/misc/cython.py) - it just works.
SAGE_SRC isn't quite right in some cases, e.g. for a setup where Sage is installed from a package manager. Can @orlitzky @antonio-rojas @tornaria comment?

I'm far from an expert on this. For the pxd files, things usually just work if they're installed next to the pyx files? For the *.h files, the last time I needed to do this, I copied Tobias's solution of importing the corresponding module and using __file__.

Sage's .pxd files (aka Cython headers) are akin to the usual .h etc headers, in the situation where Sage is installed system-wide. If a user writes Cython code using Sage's system-wide headers, asking them to copy these over looks a bit unclean to me. So there ought to be a solution to this, and in such a case SAGE_SRC might not be appropriate to use.

@dimpase
Copy link
Member

dimpase commented Dec 13, 2024

If the test fails, then it's no longer broken: great! Meson solved the problem.

OK, "plonk" it goes then. e1167ee

@orlitzky
Copy link
Contributor

Sage's .pxd files (aka Cython headers) are akin to the usual .h etc headers, in the situation where Sage is installed system-wide. If a user writes Cython code using Sage's system-wide headers, asking them to copy these over looks a bit unclean to me. So there ought to be a solution to this, and in such a case SAGE_SRC might not be appropriate to use.

I think that there are two related problems here. The first is the pxd files, which are needed to cimport stuff from other cython programs, and the problem is that cython will look for the pxd file in the wrong place unless there is an __init__.py. For example,

sage: cython('''from sage.misc.cachefunc cimport cachefunc''')
...
_tmp_tmp7m7cufjo_tmp_cl1obc0v_pyx_0.pyx:1:0: 'sage/misc/cachefunc/cachefunc.pxd' not found

should be looking for sage/misc/cachefunc.pxd instead. If I touch sage/misc/__init__.py, this error goes away. So I think we are waiting for cython to fix this. In the meantime, should we install __init__.py? (Is anyone using the modularized distributions?)

The other issue is the *.h files. If you write an external cython module that builds against sage, you will find that the cythonized (i.e. C) code expects to find headers like sage/cpython/cython_metaclass.h, which won't be installed in the C compiler's include path. I don't really know how this is supposed to work, but Tobias's solution seems good enough if your external module has a build system. I do not know if it is possible to trick e.g. cythonize() into needing these headers.

@dimpase
Copy link
Member

dimpase commented Dec 13, 2024

Is anyone using the modularized distributions?

in my book they are basically abandonware.

@orlitzky
Copy link
Contributor

Is anyone using the modularized distributions?

in my book they are basically abandonware.

Well, meson generates the necessary __init__.py already. All we need to do is install them, probably by adding them to py.install_sources(...).

@tobiasdiez tobiasdiez mentioned this pull request Dec 16, 2024
5 tasks
@tobiasdiez
Copy link
Contributor Author

Is anyone using the modularized distributions?

in my book they are basically abandonware.

Well, meson generates the necessary __init__.py already. All we need to do is install them, probably by adding them to py.install_sources(...).

This is now #39139

@dimpase
Copy link
Member

dimpase commented Dec 16, 2024

This needs more work on macOS, where different C++ interfaces need different options, e.g. linbox needs std=gnu++11
(due to std::binary_function used) and similarly with giac (using pointer_to_binary_function, also removed in c++17)

This probably explains why macOS CI fails here.

vbraun pushed a commit to vbraun/sage that referenced this pull request Dec 19, 2024
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

As proposed in
sagemath#39030 (comment).
Namespace support is still not there for all tools (cython, pytest,
...), so it's a good idea to still distribute these `__init__.py` files
for now until the ecosystem catches up.

Also fixes failures of the meson workflow due to a missing python file.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [ ] The title is concise and informative.
- [ ] The description explains in detail what this PR is about.
- [ ] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->

- sagemath#39140
    
URL: sagemath#39139
Reported by: Tobias Diez
Reviewer(s): Dima Pasechnik
vbraun pushed a commit to vbraun/sage that referenced this pull request Dec 21, 2024
    
<!-- ^ Please provide a concise and informative title. -->
<!-- ^ Don't put issue numbers in the title, do this in the PR
description below. -->
<!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method
to calculate 1 + 2". -->
<!-- v Describe your changes below in detail. -->
<!-- v Why is this change required? What problem does it solve? -->
<!-- v If this PR resolves an open issue, please link to it here. For
example, "Fixes sagemath#12345". -->

As proposed in
sagemath#39030 (comment).
Namespace support is still not there for all tools (cython, pytest,
...), so it's a good idea to still distribute these `__init__.py` files
for now until the ecosystem catches up.

Also fixes failures of the meson workflow due to a missing python file.

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [ ] The title is concise and informative.
- [ ] The description explains in detail what this PR is about.
- [ ] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->

- sagemath#39140
    
URL: sagemath#39139
Reported by: Tobias Diez
Reviewer(s): Dima Pasechnik
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants