summaryrefslogtreecommitdiff
path: root/apps/soreduce/Library.cpp
blob: b1b0c5b3e59a4fad8e9cd56f059efe6072dce4f8 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// -*- C++ -*-
// $Id$

// File: Library.cpp

// Author: Phil Mesnier

// This file contains the implementation of the classes responsible for
// generating specialized mpc files for individual libraries, as well as
// outputting usage metrics for the various object modules contained in the
// library.

#include "ace/OS_NS_dirent.h"
#include "ace/OS_NS_stdlib.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_sys_stat.h"
#include "ace/OS_NS_ctype.h"
#include "ace/Log_Msg.h"

#include "Library.h"

ACE_RCSID(src, Library, "$Id$")

MPC_Generator::MPC_Generator (const ACE_CString& libname)
  : mpcfile_(),
    libname_(libname),
    mpcfilename_()
{
  mpcfilename_ = libname_ + "_subset.mpc";
}

MPC_Generator::~MPC_Generator ()
{
}

void
MPC_Generator::write_file (const ACE_CString& file)
{
  mpcfile_ << "    " << file << ".cpp" << endl;
}

void
MPC_Generator::write_prolog (const ACE_CString& path)
{
  ACE_CString fname (path + "/" + mpcfilename_);
  ACE_DEBUG ((LM_DEBUG, "writing file %s\n",fname.c_str()));
  mpcfile_.open(fname.c_str());
  if (!mpcfile_)
    ACE_DEBUG ((LM_DEBUG,"mpc file open failed\n"));

  mpcfile_
    << "// Generated mpc file for producing a subset of the "
    << libname_ << " library " << endl << endl
    << "project(" << libname_ << "_subset)";

  this->write_baseprojects ();

  mpcfile_
    << " {" << endl
    << "  sharedname   = " << libname_ << "_subset" << endl
    << "  pch_header   = " << endl
    << "  pch_source   = " << endl;

  this->write_projectinfo ();

  mpcfile_ << endl
           << "  Source_Files {" << endl;
}

void
MPC_Generator::write_epilog ()
{
  mpcfile_ << "  }" << endl
           << "}" << endl;
  mpcfile_.close();
}

void
MPC_Generator::write_baseprojects()
{
  mpcfile_ << ": acedefaults, aceversion, core";
}

void
MPC_Generator::write_projectinfo()
{
  mpcfile_ << "  libout       = $(ACE_ROOT)/lib" << endl
           << "  dynamicflags = ACE_BUILD_DLL ACE_OS_BUILD_DLL" << endl;
}

//-----------------------------------------------------------------------------
MPC_ACE_Dep_Lib::MPC_ACE_Dep_Lib (const ACE_CString& libname)
  : MPC_Generator(libname)
{}

void
MPC_ACE_Dep_Lib::write_baseprojects()
{
  mpcfile_ << ": acedefaults, aceversion";
}

void
MPC_ACE_Dep_Lib::write_projectinfo()
{
  mpcfile_ << "  libout       = $(ACE_ROOT)/lib" << endl
           << "  libs        += ACE_subset" << endl
           << "  after       += ACE_subset" << endl;
}

//-----------------------------------------------------------------------------
MPC_TAO_Lib::MPC_TAO_Lib (const ACE_CString& libname)
  : MPC_ACE_Dep_Lib(libname)
{}

void
MPC_TAO_Lib::write_baseprojects()
{
  MPC_ACE_Dep_Lib::write_baseprojects ();
  mpcfile_ << ", taoversion, core, tao_output, taodefaults";
}

void
MPC_TAO_Lib::write_projectinfo()
{
  MPC_ACE_Dep_Lib::write_projectinfo();
  mpcfile_ << "  dynamicflags = TAO_BUILD_DLL" << endl;
}

//-----------------------------------------------------------------------------
MPC_TAO_Dep_Lib::MPC_TAO_Dep_Lib (const ACE_CString& libname)
  : MPC_TAO_Lib(libname)
{}

void
MPC_TAO_Dep_Lib::write_baseprojects()
{
  MPC_TAO_Lib::write_baseprojects ();
  mpcfile_ << ", taoidldefaults";
}

void
MPC_TAO_Dep_Lib::write_projectinfo()
{
  // Try our best to generate the dynamicflags
  ACE_CString dflags;
  for(size_t i = 0; i < this->libname_.length (); ++i) {
    dflags += static_cast<char>(ACE_OS::ace_toupper (this->libname_[i]));
  }
  dflags += "_BUILD_DLL";

  MPC_ACE_Dep_Lib::write_projectinfo();
  mpcfile_ << "  dynamicflags = " << dflags.c_str () << endl
           << "  libs        += TAO_subset" << endl
           << "  after       += TAO_subset" << endl
           << "  includes    += $(TAO_ROOT)/orbsvcs" << endl
           << "  idlflags    += -I$(TAO_ROOT)/orbsvcs" << endl;
}

//-----------------------------------------------------------------------------

