Skip to content
Draft
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
8 changes: 8 additions & 0 deletions compiler/code-gen/files/init-scripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ void StaticInit::compile(CodeGenerator &W) const {
W << END << NL << NL;
}

if (G->is_output_mode_k2()) {
W << "namespace kphp::compiler_interface {" << NL;
FunctionSignatureGenerator(W) << ("std::string_view get_main_file_name() ") << BEGIN;
Copy link
Contributor

@apolyakov apolyakov Apr 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's make this function constexpr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang++ gives error with message that inline function ... is not defined

W << "return {\"" << G->get_main_file()->short_file_name << ".php\"};";
W << END << NL;
W << "}" << NL;
}

FunctionSignatureGenerator(W) << ("void init_php_scripts_once_in_master() ") << BEGIN;

if (!G->settings().tl_schema_file.get().empty()) {
Expand Down
13 changes: 13 additions & 0 deletions runtime-light/core/compiler-info/compiler_interface.h
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please, considering using - instead of _ in file names

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2025 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#pragma once

#include <string_view>

namespace kphp::compiler_interface {

std::string_view get_main_file_name() noexcept;

} // namespace kphp::compiler_interface
39 changes: 39 additions & 0 deletions runtime-light/state/component-state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "runtime-light/state/component-state.h"

#include <cctype>
#include <cstring>
#include <iterator>
#include <string_view>
Expand All @@ -13,6 +14,7 @@
#include "runtime-common/core/runtime-core.h"
#include "runtime-common/core/utils/kphp-assert-core.h"
#include "runtime-common/stdlib/serialization/json-functions.h"
#include "runtime-light/core/compiler-info/compiler_interface.h"
#include "runtime-light/k2-platform/k2-api.h"

void ComponentState::parse_env() noexcept {
Expand Down Expand Up @@ -54,6 +56,40 @@ void ComponentState::parse_runtime_config_arg(std::string_view value_view) noexc
}
}

void ComponentState::parse_command_line_arg(std::string_view value_view) noexcept {
if (value_view.empty()) [[unlikely]] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to have warning here

php_warning("command line argument is empty");
return;
}

if (!command_line_argv.empty()) [[unlikely]] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check should be placed at the top of the function

php_warning("command line argument support no more one usage");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird message, I'd suggest multiple command line arguments strings specified. skipping or something similar

return;
}

const auto& main_file_view{kphp::compiler_interface::get_main_file_name()};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think reference is redundant here

command_line_argv.push_back(string(main_file_view.data(), main_file_view.size()));

bool in_quote{};
string current_arg{};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like an error. You need to set special reference counter for each reference counted object

for (char letter : value_view) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not actually letter since there might be other symbols

if (std::isspace(letter) && !in_quote && !current_arg.empty()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks overcomplicated. Let's use standard string view functions instead

command_line_argv.push_back(std::move(current_arg));
current_arg = string();
} else if (letter == '"') {
in_quote = !in_quote;
} else if (letter == '\'') {
php_warning("in command line arg supported only \" quote");
Copy link
Contributor

@Danil42Russia Danil42Russia Apr 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you added an extra space \" quote"

} else {
current_arg.push_back(letter);
}
}

if (!current_arg.empty()) {
command_line_argv.push_back(std::move(current_arg));
}
}

void ComponentState::parse_args() noexcept {
for (auto i = 0; i < argc; ++i) {
const auto [arg_key, arg_value]{k2::arg_fetch(i)};
Expand All @@ -64,10 +100,13 @@ void ComponentState::parse_args() noexcept {
parse_ini_arg(key_view, value_view);
} else if (key_view == RUNTIME_CONFIG_ARG) {
parse_runtime_config_arg(value_view);
} else if (key_view == COMMAND_LINE_ARG) {
parse_command_line_arg(value_view);
} else {
php_warning("unknown argument: %s", key_view.data());
}
}
command_line_argv.set_reference_counter_to(ExtraRefCnt::for_global_const);
runtime_config.set_reference_counter_to(ExtraRefCnt::for_global_const);
ini_opts.set_reference_counter_to(ExtraRefCnt::for_global_const);
}
4 changes: 4 additions & 0 deletions runtime-light/state/component-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct ComponentState final : private vk::not_copyable {
const uint32_t argc;
const uint32_t envc;
mixed runtime_config;
array<string> command_line_argv;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cli_argv

array<string> ini_opts;
array<mixed> env;

Expand All @@ -43,6 +44,7 @@ struct ComponentState final : private vk::not_copyable {

private:
static constexpr std::string_view INI_ARG_PREFIX = "ini ";
static constexpr std::string_view COMMAND_LINE_ARG = "command-line";
Copy link
Contributor

@apolyakov apolyakov Apr 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's define it as CLI_ARG = "cli"

static constexpr std::string_view RUNTIME_CONFIG_ARG = "runtime-config";
static constexpr auto INIT_COMPONENT_ALLOCATOR_SIZE = static_cast<size_t>(512U * 1024U); // 512KB

Expand All @@ -53,4 +55,6 @@ struct ComponentState final : private vk::not_copyable {
void parse_ini_arg(std::string_view, std::string_view) noexcept;

void parse_runtime_config_arg(std::string_view) noexcept;

void parse_command_line_arg(std::string_view) noexcept;
};
6 changes: 4 additions & 2 deletions runtime-light/state/init-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "runtime-light/server/http/init-functions.h"
#include "runtime-light/server/init-functions.h"
#include "runtime-light/server/job-worker/job-worker-server-state.h"
#include "runtime-light/state/component-state.h"
#include "runtime-light/state/instance-state.h"
#include "runtime-light/streams/streams.h"
#include "runtime-light/tl/tl-core.h"
Expand Down Expand Up @@ -44,8 +45,9 @@ kphp::coro::task<uint64_t> init_kphp_cli_component() noexcept {
{ // TODO superglobals init
auto& superglobals{InstanceState::get().php_script_mutable_globals_singleton.get_superglobals()};
using namespace PhpServerSuperGlobalIndices;
superglobals.v$argc = static_cast<int64_t>(0);
superglobals.v$argv = array<mixed>{};
const auto& command_line_argv{ComponentState::get().command_line_argv};
superglobals.v$argv = command_line_argv;
superglobals.v$argc = command_line_argv.size().size;
superglobals.v$_SERVER.set_value(string{ARGC.data(), ARGC.size()}, superglobals.v$argc);
superglobals.v$_SERVER.set_value(string{ARGV.data(), ARGV.size()}, superglobals.v$argv);
superglobals.v$_SERVER.set_value(string{PHP_SELF.data(), PHP_SELF.size()}, string{});
Expand Down
Loading