Skip to content

Commit

Permalink
用户自定义规则支持多行命令了
Browse files Browse the repository at this point in the history
  • Loading branch information
duyanning committed Jul 29, 2019
1 parent f885519 commit 8c524d6
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 4 deletions.
18 changes: 18 additions & 0 deletions README.chinese.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,24 @@ include-dir = F:\vcpkg\installed\x86-windows\include
lib-dir = F:\vcpkg\installed\x86-windows\lib
dll-dir = F:\vcpkg\installed\x86-windows\bin
```
## 内嵌的Make
现实中的C++工程,源代码除了C++的.cpp跟.h外,还会涉及到其他一些类型的文件。

比如Qt的界面描述文件.ui,FLTK的界面描述文件.fl。
这些文件经过编译之后,会产生.cpp或.h文件。
除此之外,Qt著名的moc还会从.h生成.cpp。
对于这类千变万化的东西,cpps提供了嵌入式的make脚本。
```c++
#include "finddialog.h" // usingcpp
// using nocheck finddialog.h.moc.cpp
/* cpps-make
finddialog.h.moc.cpp : finddialog.h
moc finddialog.h -o finddialog.h.moc.cpp
*/
```
`using`指令后的`nocheck`告诉cpps,这个叫finddialog.h.moc.cpp的文件是build阶段生成出来的,即便扫描阶段不存在,也不要报错。

`cpps-make`指令引入一条用户自定义规则,语法同Makefile,当然,不会有普通Makefile那么完备。具体请参考examples里fltk跟qt的例子。

## 其他
QQ交流群:492455203
1 change: 1 addition & 0 deletions examples/hello-qt-moc/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

// using nocheck finddialog.h.moc.cpp
// cpps-make finddialog.h.moc.cpp : finddialog.h // moc finddialog.h -o finddialog.h.moc.cpp
// 上面这是单行make。只支持一条命令。命令写在//之后

int main(int argc, char *argv[])
{
Expand Down
8 changes: 5 additions & 3 deletions examples/hello-qt-uic/main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include <QApplication>
#include <QDialog>

#include "gotocelldialog.h"

// cpps-make ui_gotocelldialog.h : gotocelldialog.ui // uic gotocelldialog.ui -o gotocelldialog.h
#include "ui_gotocelldialog.h"

// vc-extra-compile-flags: -IF:\vcpkg\installed\x86-windows\include\QtWidgets

/* cpps-make
ui_gotocelldialog.h : gotocelldialog.ui
uic gotocelldialog.ui -o gotocelldialog.h
del ui_gotocelldialog.h > NUL 2>&1
ren gotocelldialog.h ui_gotocelldialog.h
*/
// 上面这是多行make。但这个例子根本不需要这么复杂,用单行make即可。我只是为了举个例子。//之后那个-是为了避免cpps将其当作指令
//- cpps-make ui_gotocelldialog.h : gotocelldialog.ui // uic gotocelldialog.ui -o ui_gotocelldialog.h

int main(int argc, char *argv[])
{
Expand Down
File renamed without changes.
50 changes: 50 additions & 0 deletions src/UserDefinedRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,53 @@ void process_user_defined_rule(fs::path src_path, string rule_string, InfoPackag

}

void process_user_defined_rule_multi_lines(fs::path src_path, string dependency_relationship,
vector<string> commands, InfoPackageScanned& pack)
{
// 分离dependency relationship中的targets与prerequisites
auto colon_pos = dependency_relationship.find(":");
string targets = dependency_relationship.substr(0, colon_pos);
boost::algorithm::trim(targets);
string prerequisites = dependency_relationship.substr(colon_pos + 1);
boost::algorithm::trim(prerequisites);
//cout << targets << endl;
//cout << prerequisites << endl;

// 分离dependency relationship中的各个target
vector<string> target_vector;
std::string target_delimiters(" "); // 支持多个分界符
boost::split(target_vector, targets, boost::is_any_of(target_delimiters), boost::token_compress_on); // token_compress_on是为了将单词之间的多个空格视为一个
//for (auto t : target_vector) {
// cout << ">>>" << t << "<<<" << endl;
//}

// 分离prerequisites中的各个prerequisite
vector<string> prerequisite_vector;
std::string prerequisite_delimiters(" "); // 支持多个分界符
boost::split(prerequisite_vector, prerequisites, boost::is_any_of(prerequisite_delimiters), boost::token_compress_on); // token_compress_on是为了将单词之间的多个空格视为一个
//for (auto p : prerequisite_vector) {
// cout << ">>>" << p << "<<<" << endl;
//}

UserDefinedRule rule;
rule.dir = src_path;
rule.dir.remove_filename();

for (auto t : target_vector) {
fs::path a = rule.dir;
a /= t;
rule.targets.push_back(a);
}

for (auto p : prerequisite_vector) {
fs::path a = rule.dir;
a /= p;
rule.prerequisites.push_back(a);
}


rule.commands = commands;

pack.user_defined_rules.push_back(rule);

}
2 changes: 2 additions & 0 deletions src/UserDefinedRule.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ struct UserDefinedRule {
};

void process_user_defined_rule(fs::path src_path, string rule, InfoPackageScanned& pack);
void process_user_defined_rule_multi_lines(fs::path src_path, string dependency_relationship,
vector<string> commands, InfoPackageScanned& pack);

#endif // USERDEFINEDRULE_H
19 changes: 18 additions & 1 deletion src/scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ void scan(fs::path src_path, InfoPackageScanned& pack)
// cpps-before-link shell命令 在链接之前执行的操作
// cpps-after-link shell命令 在链接之后执行的操作

regex user_defined_rule_pat{ R"(//\scpps-make\s+(.+\w))" };
regex user_defined_rule_pat{ R"(//\scpps-make\s+(.+\w))" }; // 单行
regex user_defined_rule_multi_lines_pat{R"(/\* cpps-make)"};

int n = 0;
while (getline(in, line)) {
Expand Down Expand Up @@ -186,7 +187,23 @@ void scan(fs::path src_path, InfoPackageScanned& pack)
process_user_defined_rule(src_path, matches[1], pack);

}
else if (regex_search(line, matches, user_defined_rule_multi_lines_pat)) {
//cout << matches[1] << endl;
string dependency_relationship;
getline(in, dependency_relationship);

vector<string> commands;
string c;
getline(in, c);
boost::algorithm::trim(c);
while (c != "*/") {
commands.push_back(c);
getline(in, c);
boost::algorithm::trim(c);
}
process_user_defined_rule_multi_lines(src_path, dependency_relationship, commands, pack);

}

if (max_line_scan != -1) {
n++;
Expand Down

0 comments on commit 8c524d6

Please sign in to comment.