Library::Library (const char *name)
  : name_(name),
    path_(),
    num_modules_(0),
    num_exports_(0),
    num_extrefs_(0),
    modules_(0),
    exported_(0),
    mpcfile_(0)
{
  if (name_ == "ACE")
    mpcfile_ = new MPC_Generator(name_);
  else if (name_.find ("ACE_") == 0)
    mpcfile_ = new MPC_ACE_Dep_Lib (name_);
  else if (name_ == "TAO")
    mpcfile_ = new MPC_TAO_Lib (name_);
  else
    mpcfile_ = new MPC_TAO_Dep_Lib (name_);
}

Library::~Library ()
{
  delete mpcfile_;
  int i;
  for (i = 0; i < num_modules_; delete modules_[i++]);
  delete [] modules_;
}

void
Library::set_path (const char *p)
{
  char abspath[1000];
  memset (abspath,0,1000);
  ssize_t abspathlen = ACE_OS::readlink(p,abspath,999);
  ACE_CString path (p);
  if (abspathlen > 0) {
    abspath[abspathlen] = 0;
    path = abspath;
  }

  int pathsep = path.rfind('/');

  if (pathsep == ACE_CString::npos) {
    path_ = ".";
  } else {
    path_ = path.substr(0,pathsep);
  }
}

const ACE_CString &
Library::name () const
{
  return name_;
}

int
Library::has_modules () const
{
  return num_modules_ > 0;
}

static int
selector (const dirent *d)
{
  return ACE_OS::strstr (d->d_name, ACE_TEXT (".o")) != 0;
}

static int
comparator (const dirent **d1, const dirent **d2)
{
  return ACE_OS::strcmp ((*d1)->d_name, (*d2)->d_name);
}

void
Library::load_modules ()
{
  ACE_CString subdir = path_ + "/.shobj";

  struct dirent **dent;
  num_modules_ = ACE_OS::scandir(ACE_TEXT_TO_TCHAR_IN (subdir.c_str()),
                                 &dent,selector,comparator);

  if (num_modules_ > 0) {
    modules_ = new Obj_Module * [num_modules_];
    for (int i = 0; i < num_modules_; i++) {
      ACE_CString ent_name (ACE_TEXT_TO_CHAR_IN (dent[i]->d_name));
      modules_[i] = new Obj_Module(ent_name);
      modules_[i]->add_source (ACE_CString(subdir + "/" + ent_name).c_str());
      ACE_OS::free(dent[i]);
    };
  }

  if (num_modules_ > -1)
    ACE_OS::free(dent);
}

void
Library::resolve (Sig_List &undefs)
{
  if (num_modules_ < 1)
    return;

  for (const Signature *uname = undefs.first();
       undefs.hasmore();
       uname = undefs.next()) {
    if (exported_.index_of(uname) != -1) {
      undefs.remove_current();
    }
    else
      for (int i = 0; i < num_modules_; i++)
        if (modules_[i]->extref() == 0 &&
            modules_[i]->exports().index_of(uname) != -1)
          {
            undefs.remove_current();
            exported_.add (modules_[i]->exports());
            for (const Signature *needed = modules_[i]->imports().first();
                 modules_[i]->imports().hasmore();
                 needed = modules_[i]->imports().next())
              if (exported_.index_of(needed) == -1)
                undefs.add (needed->name());
            modules_[i]->add_extref();
            num_extrefs_++;
            break;
          }
  }
}

void
Library::write_export_list (int show_ref_counts)
{
  if (num_modules_ < 1)
    return;

  ACE_CString excludedfilename = path_ + "/excluded_modules";
  ACE_CString rcpath = path_ + "/usage_metrics";

  ofstream exclusions (excludedfilename.c_str());
  if (!exclusions) {
    ACE_ERROR ((LM_ERROR, "%p\n", "open exclusions list"));
  }

  if (show_ref_counts) {
    ACE_DEBUG ((LM_DEBUG, "Making directory %s\n",rcpath.c_str()));
    if (ACE_OS::mkdir(ACE_TEXT_TO_TCHAR_IN (rcpath.c_str())) == -1 &&
        errno != EEXIST)
      ACE_ERROR ((LM_ERROR, "%p\n", "mkdir"));
  }

  ACE_DEBUG ((LM_DEBUG,"%s: %d out of %d modules required\n",
              name_.c_str(), num_extrefs_, num_modules_));

  mpcfile_->write_prolog(path_);

  for (int i = 0; i < num_modules_ ; i++)
    if (modules_[i]->extref()) {
      if (show_ref_counts) {
        ACE_CString fname = rcpath + "/" +  modules_[i]->name();
        ofstream countfile (fname.c_str());
        countfile << "Exported symbols:" << endl;
        for (const Signature *sig = modules_[i]->exports().first();
             modules_[i]->exports().hasmore();
             sig = modules_[i]->exports().next())
          {
            countfile.width(5);
            countfile << sig->used_count() << " " << sig->name() << endl;
          }
        countfile << "\nImported symbols:" << endl;
        for (const Signature *n_sig = modules_[i]->imports().first();
             modules_[i]->imports().hasmore();
             n_sig = modules_[i]->imports().next())
          countfile << n_sig->name() << endl;
      }
      mpcfile_->write_file(modules_[i]->name().substring(0,modules_[i]->name().length()-2));
    } else {
      //      const char * modname = modules_[i]->name().c_str();
      exclusions
        << modules_[i]->name().substring(0,modules_[i]->name().length()-2)
        << endl;
    }

  mpcfile_->write_epilog();
}