Skip to content

Commit 28a721d

Browse files
committed
Added basic source build with recompilation using serialization
- Added fbs_utils to read, write to the flatbuffer file - Updated CMakeLists.txt with buildcc library - Added user headers -- buildcc.h -- fatal_assert.h -- file.h -- toolchain.h - Added target.cpp to initialize, compile and recompile a target -- Currently only has support for source files Minor - Added .clang-format file
1 parent 98496be commit 28a721d

9 files changed

+730
-0
lines changed

.clang-format

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
---
2+
Language: Cpp
3+
# BasedOnStyle: LLVM
4+
AccessModifierOffset: -2
5+
AlignAfterOpenBracket: Align
6+
AlignConsecutiveMacros: false
7+
AlignConsecutiveAssignments: false
8+
AlignConsecutiveDeclarations: false
9+
AlignEscapedNewlines: Right
10+
AlignOperands: true
11+
AlignTrailingComments: true
12+
AllowAllArgumentsOnNextLine: true
13+
AllowAllConstructorInitializersOnNextLine: true
14+
AllowAllParametersOfDeclarationOnNextLine: true
15+
AllowShortBlocksOnASingleLine: Never
16+
AllowShortCaseLabelsOnASingleLine: false
17+
AllowShortFunctionsOnASingleLine: All
18+
AllowShortLambdasOnASingleLine: All
19+
AllowShortIfStatementsOnASingleLine: Never
20+
AllowShortLoopsOnASingleLine: false
21+
AlwaysBreakAfterDefinitionReturnType: None
22+
AlwaysBreakAfterReturnType: None
23+
AlwaysBreakBeforeMultilineStrings: false
24+
AlwaysBreakTemplateDeclarations: MultiLine
25+
BinPackArguments: true
26+
BinPackParameters: true
27+
BraceWrapping:
28+
AfterCaseLabel: false
29+
AfterClass: false
30+
AfterControlStatement: false
31+
AfterEnum: false
32+
AfterFunction: false
33+
AfterNamespace: false
34+
AfterObjCDeclaration: false
35+
AfterStruct: false
36+
AfterUnion: false
37+
AfterExternBlock: false
38+
BeforeCatch: false
39+
BeforeElse: false
40+
IndentBraces: false
41+
SplitEmptyFunction: true
42+
SplitEmptyRecord: true
43+
SplitEmptyNamespace: true
44+
BreakBeforeBinaryOperators: None
45+
BreakBeforeBraces: Attach
46+
BreakBeforeInheritanceComma: false
47+
BreakInheritanceList: BeforeColon
48+
BreakBeforeTernaryOperators: true
49+
BreakConstructorInitializersBeforeComma: false
50+
BreakConstructorInitializers: BeforeColon
51+
BreakAfterJavaFieldAnnotations: false
52+
BreakStringLiterals: true
53+
ColumnLimit: 80
54+
CommentPragmas: '^ IWYU pragma:'
55+
CompactNamespaces: false
56+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
57+
ConstructorInitializerIndentWidth: 4
58+
ContinuationIndentWidth: 4
59+
Cpp11BracedListStyle: true
60+
DeriveLineEnding: true
61+
DerivePointerAlignment: false
62+
DisableFormat: false
63+
ExperimentalAutoDetectBinPacking: false
64+
FixNamespaceComments: true
65+
ForEachMacros:
66+
- foreach
67+
- Q_FOREACH
68+
- BOOST_FOREACH
69+
IncludeBlocks: Preserve
70+
IncludeCategories:
71+
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
72+
Priority: 2
73+
SortPriority: 0
74+
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
75+
Priority: 3
76+
SortPriority: 0
77+
- Regex: '.*'
78+
Priority: 1
79+
SortPriority: 0
80+
IncludeIsMainRegex: '(Test)?$'
81+
IncludeIsMainSourceRegex: ''
82+
IndentCaseLabels: false
83+
IndentGotoLabels: true
84+
IndentPPDirectives: None
85+
IndentWidth: 2
86+
IndentWrappedFunctionNames: false
87+
JavaScriptQuotes: Leave
88+
JavaScriptWrapImports: true
89+
KeepEmptyLinesAtTheStartOfBlocks: true
90+
MacroBlockBegin: ''
91+
MacroBlockEnd: ''
92+
MaxEmptyLinesToKeep: 1
93+
NamespaceIndentation: None
94+
ObjCBinPackProtocolList: Auto
95+
ObjCBlockIndentWidth: 2
96+
ObjCSpaceAfterProperty: false
97+
ObjCSpaceBeforeProtocolList: true
98+
PenaltyBreakAssignment: 2
99+
PenaltyBreakBeforeFirstCallParameter: 19
100+
PenaltyBreakComment: 300
101+
PenaltyBreakFirstLessLess: 120
102+
PenaltyBreakString: 1000
103+
PenaltyBreakTemplateDeclaration: 10
104+
PenaltyExcessCharacter: 1000000
105+
PenaltyReturnTypeOnItsOwnLine: 60
106+
PointerAlignment: Right
107+
ReflowComments: true
108+
SortIncludes: true
109+
SortUsingDeclarations: true
110+
SpaceAfterCStyleCast: false
111+
SpaceAfterLogicalNot: false
112+
SpaceAfterTemplateKeyword: true
113+
SpaceBeforeAssignmentOperators: true
114+
SpaceBeforeCpp11BracedList: false
115+
SpaceBeforeCtorInitializerColon: true
116+
SpaceBeforeInheritanceColon: true
117+
SpaceBeforeParens: ControlStatements
118+
SpaceBeforeRangeBasedForLoopColon: true
119+
SpaceInEmptyBlock: false
120+
SpaceInEmptyParentheses: false
121+
SpacesBeforeTrailingComments: 1
122+
SpacesInAngles: false
123+
SpacesInConditionalStatement: false
124+
SpacesInContainerLiterals: true
125+
SpacesInCStyleCastParentheses: false
126+
SpacesInParentheses: false
127+
SpacesInSquareBrackets: false
128+
SpaceBeforeSquareBrackets: false
129+
Standard: Latest
130+
StatementMacros:
131+
- Q_UNUSED
132+
- QT_REQUIRE_VERSION
133+
TabWidth: 8
134+
UseCRLF: false
135+
UseTab: Never
136+
...
137+

