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

#include <bpkg/cfg-create>

#include <utility> // move()
#include <cassert>
#include <fstream>

#include <bpkg/types>
#include <bpkg/utility>
#include <bpkg/database>
#include <bpkg/diagnostics>

using namespace std;
using namespace butl;

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

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

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

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

        if (!o.wipe ())
          fail << "directory " << d << " is not empty";

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

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

    while (args.more ())
    {
      string a (args.next ());
      (a.find ('=') != string::npos ? vars : mods).push_back (move (a));
    }

    // Create build/.
    //
    dir_path bd (d / dir_path ("build"));
    mk (bd);

    // Write build/bootstrap.build.
    //
    path f (bd / path ("bootstrap.build"));
    try
    {
      ofstream ofs;
      ofs.exceptions (ofstream::badbit | ofstream::failbit);
      ofs.open (f.string ());

      ofs << "# Maintained automatically by bpkg, do not edit." << endl
          << "#" << endl
          << "project =" << endl
          << "amalgamation =" << endl
          << "using config" << endl
          << "using install" << endl;

      // Load user-supplied modules in bootstrap.build instead of root.build
      // since we don't know whether they can be loaded in the latter.
      //
      for (const string& m: mods)
        ofs << "using " << m << endl;
    }
    catch (const ofstream::failure&)
    {
      fail << "unable to write to " << f;
    }

    // Write root buildfile.
    //
    f = path (d / path ("buildfile"));
    try
    {
      ofstream ofs;
      ofs.exceptions (ofstream::badbit | ofstream::failbit);
      ofs.open (f.string ());

      ofs << "# Maintained automatically by bpkg, do not edit." << endl
          << "#" << endl
          << "./:" << endl;
    }
    catch (const ofstream::failure&)
    {
      fail << "unable to write to " << f;
    }

    // Configure.
    //
    run_b ("configure(" + d.string () + "/)", vars);

    // Create the database.
    //
    open (d, true);

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