Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ cl /nologo ^
/std:c++latest /W4 /MDd /EHsc ^
/reference "%modules_dir%\std.ifc" ^
/reference "%modules_dir%\std.compat.ifc" ^
/c /interface /TP "%root_dir%src\dylib.cppm" > NUL
/c /interface /TP "%root_dir%src\dylib.cppm"
popd

if %ERRORLEVEL% neq 0 (
Expand All @@ -132,7 +132,7 @@ cl /nologo ^
/std:c++latest /W4 /MDd /EHsc ^
/reference "%modules_dir%\std.ifc" ^
/reference "%modules_dir%\std.compat.ifc" ^
/c /interface /TP "%root_dir%src\nlohmann.json.cppm" > NUL
/c /interface /TP "%root_dir%src\nlohmann.json.cppm"
popd

if %ERRORLEVEL% neq 0 (
Expand All @@ -147,7 +147,7 @@ cl /nologo ^
-I"%cppfront_include_dir%" ^
/reference "%modules_dir%\std.ifc" ^
/reference "%modules_dir%\std.compat.ifc" ^
/c /interface /TP "%root_dir%src\cpp2b_build_info_parser.cppm" > NUL
/c /interface /TP "%root_dir%src\cpp2b_build_info_parser.cppm"
popd

if %ERRORLEVEL% neq 0 (
Expand Down
1 change: 1 addition & 0 deletions build.cpp2
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import cpp2b.build;

cppfront :== "v0.8.1";
compiler :== cpp2b::compiler_choice::preferred;

build: (inout b: cpp2b::build) -> void = {
_ = b.cpp1_module().source_path("src/dylib.cppm");
Expand Down
12 changes: 11 additions & 1 deletion share/cpp2b/cpp2b.build.cppm.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module;
export module cpp2b.build;

import std;
export import cpp2b;

struct cpp2b_detail_build_impl;
struct cpp2b_detail_git_repo_impl;
Expand Down Expand Up @@ -93,6 +94,14 @@ CPP2B_BUILD_DECL_FN(
);

export namespace cpp2b {

enum class compiler_choice {
preferred,
msvc,
gcc,
clang
};

class git_repo {
std::shared_ptr<cpp2b_detail_git_repo_impl> impl;

Expand Down Expand Up @@ -147,7 +156,7 @@ public:
std::source_location caller_srcloc = std::source_location::current()
) -> cpp1_module {
CPP2B_BUILD_FN_CHECK(cpp2b_detail_cpp1_module_include_directory);
(*cpp2b_detail_cpp1_module_include_directory)(impl.get(), p, caller_srcloc);
(*cpp2b_detail_cpp1_module_include_directory)(impl.get(), p, caller_srcloc);
return *this;
}

Expand Down Expand Up @@ -228,3 +237,4 @@ CPP2B_BUILD_API void cpp2b_detail_build(cpp2b_detail_build_impl* impl) {
cpp2b::build b(impl);
::build(b);
}

5 changes: 2 additions & 3 deletions share/cpp2b/cpp2b.cppm.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module;

#ifdef _MSC_VER
# include <Windows.h>
# include <stdlib.h>
#else
# include <stdlib.h>
# include <unistd.h>
Expand All @@ -12,9 +13,7 @@ export module cpp2b;
import std;
import std.compat;

#ifdef _MSC_VER
extern char** _environ;
#else
#if !defined(_MSC_VER)
extern "C" char** environ;
#endif

Expand Down
95 changes: 77 additions & 18 deletions src/cpp2b_build_info_parser.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,65 @@ source_info parse_source(const std::string& filename) {
result.kind = source_kind::build;
// Extract constants
for(size_t j = 0; j < all_tokens.size(); ++j) {
auto name = all_tokens[j].as_string_view();

// Specific handling for 'compiler :== ...'
if(name == "compiler" && j + 2 < all_tokens.size() &&
all_tokens[j + 1].type() == cpp2::lexeme::Colon &&
all_tokens[j + 2].type() == cpp2::lexeme::EqualComparison) {
size_t k = j + 3;
auto match_path = [&](const std::vector<std::string_view>& parts) {
size_t cur = k;
for(size_t i = 0; i < parts.size(); ++i) {
if(cur >= all_tokens.size()) {
return false;
}
if(all_tokens[cur].as_string_view() != parts[i]) {
return false;
}
cur++;
if(i < parts.size() - 1) {
if(cur >= all_tokens.size()) {
return false;
}
if(all_tokens[cur].as_string_view() == "::") {
cur++;
} else if(cur + 1 < all_tokens.size() &&
all_tokens[cur].as_string_view() == ":" &&
all_tokens[cur + 1].as_string_view() == ":") {
cur += 2;
} else {
return false;
}
}
}
k = cur;
return true;
};

// Case 1: compiler :== cpp2b::compiler_choice::<value>;
if(match_path({"cpp2b", "compiler_choice"})) {
bool has_scope = false;
if(k < all_tokens.size() && all_tokens[k].as_string_view() == "::") {
k++;
has_scope = true;
} else if(k + 1 < all_tokens.size() &&
all_tokens[k].as_string_view() == ":" &&
all_tokens[k + 1].as_string_view() == ":") {
k += 2;
has_scope = true;
}

if(has_scope && k < all_tokens.size() &&
all_tokens[k].type() == cpp2::lexeme::Identifier) {
result.constants["compiler"] =
std::string(all_tokens[k].as_string_view());
j = k;
continue;
}
}
}

// identifier == string_literal ;
if(j + 3 < all_tokens.size() &&
all_tokens[j].type() == cpp2::lexeme::Identifier &&
Expand All @@ -78,26 +137,26 @@ source_info parse_source(const std::string& filename) {
j += 3;
}
// identifier :== string_literal ;
else if (j + 3 < all_tokens.size() &&
all_tokens[j].type() == cpp2::lexeme::Identifier &&
all_tokens[j+1].type() == cpp2::lexeme::Colon &&
all_tokens[j+2].type() == cpp2::lexeme::EqualComparison &&
all_tokens[j+3].type() == cpp2::lexeme::StringLiteral) {
auto name = all_tokens[j].as_string_view();
auto value_raw = all_tokens[j+3].as_string_view();
if (value_raw.size() >= 2) {
auto value = std::string(value_raw.substr(1, value_raw.size() - 2));
result.constants[std::string(name)] = value;
}
j += 3;
else if(j + 3 < all_tokens.size() &&
all_tokens[j].type() == cpp2::lexeme::Identifier &&
all_tokens[j + 1].type() == cpp2::lexeme::Colon &&
all_tokens[j + 2].type() == cpp2::lexeme::EqualComparison &&
all_tokens[j + 3].type() == cpp2::lexeme::StringLiteral) {
auto name = all_tokens[j].as_string_view();
auto value_raw = all_tokens[j + 3].as_string_view();
if(value_raw.size() >= 2) {
auto value = std::string(value_raw.substr(1, value_raw.size() - 2));
result.constants[std::string(name)] = value;
}
j += 3;
}
// identifier : type == string_literal ;
else if (j + 5 < all_tokens.size() &&
all_tokens[j].type() == cpp2::lexeme::Identifier &&
all_tokens[j+1].type() == cpp2::lexeme::Colon &&
all_tokens[j+3].type() == cpp2::lexeme::EqualComparison &&
all_tokens[j+4].type() == cpp2::lexeme::StringLiteral &&
all_tokens[j+5].type() == cpp2::lexeme::Semicolon) {
else if(j + 5 < all_tokens.size() &&
all_tokens[j].type() == cpp2::lexeme::Identifier &&
all_tokens[j + 1].type() == cpp2::lexeme::Colon &&
all_tokens[j + 3].type() == cpp2::lexeme::EqualComparison &&
all_tokens[j + 4].type() == cpp2::lexeme::StringLiteral &&
all_tokens[j + 5].type() == cpp2::lexeme::Semicolon) {
auto name = all_tokens[j].as_string_view();
auto value_raw = all_tokens[j + 4].as_string_view();
if(value_raw.size() >= 2) {
Expand Down
Loading
Loading