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

Refactor modules support #4321

Merged
merged 47 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
e20c6ca
improve module tests
Arthapz Jan 23, 2024
d52291f
refactor module common infrastructure
Arthapz Jan 23, 2024
2148980
refactor clang module infrastructure
Arthapz Jan 23, 2024
da5f589
refactor gcc module infrastructure
Arthapz Jan 23, 2024
82714f7
refactor msvc module infrastructure
Arthapz Jan 23, 2024
7dddd80
optimise clang fallback dependency scanner
Arthapz Jan 23, 2024
a6f1cc3
add a policy to toggle C++23 std module
Arthapz Jan 23, 2024
d914662
fix headerunit compilation on clang
Arthapz Jan 23, 2024
35f41b6
fix gcc module support
Arthapz Jan 23, 2024
a165624
use execv instead of iorunv
Arthapz Jan 23, 2024
713e8a5
Revert "use execv instead of iorunv"
Arthapz Jan 23, 2024
931e8c6
reenable build across targets in parallel policy
Arthapz Jan 25, 2024
635f106
Revert "reenable build across targets in parallel policy"
Arthapz Jan 26, 2024
300600d
take advantage of XMake runtimes support
Arthapz Jan 26, 2024
cb8b45b
update tests to use XMake runtimes support
Arthapz Jan 26, 2024
d50a83f
apply PR suggestions
Arthapz Jan 29, 2024
952b813
populate module map before generating batchjobs
Arthapz Jan 29, 2024
e8c63dc
fix space indent of build.c++.modules.std policy
Arthapz Jan 30, 2024
0ff527a
remove removed build.c++.clang.stdmodules policy from tests
Arthapz Jan 30, 2024
a9f3040
use -fkeep-system-includes only if supported by underlying clang
Arthapz Jan 30, 2024
7e1cf4f
remove now useless before_link on C++ modules rule
Arthapz Jan 30, 2024
a2ccc58
use try{} instead of assert to preserve iorunv error colors
Arthapz Jan 30, 2024
8e10652
revert last commit
Arthapz Jan 30, 2024
98646d9
update comment
Arthapz Jan 31, 2024
cc88697
remove dead code
Arthapz Jan 31, 2024
95996d7
improve _should_build
Arthapz Jan 31, 2024
f3ec9cc
implement missing headerunit aliasing
Arthapz Jan 31, 2024
89f151a
add a test for aliased headerunits
Arthapz Jan 31, 2024
0b37f42
use os.vrunv for dependency scanning
Arthapz Jan 31, 2024
a5fb58f
improve module cmdline print
Arthapz Jan 31, 2024
2465d8a
use table.orderpair to ensure dependency order stay the same
Arthapz Jan 31, 2024
b9b078d
code cleanup
Arthapz Jan 31, 2024
e339cb5
fix inconsistent print format for gcc module mapper
Arthapz Jan 31, 2024
15f2678
use table.orderpair to ensure dependency order stay the same
Arthapz Jan 31, 2024
77d7dc5
improve std c++ lib handling for clang
Arthapz Jan 31, 2024
47afe0c
cleanup
Arthapz Jan 31, 2024
adee617
fix progress for clang
Arthapz Jan 31, 2024
9cb5dba
fix -fkeep-system-includes detection
Arthapz Jan 31, 2024
0bb30a6
fix potentially nil runtime_flag for clang toolchain include detection
Arthapz Jan 31, 2024
e219f8a
cleanup
Arthapz Jan 31, 2024
f17ccef
use ipairs + orderpkgs
Arthapz Jan 31, 2024
9b378dd
cleanup
Arthapz Jan 31, 2024
7dbdcf9
remove useless path.translate
Arthapz Jan 31, 2024
69e66b7
cleanup
Arthapz Jan 31, 2024
7d5573a
optimise header duplication detection
Arthapz Jan 31, 2024
c7a2537
fix assert in gcc module dependency mapper
Arthapz Feb 1, 2024
b1005e8
use compinst:compile when possible
Arthapz Feb 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tests/projects/c++/modules/aliased_headerunit/src/foo/hello.mpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module;
#include <cstdio>

export module hello;

import "../header.hpp";

export namespace hello {
void say(const char *arg) {
printf("%s: %s\n", FOO, arg);
}
}
5 changes: 5 additions & 0 deletions tests/projects/c++/modules/aliased_headerunit/src/header.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

