Skip to content
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.17)
project(cppship VERSION 0.8.1)
project(cppship VERSION 0.8.2)

# cpp std
set(CMAKE_CXX_STANDARD 20)
Expand Down
2 changes: 1 addition & 1 deletion cppship.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cppship"
version = "0.8.1"
version = "0.8.2"
authors = []
std = 20

Expand Down
11 changes: 10 additions & 1 deletion include/cppship/cmake/naming.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@ namespace cppship::cmake {

class NameTargetMapper {
public:
explicit NameTargetMapper(std::string_view package)
: mPackage(package)
{
}

std::string binary(std::string_view name);
std::string test(std::string_view name);
std::string example(std::string_view name);
std::string bench(std::string_view name);

private:
std::string mPackage;
};

}
}
2 changes: 1 addition & 1 deletion include/cppship/cmd/bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace cppship::cmd {

struct BenchOptions {
Profile profile = Profile::release;
std::optional<std::string> target;
std::optional<std::string> name;
std::optional<std::string> package;
};

Expand Down
2 changes: 1 addition & 1 deletion include/cppship/cmd/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace cppship::cmd {

struct TestOptions {
Profile profile = Profile::debug;
std::optional<std::string> target;
std::optional<std::string> name;
std::optional<std::string> package;
std::optional<std::string> name_regex;
bool rerun_failed = false;
Expand Down
7 changes: 5 additions & 2 deletions include/cppship/core/layout.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#pragma once

#include "cppship/util/fs.h"
#include <map>
#include <optional>
#include <set>
#include <vector>

#include "cppship/util/fs.h"

namespace cppship {

struct Target {
Expand All @@ -18,6 +19,8 @@ class Layout {
public:
Layout(const fs::path& root, std::string_view name);

std::string_view package() const { return mName; }

std::set<fs::path> all_files() const;

std::optional<Target> lib() const;
Expand Down Expand Up @@ -56,4 +59,4 @@ class Layout {
std::map<std::string, Target, std::less<>> mTests;
};

}
}
3 changes: 2 additions & 1 deletion include/cppship/core/workspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ class Workspace {

auto layouts() const { return ranges::views::values(packages_); }

const Layout* layout(std::string_view package) const;

private:
fs::path root_;

// if there is only one package, the key is fs::path{}
std::map<fs::path, Layout> packages_;
};

// Workspace is not full supported now, use this to enforce default layout.
const Layout& enforce_default_package(const Workspace& workspace);

}
23 changes: 15 additions & 8 deletions lib/cmake/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,12 @@ void CmakeGenerator::add_app_sources_()
boost::replace_all_copy(boost::to_upper_copy(std::string { mName }), "-", "_")),
};

NameTargetMapper mapper(mName);
for (const auto& bin : mLayout->binaries()) {
const auto target = mapper.binary(bin.name);
cmake::CmakeBin gen({
.name = bin.name,
.name = target,
.name_alias = bin.name,
.sources = bin.sources,
.lib = mLib,
.deps = mDeps,
Expand All @@ -208,7 +211,7 @@ void CmakeGenerator::add_app_sources_()
});

gen.build(mOut);
mBinaryTargets.emplace(bin.name);
mBinaryTargets.emplace(target);
}
}

Expand Down Expand Up @@ -250,7 +253,7 @@ void CmakeGenerator::add_benches_()
find_package(benchmark REQUIRED)
)";

NameTargetMapper mapper;
NameTargetMapper mapper(mName);
auto deps = mDevDeps;
deps.push_back({
.cmake_package = "benchmark",
Expand All @@ -264,6 +267,7 @@ find_package(benchmark REQUIRED)
.sources = bin.sources,
.lib = mLib,
.deps = deps,
.runtime_dir = "benches",
});

gen.build(mOut);
Expand All @@ -278,13 +282,12 @@ void CmakeGenerator::add_examples_()
return;
}

