Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows installdir, bindir and libdir to be configured via opt arguments #5634

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions xmake/modules/target/action/install/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ end
-- install shared libraries
function _install_shared_libraries(target, opt)
local bindir = target:is_plat("windows", "mingw") and target:bindir() or target:libdir()
if target:is_plat("windows", "mingw") and opt.bindir then
bindir = target:installdir(opt.bindir)
elseif opt.libdir then
bindir = target:installdir(opt.libdir)
end

-- get all dependent shared libraries
local libfiles = {}
Expand Down Expand Up @@ -158,7 +163,7 @@ function _update_install_rpath(target, opt)
if target:is_plat("windows", "mingw") then
return
end
local bindir = target:bindir()
local bindir = opt.bindir and target:installdir(opt.bindir) or target:bindir()
local targetfile = path.join(bindir, target:filename())
if target:policy("install.rpath") then
local result, sources = target:get_from("rpathdirs", "*")
Expand All @@ -179,7 +184,7 @@ end

-- install binary
function _install_binary(target, opt)
local bindir = target:bindir()
local bindir = opt.bindir and target:installdir(opt.bindir) or target:bindir()
os.mkdir(bindir)
os.vcp(target:targetfile(), bindir)
os.trycp(target:symbolfile(), path.join(bindir, path.filename(target:symbolfile())))
Expand All @@ -190,14 +195,19 @@ end
-- install shared library
function _install_shared(target, opt)
local bindir = target:is_plat("windows", "mingw") and target:bindir() or target:libdir()
if target:is_plat("windows", "mingw") and opt.bindir then
bindir = target:installdir(opt.bindir)
elseif opt.libdir then
bindir = target:installdir(opt.libdir)
end
os.mkdir(bindir)
local targetfile = target:targetfile()

if target:is_plat("windows", "mingw") then
-- install *.lib for shared/windows (*.dll) target
-- @see https://github.com/xmake-io/xmake/issues/714
os.vcp(target:targetfile(), bindir)
local libdir = target:libdir()
local libdir = opt.libdir and target:installdir(opt.libdir) or target:libdir()
local targetfile_lib = path.join(path.directory(targetfile), path.basename(targetfile) .. (target:is_plat("mingw") and ".dll.a" or ".lib"))
if os.isfile(targetfile_lib) then
os.mkdir(libdir)
Expand All @@ -215,7 +225,7 @@ end

-- install static library
function _install_static(target, opt)
local libdir = target:libdir()
local libdir = opt.libdir and target:installdir(opt.libdir) or target:libdir()
os.mkdir(libdir)
os.vcp(target:targetfile(), libdir)
os.trycp(target:symbolfile(), path.join(libdir, path.filename(target:symbolfile())))
Expand All @@ -233,7 +243,8 @@ function _install_moduleonly(target, opt)
end

function main(target, opt)
local installdir = target:installdir()
opt = opt or {}
local installdir = opt.installdir and target:set("installdir", opt.installdir) or target:installdir()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not call target:set to modify target installdir configuration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if I could save the installdir of a target, then set it to the old value at the end of the function.
The other solution that comes to mind is to add an opt argument to installdir, libdir and bindir of target.

Which solution do you prefer?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use local installdir = opt.installdir and opt.installdir or target:installdir()?

Copy link
Contributor Author

@A2va A2va Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because when target:installdir(opt.bindir) is called, installdir is not set in the target (or not the right value). One solution would be to join installdir and bindir each time, but as this is already what bindir does, I haven't considered this option.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can add function _get_installdir(target, opt) to wrap it. then get them each time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like this

function _get_target_bindir(package, target)
local bindir = package:bindir()
local prefixdir = target:prefixdir()
if prefixdir then
bindir = path.join(package:installdir(), prefixdir, target:extraconf("prefixdir", prefixdir, "bindir") or "bin")
end
return path.normalize(bindir)
end
function _get_target_libdir(package, target)
local libdir = package:libdir()
local prefixdir = target:prefixdir()
if prefixdir then
libdir = path.join(package:installdir(), prefixdir, target:extraconf("prefixdir", prefixdir, "libdir") or "lib")
end
return path.normalize(libdir)
end
function _get_target_includedir(package, target)
local includedir = package:includedir()
local prefixdir = target:prefixdir()
if prefixdir then
includedir = path.join(package:installdir(), prefixdir, target:extraconf("prefixdir", prefixdir, "includedir") or "include")
end
return path.normalize(includedir)
end
function _get_target_installdir(package, target)
local installdir = package:installdir()
local prefixdir = target:prefixdir()
if prefixdir then
installdir = path.join(package:installdir(), prefixdir)
end
return path.normalize(installdir)
end

if not installdir then
wprint("please use `xmake install -o installdir` or `set_installdir` to set install directory.")
return
Expand Down
20 changes: 15 additions & 5 deletions xmake/modules/target/action/uninstall/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function _get_target_package_libfiles(target, opt)
return {}
end
local libfiles = {}
local bindir = target:is_plat("windows", "mingw") and target:bindir() or target:libdir()
for _, pkg in ipairs(target:orderpkgs(opt)) do
if pkg:enabled() and pkg:get("libfiles") then
for _, libfile in ipairs(table.wrap(pkg:get("libfiles"))) do
Expand Down Expand Up @@ -115,6 +114,11 @@ end
-- uninstall shared libraries
function _uninstall_shared_libraries(target, opt)
local bindir = target:is_plat("windows", "mingw") and target:bindir() or target:libdir()
if target:is_plat("windows", "mingw") and opt.bindir then
bindir = target:installdir(opt.bindir)
elseif opt.libdir then
bindir = target:installdir(opt.libdir)
end

-- get all dependent shared libraries
local libfiles = {}
Expand Down Expand Up @@ -142,7 +146,7 @@ end

-- uninstall binary
function _uninstall_binary(target, opt)
local bindir = target:bindir()
local bindir = opt.bindir and target:installdir(opt.bindir) or target:bindir()
remove_files(path.join(bindir, path.filename(target:targetfile())), {emptydir = true})
remove_files(path.join(bindir, path.filename(target:symbolfile())), {emptydir = true})
_uninstall_shared_libraries(target, opt)
Expand All @@ -151,11 +155,16 @@ end
-- uninstall shared library
function _uninstall_shared(target, opt)
local bindir = target:is_plat("windows", "mingw") and target:bindir() or target:libdir()
if target:is_plat("windows", "mingw") and opt.bindir then
bindir = target:installdir(opt.bindir)
elseif opt.libdir then
bindir = target:installdir(opt.libdir)
end

if target:is_plat("windows", "mingw") then
-- uninstall *.lib for shared/windows (*.dll) target
-- @see https://github.com/xmake-io/xmake/issues/714
local libdir = target:libdir()
local libdir = opt.libdir and target:installdir(opt.libdir) or target:libdir()
local targetfile = target:targetfile()
remove_files(path.join(bindir, path.filename(targetfile)), {emptydir = true})
remove_files(path.join(libdir, path.basename(targetfile) .. (target:is_plat("mingw") and ".dll.a" or ".lib")), {emptydir = true})
Expand All @@ -171,7 +180,7 @@ end

-- uninstall static library
function _uninstall_static(target, opt)
local libdir = target:libdir()
local libdir = opt.libdir and target:installdir(opt.libdir) or target:libdir()
remove_files(path.join(libdir, path.filename(target:targetfile())), {emptydir = true})
remove_files(path.join(libdir, path.filename(target:symbolfile())), {emptydir = true})
_uninstall_headers(target, opt)
Expand All @@ -188,7 +197,8 @@ function _uninstall_moduleonly(target, opt)
end

function main(target, opt)
local installdir = target:installdir()
opt = opt or {}
local installdir = opt.installdir and target:set("installdir", opt.installdir) or target:installdir()
if not installdir then
wprint("please use `xmake install -o installdir` or `set_installdir` to set install directory.")
return
Expand Down
Loading