Skip to content

Commit

Permalink
Move resources_path() back into lager core, but make it optional for …
Browse files Browse the repository at this point in the history
…relocatability
  • Loading branch information
ltjax committed Nov 21, 2019
1 parent 4d6378e commit c7f26db
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 41 deletions.
17 changes: 16 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ endif()
option(lager_BUILD_TESTS "Build tests" ON)
option(lager_BUILD_EXAMPLES "Build examples" ON)
option(lager_BUILD_DOCS "Build docs" ON)
option(lager_EMBED_RESOURCES_PATH "Embed installation paths for easier, non-portable resource location" ON)

if ((NOT lager_BUILD_EXAMPLES) AND lager_BUILD_TESTS)
message(FATA_ERROR "Examples require embedded resources path")
endif()

find_program(CCACHE ccache)
if (CCACHE)
Expand Down Expand Up @@ -100,5 +105,15 @@ if (lager_BUILD_DOCS)
add_subdirectory(doc)
endif()

# Also configure and install the resources_path.hpp file if wanted
if (lager_EMBED_RESOURCES_PATH)
set(LAGER_PREFIX_PATH ${CMAKE_INSTALL_PREFIX})
configure_file(lager/resources_path.hpp.in
${CMAKE_BINARY_DIR}/include/resources_path.hpp)
target_include_directories(lager
INTERFACE $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/>)
install(FILES ${CMAKE_BINARY_DIR}/include/resources_path.hpp DESTINATION include)
endif()

install(EXPORT LagerConfig DESTINATION lib/cmake/Lager)
install(DIRECTORY lager DESTINATION include)
install(DIRECTORY lager DESTINATION include PATTERN "*.hpp")
7 changes: 0 additions & 7 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
add_custom_target(examples COMMENT "Build all examples")
add_dependencies(check examples)

# inject LAGER_PREFIX_PATH into examples
add_library(lager-prefix INTERFACE)
target_compile_definitions(lager-prefix INTERFACE LAGER_PREFIX_PATH="\\"${CMAKE_INSTALL_PREFIX}\\"")

# std examples
# ====================
Expand All @@ -28,7 +25,6 @@ set(counter_ncurses_include_directories
${CURSES_INCLUDE_DIR})
set(counter_ncurses_link_libraries
lager-debugger
lager-prefix
${CURSES_LIBRARIES})

add_executable(counter-ncurses EXCLUDE_FROM_ALL ${counter_ncurses_sources})
Expand Down Expand Up @@ -63,7 +59,6 @@ if (SDL2_FOUND AND SDL2_ttf_FOUND)
${SDL2_TTF_INCLUDE_DIR})
target_link_libraries(counter-sdl2
lager-debugger
lager-prefix
${SDL2_LIBRARY}
${SDL2_TTF_LIBRARIES})
add_dependencies(examples counter-sdl2)
Expand All @@ -76,7 +71,6 @@ if (SDL2_FOUND AND SDL2_ttf_FOUND)
${SDL2_TTF_INCLUDE_DIR})
target_link_libraries(autopong
lager-debugger
lager-prefix
${SDL2_LIBRARY}
${SDL2_TTF_LIBRARIES})
add_dependencies(examples autopong)
Expand All @@ -90,7 +84,6 @@ if (SDL2_FOUND AND SDL2_ttf_FOUND)
${SDL2_TTF_INCLUDE_DIR})
target_link_libraries(autopong-debug
lager-debugger
lager-prefix
${SDL2_LIBRARY}
${SDL2_TTF_LIBRARIES})
add_dependencies(examples autopong-debug)
Expand Down
15 changes: 3 additions & 12 deletions example/autopong/sdl2/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,20 @@
#include <lager/debug/http_server.hpp>
#include <lager/event_loop/sdl.hpp>
#include <lager/store.hpp>
#include <lager/resources_path.hpp>

#include <cmath>
#include <iostream>
#include <string>
#include <variant>

#ifndef LAGER_PREFIX_PATH
#error LAGER_PREFIX_PATH needs to be defined for examples
#endif

constexpr auto font_size = 32;

