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

C++ modules, xmake and compile_commands.json? #4330

Closed
Tomcat-42 opened this issue Oct 27, 2023 · 11 comments
Closed

C++ modules, xmake and compile_commands.json? #4330

Tomcat-42 opened this issue Oct 27, 2023 · 11 comments
Labels

Comments

@Tomcat-42
Copy link

Xmake Version

v2.8.3

Operating System Version and Architecture

arch linux

Describe Bug

I am experimenting with C++ modules and Xmake. Building and running modularized code on xmake works flawlessly, however, clangd seems not to work with the exported compile_commands.json.

I have the latest llvm ecossystem instaled (clang 18.0.0 and the latest libc++) with the experimental clangd modules support applyed on top.

Expected Behavior

Is this a expected behavior? Does Xmake support generating a compile_commands.json compatible with c++ modules?

Project Configuration

My source code tree(basically the stl_headerunit example):

.
├── compile_commands.json
├── src
│  ├── hello.cpp
│  └── main.cpp
├── test.lua
└── xmake.lua

My xmake.lua:

add_rules("mode.release", "mode.debug")
set_languages("c++2b")
set_toolchains("clang")
add_cxxflags(
	"-fmodules",
	"-fimplicit-modules",
	"-fmodule-map-file=/usr/include/c++/v1/module.modulemap",
	"-std=c++2b",
	"-stdlib=libc++",
	"-nostdlib++",
	"-Wall",
	"-Wextra",
	"-Werror",
	"-Wpedantic",
	{ force = true }
)
add_ldflags("-lc++", "-lc++abi", "-lfmt", { force = true })

target("stl_headerunit")
set_kind("binary")
set_policy("build.c++.modules", true)
set_policy("build.c++.clang.stdmodules", true)
add_files("src/*.cpp")

Additional Information and Error Logs

The generation of my compile_commands.json

➜  xmake project -k compile_commands
[  0%]: generating.module.deps src/main.cpp
[  0%]: generating.module.deps src/hello.cpp
create ok!

The file

[
  {
    "directory": "/home/pablo/xmake/tests/projects/c++/modules/stl_headerunit",
    "arguments": [
      "/usr/bin/clang",
      "-Qunused-arguments",
      "-m64",
      "-fvisibility=hidden",
      "-fvisibility-inlines-hidden",
      "-O3",
      "-fmodules",
      "-fimplicit-modules",
      "-fmodule-map-file=/usr/include/c++/v1/module.modulemap",
      "-std=c++2b",
      "-stdlib=libc++",
      "-nostdlib++",
      "-Wall",
      "-Wextra",
      "-Werror",
      "-Wpedantic",
      "-DNDEBUG",
      "-fmodules-cache-path=build/.gens/stl_headerunit/linux/x86_64/release/rules/modules/cache",
      "-x",
      "c++-module",
      "--precompile",
      "-c",
      "src/hello.cpp",
      "-o",
      "build/.gens/stl_headerunit/linux/x86_64/release/rules/modules/cache/37b9dad1/hello.pcm"
    ],
    "file": "src/hello.cpp"
  },
  {
    "directory": "/home/pablo/xmake/tests/projects/c++/modules/stl_headerunit",
    "arguments": [
      "/usr/bin/clang",
      "-c",
      "-Qunused-arguments",
      "-m64",
      "-fvisibility=hidden",
      "-fvisibility-inlines-hidden",
      "-O3",
      "-fmodules",
      "-fimplicit-modules",
      "-fmodule-map-file=/usr/include/c++/v1/module.modulemap",
      "-std=c++2b",
      "-stdlib=libc++",
      "-nostdlib++",
      "-Wall",
      "-Wextra",
      "-Werror",
      "-Wpedantic",
      "-DNDEBUG",
      "-fmodule-file=hello=build/.gens/stl_headerunit/linux/x86_64/release/rules/modules/cache/37b9dad1/hello.pcm",
      "-o",
      "build/.objs/stl_headerunit/linux/x86_64/release/src/main.cpp.o",
      "src/main.cpp"
    ],
    "file": "src/main.cpp"
  },
  {
    "directory": "/home/pablo/xmake/tests/projects/c++/modules/stl_headerunit",
    "arguments": [
      "/usr/bin/clang",
      "-c",
      "-Qunused-arguments",
      "-m64",
      "-fvisibility=hidden",
      "-fvisibility-inlines-hidden",
      "-O3",
      "-fmodules",
      "-fimplicit-modules",
      "-fmodule-map-file=/usr/include/c++/v1/module.modulemap",
      "-std=c++2b",
      "-stdlib=libc++",
      "-nostdlib++",
      "-Wall",
      "-Wextra",
      "-Werror",
      "-Wpedantic",
      "-DNDEBUG",
      "-o",
      "build/.objs/stl_headerunit/linux/x86_64/release/src/hello.cpp.o",
      "src/hello.cpp"
    ],
    "file": "src/hello.cpp"
  }
]
@Tomcat-42 Tomcat-42 added the bug label Oct 27, 2023
@waruqi
Copy link
Member

