Skip to content

Commit 651fedd

Browse files
authored
Merge pull request #5661 from ab9rf/fmtlib
switch to using fmtlib for output formatting
2 parents 3ca35c6 + 03d294f commit 651fedd

File tree

149 files changed

+1749
-1705
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

149 files changed

+1749
-1705
lines changed

CMakeLists.txt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ option(REMOVE_SYMBOLS_FROM_DF_STUBS "Remove debug symbols from DF stubs. (Reduce
4646
macro(CHECK_GCC compiler_path)
4747
execute_process(COMMAND ${compiler_path} -dumpversion OUTPUT_VARIABLE GCC_VERSION_OUT)
4848
string(STRIP "${GCC_VERSION_OUT}" GCC_VERSION_OUT)
49-
if(${GCC_VERSION_OUT} VERSION_LESS "10")
50-
message(SEND_ERROR "${compiler_path} version ${GCC_VERSION_OUT} cannot be used - use GCC 10 or later")
49+
if(${GCC_VERSION_OUT} VERSION_LESS "11")
50+
message(SEND_ERROR "${compiler_path} version ${GCC_VERSION_OUT} cannot be used - use GCC 11 or later")
5151
endif()
5252
endmacro()
5353

@@ -347,6 +347,17 @@ if(BUILD_LIBRARY)
347347
endif()
348348
endif()
349349

350+
# this can be made conditional once we get to better platform support for std::format
351+
INCLUDE(FetchContent)
352+
FetchContent_Declare(
353+
fmt
354+
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
355+
GIT_TAG 790b9389ae99c4ddebdd2736a8602eca1fec684e # 12.1.0 + bugfix for MSVC warning + build time improvements
356+
)
357+
FetchContent_MakeAvailable(fmt)
358+
set(FMTLIB fmt)
359+
add_definitions("-DUSE_FMTLIB")
360+
350361
if(APPLE)
351362
# libstdc++ (GCC 4.8.5 for OS X 10.6)
352363
# fixes crash-on-unwind bug in DF's libstdc++
@@ -428,7 +439,7 @@ macro(dfhack_test name files)
428439
if(BUILD_LIBRARY AND UNIX AND NOT APPLE) # remove this once our MSVC build env has been updated
429440
add_executable(${name} ${files})
430441
target_include_directories(${name} PUBLIC depends/googletest/googletest/include)
431-
target_link_libraries(${name} dfhack gtest)
442+
target_link_libraries(${name} dfhack ${FMTLIB} gtest)
432443
add_test(NAME ${name} COMMAND ${name})
433444
endif()
434445
endmacro()

docs/dev/compile/Compile.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,14 @@ a command starting with ``cmake .. -G Ninja`` on Linux and macOS, following the
442442
instructions in the sections above. CMake should automatically locate files that
443443
you placed in ``CMake/downloads``, and use them instead of attempting to
444444
download them.
445+
446+
In addition, some packages used by DFHack are managed using CMake's ``FetchContent``
447+
feature, which requires an online connection during builds. The simplest way to address
448+
this is to have a connection during the first build (during which CMake will download the
449+
dependencies), and then to use CMake's ``FETCHCONTENT_FULLY_DISCONNECTED`` or
450+
``FETCHCONTENT_UPDATES_DISCONNECTED`` defines to control how CMake manages cached
451+
dependencies. If you need even the first-time build be an offline build, you will need
452+
to provide a CMake dependency provider. We do not provide one, but CMake's own documentation
453+
includes a simple provider. For more information about CMake's ``FetchContent`` feature
454+
and how to use it in offline builds, see the
455+
`CMake documentation <https://cmake.org/cmake/help/latest/module/FetchContent.html>`_.

library/CMakeLists.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ set(MAIN_HEADERS
5959
include/DebugManager.h
6060
include/Error.h
6161
include/Export.h
62+
include/Format.h
6263
include/Hooks.h
6364
include/LuaTools.h
6465
include/LuaWrapper.h
@@ -326,12 +327,12 @@ if(UNIX)
326327
endif()
327328

328329
if(APPLE)
329-
set(PROJECT_LIBS dl dfhack-md5 ${DFHACK_TINYXML})
330+
set(PROJECT_LIBS dl dfhack-md5 ${FMTLIB} ${DFHACK_TINYXML})
330331
elseif(UNIX)
331-
set(PROJECT_LIBS rt dl dfhack-md5 ${DFHACK_TINYXML})
332+
set(PROJECT_LIBS rt dl dfhack-md5 ${FMTLIB} ${DFHACK_TINYXML})
332333
else(WIN32)
333334
# FIXME: do we really need psapi?
334-
set(PROJECT_LIBS psapi dbghelp dfhack-md5 ${DFHACK_TINYXML})
335+
set(PROJECT_LIBS psapi dbghelp dfhack-md5 ${FMTLIB} ${DFHACK_TINYXML})
335336
endif()
336337

337338
set(VERSION_SRCS DFHackVersion.cpp)
@@ -397,7 +398,7 @@ else()
397398
set_target_properties(dfhack PROPERTIES COMPILE_FLAGS "-include Export.h" )
398399
set_target_properties(dfhack-client PROPERTIES COMPILE_FLAGS "-include Export.h" )
399400
add_library(dfhooks_dfhack SHARED Hooks.cpp)
400-
target_link_libraries(dfhooks_dfhack dfhack)
401+
target_link_libraries(dfhooks_dfhack dfhack ${FMTLIB})
401402
endif()
402403

403404
# effectively disables debug builds...
@@ -426,7 +427,7 @@ endif()
426427
target_link_libraries(dfhack protobuf-lite clsocket lua jsoncpp_static dfhack-version ${PROJECT_LIBS})
427428
set_target_properties(dfhack PROPERTIES INTERFACE_LINK_LIBRARIES "")
428429

429-
target_link_libraries(dfhack-client protobuf-lite clsocket jsoncpp_static)
430+
target_link_libraries(dfhack-client protobuf-lite clsocket jsoncpp_static ${FMTLIB})
430431
if(WIN32)
431432
target_link_libraries(dfhack-client dbghelp)
432433
endif()

library/ColorText.cpp

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -91,54 +91,6 @@ color_ostream::~color_ostream()
9191
delete buf();
9292
}
9393

94-
void color_ostream::print(const char *format, ...)
95-
{
96-
va_list args;
97-
va_start(args, format);
98-
vprint(format, args);
99-
va_end(args);
100-
}
101-
102-
void color_ostream::vprint(const char *format, va_list args)
103-
{
104-
std::string str = stl_vsprintf(format, args);
105-
106-
if (!str.empty()) {
107-
flush_buffer(false);
108-
add_text(cur_color, str);
109-
if (str[str.size()-1] == '\n')
110-
flush_proxy();
111-
}
112-
}
113-
114-
void color_ostream::printerr(const char * format, ...)
115-
{
116-
va_list args;
117-
va_start(args, format);
118-
vprinterr(format, args);
119-
va_end(args);
120-
}
121-
122-
void color_ostream::vprinterr(const char *format, va_list args)
123-
{
124-
color_value save = cur_color;
125-
126-
if (log_errors_to_stderr)
127-
{
128-
va_list args1;
129-
va_copy(args1, args);
130-
vfprintf(stderr, format, args1);
131-
va_end(args1);
132-
}
133-
134-
color(COLOR_LIGHTRED);
135-
va_list args2;
136-
va_copy(args2, args);
137-
vprint(format, args2);
138-
va_end(args2);
139-
color(save);
140-
}
141-
14294
void color_ostream::color(color_value c)
14395
{
14496
if (c == cur_color)

library/Commands.cpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace DFHack
4949
"See more commands by running 'ls'.\n\n"
5050
);
5151

52-
con.print("DFHack version %s\n", dfhack_version_desc().c_str());
52+
con.print("DFHack version {}\n", dfhack_version_desc());
5353
}
5454
else
5555
{
@@ -100,12 +100,12 @@ namespace DFHack
100100
}
101101
}
102102
if (ret != CR_OK)
103-
con.printerr("%s failed\n", first.c_str());
103+
con.printerr("{} failed\n", first.c_str());
104104
return ret;
105105
}
106106
else
107107
{
108-
con.printerr("%s: no arguments\n", first.c_str());
108+
con.printerr("{}: no arguments\n", first.c_str());
109109
return CR_FAILURE;
110110
}
111111

