Skip to content

Commit

Permalink
Remove dependency on external fmt library
Browse files Browse the repository at this point in the history
  • Loading branch information
cathyjf committed Nov 4, 2024
1 parent 73e27d4 commit edcea58
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ OBJECT_DIR := $(BUILD_DIR)/objects
BIN_DIR := $(BUILD_DIR)/bin
BIN_APP := $(BIN_DIR)/keychain-interpose.app
CPPFLAGS_MINIMAL := -std=c++20 -O3 -flto -Wall -Werror -fprebuilt-module-path="$(OBJECT_DIR)" $(CPPFLAGS_EXTRA)
CPPFLAGS := $(CPPFLAGS_MINIMAL) $(shell pkg-config --cflags fmt gpg-error) -Idependencies/libCF++/CF++/include \
CPPFLAGS := $(CPPFLAGS_MINIMAL) $(shell pkg-config --cflags gpg-error) -Idependencies/libCF++/CF++/include \
-I$(shell brew --prefix boost)/include
OBJCFLAGS := -fobjc-arc -Wno-unused-but-set-variable
LIBCF++ := dependencies/libCF++/$(BUILD_DIR)/Build/lib/libCF++.a
LDFLAGS := -framework Security -framework CoreFoundation \
$(shell brew --prefix fmt)/lib/libfmt.a \
$(shell brew --prefix boost)/lib/libboost_program_options.a
MODULE_OBJECTS := $(addprefix $(OBJECT_DIR)/, cathyjf.ki.common.pcm cathyjf.ki.log.pcm)
MIGRATE_OBJECTS := $(addprefix $(OBJECT_DIR)/, migrate-keys.o cathyjf.ki.common.o migrate-keys-helper.o)
Expand Down Expand Up @@ -95,7 +94,7 @@ clean-deps:
$(OBJECT_DIR)/gpg-agent-deps : $(BIN_DIR)/encapsulate-app $(OBJECT_DIR)/gpg-agent-entitlements.plist
mkdir -p $@/bin $@/pkg-info
$(BIN_DIR)/encapsulate-app "$(shell brew --prefix gnupg)/bin/gpg-agent" "$@" \
"$(shell brew --prefix)" fmt boost
"$(shell brew --prefix)" boost
$(call CODESIGN, "$@/bin/gpg-agent", --entitlements $(OBJECT_DIR)/gpg-agent-entitlements.plist)
find "$@/bin" -name "*.dylib" -print0 | xargs -0 -I{} $(call CODESIGN, {})

Expand Down
12 changes: 6 additions & 6 deletions src/encapsulate-app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <array>
#include <filesystem>
#include <format>
#include <iostream>
#include <map>
#include <ranges>
Expand All @@ -12,7 +13,6 @@

#include <boost/algorithm/string/replace.hpp>
#include <boost/regex.hpp>
#include <fmt/core.h>

import cathyjf.ki.common;

Expand All @@ -25,7 +25,7 @@ namespace {
[[nodiscard]] auto get_dylibs_from_otool(const auto binary_file) {
auto objects = std::set<std::string>{};
const auto escaped_file = escape_shell_argument_single_quotes(binary_file);
const auto file = managed_popen(fmt::format("otool -L '{}'", escaped_file).c_str(), "r");
const auto file = managed_popen(std::format("otool -L '{}'", escaped_file).c_str(), "r");
while (auto data = fgetln_string(file.get())) {
auto what = boost::cmatch{};
static const auto otool_dylib_regex = boost::regex{ "^\\h+(.*)\\h+\\(.*" };
Expand Down Expand Up @@ -54,7 +54,7 @@ auto populate_objects(const auto binary_file, auto &objects, const int depth = 1
}

[[nodiscard]] auto make_relative_path(const auto &target) {
return fmt::format("@executable_path/../Frameworks/{}",
return std::format("@executable_path/../Frameworks/{}",
std::filesystem::path{ target }.filename().string());
}

Expand All @@ -71,7 +71,7 @@ auto populate_objects(const auto binary_file, auto &objects, const int depth = 1
std::filesystem::permissions(target, std::filesystem::perms::owner_write, std::filesystem::perm_options::add);
}
std::filesystem::copy_file(source, target, std::filesystem::copy_options::overwrite_existing);
std::cout << fmt::format("+ {} (from {})", target.string(), source.string()) << std::endl;
std::cout << std::format("+ {} (from {})", target.string(), source.string()) << std::endl;
map.insert({ source.string(), dylib_data {
.target_path = target.string(),
.relative_target_path = make_relative_path(target.string())
Expand All @@ -81,13 +81,13 @@ auto populate_objects(const auto binary_file, auto &objects, const int depth = 1
}

auto apply_install_name_tool_for_binary(const auto &binary_entry, const auto &map) -> void {
std::system(fmt::format(
std::system(std::format(
"install_name_tool -id '{}' '{}' 2>/dev/null",
escape_shell_argument_single_quotes(binary_entry.second.relative_target_path),
escape_shell_argument_single_quotes(binary_entry.second.target_path)
).c_str());
for (const auto &entry : map) {
const auto command = fmt::format("install_name_tool -change '{}' '{}' '{}' 2>/dev/null",
const auto command = std::format("install_name_tool -change '{}' '{}' '{}' 2>/dev/null",
escape_shell_argument_single_quotes(entry.first),
escape_shell_argument_single_quotes(entry.second.relative_target_path),
escape_shell_argument_single_quotes(binary_entry.second.target_path)
Expand Down
4 changes: 2 additions & 2 deletions src/keychain-interpose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <format>
#include <iostream>
#include <memory>
#include <set>

#include <fmt/core.h>
#include <gpg-error.h>
#include <CF++.hpp>
#include <CoreFoundation/CFData.h>
Expand Down Expand Up @@ -151,7 +151,7 @@ ssize_t my_gpgrt_read_line(gpgrt_stream_t any_stream, char **pbuffer, size_t *bu
(*pbuffer)[i] = '\n';
}
(*pbuffer)[i + 1] = '\0';
write_log_message(fmt::format("Read a line of {} characters from stream: {}", i, stream->keygrip));
write_log_message(std::format("Read a line of {} characters from stream: {}", i, stream->keygrip));
return i;
}

Expand Down
2 changes: 1 addition & 1 deletion src/meta/make-universal.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ make_arch() {
echo "Using this brew for $HOMEBREW_WRAPPER_ARCH: ${brew[*]:?}."
if [ -z "$skip_updates" ]; then
"${brew[@]:?}" update --force --quiet
"$SCRIPT_DIR/brew/install.sh" "boost" "fmt" "gnupg"
"$SCRIPT_DIR/brew/install.sh" "boost" "gnupg"
fi
make CPPFLAGS_EXTRA="-arch '$HOMEBREW_WRAPPER_ARCH'" BUILD_DIR="$build_dir" \
CXX="$CXX" LIBTOOL="$LIBTOOL" "$@"
Expand Down
21 changes: 11 additions & 10 deletions src/migrate-keys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
#include <span>
Expand All @@ -11,7 +12,6 @@
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
#include <fmt/core.h>
#include <CF++.hpp>
#include <CoreFoundation/CFArray.h>
#include <Security/Security.h>
Expand Down Expand Up @@ -128,18 +128,19 @@ namespace {
auto failures = 0;
auto replacements = 0;
for (auto entry : std::filesystem::directory_iterator(private_key_path)) {
const auto keygrip = entry.path().filename();
const auto path = entry.path();
const auto keygrip = path.filename();
std::cout << "Found " << keygrip << "." << std::endl;
if (is_file_placeholder(entry)) {
if (is_file_placeholder(path)) {
std::cout << " Skipping this file because it appears to be a placeholder." << std::endl;
continue;
}
std::cout << " This appears to be a private key." << std::endl;
const auto data = read_entire_file(entry);
const auto data = read_entire_file(path);
if (keychain_has_item(keygrip)) {
if (is_same_key_in_keychain(std::string_view{ data }, keygrip)) {
std::cout << " A copy of this key is already in the keychain." << std::endl;
if (write_placeholder(entry)) {
if (write_placeholder(path)) {
++replacements;
}
} else {
Expand All @@ -157,7 +158,7 @@ namespace {
} else {
std::cout << " Successfully added the key to the keychain." << std::endl;
++successes;
if (write_placeholder(entry)) {
if (write_placeholder(path)) {
++replacements;
}
}
Expand Down Expand Up @@ -232,7 +233,7 @@ void throw_if_invalid_options(const auto argc, const auto argv) {
continue;
}
throw boost::program_options::error{
fmt::format("Invalid command line argument: '{}'", string)
std::format("Invalid command line argument: '{}'", string)
};
}
}
Expand Down Expand Up @@ -266,7 +267,7 @@ void throw_if_conflicting_options(const auto &vm, const std::initializer_list<T>

auto main(const int argc, char **argv) -> int {
namespace po = boost::program_options;
auto desc = po::options_description{ fmt::format("Allowed options for {}", argv[0]) };
auto desc = po::options_description{ std::format("Allowed options for {}", argv[0]) };
desc.add_options()
("help", "Print this help message.")
("migrate-to-keychain",
Expand All @@ -283,7 +284,7 @@ auto main(const int argc, char **argv) -> int {
} catch (po::error &ex) {
const auto error = ([&ex]() -> std::string {
if (const auto unknown = dynamic_cast<po::unknown_option *>(&ex)) {
return fmt::format("Invalid option: '{}'", unknown->get_option_name());
return std::format("Invalid option: '{}'", unknown->get_option_name());
}
return ex.what();
})();
Expand Down Expand Up @@ -317,7 +318,7 @@ auto main(const int argc, char **argv) -> int {
// of {stdout, stderr, stdin} is connected to a TTY, we'll open the Terminal
// application for the user and display a helpful message, instead of running the
// default command (--migrate-to-keychain). This should help avoid surprises.
return open_script_with_default_terminal(argv[0], std::filesystem::canonical(fmt::format(
return open_script_with_default_terminal(argv[0], std::filesystem::canonical(std::format(
"{}/../Resources/help-message.sh",
std::filesystem::path{ argv[0] }.parent_path().string())).string()) ? 0 : 1;
}
Expand Down

0 comments on commit edcea58

Please sign in to comment.