summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-09-29 22:47:38 +0800
committerruki <[email protected]>2021-09-29 22:47:38 +0800
commit913fcb7fbe40b009eb6759dd00b37ece56ef72a8 (patch)
treed9997a6907cf162e47368b0f501938054c7aa731
parent9b11147b98d5b51bc5064757c7f3629e5ad37cbb (diff)
support to build embed package with cmake
-rw-r--r--tests/projects/c/library_with_cmakelists/test.lua6
-rw-r--r--tests/projects/c/library_with_cmakelists/xmake.lua6
-rw-r--r--xmake/core/package/package.lua30
-rw-r--r--xmake/core/project/config.lua8
-rw-r--r--xmake/modules/package/tools/cmake.lua4
5 files changed, 43 insertions, 11 deletions
diff --git a/tests/projects/c/library_with_cmakelists/test.lua b/tests/projects/c/library_with_cmakelists/test.lua
new file mode 100644
index 000000000..b76241be2
--- /dev/null
+++ b/tests/projects/c/library_with_cmakelists/test.lua
@@ -0,0 +1,6 @@
+-- main entry
+function main(t)
+
+ -- build project
+ t:build()
+end
diff --git a/tests/projects/c/library_with_cmakelists/xmake.lua b/tests/projects/c/library_with_cmakelists/xmake.lua
index 4bf632e08..e5646c372 100644
--- a/tests/projects/c/library_with_cmakelists/xmake.lua
+++ b/tests/projects/c/library_with_cmakelists/xmake.lua
@@ -2,15 +2,15 @@ add_rules("mode.debug", "mode.release")
package("foo")
add_deps("cmake")
- set_sourcedir("foo")
+ set_sourcedir(path.join(os.scriptdir(), "foo"))
on_install(function (package)
local configs = {}
table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
- import("package.tools.cmake").install(package, configs, {buildir = "build"})
+ import("package.tools.cmake").install(package, configs)
end)
on_test(function (package)
- assert(package:has_cincludes("foo.h"))
+ assert(package:has_cfuncs("add", {includes = "foo.h"}))
end)
package_end()
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua
index e6a8335ee..e636cfbf5 100644
--- a/xmake/core/package/package.lua
+++ b/xmake/core/package/package.lua
@@ -531,6 +531,22 @@ function _instance:sourcedir()
return self:get("sourcedir")
end
+-- get the build directory
+function _instance:buildir()
+ local buildir = self._BUILDIR
+ if not buildir then
+ if self:is_local() then
+ local name = self:name():lower():gsub("::", "_")
+ local rootdir = path.join(config.buildir({absolute = true}), ".packages", name:sub(1, 1):lower(), name, self:version_str())
+ buildir = path.join(rootdir, "cache", "build_" .. self:buildhash():sub(1, 8))
+ else
+ buildir = "build_" .. self:buildhash():sub(1, 8)
+ end
+ self._BUILDIR = buildir
+ end
+ return buildir
+end
+
-- get the cached directory of this package
function _instance:cachedir()
local cachedir = self._CACHEDIR
@@ -538,8 +554,11 @@ function _instance:cachedir()
cachedir = self:get("cachedir")
if not cachedir then
local name = self:name():lower():gsub("::", "_")
- local rootdir = self:is_local() and path.join(config.directory(), "cache", "packages") or package.cachedir()
- cachedir = path.join(rootdir, name:sub(1, 1):lower(), name, self:version_str())
+ if self:is_local() then
+ cachedir = path.join(config.buildir({absolute = true}), ".packages", name:sub(1, 1):lower(), name, self:version_str(), "cache")
+ else
+ cachedir = path.join(package.cachedir(), name:sub(1, 1):lower(), name, self:version_str())
+ end
end
self._CACHEDIR = cachedir
end
@@ -553,8 +572,11 @@ function _instance:installdir(...)
installdir = self:get("installdir")
if not installdir then
local name = self:name():lower():gsub("::", "_")
- local rootdir = self:is_local() and path.join(config.directory(), "packages") or package.installdir()
- installdir = path.join(rootdir, name:sub(1, 1):lower(), name)
+ if self:is_local() then
+ installdir = path.join(config.buildir({absolute = true}), ".packages", name:sub(1, 1):lower(), name)
+ else
+ installdir = path.join(package.installdir(), name:sub(1, 1):lower(), name)
+ end
if self:version_str() then
installdir = path.join(installdir, self:version_str())
end
diff --git a/xmake/core/project/config.lua b/xmake/core/project/config.lua
index 31e7d0cb5..16912ab68 100644
--- a/xmake/core/project/config.lua
+++ b/xmake/core/project/config.lua
@@ -90,9 +90,11 @@ function config.options()
end
-- get the buildir
-function config.buildir()
+-- we can use `{absolute = true}` to force to get absolute path
+function config.buildir(opt)
-- get the absolute path first
+ opt = opt or {}
local buildir = config.get("buildir") or "build"
if not path.is_absolute(buildir) then
local rootdir
@@ -106,7 +108,9 @@ function config.buildir()
end
-- adjust path for the current directory
- buildir = path.relative(buildir, os.curdir())
+ if not opt.absolute then
+ buildir = path.relative(buildir, os.curdir())
+ end
return buildir
end
diff --git a/xmake/modules/package/tools/cmake.lua b/xmake/modules/package/tools/cmake.lua
index 048ff5909..1134339d0 100644
--- a/xmake/modules/package/tools/cmake.lua
+++ b/xmake/modules/package/tools/cmake.lua
@@ -665,7 +665,7 @@ function build(package, configs, opt)
opt = opt or {}
-- enter build directory
- local buildir = opt.buildir or "build_" .. hash.uuid4():split('%-')[1]
+ local buildir = opt.buildir or package:buildir()
os.mkdir(path.join(buildir, "install"))
local oldir = os.cd(buildir)
@@ -724,7 +724,7 @@ function install(package, configs, opt)
opt = opt or {}
-- enter build directory
- local buildir = opt.buildir or "build_" .. hash.uuid4():split('%-')[1]
+ local buildir = opt.buildir or package:buildir()
os.mkdir(path.join(buildir, "install"))
local oldir = os.cd(buildir)