summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-10-16 23:18:53 +0800
committerruki <[email protected]>2021-10-16 23:18:53 +0800
commit194e8abb709553c79d5ee34d9bd4a714aa3004d7 (patch)
tree919729e21b2c434e63d3d8d671fdc83adebf53b5
parentbbe1a5c2f7418d085304d1dcd0462cf4b2193497 (diff)
add headeronly kind for target
-rw-r--r--tests/projects/c/headeronly/src/foo.h9
-rw-r--r--tests/projects/c/headeronly/test.lua6
-rw-r--r--tests/projects/c/headeronly/xmake.lua9
-rw-r--r--xmake/actions/build/build.lua5
-rw-r--r--xmake/actions/install/main.lua2
-rw-r--r--xmake/actions/package/local/main.lua68
-rw-r--r--xmake/actions/package/oldpkg/main.lua25
-rw-r--r--xmake/actions/package/remote/main.lua9
-rw-r--r--xmake/actions/run/main.lua2
-rw-r--r--xmake/core/project/target.lua18
-rw-r--r--xmake/modules/target/action/install/cmake_importfiles.lua8
-rw-r--r--xmake/modules/target/action/install/pkgconfig_importfiles.lua10
-rw-r--r--xmake/modules/target/action/install/unix.lua4
-rw-r--r--xmake/modules/target/action/install/windows.lua4
-rw-r--r--xmake/modules/target/action/uninstall/unix.lua4
-rw-r--r--xmake/modules/target/action/uninstall/windows.lua4
-rw-r--r--xmake/scripts/cmake_importfiles/xxxTargets-headeronly.cmake9
17 files changed, 157 insertions, 39 deletions
diff --git a/tests/projects/c/headeronly/src/foo.h b/tests/projects/c/headeronly/src/foo.h
new file mode 100644
index 000000000..915f44b87
--- /dev/null
+++ b/tests/projects/c/headeronly/src/foo.h
@@ -0,0 +1,9 @@
+/*! calculate add(a, b)
+ *
+ * @param a the first argument
+ * @param b the second argument
+ *
+ * @return the result
+ */
+int add(int a, int b);
+
diff --git a/tests/projects/c/headeronly/test.lua b/tests/projects/c/headeronly/test.lua
new file mode 100644
index 000000000..b76241be2
--- /dev/null
+++ b/tests/projects/c/headeronly/test.lua
@@ -0,0 +1,6 @@
+-- main entry
+function main(t)
+
+ -- build project
+ t:build()
+end
diff --git a/tests/projects/c/headeronly/xmake.lua b/tests/projects/c/headeronly/xmake.lua
new file mode 100644
index 000000000..d43bdceda
--- /dev/null
+++ b/tests/projects/c/headeronly/xmake.lua
@@ -0,0 +1,9 @@
+add_rules("mode.release", "mode.debug")
+
+target("foo")
+ set_kind("headeronly")
+ add_headerfiles("src/foo.h")
+ add_rules("utils.install.cmake_importfiles")
+ add_rules("utils.install.pkgconfig_importfiles")
+
+
diff --git a/xmake/actions/build/build.lua b/xmake/actions/build/build.lua
index 267a802d3..c3817fb0a 100644
--- a/xmake/actions/build/build.lua
+++ b/xmake/actions/build/build.lua
@@ -28,8 +28,7 @@ import("core.base.hashset")
-- clean target for rebuilding
function _clean_target(target)
- local targetkind = target:kind()
- if targetkind ~= "phony" and targetkind ~= "object" then
+ if target:targetfile() then
os.tryrm(target:symbolfile())
os.tryrm(target:targetfile())
end
@@ -54,7 +53,7 @@ function _add_batchjobs_builtin(batchjobs, rootjob, target)
end
-- uses the builtin target script
- if not job and not target:is_phony() then
+ if not job and (target:is_static() or target:is_binary() or target:is_shared() or target:is_object()) then
job, job_leaf = import("kinds." .. target:kind(), {anonymous = true})(batchjobs, rootjob, target)
end
job = job or rootjob
diff --git a/xmake/actions/install/main.lua b/xmake/actions/install/main.lua
index febae713b..82534ffe0 100644
--- a/xmake/actions/install/main.lua
+++ b/xmake/actions/install/main.lua
@@ -46,7 +46,7 @@ function _check_targets(targetname)
-- filter and check targets with builtin-install script
local targetnames = {}
for _, target in ipairs(targets) do
- if not target:is_phony() and target:is_enabled() and not target:script("install") then
+ if target:targetfile() and target:is_enabled() and not target:script("install") then
local targetfile = target:targetfile()
if targetfile and not os.isfile(targetfile) then
table.insert(targetnames, target:name())
diff --git a/xmake/actions/package/local/main.lua b/xmake/actions/package/local/main.lua
index 72cfac3ac..dd7be7de0 100644
--- a/xmake/actions/package/local/main.lua
+++ b/xmake/actions/package/local/main.lua
@@ -174,14 +174,76 @@ function _package_library(target)
print("package(%s): %s generated", packagename, packagedir)
end
+-- package headeronly library
+function _package_headeronly(target)
+
+ -- get the output directory
+ local outputdir = option.get("outputdir") or config.buildir()
+ local packagename = target:name():lower()
+ if #packagename > 1 and bit.band(packagename:byte(2), 0xc0) == 0x80 then
+ wprint("package(%s): cannot generate package, becauese it contains unicode characters!", packagename)
+ return
+ end
+ local packagedir = path.join(outputdir, "packages", packagename:sub(1, 1), packagename)
+ local headerdir = path.join(packagedir, target:plat(), target:arch(), config.mode(), "include")
+
+ -- copy headers
+ local srcheaders, dstheaders = target:headerfiles(headerdir)
+ if srcheaders and dstheaders then
+ local i = 1
+ for _, srcheader in ipairs(srcheaders) do
+ local dstheader = dstheaders[i]
+ if dstheader then
+ os.vcp(srcheader, dstheader)
+ end
+ i = i + 1
+ end
+ end
+
+ -- generate xmake.lua
+ local file = io.open(path.join(packagedir, "xmake.lua"), "w")
+ if file then
+ local deps = _get_linkdeps(target)
+ file:print("package(\"%s\")", packagename)
+ local homepage = option.get("homepage")
+ if homepage then
+ file:print(" set_homepage(\"%s\")", homepage)
+ end
+ local description = option.get("description") or ("The " .. packagename .. " package")
+ file:print(" set_description(\"%s\")", description)
+ if target:license() then
+ file:print(" set_license(\"%s\")", target:license())
+ end
+ if #deps > 0 then
+ file:print(" add_deps(\"%s\")", table.concat(deps, "\", \""))
+ end
+ file:print("")
+ file:print([[
+ on_load(function (package)
+ package:set("installdir", path.join(os.scriptdir(), package:plat(), package:arch(), package:mode()))
+ end)
+
+ on_fetch(function (package)
+ local result = {}
+ result.includedirs = package:installdir("include")
+ return result
+ end)]])
+ file:close()
+ end
+
+ -- show tips
+ print("package(%s): %s generated", packagename, packagedir)
+end
+
-- do package target
function _do_package_target(target)
if not target:is_phony() then
local scripts =
{
- binary = _package_binary
- , static = _package_library
- , shared = _package_library
+ binary = _package_binary
+ , static = _package_library
+ , shared = _package_library
+ , headeronly = _package_headeronly
}
local kind = target:kind()
assert(scripts[kind], "this target(%s) with kind(%s) can not be packaged!", target:name(), kind)
diff --git a/xmake/actions/package/oldpkg/main.lua b/xmake/actions/package/oldpkg/main.lua
index d47d8d474..4dea87c4b 100644
--- a/xmake/actions/package/oldpkg/main.lua
+++ b/xmake/actions/package/oldpkg/main.lua
@@ -35,12 +35,14 @@ function _package_library(target)
local targetname = target:name()
-- copy the library file to the output directory
- os.vcp(target:targetfile(), format("%s/%s.pkg/$(plat)/$(arch)/lib/$(mode)/%s", outputdir, targetname, path.filename(target:targetfile())))
+ if not target:is_headeronly() then
+ os.vcp(target:targetfile(), format("%s/%s.pkg/$(plat)/$(arch)/lib/$(mode)/%s", outputdir, targetname, path.filename(target:targetfile())))
- -- copy the symbol file to the output directory
- local symbolfile = target:symbolfile()
- if os.isfile(symbolfile) then
- os.vcp(symbolfile, format("%s/%s.pkg/$(plat)/$(arch)/lib/$(mode)/%s", outputdir, targetname, path.filename(symbolfile)))
+ -- copy the symbol file to the output directory
+ local symbolfile = target:symbolfile()
+ if os.isfile(symbolfile) then
+ os.vcp(symbolfile, format("%s/%s.pkg/$(plat)/$(arch)/lib/$(mode)/%s", outputdir, targetname, path.filename(symbolfile)))
+ end
end
-- copy *.lib for shared/windows (*.dll) target
@@ -78,8 +80,10 @@ function _package_library(target)
file:print("option(\"%s\")", targetname)
file:print(" set_showmenu(true)")
file:print(" set_category(\"package\")")
- file:print(" add_links(\"%s\")", target:basename())
- file:write(" add_linkdirs(\"$(plat)/$(arch)/lib/$(mode)\")\n")
+ if not target:is_headeronly() then
+ file:print(" add_links(\"%s\")", target:basename())
+ file:write(" add_linkdirs(\"$(plat)/$(arch)/lib/$(mode)\")\n")
+ end
file:write(" add_includedirs(\"$(plat)/$(arch)/include\")\n")
local languages = target:get("languages")
if languages then
@@ -94,9 +98,10 @@ function _do_package_target(target)
if not target:is_phony() then
local scripts =
{
- binary = function (target) end
- , static = _package_library
- , shared = _package_library
+ binary = function (target) end
+ , static = _package_library
+ , shared = _package_library
+ , headeronly = _package_library
}
local kind = target:kind()
assert(scripts[kind], "this target(%s) with kind(%s) can not be packaged!", target:name(), kind)
diff --git a/xmake/actions/package/remote/main.lua b/xmake/actions/package/remote/main.lua
index f1aacf057..9db86e8d5 100644
--- a/xmake/actions/package/remote/main.lua
+++ b/xmake/actions/package/remote/main.lua
@@ -57,6 +57,8 @@ function _package_remote(target)
file:print("package(\"%s\")", packagename)
if target:is_binary() then
file:print(" set_kind(\"binary\")")
+ elseif target:is_headeronly() then
+ file:print(" set_kind(\"library\", {headeronly = true})")
end
local homepage = option.get("homepage")
if homepage then
@@ -102,9 +104,10 @@ function _package_target(target)
if not target:is_phony() then
local scripts =
{
- binary = _package_remote
- , static = _package_remote
- , shared = _package_remote
+ binary = _package_remote
+ , static = _package_remote
+ , shared = _package_remote
+ , headeronly = _package_remote
}
local kind = target:kind()
assert(scripts[kind], "this target(%s) with kind(%s) can not be packaged!", target:name(), kind)
diff --git a/xmake/actions/run/main.lua b/xmake/actions/run/main.lua
index f79a5d6b2..3b51c3c81 100644
--- a/xmake/actions/run/main.lua
+++ b/xmake/actions/run/main.lua
@@ -162,7 +162,7 @@ function _check_targets(targetname)
-- filter and check targets with builtin-run script
local targetnames = {}
for _, target in ipairs(targets) do
- if not target:is_phony() and target:is_enabled() and not target:script("run") then
+ if target:targetfile() and target:is_enabled() and not target:script("run") then
local targetfile = target:targetfile()
if targetfile and not os.isfile(targetfile) then
table.insert(targetnames, target:name())
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index 53d17dadc..d6bba579a 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -676,9 +676,19 @@ function _instance:is_static()
return self:kind() == "static"
end
+-- is object files target?
+function _instance:is_object()
+ return self:kind() == "object"
+end
+
+-- is headeronly target?
+function _instance:is_headeronly()
+ return self:kind() == "headeronly"
+end
+
-- is library target?
function _instance:is_library()
- return self:is_static() or self:is_shared()
+ return self:is_static() or self:is_shared() or self:is_headeronly()
end
-- is default target?
@@ -1011,13 +1021,13 @@ end
-- get the target file name
function _instance:filename()
- -- only compile objects? no target file
- local targetkind = self:kind()
- if targetkind == "object" then
+ -- no target file?
+ if self:is_object() or self:is_phony() or self:is_headeronly() then
return
end
-- make the target file name and attempt to use the format of linker first
+ local targetkind = self:targetkind()
local filename = self:get("filename")
if not filename then
local prefixname = self:get("prefixname")
diff --git a/xmake/modules/target/action/install/cmake_importfiles.lua b/xmake/modules/target/action/install/cmake_importfiles.lua
index e4e5d118e..ea53ab519 100644
--- a/xmake/modules/target/action/install/cmake_importfiles.lua
+++ b/xmake/modules/target/action/install/cmake_importfiles.lua
@@ -40,7 +40,7 @@ end
function _get_builtinvars(target, installdir)
return {TARGETNAME = target:name(),
PROJECTNAME = project.name() or target:name(),
- TARGETFILENAME = _get_libfile(target, installdir),
+ TARGETFILENAME = target:targetfile() and _get_libfile(target, installdir),
TARGETKIND = target:is_shared() and "SHARED" or "STATIC",
PACKAGE_VERSION = target:get("version") or "1.0.0",
TARGET_PTRBYTES = target:is_arch("x86", "i386") and "4" or "8"}
@@ -85,7 +85,7 @@ function _append_cmake_configfile(target, installdir, filename, opt)
local builtinvars = _get_builtinvars(target, installdir)
-- generate the file if not exist / file is outdated
- if not os.isfile(importfile_dst) or os.mtime(importfile_dst) < os.mtime(target:targetfile()) then
+ if target:is_headeronly() or not os.isfile(importfile_dst) or os.mtime(importfile_dst) < os.mtime(target:targetfile()) then
_install_cmake_configfile(target, installdir, filename, opt)
end
@@ -151,7 +151,9 @@ function main(target, opt)
_append_cmake_configfile(target, installdir, "xxxConfig.cmake", opt)
_install_cmake_configfile(target, installdir, "xxxConfigVersion.cmake", opt)
_install_cmake_targetfile(target, installdir, "xxxTargets.cmake", opt)
- if is_mode("debug") then
+ if target:is_headeronly() then
+ _install_cmake_targetfile(target, installdir, "xxxTargets-headeronly.cmake", opt)
+ elseif is_mode("debug") then
_install_cmake_targetfile(target, installdir, "xxxTargets-debug.cmake", opt)
else
_install_cmake_targetfile(target, installdir, "xxxTargets-release.cmake", opt)
diff --git a/xmake/modules/target/action/install/pkgconfig_importfiles.lua b/xmake/modules/target/action/install/pkgconfig_importfiles.lua
index 1187bb5e6..0cad0d267 100644
--- a/xmake/modules/target/action/install/pkgconfig_importfiles.lua
+++ b/xmake/modules/target/action/install/pkgconfig_importfiles.lua
@@ -66,7 +66,9 @@ function main(target, opt)
if file then
file:print("prefix=%s", installdir)
file:print("exec_prefix=${prefix}")
- file:print("libdir=${exec_prefix}/lib")
+ if not target:is_headeronly() then
+ file:print("libdir=${exec_prefix}/lib")
+ end
file:print("includedir=${prefix}/include")
file:print("")
file:print("Name: %s", target:name())
@@ -75,8 +77,10 @@ function main(target, opt)
if version then
file:print("Version: %s", version)
end
- file:print("Libs: %s", libs)
- file:print("Libs.private: ")
+ if not target:is_headeronly() then
+ file:print("Libs: %s", libs)
+ file:print("Libs.private: ")
+ end
file:print("Cflags: %s", cflags)
file:close()
end
diff --git a/xmake/modules/target/action/install/unix.lua b/xmake/modules/target/action/install/unix.lua
index 165d8650e..af641e5bc 100644
--- a/xmake/modules/target/action/install/unix.lua
+++ b/xmake/modules/target/action/install/unix.lua
@@ -127,7 +127,7 @@ function install_static(target, opt)
_install_headers(target, opt)
end
--- install phony
-function install_phony(target, opt)
+-- install headeronly library
+function install_headeronly(target, opt)
_install_headers(target, opt)
end
diff --git a/xmake/modules/target/action/install/windows.lua b/xmake/modules/target/action/install/windows.lua
index 448415fa6..0ea1e22b6 100644
--- a/xmake/modules/target/action/install/windows.lua
+++ b/xmake/modules/target/action/install/windows.lua
@@ -135,7 +135,7 @@ function install_static(target, opt)
_install_headers(target, opt)
end
--- install phony
-function install_phony(target, opt)
+-- install headeronly
+function install_headeronly(target, opt)
_install_headers(target, opt)
end
diff --git a/xmake/modules/target/action/uninstall/unix.lua b/xmake/modules/target/action/uninstall/unix.lua
index 614d59448..e11446dd4 100644
--- a/xmake/modules/target/action/uninstall/unix.lua
+++ b/xmake/modules/target/action/uninstall/unix.lua
@@ -96,7 +96,7 @@ function uninstall_static(target, opt)
_uninstall_headers(target, opt)
end
--- uninstall phony
-function uninstall_phony(target, opt)
+-- uninstall headeronly library
+function uninstall_headeronly(target, opt)
_uninstall_headers(target, opt)
end
diff --git a/xmake/modules/target/action/uninstall/windows.lua b/xmake/modules/target/action/uninstall/windows.lua
index cb9dcf16e..73c43cc0a 100644
--- a/xmake/modules/target/action/uninstall/windows.lua
+++ b/xmake/modules/target/action/uninstall/windows.lua
@@ -101,7 +101,7 @@ function uninstall_static(target, opt)
_uninstall_headers(target, opt)
end
--- uninstall phony
-function uninstall_phony(target, opt)
+-- uninstall headeronly library
+function uninstall_headeronly(target, opt)
_uninstall_headers(target, opt)
end
diff --git a/xmake/scripts/cmake_importfiles/xxxTargets-headeronly.cmake b/xmake/scripts/cmake_importfiles/xxxTargets-headeronly.cmake
new file mode 100644
index 000000000..c35a310a4
--- /dev/null
+++ b/xmake/scripts/cmake_importfiles/xxxTargets-headeronly.cmake
@@ -0,0 +1,9 @@
+#----------------------------------------------------------------
+# Generated CMake target import file for configuration "Headeronly".
+#----------------------------------------------------------------
+
+# Commands may need to know the format version.
+set(CMAKE_IMPORT_FILE_VERSION 1)
+
+# Commands beyond this point should not need to know the version.
+set(CMAKE_IMPORT_FILE_VERSION)