summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-03-26 22:37:30 +0800
committerruki <[email protected]>2019-03-26 09:45:05 +0800
commit68c02694b246cd6e11e3d18e8cb4327877144c74 (patch)
tree2b34ed3b4ec823fc2484e6ff48616dadd32971e2
parentb0d272c0ee5450c555c20f465bba7f303a14de95 (diff)
improve to patch packages
-rw-r--r--xmake/actions/require/impl/action/download.lua2
-rw-r--r--xmake/actions/require/impl/action/install.lua4
-rw-r--r--xmake/actions/require/impl/action/patch.lua105
-rw-r--r--xmake/actions/require/impl/package.lua8
-rw-r--r--xmake/core/package/package.lua10
5 files changed, 121 insertions, 8 deletions
diff --git a/xmake/actions/require/impl/action/download.lua b/xmake/actions/require/impl/action/download.lua
index 9eba21afd..0822f0a08 100644
--- a/xmake/actions/require/impl/action/download.lua
+++ b/xmake/actions/require/impl/action/download.lua
@@ -103,7 +103,7 @@ function _download(package, url, sourcedir, url_alias, url_excludes)
cached = false
-- attempt to remove package file first
- os.rm(packagefile)
+ os.tryrm(packagefile)
-- download package file
http.download(url, packagefile)
diff --git a/xmake/actions/require/impl/action/install.lua b/xmake/actions/require/impl/action/install.lua
index 81a456b0d..750bc79bd 100644
--- a/xmake/actions/require/impl/action/install.lua
+++ b/xmake/actions/require/impl/action/install.lua
@@ -27,6 +27,7 @@ import("core.base.option")
import("core.project.target")
import("lib.detect.find_file")
import("test")
+import("patch")
import(".utils.filter")
-- empty chars
@@ -162,6 +163,9 @@ function main(package)
dep:envs_enter()
end
+ -- patch it first
+ patch(package)
+
-- do install
for i = 1, 3 do
local script = scripts[i]
diff --git a/xmake/actions/require/impl/action/patch.lua b/xmake/actions/require/impl/action/patch.lua
new file mode 100644
index 000000000..da75ac521
--- /dev/null
+++ b/xmake/actions/require/impl/action/patch.lua
@@ -0,0 +1,105 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements. See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership. The ASF licenses this file
+-- to you 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 - 2019, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file patch.lua
+--
+
+-- imports
+import("lib.detect.find_tool")
+import("core.base.option")
+import("net.http")
+
+-- download patch file
+function _dowload_patch_file(package, patch_url, patch_hash)
+
+ -- get the patch file
+ local patch_file = path.join(os.tmpdir(), "patches", package:name(), package:version_str(), path.filename(patch_url))
+
+ -- the package file have been downloaded?
+ local cached = true
+ if option.get("force") or not os.isfile(patch_file) or patch_hash ~= hash.sha256(patch_file) then
+
+ -- no cached
+ cached = false
+
+ -- attempt to remove the previous file first
+ os.tryrm(patch_file)
+
+ -- download the patch file
+ if patch_url:find(string.ipattern("https-://")) or patch_url:find(string.ipattern("ftps-://")) then
+ http.download(patch_url, patch_file)
+ else
+ -- copy the patch file
+ if os.isfile(patch_url) then
+ os.cp(patch_url, patch_file)
+ else
+ local scriptdir = package:scriptdir()
+ if scriptdir and os.isfile(path.join(scriptdir, patch_url)) then
+ os.cp(path.join(scriptdir, patch_url), patch_file)
+ else
+ raise("patch(%s): not found!", patch_url)
+ end
+ end
+ end
+
+ -- check hash
+ if patch_hash and patch_hash ~= hash.sha256(patch_file) then
+ raise("patch(%s): unmatched checksum!", patch_url)
+ end
+ end
+ return patch_file
+end
+
+-- apply patch file
+function _apply_patch_file(package, patch, patch_file)
+ os.vrunv(patch.program, {"-p1", "-t", "-i", patch_file})
+end
+
+-- do patch
+function _patch(package, patch, patch_url, patch_hash)
+
+ -- trace
+ vprint("patching %s to %s-%s ..", patch_url, package:name(), package:version_str())
+
+ -- download the patch file
+ local patch_file = _dowload_patch_file(package, patch_url, patch_hash)
+
+ -- apply the patch file
+ _apply_patch_file(package, patch, patch_file)
+end
+
+-- patch the given package
+function main(package)
+
+ -- no patches?
+ local patches = package:patches()
+ if not patches then
+ return
+ end
+
+ -- find patch tool
+ local patch = assert(find_tool("patch"), "patch not found!")
+
+ -- do all patches
+ for _, patchinfo in ipairs(patches) do
+ _patch(package, patch, patchinfo[1], patchinfo[2])
+ end
+end
diff --git a/xmake/actions/require/impl/package.lua b/xmake/actions/require/impl/package.lua
index ec32a97bb..6e2978a32 100644
--- a/xmake/actions/require/impl/package.lua
+++ b/xmake/actions/require/impl/package.lua
@@ -487,16 +487,14 @@ function _patch_packages(packages_install, packages_download)
if patched_package then
local packages = load_packages("patch")
if packages and #packages > 0 then
+ -- install patch package
local package = packages[1]
if not package:fetch() then
-
- -- add this package
packages_download[tostring(package)] = package
table.insert(packages_install, 1, package)
-
- -- add dependences to ensure to be installed first
- patched_package:deps_add(package)
end
+ -- add dependences to ensure to be installed first
+ patched_package:deps_add(package)
end
end
end
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua
index 7a385b801..92e60489d 100644
--- a/xmake/core/package/package.lua
+++ b/xmake/core/package/package.lua
@@ -44,10 +44,11 @@ local sandbox_os = require("sandbox/modules/os")
local sandbox_module = require("sandbox/modules/import/core/sandbox/module")
-- new an instance
-function _instance.new(name, info)
+function _instance.new(name, info, scriptdir)
local instance = table.inherit(_instance)
instance._NAME = name
instance._INFO = info
+ instance._SCRIPTDIR = scriptdir
return instance
end
@@ -241,6 +242,11 @@ function _instance:installdir(...)
return dir
end
+-- get the script directory
+function _instance:scriptdir()
+ return self._SCRIPTDIR
+end
+
-- get the references info of this package
function _instance:references()
local references_file = path.join(self:installdir(), "references.txt")
@@ -1051,7 +1057,7 @@ function package.load_from_repository(packagename, repo, packagedir, packagefile
end
-- new an instance
- local instance, errors = _instance.new(packagename, packageinfo)
+ local instance, errors = _instance.new(packagename, packageinfo, path.directory(scriptpath))
if not instance then
return nil, errors
end