summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-11-08 19:18:52 +0800
committerruki <[email protected]>2020-11-08 19:18:52 +0800
commitda3c6af9d6584593311313c2ced57c1a39fff5ae (patch)
tree9226e6b74b8a7e7f1a68804e3c29d9f6b8f36cff
parentea6afc015b3c3f391761ba13a03c966daedd6db4 (diff)
add license checking
-rw-r--r--xmake/actions/require/install.lua4
-rw-r--r--xmake/core/project/policy.lua2
-rw-r--r--xmake/core/project/requireinfo.lua10
-rw-r--r--xmake/core/project/target.lua5
-rw-r--r--xmake/modules/core/base/license.lua75
-rw-r--r--xmake/modules/package/manager/xmake/find_package.lua6
-rw-r--r--xmake/rules/utils/check_licenses/check_licenses.lua29
7 files changed, 123 insertions, 8 deletions
diff --git a/xmake/actions/require/install.lua b/xmake/actions/require/install.lua
index 75da17097..607f91fbc 100644
--- a/xmake/actions/require/install.lua
+++ b/xmake/actions/require/install.lua
@@ -63,8 +63,10 @@ function _register_required_package_libs(instance, requireinfo, is_deps)
--
fetchinfo.license = nil
- -- we need only root package version
+ -- we need only some infos for root package
fetchinfo.version = nil
+ fetchinfo.static = nil
+ fetchinfo.shared = nil
end
requireinfo:add(fetchinfo)
end
diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua
index bf2899d69..004bee33e 100644
--- a/xmake/core/project/policy.lua
+++ b/xmake/core/project/policy.lua
@@ -39,6 +39,8 @@ function policy.policies()
["check.auto_ignore_flags"] = {description = "Enable check and ignore unsupported flags automatically.", default = true, type = "boolean"},
-- we will map gcc flags to the current compiler and linker by default.
["check.auto_map_flags"] = {description = "Enable map gcc flags to the current compiler and linker automatically.", default = true, type = "boolean"},
+ -- we will check the compatibility of target and package licenses
+ ["check.target_package_licenses"] = {description = "Enable check the compatibility of target and package licenses.", default = true, type = "boolean"},
-- we can compile the source files for each target in parallel
["build.across_targets_in_parallel"] = {description = "Enable compile the source files for each target in parallel.", default = true, type = "boolean"}
}
diff --git a/xmake/core/project/requireinfo.lua b/xmake/core/project/requireinfo.lua
index 2ae0a8650..ad8304aab 100644
--- a/xmake/core/project/requireinfo.lua
+++ b/xmake/core/project/requireinfo.lua
@@ -122,6 +122,16 @@ function requireinfo:license()
return self:get("license")
end
+-- has static libraries?
+function requireinfo:has_static()
+ return self:get("static")
+end
+
+-- has shared libraries?
+function requireinfo:has_shared()
+ return self:get("shared")
+end
+
-- get the require string
function requireinfo:requirestr()
return self:get("__requirestr")
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index 4f7c58dc1..79e42df34 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -514,6 +514,11 @@ function _instance:version()
return version, version_build
end
+-- get the target license
+function _instance:license()
+ return self:get("license")
+end
+
-- get the target policy
function _instance:policy(name)
local policies = self._POLICIES
diff --git a/xmake/modules/core/base/license.lua b/xmake/modules/core/base/license.lua
new file mode 100644
index 000000000..ca1c80eef
--- /dev/null
+++ b/xmake/modules/core/base/license.lua
@@ -0,0 +1,75 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-2020, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file license.lua
+--
+
+-- imports
+import("core.base.object")
+import("core.base.hashset")
+
+-- get licenses
+function _licenses()
+ local licenses = _g.licenses
+ if not licenses then
+ licenses = hashset.from({"Apache-2.0",
+ "MIT",
+ "Public Domain",
+ "LGPL-2.0", "LGPL-2.1", "LGPL-3.0",
+ "GPL-2.0", "GPL-3.0", "GPL-3.0",
+ "BSD-2-Clause", "BSD-3-Clause"})
+ _g.licenses = licenses
+ end
+ return licenses
+end
+
+-- get all licenses list
+function list()
+ return _licenses():to_array()
+end
+
+-- normalize license
+function normalize(license)
+ -- TODO parse and convert license strings in other formats
+ return license
+end
+
+-- check if the license is compatible
+-- @see https://github.com/xmake-io/xmake/issues/1016
+--
+function compatible(target_license, library_license, opt)
+ opt = opt or {}
+ library_license = normalize(library_license)
+ if library_license then
+ target_license = normalize(target_license)
+ if library_license:startswith("GPL-") then
+ return target_license and target_license:startswith("GPL-")
+ elseif library_license:startswith("LGPL-") then
+ if target_license and target_license:startswith("LGPL-") then
+ return true
+ elseif opt.library_kind and opt.library_kind == "shared" then
+ -- we can only use shared library with LGPL-x
+ return true
+ else
+ return false, string.format("we can use shared libraries with %s or use set_license()/set_policy() to modify/disable license", library_license)
+ end
+ else
+ -- TODO maybe we need handle more licenses
+ end
+ end
+ return true
+end
diff --git a/xmake/modules/package/manager/xmake/find_package.lua b/xmake/modules/package/manager/xmake/find_package.lua
index 4508ecde9..c10be64ac 100644
--- a/xmake/modules/package/manager/xmake/find_package.lua
+++ b/xmake/modules/package/manager/xmake/find_package.lua
@@ -128,6 +128,12 @@ function _find_package_from_repo(name, opt)
for _, link in ipairs(links) do
local libinfo = find_library(link, linkdirs)
if libinfo then
+ if libinfo.kind == "shared" then
+ result.shared = true
+ end
+ if libinfo.kind == "static" then
+ result.static = true
+ end
result.links = table.join(result.links or {}, libinfo.link)
result.linkdirs = table.join(result.linkdirs or {}, libinfo.linkdir)
result.libfiles = table.join(result.libfiles or {}, path.join(libinfo.linkdir, libinfo.filename))
diff --git a/xmake/rules/utils/check_licenses/check_licenses.lua b/xmake/rules/utils/check_licenses/check_licenses.lua
index 2d356f61a..9e5cf504f 100644
--- a/xmake/rules/utils/check_licenses/check_licenses.lua
+++ b/xmake/rules/utils/check_licenses/check_licenses.lua
@@ -18,9 +18,23 @@
-- @file check_licenses.lua
--
+-- imports
+import("core.base.license")
+
-- check licenses
-function _check_licenses_for_package(target_name, target_license, package_name, package_licenses)
- -- TODO
+function _check_licenses_for_package(target, package)
+ local target_license = target:license()
+ local package_license = package:license()
+ local package_kind = package:has_shared() and "shared"
+ local ok, errors = license.compatible(target_license, package_license, {library_kind = package_kind})
+ if not ok then
+ errors = errors or "you can use set_license()/set_policy() to modify/disable license"
+ if target_license then
+ wprint("license(%s) of target(%s) is not compatible with license(%s) of package(%s)\n%s!", target_license, target:name(), package_license, package:name(), errors)
+ else
+ wprint("target(%s) maybe is not compatible with license(%s) of package(%s), \n%s!", target:name(), package_license, package:name(), errors)
+ end
+ end
end
-- check licenses for all dependent packages
@@ -28,15 +42,16 @@ end
-- @see https://github.com/xmake-io/xmake/issues/1016
--
function _check_licenses_for_packages(target)
- local target_license = target:get("license")
- if target_license then
- for _, pkg in ipairs(target:orderpkgs()) do
- _check_licenses_for_package(target:name(), target_license, pkg:name(), pkg:get("license"))
+ for _, pkg in ipairs(target:orderpkgs()) do
+ if pkg:license() then
+ _check_licenses_for_package(target, pkg)
end
end
end
-- main entry
function main(target)
- _check_licenses_for_packages(target)
+ if target:policy("check.target_package_licenses") then
+ _check_licenses_for_packages(target)
+ end
end