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

Structured Sources #9339

Merged
merged 15 commits into from
Mar 12, 2022
Merged

Conversation

dcbaker
Copy link
Member

@dcbaker dcbaker commented Oct 1, 2021

This is a reworked version of #8775, with the main complaints resolved.

The purpose of this series is to provide a mechanism for languages in which filesystem layout is relevant to ensure that generated sources end up in the right place. To do this, one describes the layout they want to a structrued_sources object, which the backend converts (if necessary) into a series of copy commands, to build the correct directory structure in the build directory. This structure is placed in the private directory of the target, to eliminate collisions. Then the actual compile is done on the copied sources rather than on the original sources. This is done at build time to avoid reconfiguration when any of the structured sources are changed.

@dcbaker
Copy link
Member Author

dcbaker commented Oct 1, 2021

@rgommers I'd like to have this work for cython too before it's merged, but I'm struggling to write a test case so that I can get started. Do you think you could at least help point me in the right direction?

@codecov
Copy link

codecov bot commented Oct 1, 2021

Codecov Report

Merging #9339 (3f3602e) into master (ade6e3a) will decrease coverage by 2.80%.
The diff coverage is n/a.

❗ Current head 3f3602e differs from pull request most recent head ff4c283. Consider uploading reports for the commit ff4c283 to get more accurate results

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #9339      +/-   ##
==========================================
- Coverage   64.66%   61.85%   -2.81%     
==========================================
  Files         402      202     -200     
  Lines       85729    42858   -42871     
  Branches    18909     9374    -9535     
==========================================
- Hits        55433    26509   -28924     
+ Misses      25934    14229   -11705     
+ Partials     4362     2120    -2242     
Impacted Files Coverage Δ
scripts/msgfmthelper.py 83.33% <0.00%> (-16.67%) ⬇️
modules/unstable_wayland.py 75.00% <0.00%> (-3.19%) ⬇️
modules/i18n.py 76.37% <0.00%> (-2.99%) ⬇️
scripts/gettext.py 37.50% <0.00%> (-2.80%) ⬇️
cmake/traceparser.py 55.46% <0.00%> (-1.60%) ⬇️
scripts/itstool.py 66.00% <0.00%> (-0.67%) ⬇️
build.py 75.21% <0.00%> (-0.33%) ⬇️
minstall.py 67.61% <0.00%> (-0.33%) ⬇️
mintro.py 90.25% <0.00%> (-0.19%) ⬇️
interpreter/dependencyfallbacks.py 94.71% <0.00%> (-0.11%) ⬇️
... and 212 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8434fb1...ff4c283. Read the comment docs.

@dcbaker dcbaker force-pushed the submit/structured_sources branch from 6ede98f to a5213cd Compare October 1, 2021 21:52
@mesonbuild mesonbuild deleted a comment from lgtm-com bot Oct 1, 2021
@rgommers
Copy link
Contributor

rgommers commented Oct 2, 2021

@rgommers I'd like to have this work for cython too before it's merged, but I'm struggling to write a test case so that I can get started. Do you think you could at least help point me in the right direction?

Yes of course. There are three file types that matter: __init__.py's and .pxi/.pxd files. Here is a relevant subset of the SciPy code with comments:

scipy/meson.build:

# Needed to trick Cython, it won't do a relative import outside a package.
# Every Cython extension built from a generated `.pyx` file needs to rely on this,
# as well as on the `__init__.py` files in subdirs between the root one we're copying
# here and the subdir it's in.
_cython_tree = custom_target('_cython_tree',
  output : [
    '__init__.py',
    'linalg.pxd',
    'special.pxd',
  ],
  input : [
    '__init__.py',
    'linalg.pxd',
    'special.pxd',
  ],
  command : ['cp', '@INPUT@', '@OUTDIR@'],
)

Codegen in scipy/special/meson.build:

cython_special = custom_target('cython_special',
  output : [...
            '_ufuncs.pyi',
            'cython_special.pyx',
            'cython_special.pxd'
           ],
  input : ['_generate_pyx.py', 'functions.json', 'add_newdocs.py'],
  command : [py3, '@INPUT0@', '-o', '@OUTDIR@'],
)

scipy/stats/meson.build (uses cython_special):

# Relies on cython_special, which ends up in the build dir. Hence we need ` '-I', '@BUILD_ROOT@'` here.
# (note: I need to double check if this `custom_target` is still needed, may have to do with the now
#        solved bug about `cython_special` not being indexable when passed to `extension_module`.
#        Either that, or to allow using `@BUILD_ROOT@` here.)
_stats_c = custom_target('_stats_c',
  output: '_stats.c',
  input: [
    '_stats.pyx',
    _cython_tree,
    cython_special,
  ],
  command: [cython, '-3', '--fast-fail',
            '@INPUT0@', '-o', '@OUTPUT@', '-I', '@BUILD_ROOT@'],
)

_stats = py3.extension_module('_stats',
  _stats_c,
  dependencies : [py3_dep],
  install : true,
  subdir : 'scipy/stats'
)

The __init__.py files should always be copied over I believe (without listing them in structured sources), and that logic is easy to implement. The .pxd/.pxi files need to be explicitly declared I'd think.

