Skip to content

Commit

Permalink
Implement conditional compilation -DWITH_MODULEMD=OFF
Browse files Browse the repository at this point in the history
When WITH_MODULEMD is turned off module sources are not compiled and
module headers are not installed.

Public API in non-module headers (base.hpp and goal.hpp) is still
available but throws an exception when used.
Another option would be to also hide the functions in non-module headers
but in order to ensure the headers always match the compiled library (so
they don't depend on the user not defining `WITH_MODULEMD`) they would
have to be configured via a constant at compile time. For example using
cmake to `configure_file` turning them into `.in` files.
  • Loading branch information
kontura committed Jun 25, 2024
1 parent 0016684 commit 8a65cc2
Show file tree
Hide file tree
Showing 25 changed files with 198 additions and 20 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
message("Running CMake on dnf5...")
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.6)


include(VERSION.cmake)
Expand Down Expand Up @@ -119,6 +119,10 @@ if(WITH_TRANSLATIONS)
add_custom_target(gettext-potfiles)
endif()

# When modules are enabled add WITH_MODULEMD definition for all sub-directories (all components).
if (WITH_MODULEMD)
add_definitions(-DWITH_MODULEMD)
endif()

include_directories("${PROJECT_SOURCE_DIR}/include")
include_directories("${PROJECT_SOURCE_DIR}/common")
Expand Down
8 changes: 7 additions & 1 deletion dnf5/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ add_definitions(-DINSTALL_PREFIX=\"${CMAKE_INSTALL_PREFIX}\")
# set SYSCONFIG_DIR, used to find the directory containing the dnf5 configuration
add_definitions(-DSYSCONFIG_DIR=\"${CMAKE_INSTALL_FULL_SYSCONFDIR}\")

# use any sources found under the current directory
# use sources found under the current directory
file(GLOB_RECURSE DNF5_SOURCES *.cpp)

# exclude module sources if WITH_MODULEMD not defined
if (NOT WITH_MODULEMD)
file(GLOB_RECURSE DNF5_SOURCES_MODULES commands/module/*.cpp)
list(REMOVE_ITEM DNF5_SOURCES ${DNF5_SOURCES_MODULES})
endif()

include_directories("${PROJECT_SOURCE_DIR}/dnf5/include/")
include_directories(.)

Expand Down
4 changes: 4 additions & 0 deletions dnf5/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "commands/list/list.hpp"
#include "commands/makecache/makecache.hpp"
#include "commands/mark/mark.hpp"
#ifdef WITH_MODULEMD
#include "commands/module/module.hpp"
#endif
#include "commands/offline/offline.hpp"
#include "commands/provides/provides.hpp"
#include "commands/reinstall/reinstall.hpp"
Expand Down Expand Up @@ -687,7 +689,9 @@ static void add_commands(Context & context) {

context.add_and_initialize_command(std::make_unique<GroupCommand>(context));
context.add_and_initialize_command(std::make_unique<EnvironmentCommand>(context));
#ifdef WITH_MODULEMD
context.add_and_initialize_command(std::make_unique<ModuleCommand>(context));
#endif
context.add_and_initialize_command(std::make_unique<HistoryCommand>(context));
context.add_and_initialize_command(std::make_unique<RepoCommand>(context));
context.add_and_initialize_command(std::make_unique<AdvisoryCommand>(context));
Expand Down
12 changes: 12 additions & 0 deletions include/libdnf5-cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ endif()

file(GLOB_RECURSE LIBDNF5_CLI_HEADERS *.hpp)

if (NOT WITH_MODULEMD)
file(GLOB_RECURSE LIBDNF5_CLI_HEADERS_MODULES
output/adapters/module.hpp
output/adapters/module_tmpl.hpp
output/interfaces/module.hpp
output/moduleinfo.hpp
output/modulelist.hpp)

list(REMOVE_ITEM LIBDNF5_CLI_HEADERS ${LIBDNF5_CLI_HEADERS_MODULES})
endif()


# preserve relative paths of the header files
foreach(abspath ${LIBDNF5_CLI_HEADERS})
# relative path to the header file
Expand Down
10 changes: 10 additions & 0 deletions include/libdnf5/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
file(GLOB_RECURSE LIBDNF5_HEADERS *.hpp *.h)

if (NOT WITH_MODULEMD)
file(GLOB_RECURSE LIBDNF5_HEADERS_MODULES module/*.hpp)

# module_sack_weak.hpp is needed for base.hpp API
# its not actually used and it doesn't include any other libdnf5 header
list(FILTER LIBDNF5_HEADERS_MODULES EXCLUDE REGEX .*module_sack_weak.hpp)

list(REMOVE_ITEM LIBDNF5_HEADERS ${LIBDNF5_HEADERS_MODULES})
endif()

# preserve relative paths of the header files
foreach(abspath ${LIBDNF5_HEADERS})
# relative path to the header file
Expand Down
4 changes: 3 additions & 1 deletion include/libdnf5/base/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "libdnf5/conf/vars.hpp"
#include "libdnf5/defs.h"
#include "libdnf5/logger/log_router.hpp"
#include "libdnf5/module/module_sack.hpp"
#include "libdnf5/module/module_sack_weak.hpp"
#include "libdnf5/plugin/plugin_info.hpp"
#include "libdnf5/repo/download_callbacks.hpp"
#include "libdnf5/repo/repo_sack.hpp"
Expand All @@ -42,6 +42,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
namespace libdnf5::module {

class ModuleDB;
class ModuleSack;

}

Expand Down Expand Up @@ -86,6 +87,7 @@ class LIBDNF_API Base {
LogRouterWeakPtr get_logger();
repo::RepoSackWeakPtr get_repo_sack();
rpm::PackageSackWeakPtr get_rpm_package_sack();
/// Throws libdnf5::AssertionError when used with libdnf5 compiled without modules enabled.
module::ModuleSackWeakPtr get_module_sack();

/// Adds a request to enable/disable plugins that match the names (glob patterns) in the list.
Expand Down
3 changes: 3 additions & 0 deletions include/libdnf5/base/goal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,21 @@ class LIBDNF_API Goal {
~Goal();

/// Add module enable request to the goal.
/// Throws libdnf5::AssertionError when used with libdnf5 compiled without modules enabled.
/// @param spec A string with module spec to enable.
/// @param settings A structure to override default goal settings.
// @replaces dnf:dnf/module/module_base.py:method:ModuleBase().enable(self, module_specs)
void add_module_enable(const std::string & spec, const libdnf5::GoalJobSettings & settings);

/// Add module disable request to the goal.
/// Throws libdnf5::AssertionError when used with libdnf5 compiled without modules enabled.
/// @param spec A string with module spec to disable.
/// @param settings A structure to override default goal settings.
// @replaces dnf:dnf/module/module_base.py:method:ModuleBase().disable(self, module_specs)
void add_module_disable(const std::string & spec, const libdnf5::GoalJobSettings & settings);

/// Add module reset request to the goal.
/// Throws libdnf5::AssertionError when used with libdnf5 compiled without modules enabled.
/// @param spec A string with module spec to reset.
/// @param settings A structure to override default goal settings.
// @replaces dnf:dnf/module/module_base.py:method:ModuleBase().reset(self, module_specs)
Expand Down
11 changes: 10 additions & 1 deletion libdnf5-cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ set(GETTEXT_DOMAIN libdnf5-cli)
add_definitions(-DGETTEXT_DOMAIN=\"${GETTEXT_DOMAIN}\")


# use any sources found under the current directory
# use sources found under the current directory
file(GLOB_RECURSE LIBDNF5_CLI_SOURCES *.cpp)

# exclude module sources if WITH_MODULEMD not defined
if (NOT WITH_MODULEMD)
file(GLOB_RECURSE LIBDNF5_CLI_SOURCES_MODULES
output/adapters/module.cpp
output/moduleinfo.cpp
output/modulelist.cpp)

list(REMOVE_ITEM LIBDNF5_CLI_SOURCES ${LIBDNF5_CLI_SOURCES_MODULES})
endif()

# gather all pkg-config requires and write them to a .pc file later
list(APPEND LIBDNF5_CLI_PC_REQUIRES)
Expand Down
18 changes: 13 additions & 5 deletions libdnf5/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# use any sources found under the current directory
# use sources found under the current directory
file(GLOB_RECURSE LIBDNF5_SOURCES *.cpp *.c)

# exclude module sources if WITH_MODULEMD not defined
if (NOT WITH_MODULEMD)
file(GLOB_RECURSE LIBDNF5_SOURCES_MODULES module/*.cpp)
list(REMOVE_ITEM LIBDNF5_SOURCES ${LIBDNF5_SOURCES_MODULES})
endif()

# create config header file
configure_file("config.h.in" ${CMAKE_CURRENT_SOURCE_DIR}/conf/config.h)

Expand Down Expand Up @@ -65,10 +71,12 @@ include_directories(${JSONC_INCLUDE_DIRS})
target_link_libraries(libdnf5 PRIVATE ${JSONC_LIBRARIES})
target_link_libraries(libdnf5_static PRIVATE ${JSONC_LIBRARIES})

pkg_check_modules(LIBMODULEMD REQUIRED modulemd-2.0>=2.11.2)
list(APPEND LIBDNF5_PC_REQUIRES "${LIBMODULEMD_MODULE_NAME}")
target_link_libraries(libdnf5 PRIVATE ${LIBMODULEMD_LIBRARIES})
target_link_libraries(libdnf5_static PRIVATE ${LIBMODULEMD_LIBRARIES})
if (WITH_MODULEMD)
pkg_check_modules(LIBMODULEMD REQUIRED modulemd-2.0>=2.11.2)
list(APPEND LIBDNF5_PC_REQUIRES "${LIBMODULEMD_MODULE_NAME}")
target_link_libraries(libdnf5 PRIVATE ${LIBMODULEMD_LIBRARIES})
target_link_libraries(libdnf5_static PRIVATE ${LIBMODULEMD_LIBRARIES})
endif()

pkg_check_modules(LIBSOLV REQUIRED libsolv>=0.7.25)
list(APPEND LIBDNF5_PC_REQUIRES "${LIBSOLV_MODULE_NAME}")
Expand Down
19 changes: 18 additions & 1 deletion libdnf5/base/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "../conf/config_utils.hpp"
#include "base_impl.hpp"
#include "conf/config.h"

#ifdef WITH_MODULEMD
#include "module/module_sack_impl.hpp"

#include "libdnf5/module/module_sack.hpp"
#endif

#include "solv/pool.hpp"
#include "utils/dnf4convert/dnf4convert.hpp"
#include "utils/fs/utils.hpp"
Expand Down Expand Up @@ -55,9 +61,12 @@ Base::Impl::Impl(const libdnf5::BaseWeakPtr & base, std::vector<std::unique_ptr<
log_router(std::move(loggers)),
repo_sack(base),
rpm_package_sack(base),
#ifdef WITH_MODULEMD
module_sack(base),
#endif
transaction_history(base),
vars(base) {}
vars(base) {
}

void Base::lock() {
locked_base_mutex.lock();
Expand Down Expand Up @@ -233,9 +242,11 @@ void Base::setup() {
// dnf4 persistor from /etc/dnf/modules.d/
// Remove once reading of dnf4 data is not needed
libdnf5::dnf4convert::Dnf4Convert convertor(get_weak_ptr());
#ifdef WITH_MODULEMD
if ((!std::filesystem::exists(system_state.get_module_state_path()))) {
system_state.reset_module_states(convertor.read_module_states());
}
#endif

if (system_state.packages_import_required()) {
// TODO(mblaha) - first try dnf5 history database, then fall back to dnf4
Expand All @@ -260,7 +271,9 @@ void Base::setup() {
// (and force to recompute provides) or locked
const char * arch = vars->get_value("arch").c_str();
pool_setarch(**pool, arch);
#ifdef WITH_MODULEMD
p_impl->module_sack.p_impl->set_arch(arch);
#endif
pool_set_rootdir(**pool, installroot.get_value().c_str());

p_impl->plugins.post_base_setup();
Expand Down Expand Up @@ -303,7 +316,11 @@ transaction::TransactionHistoryWeakPtr Base::get_transaction_history() {
return p_impl->transaction_history.get_weak_ptr();
}
libdnf5::module::ModuleSackWeakPtr Base::get_module_sack() {
#ifdef WITH_MODULEMD
return p_impl->module_sack.get_weak_ptr();
#else
libdnf_throw_assertion("libdnf5 compiled without module support.");
#endif
}

VarsWeakPtr Base::get_vars() {
Expand Down
6 changes: 5 additions & 1 deletion libdnf5/base/base_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#ifndef LIBDNF5_BASE_BASE_IMPL_HPP
#define LIBDNF5_BASE_BASE_IMPL_HPP


#include "../advisory/advisory_sack.hpp"
#include "plugin/plugins.hpp"
#include "system/state.hpp"

#include "libdnf5/base/base.hpp"
#ifdef WITH_MODULEMD
#include "libdnf5/module/module_sack.hpp"
#endif


namespace libdnf5 {
Expand Down Expand Up @@ -90,7 +92,9 @@ class Base::Impl {
ConfigMain config;
repo::RepoSack repo_sack;
rpm::PackageSack rpm_package_sack;
#ifdef WITH_MODULEMD
module::ModuleSack module_sack;
#endif
std::map<std::string, std::string> variables;
transaction::TransactionHistory transaction_history;
Vars vars;
Expand Down
39 changes: 35 additions & 4 deletions libdnf5/base/goal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "advisory/advisory_package_private.hpp"
#include "base_private.hpp"
#ifdef WITH_MODULEMD
#include "module/module_goal_private.hpp"
#include "module/module_sack_impl.hpp"
#endif
#include "rpm/package_query_impl.hpp"
#include "rpm/package_sack_impl.hpp"
#include "rpm/package_set_impl.hpp"
Expand All @@ -38,7 +40,9 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "libdnf5/common/exception.hpp"
#include "libdnf5/comps/environment/query.hpp"
#include "libdnf5/comps/group/query.hpp"
#ifdef WITH_MODULEMD
#include "libdnf5/module/module_errors.hpp"
#endif
#include "libdnf5/rpm/package_query.hpp"
#include "libdnf5/rpm/reldep.hpp"
#include "libdnf5/utils/bgettext/bgettext-mark-domain.h"
Expand Down Expand Up @@ -106,7 +110,9 @@ class Goal::Impl {
GoalProblem resolve_group_specs(std::vector<GroupSpec> & specs, base::Transaction & transaction);
void add_resolved_group_specs_to_goal(base::Transaction & transaction);
void add_resolved_environment_specs_to_goal(base::Transaction & transaction);
#ifdef WITH_MODULEMD
GoalProblem add_module_specs_to_goal(base::Transaction & transaction);
#endif
GoalProblem add_serialized_transaction_to_goal(base::Transaction & transaction);
GoalProblem add_reason_change_specs_to_goal(base::Transaction & transaction);

Expand Down Expand Up @@ -232,16 +238,31 @@ Goal::~Goal() = default;

Goal::Impl::~Impl() = default;

void Goal::add_module_enable(const std::string & spec, const libdnf5::GoalJobSettings & settings) {
void Goal::add_module_enable(
[[maybe_unused]] const std::string & spec, [[maybe_unused]] const libdnf5::GoalJobSettings & settings) {
#ifdef WITH_MODULEMD
p_impl->module_specs.push_back(std::make_tuple(GoalAction::ENABLE, spec, settings));
#else
libdnf_throw_assertion("libdnf5 compiled without module support.");
#endif
}

void Goal::add_module_disable(const std::string & spec, const libdnf5::GoalJobSettings & settings) {
void Goal::add_module_disable(
[[maybe_unused]] const std::string & spec, [[maybe_unused]] const libdnf5::GoalJobSettings & settings) {
#ifdef WITH_MODULEMD
p_impl->module_specs.push_back(std::make_tuple(GoalAction::DISABLE, spec, settings));
#else
libdnf_throw_assertion("libdnf5 compiled without module support.");
#endif
}

void Goal::add_module_reset(const std::string & spec, const libdnf5::GoalJobSettings & settings) {
void Goal::add_module_reset(
[[maybe_unused]] const std::string & spec, [[maybe_unused]] const libdnf5::GoalJobSettings & settings) {
#ifdef WITH_MODULEMD
p_impl->module_specs.push_back(std::make_tuple(GoalAction::RESET, spec, settings));
#else
libdnf_throw_assertion("libdnf5 compiled without module support.");
#endif
}

void Goal::add_install(const std::string & spec, const libdnf5::GoalJobSettings & settings) {
Expand Down Expand Up @@ -602,6 +623,8 @@ GoalProblem Goal::Impl::add_specs_to_goal(base::Transaction & transaction) {
return ret;
}


#ifdef WITH_MODULEMD
GoalProblem Goal::Impl::add_module_specs_to_goal(base::Transaction & transaction) {
auto ret = GoalProblem::NO_PROBLEM;
module::ModuleSack & module_sack = *base->get_module_sack();
Expand Down Expand Up @@ -656,6 +679,7 @@ GoalProblem Goal::Impl::add_module_specs_to_goal(base::Transaction & transaction
}
return ret;
}
#endif

GoalProblem Goal::Impl::add_serialized_transaction_to_goal(base::Transaction & transaction) {
if (!serialized_transaction) {
Expand Down Expand Up @@ -2789,6 +2813,7 @@ base::Transaction Goal::resolve() {
sack->p_impl->make_provides_ready();


#ifdef WITH_MODULEMD
module::ModuleSack & module_sack = *p_impl->base->get_module_sack();
ret |= p_impl->add_module_specs_to_goal(transaction);

Expand Down Expand Up @@ -2828,6 +2853,7 @@ base::Transaction Goal::resolve() {
}

module_sack.p_impl->enable_dependent_modules();
#endif


// TODO(jmracek) Apply comps second or later
Expand Down Expand Up @@ -2922,7 +2948,12 @@ base::Transaction Goal::resolve() {
libdnf5::Logger::Level::WARNING);
}

transaction.p_impl->set_transaction(p_impl->rpm_goal, module_sack, ret);
transaction.p_impl->set_transaction(
p_impl->rpm_goal,
#ifdef WITH_MODULEMD
module_sack,
#endif
ret);

return transaction;
}
Expand Down
Loading

0 comments on commit 8a65cc2

Please sign in to comment.