aboutsummaryrefslogtreecommitdiff
path: root/bpkg/cfg-create.cxx
blob: 1d8171feef1dee554de7627a87cffcfa41098c33 (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
// file      : bpkg/cfg-create.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <bpkg/cfg-create>

#include <butl/fdstream>

#include <bpkg/package>
#include <bpkg/package-odb>
#include <bpkg/database>
#include <bpkg/diagnostics>

using namespace std;
using namespace butl;

namespace bpkg
{
  int
  cfg_create (const cfg_create_options& o, cli::scanner& args)
  {
    tracer trace ("cfg_create");

    if (o.wipe () && !o.directory_specified ())
      fail << "--wipe requires explicit --directory|-d";

    dir_path c (o.directory ());
    l4 ([&]{trace << "creating configuration in " << c;});

    // If the directory already exists, make sure it is empty.
    // Otherwise, create it.
    //
    if (exists (c))
    {
      l5 ([&]{trace << "directory " << c << " exists";});

      if (!empty (c))
      {
        l5 ([&]{trace << "directory " << c << " not empty";});

        if (!o.wipe ())
          fail << "directory " << c << " is not empty" <<
            info << "use --wipe to clean it up but be careful";

        rm_r (c, false);
      }
    }
    else
    {
      l5 ([&]{trace << "directory " << c << " does not exist";});
      mk_p (c);
    }

    // Sort arguments into modules and configuration variables.
    //
    strings mods;
    strings vars;

    while (args.more ())
    {
      string a (args.next ());

      if (a.find ('=') != string::npos)
      {
        vars.push_back (move (a));
      }
      else if (!a.empty ())
      {
        // Append .config unless the module name ends with '.', in which case
        // strip it.
        //
        if (a.back () != '.')
          a += ".config";
        else
          a.pop_back ();

        mods.push_back (move (a));
      }
      else
        fail << "empty string as argument";
    }

    // Create build/.
    //
    dir_path b (c / dir_path ("build"));
    mk (b);

    // Write build/bootstrap.build.
    //
    path f (b / path ("bootstrap.build"));
    try
    {
      ofdstream ofs (f);

      ofs << "# Maintained automatically by bpkg. Edit if you know what " <<
        "you are doing." << endl
          << "#" << endl
          << "project =" << endl
          << "amalgamation =" << endl
          << endl
          << "using config" << endl
          << "using test" << endl
          << "using install" << endl;

      ofs.close ();
    }
    catch (const ofdstream::failure& e)
    {
      fail << "unable to write to " << f << ": " << e.what ();
    }

    // Write build/root.build.
    //
    f = b / path ("root.build");
    try
    {
      ofdstream ofs (f);

      ofs << "# Maintained automatically by bpkg. Edit if you know what " <<
        "you are doing." << endl
          << "#" << endl;

      // Load user-supplied modules. We don't really know whether they must
      // be loaded in bootstrap.
      //
      for (const string& m: mods)
      {
        // If the module name start with '?', then use optional load.
        //
        if (m.front () != '?')
          ofs << "using " << m << endl;
        else
          ofs << "using? " << m.c_str () + 1 << endl;
      }

      ofs.close ();
    }
    catch (const ofdstream::failure& e)
    {
      fail << "unable to write to " << f << ": " << e.what ();
    }

    // Write root buildfile.
    //
    f = c / path ("buildfile");
    try
    {
      ofdstream ofs (f);

      ofs << "# Maintained automatically by bpkg. Edit if you know what " <<
        "you are doing." << endl
          << "#" << endl
          << "./:" << endl;

      ofs.close ();
    }
    catch (const ofdstream::failure& e)
    {
      fail << "unable to write to " << f << ": " << e.what ();
    }

    // Configure.
    //
    // Run quiet. Use path representation to get canonical trailing slash.
    //
    run_b (o, c, "configure('" + c.representation () + "')", true, vars);

    // Create .bpkg/.
    //
    mk (c / bpkg_dir);

    // Create the database.
    //
    database db (open (c, trace, true));

    // Add the special, root repository object with empty location.
    //
    transaction t (db.begin ());
    db.persist (repository (repository_location ()));
    t.commit ();

    if (verb)
    {
      c.complete ().normalize ();
      text << "created new configuration in " << c;
    }

    return 0;
  }
}