2018-10-02 14:28:42 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
function disjoint_set_find(&$parents, $x) {
|
|
|
|
if ($parents[$x] !== $x) {
|
|
|
|
return $parents[$x] = disjoint_set_find($parents, $parents[$x]);
|
|
|
|
}
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
|
|
|
|
function disjoint_set_union(&$parents, $x, $y) {
|
|
|
|
$x = disjoint_set_find($parents, $x);
|
|
|
|
$y = disjoint_set_find($parents, $y);
|
|
|
|
|
|
|
|
if ($x !== $y) {
|
|
|
|
if (rand(0, 1) == 0) {
|
|
|
|
$parents[$x] = $y;
|
|
|
|
} else {
|
|
|
|
$parents[$y] = $x;
|
2018-10-28 18:30:47 +01:00
|
|
|
}
|
2018-10-02 14:28:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function split_file($file, $chunks, $undo) {
|
|
|
|
$cpp_name = "$file.cpp";
|
|
|
|
|
|
|
|
echo "Processing file $cpp_name".PHP_EOL;
|
|
|
|
|
|
|
|
$new_files = array();
|
|
|
|
foreach (range(0, $chunks - 1) as $n) {
|
|
|
|
$new_files[] = "$file$n.cpp";
|
|
|
|
}
|
|
|
|
|
|
|
|
$is_generated = (strpos($file, 'td/generate/') === 0);
|
|
|
|
|
|
|
|
$cmake_file = $is_generated ? 'td/generate/CMakeLists.txt' : 'CMakeLists.txt';
|
|
|
|
$cmake = file_get_contents($cmake_file);
|
|
|
|
|
|
|
|
$cmake_cpp_name = $cpp_name;
|
|
|
|
$cmake_new_files = $new_files;
|
|
|
|
if ($is_generated) {
|
|
|
|
foreach ($cmake_new_files as &$file_ref) {
|
2020-06-13 19:22:23 +02:00
|
|
|
$file_ref = str_replace('td/generate/auto/td', '${TD_AUTO_INCLUDE_DIR}', $file_ref);
|
2018-10-02 14:28:42 +02:00
|
|
|
}
|
2020-06-13 19:22:23 +02:00
|
|
|
$cmake_cpp_name = str_replace('td/generate/auto/td', '${TD_AUTO_INCLUDE_DIR}', $cmake_cpp_name);
|
2018-10-02 14:28:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($undo) {
|
|
|
|
foreach ($new_files as $file) {
|
|
|
|
if (file_exists($file)) {
|
|
|
|
echo "Unlinking ".$file.PHP_EOL;
|
|
|
|
unlink($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($cmake, $cmake_cpp_name) === false) {
|
|
|
|
$cmake = str_replace(implode(PHP_EOL.' ', $cmake_new_files), $cmake_cpp_name, $cmake);
|
|
|
|
file_put_contents($cmake_file, $cmake);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($cmake, $cmake_cpp_name) !== false) {
|
|
|
|
$cmake = str_replace($cmake_cpp_name, implode(PHP_EOL.' ', $cmake_new_files), $cmake);
|
|
|
|
file_put_contents($cmake_file, $cmake);
|
|
|
|
}
|
|
|
|
|
2018-11-02 16:30:00 +01:00
|
|
|
if (!file_exists($cpp_name)) {
|
|
|
|
echo "ERROR: skip unexisting file $cpp_name".PHP_EOL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-02 14:28:42 +02:00
|
|
|
$lines = file($cpp_name);
|
|
|
|
$depth = 0;
|
|
|
|
$target_depth = 1 + $is_generated;
|
|
|
|
$is_static = false;
|
|
|
|
$in_define = false;
|
2021-02-12 15:27:07 +01:00
|
|
|
$in_comment = false;
|
2018-10-02 14:28:42 +02:00
|
|
|
$current = '';
|
|
|
|
$common = '';
|
|
|
|
$functions = array();
|
|
|
|
$namespace_begin = '';
|
|
|
|
$namespace_end = '';
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$add_depth = strpos($line, 'namespace ') === 0 ? 1 : (strpos($line, '} // namespace') === 0 ? -1 : 0);
|
|
|
|
if ($add_depth) {
|
|
|
|
# namespace begin/end
|
2019-04-26 15:33:39 +02:00
|
|
|
if ($add_depth > 0) {
|
|
|
|
$depth += $add_depth;
|
|
|
|
}
|
2018-10-02 14:28:42 +02:00
|
|
|
if ($depth <= $target_depth) {
|
|
|
|
if ($add_depth > 0) {
|
|
|
|
$namespace_begin .= $line;
|
|
|
|
} else {
|
|
|
|
$namespace_end .= $line;
|
|
|
|
}
|
|
|
|
}
|
2019-04-26 15:33:39 +02:00
|
|
|
if ($add_depth < 0) {
|
|
|
|
$depth += $add_depth;
|
|
|
|
}
|
2018-10-02 14:28:42 +02:00
|
|
|
if ($is_static) {
|
|
|
|
$common .= $current;
|
|
|
|
} else {
|
|
|
|
$functions[] = $current;
|
|
|
|
}
|
|
|
|
$common .= $line;
|
|
|
|
$current = '';
|
|
|
|
$is_static = false;
|
|
|
|
$in_define = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($line, '#undef') === 0 && !trim($current)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-02-12 15:27:07 +01:00
|
|
|
if ($in_comment && strpos($line, '*/') === 0) {
|
|
|
|
$in_comment = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strpos($line, '/*') === 0) {
|
|
|
|
$in_comment = true;
|
|
|
|
}
|
|
|
|
if ($in_comment) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-02 14:28:42 +02:00
|
|
|
if ($depth !== $target_depth) {
|
|
|
|
$common .= $line;
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-28 18:30:47 +01:00
|
|
|
|
2018-10-02 14:28:42 +02:00
|
|
|
if (strpos($line, 'static ') === 0 && $depth === $target_depth) {
|
|
|
|
$is_static = true;
|
|
|
|
}
|
|
|
|
if (!trim($current) && strpos($line, '#define ') === 0) {
|
|
|
|
$is_static = true;
|
|
|
|
$in_define = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$current .= $line;
|
|
|
|
if ((strpos($line, '}') === 0 || ($in_define && !trim($line)) || preg_match('/^[a-z].*;\s*$/i', $line)) && $depth === $target_depth) {
|
|
|
|
# block end
|
|
|
|
if ($is_static) {
|
|
|
|
$common .= $current;
|
|
|
|
} else {
|
|
|
|
$functions[] = $current;
|
|
|
|
}
|
|
|
|
$current = '';
|
|
|
|
$is_static = false;
|
|
|
|
$in_define = false;
|
|
|
|
}
|
|
|
|
}
|
2021-02-23 17:52:57 +01:00
|
|
|
$current = trim($current);
|
|
|
|
if (!empty($current)) {
|
2018-10-02 14:28:42 +02:00
|
|
|
fwrite(STDERR, "ERROR: $current".PHP_EOL);
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($functions) < $chunks) {
|
|
|
|
fwrite(STDERR, "ERROR: file is too small to be splitted more".PHP_EOL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$deps = array(); // all functions from the same subarray must be in the same file
|
|
|
|
$parents = array();
|
|
|
|
foreach ($functions as $i => $f) {
|
|
|
|
if (preg_match_all('/(?J)(create_handler|create_net_actor)<(?<name>[A-Z][A-Za-z]*)>|'.
|
2021-07-16 04:27:04 +02:00
|
|
|
'(?<name>[A-Z][A-Za-z]*) (final )?: public (Td::ResultHandler|NetActor|Request)|'.
|
2018-10-02 14:28:42 +02:00
|
|
|
'(CREATE_REQUEST|CREATE_NO_ARGS_REQUEST)[(](?<name>[A-Z][A-Za-z]*)|'.
|
|
|
|
'(?<name>complete_pending_preauthentication_requests)|'.
|
2020-09-30 03:26:39 +02:00
|
|
|
'(?<name>get_message_history_slice)|'.
|
2018-10-02 14:28:42 +02:00
|
|
|
'(Up|Down)load[a-zA-Z]*C(?<name>allback)|(up|down)load_[a-z_]*_c(?<name>allback)_|'.
|
2020-01-04 12:59:33 +01:00
|
|
|
'(?<name>lazy_to_json)|'.
|
2018-10-02 14:28:42 +02:00
|
|
|
'(?<name>LogEvent)[^sA]|'.
|
|
|
|
'(?<name>parse)[(]|'.
|
|
|
|
'(?<name>store)[(]/', $f, $matches, PREG_SET_ORDER)) {
|
|
|
|
foreach ($matches as $match) {
|
|
|
|
$name = $match['name'];
|
|
|
|
if ($name === 'parse' || $name === 'store') {
|
|
|
|
if ($is_generated) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$name = 'LogEvent';
|
|
|
|
}
|
|
|
|
$deps[$name][] = $i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$parents[$i] = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($deps as $func_ids) {
|
|
|
|
foreach ($func_ids as $func_id) {
|
|
|
|
disjoint_set_union($parents, $func_ids[0], $func_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$sets = array();
|
|
|
|
$set_sizes = array();
|
|
|
|
foreach ($functions as $i => $f) {
|
|
|
|
$parent = disjoint_set_find($parents, $i);
|
|
|
|
if (!isset($sets[$parent])) {
|
|
|
|
$sets[$parent] = '';
|
|
|
|
$set_sizes[$parent] = 0;
|
|
|
|
}
|
|
|
|
$sets[$parent] .= $f;
|
2019-04-27 00:43:11 +02:00
|
|
|
$set_sizes[$parent] += preg_match('/Td::~?Td/', $f) ? 1000000 : strlen($f);
|
2018-10-02 14:28:42 +02:00
|
|
|
}
|
|
|
|
arsort($set_sizes);
|
|
|
|
|
|
|
|
$files = array_fill(0, $chunks, '');
|
|
|
|
$file_sizes = array_fill(0, $chunks, 0);
|
|
|
|
foreach ($set_sizes as $parent => $size) {
|
2019-04-26 16:30:13 +02:00
|
|
|
$file_id = array_search(min($file_sizes), $file_sizes);
|
|
|
|
$files[$file_id] .= $sets[$parent];
|
|
|
|
$file_sizes[$file_id] += $size;
|
2018-10-02 14:28:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($files as $n => $f) {
|
|
|
|
$new_content = $common.$namespace_begin.$f.$namespace_end;
|
2019-04-26 16:30:13 +02:00
|
|
|
|
|
|
|
$std_methods = array();
|
2021-12-10 11:09:37 +01:00
|
|
|
preg_match_all('/std::[a-z_0-9]*|td::unique(?!_)/', $new_content, $std_methods);
|
2019-04-26 16:30:13 +02:00
|
|
|
$std_methods = array_unique($std_methods[0]);
|
|
|
|
|
|
|
|
$needed_std_headers = array();
|
|
|
|
$type_headers = array(
|
|
|
|
'std::move' => '',
|
|
|
|
'std::vector' => '',
|
|
|
|
'std::string' => '',
|
|
|
|
'std::uint32_t' => '',
|
|
|
|
'std::int32_t' => '',
|
|
|
|
'std::int64_t' => '',
|
2021-12-10 11:09:37 +01:00
|
|
|
'td::unique' => 'algorithm',
|
2019-04-26 16:30:13 +02:00
|
|
|
'std::fill' => 'algorithm',
|
|
|
|
'std::find' => 'algorithm',
|
|
|
|
'std::max' => 'algorithm',
|
|
|
|
'std::min' => 'algorithm',
|
|
|
|
'std::remove' => 'algorithm',
|
|
|
|
'std::reverse' => 'algorithm',
|
|
|
|
'std::rotate' => 'algorithm',
|
|
|
|
'std::sort' => 'algorithm',
|
2020-01-08 21:41:03 +01:00
|
|
|
'std::abs' => 'cmath',
|
2019-04-26 16:30:13 +02:00
|
|
|
'std::numeric_limits' => 'limits',
|
|
|
|
'std::make_shared' => 'memory',
|
|
|
|
'std::shared_ptr' => 'memory',
|
|
|
|
'std::tie' => 'tuple',
|
|
|
|
'std::tuple' => 'tuple',
|
|
|
|
'std::decay_t' => 'type_traits',
|
|
|
|
'std::is_same' => 'type_traits',
|
|
|
|
'std::make_pair' => 'utility',
|
|
|
|
'std::pair' => 'utility',
|
|
|
|
'std::swap' => 'utility',
|
|
|
|
'std::unordered_map' => 'unordered_map',
|
|
|
|
'std::unordered_set' => 'unordered_set');
|
|
|
|
foreach ($type_headers as $type => $header) {
|
|
|
|
if (in_array($type, $std_methods)) {
|
|
|
|
$std_methods = array_diff($std_methods, array($type));
|
|
|
|
if ($header && !in_array($header, $needed_std_headers)) {
|
|
|
|
$needed_std_headers[] = $header;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$std_methods) { // know all needed std headers
|
|
|
|
$new_content = preg_replace_callback(
|
|
|
|
'/#include <([a-z_]*)>/',
|
|
|
|
function ($matches) use ($needed_std_headers) {
|
|
|
|
if (in_array($matches[1], $needed_std_headers)) {
|
|
|
|
return $matches[0];
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
},
|
|
|
|
$new_content
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-27 00:43:11 +02:00
|
|
|
if (!preg_match('/Td::~?Td/', $new_content)) { // destructor Td::~Td needs to see definitions of all forward-declared classes
|
2019-04-26 20:26:48 +02:00
|
|
|
$td_methods = array(
|
2019-04-27 00:43:11 +02:00
|
|
|
'animations_manager[_(-][^.]|AnimationsManager[^;>]' => "AnimationsManager",
|
|
|
|
'audios_manager[_(-][^.]|AudiosManager' => "AudiosManager",
|
2019-04-26 20:26:48 +02:00
|
|
|
'auth_manager[_(-][^.]|AuthManager' => 'AuthManager',
|
2021-03-08 19:01:40 +01:00
|
|
|
'background_manager[_(-][^.]|BackgroundManager' => "BackgroundManager",
|
2019-04-27 00:21:08 +02:00
|
|
|
'ConfigShared|shared_config[(]' => 'ConfigShared',
|
2021-06-18 02:06:20 +02:00
|
|
|
'contacts_manager[_(-][^.]|ContactsManager([^ ;.]| [^*])' => 'ContactsManager',
|
2021-03-08 19:01:40 +01:00
|
|
|
'country_info_manager[_(-][^.]|CountryInfoManager' => 'CountryInfoManager',
|
2019-04-27 00:43:11 +02:00
|
|
|
'documents_manager[_(-][^.]|DocumentsManager' => "DocumentsManager",
|
2019-04-26 20:26:48 +02:00
|
|
|
'file_reference_manager[_(-][^.]|FileReferenceManager|file_references[)]' => 'FileReferenceManager',
|
|
|
|
'file_manager[_(-][^.]|FileManager([^ ;.]| [^*])|update_file[)]' => 'files/FileManager',
|
|
|
|
'G[(][)]|Global[^A-Za-z]' => 'Global',
|
2021-09-07 09:02:44 +02:00
|
|
|
'game_manager[_(-][^.]|GameManager' => 'GameManager',
|
2021-03-08 19:01:40 +01:00
|
|
|
'group_call_manager[_(-][^.]|GroupCallManager' => 'GroupCallManager',
|
2019-04-26 20:26:48 +02:00
|
|
|
'HashtagHints' => 'HashtagHints',
|
|
|
|
'inline_queries_manager[_(-][^.]|InlineQueriesManager' => 'InlineQueriesManager',
|
2019-04-27 00:43:11 +02:00
|
|
|
'language_pack_manager[_(-][^.]|LanguagePackManager' => 'LanguagePackManager',
|
2021-05-25 01:24:30 +02:00
|
|
|
'link_manager[_(-][^.]|LinkManager' => 'LinkManager',
|
2020-09-22 01:15:09 +02:00
|
|
|
'LogeventIdWithGeneration|add_log_event|delete_log_event|get_erase_log_event_promise|parse_time|store_time' => 'logevent/LogEventHelper',
|
2020-08-09 13:18:08 +02:00
|
|
|
'MessageCopyOptions' => 'MessageCopyOptions',
|
2019-04-26 20:26:48 +02:00
|
|
|
'messages_manager[_(-][^.]|MessagesManager' => 'MessagesManager',
|
|
|
|
'notification_manager[_(-][^.]|NotificationManager|notifications[)]' => 'NotificationManager',
|
2021-12-15 23:09:59 +01:00
|
|
|
'option_manager[_(-][^.]|OptionManager' => "OptionManager",
|
2021-03-08 19:01:40 +01:00
|
|
|
'phone_number_manager[_(-][^.]|PhoneNumberManager' => "PhoneNumberManager",
|
|
|
|
'poll_manager[_(-][^.]|PollManager' => "PollManager",
|
2019-10-22 12:40:24 +02:00
|
|
|
'PublicDialogType|get_public_dialog_type' => 'PublicDialogType',
|
2019-04-26 20:26:48 +02:00
|
|
|
'SecretChatActor' => 'SecretChatActor',
|
|
|
|
'secret_chats_manager[_(-][^.]|SecretChatsManager' => 'SecretChatsManager',
|
2021-09-07 14:49:34 +02:00
|
|
|
'sponsored_message_manager[_(-][^.]|SponsoredMessageManager' => 'SponsoredMessageManager',
|
2021-06-18 02:06:20 +02:00
|
|
|
'stickers_manager[_(-][^.]|StickersManager' => 'StickersManager',
|
2019-04-26 20:26:48 +02:00
|
|
|
'[>](td_db[(][)]|get_td_db_impl[(])|TdDb[^A-Za-z]' => 'TdDb',
|
2021-08-27 14:51:50 +02:00
|
|
|
'theme_manager[_(-][^.]|ThemeManager' => "ThemeManager",
|
2020-06-22 03:28:03 +02:00
|
|
|
'TopDialogCategory|get_top_dialog_category' => 'TopDialogCategory',
|
2019-04-26 20:26:48 +02:00
|
|
|
'top_dialog_manager[_(-][^.]|TopDialogManager' => 'TopDialogManager',
|
2021-02-12 15:27:07 +01:00
|
|
|
'updates_manager[_(-][^.]|UpdatesManager|get_difference[)]|updateSentMessage|dummyUpdate' => 'UpdatesManager',
|
2019-04-26 20:26:48 +02:00
|
|
|
'WebPageId(Hash)?' => 'WebPageId',
|
|
|
|
'web_pages_manager[_(-][^.]|WebPagesManager' => 'WebPagesManager');
|
|
|
|
|
|
|
|
foreach ($td_methods as $pattern => $header) {
|
|
|
|
if (strpos($cpp_name, $header) !== false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$include_name = '#include "td/telegram/'.$header.'.h"';
|
|
|
|
if (strpos($new_content, $include_name) !== false && preg_match('/'.$pattern.'/', str_replace($include_name, '', $new_content)) === 0) {
|
|
|
|
$new_content = str_replace($include_name, '', $new_content);
|
|
|
|
}
|
|
|
|
}
|
2019-04-27 03:34:52 +02:00
|
|
|
} else {
|
|
|
|
$new_content = preg_replace_callback(
|
|
|
|
'|#include "[a-z_A-Z/0-9.]*"|',
|
|
|
|
function ($matches) {
|
|
|
|
if (strpos($matches[0], "Manager") !== false || strpos($matches[0], "HashtagHints") !== false || strpos($matches[0], "Td.h") !== false) {
|
|
|
|
return $matches[0];
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
},
|
|
|
|
$new_content
|
|
|
|
);
|
2019-04-26 20:26:48 +02:00
|
|
|
}
|
|
|
|
|
2018-10-02 14:28:42 +02:00
|
|
|
if (!file_exists($new_files[$n]) || file_get_contents($new_files[$n]) !== $new_content) {
|
|
|
|
echo "Writing file ".$new_files[$n].PHP_EOL;
|
|
|
|
file_put_contents($new_files[$n], $new_content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array('--help', $argv) || in_array('-h', $argv)) {
|
2020-01-06 19:44:09 +01:00
|
|
|
echo "Usage: php SplitSource.php [OPTION]...\n".
|
|
|
|
"Splits some source files to reduce a maximum amount of RAM needed for compiling a single file.\n".
|
|
|
|
" -u, --undo Undo all source code changes.\n".
|
|
|
|
" -h, --help Show this help.\n";
|
2018-10-02 14:28:42 +02:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
$undo = in_array('--undo', $argv) || in_array('-u', $argv);
|
2019-04-26 15:33:39 +02:00
|
|
|
$files = array('td/telegram/ContactsManager' => 20,
|
|
|
|
'td/telegram/MessagesManager' => 50,
|
|
|
|
'td/telegram/NotificationManager' => 10,
|
2018-10-02 14:28:42 +02:00
|
|
|
'td/telegram/StickersManager' => 10,
|
2019-04-27 00:21:08 +02:00
|
|
|
'td/telegram/Td' => 50,
|
2018-10-02 14:28:42 +02:00
|
|
|
'td/generate/auto/td/telegram/td_api' => 10,
|
|
|
|
'td/generate/auto/td/telegram/td_api_json' => 10,
|
|
|
|
'td/generate/auto/td/telegram/telegram_api' => 10);
|
|
|
|
|
|
|
|
foreach ($files as $file => $chunks) {
|
|
|
|
split_file($file, $chunks, $undo);
|
|
|
|
}
|