buildcc/CMakeLists.txt

+17
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,20 @@ add_custom_command(OUTPUT ${FBS_GEN_FILES}
1616
add_custom_target(fbs_to_header
1717
DEPENDS ${FBS_GEN_FILES}
1818
)
19+
20+
# Library
21+
add_library(buildcc)
22+
target_sources(buildcc PRIVATE
23+
src/target.cpp
24+
src/fbs_utils.cpp
25+
)
26+
target_include_directories(buildcc PUBLIC include)
27+
add_dependencies(buildcc fbs_to_header)
28+
29+
# PRIVATE linkages
30+
target_include_directories(buildcc PRIVATE
31+
${FlatBuffers_SOURCE_DIR}/include
32+
generated
33+
fbs
34+
)
35+
target_link_libraries(buildcc PRIVATE flatbuffers spdlog::spdlog)

buildcc/fbs/fbs_utils.h

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#ifndef BUILDCC_FBS_FBS_UTILS_H_
2+
#define BUILDCC_FBS_FBS_UTILS_H_
3+
4+
#include <filesystem>
5+
#include <string>
6+
7+
#include "buildcc.h"
8+
#include "toolchain.h"
9+
10+
// Third party
11+
#include "target_generated.h"
12+
13+
namespace fs = std::filesystem;
14+
namespace fbs = schema::internal;
15+
16+
namespace buildcc::internal {
17+
18+
/**
19+
* @brief Creates a new FBS Target
20+
* Writes to a <name>.bin binary file
21+
*
22+
* @param target
23+
*/
24+
bool fbs_utils_save_fbs_target(const buildcc::Target &target);
25+
26+
/**
27+
* @brief
28+
*
29+
* @param target
30+
* @return true
31+
* @return false
32+
*/
33+
bool fbs_utils_fbs_load_target(const buildcc::Target &target,
34+
fbs::TargetT *targetT);
35+
bool fbs_utils_fbs_load_target(const std::string &name,
36+
const fs::path &relative_path,
37+
fbs::TargetT *targetT);
38+
39+
/**
40+
* @brief Checks if an FBS Target exists
41+
* Checks against <name>.bin binary file
42+
*
43+
* @param target
44+
* @return true If file exists
45+
* @return false If file does not exist
46+
*/
47+
bool fbs_utils_fbs_target_exists(const buildcc::Target &target);
48+
bool fbs_utils_fbs_target_exists(const std::string &name,
49+
const fs::path &relative_path);
50+
51+
} // namespace buildcc::internal
52+
53+
#endif

buildcc/include/buildcc.h

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#ifndef BUILDCC_INCLUDE_BUILDCC_H_
2+
#define BUILDCC_INCLUDE_BUILDCC_H_
3+
4+
#include <filesystem>
5+
#include <string>
6+
#include <unordered_set>
7+
#include <vector>
8+
9+
#include "file.h"
10+
#include "toolchain.h"
11+
12+
namespace buildcc {
13+
14+
namespace fs = std::filesystem;
15+
16+
enum class TargetType {
17+
Executable,
18+
StaticLibrary,
19+
DynamicLibrary,
20+
};
21+
22+
class Target {
23+
public:
24+
explicit Target(const std::string &name, TargetType type,
25+
const Toolchain &toolchain,
26+
const std::filesystem::path &base_relative_path)
27+
: name_(name), type_(type), toolchain_(toolchain),
28+
relative_path_(base_relative_path) {
29+
Initialize();
30+
}
31+
32+
// Builders
33+
void Build();
34+
35+
// Setters
36+
void AddSource(const std::string &relative_filename);
37+
void AddSource(const std::string &relative_filename,
38+
const std::filesystem::path &relative_to_base_relative_path);
39+
40+
// TODO,
41+
void AppendSources(const std::vector<std::string> &relative_filenames);
42+
void
43+
AppendSources(const std::vector<std::string> &relative_filenames,
44+
const std::filesystem::path &relative_to_base_relative_path);
45+
46+
void AddIncludeDir(const std::string &dir);
47+
void AppendIncludeDirs(const std::vector<std::string> &dirs);
48+
49+
void AddLibDep(const Target &target);
50+
void AppendLibDeps(const std::vector<Target> &targets);
51+
52+
void AddLibDir(const std::string &dir);
53+
void AppendLibDirs(const std::vector<std::string> &dirs);
54+
55+
// Getters
56+
const std::string &GetName() const { return name_; }
57+
const Toolchain &GetToolchain() const { return toolchain_; }
58+
const std::filesystem::path &GetRelativePath() const {
59+
return relative_path_;
60+
}
61+
TargetType GetTargetType() const { return type_; }
62+
63+
const std::unordered_set<std::string> &GetSources() const {
64+
return current_source_files_;
65+
}
66+
67+
// TODO, Add more getters
68+
69+
private:
70+
// Open the serialized file and parse the target
71+
void Initialize();
72+
73+
std::string CompileSource(const std::string &source);
74+
std::vector<std::string> CompileSources();
75+
std::vector<std::string> RecompileSources();
76+
77+
void BuildTarget(const std::vector<std::string> &compiled_sources);
78+
79+
private:
80+
// Constructor defined
81+
std::string name_;
82+
Toolchain toolchain_;
83+
std::filesystem::path relative_path_;
84+
TargetType type_;
85+
86+
//
87+
std::unordered_set<std::string> current_source_files_;
88+
std::unordered_set<internal::File, internal::FileHash> loaded_source_files_;
89+
90+
// ! UNUSED
91+
std::unordered_set<std::string> current_include_dirs_;
92+
std::unordered_set<std::string> loaded_include_dirs_;
93+
94+
std::vector<Target> lib_deps_;
95+
std::vector<std::string> current_lib_dirs;
96+
std::vector<std::string> loaded_lib_dirs_;
97+
98+
// Internal
99+
bool dirty_ = false; // NOTE, Might not be needed
100+
};
101+
102+
} // namespace buildcc
103+
104+
#endif

buildcc/include/fatal_assert.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef BUILDCC_INCLUDE_ASSERT_H_
2+
#define BUILDCC_INCLUDE_ASSERT_H_
3+
4+
#include <cassert>
5+
#include <string>
6+
7+
#include "spdlog/spdlog.h"
8+
9+
namespace buildcc {
10+
11+
template <typename T>
12+
void assert_fatal(T first, T second, const std::string &message) {
13+
if (first != second) {
14+
spdlog::error(message);
15+
assert(first == second);
16+
}
17+
}
18+
19+
template <typename T>
20+
void assert_fatal_true(T first, const std::string &message) {
21+
assert_fatal(first, true, message);
22+
}
23+
24+
} // namespace buildcc
25+
26+
#endif

buildcc/include/file.h

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#ifndef BUILDCC_INCLUDE_FILE_H_
2+
#define BUILDCC_INCLUDE_FILE_H_
3+
4+
#include <filesystem>
5+
#include <string>
6+
7+
namespace buildcc::internal {
8+
9+
class File {
10+
public:
11+
File(const std::string &filename)
12+
: filename_(filename),
13+
last_write_timestamp_(std::filesystem::last_write_time(filename)
14+
.time_since_epoch()
15+
.count()) {}
16+
17+
File(const std::string &filename, std::uint64_t last_write_timestamp)
18+
: filename_(filename), last_write_timestamp_(last_write_timestamp) {}
19+
20+
// Setters
21+
void SetLastWriteTimestamp(uint64_t last_write_timestamp) {
22+
last_write_timestamp_ = last_write_timestamp;
23+
}
24+
25+
// Getters
26+
std::uint64_t GetLastWriteTimestamp() const { return last_write_timestamp_; }
27+
const std::string &GetFilename() const { return filename_; }
28+
29+
// Used during find operation
30+
bool operator==(const File &f) const {
31+
return GetFilename() == f.GetFilename();
32+
}
33+
34+
private:
35+
std::string filename_;
36+
std::uint64_t last_write_timestamp_;
37+
};
38+
39+
// Used by File
40+
class FileHash {
41+
public:
42+
size_t operator()(const File &f) const {
43+
return std::hash<std::string>()(f.GetFilename());
44+
}
45+
};
46+
47+
} // namespace buildcc::internal
48+
49+
#endif

0 commit comments

Comments
 (0)