namespace hello {
inline constexpr auto FOO = "Hello";
}
7 changes: 7 additions & 0 deletions tests/projects/c++/modules/aliased_headerunit/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import hello;
import "header.hpp";

int main() {
hello::say(hello::FOO);
return 0;
}
1 change: 1 addition & 0 deletions tests/projects/c++/modules/aliased_headerunit/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
inherit(".test_headerunits")
8 changes: 8 additions & 0 deletions tests/projects/c++/modules/aliased_headerunit/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_rules("mode.release", "mode.debug")
set_languages("c++20")

-- header.hpp should be built only one time
target("aliased_headerunit")
set_kind("binary")
add_headerfiles("src/*.hpp")
add_files("src/*.cpp", "src/foo/*.mpp")
3 changes: 2 additions & 1 deletion tests/projects/c++/modules/cpp_with_moduledeps/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ set_languages("c++20")

target("mod")
set_kind("static")
add_files("src/mod.mpp", "src/mod.cpp")
add_files("src/mod.mpp", {public = true})
add_files("src/mod.cpp")

target("cpp_with_moduledeps")
set_kind("binary")
Expand Down
6 changes: 3 additions & 3 deletions tests/projects/c++/modules/inline_and_template/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include <iostream>

import hello;
import say;
import foo;

#include <iostream>

int main() {
hello::say_hello();
say{}.hello<sizeof(say)>();
std::cout << foo<int>{}.hello() << std::endl;
return 0;
}
}
4 changes: 2 additions & 2 deletions tests/projects/c++/modules/link_order/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ set_languages("c++20")
target("foo")
add_rules("c++")
set_kind("static")
add_files("src/foo.mpp")
add_files("src/foo.mpp", {public = true})

target("bar")
add_rules("c++")
set_kind("static")
add_files("src/bar.mpp")
add_files("src/bar.mpp", {public = true})

target("link_order_1")
set_kind("binary")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module bar;
#include <a.hpp>

