|
| 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 wsw0108 |
| 18 | +-- @file capnp.lua |
| 19 | +-- |
| 20 | + |
| 21 | +-- imports |
| 22 | +import("lib.detect.find_tool") |
| 23 | + |
| 24 | +-- get capnp |
| 25 | +function _get_capnp(target) |
| 26 | + |
| 27 | + -- find capnp |
| 28 | + local capnp = target:data("capnproto.capnp") |
| 29 | + if not capnp then |
| 30 | + capnp = find_tool("capnp") |
| 31 | + if capnp and capnp.program then |
| 32 | + target:data_set("capnproto.capnp", capnp.program) |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + -- get capnp |
| 37 | + return assert(target:data("capnproto.capnp"), "capnp not found!") |
| 38 | +end |
| 39 | + |
| 40 | +-- generate build commands |
| 41 | +function buildcmd(target, batchcmds, sourcefile_capnp, opt) |
| 42 | + |
| 43 | + -- get capnp |
| 44 | + local capnp = _get_capnp(target) |
| 45 | + |
| 46 | + -- get c/c++ source file for capnproto |
| 47 | + local prefixdir |
| 48 | + local public |
| 49 | + local fileconfig = target:fileconfig(sourcefile_capnp) |
| 50 | + if fileconfig then |
| 51 | + public = fileconfig.capnp_public |
| 52 | + prefixdir = fileconfig.capnp_rootdir |
| 53 | + end |
| 54 | + local rootdir = path.join(target:autogendir(), "rules", "capnproto") |
| 55 | + local filename = path.basename(sourcefile_capnp) .. ".capnp.c++" |
| 56 | + local sourcefile_cx = target:autogenfile(sourcefile_capnp, {rootdir = rootdir, filename = filename}) |
| 57 | + local sourcefile_dir = prefixdir and path.join(rootdir, prefixdir) or path.directory(sourcefile_cx) |
| 58 | + |
| 59 | + -- add includedirs |
| 60 | + target:add("includedirs", sourcefile_dir, {public = public}) |
| 61 | + |
| 62 | + -- add objectfile |
| 63 | + local objectfile = target:objectfile(sourcefile_cx) |
| 64 | + table.insert(target:objectfiles(), objectfile) |
| 65 | + |
| 66 | + -- add commands |
| 67 | + batchcmds:mkdir(sourcefile_dir) |
| 68 | + batchcmds:show_progress(opt.progress, "${color.build.object}compiling.capnp %s", sourcefile_capnp) |
| 69 | + local capnproto = target:pkg("capnproto") |
| 70 | + local includes = capnproto:get("sysincludedirs") |
| 71 | + local argv = {"compile"} |
| 72 | + for _, value in ipairs(includes) do |
| 73 | + table.insert(argv, "-I" .. value) |
| 74 | + end |
| 75 | + table.insert(argv, "-I" .. (prefixdir and prefixdir or path.directory(sourcefile_capnp))) |
| 76 | + if prefixdir then |
| 77 | + table.insert(argv, "--src-prefix=" .. prefixdir) |
| 78 | + end |
| 79 | + table.insert(argv, "-o") |
| 80 | + table.insert(argv, "c++:" .. sourcefile_dir) |
| 81 | + table.insert(argv, sourcefile_capnp) |
| 82 | + batchcmds:vrunv(capnp, argv) |
| 83 | + local configs = {includedirs = sourcefile_dir, languages = "c++14"} |
| 84 | + if package:is_plat("windows") then |
| 85 | + configs.cxflags = "/TP" |
| 86 | + end |
| 87 | + batchcmds:compile(sourcefile_cx, objectfile, {sourcekind = "cxx", configs = configs}) |
| 88 | + |
| 89 | + -- add deps |
| 90 | + batchcmds:add_depfiles(sourcefile_capnp) |
| 91 | + batchcmds:set_depmtime(os.mtime(objectfile)) |
| 92 | + batchcmds:set_depcache(target:dependfile(objectfile)) |
| 93 | +end |
0 commit comments