|
| 1 | +--!A cross-platform build utility based on Lua |
| 2 | +-- |
| 3 | +-- Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +-- you may not use this file except in compliance with the License. |
| 5 | +-- You may obtain a copy of the License at |
| 6 | +-- |
| 7 | +-- http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +-- |
| 9 | +-- Unless required by applicable law or agreed to in writing, software |
| 10 | +-- distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +-- See the License for the specific language governing permissions and |
| 13 | +-- limitations under the License. |
| 14 | +-- |
| 15 | +-- Copyright (C) 2015-present, TBOOX Open Source Group. |
| 16 | +-- |
| 17 | +-- @author ruki |
| 18 | +-- @file cl6x.lua |
| 19 | +-- |
| 20 | + |
| 21 | +-- imports |
| 22 | +import("core.base.option") |
| 23 | +import("core.base.global") |
| 24 | +import("core.project.policy") |
| 25 | +import("core.language.language") |
| 26 | +import("utils.progress") |
| 27 | + |
| 28 | +-- init it |
| 29 | +function init(self) |
| 30 | +end |
| 31 | + |
| 32 | +-- make the symbol flag |
| 33 | +function nf_symbol(self, level) |
| 34 | + -- only for source kind |
| 35 | + local kind = self:kind() |
| 36 | + if language.sourcekinds()[kind] then |
| 37 | + local maps = _g.symbol_maps |
| 38 | + if not maps then |
| 39 | + maps = |
| 40 | + { |
| 41 | + debug = "-g" |
| 42 | + } |
| 43 | + _g.symbol_maps = maps |
| 44 | + end |
| 45 | + return maps[level .. '_' .. kind] or maps[level] |
| 46 | + end |
| 47 | +end |
| 48 | + |
| 49 | +-- make the optimize flag |
| 50 | +function nf_optimize(self, level) |
| 51 | + local maps = |
| 52 | + { |
| 53 | + none = "-O0" |
| 54 | + , fast = "-O1" |
| 55 | + , faster = "-O2" |
| 56 | + , fastest = "-O3" |
| 57 | + , smallest = "-m3" |
| 58 | + , aggressive = "-O3" |
| 59 | + } |
| 60 | + return maps[level] |
| 61 | +end |
| 62 | + |
| 63 | +-- make the define flag |
| 64 | +function nf_define(self, macro) |
| 65 | + return "-D" .. macro |
| 66 | +end |
| 67 | + |
| 68 | +-- make the undefine flag |
| 69 | +function nf_undefine(self, macro) |
| 70 | + return "-U" .. macro |
| 71 | +end |
| 72 | + |
| 73 | +-- make the includedir flag |
| 74 | +function nf_includedir(self, dir) |
| 75 | + return {"-I" .. dir} |
| 76 | +end |
| 77 | + |
| 78 | +-- make the sysincludedir flag |
| 79 | +function nf_sysincludedir(self, dir) |
| 80 | + return nf_includedir(self, dir) |
| 81 | +end |
| 82 | + |
| 83 | +-- make the link flag |
| 84 | +function nf_link(self, lib) |
| 85 | + if not lib:endswith(".a") and not lib:endswith(".so") then |
| 86 | + lib = "lib" .. lib .. ".a" |
| 87 | + end |
| 88 | + return "-l" .. lib |
| 89 | +end |
| 90 | + |
| 91 | +-- make the syslink flag |
| 92 | +function nf_syslink(self, lib) |
| 93 | + return nf_link(self, lib) |
| 94 | +end |
| 95 | + |
| 96 | +-- make the linkdir flag |
| 97 | +function nf_linkdir(self, dir) |
| 98 | + return {"-i" .. path.translate(dir)} |
| 99 | +end |
| 100 | + |
| 101 | +-- make the rpathdir flag |
| 102 | +function nf_rpathdir(self, dir, opt) |
| 103 | + opt = opt or {} |
| 104 | + local extra = opt.extra |
| 105 | + if extra and extra.installonly then |
| 106 | + return |
| 107 | + end |
| 108 | + dir = path.translate(dir) |
| 109 | + return {"-rpath=" .. dir} |
| 110 | +end |
| 111 | + |
| 112 | +-- make the link arguments list |
| 113 | +function linkargv(self, objectfiles, targetkind, targetfile, flags, opt) |
| 114 | + local argv = table.join("-z", "--output_file=" .. targetfile, objectfiles, flags) |
| 115 | + return self:program(), argv |
| 116 | +end |
| 117 | + |
| 118 | +-- link the target file |
| 119 | +-- |
| 120 | +-- maybe we need to use os.vrunv() to show link output when enable verbose information |
| 121 | +-- @see https://github.com/xmake-io/xmake/discussions/2916 |
| 122 | +-- |
| 123 | +function link(self, objectfiles, targetkind, targetfile, flags, opt) |
| 124 | + opt = opt or {} |
| 125 | + os.mkdir(path.directory(targetfile)) |
| 126 | + local program, argv = linkargv(self, objectfiles, targetkind, targetfile, flags) |
| 127 | + if option.get("verbose") then |
| 128 | + os.execv(program, argv, {envs = self:runenvs(), shell = opt.shell}) |
| 129 | + else |
| 130 | + os.vrunv(program, argv, {envs = self:runenvs(), shell = opt.shell}) |
| 131 | + end |
| 132 | +end |
| 133 | + |
| 134 | +-- make the compile arguments list |
| 135 | +function compargv(self, sourcefile, objectfile, flags) |
| 136 | + return self:program(), table.join("-c", "--preproc_with_compile", flags, "--output_file=" .. objectfile, sourcefile) |
| 137 | +end |
| 138 | + |
| 139 | +-- compile the source file |
| 140 | +function compile(self, sourcefile, objectfile, dependinfo, flags, opt) |
| 141 | + os.mkdir(path.directory(objectfile)) |
| 142 | + local depfile = dependinfo and os.tmpfile() or nil |
| 143 | + try |
| 144 | + { |
| 145 | + function () |
| 146 | + local compflags = flags |
| 147 | + if depfile then |
| 148 | + compflags = table.join(compflags, "-ppd=" .. depfile) |
| 149 | + end |
| 150 | + local outdata, errdata = os.iorunv(compargv(self, sourcefile, objectfile, compflags)) |
| 151 | + return (outdata or "") .. (errdata or "") |
| 152 | + end, |
| 153 | + catch |
| 154 | + { |
| 155 | + function (errors) |
| 156 | + |
| 157 | + -- try removing the old object file for forcing to rebuild this source file |
| 158 | + os.tryrm(objectfile) |
| 159 | + |
| 160 | + -- find the start line of error |
| 161 | + local lines = tostring(errors):split("\n") |
| 162 | + local start = 0 |
| 163 | + for index, line in ipairs(lines) do |
| 164 | + if line:find("error:", 1, true) or line:find("错误:", 1, true) then |
| 165 | + start = index |
| 166 | + break |
| 167 | + end |
| 168 | + end |
| 169 | + |
| 170 | + -- get 16 lines of errors |
| 171 | + if start > 0 or not option.get("verbose") then |
| 172 | + if start == 0 then start = 1 end |
| 173 | + errors = table.concat(table.slice(lines, start, start + ((#lines - start > 16) and 16 or (#lines - start))), "\n") |
| 174 | + end |
| 175 | + |
| 176 | + -- raise compiling errors |
| 177 | + raise(errors) |
| 178 | + end |
| 179 | + }, |
| 180 | + finally |
| 181 | + { |
| 182 | + function (ok, warnings) |
| 183 | + |
| 184 | + -- print some warnings |
| 185 | + if warnings and #warnings > 0 and policy.build_warnings(opt) then |
| 186 | + if progress.showing_without_scroll() then |
| 187 | + print("") |
| 188 | + end |
| 189 | + cprint("${color.warning}%s", table.concat(table.slice(warnings:split('\n'), 1, 8), '\n')) |
| 190 | + end |
| 191 | + |
| 192 | + -- generate the dependent includes |
| 193 | + if depfile and os.isfile(depfile) then |
| 194 | + if dependinfo then |
| 195 | + dependinfo.depfiles_cl6x = io.readfile(depfile, {continuation = "\\"}) |
| 196 | + end |
| 197 | + |
| 198 | + -- remove the temporary dependent file |
| 199 | + os.tryrm(depfile) |
| 200 | + end |
| 201 | + end |
| 202 | + } |
| 203 | + } |
| 204 | +end |
| 205 | + |
| 206 | + |
0 commit comments