const char *bar() {
const char *hello() {
return "Hello world";
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
module;

#include <a.hpp>

export module bar;

export const char *bar();
export namespace bar {
const char *hello() {
return ::hello();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef A_HPP
#define A_HPP

[[gnu::visibility("default")]] const char *hello();

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ set_languages("c++20")

target("bar")
set_kind("static")
add_headerfiles("include/(**.hpp)")
add_includedirs("include")
add_files("*.cpp")
add_files("*.mpp", { install = true })
add_files("*.mpp", { public = true })
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package("bar")

on_install(function(package)
import("package.tools.xmake").install(package, {})
end)
end)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export module foo;

export namespace foo {
#ifdef FOO_EXPORT
void say(const char *);
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ set_languages("c++20")
target("foo")
set_kind("static")
add_files("*.cpp")
add_files("*.mpp", { install = true })
add_files("*.mpp", {defines = "FOO_EXPORT", public = true})
2 changes: 1 addition & 1 deletion tests/projects/c++/modules/packages/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import foo;
import bar;

int main() {
foo::say(bar());
foo::say(bar::hello());
return 0;
}
2 changes: 1 addition & 1 deletion tests/projects/c++/modules/packages/xmake.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_rules("mode.release", "mode.debug")
set_languages("c++20")
set_languages("c++2b")

add_repositories("my-repo my-repo")
add_requires("foo", "bar")
Expand Down
2 changes: 1 addition & 1 deletion tests/projects/c++/modules/staticlib/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set_languages("c++20")

target("mod")
set_kind("static")
add_files("src/mod.mpp", "src/mod.cpp")
add_files("src/mod.mpp", "src/mod.cpp", {public = true})

target("hello")
set_kind("binary")
Expand Down
5 changes: 2 additions & 3 deletions tests/projects/c++/modules/stdmodules/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ set_languages("c++latest")

target("mod")
set_kind("static")
add_files("src/*.cpp", "src/*.mpp")
set_policy("build.c++.clang.stdmodules", true)
add_files("src/*.cpp")
add_files("src/*.mpp", {public = true})

target("stdmodules")
set_kind("binary")
add_files("test/*.cpp")
add_deps("mod")
set_policy("build.c++.clang.stdmodules", true)
1 change: 0 additions & 1 deletion tests/projects/c++/modules/stdmodules_cpp_only/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ target("stdmodules_cpp_only")
set_kind("binary")
add_files("src/*.cpp")
set_policy("build.c++.modules", true)
set_policy("build.c++.clang.stdmodules", true)

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ target("mod")
set_kind("static")
add_files("src/*.cpp", "src/*.mpp")

set_policy("build.c++.clang.stdmodules", true)

target("mod2")
set_kind("static")
add_files("src/*.cpp", "src/*.mpp")

set_policy("build.c++.clang.stdmodules", true)
23 changes: 18 additions & 5 deletions tests/projects/c++/modules/test_base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,34 @@ end

function main(t)
if is_subhost("windows") then
os.exec("xmake f -c")
local clang = find_tool("clang", {version = true})
if clang and clang.version and semver.compare(clang.version, "14.0") >= 0 then
os.exec("xmake f --toolchain=clang -c --yes")
_build()
os.exec("xmake clean -a")
os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes")
_build()
end

os.exec("xmake clean -a")
os.exec("xmake f -c --yes")
_build()
elseif is_subhost("msys") then
os.exec("xmake f -c -p mingw --yes")
_build()
elseif is_host("linux") then
local gcc = find_tool("gcc", {version = true})
if is_host("linux") and gcc and gcc.version and semver.compare(gcc.version, "11.0") >= 0 then
os.exec("xmake f -c")
if gcc and gcc.version and semver.compare(gcc.version, "11.0") >= 0 then
os.exec("xmake f -c --yes")
_build()
end
local clang = find_tool("clang", {version = true})
if clang and clang.version and semver.compare(clang.version, "14.0") >= 0 then
os.exec("xmake clean -a")
os.exec("xmake f --toolchain=clang -c")
os.exec("xmake f --toolchain=clang -c --yes")
_build()
os.exec("xmake clean -a")
os.exec("xmake f --toolchain=clang --cxxflags=\"-stdlib=libc++\" -c")
os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes")
_build()
end
end
Expand Down
35 changes: 27 additions & 8 deletions tests/projects/c++/modules/test_headerunits.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,33 @@ end

function main(t)
if is_subhost("windows") then
local clang = find_tool("clang", {version = true})
if clang and clang.version and semver.compare(clang.version, "15.0") >= 0 then
-- clang headerunit are bugged
-- os.exec("xmake f --toolchain=clang --policies=build.c++.clang.fallbackscanner -c --yes")
-- _build()
-- if semver.compare(clang.version, "17.0") >= 0 then
-- os.exec("xmake clean -a")
-- -- clang-scan-deps dependency detection doesn't support header units atm
-- os.exec("xmake f --toolchain=clang --policies=build.c++.clang.fallbackscanner --runtimes=c++_shared -c --yes")
-- _build()
-- end
end

local vs = find_vstudio()
if vs and vs["2022"] then
os.exec("xmake clean -a")
os.exec("xmake f -c --yes")
_build()
end
elseif is_subhost("msys") then
-- on windows, mingw modulemapper doesn't handle headeunit path correctly, but it's working with mingw on macOS / Linux
-- os.exec("xmake f -c -p mingw --yes")
-- _build()
elseif is_host("linux") then
local gcc = find_tool("gcc", {version = true})
if gcc and gcc.version and semver.compare(gcc.version, "11.0") >= 0 then
-- gcc trtbd dependency detection doesn't support header units atm
-- gcc dependency detection doesn't support header units atm
os.exec("xmake f --policies=build.c++.gcc.fallbackscanner -c --yes")
_build()
end
Expand All @@ -30,15 +48,16 @@ function main(t)
if semver.compare(clang.version, "15.0") >= 0 then
os.exec("xmake clean -a")
-- clang-scan-deps dependency detection doesn't support header units atm
os.exec("xmake f --toolchain=clang --policies=build.c++.clang.fallbackscanner -c")
os.exec("xmake f --toolchain=clang --policies=build.c++.clang.fallbackscanner -c --yes")
_build()
-- elseif semver.compare(clang.version, "15.0") >= 0 then
-- there is currently a bug on llvm git that prevent to build STL header units https://github.com/llvm/llvm-project/issues/58540
-- os.exec("xmake clean -a")
-- clang-scan-deps dependency detection doesn't support header units atm
-- os.exec("xmake f --toolchain=clang --policies=build.c++.modules.fallbackscanner.clang --cxxflags=\"-stdlib=libc++\" -c")
-- _build()
end
-- libc++ headerunit are bugged
-- if semver.compare(clang.version, "17.0") >= 0 then
-- os.exec("xmake clean -a")
-- -- clang-scan-deps dependency detection doesn't support header units atm
-- os.exec("xmake f --toolchain=clang --policies=build.c++.clang.fallbackscanner --runtimes=c++_shared -c --yes")
-- _build()
-- end
end
end
end
32 changes: 23 additions & 9 deletions tests/projects/c++/modules/test_stdmodules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,47 @@ end

function main(t)
if is_subhost("windows") then
-- local clang = find_tool("clang", {version = true})
-- if clang and clang.version and semver.compare(clang.version, "18.0") >= 0 then
-- os.exec("xmake f --toolchain=clang -c --yes")
-- _build()
-- clang don't support libc++ std modules atm
-- os.exec("xmake clean -a")
-- os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes")
-- _build()
-- end
local msvc = toolchain.load("msvc")
if msvc and msvc:check() then
local vcvars = msvc:config("vcvars")
if vcvars and vcvars.VCInstallDir and vcvars.VCToolsVersion and semver.compare(vcvars.VCToolsVersion, "14.35") then
local stdmodulesdir = path.join(vcvars.VCInstallDir, "Tools", "MSVC", vcvars.VCToolsVersion, "modules")
if os.isdir(stdmodulesdir) then
os.exec("xmake f -c")
os.exec("xmake clean -a")
os.exec("xmake f -c --yes")
_build()
end
end
end
elseif is_subhost("msys") then
-- os.exec("xmake f -c -p mingw --yes")
-- _build()
elseif is_host("linux") then -- or is_host("macosx") then
-- gcc don't support std modules atm
-- local gcc = find_tool("gcc", {version = true})
-- if is_host("linux") and gcc and gcc.version and semver.compare(gcc.version, "11.0") >= 0 then
-- os.exec("xmake f -c")
-- os.exec("xmake f -c --yes")
-- _build()
-- end
local clang = find_tool("clang", {version = true})
if clang and clang.version and semver.compare(clang.version, "14.0") >= 0 then
-- local clang = find_tool("clang", {version = true})
-- if clang and clang.version and semver.compare(clang.version, "18.0") >= 0 then
-- clang don't support libstdc++ std modules atm
-- os.exec("xmake clean -a")
-- os.exec("xmake f --toolchain=clang -c")
-- os.exec("xmake f --toolchain=clang -c --yes")
-- _build()
os.exec("xmake clean -a")
os.exec("xmake f --toolchain=clang --cxxflags=\"-stdlib=libc++\" -c")
_build()
end
-- clang don't support libc++ std modules atm
-- os.exec("xmake clean -a")
-- os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes")
-- _build()
-- end
end
end
2 changes: 1 addition & 1 deletion tests/projects/c++/modules/user_headerunit2/a/xmake.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target("a")
set_languages("cxxlatest")
set_kind("object")
add_files("a.mpp")
add_files("a.mpp", {public = true})
2 changes: 1 addition & 1 deletion tests/projects/c++/modules/user_headerunit2/b/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
add_deps("a")
set_languages("cxxlatest")
set_kind("object")
add_files("b.mpp")
add_files("b.mpp", {public = true})
4 changes: 2 additions & 2 deletions xmake/core/project/policy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ function policy.policies()
["build.sanitizer.undefined"] = {description = "Enable undefined sanitizer for c/c++ building.", type = "boolean"},
-- Enable C++ modules for C++ building, even if no .mpp is involved in the compilation
["build.c++.modules"] = {description = "Enable C++ modules for C++ building.", type = "boolean"},
-- Enable clang std modulemap
["build.c++.clang.stdmodules"] = {description = "Enable clang std modulemap.", default = false, type = "boolean"},
-- Enable std module
["build.c++.modules.std"] = {description = "Enable std modules.", default = true, type = "boolean"},
-- Force C++ modules fallback dependency scanner for clang
["build.c++.clang.fallbackscanner"] = {description = "Force clang fallback module dependency scanner.", default = false, type = "boolean"},
-- Force C++ modules fallback dependency scanner for msvc
Expand Down
Loading
Loading