NameTargetMapper mapper;
NameTargetMapper mapper(mName);
for (const auto& bin : examples) {
const auto target = mapper.example(bin.name);

cmake::CmakeBin gen({
.name = target,
.name_alias = bin.name,
.sources = bin.sources,
.lib = mLib,
.deps = mDevDeps,
Expand All @@ -308,13 +311,17 @@ void CmakeGenerator::add_test_sources_()
find_package(GTest REQUIRED)
)";

NameTargetMapper mapper;
NameTargetMapper mapper(mName);
for (const auto& test : tests) {
const auto target = mapper.test(test.name);

mOut << '\n'
<< fmt::format("add_executable({} {})\n", target, test.sources.begin()->generic_string())
<< fmt::format("target_link_libraries({} PRIVATE GTest::gtest_main)\n", target);
<< fmt::format("target_link_libraries({} PRIVATE GTest::gtest_main)\n", target)
<< fmt::format(
R"(set_target_properties({} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${{CMAKE_BINARY_DIR}}/tests"))",
target)
<< '\n';

if (mLib) {
mOut << fmt::format("target_link_libraries({} PRIVATE {})\n", target, *mLib);
Expand All @@ -323,7 +330,7 @@ find_package(GTest REQUIRED)
mOut << fmt::format("target_link_libraries({} PRIVATE {})\n", target, boost::join(dep.cmake_targets, " "));
}

mOut << fmt::format("add_test({} {})\n", target, target);
mOut << fmt::format("add_test(NAME {} COMMAND {})\n", target, target);
mOut << fmt::format("set_tests_properties({} PROPERTIES LABELS {})\n", target, mName);

mTestTargets.emplace(target);
Expand Down
8 changes: 5 additions & 3 deletions lib/cmake/naming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

using namespace cppship::cmake;

std::string NameTargetMapper::test(std::string_view name) { return fmt::format("{}_test", name); }
std::string NameTargetMapper::binary(std::string_view name) { return fmt::format("{}_bin", name); }

std::string NameTargetMapper::example(std::string_view name) { return fmt::format("{}_example", name); }
std::string NameTargetMapper::test(std::string_view name) { return fmt::format("{}_{}_test", mPackage, name); }

std::string NameTargetMapper::bench(std::string_view name) { return fmt::format("{}_bench", name); }
std::string NameTargetMapper::example(std::string_view name) { return fmt::format("{}_{}_example", mPackage, name); }

std::string NameTargetMapper::bench(std::string_view name) { return fmt::format("{}_{}_bench", mPackage, name); }
27 changes: 17 additions & 10 deletions lib/cmd/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

#include <boost/process/system.hpp>
#include <gsl/narrow>
#include <range/v3/algorithm/all_of.hpp>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view.hpp>
#include <range/v3/view/filter.hpp>
#include <spdlog/spdlog.h>

#include "cppship/cmake/msvc.h"
Expand All @@ -24,7 +23,7 @@ namespace {

int run_one_bench(const cmd::BuildContext& ctx, const std::string_view bench)
{
const auto bin = ctx.profile_dir / msvc::fix_bin_path(ctx, bench);
const auto bin = ctx.profile_dir / kBenchesPath / msvc::fix_bin_path(ctx, bench);
const auto cmd = bin.string();
status("bench", "{}", cmd);
return boost::process::system(cmd);
Expand All @@ -34,17 +33,23 @@ int run_one_bench(const cmd::BuildContext& ctx, const std::string_view bench)

int cmd::run_bench(const BenchOptions& options)
{
NameTargetMapper mapper;

BuildContext ctx(options.profile);
BuildOptions build_options { .profile = options.profile };
if (options.target) {
if (ranges::all_of(
ctx.workspace.layouts(), [&](const auto& layout) { return !layout.bench(*options.target); })) {
throw Error { fmt::format("bench `{}` not found", *options.target) };
if (options.name) {
const auto layouts = ctx.workspace.layouts()
| filter([&](const Layout& layout) { return layout.bench(*options.name).has_value(); })
| ranges::to<std::vector>();
if (layouts.empty()) {
throw Error { fmt::format("bench `{}` not found", *options.name) };
}
if (layouts.size() > 1) {
throw Error { fmt::format("too many benches with name {}, eg. {}, {}",
*options.name,
layouts.front().package(),
layouts.back().package()) };
}

build_options.cmake_target = mapper.bench(*options.target);
build_options.cmake_target = NameTargetMapper(layouts.front().package()).bench(*options.name);
} else {
build_options.package = options.package;
build_options.groups.insert(BuildGroup::benches);
Expand All @@ -61,6 +66,8 @@ int cmd::run_bench(const BenchOptions& options)

for (const auto& layout : ctx.workspace.layouts()) {
for (const auto& bench : layout.benches()) {
NameTargetMapper mapper(layout.package());

const int res = run_one_bench(ctx, mapper.bench(bench.name));
if (res != 0) {
result = res;
Expand Down
3 changes: 3 additions & 0 deletions lib/cmd/build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ bool cmd::BuildContext::is_expired(const fs::path& path) const
std::optional<std::string> cmd::BuildContext::get_active_package() const
{
if (package_root == root) {
if (const auto* p = manifest.get_if_package()) {
return std::make_optional<std::string>(p->name());
}
return std::nullopt;
}

Expand Down
7 changes: 6 additions & 1 deletion lib/cmd/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <range/v3/view/transform.hpp>
#include <spdlog/spdlog.h>

#include "cppship/cmake/naming.h"
#include "cppship/cmd/build.h"
#include "cppship/core/layout.h"
#include "cppship/core/manifest.h"
Expand Down Expand Up @@ -54,7 +55,11 @@ int cmd::run_install([[maybe_unused]] const InstallOptions& options)
throw InvalidCmdOption { "--bin", fmt::format("specified binary {} is not found", *options.binary) };
}

const int result = run_build({ .profile = options.profile, .cmake_target = options.binary });
cmake::NameTargetMapper mapper(layout.package());
const int result = run_build({
.profile = options.profile,
.cmake_target = options.binary.has_value() ? std::make_optional(mapper.binary(*options.binary)) : std::nullopt,
});
if (result != 0) {
return EXIT_FAILURE;
}
Expand Down
Loading