inline const char* resources_path()
{
auto env_resources_path = std::getenv("LAGER_RESOURCES_PATH");
return env_resources_path ? env_resources_path
: LAGER_PREFIX_PATH "/share/lager";
}

std::string font_path()
{
using namespace std::string_literals;
return resources_path() + "/SourceSansPro-Bold.ttf"s;
return lager::resources_path() + "/SourceSansPro-Bold.ttf"s;
}

struct sdl_view
Expand Down Expand Up @@ -154,7 +145,7 @@ int main(int argc, const char** argv)
SDL_SetRelativeMouseMode(SDL_TRUE);

#ifdef DEBUGGER
auto debugger = lager::http_debug_server{argc, argv, 8080, resources_path()};
auto debugger = lager::http_debug_server{argc, argv, 8080, lager::resources_path()};
#endif
auto view = sdl_view{};
auto loop = lager::sdl_event_loop{};
Expand Down
12 changes: 3 additions & 9 deletions example/counter/ncurses/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <lager/debug/tree_debugger.hpp>
#include <lager/event_loop/boost_asio.hpp>
#include <lager/store.hpp>
#include <lager/resources_path.hpp>

#include <zug/compose.hpp>

Expand All @@ -27,13 +28,6 @@

using namespace std::string_literals;

inline const char* resources_path()
{
auto env_resources_path = std::getenv("LAGER_RESOURCES_PATH");
return env_resources_path ? env_resources_path
: LAGER_PREFIX_PATH "/share/lager";
}

void draw(const counter::model& c)
{
static const auto prelude = "current value is "s;
Expand Down Expand Up @@ -72,10 +66,10 @@ int main(int argc, const char** argv)
::init_pair(2, COLOR_WHITE, COLOR_RED);
::init_pair(3, COLOR_WHITE, COLOR_BLUE);
#if defined(DEBUGGER) || defined(TREE_DEBUGGER)
auto debugger = lager::http_debug_server{argc, argv, 8080, resources_path()};
auto debugger = lager::http_debug_server{argc, argv, 8080, lager::resources_path()};
#endif
#ifdef META_DEBUGGER
auto meta_debugger = lager::http_debug_server{argc, argv, 8081, resources_path()};
auto meta_debugger = lager::http_debug_server{argc, argv, 8081, lager::resources_path()};
#endif
auto store = lager::make_store<counter::action>(
counter::model{},
Expand Down
14 changes: 2 additions & 12 deletions example/counter/sdl2/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,15 @@
#include "../counter.hpp"
#include <lager/event_loop/sdl.hpp>
#include <lager/store.hpp>
#include <lager/resources_path.hpp>

#include <iostream>
#include <string>

#ifndef LAGER_PREFIX_PATH
#error LAGER_PREFIX_PATH needs to be defined for examples
#endif

inline const char* resources_path()
{
auto env_resources_path = std::getenv("LAGER_RESOURCES_PATH");
return env_resources_path ? env_resources_path
: LAGER_PREFIX_PATH "/share/lager";
}

std::string font_path()
{
using namespace std::string_literals;
return resources_path() + "/SourceSansPro-Regular.ttf"s;
return lager::resources_path() + "/SourceSansPro-Regular.ttf"s;
}

struct sdl_view
Expand Down
26 changes: 26 additions & 0 deletions lager/resources_path.hpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// lager - library for functional interactive c++ programs
// Copyright (C) 2017 Juan Pedro Bolivar Puente
//
// This file is part of lager.
//
// lager is free software: you can redistribute it and/or modify
// it under the terms of the MIT License, as detailed in the LICENSE
// file located at the root of this source code distribution,
// or here: <https://github.com/arximboldi/lager/blob/master/LICENSE>
//

#pragma once

#include <cstdlib>

namespace lager {

inline const char* resources_path()
{
auto env_resources_path = std::getenv("LAGER_RESOURCES_PATH");
return env_resources_path ? env_resources_path
: "@LAGER_PREFIX_PATH@" "/share/lager";
}

}

0 comments on commit c7f26db

Please sign in to comment.