aboutsummaryrefslogtreecommitdiff
path: root/paludis/libtool_linkage_checker.cc
blob: 22bbd1ad3070b009a9cc73d365c047028a710a87 (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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007 David Leverton
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis/libtool_linkage_checker.hh>

#include <paludis/util/realpath.hh>
#include <paludis/util/config_file.hh>
#include <paludis/util/fs_entry.hh>
#include <paludis/util/log.hh>
#include <paludis/util/mutex.hh>
#include <paludis/util/options.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/tokeniser.hh>
#include <paludis/util/safe_ifstream.hh>

#include <algorithm>
#include <cstring>
#include <cerrno>
#include <functional>
#include <vector>

using namespace paludis;

typedef std::vector<std::pair<FSEntry, std::string> > Breakage;

namespace paludis
{
    template <>
    struct Imp<LibtoolLinkageChecker>
    {
        FSEntry root;

        Mutex mutex;

        Breakage breakage;

        Imp(const FSEntry & the_root) :
            root(the_root)
        {
        }
    };
}

namespace
{
    struct IsNotAbsolutePath : std::unary_function<std::string, bool>
    {
        bool operator() (const std::string & str)
        {
            return str.empty() || '/' != str[0];
        }
    };
}

LibtoolLinkageChecker::LibtoolLinkageChecker(const FSEntry & root) :
    Pimp<LibtoolLinkageChecker>(root)
{
}

LibtoolLinkageChecker::~LibtoolLinkageChecker()
{
}

bool
LibtoolLinkageChecker::check_file(const FSEntry & file)
{
    std::string basename(file.basename());
    if (! (3 <= basename.length() &&
           ".la" == basename.substr(basename.length() - 3)))
        return false;

    Context ctx("When checking '" + stringify(file) + "' as a libtool library:");

    SafeIFStream stream(file);

    KeyValueConfigFileOptions opts;
    opts += kvcfo_disallow_space_around_equals;
    opts += kvcfo_disallow_space_inside_unquoted_values;

    std::vector<std::string> deps;

    try
    {
        KeyValueConfigFile kvs(stream, opts,
                &KeyValueConfigFile::no_defaults, &KeyValueConfigFile::no_transformation);
        tokenise_whitespace(kvs.get("dependency_libs"), std::back_inserter(deps));
    }
    catch (const ConfigFileError & ex)
    {
        Log::get_instance()->message("reconcilio.broken_linkage_finder.failure", ll_warning, lc_context) << ex.message();
        return true;
    }

    deps.erase(std::remove_if(deps.begin(), deps.end(), IsNotAbsolutePath()), deps.end());
    if (deps.empty())
    {
        Log::get_instance()->message("reconcilio.broken_linkage_finder.no_libtool", ll_debug, lc_context)
            << "No libtool library dependencies found";
        return true;
    }

    for (std::vector<std::string>::const_iterator it(deps.begin()),
             it_end(deps.end()); it_end != it; ++it)
    {
        try
        {
            FSEntry dep(_imp->root / *it);
            if (! dereference_with_root(dep, _imp->root).is_regular_file())
            {
                Log::get_instance()->message("reconcilio.broken_linkage_finder.dependency_missing",
                    ll_debug, lc_context) << "Dependency '" << *it <<
                    "' is missing or not a regular file in '" << _imp->root << "'";

                Lock l(_imp->mutex);
                _imp->breakage.push_back(std::make_pair(file, *it));
            }

            else
                Log::get_instance()->message("reconcilio.broken_linkage_finder.dependency_exists", ll_debug, lc_context)
                    << "Dependency '" << *it << "' exists in '" << _imp->root << "'";
        }

        catch (const FSError & ex)
        {
            Log::get_instance()->message("reconcilio.broken_linkage_finder.failure", ll_warning, lc_no_context) << ex.message();
        }
    }

    return true;
}

void
LibtoolLinkageChecker::note_symlink(const FSEntry &, const FSEntry &)
{
}

void
LibtoolLinkageChecker::add_extra_lib_dir(const FSEntry &)
{
}

void
LibtoolLinkageChecker::need_breakage_added(
    const std::function<void (const FSEntry &, const std::string &)> & callback)
{
    for (Breakage::const_iterator it(_imp->breakage.begin()), it_end(_imp->breakage.end()); it_end != it; ++it)
        callback(it->first, it->second);
}