Skip to content

Commit fe61995

Browse files
committed
Add availability to build Jinja2C++ as a shared library
1 parent 9069a2d commit fe61995

File tree

10 files changed

+95
-32
lines changed

10 files changed

+95
-32
lines changed

.github/workflows/linux-build.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
compiler: [gcc-8, gcc-6, gcc-7, gcc-5, gcc-9]
2424
base-flags: ["", -DJINJA2CPP_CXX_STANDARD=17]
2525
build-config: [Release, Debug]
26+
build-shared: [TRUE, FALSE]
2627

2728
include:
2829
- compiler: gcc-8
@@ -62,9 +63,11 @@ jobs:
6263
export EXTRA_FLAGS="${INPUT_BASE_FLAGS} ${INPUT_EXTRA_FLAGS}"
6364
6465
- name: Build
66+
env:
67+
INPUT_BUILD_SHARED: ${{ matrix.build-shared }}
6568
run: |
6669
mkdir -p .build && cd .build
67-
cmake $CMAKE_OPTS -DCMAKE_BUILD_TYPE=$BUILD_CONFIG -DCMAKE_CXX_FLAGS=$CMAKE_CXX_FLAGS -DJINJA2CPP_DEPS_MODE=internal $EXTRA_FLAGS .. && cmake --build . --config $BUILD_CONFIG --target all -- -j4
70+
cmake $CMAKE_OPTS -DCMAKE_BUILD_TYPE=$BUILD_CONFIG -DCMAKE_CXX_FLAGS=$CMAKE_CXX_FLAGS -DJINJA2CPP_DEPS_MODE=internal -DJINJA2CPP_BUILD_SHARED=$INPUT_BUILD_SHARED $EXTRA_FLAGS .. && cmake --build . --config $BUILD_CONFIG --target all -- -j4
6871
6972
- name: Test
7073
run: |

.github/workflows/windows-build.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
build-config: [Release, Debug]
2525
build-platform: [x86, x64]
2626
build-runtime: ["", /MT, /MD]
27+
build-shared: [TRUE, FALSE]
2728

2829
include:
2930
- compiler: msvc-2017
@@ -57,6 +58,7 @@ jobs:
5758
INPUT_COMPILER: ${{ matrix.compiler }}
5859
INPUT_BASE_FLAGS: ${{ matrix.base-flags }}
5960
INPUT_BUILD_CONFIG: ${{ matrix.build-config }}
61+
INPUT_BUILD_SHARED: ${{ matrix.build-shared }}
6062
INPUT_EXTRA_FLAGS: ${{ matrix.extra_flags }}
6163
INPUT_BUILD_PLATFORM: ${{ matrix.build-platform }}
6264
INPUT_BUILD_RUNTIME: ${{ matrix.build-runtime }}
@@ -66,7 +68,7 @@ jobs:
6668
call "%VC_VARS%"
6769
mkdir -p .build
6870
cd .build
69-
cmake .. -G "%INPUT_GENERATOR%" -DCMAKE_BUILD_TYPE=%INPUT_BUILD_CONFIG% -DJINJA2CPP_MSVC_RUNTIME_TYPE="%INPUT_BUILD_RUNTIME%" -DJINJA2CPP_DEPS_MODE=external-boost %INPUT_BASE_FLAGS% %INPUT_EXTRA_FLAGS%
71+
cmake .. -G "%INPUT_GENERATOR%" -DCMAKE_BUILD_TYPE=%INPUT_BUILD_CONFIG% -DJINJA2CPP_MSVC_RUNTIME_TYPE="%INPUT_BUILD_RUNTIME%" -DJINJA2CPP_DEPS_MODE=external-boost -DJINJA2CPP_BUILD_SHARED=%INPUT_BUILD_SHARED% %INPUT_BASE_FLAGS% %INPUT_EXTRA_FLAGS%
7072
cmake --build . --config %INPUT_BUILD_CONFIG%
7173
7274
- name: Test

