Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NOSQUASH] Precompiled headers support #13355

Merged
merged 2 commits into from
May 27, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ find_package(Lua REQUIRED)
if(NOT USE_LUAJIT)
add_subdirectory(lib/bitop)
endif()
add_subdirectory(lib/sha256)

if(BUILD_UNITTESTS OR BUILD_BENCHMARKS)
add_subdirectory(lib/catch2)
Expand Down
2 changes: 2 additions & 0 deletions doc/compiling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ General options and their default values:
SemiDebug - Partially optimized debug build
RelWithDebInfo - Release build with debug information
MinSizeRel - Release build with -Os passed to compiler to make executable as small as possible
PRECOMPILE_HEADERS=FALSE - Precompile some headers (experimental; requires CMake 3.16 or later)
PRECOMPILED_HEADERS_PATH= - Path to a file listing all headers to precompile (default points to src/precompiled_headers.txt)
ENABLE_CURL=ON - Build with cURL; Enables use of online mod repo, public serverlist and remote media fetching via http
ENABLE_CURSES=ON - Build with (n)curses; Enables a server side terminal (command line option: --terminal)
ENABLE_GETTEXT=ON - Build with Gettext; Allows using translations
Expand Down
14 changes: 14 additions & 0 deletions lib/sha256/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
project(sha256 C)

add_library(sha256 STATIC sha256.c)

target_include_directories(sha256 INTERFACE .)

INCLUDE(CheckIncludeFiles)
check_include_files(endian.h HAVE_ENDIAN_H)

configure_file(
"${PROJECT_SOURCE_DIR}/cmake_config.h.in"
"${PROJECT_BINARY_DIR}/cmake_config.h"
)
target_include_directories(sha256 PRIVATE "${PROJECT_BINARY_DIR}")
5 changes: 5 additions & 0 deletions lib/sha256/cmake_config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Filled in by the build system

#pragma once

#cmakedefine HAVE_ENDIAN_H
8 changes: 2 additions & 6 deletions src/util/sha256.c → lib/sha256/sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,13 @@
#include <stdlib.h>
#include <string.h>

#include "util/sha256.h"
#include "sha256/sha256.h"

#if defined(_MSC_VER) && !defined(__clang__) && !defined(__attribute__)
#define __attribute__(a)
#endif

/* pull HAVE_ENDIAN_H from Minetest */
#include "config.h"
#if !HAVE_ENDIAN_H
#undef HAVE_ENDIAN_H
#endif
#include "cmake_config.h" /* HAVE_ENDIAN_H */

