Skip to content

Commit

Permalink
Merge pull request #5376 from Arthapz/fix-module-objectfiles-handling
Browse files Browse the repository at this point in the history
Improve module objectfiles handling and moduleonly package
  • Loading branch information
waruqi authored Jul 25, 2024
2 parents 2eac22e + 626b80e commit deef593
Show file tree
Hide file tree
Showing 28 changed files with 504 additions and 189 deletions.
70 changes: 69 additions & 1 deletion tests/projects/c++/modules/culling/test.lua
Original file line number Diff line number Diff line change
@@ -1 +1,69 @@
inherit(".test_base")
import("lib.detect.find_tool")
import("core.base.semver")
import("utils.ci.is_running", {alias = "ci_is_running"})

function _build()
local outdata
if ci_is_running() then
outdata = os.iorun("xmake -rvD")
else
outdata = os.iorun("xmake -rv")
end
if outdata then
if outdata:find("culled") then
raise("Modules culling does not work\n%s", outdata)
end
end
end

function can_build()
if is_subhost("windows") then
return true
elseif is_subhost("msys") then
return true
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
return true
end
local clang = find_tool("clang", {version = true})
if clang and clang.version and semver.compare(clang.version, "14.0") >= 0 then
return true
end
end
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, "17.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 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 --yes")
_build()
os.exec("xmake clean -a")
os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes")
_build()
end
end
end
70 changes: 69 additions & 1 deletion tests/projects/c++/modules/culling2/test.lua
Original file line number Diff line number Diff line change
@@ -1 +1,69 @@
inherit(".test_base")
import("lib.detect.find_tool")
import("core.base.semver")
import("utils.ci.is_running", {alias = "ci_is_running"})

function _build()
local outdata
if ci_is_running() then
outdata = os.iorun("xmake -rvD")
else
outdata = os.iorun("xmake -rv")
end
if outdata then
if outdata:find("culled") then
raise("Modules culling does not work\n%s", outdata)
end
end
end

function can_build()
if is_subhost("windows") then
return true
elseif is_subhost("msys") then
return true
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
return true
end
local clang = find_tool("clang", {version = true})
if clang and clang.version and semver.compare(clang.version, "14.0") >= 0 then
return true
end
end
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, "17.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 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 --yes")
_build()
os.exec("xmake clean -a")
os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes")
_build()
end
end
end
10 changes: 10 additions & 0 deletions tests/projects/c++/modules/culling3/src/hello.mpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module;
#include <cstdio>

export module hello;

export namespace hello {
void say(const char* str) {
printf("%s\n", str);
}
}
69 changes: 69 additions & 0 deletions tests/projects/c++/modules/culling3/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import("lib.detect.find_tool")
import("core.base.semver")
import("utils.ci.is_running", {alias = "ci_is_running"})

function _build()
local outdata
if ci_is_running() then
outdata = os.iorun("xmake -rvD")
else
outdata = os.iorun("xmake -rv")
end
if outdata then
if not outdata:find("culled") then
raise("Modules culling does not work\n%s", outdata)
end
end
end

function can_build()
if is_subhost("windows") then
return true
elseif is_subhost("msys") then
return true
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
return true
end
local clang = find_tool("clang", {version = true})
if clang and clang.version and semver.compare(clang.version, "14.0") >= 0 then
return true
end
end
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, "17.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 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 --yes")
_build()
os.exec("xmake clean -a")
os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes")
_build()
end
end
end
6 changes: 6 additions & 0 deletions tests/projects/c++/modules/culling3/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_rules("mode.release", "mode.debug")
set_languages("c++20")

target("culling")
set_kind("static")
add_files("src/*.mpp")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export module duplicate;
export module foo;

export int value() {
return 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export module duplicate;
export module foo;

export int value() {
return 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import duplicate;
import foo;

int main() {
return value();
Expand Down
75 changes: 75 additions & 0 deletions tests/projects/c++/modules/duplicate_name_detection/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import("lib.detect.find_tool")
import("core.base.semver")
import("utils.ci.is_running", {alias = "ci_is_running"})

function _build()
try {
function()
if ci_is_running() then
os.run("xmake -rvD")
else
os.run("xmake -r")
end
end,
catch {
function (errors)
errors = tostring(errors)
if not errors:find("duplicate module name detected", 1, true) then
raise("Modules duplicate name detection does not work\n%s", errors)
end
end
}
}
end

function can_build()
if is_subhost("windows") then
return true
elseif is_subhost("msys") then
return true
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
return true
end
local clang = find_tool("clang", {version = true})
if clang and clang.version and semver.compare(clang.version, "14.0") >= 0 then
return true
end
end
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, "17.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 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 --yes")
_build()
os.exec("xmake clean -a")
os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes")
_build()
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ target("bar")
set_kind("moduleonly")
add_files("src/bar.mpp")

target("link_order_1")
target("duplicate_name_detection_1")
set_kind("binary")
add_deps("foo", "bar")
add_files("src/main.cpp")

target("link_order_2")
target("duplicate_name_detection_2")
set_kind("binary")
add_deps("bar", "foo")
add_files("src/main.cpp")
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package("bar2")
set_kind("library")
set_kind("library", {moduleonly = true})
set_sourcedir(path.join(os.scriptdir(), "src"))

on_install(function(package)
Expand Down
8 changes: 8 additions & 0 deletions tests/projects/c++/modules/staticlib2/src/bar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module bar;

namespace bar {
int hello() {
return 2;
}
}

6 changes: 6 additions & 0 deletions tests/projects/c++/modules/staticlib2/src/bar.mpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export module bar;

export namespace bar {
int hello();
}

10 changes: 10 additions & 0 deletions tests/projects/c++/modules/staticlib2/src/foo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module foo;

import bar;

namespace foo {
int hello() {
return bar::hello();
}
}

6 changes: 6 additions & 0 deletions tests/projects/c++/modules/staticlib2/src/foo.mpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export module foo;

export namespace foo {
int hello();
}

8 changes: 8 additions & 0 deletions tests/projects/c++/modules/staticlib2/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>
import foo;

int main() {
printf("%d\n", foo::hello());
return 0;
}

Loading

0 comments on commit deef593

Please sign in to comment.