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

CMake subprojects #4969

Merged
merged 35 commits into from
Jun 6, 2019
Merged

CMake subprojects #4969

merged 35 commits into from
Jun 6, 2019

Conversation

mensinda
Copy link
Member

This PR adds support for using CMake projects as subprojects (without requiring a meson.build).

Basic principle

  1. Configure the subproject with CMake
  2. Start the CMake server
  3. Extract the codemodel from the server
  4. Post process the codemodel
  5. Generate a meson AST
  6. Use the existing submodule architecture to execute the AST

Current limitations:

  • Projects that rely on add_custom_command() and/or other code generators won't work
  • Everything else I have missed and don't know (yet).

Tested projects:

This PR was tested with the following projects:

  • SDL (2.0.9) with cmake_options: ['-DVIDEO_WAYLAND=OFF'] (wayland uses a code generator)
  • glslang (master) doesn't work because of a single .y file meson doesn't recognize

If someone has tested other projects with this PR, please let me know.

@mensinda mensinda force-pushed the cmakeSubProject branch 7 times, most recently from 58013fc to 84d435c Compare February 27, 2019 13:01
@mensinda mensinda changed the title CMake subprojects WIP: CMake subprojects Feb 28, 2019
@dcbaker
Copy link
Member

dcbaker commented Mar 5, 2019

I haven't looked too closely at the implementation, but as we discussed on IRC and from your proposal this seems like a fine approach, and probably very useful. The one thing I think I'd prefer is to have a module to handle this rather than being part of subproject.

My reasoning is that a subproject generally has a in meson defined dependency:

dep = declare_dependency(link_with : lib, include_directories : incs)

CMake obviously doesn't have that in the form we would expect, Since we may not want to build all of the targets in the cmake build definition, a module would make it easier to take the AST, store it, and then pull out only the bits that we want so if we wanted two targets out of a CMake build (but at different times or under different condtions) we could do that.

Ultimately this is a @jpakkane decision.

P.S. This is really interesting to me, and if we land this I think I'll work on a version for make/automake that does something similar.

@xclaesse
Copy link
Member

I haven't looked at the patches themself, but my PR also allows building cmake subprojects: #4321

@dcbaker
Copy link
Member

dcbaker commented Mar 25, 2019

@xclaesse, I think this is a better approach, as the goal is to use cmake to configure the project, then use cmake's introspection API to create a meson representation and allow the subproject to be built as part of the main ninja call like a native meson subproject. If it can be made to work reliably of course :)

@mensinda
Copy link
Member Author

@xclaesse I haven't looked at #4321 before, but the PR's seem to have two different approaches (as @dcbaker has already pointed out).

I am currently a bit short on time (I have two more exams), so I am not sure when I have time again to continue working on this PR.

@mensinda mensinda force-pushed the cmakeSubProject branch 4 times, most recently from 2240f63 to 01c0d00 Compare April 19, 2019 08:43
@mensinda mensinda changed the title WIP: CMake subprojects CMake subprojects Apr 19, 2019
@mensinda
Copy link
Member Author

This PR now also works on Windows so it is no longer WIP.

There are some weird issues with the msvc2015 CI that I can't resolve so I disabled the tests there.

@mensinda
Copy link
Member Author

mensinda commented May 3, 2019

ping :)

@ignatenkobrain
Copy link
Member

Flake8 detected 6 issues on 926269e
Visit https://sider.review/gh/repos/19784232/pulls/4969 to review the issues.

posted by Sider

Copy link
Contributor

@FelixSchwarz FelixSchwarz left a comment

Choose a reason for hiding this comment

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

nitpick: Typo in the commit comment "available"

@FelixSchwarz
Copy link
Contributor

Just a question from a meson newbie: How do include dirs work with this patch?
I built a simple exe which links to docopt.cpp (cmake) as a subproject but compilation of my executable fails because there is no "-I" pointing to docopt? My workaround was to use executable(..., subprojects/docopt.cpp)

Other than that your code worked great for me - even cross-compiling with Windows worked. :-)

(Excuse me if this comment derails the pull request. If you want me to post in a different - or not at all - please just tell me.)

Copy link
Contributor

@FelixSchwarz FelixSchwarz left a comment

Choose a reason for hiding this comment

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

There are a lot of changes due to trailing newline in this commit. Not sure what meson's policy is about unrelated cosmetic changes.

@mensinda
Copy link
Member Author

@FelixSchwarz for each CMake target, this PR generates a meson target with the same name and the variables targetname_inc for the include directories, targetname_src for the source list (though this might not be useful) and targetname_dep which should be used as a dependency in your meson.build.

Basically this CMake code:

set(source_list "docopt.cpp")
include_directories(...)
add_executable(foo ${source_list})

is converted into this meson code:

foo_inc = include_directories('__CMake_build', '.', ...)
foo_src = files('docopt.cpp')
foo = executable(
  'foo',
  foo_src,
  link_args : [],
  link_with : [],
  include_directories : foo_inc,
  install : false,
  install_dir : '',
  override_options : [],
  objects : [],
  cpp_args : []
)
foo_dep = declare_dependency(
  link_args : [],
  link_with : foo,
  include_directories : foo_inc
)

Then, something like

executable('mainPro', main_src, dependencies: [subPro.get_variable('foo_dep')])

in your main project should work 99% of the time.

You can also view the generated meson code for the subproject in buildDir/meson-logs/meson-log.txt.

Could you please provide an example repository, if this didn't answer your question?

@mensinda
Copy link
Member Author

mensinda commented Jun 6, 2019

Fixed. Let's hope for the CI.

@jpakkane jpakkane merged commit 266b297 into mesonbuild:master Jun 6, 2019
@mensinda mensinda deleted the cmakeSubProject branch June 6, 2019 19:27
@mensinda
Copy link
Member Author

mensinda commented Jun 6, 2019

Thanks!

@mensinda mensinda mentioned this pull request Jul 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants