summaryrefslogtreecommitdiff
path: root/xmake/modules/target/action/install/cmake_importfiles.lua
blob: 4fc38a5aa631b27038a509e4502590ec6a19490c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
--!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-present, Xmake Open Source Community.
--
-- @author      ruki
-- @file        cmake_importfiles.lua
--

-- imports
import("core.project.project")

-- get install libdir
function _get_install_libdir(target, installdir, opt)
    opt = opt or {}
    return path.normalize(opt.libdir and path.join(installdir, opt.libdir) or target:libdir())
end

-- get the lib file of the target
function _get_libfilename(target, libdir)
    local libfile = path.filename(target:targetfile())
    if target:is_plat("windows") then
        libfile = libfile:gsub("%.dll$", ".lib")
    elseif target:is_plat("mingw") then
        if os.isfile(path.join(libdir, libfile:gsub("%.dll$", ".dll.a"))) then
            libfile = libfile:gsub("%.dll$", ".dll.a")
        else
            libfile = libfile:gsub("%.dll$", ".lib")
        end
    end
    return libfile
end

-- get the builtin variables
function _get_builtinvars(target, installdir, libdir)
    local target_ptrbytes
    if target:is_plat("cross") then
        target_ptrbytes = target:check_sizeof("void*")
    else
        target_ptrbytes = target:is_arch64() and "8" or "4"
    end
    local libsubdir
    if libdir:startswith(installdir) then
        libsubdir = path.relative(libdir, installdir)
    else
        raise("target(%s): libdir(%s) is not in installdir(%s)", target:name(), libdir, installdir)
    end
    return {LIBDIR          = libsubdir,
            TARGETNAME      = target:name(),
            PROJECTNAME     = project.name() or target:name(),
            TARGETFILENAME  = target:targetfile() and _get_libfilename(target, libdir),
            TARGETKIND      = target:is_headeronly() and "INTERFACE" or (target:is_shared() and "SHARED" or "STATIC"),
            PACKAGE_VERSION = target:get("version") or "1.0.0",
            TARGET_PTRBYTES = target_ptrbytes}
end

-- install cmake config file
function _install_cmake_configfile(target, installdir, filename, opt)

    -- get import file path
    local libdir = _get_install_libdir(target, installdir, opt)
    local projectname = project.name() or target:name()
    local importfile_src = path.join(os.programdir(), "scripts", "cmake_importfiles", filename)
    local importfile_dst = path.join(libdir, "cmake", projectname, (filename:gsub("xxx", projectname)))

    -- trace
    vprint("generating %s ..", importfile_dst)

    -- get the builtin variables
    local builtinvars = _get_builtinvars(target, installdir, libdir)

    -- copy and replace builtin variables
    local content = io.readfile(importfile_src)
    if content then
        content = content:split("#######################+#")[1]
        content = content:gsub("(@(.-)@)", function(_, variable)
            variable = variable:trim()
            local value = builtinvars[variable]
            return type(value) == "function" and value() or value
        end)
        io.writefile(importfile_dst, content)
    end
end

-- append target to cmake config file
function _append_cmake_configfile(target, installdir, filename, opt)

    -- get import file path
    local libdir = _get_install_libdir(target, installdir, opt)
    local projectname = project.name() or target:name()
    local importfile_src = path.join(os.programdir(), "scripts", "cmake_importfiles", filename)
    local importfile_dst = path.join(libdir, "cmake", projectname, (filename:gsub("xxx", projectname)))

    -- get the builtin variables
    local builtinvars = _get_builtinvars(target, installdir, libdir)

    -- generate the file if not exist / file is outdated
    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

    -- copy and replace builtin variables
    local content = io.readfile(importfile_src)
    local dst_content = io.readfile(importfile_dst)
    if content then
        content = content:split("#######################+#")[2]
        content = content:gsub("(@(.-)@)", function(_, variable)
            variable = variable:trim()
            local value = builtinvars[variable]
            return type(value) == "function" and value() or value
        end)
        content = content:trim()

        -- check if the target already exists
        if not dst_content:match(format("%sTargets.cmake", target:name())) then
            io.writefile(importfile_dst, dst_content:trim() .. "\n\n" .. content .. "\n")
        end
    end
end

-- install cmake target file
function _install_cmake_targetfile(target, installdir, filename, opt)

    -- get import file path
    local libdir = _get_install_libdir(target, installdir, opt)
    local projectname = project.name() or target:name()
    local importfile_src = path.join(os.programdir(), "scripts", "cmake_importfiles", filename)
    local importfile_dst = path.join(libdir, "cmake", projectname, (filename:gsub("xxx", target:name())))

    -- trace
    vprint("generating %s ..", importfile_dst)

    -- get the builtin variables
    local builtinvars = _get_builtinvars(target, installdir, libdir)

    -- copy and replace builtin variables
    local content = io.readfile(importfile_src)
    if content then
        content = content:gsub("(@(.-)@)", function(_, variable)
            variable = variable:trim()
            local value = builtinvars[variable]
            return type(value) == "function" and value() or value
        end)
        local libfilename = path.filename(target:targetfile())
        local postfix = is_mode("debug") and "DEBUG" or "RELEASE"
        if target:is_shared() and (_get_libfilename(target, libdir) ~= libfilename) then
            -- On DLL platforms, the import library is named differently from the target file
            content = content:gsub("# IMPORTED_IMPLIB_" .. postfix, "IMPORTED_IMPLIB_" .. postfix)
            content = content:gsub(
                "IMPORTED_LOCATION_" .. postfix .. " \"%${_IMPORT_PREFIX}/lib/.-\"",
                "IMPORTED_LOCATION_" .. postfix .. " \"${_IMPORT_PREFIX}/bin/" .. libfilename .. "\""
            )
        end
        io.writefile(importfile_dst, content)
    end
end

-- install .cmake import files for the target
--
-- @param target    the target instance
-- @param opt       the options, e.g. {installdir = "", libdir = ""}
--
function main(target, opt)

    -- check
    opt = opt or {}
    assert(target:is_library(), 'cmake_importfiles: only support for library target(%s)!', target:name())

    -- get install directory
    local installdir = target:installdir()
    if not installdir then
        return
    end

    -- do install
    installdir = path.normalize(installdir)
    _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 not target:is_headeronly() then
        if is_mode("debug") then
            _install_cmake_targetfile(target, installdir, "xxxTargets-debug.cmake", opt)
        else
            _install_cmake_targetfile(target, installdir, "xxxTargets-release.cmake", opt)
        end
    end
end