summaryrefslogtreecommitdiff
path: root/tools/intergen/cppgen/src/cppgen/module_manager.cc
blob: 104e3fa856828736fae0b45404b6ee9be7600af0 (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
/**
 * Copyright (c) 2014, Ford Motor Company
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the Ford Motor Company nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "cppgen/module_manager.h"

#include <errno.h>
#include <fstream>

#include "cppgen/generator_preferences.h"

#ifdef OS_POSIX
#include <sys/stat.h>
#include <sys/types.h>

namespace {
bool CreateDirectory(const std::string& name) {
  int result = mkdir(name.c_str(), 0777) == 0;
  if (0 == result) {
    return true;
  } else if (EEXIST == result || EPERM == result) {
    struct stat s;
    result = stat(name.c_str(), &s);
    if (result != 0) {
      return false;
    }
    return (S_ISDIR(s.st_mode));
  }
  return false;
}

}  // namespace

#else
#error Please provide create directory function for your platform
#endif


namespace codegen {

ModuleManager::ModuleManager(const std::string& name,
                             const TypePreferences& prefs)
    : module_name_(name),
      enums_header_(module_name_ + "/enums.h", module_name_, true),
      enums_source_(module_name_ + "/enums.cc", module_name_, false),
      structs_header_(module_name_ + "/types.h", module_name_, true),
      structs_source_(module_name_ + "/types.cc", module_name_, false),
      functions_header_(module_name_ + "/functions.h", module_name_, true),
      functions_source_(module_name_ + "/functions.cc", module_name_, false),
      interface_header_(module_name_ + "/interface.h", module_name_, true),
      interface_source_(module_name_ + "/interface.cc", module_name_, false),
      additional_validation_source_(module_name_ + "/validation.cc", module_name_, false){
  structs_header_.Include(CppFile::Header("rpc_base/rpc_message.h", true));
  structs_header_.Include(CppFile::Header(enums_header_.file_name(), true));
  functions_header_.Include(CppFile::Header(enums_header_.file_name(), true));
  functions_header_.Include(CppFile::Header(structs_header_.file_name(), true));
  functions_header_.Include(CppFile::Header(interface_header_.file_name(), true));
  interface_header_.Include(CppFile::Header("rpc_base/rpc_message.h", true));
  interface_header_.Include(CppFile::Header(enums_header_.file_name(), true));
  enums_header_.Include(CppFile::Header("string", false));
  enums_source_.Include(CppFile::Header(enums_header_.file_name(), true));
  structs_source_.Include(CppFile::Header(structs_header_.file_name(), true));
  functions_source_.Include(CppFile::Header(functions_header_.file_name(),
                                            true));
  interface_source_.Include((CppFile::Header(interface_header_.file_name(),
                                             true)));
  interface_source_.Include(CppFile::Header(functions_header_.file_name(),
                                            true));

  additional_validation_source_.Include(
      CppFile::Header(structs_header_.file_name(), true));
  additional_validation_source_.Include(
      CppFile::Header(functions_header_.file_name(), true));
  if (prefs.generate_json) {
    structs_source_.Include(
        CppFile::Header("rpc_base/rpc_base_json_inl.h", true));
    functions_source_.Include(
        CppFile::Header("rpc_base/rpc_base_json_inl.h", true));
  }
}

ModuleManager::~ModuleManager() {
}

CppFile& ModuleManager::HeaderForInterface() {
  return interface_header_;
}

CppFile& ModuleManager::HeaderForEnum(const Enum& enm) {
  return enums_header_;
}

CppFile& ModuleManager::HeaderForStruct(const Struct& strct) {
  return structs_header_;
}

CppFile& ModuleManager::HeaderForTypedef(const Typedef& tdef) {
  return structs_header_;
}

CppFile& ModuleManager::HeaderForFunction(const Function& function) {
  return functions_header_;
}

CppFile& ModuleManager::HeaderForResponse(const Response& request) {
  return functions_header_;
}

CppFile& ModuleManager::HeaderForNotification(
    const Notification& notification) {
  return functions_header_;
}

CppFile& ModuleManager::SourceForInterface() {
  return interface_source_;
}

CppFile& ModuleManager::SourceForEnum(const Enum& enm) {
  return enums_source_;
}

CppFile& ModuleManager::SourceForStruct(const Struct& strct) {
  return structs_source_;
}

CppFile& ModuleManager::SourceForFunction(const Function& function) {
  return functions_source_;
}

CppFile& ModuleManager::SourceForResponse(const Response& request) {
  return functions_source_;
}

CppFile& ModuleManager::SourceForNotification(
    const Notification& notification) {
  return functions_source_;
}

CppFile& ModuleManager::SourceForValidator() {
  return additional_validation_source_;
}

bool ModuleManager::Write() {
  if (!CreateDirectory(module_name_)) {
    return false;
  }
  CppFile* files[] = { &enums_header_, &enums_source_,
                       &structs_header_, &structs_source_,
                       &functions_header_, &functions_source_,
                       &interface_header_, &interface_source_,
                       &additional_validation_source_ };
  for (size_t i = 0; i != sizeof(files)/sizeof(files[0]); ++i) {
    CppFile* file = files[i];
    std::ofstream stream(file->file_name().c_str());
    file->Write(&stream);
    if (!stream) {
      return false;
    }
  }
  return true;
}

}  // namespace codegen