Skip to content

Commit

Permalink
fix package defines handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthapz committed Dec 13, 2023
1 parent 6815924 commit bfc149f
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <a.hpp>

const char *bar() {
const char *hello() {
return "Hello world";
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module;

export module bar;

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

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

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

#endif
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package("bar")
set_sourcedir(path.join(os.scriptdir(), "src"))

add_defines("FOO_EXPORT")

on_install(function(package)
import("package.tools.xmake").install(package, {})
end)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ set_languages("c++20")
target("foo")
set_kind("static")
add_files("*.cpp")
add_files("*.mpp", { install = true })
add_defines("FOO_EXPORT")
add_files("*.mpp", {defines = "FOO_EXPORT", install = 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;
}
4 changes: 4 additions & 0 deletions xmake/rules/c++/modules/modules_support/builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ end
--
-- e.g
-- {
-- "defines": ["FOO=BAR"]
-- "imports": ["std", "bar"]
-- "name": "foo"
-- "file": "foo.cppm"
Expand All @@ -156,6 +157,9 @@ function _generate_meta_module_info(target, name, sourcefile, requires)
local modulehash = compiler_support.get_modulehash(target, sourcefile)
local module_metadata = {name = name, file = path.join(modulehash, path.filename(sourcefile))}

-- add definitions
module_metadata.defines = _builder(target).get_module_required_defines(target, sourcefile)

-- add imports
if requires then
for name, _ in pairs(requires) do
Expand Down
16 changes: 16 additions & 0 deletions xmake/rules/c++/modules/modules_support/clang/builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,22 @@ end
function init_build_for(...)
end

-- get defines for a module
function get_module_required_defines(target, sourcefile)
local compinst = compiler.load("cxx", {target = target})
local compflags = compinst:compflags({sourcefile = sourcefile, target = target})
local defines

for _, flag in ipairs(compflags) do
if flag:startswith("-D") then
defines = defines or {}
table.insert(defines, flag:sub(3))
end
end

return defines
end

-- build module file for batchjobs
function make_module_build_job(target, batchjobs, job_name, deps, name, provide, module, cppfile, objectfile, opt)

Expand Down
16 changes: 16 additions & 0 deletions xmake/rules/c++/modules/modules_support/gcc/builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,22 @@ function init_build_for(target, batch, modules, opt)
end
end

-- get defines for a module
function get_module_required_defines(target, sourcefile)
local compinst = compiler.load("cxx", {target = target})
local compflags = compinst:compflags({sourcefile = sourcefile, target = target})
local defines

for _, flag in ipairs(compflags) do
if flag:startswith("-D") then
defines = defines or {}
table.insert(defines, flag:sub(3))
end
end

return defines
end

-- build module file for batchjobs
function make_module_build_job(target, batchjobs, job_name, deps, name, provide, module, cppfile, objectfile, opt)

Expand Down
3 changes: 3 additions & 0 deletions xmake/rules/c++/modules/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ rule("c++.build.modules.builder")
-- append to sourcebatch
for _, package_module_data in pairs(package_modules_data) do
table.insert(sourcebatch.sourcefiles, package_module_data.file)
if package_module_data.metadata.defines then
target:fileconfig_add(package_module_data.file, {defines = package_module_data.metadata.defines})
end
end

-- we need to repatch and regenerate dependencies at this point
Expand Down

0 comments on commit bfc149f

Please sign in to comment.