/** endian.h **/
/*
Expand Down
File renamed without changes.
29 changes: 29 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ if(NOT (BUILD_CLIENT OR BUILD_SERVER))
endif()


option(PRECOMPILE_HEADERS "Precompile some headers (experimental; requires CMake 3.16 or later)" FALSE)
set(PRECOMPILED_HEADERS_PATH "" CACHE FILEPATH "Path to a file listing all headers to precompile")

if(PRECOMPILE_HEADERS)
if(${CMAKE_VERSION} VERSION_LESS 3.16)
message(FATAL_ERROR "PRECOMPILE_HEADERS is on, but precompiled headers require at least CMake 3.16.")
endif()
if(PRECOMPILED_HEADERS_PATH)
set(PRECOMPILED_HEADERS ${PRECOMPILED_HEADERS_PATH})
else()
set(PRECOMPILED_HEADERS "${CMAKE_SOURCE_DIR}/src/precompiled_headers.txt")
endif()
message(STATUS "Reading headers to precompile from: ${PRECOMPILED_HEADERS}")
# ignore lines that begin with # and empty lines
file(STRINGS ${PRECOMPILED_HEADERS} PRECOMPILED_HEADERS_LIST REGEX "^[^#].*$")
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${PRECOMPILED_HEADERS})
endif()


option(ENABLE_CURL "Enable cURL support for fetching media" TRUE)
set(USE_CURL FALSE)

Expand Down Expand Up @@ -565,6 +584,7 @@ if(BUILD_CLIENT)
${GMP_LIBRARY}
${JSON_LIBRARY}
${LUA_BIT_LIBRARY}
sha256
${FREETYPE_LIBRARY}
${PLATFORM_LIBS}
# on Android, Minetest depends on SDL2 directly
Expand Down Expand Up @@ -618,6 +638,10 @@ if(BUILD_CLIENT)
if(BUILD_UNITTESTS OR BUILD_BENCHMARKS)
target_link_libraries(${PROJECT_NAME} Catch2::Catch2)
endif()

if(PRECOMPILE_HEADERS)
target_precompile_headers(${PROJECT_NAME} PRIVATE ${PRECOMPILED_HEADERS_LIST})
endif()
endif(BUILD_CLIENT)


Expand All @@ -637,6 +661,7 @@ if(BUILD_SERVER)
${JSON_LIBRARY}
${LUA_LIBRARY}
${LUA_BIT_LIBRARY}
sha256
${GMP_LIBRARY}
${PLATFORM_LIBS}
)
Expand Down Expand Up @@ -680,6 +705,10 @@ if(BUILD_SERVER)
if(BUILD_UNITTESTS OR BUILD_BENCHMARKS)
target_link_libraries(${PROJECT_NAME}server Catch2::Catch2)
endif()

if(PRECOMPILE_HEADERS)
target_precompile_headers(${PROJECT_NAME}server PRIVATE ${PRECOMPILED_HEADERS_LIST})
endif()
endif(BUILD_SERVER)

# See issue #4638
Expand Down
103 changes: 103 additions & 0 deletions src/precompiled_headers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

# stdlib
# ------

# C stuff:
<cassert>
<cctype>
<cerrno>
<cfenv>
<cfloat>
<cinttypes>
<ciso646>
<climits>
<clocale>
<cmath>
<csetjmp>
<csignal>
<cstdarg>
<cstdbool>
<cstddef>
<cstdint>
<cstdio>
<cstdlib>
<cstring>
<ctgmath>
<ctime>
<cuchar>
<cwchar>
<cwctype>

# Containers:
<array>
<deque>
<forward_list>
<list>
<map>
<queue>
<set>
<stack>
<unordered_map>
<unordered_set>
<vector>

# Input/Output:
<fstream>
<iomanip>
<ios>
<iosfwd>
<iostream>
<istream>
<ostream>
<sstream>
<streambuf>

# Multi-threading:
<atomic>
<condition_variable>
<future>
<mutex>
<shared_mutex>
<thread>

# Other:
<algorithm>
<any>
<bitset>
<charconv>
<chrono>
<codecvt>
<complex>
<exception>
<execution>
<functional>
<initializer_list>
<iterator>
<limits>
<locale>
<memory>
<memory_resource>
<new>
<numeric>
<optional>
<random>
<ratio>
<regex>
<stdexcept>
<string>
<string_view>
<system_error>
<tuple>
<typeindex>
<typeinfo>
<type_traits>
<utility>
<valarray>
<variant>


# libs
# ----

# jsoncpp
<json/json.h>
4 changes: 2 additions & 2 deletions src/script/lua_api/l_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "version.h"
#include "util/hex.h"
#include "util/sha1.h"
#include "util/sha256.h"
#include <sha256/sha256.h>
#include "util/png.h"
#include <cstdio>

Expand Down Expand Up @@ -573,7 +573,7 @@ int ModApiUtil::l_sha256(lua_State *L)

auto data = readParam<std::string_view>(L, 1);
bool hex = !lua_isboolean(L, 2) || !readParam<bool>(L, 2);

std::string data_sha256;
data_sha256.resize(SHA256_DIGEST_LENGTH);
SHA256(reinterpret_cast<const unsigned char*>(data.data()), data.size(),
Expand Down
1 change: 0 additions & 1 deletion src/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ set(UTIL_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/quicktune.cpp
${CMAKE_CURRENT_SOURCE_DIR}/serialize.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sha1.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sha256.c
${CMAKE_CURRENT_SOURCE_DIR}/string.cpp
${CMAKE_CURRENT_SOURCE_DIR}/srp.cpp
${CMAKE_CURRENT_SOURCE_DIR}/timetaker.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/util/srp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
#include <mini-gmp.h>
#endif

#include "util/sha256.h"
#include <sha256/sha256.h>

#include "srp.h"
//#define CSRP_USE_SHA1
Expand Down