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

Order of compilation is not correct when building a multi-target C++20 project with modules #1858

Closed
ozankabak opened this issue Nov 23, 2021 · 3 comments

Comments

@ozankabak
Copy link

ozankabak commented Nov 23, 2021

Describe the bug

The order of compilation is not correct when building a multi-target C++20 project with modules. The likely culprit is the wrong handling of parallelization across targets. Here are the files to reproduce:

// main.cpp
import mod;
int main() { f(); return 0; }
// mod.mpp
export module mod;
export void f();
// mod.cpp
module mod;
void f() {}
-- xmake.lua
set_languages("c++20")
add_cxxflags("-fmodules-ts")

target("mod")
  set_kind("static")
  add_files("mod.mpp", "mod.cpp")

target("main")
  set_kind("binary")
  add_deps("mod")
  add_files("main.cpp")
  -- set_policy("build.across_targets_in_parallel", false)

To reliably reproduce, use:

$ xmake config --clean && xmake config --toolchain=gcc-11
$ rm -rf build/ gcm.cache/ && xmake build --rebuild -v

Expected behavior

The build should go through and the static library should be built before the binary. Note that we can force this to happen by uncommenting the line set_policy("build.across_targets_in_parallel", false). If we manually do this, the build goes through. The expected behavior is that xmake should automatically enforce this when necessary.

Error output

checking for gcc-11 ... /usr/local/bin/gcc-11
checking for the c++ compiler (cxx) ... gcc-11
checking for /usr/local/bin/gcc-11 ... ok
checking for flags (-fmodules-ts) ... ok
generating.moduledeps mod.mpp
checking for g++-11 ... /usr/local/bin/g++-11
checking for the linker (ld) ... g++-11
checking for flags (-std=c++20) ... ok
checking for flags (-fmodules-ts) ... ok
checking for ccache ... no
[ 22%]: compiling.release main.cpp
/usr/local/bin/gcc-11 -c -m64 -std=c++20 -fmodules-ts -o build/.objs/main/macosx/x86_64/release/main.cpp.o main.cpp
[ 22%]: compiling.release mod.mpp
/usr/local/bin/gcc-11 -c -m64 -std=c++20 -fmodules-ts -x c++ -o build/.objs/mod/macosx/x86_64/release/mod.mpp.o mod.mpp
checking for flags (-MMD -MF) ... ok
checking for flags (-fdiagnostics-color=always) ... ok
error: In module imported at main.cpp:1:1:
mod: error: failed to read compiled module: No such file or directory
mod: note: compiled module file is 'gcm.cache/mod.gcm'
mod: note: imports must be built before being imported
mod: fatal error: returning to the gate for a mechanical issue
compilation terminated.

Related Environment

  • xmake version: v2.5.9+202110311547
  • os: macOS Big Sur v11.6.1
  • target platform: x86_64
@ozankabak ozankabak added the bug label Nov 23, 2021
@ozankabak
Copy link
Author

ozankabak commented Nov 23, 2021

BTW, the same example also doesn't work with clang. This time, there are two simultaneous reasons. The first reason is this bug. The second reason is that xmake forgets to append the -fmodules-cache-path flag when compiling the binary target. Obviously, it should have added the flag because the binary depends on the static library, which is declared with the add_deps call in the make file. Please consider this as another distinct bug report. I am not opening a separate issue for this second bug, since the minimal example to reproduce is the same and I don't want to clutter your issues list. We can discuss both here. Thanks!

@waruqi
Copy link
Member

waruqi commented Nov 23, 2021

I have improved it for gcc11/clang, you can try it again on dev branch.

and you can remove add_cxxflags("-fmodules-ts")

@ozankabak
Copy link
Author

Thank you, looking forward to the next release including this 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants