Skip to content

Commit

Permalink
Merge branch 'team-4-graph' into new-graph-updated
Browse files Browse the repository at this point in the history
Signed-off-by: waynewangyuxuan <[email protected]>
  • Loading branch information
waynewangyuxuan authored Dec 6, 2024
2 parents 749544c + d29af1d commit ace523a
Show file tree
Hide file tree
Showing 15 changed files with 475 additions and 16 deletions.
40 changes: 40 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,45 @@ else()
endif()
target_include_directories(libninja-re2c PRIVATE src)

# --- Create shared library ---
add_library(libninja_shared SHARED
src/build_log.cc
src/build.cc
src/clean.cc
src/clparser.cc
src/dyndep.cc
src/dyndep_parser.cc
src/debug_flags.cc
src/deps_log.cc
src/disk_interface.cc
src/edit_distance.cc
src/elide_middle.cc
src/eval_env.cc
src/graph.cc
src/graphviz.cc
src/json.cc
src/line_printer.cc
src/manifest_parser.cc
src/metrics.cc
src/missing_deps.cc
src/parser.cc
src/state.cc
src/status_printer.cc
src/string_piece_util.cc
src/util.cc
src/version.cc
)

# Link re2c library if required
target_link_libraries(libninja_shared PRIVATE libninja-re2c)

# Include directories for the shared library
target_include_directories(libninja_shared PUBLIC ${CMAKE_SOURCE_DIR}/src)

# Set shared library properties
set_target_properties(libninja_shared PROPERTIES OUTPUT_NAME "ninja_shared")


# --- Check for 'browse' mode support
function(check_platform_supports_browse_mode RESULT)
# Make sure the inline.sh script works on this platform.
Expand Down Expand Up @@ -320,6 +359,7 @@ if(BUILD_TESTING)

add_test(NAME NinjaTest COMMAND ninja_test)


endif()

if(NINJA_BUILD_BINARY)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Or your can do it manually:
a. Before building simulate-ninja-graph, you need to compile and generate a shared object that includes partial functionality (functions) of Ninja. You can refer to lines 41 to 67 in `new-graph/easybuild.sh` to find the .cc files that need to be included.
b. After generating the shared object, compile the `new-graph/simulate-ninja-graph.cc` file and link it with the previously built shared object.
c. Once completed, run ./simulate-ninja-graph and pass hello_world as an argument (./simulate-ninja-graph hello_world). This program will look for a `new-graph/hello_world.cc` file in the current folder and compile it.
4. After the compilation, an executable file named `new-graph/hello_world` will be created, which you can run.
e. After the compilation, an executable file named `new-graph/hello_world` will be created, which you can run.
(You can modify easybuild.sh based on your OS and compiler to simplify this process.)

## CMake
Expand Down
23 changes: 23 additions & 0 deletions commands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
mkdir build && cd build
cmake -S ..
make -j $(nproc)
ls
ctest
sudo cp ninja /usr/local/bin/ (move nin to local/bin)
ninja --version

(cd ..)
git clone https://github.com/llvm/llvm-project.git
cd llvm-project
mkdir build
cd build
cmake -G "Ninja" -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" -DCMAKE_BUILD_TYPE=Release ../llvm
ninja clang-tidy
export PATH=$PATH:/home/yuwei/Documents/llvm-project/build/bin
clang-tidy --version


cmake -S .. -B build-cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
cmake --build build-cmake --target run-clang-tidy


25 changes: 12 additions & 13 deletions src/manifest_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ using namespace std;

ManifestParser::ManifestParser(State* state, FileReader* file_reader,
ManifestParserOptions options)
: Parser(state, file_reader),
options_(options), quiet_(false) {
: Parser(state, file_reader), options_(options), quiet_(false) {
env_ = &state->bindings_;
}

Expand Down Expand Up @@ -87,14 +86,12 @@ bool ManifestParser::Parse(const string& filename, const string& input,
case Lexer::NEWLINE:
break;
default:
return lexer_.Error(string("unexpected ") + Lexer::TokenName(token),
err);
return lexer_.Error(string("unexpected ") + Lexer::TokenName(token), err);
}
}
return false; // not reached
}


bool ManifestParser::ParsePool(string* err) {
string name;
if (!lexer_.ReadIdent(&name))
Expand Down Expand Up @@ -131,7 +128,6 @@ bool ManifestParser::ParsePool(string* err) {
return true;
}


bool ManifestParser::ParseRule(string* err) {
string name;
if (!lexer_.ReadIdent(&name))
Expand Down Expand Up @@ -162,8 +158,10 @@ bool ManifestParser::ParseRule(string* err) {

if (rule->bindings_["rspfile"].empty() !=
rule->bindings_["rspfile_content"].empty()) {
return lexer_.Error("rspfile and rspfile_content need to be "
"both specified", err);
return lexer_.Error(
"rspfile and rspfile_content need to be "
"both specified",
err);
}

if (rule->bindings_["command"].empty())
Expand Down Expand Up @@ -365,7 +363,7 @@ bool ManifestParser::ParseEdge(string* err) {

edge->validations_.reserve(validations.size());
for (std::vector<EvalString>::iterator v = validations.begin();
v != validations.end(); ++v) {
v != validations.end(); ++v) {
string path = v->Evaluate(env);
if (path.empty())
return lexer_.Error("empty path", err);
Expand All @@ -386,9 +384,10 @@ bool ManifestParser::ParseEdge(string* err) {
if (new_end != edge->inputs_.end()) {
edge->inputs_.erase(new_end, edge->inputs_.end());
if (!quiet_) {
Warning("phony target '%s' names itself as an input; "
"ignoring [-w phonycycle=warn]",
out->path().c_str());
Warning(
"phony target '%s' names itself as an input; "
"ignoring [-w phonycycle=warn]",
out->path().c_str());
}
}
}
Expand All @@ -403,7 +402,7 @@ bool ManifestParser::ParseEdge(string* err) {
edge->dyndep_ = state_->GetNode(dyndep, slash_bits);
edge->dyndep_->set_dyndep_pending(true);
vector<Node*>::iterator dgi =
std::find(edge->inputs_.begin(), edge->inputs_.end(), edge->dyndep_);
std::find(edge->inputs_.begin(), edge->inputs_.end(), edge->dyndep_);
if (dgi == edge->inputs_.end()) {
return lexer_.Error("dyndep '" + dyndep + "' is not an input", err);
}
Expand Down
2 changes: 1 addition & 1 deletion src/manifest_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct ManifestParser : public Parser {
std::string* err);

/// Parse various statement types.
bool ParsePool(std::string* err);
bool ParsePool(std::string* err); // wokers
bool ParseRule(std::string* err);
bool ParseLet(std::string* key, EvalString* val, std::string* err);
bool ParseEdge(std::string* err);
Expand Down
2 changes: 1 addition & 1 deletion src/ninja.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ int main(int argc, char** argv) {
#else
real_main(argc, argv);
#endif
}
}
127 changes: 127 additions & 0 deletions src/test1.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include <iostream>
#include <memory>

#include "build.h"
#include "graph.h"
#include "state.h"
#include "util.h"
using namespace std;

void CreateHelloWorldGraph(State* state) {
BindingEnv env;
env.AddBinding("in", "hello_world.cpp");
env.AddBinding("out", "hello_world");
env.AddBinding("DEP_FILE", "hello_world.d");

// Create rules
Rule* cxx_compiler = new Rule("CXX_COMPILER__hello_world_");

// Create command
EvalString command;
command.AddText("g++ -std=gnu++11 ");
command.AddSpecial("$in");
command.AddText(" -o ");
command.AddSpecial("$out");
cxx_compiler->AddBinding("command", command);

// Create description
EvalString description;
description.AddText("Building CXX object ");
description.AddSpecial("$out");
cxx_compiler->AddBinding("description", description);

// Add dependency file
EvalString depfile;
depfile.AddSpecial("$DEP_FILE");
cxx_compiler->AddBinding("depfile", depfile);

// Add dependency format
EvalString deps;
deps.AddText("gcc");
cxx_compiler->AddBinding("deps", deps);

// Add rule to state
state->bindings_.AddRule(cxx_compiler);

Rule* cxx_linker = new Rule("CXX_EXECUTABLE_LINKER__hello_world_");
EvalString link;
link.AddText("g++ ");
link.AddSpecial("$in");
link.AddText(" -o ");
link.AddSpecial("$out");
cxx_linker->AddBinding("command", link);
EvalString linkDescription;
description.AddText("Linking CXX object ");
description.AddSpecial("$out");
cxx_linker->AddBinding("description", linkDescription);
state->bindings_.AddRule(cxx_linker);

// Create nodes
Node* source_file = state->GetNode("hello_world.cpp", 0);
Node* object_file =
state->GetNode("CMakeFiles/hello_world.dir/hello_world.cpp.o", 0);
Node* executable = state->GetNode("hello_world", 0);

// Create edges
string error_message = "";
string* err_ptr = &error_message;
Edge* compile_edge = state->AddEdge(cxx_compiler);
if (!state->AddOut(compile_edge, object_file->path(), 0, err_ptr)) {
// Handle error
*err_ptr = "Failed to add output to compile edge\n";
Error(error_message.c_str());
return;
}
compile_edge->inputs_.push_back(source_file);

Edge* link_edge = state->AddEdge(cxx_linker);
if (!state->AddOut(link_edge, executable->path(), 0, err_ptr)) {
// Handle error
*err_ptr = "Failed to add output to link edge\n";
Error(error_message.c_str());
return;
}
link_edge->inputs_.push_back(object_file);

// Set up environment for compile edge
if (compile_edge->env_ == nullptr) {
compile_edge->env_ = new BindingEnv(&state->bindings_);
}
compile_edge->env_->AddBinding(
"DEP_FILE", "CMakeFiles/hello_world.dir/hello_world.cpp.o.d");

// Add default target
if (!state->AddDefault(executable->path(), err_ptr)) {
// Handle error
*err_ptr = "Failed to add default target: \n";
Error(error_message.c_str());
return;
}
}

void Error(const string& message) {
cerr << "Error: " << message << endl;
}

int main() {
State state;
// Create Graph
CreateHelloWorldGraph(&state);
// Create the Builder object
Builder builder(&state);

string err;
if (!builder.AddTarget("hello_world", &err)) {
Error("Failed to add build target: " + err);
return 1;
}

if (!builder.Build(&err)) {
Error("Build failed: " + err);
return 1;
}

cout << "Build completed successfully!" << std::endl;
return 0;
return 0;
}
14 changes: 14 additions & 0 deletions steps/hello-world-graph/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.10)

# Set the project name
project(HelloWorld)

# Specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# Add the executable
add_executable(hello_world hello_world.cpp)

# Optional: You can set the value of N here
# target_compile_definitions(hello_world PRIVATE N="CMake")
4 changes: 4 additions & 0 deletions steps/hello-world-graph/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Command for Hello World Graph
cmake -G "Ninja" -S ..
ninja
ninja -t graph | dot -Tpng -ograph.png
48 changes: 48 additions & 0 deletions steps/hello-world-graph/commands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/home/yuwei/Documents/ShadowDash/src

export LD_LIBRARY_PATH=/home/yuwei/Documents/ShadowDash/build:$LD_LIBRARY_PATH

g++ -I /home/yuwei/Documents/ShadowDash/src -L /home/yuwei/Documents/ShadowDash/build -lninja_shared -o test1 test1.cc
or
g++ -I /home/yuwei/Documents/ShadowDash/src -L /home/yuwei/Documents/ShadowDash/build -lninja_shared -Wl,-rpath,/home/yuwei/Documents/ShadowDash/build -o test1 test1.cc

g++ eval_env.cc test1.cc state.cc graph.cc -o exe.out

### SO
g++ -fPIC -shared -I../../src ../../src/build_log.cc \
../../src/build.cc \
../../src/clean.cc \
../../src/clparser.cc \
../../src/dyndep.cc \
../../src/dyndep_parser.cc \
../../src/debug_flags.cc \
../../src/deps_log.cc \
../../src/disk_interface.cc \
../../src/edit_distance.cc \
../../src/elide_middle.cc \
../../src/eval_env.cc \
../../src/graph.cc \
../../src/graphviz.cc \
../../src/json.cc \
../../src/line_printer.cc \
../../src/manifest_parser.cc \
../../src/metrics.cc \
../../src/missing_deps.cc \
../../src/parser.cc \
../../src/state.cc \
../../src/status_printer.cc \
../../src/string_piece_util.cc \
../../src/util.cc \
../../src/subprocess-posix.cc \
../../src/lexer.cc \
../../src/depfile_parser.cc \
../../src/version.cc -o libninja.so
g++ -fPIC test1.cc -I../../src -L./ -lninja -Wl,-rpath=. -o test1

### For debug
g++ -g test1.cc -I../../src -L./ -lninja -Wl,-rpath=. -o test1
gdb ./test1
sharedlibrary libninja.so
run
backtrace
break xx
Binary file added steps/hello-world-graph/graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions steps/hello-world-graph/hello_world.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>

#ifndef N
#define N "World"
#endif

int main() {
printf("Hello %s! \n", N);
return 0;
}
Binary file added steps/hello-world-graph/libninja.so
Binary file not shown.
Loading

0 comments on commit ace523a

Please sign in to comment.