I believe the above covers most/all of what is relevant. Please let me know if it's not clear or if I should try and turn this into a self-contained test case.

EDIT: the bug in gh-8961 is relevant to this.

@dcbaker dcbaker modified the milestones: 0.60.0, 0.61.0 Oct 8, 2021
@dcbaker dcbaker force-pushed the submit/structured_sources branch from a5213cd to e720ff4 Compare December 17, 2021 22:05
@dcbaker dcbaker marked this pull request as ready for review December 17, 2021 22:08
@dcbaker dcbaker force-pushed the submit/structured_sources branch 2 times, most recently from fb451e7 to 2f60a57 Compare December 18, 2021 03:34
@dcbaker dcbaker modified the milestones: 0.61.0, 0.62.0 Jan 6, 2022
@dcbaker dcbaker force-pushed the submit/structured_sources branch 4 times, most recently from f33f1a2 to 9893416 Compare February 23, 2022 23:28
@dcbaker dcbaker force-pushed the submit/structured_sources branch from 9893416 to f4fc2c8 Compare March 2, 2022 20:38
@dcbaker
Copy link
Member Author

dcbaker commented Mar 2, 2022

@rgommers: I've added a cython test, and it compiles but doesn't run. Maybe you can help me, I'm bashing my head against the wall, the merge window closes Sunday, and I'd really like to get this in before that.

@eli-schwartz
Copy link
Member

You have created:

builddir
├── lib.cpython-310-x86_64-linux-gnu.so
└── lib.cpython-310-x86_64-linux-gnu.so.p
    ├── meson-generated_structured_gen_gen.pyx.c.o
    ├── meson-generated_structured_lib.pyx.c.o
    └── structured
        ├── gen
        │   └── gen.pyx
        ├── lib.pyx
        └── lib.pyx.c

Then you add builddir to the PYTHONPATH, run a script that imports lib, which works, and then lib itself tries to do from gen import generated however gen.cpython-310-x86_64-linux-gnu.so is never created. The object file built from gen.pyx is being merged into the lib extension_module.

It seems like the problem is that the structured source is being treated as a file layout for compiling lib, when what you actually want is a file layout for compiling both lib and gen as well as syncing an entire tree of pure python source code into a directory not named *.p/ suitable for public access.

@dcbaker dcbaker force-pushed the submit/structured_sources branch from f4fc2c8 to 54837ab Compare March 2, 2022 23:05
@dcbaker
Copy link
Member Author

dcbaker commented Mar 2, 2022

So, for the moment I've dropped cython support, and I'll go at that again in a follow up, as it's going to require some thought on how it's going to work, and I'm not sure what that needs to look like.

@dcbaker dcbaker force-pushed the submit/structured_sources branch from 54837ab to 5c37f14 Compare March 3, 2022 03:57
@dcbaker dcbaker force-pushed the submit/structured_sources branch 2 times, most recently from eb5fbfc to eef70a3 Compare March 5, 2022 03:57
@jpakkane
Copy link
Member

jpakkane commented Mar 5, 2022

As this is a notable feature that has been long underway, I'll grant an exception so that this can be merged after the actual DL in case it can't get reviewed in time.

*Note* This feature was added in 0.60

You can use a "structured source" for this. Structured sources are a dictionary
mapping a string of the directory ('' means root), to a source or list of sources.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain what "root" means here, specifically. It is a term with many potential meanings.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, that's more leftover old implementation, '' is not allowed in the dictionary. I've fixed that and updated the 0.60 comment.

@jpakkane
Copy link
Member

jpakkane commented Mar 7, 2022

Featurewise this seems like a useful addition so approval on that front.

I have not looked too deeply in the implementation yet, but it seems that there should be a failing test case when using two different structured sources that define the same file in two different structured sources. Something like:

executable(...,
    structured_sources({'subdir': 'clashing.rs'}),
    structured_sources({'subdir': 'other/clashing.rs'})
)

Either there can only be one structured sources per target (which seems a bit limiting) or it should validate all the files upfront.

@dcbaker
Copy link
Member Author

dcbaker commented Mar 7, 2022

Only one structured_source is allowed to be passed, but I can add a test to ensure that passing two structured_sources fails.

Actually, that's interesting, yeah. I can fix that.

@dcbaker dcbaker force-pushed the submit/structured_sources branch from eef70a3 to cc54294 Compare March 7, 2022 18:21
dcbaker added a commit to dcbaker/vscode-meson that referenced this pull request Mar 7, 2022
@dcbaker dcbaker force-pushed the submit/structured_sources branch from cc54294 to ff4c283 Compare March 7, 2022 20:33
@dcbaker
Copy link
Member Author

dcbaker commented Mar 8, 2022

@jpakkane I've added the validation and tests you wanted

@jpakkane jpakkane merged commit 69ade4f into mesonbuild:master Mar 12, 2022
@dcbaker dcbaker deleted the submit/structured_sources branch March 12, 2022 23:55
dcbaker added a commit to mesonbuild/vscode-meson that referenced this pull request Mar 14, 2022
@lithomas1 lithomas1 mentioned this pull request Jul 25, 2022
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants