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

Use build system libdir #95

Merged
merged 6 commits into from
Sep 25, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ project(
LANGUAGES CXX
VERSION ${VERSION_STRIPPED}
)
include(GNUInstallDirs)

add_subdirectory(src)

Expand Down
28 changes: 1 addition & 27 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,4 @@ cps_config = executable(
implicit_include_directories : false,
)

build_tests = get_option('tests')

python_interpreter = find_program('python', version : '>=3.11', required : build_tests, disabler : true)
foreach t : [['cps integration tests', 'cps-config.toml'], ['pkg-config compatibility', 'pkg-config-compat.toml']]
test(
t[0],
python_interpreter,
args: [files('tests/runner.py'), cps_config, 'tests/cases/' + t[1]],
protocol : 'tap',
env : {'CPS_PREFIX_PATH' : meson.current_source_dir() / 'tests' / 'cps-files' },
)
endforeach

dep_gtest = dependency('gtest_main', required : build_tests, disabler : true, allow_fallback : true)

foreach t : ['loader', 'version', 'utils']
test(
t,
executable(
f'@t@_test',
f'tests/@[email protected]',
dependencies : [dep_cps, dep_gtest, dep_fmt, dep_expected],
implicit_include_directories : false,
),
protocol : 'gtest',
)
endforeach
subdir('tests')
2 changes: 2 additions & 0 deletions src/cps/config.hpp.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#define CPS_CONFIG_VERSION "${CMAKE_PROJECT_VERSION}"
#define CPS_CONFIG_LIBDIR "${CMAKE_INSTALL_LIBDIR}"
#define CPS_CONFIG_DATADIR "${CMAKE_INSTALL_DATADIR}"
8 changes: 5 additions & 3 deletions src/cps/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// Copyright © 2024 Tyler Weaver
// SPDX-License-Identifier: MIT

#include "cps/env.hpp"

#include <cstdlib>
#include <string>

#include "cps/env.hpp"
#include "cps/utils.hpp"

namespace cps {

Env get_env() {
Expand All @@ -15,7 +16,8 @@ namespace cps {
env.cps_path = std::string(env_c);
}
if (const char * env_c = std::getenv("CPS_PREFIX_PATH")) {
env.cps_prefix_path = std::string(env_c);
// TODO: Windows
env.cps_prefix_path = utils::split(env_c, ":");
}
if (std::getenv("PKG_CONFIG_DEBUG_SPEW") || std::getenv("CPS_CONFIG_DEBUG_SPEW")) {
env.debug_spew = true;
Expand Down
3 changes: 2 additions & 1 deletion src/cps/env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

#include <optional>
#include <string>
#include <vector>

namespace cps {

struct Env {
std::optional<std::string> cps_path = std::nullopt;
std::optional<std::string> cps_prefix_path = std::nullopt;
std::optional<std::vector<std::string>> cps_prefix_path = std::nullopt;
bool debug_spew = false;
};

Expand Down
2 changes: 2 additions & 0 deletions src/cps/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

conf = configuration_data()
conf.set_quoted('CPS_CONFIG_VERSION', meson.project_version())
conf.set_quoted('CPS_CONFIG_LIBDIR', get_option('libdir'))
conf.set_quoted('CPS_CONFIG_DATADIR', get_option('datadir'))

conf_h = configure_file(
configuration : conf,
Expand Down
5 changes: 3 additions & 2 deletions src/cps/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// Copyright © 2024 Dylan Baker

#include "cps/platform.hpp"
#include "cps/config.hpp"

namespace cps::platform {

fs::path libdir() { return "lib"; }
fs::path libdir() { return CPS_CONFIG_LIBDIR; }

fs::path datadir() { return "share"; }
fs::path datadir() { return CPS_CONFIG_DATADIR; }

} // namespace cps::platform
57 changes: 34 additions & 23 deletions src/cps/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ namespace cps::search {
}

if (env.cps_prefix_path) {
auto && prefixes = utils::split(env.cps_prefix_path.value());
auto && prefixes = env.cps_prefix_path.value();
for (auto && p : prefixes) {
auto && paths = expand_prefix(p);
cached_paths.reserve(cached_paths.size() + paths.size());
Expand Down Expand Up @@ -309,7 +309,8 @@ namespace cps::search {
}
}

fs::path calculate_prefix(const std::optional<std::string> & path, const fs::path & filename) {
tl::expected<fs::path, std::string> calculate_prefix(const std::optional<std::string> & path,
const fs::path & filename) {
// TODO: Windows
// TODO: /cps/<name-like>
if (path) {
Expand All @@ -319,33 +320,43 @@ namespace cps::search {
}
fs::path f = filename.parent_path();
while (p != "@prefix@") {
utils::assert_fn(
p.stem() == f.stem(),
fmt::format("filepath and cps_path have non overlapping stems, prefix: {}, filename {}",
std::string{p}, std::string{f}));
if (p.stem() != f.stem()) {
return tl::unexpected(
fmt::format("filepath and cps_path have non overlapping stems, prefix: {}, filename {}",
std::string{p}, std::string{f}));
}
p = p.parent_path();
f = f.parent_path();
}
return f;
}

std::vector<std::string> split = utils::split(std::string{filename.parent_path()}, "/");
if (split.back() == "cps") {
split.pop_back();
}
if (split.back() == "share") {
split.pop_back();
}
// TODO: this needs to be generic
if (split.back() == "lib") {
split.pop_back();
}
fs::path p = filename.parent_path();

const auto reducer = [&p](fs::path dir) -> std::optional<fs::path> {
fs::path np = p;

// remove a trailing slash
if (dir.stem() == "") {
dir = dir.parent_path();
}

while (dir.stem() == np.stem()) {
dir = dir.parent_path();
np = np.parent_path();
}

fs::path p{"/"};
for (auto && s : split) {
p /= s;
// If our new path has changed and we have consumed the entire
// directory, then return that, otherwise this was not
// successful.
return np != p && dir == dir.root_path() ? std::optional{np} : std::nullopt;
};

if (p.stem() == "cps") {
p = p.parent_path();
}
return p;

return reducer(platform::libdir()).value_or(reducer(platform::datadir()).value_or(p));
}

/// @brief Calculate the required components in the graph
Expand Down Expand Up @@ -418,8 +429,8 @@ namespace cps::search {

for (auto && node : flat) {

const auto prefix =
prefix_path.value_or(calculate_prefix(node->data.package.cps_path, node->data.package.filename));
const auto prefix = prefix_path.value_or(
CPS_TRY(calculate_prefix(node->data.package.cps_path, node->data.package.filename)));

const auto && prefix_replacer = [&](const std::string & s) -> std::string {
// TODO: Windows…
Expand Down
20 changes: 19 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ foreach (test_name test_case IN ZIP_LISTS test_names test_cases)
COMMAND
${CMAKE_COMMAND} -E env CPS_PREFIX_PATH=${CMAKE_CURRENT_SOURCE_DIR}/cps-files
${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/runner.py
$<TARGET_FILE:cps-config> ${test_case}
$<TARGET_FILE:cps-config> ${test_case} --libdir "${CMAKE_INSTALL_LIBDIR}"
)
endforeach ()

set(infiles
"cps-files/lib/cps/cps-path-not-set.cps"
"cps-files/lib/cps/cps-path-set.cps")
set(libdir ${CMAKE_INSTALL_LIBDIR})
set(prefix "@prefix@") # Work around for not having the inverse of @ONLY
foreach(infile ${infiles})
configure_file("${infile}.in" "${infile}")
endforeach()

add_test(
NAME "prefix calculation tests"
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMAND
${CMAKE_COMMAND} -E env CPS_PREFIX_PATH=${CMAKE_CURRENT_BINARY_DIR}/cps-files
${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/runner.py
$<TARGET_FILE:cps-config> ${CMAKE_CURRENT_SOURCE_DIR}/cases/cps-prefix-calculation.toml --libdir "${CMAKE_INSTALL_LIBDIR}" --prefix ${PROJECT_BINARY_DIR}
)
18 changes: 0 additions & 18 deletions tests/cases/cps-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,6 @@ cps = "multiple-components"
args = ["flags", "--cflags-only-I", "--component", "requires-external"]
expected = "-I/err"

[[case]]
name = "prefix set"
cps = "cps-path-set"
args = ["flags", "--cflags-only-I", "--libs-only-l"]
expected = "-I{prefix}/err -l{prefix}/lib/libfoo.a"

[[case]]
name = "prefix set (called by path)"
cps = "{prefix}/cps-path-set.cps"
args = ["flags", "--cflags-only-I", "--libs-only-l"]
expected = "-I{prefix}/err -l{prefix}/lib/libfoo.a"

[[case]]
name = "prefix calculated"
cps = "cps-path-not-set"
args = ["flags", "--cflags-only-I", "--libs-only-l"]
expected = "-I{prefix}/err -l{prefix}/lib/libfoo.a"

[[case]]
name = "component diamond"
cps = "diamond"
Expand Down
17 changes: 17 additions & 0 deletions tests/cases/cps-prefix-calculation.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[[case]]
name = "prefix set"
cps = "cps-path-set"
args = ["flags", "--cflags-only-I", "--libs-only-l", "--print-errors"]
expected = "-I{prefix}/err -l{prefix}/{libdir}/libfoo.a"

[[case]]
name = "prefix set (called by path)"
cps = "{prefix}/cps-path-set.cps"
args = ["flags", "--cflags-only-I", "--libs-only-l", "--print-errors"]
expected = "-I{prefix}/err -l{prefix}/{libdir}/libfoo.a"

[[case]]
name = "prefix calculated"
cps = "cps-path-not-set"
args = ["flags", "--cflags-only-I", "--libs-only-l"]
expected = "-I{prefix}/err -l{prefix}/{libdir}/libfoo.a"
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@prefix@/err"
]
},
"location": "@prefix@/lib/libfoo.a"
"location": "@prefix@/${libdir}/libfoo.a"
}
},
"default_components": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cps-path-set",
"cps_version": "0.10.0",
"cps_path": "@prefix@/lib/cps/",
"cps_path": "@prefix@/${libdir}/cps/",
"version": "1.0.0",
"components": {
"default": {
Expand All @@ -11,7 +11,7 @@
"@prefix@/err"
]
},
"location": "@prefix@/lib/libfoo.a"
"location": "@prefix@/${libdir}/libfoo.a"
}
},
"default_components": [
Expand Down
16 changes: 16 additions & 0 deletions tests/cps-files/lib/cps/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# SPDX-License-Identifier: MIT
# Copyright © 2024 Dylan Baker

conf = configuration_data()
conf.set('libdir', get_option('libdir'))
# Should be required, but: https://github.com/mesonbuild/meson/issues/13665
conf.set('prefix', '@prefix@')

foreach infile : ['cps-path-set.cps.in', 'cps-path-not-set.cps.in']
configure_file(
configuration : conf,
input : infile,
output : '@BASENAME@',
format : 'cmake',
)
endforeach
49 changes: 49 additions & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# SPDX-License-Identifier: MIT
# Copyright © 2024 Dylan Baker

subdir('cps-files/lib/cps')

build_tests = get_option('tests')

python_interpreter = find_program('python', version : '>=3.11', required : build_tests, disabler : true)
runner_args = [
files('runner.py'),
cps_config,
'--libdir', get_option('libdir'),
]

foreach t : [['cps integration tests', 'cps-config.toml'], ['pkg-config compatibility', 'pkg-config-compat.toml']]
test(
t[0],
python_interpreter,
args: [runner_args, meson.current_source_dir() / 'cases' / t[1]],
protocol : 'tap',
env : {'CPS_PREFIX_PATH' : meson.current_source_dir() / 'cps-files' },
)
endforeach
test(
'prefix calculation tests',
python_interpreter,
args: [
runner_args,
'--prefix', meson.project_build_root(),
meson.current_source_dir() / 'cases' / 'cps-prefix-calculation.toml',
],
protocol : 'tap',
env : {'CPS_PREFIX_PATH' : meson.current_build_dir() / 'cps-files' },
)

dep_gtest = dependency('gtest_main', required : build_tests, disabler : true, allow_fallback : true)

foreach t : ['loader', 'version', 'utils']
test(
t,
executable(
f'@t@_test',
f'@[email protected]',
dependencies : [dep_cps, dep_gtest, dep_fmt, dep_expected],
implicit_include_directories : false,
),
protocol : 'gtest',
)
endforeach
Loading
Loading