<commit_before>--!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 - 2018, TBOOX Open Source Group.
--
-- @author      ruki
-- @file        builder.lua
--

-- define module
local builder = builder or {}

-- load modules
local io       = require("base/io")
local path     = require("base/path")
local utils    = require("base/utils")
local table    = require("base/table")
local string   = require("base/string")
local option   = require("base/option")
local tool     = require("tool/tool")
local config   = require("project/config")
local sandbox  = require("sandbox/sandbox")
local language = require("language/language")
local platform = require("platform/platform")

-- get the tool of builder
function builder:_tool()
    return self._TOOL
end

-- get the name flags
function builder:_nameflags()
    return self._NAMEFLAGS
end

-- get the target kind
function builder:_targetkind()
    return self._TARGETKIND
end

-- map gcc flag to the given builder flag
function builder:_mapflag(flag, mapflags)

    -- attempt to map it directly
    local flag_mapped = mapflags[flag]
    if flag_mapped then
        return flag_mapped
    end

    -- find and replace it using pattern
    for k, v in pairs(mapflags) do
        local flag_mapped, count = flag:gsub("^" .. k .. "$", function (w) return v end)
        if flag_mapped and count ~= 0 then
            return utils.ifelse(#flag_mapped ~= 0, flag_mapped, nil) 
        end
    end

    -- has this flag?
    if self:has_flags(flag) then
        return flag
    end
end

-- map gcc flags to the given builder flags
function builder:_mapflags(flags)

    -- wrap flags first
    flags = table.wrap(flags)

    -- done
    local results = {}
    local mapflags = self:get("mapflags")
    if mapflags then

        -- map flags
        for _, flag in pairs(flags) do
            local flag_mapped = self:_mapflag(flag, mapflags)
            if flag_mapped then
                table.insert(results, flag_mapped)
            end
        end

    else

        -- has flags?
        for _, flag in pairs(flags) do
            if self:has_flags(flag) then
                table.insert(results, flag)
            end
        end

    end

    -- ok?
    return results
end

-- get the flag kinds
function builder:_flagkinds()
    return self._FLAGKINDS
end

-- inherts from target deps
function builder:_inherit_from_target(values, target, name)
    table.join2(values, target:get(name))
    if target:type() == "target" then
        for _, opt in ipairs(target:options()) do
            table.join2(values, opt:get(name))
        end
    end
end

-- inherts from target deps
function builder:_inherit_from_targetdeps(results, target, flagname)

    -- for all target deps
    local orderdeps = target:orderdeps()
    local total = #orderdeps
    for idx, _ in ipairs(orderdeps) do

        -- reverse deps order for links
        local dep = orderdeps[total + 1 - idx]

        -- is static or shared target library? link it
        local depkind      = dep:get("kind")
        local targetkind   = target:get("kind")
        local depconfig    = table.wrap(target:depconfig(dep:name()))
        if (depkind == "static" or depkind == "shared") and (depconfig.inherit == nil or depconfig.inherit) then
            if flagname == "links" and (targetkind == "binary" or targetkind == "shared") then

                -- add dependent link
                table.insert(results, dep:name())

                -- inherit links from the depdent target
                self:_inherit_from_target(results, dep, "links")

            elseif flagname == "linkdirs" and (targetkind == "binary" or targetkind == "shared") then

                -- add dependent linkdirs
                table.insert(results, path.directory(dep:targetfile()))

                -- inherit linkdirs from the depdent target
                self:_inherit_from_target(results, dep, "linkdirs")

            elseif flagname == "rpathdirs" and targetkind == "binary" then

                -- make rpathdir 
                local rpathdir = "@loader_path"
                local subdir = path.relative(path.directory(dep:targetfile()), path.directory(target:targetfile()))
                if subdir and subdir ~= '.' then
                    rpathdir = path.join(rpathdir, subdir)
                end
<commit_msg>fix dependent target link bug<commit_after>--!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 - 2018, TBOOX Open Source Group.
--
-- @author      ruki
-- @file        builder.lua
--

-- define module
local builder = builder or {}

-- load modules
local io       = require("base/io")
local path     = require("base/path")
local utils    = require("base/utils")
local table    = require("base/table")
local string   = require("base/string")
local option   = require("base/option")
local tool     = require("tool/tool")
local config   = require("project/config")
local sandbox  = require("sandbox/sandbox")
local language = require("language/language")
local platform = require("platform/platform")

-- get the tool of builder
function builder:_tool()
    return self._TOOL
end

-- get the name flags
function builder:_nameflags()
    return self._NAMEFLAGS
end

-- get the target kind
function builder:_targetkind()
    return self._TARGETKIND
end

-- map gcc flag to the given builder flag
function builder:_mapflag(flag, mapflags)

    -- attempt to map it directly
    local flag_mapped = mapflags[flag]
    if flag_mapped then
        return flag_mapped
    end

    -- find and replace it using pattern
    for k, v in pairs(mapflags) do
        local flag_mapped, count = flag:gsub("^" .. k .. "$", function (w) return v end)
        if flag_mapped and count ~= 0 then
            return utils.ifelse(#flag_mapped ~= 0, flag_mapped, nil) 
        end
    end

    -- has this flag?
    if self:has_flags(flag) then
        return flag
    end
end

-- map gcc flags to the given builder flags
function builder:_mapflags(flags)

    -- wrap flags first
    flags = table.wrap(flags)

    -- done
    local results = {}
    local mapflags = self:get("mapflags")
    if mapflags then

        -- map flags
        for _, flag in pairs(flags) do
            local flag_mapped = self:_mapflag(flag, mapflags)
            if flag_mapped then
                table.insert(results, flag_mapped)
            end
        end

    else

        -- has flags?
        for _, flag in pairs(flags) do
            if self:has_flags(flag) then
                table.insert(results, flag)
            end
        end

    end

    -- ok?
    return results
end

-- get the flag kinds
function builder:_flagkinds()
    return self._FLAGKINDS
end

-- inherts from target deps
function builder:_inherit_from_target(values, target, name)
    table.join2(values, target:get(name))
    if target:type() == "target" then
        for _, opt in ipairs(target:options()) do
            table.join2(values, opt:get(name))
        end
    end
end

-- inherts from target deps
function builder:_inherit_from_targetdeps(results, target, flagname)

    -- for all target deps
    local orderdeps = target:orderdeps()
    local total = #orderdeps
    for idx, _ in ipairs(orderdeps) do

        -- reverse deps order for links
        local dep = orderdeps[total + 1 - idx]

        -- is static or shared target library? link it
        local depkind      = dep:get("kind")
        local targetkind   = target:get("kind")
        local depconfig    = table.wrap(target:depconfig(dep:name()))
        if (depkind == "static" or depkind == "shared") and (depconfig.inherit == nil or depconfig.inherit) then
            if flagname == "links" and (targetkind == "binary" or targetkind == "shared") then

                -- add dependent link
                table.insert(results, dep:basename())

                -- inherit links from the depdent target
                self:_inherit_from_target(results, dep, "links")

            elseif flagname == "linkdirs" and (targetkind == "binary" or targetkind == "shared") then

                -- add dependent linkdirs
                table.insert(results, path.directory(dep:targetfile()))

                -- inherit linkdirs from the depdent target
                self:_inherit_from_target(results, dep, "linkdirs")

            elseif flagname == "rpathdirs" and targetkind == "binary" then

                -- make rpathdir 
                local rpathdir = "@loader_path"
                local subdir = path.relative(path.directory(dep:targetfile()), path.directory(target:targetfile()))
                if subdir and subdir ~= '.' then
                    rpathdir = path.join(rpathdir, subdir)
                end

{'fraction_non_alphanumeric': 0.0959037080178606, 'fraction_numerical': 0.0033003300330033004, 'mean_word_length': 2.408203771088323, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4525352', 'n_tokens_mistral': 2872, 'n_tokens_neox': 2775, 'n_words': 1004}