Skip to content

Commit d365fc0

Browse files
authored
Add k2-lib mode (#1385)
1 parent e10c755 commit d365fc0

15 files changed

+59
-32
lines changed

compiler/code-gen/const-globals-batched-mem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ void GlobalsMemAllocation::compile(CodeGenerator &W) const {
301301
W << "// count(php global scope) = " << mem.count_of_php_global_scope << NL;
302302
W << "// n_batches = " << mem.batches.size() << NL;
303303

304-
if (!G->is_output_mode_lib()) {
304+
if (!G->is_output_mode_lib() && !G->is_output_mode_k2_lib()) {
305305
W << "php_globals.once_alloc_linear_mem(" << mem.total_mem_size << ");" << NL;
306306
} else {
307307
W << "php_globals.once_alloc_linear_mem(\"" << G->settings().static_lib_name.get() << "\", " << mem.total_mem_size << ");" << NL;
@@ -331,7 +331,7 @@ void PhpMutableGlobalsConstRefArgument::compile(CodeGenerator &W) const {
331331
void GlobalVarInPhpGlobals::compile(CodeGenerator &W) const {
332332
if (global_var->is_builtin_runtime) {
333333
W << "php_globals.get_superglobals().v$" << global_var->name;
334-
} else if (!G->is_output_mode_lib()) {
334+
} else if (!G->is_output_mode_lib() && !G->is_output_mode_k2_lib()) {
335335
W << "(*reinterpret_cast<" << type_out(tinf::get_type(global_var)) << "*>(php_globals.mem()+" << global_var->offset_in_linear_mem << "))";
336336
} else {
337337
W << "(*reinterpret_cast<" << type_out(tinf::get_type(global_var)) << "*>(php_globals.mem_for_lib(\"" << G->settings().static_lib_name.get() << "\")+" << global_var->offset_in_linear_mem << "))";

compiler/code-gen/files/const-vars-init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void ConstVarsInit::compile_const_init_part(CodeGenerator &W, const ConstantsBat
5959
IncludesCollector includes;
6060
ConstantsExternCollector c_mem_extern;
6161
for (VarPtr var : batch.constants) {
62-
if (!G->is_output_mode_lib()) {
62+
if (!G->is_output_mode_lib() && !G->is_output_mode_k2_lib()) {
6363
includes.add_var_signature_depends(var);
6464
includes.add_vertex_depends(var->init_val);
6565
}

compiler/code-gen/files/init-scripts.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct StaticInit {
2020
};
2121

2222
void StaticInit::compile(CodeGenerator &W) const {
23-
if (G->is_output_mode_lib()) {
23+
if (G->is_output_mode_lib() || G->is_output_mode_k2_lib()) {
2424
return;
2525
}
2626

@@ -220,7 +220,7 @@ void InitScriptsCpp::compile(CodeGenerator &W) const {
220220

221221
W << NL << StaticInit() << NL;
222222

223-
if (G->is_output_mode_lib()) {
223+
if (G->is_output_mode_lib() || G->is_output_mode_k2_lib()) {
224224
W << LibRunFunction(main_file_id->main_function);
225225
W << CloseFile();
226226
return;

compiler/compiler-core.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ void CompilerCore::register_settings(CompilerSettings *settings) {
139139
output_mode = OutputMode::k2_oneshot;
140140
} else if (mode == "k2-multishot") {
141141
output_mode = OutputMode::k2_multishot;
142+
} else if (mode == "k2-lib") {
143+
output_mode = OutputMode::k2_lib;
142144
} else {
143145
output_mode = OutputMode::server;
144146
}

compiler/compiler-core.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum class OutputMode {
2828
server, // -M server
2929
cli, // -M cli
3030
lib, // -M lib
31+
k2_lib, // -M k2-lib
3132
k2_cli, // -M k2-cli
3233
k2_server, // -M k2-server
3334
k2_oneshot, // -M k2-oneshot
@@ -202,6 +203,10 @@ class CompilerCore {
202203
return output_mode == OutputMode::lib;
203204
}
204205

206+
bool is_output_mode_k2_lib() const {
207+
return output_mode == OutputMode::k2_lib;
208+
}
209+
205210
bool is_output_mode_k2_cli() const {
206211
return output_mode == OutputMode::k2_cli;
207212
}
@@ -219,7 +224,7 @@ class CompilerCore {
219224
}
220225

221226
bool is_output_mode_k2() const {
222-
return is_output_mode_k2_cli() || is_output_mode_k2_server() || is_output_mode_k2_oneshot() || is_output_mode_k2_multishot();
227+
return is_output_mode_k2_cli() || is_output_mode_k2_server() || is_output_mode_k2_oneshot() || is_output_mode_k2_multishot() || is_output_mode_k2_lib();
223228
}
224229

225230
void set_exclude_namespaces(std::vector<std::string> &&excludes) {

compiler/compiler-settings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ void CompilerSettings::init() {
251251
kphp_error(is_k2_mode, "Option \"k2-component-name\" is only available for k2 component modes");
252252
}
253253

254-
if (mode.get() == "lib") {
254+
if (mode.get() == "lib" || mode.get() == "k2-lib") {
255255
if (!tl_schema_file.get().empty()) {
256256
throw std::runtime_error{"Option " + tl_schema_file.get_env_var() + " is forbidden for static lib mode"};
257257
}

compiler/kphp2cpp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ int main(int argc, char *argv[]) {
217217
"tracked-builtins-list", "KPHP_TRACKED_BUILTINS_LIST");
218218
parser.add("File with kphp runtime sha256 hash", settings->runtime_sha256_file,
219219
"runtime-sha256", "KPHP_RUNTIME_SHA256", "${KPHP_PATH}/objs/php_lib_version.sha256");
220-
parser.add("The output binary type: server, cli, lib, k2-cli, k2-server, k2-oneshot, k2-multishot", settings->mode, 'M', "mode", "KPHP_MODE", "server",
221-
{"server", "cli", "lib", "k2-cli", "k2-server", "k2-oneshot", "k2-multishot"});
220+
parser.add("The output binary type: server, cli, lib, k2-lib, k2-cli, k2-server, k2-oneshot, k2-multishot", settings->mode, 'M', "mode", "KPHP_MODE", "server",
221+
{"server", "cli", "lib", "k2-lib", "k2-cli", "k2-server", "k2-oneshot", "k2-multishot"});
222222
parser.add("A runtime library for building the output binary", settings->link_file,
223223
'l', "link-with", "KPHP_LINK_FILE");
224224
parser.add("Build runtime from sources", settings->force_link_runtime,

compiler/make/make.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -538,9 +538,10 @@ static std::vector<File *> run_pre_make(OutputMode output_mode, const CompilerSe
538538
}
539539

540540
auto lib_header_dirs = collect_imported_headers();
541-
auto targets = output_mode == OutputMode::lib ? kphp_make_static_lib_target(obj_index, G->get_index(), lib_header_dirs, make)
542-
: kphp_make_target(obj_index, G->get_index(), lib_header_dirs, make);
543-
for (File * file : targets) {
541+
auto targets = vk::any_of_equal(output_mode, OutputMode::lib, OutputMode::k2_lib)
542+
? kphp_make_static_lib_target(obj_index, G->get_index(), lib_header_dirs, make)
543+
: kphp_make_target(obj_index, G->get_index(), lib_header_dirs, make);
544+
for (File* file : targets) {
544545
response.push_back(file);
545546
}
546547

@@ -569,8 +570,7 @@ void run_make() {
569570
auto objs = run_pre_make(output_mode, settings, make_stats_file, make, obj_index, bin_file, obj_rt_index);
570571
stage::die_if_global_errors();
571572

572-
if (G->is_output_mode_lib()) {
573-
// todo:k2 think about kphp libraries
573+
if (G->is_output_mode_lib() || G->is_output_mode_k2_lib()) {
574574
make.create_objs2static_lib_target(objs, &bin_file);
575575
} else if (G->is_output_mode_k2()) {
576576
make.create_objs2k2_component_target(objs, &bin_file);
@@ -584,8 +584,10 @@ void run_make() {
584584
}
585585
stage::die_if_global_errors();
586586

587-
const std::string build_stage{output_mode == OutputMode::lib ? "Compiling" : "Linking"};
588-
stage::set_exit_code(output_mode == OutputMode::lib ? ExitCode::CPP_TO_OBJS_STAGE : ExitCode::OBJS_TO_BINARY_STAGE);
587+
const bool is_lib = vk::any_of_equal(output_mode, OutputMode::lib, OutputMode::k2_lib);
588+
589+
const std::string build_stage{is_lib ? "Compiling" : "Linking"};
590+
stage::set_exit_code(is_lib ? ExitCode::CPP_TO_OBJS_STAGE : ExitCode::OBJS_TO_BINARY_STAGE);
589591
AutoProfiler profiler{get_profiler(build_stage)};
590592

591593
bool ok = make.make_target(&bin_file, build_stage, settings.jobs_count.get());
@@ -610,7 +612,7 @@ void run_make() {
610612
if (!settings.user_binary_path.get().empty()) {
611613
hard_link_or_copy(bin_file.path, settings.user_binary_path.get());
612614
}
613-
if (output_mode == OutputMode::lib) {
615+
if (vk::any_of_equal(output_mode, OutputMode::lib, OutputMode::k2_lib)) {
614616
copy_static_lib_to_out_dir(std::move(bin_file));
615617
}
616618
}

compiler/pipes/calc-bad-vars.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ class CalcBadVars {
628628
function->dep = std::move(call_graph.graph[function]);
629629
}
630630

631-
if (!G->is_output_mode_lib()) {
631+
if (!G->is_output_mode_lib() && !G->is_output_mode_k2_lib()) {
632632
return;
633633
}
634634

compiler/pipes/calc-empty-functions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void CalcEmptyFunctions::execute(FunctionPtr f, DataStream<FunctionPtr> &os) {
9090
stage::set_function(f);
9191
f->body_seq = calc_function_body_type(f);
9292

93-
if (f->is_main_function() && G->is_output_mode_lib() && G->get_main_file()->main_function == f) {
93+
if (f->is_main_function() && (G->is_output_mode_lib() || G->is_output_mode_k2_lib()) && G->get_main_file()->main_function == f) {
9494
kphp_error(f->body_seq == FunctionData::body_value::empty, "main php file of a lib mustn't contain code, only declarations");
9595
}
9696

0 commit comments

Comments
 (0)