waruqi commented Oct 28, 2023

It should work for 2.8.3

see #4199

@mathstuf
Copy link

Note that I don't think compile_commands.json is suitable for module information. There is some effort in SG15 to define a file, similar to compile_commands.json, that does provide sufficient information.

@Tomcat-42
Copy link
Author

Idk if the problem is in my setup or in clangd then, because I keep getting the error:

Area-2023-10-28_10:59:18

@waruqi
Copy link
Member

waruqi commented Oct 28, 2023

You can check the generated compile_commands, if the generated flags don't have any problem, it should be an issue with clangd.

@Arthapz
Copy link
Member

Arthapz commented Nov 1, 2023

I'm working on a big refactor of module compilation so maybe somes of your issues Will be fixed
With the current implementation the compile_commands """work""" if your clangd is exactly the same version of the clang used for module compilation but it require full project compilation before linting and (and you need to rebuild your files before the lint get updatef)

@Tomcat-42
Copy link
Author

I'm working on a big refactor of module compilation so maybe somes of your issues Will be fixed
With the current implementation the compile_commands """work""" if your clangd is exactly the same version of the clang used for module compilation but it require full project compilation before linting and (and you need to rebuild your files before the lint get updatef)

Yeah, my clangd and clang are both in version 18.0. Also, my stl header modules are generated using my system clang( I compiled libc++ from source), but despite of that I can't get it working.

Let me know when you progress with that. I can help too if you want.

@Arthapz
Copy link
Member

Arthapz commented Nov 1, 2023

I'm working on a big refactor of module compilation so maybe somes of your issues Will be fixed
With the current implementation the compile_commands """work""" if your clangd is exactly the same version of the clang used for module compilation but it require full project compilation before linting and (and you need to rebuild your files before the lint get updatef)

Yeah, my clangd and clang are both in version 18.0. Also, my stl header modules are generated using my system clang( I compiled libc++ from source), but despite of that I can't get it working.

Let me know when you progress with that. I can help too if you want.

No problem, i'll probably recompile my llvm with the clangd PR to try it

@waruqi
Copy link
Member

waruqi commented Feb 19, 2024

Note that I don't think compile_commands.json is suitable for module information. There is some effort in SG15 to define a file, similar to compile_commands.json, that does provide sufficient information.

Does clangd support it now?

@Tomcat-42
Copy link
Author

No, the support is incomplete: llvm/llvm-project#66462.

I think we can close this.

@Tomcat-42
Copy link
Author

Note that, despite some intellisense working, I still got errors while importing std modules, like:

Could not build module std_iostream

@Arthapz
Copy link
Member

Arthapz commented Feb 19, 2024

Note that, despite some intellisense working, I still got errors while importing std modules, like:

Could not build module std_iostream

The only way i got the intellisense working is by using only modules (and not headerunits)
On Linux / macOS, and it work only after a full build of the project (and modification of a module interface is only updated after rebuild)

@waruqi waruqi closed this as completed Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants