Skip to content

Commit

Permalink
Merge pull request #4321 from Arthapz/improve-modules-support
Browse files Browse the repository at this point in the history
Refactor modules support
  • Loading branch information
waruqi authored Feb 2, 2024
2 parents 5c31fdb + b1005e8 commit b81eed5
Show file tree
Hide file tree
Showing 44 changed files with 3,204 additions and 2,912 deletions.
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

0 comments on commit b81eed5

Please sign in to comment.