CMakeLists.txt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ option(JINJA2CPP_STRICT_WARNINGS "Enable additional warnings and treat them as e
2121
option(JINJA2CPP_BUILD_SHARED "Build shared linkage version of Jinja2Cpp" OFF)
2222
option(JINJA2CPP_PIC "Control -fPIC option for library build" OFF)
2323

24+
if (DEFINED BUILD_SHARED_LIBS)
25+
set(JINJA2CPP_BUILD_SHARED BUILD_SHARED_LIBS)
26+
endif ()
27+
2428
if (NOT JINJA2CPP_DEPS_MODE)
2529
set (JINJA2CPP_DEPS_MODE "internal")
2630
endif ()
@@ -70,10 +74,15 @@ if(NOT ${JINJA2CPP_WITH_SANITIZERS} STREQUAL "none")
7074
endif()
7175

7276
if (UNIX)
73-
if (JINJA2CPP_PIC OR CONAN_CMAKE_POSITION_INDEPENDENT_CODE)
74-
set(GCC_CXX_FLAGS ${GCC_CXX_FLAGS} -fPIC)
75-
set(CLANG_CXX_FLAGS ${CLANG_CXX_FLAGS} -fPIC)
76-
endif ()
77+
if (JINJA2CPP_PIC OR CONAN_CMAKE_POSITION_INDEPENDENT_CODE)
78+
set(GCC_CXX_FLAGS ${GCC_CXX_FLAGS} -fPIC)
79+
set(CLANG_CXX_FLAGS ${CLANG_CXX_FLAGS} -fPIC)
80+
endif ()
81+
82+
if (DEFINED CONAN_SHARED_LINKER_FLAGS)
83+
set(GCC_CXX_FLAGS ${GCC_CXX_FLAGS} -Wl,${CONAN_SHARED_LINKER_FLAGS})
84+
set(CLANG_CXX_FLAGS ${CLANG_CXX_FLAGS} -Wl,${CONAN_SHARED_LINKER_FLAGS})
85+
endif ()
7786
else ()
7887
set(GCC_CXX_FLAGS ${GCC_CXX_FLAGS} "-Wa,-mbig-obj" -O1)
7988
if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
@@ -113,8 +122,10 @@ endif()
113122

114123
if (JINJA2CPP_BUILD_SHARED)
115124
set(LIB_LINK_TYPE SHARED)
125+
message(STATUS "Jinja2C++ library type: SHARED")
116126
else()
117127
set(LIB_LINK_TYPE STATIC)
128+
message(STATUS "Jinja2C++ library type: STATIC")
118129
endif()
119130

120131
include(collect_sources)
@@ -186,7 +197,11 @@ elseif (${CMAKE_CXX_COMPILER_ID} MATCHES "MSVC")
186197
target_compile_options(${LIB_TARGET_NAME} PRIVATE ${MSVC_CXX_FLAGS})
187198
endif ()
188199

189-
target_compile_definitions(${LIB_TARGET_NAME} PUBLIC BOOST_SYSTEM_NO_DEPRECATED BOOST_ERROR_CODE_HEADER_ONLY)
200+
target_compile_definitions(${LIB_TARGET_NAME} PUBLIC -DBOOST_SYSTEM_NO_DEPRECATED -DBOOST_ERROR_CODE_HEADER_ONLY)
201+
202+
if (JINJA2CPP_BUILD_SHARED)
203+
target_compile_definitions(${LIB_TARGET_NAME} PRIVATE -DJINJA2CPP_BUILD_AS_SHARED PUBLIC -DJINJA2CPP_LINK_AS_SHARED)
204+
endif ()
190205

191206
set_target_properties(${LIB_TARGET_NAME} PROPERTIES
192207
VERSION ${PROJECT_VERSION}

cmake/public/jinja2cpp-config.cmake.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ set_target_properties(jinja2cpp PROPERTIES
5757
INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
5858
)
5959

60+
if (JINJA2CPP_BUILD_SHARED)
61+
target_compile_definitions(jinja2cpp PUBLIC -DJINJA2CPP_LINK_AS_SHARED)
62+
else()
63+
64+
6065
# INTERFACE_LINK_LIBRARIES "nonstd::expected-lite;nonstd::variant-lite;nonstd::value_ptr-lite;nonstd::optional-lite;\$<LINK_ONLY:boost::variant>;\$<LINK_ONLY:boost::filesystem>;\$<LINK_ONLY:boost::algorithm>"
6166

6267
if(CMAKE_VERSION VERSION_LESS 2.8.12)

include/jinja2cpp/config.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef JINJA2CPP_CONFIG_H
2+
#define JINJA2CPP_CONFIG_H
3+
4+
// The Jinja2C++ library version in the form major * 10000 + minor * 100 + patch.
5+
#define JINJA2CPP_VERSION 10100
6+
7+
#ifdef _WIN32
8+
#define JINJA2_DECLSPEC(S) __declspec(S)
9+
#if _MSC_VER
10+
#pragma warning(disable : 4251)
11+
#endif
12+
#else
13+
#define JINJA2_DECLSPEC(S)
14+
#endif
15+
16+
#ifdef JINJA2CPP_BUILD_AS_SHARED
17+
#define JINJA2CPP_EXPORT JINJA2_DECLSPEC(dllexport)
18+
#define JINJA2CPP_SHARED_LIB
19+
#elif JINJA2CPP_LINK_AS_SHARED
20+
#define JINJA2CPP_EXPORT JINJA2_DECLSPEC(dllimport)
21+
#define JINJA2CPP_SHARED_LIB
22+
#else
23+
#define JINJA2CPP_EXPORT
24+
#define JINJA2CPP_STATIC_LIB
25+
#endif
26+
27+
#endif // JINJA2CPP_CONFIG_H

include/jinja2cpp/error_info.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#ifndef JINJA2CPP_ERROR_INFO_H
22
#define JINJA2CPP_ERROR_INFO_H
33

4+
#include "config.h"
45
#include "value.h"
56

67
#include <iostream>
7-
#include <vector>
88
#include <type_traits>
9+
#include <vector>
910

1011
namespace jinja2
1112
{
@@ -165,13 +166,10 @@ class ErrorInfoTpl
165166
*
166167
* @return Vector with extra error params
167168
*/
168-
auto& GetExtraParams() const
169-
{
170-
return m_errorData.extraParams;
171-
}
169+
auto& GetExtraParams() const { return m_errorData.extraParams; }
172170

173171
//! Convert error to the detailed string representation
174-
std::basic_string<CharT> ToString() const;
172+
JINJA2CPP_EXPORT std::basic_string<CharT> ToString() const;
175173

176174
private:
177175
Data m_errorData;
@@ -180,8 +178,8 @@ class ErrorInfoTpl
180178
using ErrorInfo = ErrorInfoTpl<char>;
181179
using ErrorInfoW = ErrorInfoTpl<wchar_t>;
182180

183-
std::ostream& operator << (std::ostream& os, const ErrorInfo& res);
184-
std::wostream& operator << (std::wostream& os, const ErrorInfoW& res);
181+
JINJA2CPP_EXPORT std::ostream& operator<<(std::ostream& os, const ErrorInfo& res);
182+
JINJA2CPP_EXPORT std::wostream& operator<<(std::wostream& os, const ErrorInfoW& res);
185183
} // jinja2
186184

187185
#endif // JINJA2CPP_ERROR_INFO_H

include/jinja2cpp/filesystem_handler.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#ifndef JINJA2CPP_FILESYSTEM_HANDLER_H
22
#define JINJA2CPP_FILESYSTEM_HANDLER_H
33

4-
#include <nonstd/variant.hpp>
4+
#include "config.h"
5+
56
#include <nonstd/optional.hpp>
7+
#include <nonstd/variant.hpp>
68

9+
#include <chrono>
710
#include <iostream>
811
#include <memory>
912
#include <string>
1013
#include <unordered_map>
11-
#include <chrono>
1214

1315
namespace jinja2
1416
{
@@ -26,7 +28,7 @@ using WCharFileStreamPtr = FileStreamPtr<wchar_t>;
2628
* So, the exact type (ex. `ifstream`, `istringstream` etc.) of input stream is unspecified. In order to delete stream object correctly returned pointer
2729
* provide the custom deleter which should properly delete the stream object.
2830
*/
29-
class IFilesystemHandler
31+
class JINJA2CPP_EXPORT IFilesystemHandler
3032
{
3133
public:
3234
//! Destructor
@@ -73,7 +75,7 @@ using FilesystemHandlerPtr = std::shared_ptr<IFilesystemHandler>;
7375
* This filesystem handler implements the simple dictionary object which maps name of the file to it's content. New files can be added by \ref AddFile
7476
* methods. Content of the files automatically converted to narrow/wide strings representation if necessary.
7577
*/
76-
class MemoryFileSystem : public IFilesystemHandler
78+
class JINJA2CPP_EXPORT MemoryFileSystem : public IFilesystemHandler
7779
{
7880
public:
7981
/*!
@@ -114,7 +116,7 @@ class MemoryFileSystem : public IFilesystemHandler
114116
* This filesystem handler is an interface to the real file system. Root directory for file name mapping provided as a constructor argument. Each name (path) of
115117
* the file to open is appended to the root directory path and then passed to the stream open methods.
116118
*/
117-
class RealFileSystem : public IFilesystemHandler
119+
class JINJA2CPP_EXPORT RealFileSystem : public IFilesystemHandler
118120
{
119121
public:
120122
/*!

include/jinja2cpp/template.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
#ifndef JINJA2_TEMPLATE_H
22
#define JINJA2_TEMPLATE_H
33

4+
#include "config.h"
45
#include "error_info.h"
56
#include "value.h"
67

78
#include <nonstd/expected.hpp>
89

9-
#include <string>
1010
#include <iostream>
1111
#include <memory>
12+
#include <string>
1213

1314
namespace jinja2
1415
{
15-
class ITemplateImpl;
16-
class TemplateEnv;
17-
template<typename CharT> class TemplateImpl;
16+
class JINJA2CPP_EXPORT ITemplateImpl;
17+
class JINJA2CPP_EXPORT TemplateEnv;
18+
template<typename CharT>
19+
class TemplateImpl;
1820
template<typename U>
1921
using Result = nonstd::expected<U, ErrorInfo>;
2022
template<typename U>
@@ -43,13 +45,16 @@ struct MetadataInfo
4345
* std::string result = tpl.RenderAsString(ValuesMap{}).value();
4446
* ```
4547
*/
46-
class Template
48+
class JINJA2CPP_EXPORT Template
4749
{
4850
public:
4951
/*!
5052
* \brief Default constructor
5153
*/
52-
Template() : Template(nullptr) {}
54+
Template()
55+
: Template(nullptr)
56+
{
57+
}
5358
/*!
5459
* \brief Initializing constructor
5560
*
@@ -165,13 +170,16 @@ class Template
165170
* std::string result = tpl.RenderAsString(ValuesMap{}).value();
166171
* ```
167172
*/
168-
class TemplateW
173+
class JINJA2CPP_EXPORT TemplateW
169174
{
170175
public:
171176
/*!
172177
* \brief Default constructor
173178
*/
174-
TemplateW() : TemplateW(nullptr) {}
179+
TemplateW()
180+
: TemplateW(nullptr)
181+
{
182+
}
175183
/*!
176184
* \brief Initializing constructor
177185
*

include/jinja2cpp/template_env.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#ifndef JINJA2CPP_TEMPLATE_ENV_H
22
#define JINJA2CPP_TEMPLATE_ENV_H
33

4+
#include "config.h"
45
#include "error_info.h"
5-
66
#include "filesystem_handler.h"
77
#include "template.h"
88

9-
#include <unordered_map>
109
#include <shared_mutex>
10+
#include <unordered_map>
1111

1212
namespace jinja2
1313
{
@@ -59,7 +59,7 @@ struct Settings
5959
* It's possible to load templates from the environment via \ref LoadTemplate or \ref LoadTemplateW methods
6060
* or to pass instance of the environment directly to the \ref Template via constructor.
6161
*/
62-
class TemplateEnv
62+
class JINJA2CPP_EXPORT TemplateEnv
6363
{
6464
public:
6565
using TimePoint = std::chrono::system_clock::time_point;

test/rapid_json_serializer_test.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <jinja2cpp/config.h>
2+
#ifndef JINJA2CPP_SHARED_LIB
13
#include "../src/rapid_json_serializer.h"
24
#include "gtest/gtest.h"
35

@@ -56,11 +58,12 @@ TEST(RapidJsonSerializerTest, SerializeComplexTypesWithIndention)
5658
const auto jsonValue = document.CreateValue(CreateMapAdapter(std::move(params)));
5759

5860
auto indentedDocument =
59-
R"({
61+
R"({
6062
"map": {
6163
"array": [1, 2, 3]
6264
}
6365
})";
6466

6567
EXPECT_EQ(indentedDocument, jsonValue.AsString(4));
6668
}
69+
#endif

0 commit comments

Comments
 (0)