@@ -128,7 +128,7 @@ namespace DFHack
128128

129129
if (has_backslashes(part))
130130
{
131-
con.printerr("Replacing backslashes with forward slashes in \"%s\"\n", part.c_str());
131+
con.printerr("Replacing backslashes with forward slashes in \"{}\"\n", part);
132132
replace_backslashes_with_forwardslashes(part);
133133
}
134134

@@ -146,20 +146,20 @@ namespace DFHack
146146
else
147147
{
148148
res = CR_NOT_FOUND;
149-
con.printerr("No such plugin or Lua script: %s\n", part.c_str());
149+
con.printerr("No such plugin or Lua script: {}\n", part);
150150
}
151151
}
152152
else if (!plug->can_set_enabled())
153153
{
154154
res = CR_NOT_IMPLEMENTED;
155-
con.printerr("Cannot %s plugin: %s\n", first.c_str(), part.c_str());
155+
con.printerr("Cannot {} plugin: {}\n", first, part);
156156
}
157157
else
158158
{
159159
res = plug->set_enabled(con, enable);
160160

161161
if (res != CR_OK || plug->is_enabled() != enable)
162-
con.printerr("Could not %s plugin: %s\n", first.c_str(), part.c_str());
162+
con.printerr("Could not {} plugin: {}\n", first, part);
163163
}
164164
}
165165

@@ -174,7 +174,7 @@ namespace DFHack
174174
if (!plug->can_be_enabled()) continue;
175175

176176
con.print(
177-
"%21s %-3s%s\n",
177+
"{:>21} {:<3}{}\n",
178178
(key + ":").c_str(),
179179
plug->is_enabled() ? "on" : "off",
180180
plug->can_set_enabled() ? "" : " (controlled internally)"
@@ -189,8 +189,8 @@ namespace DFHack
189189

190190
command_result Commands::plug(color_ostream& con, Core& core, const std::string& first, const std::vector<std::string>& parts)
191191
{
192-
constexpr auto header_format = "%30s %10s %4s %8s\n";
193-
constexpr auto row_format = "%30s %10s %4zu %8s\n";
192+
constexpr auto header_format = "{:30} {:10} {:4} {:8}\n";
193+
constexpr auto row_format = header_format;
194194

195195
con.print(header_format, "Name", "State", "Cmds", "Enabled");
196196

@@ -227,7 +227,7 @@ namespace DFHack
227227
}
228228
con.color(color);
229229
con.print(row_format,
230-
plug->getName().c_str(),
230+
plug->getName(),
231231
Plugin::getStateDescription(plug->getState()),
232232
plug->size(),
233233
(plug->can_be_enabled()
@@ -293,11 +293,11 @@ namespace DFHack
293293
for (const auto& part : parts | std::views::drop(2) | std::views::reverse) {
294294
auto spec = KeySpec::parse(keystr, &parse_error);
295295
if (!spec.has_value()) {
296-
con.printerr("%s\n", parse_error.c_str());
296+
con.printerr("{}\n", parse_error);
297297
break;
298298
}
299299
if (!hotkey_mgr->addKeybind(spec.value(), part)) {
300-
con.printerr("Invalid command: '%s'\n", part.c_str());
300+
con.printerr("Invalid command: '{}'\n", part);
301301
break;
302302
}
303303
}
@@ -306,7 +306,7 @@ namespace DFHack
306306
for (const auto& part : parts | std::views::drop(1)) {
307307
auto spec = KeySpec::parse(part, &parse_error);
308308
if (!spec.has_value()) {
309-
con.printerr("%s\n", parse_error.c_str());
309+
con.printerr("{}\n", parse_error);
310310
}
311311
if (!hotkey_mgr->removeKeybind(spec.value())) {
312312
con.printerr("No matching keybinds to remove\n");
@@ -317,7 +317,7 @@ namespace DFHack
317317
else if (parts.size() == 2 && parts[0] == "list") {
318318
auto spec = KeySpec::parse(parts[1], &parse_error);
319319
if (!spec.has_value()) {
320-
con.printerr("%s\n", parse_error.c_str());
320+
con.printerr("{}\n", parse_error);
321321
return CR_FAILURE;
322322
}
323323
std::vector<std::string> list = hotkey_mgr->listKeybinds(spec.value());
@@ -326,7 +326,8 @@ namespace DFHack
326326
for (const auto& kb : list)
327327
con << " " << kb << std::endl;
328328
}
329-
else {
329+
else
330+
{
330331
con << "Usage:\n"
331332
<< " keybinding list <key>\n"
332333
<< " keybinding clear <key>[@context]...\n"
@@ -350,15 +351,15 @@ namespace DFHack
350351
std::vector<std::string> cmd(parts.begin() + 2, parts.end());
351352
if (!core.AddAlias(name, cmd, parts[0] == "replace"))
352353
{
353-
con.printerr("Could not add alias %s - already exists\n", name.c_str());
354+
con.printerr("Could not add alias {} - already exists\n", name);
354355
return CR_FAILURE;
355356
}
356357
}
357358
else if (parts.size() >= 2 && (parts[0] == "delete" || parts[0] == "clear"))
358359
{
359360
if (!core.RemoveAlias(parts[1]))
360361
{
361-
con.printerr("Could not remove alias %s\n", parts[1].c_str());
362+
con.printerr("Could not remove alias {}\n", parts[1]);
362363
return CR_FAILURE;
363364
}
364365
}
@@ -490,10 +491,10 @@ namespace DFHack
490491
{
491492
if (!parts[1].size() || (state_script.event == sc_event_id(parts[1])))
492493
{
493-
con.print("%s (%s): %s%s\n", sc_event_name(state_script.event).c_str(),
494+
con.print("{} ({}): {}{}\n", sc_event_name(state_script.event),
494495
state_script.save_specific ? "save-specific" : "global",
495496
state_script.save_specific ? "<save folder>/raw/" : "<DF folder>/",
496-
state_script.path.c_str());
497+
state_script.path);
497498
}
498499
}
499500
return CR_OK;

0 commit comments

Comments
 (0)